-
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
crypto/goolm/message: improve encode/decode to use buffers
Signed-off-by: Sumner Evans <[email protected]>
- Loading branch information
1 parent
3e4b1a3
commit 0b814b1
Showing
5 changed files
with
159 additions
and
215 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,49 @@ | ||
package message | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
|
||
"maunium.net/go/mautrix/crypto/olm" | ||
) | ||
|
||
// checkDecodeErr checks if there was an error during decode. | ||
func checkDecodeErr(readBytes int) error { | ||
if readBytes == 0 { | ||
//end reached | ||
return olm.ErrInputToSmall | ||
} | ||
if readBytes < 0 { | ||
return olm.ErrOverflow | ||
} | ||
return nil | ||
type Decoder struct { | ||
*bytes.Buffer | ||
} | ||
|
||
// decodeVarInt decodes a single big-endian encoded varint. | ||
func decodeVarInt(input []byte) (uint32, int) { | ||
value, readBytes := binary.Uvarint(input) | ||
return uint32(value), readBytes | ||
func NewDecoder(buf []byte) *Decoder { | ||
return &Decoder{bytes.NewBuffer(buf)} | ||
} | ||
|
||
// decodeVarString decodes the length of the string (varint) and returns the actual string | ||
func decodeVarString(input []byte) ([]byte, int) { | ||
stringLen, readBytes := decodeVarInt(input) | ||
if readBytes <= 0 { | ||
return nil, readBytes | ||
} | ||
input = input[readBytes:] | ||
value := input[:stringLen] | ||
readBytes += int(stringLen) | ||
return value, readBytes | ||
func (d *Decoder) ReadVarInt() (uint64, error) { | ||
return binary.ReadUvarint(d) | ||
} | ||
|
||
// encodeVarIntByteLength returns the number of bytes needed to encode the uint32. | ||
func encodeVarIntByteLength(input uint32) int { | ||
result := 1 | ||
for input >= 128 { | ||
result++ | ||
input >>= 7 | ||
func (d *Decoder) ReadVarBytes() ([]byte, error) { | ||
if n, err := d.ReadVarInt(); err != nil { | ||
return nil, err | ||
} else { | ||
out := make([]byte, n) | ||
_, err = d.Read(out) | ||
return out, err | ||
} | ||
return result | ||
} | ||
|
||
// encodeVarStringByteLength returns the number of bytes needed to encode the input. | ||
func encodeVarStringByteLength(input []byte) int { | ||
result := encodeVarIntByteLength(uint32(len(input))) | ||
result += len(input) | ||
return result | ||
type Encoder struct { | ||
buf []byte | ||
} | ||
|
||
func (e *Encoder) Bytes() []byte { | ||
return e.buf | ||
} | ||
|
||
func (e *Encoder) PutByte(val byte) { | ||
e.buf = append(e.buf, val) | ||
} | ||
|
||
// encodeVarInt encodes a single uint32 | ||
func encodeVarInt(input uint32) []byte { | ||
out := make([]byte, encodeVarIntByteLength(input)) | ||
binary.PutUvarint(out, uint64(input)) | ||
return out | ||
func (e *Encoder) PutVarInt(val uint64) { | ||
e.buf = binary.AppendUvarint(e.buf, val) | ||
} | ||
|
||
// encodeVarString encodes the length of the input (varint) and appends the actual input | ||
func encodeVarString(input []byte) []byte { | ||
out := make([]byte, encodeVarStringByteLength(input)) | ||
length := encodeVarInt(uint32(len(input))) | ||
copy(out, length) | ||
copy(out[len(length):], input) | ||
return out | ||
func (e *Encoder) PutVarBytes(data []byte) { | ||
e.PutVarInt(uint64(len(data))) | ||
e.buf = append(e.buf, data...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.