Skip to content

Commit

Permalink
Shoeing
Browse files Browse the repository at this point in the history
Signed-off-by: Shivansh Vij <shivanshvij@loopholelabs.io>
  • Loading branch information
ShivanshVij committed Oct 20, 2023
1 parent ee562b0 commit 7cb822a
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 20 deletions.
7 changes: 7 additions & 0 deletions buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ func NewBuffer() *Buffer {
}
}

func NewBufferSize(size int) *Buffer {
return &Buffer{
b: make([]byte, size),
offset: 0,
}
}

func (buf *Buffer) Reset() {
buf.offset = 0
}
Expand Down
51 changes: 35 additions & 16 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,25 +86,44 @@ func decodeSlice(b []byte, kind Kind) ([]byte, uint32, error) {
}

func decodeBytes(b []byte, ret []byte) ([]byte, []byte, error) {
if len(b) > 0 && b[0] == BytesRawKind {
var size uint32
var err error
b, size, err = decodeUint32(b[1:])
if err != nil {
return b, nil, InvalidBytes
}
if len(b) > int(size)-1 {
if len(ret) < int(size) {
if ret == nil {
ret = make([]byte, size)
copy(ret, b[:size])
if len(b) > 3 && b[0] == BytesRawKind && b[1] == Uint32RawKind {
var size int
var offset int
cb := uint32(b[2])
if cb < continuation {
size = int(cb)
offset = 3
} else {
x := cb & (continuation - 1)
cb = uint32(b[3])
if cb < continuation {
size = int(x | (cb << 7))
offset = 4
} else {
x |= (cb & (continuation - 1)) << 7
cb = uint32(b[4])
if cb < continuation {
size = int(x | (cb << 14))
offset = 5
} else {
ret = append(ret[:0], b[:size]...)
x |= (cb & (continuation - 1)) << 14
cb = uint32(b[5])
if cb < continuation {
size = int(x | (cb << 21))
offset = 6
} else {
x |= (cb & (continuation - 1)) << 21
cb = uint32(b[6])
if cb < continuation {
size = int(x | (cb << 28))
offset = 7
}
}
}
} else {
copy(ret[0:], b[:size])
}
return b[size:], ret, nil
}
if len(b)-offset > size-1 {
return b[size+offset:], append(ret[:0], b[offset:size+offset]...), nil
}
}
return b, nil, InvalidBytes
Expand Down
51 changes: 47 additions & 4 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,53 @@ func EncodeBytes(b *Buffer, value []byte) {
}

func RawEncodeBytes(b *Buffer, value []byte) {
b.b[b.offset] = BytesRawKind
b.offset++
RawEncodeUint32(b, uint32(len(value)))
b.offset += copy(b.b[b.offset:], value)
castValue := uint32(len(value))
offset := b.offset
b.b[offset] = BytesRawKind
offset++
b.b[offset] = Uint32RawKind
offset++
if castValue < continuation {
b.b[offset] = byte(castValue)
offset++
} else {
b.b[offset] = byte(castValue&(continuation-1)) | continuation
offset++
castValue >>= 7
if castValue < continuation {
b.b[offset] = byte(castValue)
offset++
} else {
b.b[offset] = byte(castValue&(continuation-1)) | continuation
offset++
castValue >>= 7
if castValue < continuation {
b.b[offset] = byte(castValue)
offset++
} else {
b.b[offset] = byte(castValue&(continuation-1)) | continuation
offset++
castValue >>= 7
if castValue < continuation {
b.b[offset] = byte(castValue)
offset++
} else {
b.b[offset] = byte(castValue&(continuation-1)) | continuation
offset++
castValue >>= 7
if castValue < continuation {
b.b[offset] = byte(castValue)
} else {
b.b[offset] = byte(castValue&(continuation-1)) | continuation
offset++
b.b[offset] = byte(castValue >> 7)
offset++
}
}
}
}
}
b.offset = offset + copy(b.b[offset:], value)
}

func EncodeString(b *Buffer, value string) {
Expand Down

0 comments on commit 7cb822a

Please sign in to comment.