mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-05 15:18:49 +00:00
Merge remote-tracking branch 'origin/master'
This commit is contained in:
@@ -66,33 +66,47 @@ decode_uleb128 :: proc {decode_uleb128_buffer, decode_uleb128_byte}
|
|||||||
|
|
||||||
// Decode a slice of bytes encoding a signed LEB128 integer into value and number of bytes used.
|
// Decode a slice of bytes encoding a signed LEB128 integer into value and number of bytes used.
|
||||||
// Returns `size` == 0 for an invalid value, empty slice, or a varint > 18 bytes.
|
// Returns `size` == 0 for an invalid value, empty slice, or a varint > 18 bytes.
|
||||||
decode_ileb128 :: proc(buf: []u8) -> (val: i128, size: int, err: Error) {
|
decode_ileb128_buffer :: proc(buf: []u8) -> (val: i128, size: int, err: Error) {
|
||||||
shift: uint
|
|
||||||
|
|
||||||
if len(buf) == 0 {
|
if len(buf) == 0 {
|
||||||
return 0, 0, .Buffer_Too_Small
|
return 0, 0, .Buffer_Too_Small
|
||||||
}
|
}
|
||||||
|
|
||||||
for v in buf {
|
for v in buf {
|
||||||
size += 1
|
val, size, err = decode_ileb128_byte(v, size, val)
|
||||||
|
if err != .Buffer_Too_Small {
|
||||||
// 18 * 7 bits = 126, which including sign means we can have a 19th byte.
|
return
|
||||||
if size > LEB128_MAX_BYTES || size == LEB128_MAX_BYTES && v > 0x7f {
|
|
||||||
return 0, 0, .Value_Too_Large
|
|
||||||
}
|
}
|
||||||
|
|
||||||
val |= i128(v & 0x7f) << shift
|
|
||||||
shift += 7
|
|
||||||
|
|
||||||
if v < 128 { break }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if buf[size - 1] & 0x40 == 0x40 {
|
if err == .Buffer_Too_Small {
|
||||||
val |= max(i128) << shift
|
val, size = 0, 0
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Decode a a signed LEB128 integer into value and number of bytes used, one byte at a time.
|
||||||
|
// Returns `size` == 0 for an invalid value, empty slice, or a varint > 18 bytes.
|
||||||
|
decode_ileb128_byte :: proc(input: u8, offset: int, accumulator: i128) -> (val: i128, size: int, err: Error) {
|
||||||
|
size = offset + 1
|
||||||
|
shift := uint(offset * 7)
|
||||||
|
|
||||||
|
// 18 * 7 bits = 126, which including sign means we can have a 19th byte.
|
||||||
|
if size > LEB128_MAX_BYTES || size == LEB128_MAX_BYTES && input > 0x7f {
|
||||||
|
return 0, 0, .Value_Too_Large
|
||||||
|
}
|
||||||
|
|
||||||
|
val = accumulator | i128(input & 0x7f) << shift
|
||||||
|
|
||||||
|
if input < 128 {
|
||||||
|
if input & 0x40 == 0x40 {
|
||||||
|
val |= max(i128) << (shift + 7)
|
||||||
|
}
|
||||||
|
return val, size, .None
|
||||||
|
}
|
||||||
|
return val, size, .Buffer_Too_Small
|
||||||
|
}
|
||||||
|
decode_ileb128 :: proc{decode_ileb128_buffer, decode_ileb128_byte}
|
||||||
|
|
||||||
// Encode `val` into `buf` as an unsigned LEB128 encoded series of bytes.
|
// Encode `val` into `buf` as an unsigned LEB128 encoded series of bytes.
|
||||||
// `buf` must be appropriately sized.
|
// `buf` must be appropriately sized.
|
||||||
encode_uleb128 :: proc(buf: []u8, val: u128) -> (size: int, err: Error) {
|
encode_uleb128 :: proc(buf: []u8, val: u128) -> (size: int, err: Error) {
|
||||||
|
|||||||
+1
-1
@@ -172,7 +172,7 @@ slice_data_cast :: proc "contextless" ($T: typeid/[]$A, slice: $S/[]$B) -> T {
|
|||||||
|
|
||||||
slice_to_components :: proc "contextless" (slice: $E/[]$T) -> (data: ^T, len: int) {
|
slice_to_components :: proc "contextless" (slice: $E/[]$T) -> (data: ^T, len: int) {
|
||||||
s := transmute(Raw_Slice)slice
|
s := transmute(Raw_Slice)slice
|
||||||
return s.data, s.len
|
return (^T)(s.data), s.len
|
||||||
}
|
}
|
||||||
|
|
||||||
buffer_from_slice :: proc "contextless" (backing: $T/[]$E) -> [dynamic]E {
|
buffer_from_slice :: proc "contextless" (backing: $T/[]$E) -> [dynamic]E {
|
||||||
|
|||||||
@@ -61,6 +61,7 @@ import hash "core:hash"
|
|||||||
|
|
||||||
import image "core:image"
|
import image "core:image"
|
||||||
import png "core:image/png"
|
import png "core:image/png"
|
||||||
|
import qoi "core:image/qoi"
|
||||||
|
|
||||||
import io "core:io"
|
import io "core:io"
|
||||||
import log "core:log"
|
import log "core:log"
|
||||||
@@ -159,6 +160,7 @@ _ :: fmt
|
|||||||
_ :: hash
|
_ :: hash
|
||||||
_ :: image
|
_ :: image
|
||||||
_ :: png
|
_ :: png
|
||||||
|
_ :: qoi
|
||||||
_ :: io
|
_ :: io
|
||||||
_ :: log
|
_ :: log
|
||||||
_ :: math
|
_ :: math
|
||||||
|
|||||||
Reference in New Issue
Block a user