X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/blobdiff_plain/7cd900d61edbd6d322db3cecb913adf574389320..refs/heads/main:/hotline/file_name_with_info.go?ds=inline diff --git a/hotline/file_name_with_info.go b/hotline/file_name_with_info.go index f230856..3a4a795 100644 --- a/hotline/file_name_with_info.go +++ b/hotline/file_name_with_info.go @@ -3,49 +3,60 @@ package hotline import ( "bytes" "encoding/binary" + "io" + "slices" ) type FileNameWithInfo struct { - fileNameWithInfoHeader - name []byte // File name + FileNameWithInfoHeader + Name []byte // File Name + + readOffset int // Internal offset to track read progress } -// fileNameWithInfoHeader contains the fixed length fields of FileNameWithInfo -type fileNameWithInfoHeader struct { +// FileNameWithInfoHeader contains the fixed length fields of FileNameWithInfo +type FileNameWithInfoHeader struct { Type [4]byte // File type code Creator [4]byte // File creator code FileSize [4]byte // File Size in bytes RSVD [4]byte NameScript [2]byte // ?? - NameSize [2]byte // Length of name field + NameSize [2]byte // Length of Name field } -func (f *fileNameWithInfoHeader) nameLen() int { +func (f *FileNameWithInfoHeader) nameLen() int { return int(binary.BigEndian.Uint16(f.NameSize[:])) } -func (f *FileNameWithInfo) MarshalBinary() (data []byte, err error) { - var buf bytes.Buffer - err = binary.Write(&buf, binary.LittleEndian, f.fileNameWithInfoHeader) - if err != nil { - return data, err +// Read implements io.Reader for FileNameWithInfo +func (f *FileNameWithInfo) Read(p []byte) (int, error) { + buf := slices.Concat( + f.Type[:], + f.Creator[:], + f.FileSize[:], + f.RSVD[:], + f.NameScript[:], + f.NameSize[:], + f.Name, + ) + + if f.readOffset >= len(buf) { + return 0, io.EOF // All bytes have been read } - _, err = buf.Write(f.name) - if err != nil { - return data, err - } + n := copy(p, buf[f.readOffset:]) + f.readOffset += n - return buf.Bytes(), err + return n, nil } -func (f *FileNameWithInfo) UnmarshalBinary(data []byte) error { - err := binary.Read(bytes.NewReader(data), binary.BigEndian, &f.fileNameWithInfoHeader) +func (f *FileNameWithInfo) Write(p []byte) (int, error) { + err := binary.Read(bytes.NewReader(p), binary.BigEndian, &f.FileNameWithInfoHeader) if err != nil { - return err + return 0, err } - headerLen := binary.Size(f.fileNameWithInfoHeader) - f.name = data[headerLen : headerLen+f.nameLen()] + headerLen := binary.Size(f.FileNameWithInfoHeader) + f.Name = p[headerLen : headerLen+f.nameLen()] - return err + return len(p), nil }