-
Notifications
You must be signed in to change notification settings - Fork 20
/
common.go
147 lines (130 loc) · 4.44 KB
/
common.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
* The MIT License (MIT)
*
* Copyright (c) 2014 Yani Iliev <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package wpress
import (
"bytes"
"errors"
"os"
"path/filepath"
"strconv"
)
const (
headerSize = 4377 // length of the header
filenameSize = 255 // maximum number of bytes allowed for filename
contentSize = 14 // maximum number of bytes allowed for content size
mtimeSize = 12 // maximum number of bytes allowed for last modified date
prefixSize = 4096 // maximum number of bytes allowed for prefix
)
// Header block format of a file
// Field Name Offset Length Contents
// Name 0 255 filename (no path, no slash)
// Size 255 14 length of file contents
// Mtime 269 12 last modification date
// Prefix 281 4096 path name, no trailing slashes
type Header struct {
Name []byte
Size []byte
Mtime []byte
Prefix []byte
}
// PopulateFromBytes populates header struct from bytes array
func (h *Header) PopulateFromBytes(block []byte) {
h.Name = block[0:255]
h.Size = block[255:269]
h.Mtime = block[269:281]
h.Prefix = block[281:4377]
}
// PopulateFromFilename populates header struct from passed filename
func (h *Header) PopulateFromFilename(filename string) error {
// try to open the file
file, err := os.Open(filename)
if err != nil {
return err
}
// get the fileinfo
fi, err := file.Stat()
if err != nil {
return err
}
// validate if filename fits the allowed length
if len(fi.Name()) > filenameSize {
return errors.New("filename is longer than max allowed")
}
// create filename buffer
h.Name = make([]byte, filenameSize)
// copy filename to the buffer leaving available space as zero-bytes
copy(h.Name, fi.Name())
// get filesize as string
size := strconv.FormatInt(fi.Size(), 10)
// validate if filesize fits the allowed length
if len(size) > contentSize {
return errors.New("file size is larger than max allowed")
}
// create size buffer
h.Size = make([]byte, contentSize)
// copy content size length to the buffer
copy(h.Size, size)
// get last modified date as string
unixTime := strconv.FormatInt(fi.ModTime().Unix(), 10)
if len(unixTime) > mtimeSize {
return errors.New("last modified date is after than max allowed")
}
// create mtime buffer
h.Mtime = make([]byte, mtimeSize)
// copy mtime to the buffer
copy(h.Mtime, unixTime)
// get the path to the file
_path := filepath.Dir(filename)
// validate if path fits the allowed length
if len(_path) > prefixSize {
return errors.New("prefix size is longer than max allowed")
}
// create buffer to put the prefix in
h.Prefix = make([]byte, prefixSize)
// put the prefix in the buffer
copy(h.Prefix, _path)
// close the file
err = file.Close()
if err != nil {
return err
}
return nil
}
// GetHeaderBlock returns byte sequence of header block populated with data
func (h Header) GetHeaderBlock() []byte {
block := append(h.Name, h.Size...)
block = append(block, h.Mtime...)
block = append(block, h.Prefix...)
return block
}
// GetSize returns content size
func (h Header) GetSize() (int, error) {
// remove any trailing zero bytes, convert to string, then convert to integer
return strconv.Atoi(string(bytes.Trim(h.Size, "\x00")))
}
// GetEOFBlock returns byte sequence describing EOF
func (h Header) GetEOFBlock() []byte {
// generate zero-byte sequence of length headerSize
return bytes.Repeat([]byte("\x00"), headerSize)
}