]>
Commit | Line | Data |
---|---|---|
43ecc0f4 JH |
1 | package hotline |
2 | ||
3 | import ( | |
72dd37f1 | 4 | "bytes" |
43ecc0f4 | 5 | "encoding/binary" |
b129b7cb JH |
6 | "io" |
7 | "slices" | |
43ecc0f4 JH |
8 | ) |
9 | ||
43ecc0f4 | 10 | type FileNameWithInfo struct { |
fd740bc4 | 11 | FileNameWithInfoHeader |
95159e55 | 12 | Name []byte // File Name |
1a37f014 JH |
13 | |
14 | readOffset int // Internal offset to track read progress | |
43ecc0f4 JH |
15 | } |
16 | ||
fd740bc4 JH |
17 | // FileNameWithInfoHeader contains the fixed length fields of FileNameWithInfo |
18 | type FileNameWithInfoHeader struct { | |
7cd900d6 | 19 | Type [4]byte // File type code |
72dd37f1 JH |
20 | Creator [4]byte // File creator code |
21 | FileSize [4]byte // File Size in bytes | |
22 | RSVD [4]byte | |
23 | NameScript [2]byte // ?? | |
95159e55 | 24 | NameSize [2]byte // Length of Name field |
43ecc0f4 JH |
25 | } |
26 | ||
fd740bc4 | 27 | func (f *FileNameWithInfoHeader) nameLen() int { |
72dd37f1 | 28 | return int(binary.BigEndian.Uint16(f.NameSize[:])) |
43ecc0f4 | 29 | } |
72dd37f1 | 30 | |
b129b7cb | 31 | // Read implements io.Reader for FileNameWithInfo |
1a37f014 JH |
32 | func (f *FileNameWithInfo) Read(p []byte) (int, error) { |
33 | buf := slices.Concat( | |
34 | f.Type[:], | |
35 | f.Creator[:], | |
36 | f.FileSize[:], | |
37 | f.RSVD[:], | |
38 | f.NameScript[:], | |
39 | f.NameSize[:], | |
40 | f.Name, | |
41 | ) | |
42 | ||
43 | if f.readOffset >= len(buf) { | |
44 | return 0, io.EOF // All bytes have been read | |
45 | } | |
46 | ||
47 | n := copy(p, buf[f.readOffset:]) | |
48 | f.readOffset += n | |
49 | ||
50 | return n, nil | |
72dd37f1 JH |
51 | } |
52 | ||
b129b7cb | 53 | func (f *FileNameWithInfo) Write(p []byte) (int, error) { |
fd740bc4 | 54 | err := binary.Read(bytes.NewReader(p), binary.BigEndian, &f.FileNameWithInfoHeader) |
72dd37f1 | 55 | if err != nil { |
b129b7cb | 56 | return 0, err |
72dd37f1 | 57 | } |
fd740bc4 | 58 | headerLen := binary.Size(f.FileNameWithInfoHeader) |
95159e55 | 59 | f.Name = p[headerLen : headerLen+f.nameLen()] |
72dd37f1 | 60 | |
b129b7cb | 61 | return len(p), nil |
72dd37f1 | 62 | } |