]>
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 { |
72dd37f1 | 11 | fileNameWithInfoHeader |
95159e55 | 12 | Name []byte // File Name |
43ecc0f4 JH |
13 | } |
14 | ||
72dd37f1 JH |
15 | // fileNameWithInfoHeader contains the fixed length fields of FileNameWithInfo |
16 | type fileNameWithInfoHeader struct { | |
7cd900d6 | 17 | Type [4]byte // File type code |
72dd37f1 JH |
18 | Creator [4]byte // File creator code |
19 | FileSize [4]byte // File Size in bytes | |
20 | RSVD [4]byte | |
21 | NameScript [2]byte // ?? | |
95159e55 | 22 | NameSize [2]byte // Length of Name field |
43ecc0f4 JH |
23 | } |
24 | ||
72dd37f1 JH |
25 | func (f *fileNameWithInfoHeader) nameLen() int { |
26 | return int(binary.BigEndian.Uint16(f.NameSize[:])) | |
43ecc0f4 | 27 | } |
72dd37f1 | 28 | |
b129b7cb JH |
29 | // Read implements io.Reader for FileNameWithInfo |
30 | func (f *FileNameWithInfo) Read(b []byte) (int, error) { | |
31 | return copy(b, | |
32 | slices.Concat( | |
33 | f.Type[:], | |
34 | f.Creator[:], | |
35 | f.FileSize[:], | |
36 | f.RSVD[:], | |
37 | f.NameScript[:], | |
38 | f.NameSize[:], | |
95159e55 | 39 | f.Name, |
b129b7cb JH |
40 | ), |
41 | ), io.EOF | |
72dd37f1 JH |
42 | } |
43 | ||
b129b7cb JH |
44 | func (f *FileNameWithInfo) Write(p []byte) (int, error) { |
45 | err := binary.Read(bytes.NewReader(p), binary.BigEndian, &f.fileNameWithInfoHeader) | |
72dd37f1 | 46 | if err != nil { |
b129b7cb | 47 | return 0, err |
72dd37f1 JH |
48 | } |
49 | headerLen := binary.Size(f.fileNameWithInfoHeader) | |
95159e55 | 50 | f.Name = p[headerLen : headerLen+f.nameLen()] |
72dd37f1 | 51 | |
b129b7cb | 52 | return len(p), nil |
72dd37f1 | 53 | } |