fix: add error handling for binary.Write and remove unused variable
This commit is contained in:
@@ -96,10 +96,18 @@ func processChunks(data []byte) ([][]byte, []byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fullChunk := bytes.NewBuffer(nil)
|
fullChunk := bytes.NewBuffer(nil)
|
||||||
binary.Write(fullChunk, binary.BigEndian, chunkLength)
|
if err := binary.Write(fullChunk, binary.BigEndian, chunkLength); err != nil {
|
||||||
fullChunk.Write(chunkType)
|
return nil, nil, fmt.Errorf("error writing chunk length: %w", err)
|
||||||
fullChunk.Write(chunkData)
|
}
|
||||||
fullChunk.Write(crc)
|
if _, err := fullChunk.Write(chunkType); err != nil {
|
||||||
|
return nil, nil, fmt.Errorf("error writing chunk type: %w", err)
|
||||||
|
}
|
||||||
|
if _, err := fullChunk.Write(chunkData); err != nil {
|
||||||
|
return nil, nil, fmt.Errorf("error writing chunk data: %w", err)
|
||||||
|
}
|
||||||
|
if _, err := fullChunk.Write(crc); err != nil {
|
||||||
|
return nil, nil, fmt.Errorf("error writing CRC: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
switch string(chunkType) {
|
switch string(chunkType) {
|
||||||
case "IEND":
|
case "IEND":
|
||||||
@@ -128,10 +136,18 @@ func createTextChunk(embed PngEmbed) []byte {
|
|||||||
crc.Write(data)
|
crc.Write(data)
|
||||||
|
|
||||||
chunk := bytes.NewBuffer(nil)
|
chunk := bytes.NewBuffer(nil)
|
||||||
binary.Write(chunk, binary.BigEndian, uint32(len(data)))
|
if err := binary.Write(chunk, binary.BigEndian, uint32(len(data))); err != nil {
|
||||||
chunk.Write([]byte(textChunkType))
|
return nil, fmt.Errorf("error writing chunk length: %w", err)
|
||||||
chunk.Write(data)
|
}
|
||||||
binary.Write(chunk, binary.BigEndian, crc.Sum32())
|
if _, err := chunk.Write([]byte(textChunkType)); err != nil {
|
||||||
|
return nil, fmt.Errorf("error writing chunk type: %w", err)
|
||||||
|
}
|
||||||
|
if _, err := chunk.Write(data); err != nil {
|
||||||
|
return nil, fmt.Errorf("error writing chunk data: %w", err)
|
||||||
|
}
|
||||||
|
if err := binary.Write(chunk, binary.BigEndian, crc.Sum32()); err != nil {
|
||||||
|
return nil, fmt.Errorf("error writing CRC: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
return chunk.Bytes()
|
return chunk.Bytes()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,7 +22,6 @@ const (
|
|||||||
writeHeader = "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A"
|
writeHeader = "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A"
|
||||||
)
|
)
|
||||||
|
|
||||||
var tEXtChunkDataSpecification = "%s\x00%s"
|
|
||||||
|
|
||||||
type PngEmbed struct {
|
type PngEmbed struct {
|
||||||
Key string
|
Key string
|
||||||
|
|||||||
Reference in New Issue
Block a user