aboutsummaryrefslogtreecommitdiff
path: root/hotline/file_name_with_info.go
diff options
context:
space:
mode:
authorJeff Halter <868228+jhalter@users.noreply.github.com>2021-08-10 18:52:46 -0700
committerJeff Halter <868228+jhalter@users.noreply.github.com>2021-08-10 13:52:46 -0700
commit72dd37f1abb2b550aaaac48eac677403d5664797 (patch)
tree5c431679475647f2f932f2934e6acd65090d2499 /hotline/file_name_with_info.go
parent9d41bcdf29778eab3253f8e31670baf64ad389bf (diff)
Add tests and clean up
Diffstat (limited to 'hotline/file_name_with_info.go')
-rw-r--r--hotline/file_name_with_info.go77
1 files changed, 38 insertions, 39 deletions
diff --git a/hotline/file_name_with_info.go b/hotline/file_name_with_info.go
index eecf5ec..d9add8c 100644
--- a/hotline/file_name_with_info.go
+++ b/hotline/file_name_with_info.go
@@ -1,53 +1,52 @@
package hotline
import (
+ "bytes"
"encoding/binary"
- "github.com/jhalter/mobius/concat"
)
-// FileNameWithInfo field content is presented in this structure:
-// Type 4 Folder (‘fldr’) or other
-// Creator 4
-// File size 4
-// 4 Reserved?
-// Name script 2
-// Name size 2
-// Name data size
type FileNameWithInfo struct {
- Type []byte // file type code
- Creator []byte // File creator code
- FileSize []byte // File Size in bytes
- RSVD []byte
- NameScript []byte // TODO: What is this?
- NameSize []byte // Length of name field
- Name []byte // File name
+ fileNameWithInfoHeader
+ name []byte // File name
}
-func (f FileNameWithInfo) Payload() []byte {
- name := f.Name
- nameSize := make([]byte, 2)
- binary.BigEndian.PutUint16(nameSize, uint16(len(name)))
+// 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
+}
+
+func (f *fileNameWithInfoHeader) nameLen() int {
+ return int(binary.BigEndian.Uint16(f.NameSize[:]))
+}
- return concat.Slices(
- f.Type,
- f.Creator,
- f.FileSize,
- []byte{0, 0, 0, 0},
- f.NameScript,
- nameSize,
- f.Name,
- )
+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
+ }
+
+ _, err = buf.Write(f.name)
+ if err != nil {
+ return data, err
+ }
+
+ return buf.Bytes(), err
}
-func (f *FileNameWithInfo) Read(p []byte) (n int, err error) {
- // TODO: check p for expected len
- f.Type = p[0:4]
- f.Creator = p[4:8]
- f.FileSize = p[8:12]
- f.RSVD = p[12:16]
- f.NameScript = p[16:18]
- f.NameSize = p[18:20]
- f.Name = p[20:]
+func (f *FileNameWithInfo) UnmarshalBinary(data []byte) error {
+ err := binary.Read(bytes.NewReader(data), binary.BigEndian, &f.fileNameWithInfoHeader)
+ if err != nil {
+ return err
+ }
+ headerLen := binary.Size(f.fileNameWithInfoHeader)
+ f.name = data[headerLen : headerLen+f.nameLen()]
- return len(p), err
+ return err
}
+