package flashpart import "encoding/binary" type FlashPartHeader struct { MediaID byte Byte1 byte Byte2 byte Byte3 byte TotalSize uint32 RawSize uint32 // total - 32 VersionText string // 16byte raw []byte } func ParseFlashPartHeader(data []byte) (fph *FlashPartHeader) { if len(data) != 32 { panic("flash part header len != 32") } fph = &FlashPartHeader{ MediaID: data[0], Byte1: data[1], Byte2: data[2], Byte3: data[3], TotalSize: binary.LittleEndian.Uint32(data[4:8]), VersionText: string(data[16:]), raw: data, } fph.RawSize = fph.TotalSize - 32 // copyright string in header space if fph.MediaID == 67 && fph.Byte1 == 111 && fph.Byte2 == 112 && fph.Byte3 == 121 { fph.VersionText = string(data[:]) fph.TotalSize = 0 fph.RawSize = 0 } return fph } func (fph *FlashPartHeader) AdjustRawSize(size uint32) { fph.RawSize = size fph.AdjustTotalSize(size + 32) } func (fph *FlashPartHeader) AdjustTotalSize(size uint32) { fph.TotalSize = size totalSizeBytes := make([]byte, 4) binary.LittleEndian.PutUint32(totalSizeBytes, fph.TotalSize) // TODO: shitty copy for i, b := range totalSizeBytes { fph.raw[4+i] = b } } func (fph *FlashPartHeader) AdjustName(name string) { // truncate if too long if len(name) > 16 { name = name[:16] } for i := 16; i < 32; i++ { // copy version string charIndex := i - 16 if charIndex > len(name)-1 { fph.raw[i] = 0x20 } else { fph.raw[i] = name[charIndex] } } fph.VersionText = string(fph.raw[16:]) } func (fph *FlashPartHeader) GetBytes() []byte { return fph.raw }