lib/libesp32/berry_int64/README.md
A secure 64-bit integer implementation for Berry language on 32-bit architectures, specifically designed for embedded systems like ESP32.
This library provides comprehensive 64-bit integer support for Berry applications running on 32-bit platforms where native 64-bit integer operations are not available or efficient. It integrates seamlessly with Berry's type system and memory management.
Add to your platformio.ini:
lib_deps =
https://github.com/your-repo/berry_int64
Copy the src/ directory contents to your Berry library path.
# Create int64 values
var a = int64(42)
var b = int64("1234567890123456789")
var c = int64.fromu32(0xFFFFFFFF, 0x12345678)
# Arithmetic operations
var sum = a + b
var product = a * b
var quotient = b / a
# Comparisons
if a > b
print("a is greater")
end
# Type conversions
print(a.tostring())
print(a.toint()) # Convert to int32 (if in range)
print(a.tobool()) # Convert to boolean
# Bitwise operations
var shifted = a << 10
var masked = b >> 5
# Byte operations
var bytes_data = a.tobytes()
var from_bytes = int64.frombytes(bytes_data)
# Range checking
if a.isint()
var safe_int = a.toint()
end
# Factory methods
var from_float = int64.fromfloat(3.14159)
var from_string = int64.fromstring("999999999999")
# The library provides comprehensive error handling
try
var invalid = int64("not_a_number")
except "value_error"
print("Invalid string format")
end
try
var overflow = int64.fromu32(0xFFFFFFFF, 0x7FFFFFFF) + int64(1)
except "overflow_error"
print("Arithmetic overflow detected")
end
try
var div_error = int64(10) / int64(0)
except "divzero_error"
print("Division by zero")
end
This library has been thoroughly analyzed and hardened for security:
Run the security test suite:
load("security_tests.be")
The test suite validates:
int64() # Create int64 with value 0
int64(value) # Create from int, real, string, bool, or int64
int64.fromu32(low, high) # Create from two 32-bit values
int64.fromfloat(f) # Create from float
int64.fromstring(s) # Create from string (with validation)
int64.frombytes(bytes, idx) # Create from byte buffer
.tostring() # Convert to string representation
.toint() # Convert to int32 (if in range)
.tobool() # Convert to boolean (non-zero = true)
.tobytes() # Convert to byte representation
.isint() # Check if value fits in int32
.low32() # Get low 32 bits as int32
.high32() # Get high 32 bits as int32
# Arithmetic
+, -, *, /, % # Standard arithmetic operators
-* (unary minus) # Negation
# Comparison
==, !=, <, <=, >, >= # All comparison operators
# Bitwise
<<, >> # Left and right shift (with wrapping)
Optimized for embedded systems:
The library defines specific error types for different failure modes:
"value_error": Invalid input values (malformed strings, out of range)"overflow_error": Arithmetic overflow in operations"divzero_error": Division or modulo by zero"memory_error": Memory allocation failures# Run the original test suite
berry tests/int64.be
# Run security validation tests
berry tests/security_tests.be
Test with your application to ensure proper integration with Berry's type system and garbage collector.
When contributing to this library:
MIT License - see LICENSE file for details.
If you discover security vulnerabilities, please report them responsibly:
atoll() with validated strtoll()