aboutsummaryrefslogtreecommitdiff
path: root/file_header.go
diff options
context:
space:
mode:
authorJeff Halter <868228+jhalter@users.noreply.github.com>2021-07-24 17:54:17 -0700
committerJeff Halter <868228+jhalter@users.noreply.github.com>2021-07-24 17:54:17 -0700
commit6988a0571d5d37ea0f38ee3e4066533158f608bc (patch)
tree172010de54ea7e732d7daa836db659021dc8a933 /file_header.go
Initial squashed commit
Diffstat (limited to 'file_header.go')
-rw-r--r--file_header.go36
1 files changed, 36 insertions, 0 deletions
diff --git a/file_header.go b/file_header.go
new file mode 100644
index 0000000..60c652e
--- /dev/null
+++ b/file_header.go
@@ -0,0 +1,36 @@
+package hotline
+
+import (
+ "encoding/binary"
+ "github.com/jhalter/mobius/concat"
+)
+
+type FileHeader struct {
+ Size []byte // Total size of FileHeader payload
+ Type []byte // 0 for file, 1 for dir
+ FilePath []byte // encoded file path
+}
+
+func NewFileHeader(fileName string, isDir bool) FileHeader {
+ fh := FileHeader{
+ Size: make([]byte, 2),
+ Type: []byte{0x00, 0x00},
+ FilePath: EncodeFilePath(fileName),
+ }
+ if isDir {
+ fh.Type = []byte{0x00, 0x01}
+ }
+
+ encodedPathLen := uint16(len(fh.FilePath) + len(fh.Type))
+ binary.BigEndian.PutUint16(fh.Size, encodedPathLen)
+
+ return fh
+}
+
+func (fh *FileHeader) Payload() []byte {
+ return concat.Slices(
+ fh.Size,
+ fh.Type,
+ fh.FilePath,
+ )
+}