Fix resulting image only containing chunk data

develop
trivernis 5 years ago
parent c8fbb62f07
commit fe56f9adfe

@ -1,18 +1,18 @@
package main package main
import ( import (
"bufio"
"crypto/aes"
"crypto/cipher" "crypto/cipher"
"crypto/rand" "crypto/rand"
"encoding/base64" "encoding/base64"
"encoding/binary"
"flag" "flag"
"fmt" "fmt"
"hash/crc32"
"io" "io"
"os"
"log" "log"
"crypto/aes" "os"
"bufio"
"hash/crc32"
"encoding/binary"
) )
type ChunkData struct { type ChunkData struct {
@ -20,6 +20,7 @@ type ChunkData struct {
name string name string
data []byte data []byte
crc uint32 crc uint32
raw []byte
} }
var filename string var filename string
@ -44,7 +45,7 @@ func main() {
} }
for chunk.name != "IDAT" { for chunk.name != "IDAT" {
fmt.Printf("l: %d, n: %s, c: %d\n", chunk.length, chunk.name, chunk.crc) fmt.Printf("l: %d, n: %s, c: %d\n", chunk.length, chunk.name, chunk.crc)
_, _ = fout.Write(chunk.data) _, _ = fout.Write(chunk.raw)
chunk, err = readChunk(f) chunk, err = readChunk(f)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
@ -59,9 +60,9 @@ func main() {
panic(err) panic(err)
} }
cryptChunk := createChunk(inputData, "crPt") cryptChunk := createChunk(inputData, "crPt")
_, _ = fout.Write(cryptChunk.data) _, _ = fout.Write(cryptChunk.raw)
for { for {
_, _ = fout.Write(chunk.data) _, _ = fout.Write(chunk.raw)
chunk, err = readChunk(f) chunk, err = readChunk(f)
if err != nil { if err != nil {
break break
@ -101,11 +102,17 @@ func readChunk(f *os.File) (ChunkData, error) {
_, err = f.Read(data) _, err = f.Read(data)
_, err = f.Read(crcRaw) _, err = f.Read(crcRaw)
crc := binary.BigEndian.Uint32(crcRaw) crc := binary.BigEndian.Uint32(crcRaw)
fullData := make([]byte, 0)
fullData = append(fullData, lengthRaw...)
fullData = append(fullData, nameRaw...)
fullData = append(fullData, data...)
fullData = append(fullData, crcRaw...)
return ChunkData{ return ChunkData{
length: length, length: length,
name: name, name: name,
data: data, data: data,
crc: crc, crc: crc,
raw: fullData,
}, err }, err
} }
@ -127,8 +134,9 @@ func createChunk(data []byte, name string) ChunkData {
return ChunkData{ return ChunkData{
length: uint32(len(data)), length: uint32(len(data)),
name: name, name: name,
data: fullData, data: data,
crc: crc, crc: crc,
raw: fullData,
} }
} }

Loading…
Cancel
Save