|
| 1 | +// Copyright (C) MongoDB, Inc. 2025-present. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | +// not use this file except in compliance with the License. You may obtain |
| 5 | +// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + |
| 7 | +package binaryutil |
| 8 | + |
| 9 | +import "math" |
| 10 | + |
| 11 | +// ReadI64 reads an 8-byte little-endian int64 from src. |
| 12 | +// |
| 13 | +// Performs bit operations directly to avoid signed overflow. |
| 14 | +func ReadI64(src []byte) (int64, []byte, bool) { |
| 15 | + if len(src) < 8 { |
| 16 | + return 0, src, false |
| 17 | + } |
| 18 | + |
| 19 | + _ = src[7] // bounds check hint to compiler |
| 20 | + |
| 21 | + // Do arithmetic in uint64, then convert to int64 |
| 22 | + value := uint64(src[0]) | |
| 23 | + uint64(src[1])<<8 | |
| 24 | + uint64(src[2])<<16 | |
| 25 | + uint64(src[3])<<24 | |
| 26 | + uint64(src[4])<<32 | |
| 27 | + uint64(src[5])<<40 | |
| 28 | + uint64(src[6])<<48 | |
| 29 | + uint64(src[7])<<56 |
| 30 | + |
| 31 | + if value > math.MaxInt64 { |
| 32 | + return 0, src[8:], false |
| 33 | + } |
| 34 | + |
| 35 | + return int64(value), src[8:], true |
| 36 | +} |
| 37 | + |
| 38 | +// AppendI64 appends an int64 to dst in little-endian byte order. |
| 39 | +// This manual implementation avoids heap allocations and function call overhead compared to |
| 40 | +// using binary.LittleEndian.PutUint64. Performs bit operations directly to avoid signed overflow. |
| 41 | +func AppendI64(dst []byte, x int64) []byte { |
| 42 | + return append(dst, |
| 43 | + byte(x), |
| 44 | + byte(x>>8), |
| 45 | + byte(x>>16), |
| 46 | + byte(x>>24), |
| 47 | + byte(x>>32), |
| 48 | + byte(x>>40), |
| 49 | + byte(x>>48), |
| 50 | + byte(x>>56), |
| 51 | + ) |
| 52 | +} |
| 53 | + |
| 54 | +// AppendI32 appends an int32 to dst in little-endian byte order. |
| 55 | +func AppendI32(dst []byte, x int32) []byte { |
| 56 | + return append(dst, |
| 57 | + byte(x), |
| 58 | + byte(x>>8), |
| 59 | + byte(x>>16), |
| 60 | + byte(x>>24), |
| 61 | + ) |
| 62 | +} |
| 63 | + |
| 64 | +// ReadI32 reads a 32-bit little-endian int32 from src returning the value, remaining bytes, and ok flag. |
| 65 | +func ReadI32(src []byte) (int32, []byte, bool) { |
| 66 | + if len(src) < 4 { |
| 67 | + return 0, src, false |
| 68 | + } |
| 69 | + |
| 70 | + _ = src[3] |
| 71 | + value := int32(src[0]) | |
| 72 | + int32(src[1])<<8 | |
| 73 | + int32(src[2])<<16 | |
| 74 | + int32(src[3])<<24 |
| 75 | + return value, src[4:], true |
| 76 | +} |
| 77 | + |
| 78 | +// ReadI32Unsafe reads a 32-bit little-endian int32 from src without length checks. |
| 79 | +// The caller must ensure src has at least 4 bytes. |
| 80 | +func ReadI32Unsafe(src []byte) int32 { |
| 81 | + _ = src[3] |
| 82 | + return int32(src[0]) | int32(src[1])<<8 | int32(src[2])<<16 | int32(src[3])<<24 |
| 83 | +} |
| 84 | + |
| 85 | +// PutI32 writes a little-endian int32 into dst starting at offset. Caller must ensure capacity. |
| 86 | +func PutI32(dst []byte, offset int, value int32) { |
| 87 | + dst[offset] = byte(value) |
| 88 | + dst[offset+1] = byte(value >> 8) |
| 89 | + dst[offset+2] = byte(value >> 16) |
| 90 | + dst[offset+3] = byte(value >> 24) |
| 91 | +} |
0 commit comments