| 1 |
package zipextra |
| 2 |
|
| 3 |
import ( |
| 4 |
"math/big" |
| 5 |
"time" |
| 6 |
) |
| 7 |
|
| 8 |
|
| 9 |
type Buffer struct { |
| 10 |
buf []byte |
| 11 |
index int |
| 12 |
} |
| 13 |
|
| 14 |
|
| 15 |
func NewBuffer(e []byte) *Buffer { |
| 16 |
return &Buffer{buf: e} |
| 17 |
} |
| 18 |
|
| 19 |
|
| 20 |
func (p *Buffer) Available() int { |
| 21 |
return len(p.buf[p.index:]) |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
func (p *Buffer) Bytes() []byte { |
| 26 |
return p.buf[p.index:] |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
func (p *Buffer) Skip(n int) { |
| 31 |
p.index += n |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
func (p *Buffer) ReadBytes(size int) (b []byte) { |
| 36 |
b = p.buf[p.index : p.index+size] |
| 37 |
p.index += size |
| 38 |
return b |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
func (p *Buffer) Read8() (n uint8) { |
| 43 |
n = p.buf[p.index] |
| 44 |
p.index++ |
| 45 |
return n |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
func (p *Buffer) Read16() (n uint16) { |
| 50 |
n |= uint16(p.buf[p.index]) |
| 51 |
n |= uint16(p.buf[p.index+1]) << 8 |
| 52 |
p.index += 2 |
| 53 |
return n |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
func (p *Buffer) Read32() (n uint32) { |
| 58 |
n |= uint32(p.buf[p.index]) |
| 59 |
n |= uint32(p.buf[p.index+1]) << 8 |
| 60 |
n |= uint32(p.buf[p.index+2]) << 16 |
| 61 |
n |= uint32(p.buf[p.index+3]) << 24 |
| 62 |
p.index += 4 |
| 63 |
return n |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
func (p *Buffer) WriteByte(b byte) { |
| 68 |
p.buf = append(p.buf, b) |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
func (p *Buffer) WriteBytes(b []byte) { |
| 73 |
p.buf = append(p.buf, b...) |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
func (p *Buffer) Write8(n uint8) { |
| 78 |
p.buf = append(p.buf, n) |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
func (p *Buffer) Write16(n uint16) { |
| 83 |
p.buf = append(p.buf, uint8(n), uint8(n>>8)) |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
func (p *Buffer) Write32(n uint32) { |
| 88 |
p.buf = append(p.buf, uint8(n), uint8(n>>8), uint8(n>>16), uint8(n>>24)) |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
func (p *Buffer) WriteHeader(id uint16) func() { |
| 94 |
p.Write16(id) |
| 95 |
p.Write16(0) |
| 96 |
return func() { |
| 97 |
size := len(p.Bytes()) - 4 |
| 98 |
p.buf[2] = uint8(size) |
| 99 |
p.buf[3] = uint8(size >> 8) |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
func bigBytesToLittleEndian(x *big.Int) []byte { |
| 104 |
b := x.Bytes() |
| 105 |
for i := len(b)/2 - 1; i >= 0; i-- { |
| 106 |
opp := len(b) - 1 - i |
| 107 |
b[i], b[opp] = b[opp], b[i] |
| 108 |
} |
| 109 |
return b |
| 110 |
} |
| 111 |
|
| 112 |
func littleEndianBytesToBig(b []byte) *big.Int { |
| 113 |
for i := len(b)/2 - 1; i >= 0; i-- { |
| 114 |
opp := len(b) - 1 - i |
| 115 |
b[i], b[opp] = b[opp], b[i] |
| 116 |
} |
| 117 |
return big.NewInt(0).SetBytes(b) |
| 118 |
} |
| 119 |
|
| 120 |
func timeToFiletime(t time.Time) (uint32, uint32) { |
| 121 |
nsec := t.UnixNano() |
| 122 |
nsec /= 100 |
| 123 |
nsec += 116444736000000000 |
| 124 |
|
| 125 |
return uint32(nsec & 0xffffffff), uint32(nsec >> 32 & 0xffffffff) |
| 126 |
} |
| 127 |
|
| 128 |
func filetimeToTime(l uint32, h uint32) time.Time { |
| 129 |
nsec := int64(h)<<32 + int64(l) |
| 130 |
nsec -= 116444736000000000 |
| 131 |
nsec *= 100 |
| 132 |
|
| 133 |
return time.Unix(0, nsec) |
| 134 |
} |