Writer
in package
Low-level CBOR item builders shared by {@see Encoder} -- raw major-type/head construction, kept separate from the DXN-type -dispatch logic there.
The one sharp edge in this file: CBOR major 0's argument can
reach 2^64-1 (via the 8-byte "additional info 27" head form),
which exceeds PHP_INT_MAX (2^63-1, PHP's int is signed
64-bit) -- unlike the JS port, which has bigint with no upper
bound to lean on. self::head() therefore takes its argument
as a numeric string throughout and does every size comparison via
bccomp(), only ever casting to a native int once a branch has
already proven the value small enough (<= 4294967295, always
safely within PHP_INT_MAX) -- the 8-byte case is built byte-by
-byte via bcdiv/bcmod instead, exactly the class of bug this
project's plan flagged in advance (mirroring the JS port's own
writeBigUInt64BE header-byte-offset bug, a different bug in the
same "native integer type doesn't cover CBOR's own range" family).
Table of Contents
Constants
- UINT64_MAX : mixed = '18446744073709551615'
- 2^64 - 1, the largest value CBOR's own head encoding (major 0/1's argument) can hold.
Methods
- arrayOf() : string
- bigDigitsToBytes() : string
- Big-endian minimal-length magnitude bytes for a non-negative numeric string of any size -- built via repeated `bcdiv`/`bcmod` rather than any native-int arithmetic, so this has no upper bound the way casting to `int` would.
- bytesItem() : string
- bytesToBigDigits() : numeric-string
- The reverse of {@see self::bigDigitsToBytes()}: raw big-endian bytes to a non-negative numeric string, any length.
- floatHead() : string
- Major 7's float marker (additional info 27, "double follows") -- deliberately not `head(7, "27")`: for every other major type, `head`'s job is "encode this integer using the fewest bytes CBOR allows," but major 7's additional-info value of 27 has to mean literally "float64 follows," not "the number 27, shortest form" -- `head(7, "27")` would emit two bytes instead of the required single `0xfb` byte, a genuinely different, non-conformant encoding.
- floatItem() : string
- head() : string
- CBOR item header: major type (0-7) + argument, shortest form.
- integerItem() : string
- A DXN `integer` (arbitrary precision, hybrid native `int`/ {@see DXNInteger}) as CBOR major 0/1, or tag 2/3 (magnitude bytes) once it overflows 64 bits -- exactly `dxn/DXN.md` §2.2's integrity requirement.
- mapOf() : string
- tag() : string
- textItem() : string
- __construct() : mixed
- uint64BEBytes() : string
Constants
UINT64_MAX
2^64 - 1, the largest value CBOR's own head encoding (major 0/1's argument) can hold.
private
mixed
UINT64_MAX
= '18446744073709551615'
Methods
arrayOf()
public
static arrayOf(array<int, string> $items) : string
Parameters
- $items : array<int, string>
-
already-encoded item byte strings.
Return values
stringbigDigitsToBytes()
Big-endian minimal-length magnitude bytes for a non-negative numeric string of any size -- built via repeated `bcdiv`/`bcmod` rather than any native-int arithmetic, so this has no upper bound the way casting to `int` would.
public
static bigDigitsToBytes(numeric-string $n) : string
Parameters
- $n : numeric-string
-
non-negative.
Return values
stringbytesItem()
public
static bytesItem(string $data) : string
Parameters
- $data : string
Return values
stringbytesToBigDigits()
The reverse of {@see self::bigDigitsToBytes()}: raw big-endian bytes to a non-negative numeric string, any length.
public
static bytesToBigDigits(string $bytes) : numeric-string
Parameters
- $bytes : string
Return values
numeric-stringfloatHead()
Major 7's float marker (additional info 27, "double follows") -- deliberately not `head(7, "27")`: for every other major type, `head`'s job is "encode this integer using the fewest bytes CBOR allows," but major 7's additional-info value of 27 has to mean literally "float64 follows," not "the number 27, shortest form" -- `head(7, "27")` would emit two bytes instead of the required single `0xfb` byte, a genuinely different, non-conformant encoding.
public
static floatHead() : string
Return values
stringfloatItem()
public
static floatItem(float $n) : string
Parameters
- $n : float
Return values
stringhead()
CBOR item header: major type (0-7) + argument, shortest form.
public
static head(int $major, numeric-string $n) : string
Parameters
- $major : int
- $n : numeric-string
-
non-negative, <= 2^64-1.
Return values
stringintegerItem()
A DXN `integer` (arbitrary precision, hybrid native `int`/ {@see DXNInteger}) as CBOR major 0/1, or tag 2/3 (magnitude bytes) once it overflows 64 bits -- exactly `dxn/DXN.md` §2.2's integrity requirement.
public
static integerItem(int|DXNInteger $i) : string
Parameters
- $i : int|DXNInteger
Return values
stringmapOf()
public
static mapOf(array<int, array{0: string, 1: string}> $pairs) : string
Parameters
- $pairs : array<int, array{0: string, 1: string}>
-
already-encoded key/value byte string pairs.
Return values
stringtag()
public
static tag(int $tagNumber, string $itemBytes) : string
Parameters
- $tagNumber : int
- $itemBytes : string
Return values
stringtextItem()
public
static textItem(string $s) : string
Parameters
- $s : string
Return values
string__construct()
private
__construct() : mixed
uint64BEBytes()
private
static uint64BEBytes(numeric-string $n) : string
Parameters
- $n : numeric-string
-
non-negative, arbitrary magnitude.