Skip to content

Commit 9029f39

Browse files
committed
BinaryReader: add ReadUint24, ReadInt24, and SetLen
1 parent 7ca70f9 commit 9029f39

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

binary.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ func (r *BinaryReader) Len() uint32 {
5555
return uint32(len(r.buf)) - r.pos
5656
}
5757

58+
// SetLen sets the remaining length of the underlying buffer.
59+
func (r *BinaryReader) SetLen(n uint32) {
60+
r.buf = r.buf[: r.pos+n : r.pos+n]
61+
}
62+
5863
// EOF returns true if we reached the end-of-file.
5964
func (r *BinaryReader) EOF() bool {
6065
return r.eof
@@ -110,6 +115,18 @@ func (r *BinaryReader) ReadUint16() uint16 {
110115
return r.Endianness.Uint16(b)
111116
}
112117

118+
// ReadUint24 reads a uint24 into a uint32.
119+
func (r *BinaryReader) ReadUint24() uint32 {
120+
b := r.ReadBytes(3)
121+
if b == nil {
122+
return 0
123+
} else if r.Endianness == binary.LittleEndian {
124+
return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16
125+
} else {
126+
return uint32(b[2]) | uint32(b[1])<<8 | uint32(b[0])<<16
127+
}
128+
}
129+
113130
// ReadUint32 reads a uint32.
114131
func (r *BinaryReader) ReadUint32() uint32 {
115132
b := r.ReadBytes(4)
@@ -128,22 +145,27 @@ func (r *BinaryReader) ReadUint64() uint64 {
128145
return r.Endianness.Uint64(b)
129146
}
130147

131-
// ReadInt8 reads a int8.
148+
// ReadInt8 reads an int8.
132149
func (r *BinaryReader) ReadInt8() int8 {
133150
return int8(r.ReadByte())
134151
}
135152

136-
// ReadInt16 reads a int16.
153+
// ReadInt16 reads an int16.
137154
func (r *BinaryReader) ReadInt16() int16 {
138155
return int16(r.ReadUint16())
139156
}
140157

141-
// ReadInt32 reads a int32.
158+
// ReadInt24 reads a int24 into an int32.
159+
func (r *BinaryReader) ReadInt24() int32 {
160+
return int32(r.ReadUint24())
161+
}
162+
163+
// ReadInt32 reads an int32.
142164
func (r *BinaryReader) ReadInt32() int32 {
143165
return int32(r.ReadUint32())
144166
}
145167

146-
// ReadInt64 reads a int64.
168+
// ReadInt64 reads an int64.
147169
func (r *BinaryReader) ReadInt64() int64 {
148170
return int64(r.ReadUint64())
149171
}

0 commit comments

Comments
 (0)