@marcuspuchalla/nachos 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +64 -0
- package/LICENSE +674 -0
- package/README.md +345 -0
- package/dist/chunk-2FUTHZQQ.cjs +755 -0
- package/dist/chunk-2FUTHZQQ.cjs.map +1 -0
- package/dist/chunk-2HBCILJS.cjs +2034 -0
- package/dist/chunk-2HBCILJS.cjs.map +1 -0
- package/dist/chunk-7CFYWHS6.js +742 -0
- package/dist/chunk-7CFYWHS6.js.map +1 -0
- package/dist/chunk-PD72MVTX.cjs +160 -0
- package/dist/chunk-PD72MVTX.cjs.map +1 -0
- package/dist/chunk-ZDZ2B5PE.js +149 -0
- package/dist/chunk-ZDZ2B5PE.js.map +1 -0
- package/dist/chunk-ZRPJUEIZ.js +2020 -0
- package/dist/chunk-ZRPJUEIZ.js.map +1 -0
- package/dist/encoder/index.cjs +57 -0
- package/dist/encoder/index.cjs.map +1 -0
- package/dist/encoder/index.d.cts +72 -0
- package/dist/encoder/index.d.ts +72 -0
- package/dist/encoder/index.js +4 -0
- package/dist/encoder/index.js.map +1 -0
- package/dist/index.cjs +606 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +494 -0
- package/dist/index.d.ts +494 -0
- package/dist/index.js +523 -0
- package/dist/index.js.map +1 -0
- package/dist/metafile-cjs.json +1 -0
- package/dist/metafile-esm.json +1 -0
- package/dist/parser/index.cjs +85 -0
- package/dist/parser/index.cjs.map +1 -0
- package/dist/parser/index.d.cts +72 -0
- package/dist/parser/index.d.ts +72 -0
- package/dist/parser/index.js +4 -0
- package/dist/parser/index.js.map +1 -0
- package/dist/types-DvNlfbKB.d.cts +301 -0
- package/dist/types-DvNlfbKB.d.ts +301 -0
- package/dist/useCborSimpleEncoder-ButVU988.d.cts +268 -0
- package/dist/useCborSimpleEncoder-TVxzNJ_9.d.ts +268 -0
- package/dist/useCborTag-B_iaShG6.d.ts +142 -0
- package/dist/useCborTag-BfTIV8HM.d.cts +142 -0
- package/package.json +102 -0
- package/src/__tests__/public-api.test.ts +326 -0
- package/src/encoder/__tests__/cbor-collection-encoder.test.ts +331 -0
- package/src/encoder/__tests__/cbor-integer-encoder.test.ts +283 -0
- package/src/encoder/__tests__/cbor-simple-encoder.test.ts +224 -0
- package/src/encoder/__tests__/cbor-string-encoder.test.ts +345 -0
- package/src/encoder/__tests__/cbor-tag-encoder.test.ts +565 -0
- package/src/encoder/composables/#useCborTagEncoder.ts# +158 -0
- package/src/encoder/composables/useCborCollectionEncoder.ts +424 -0
- package/src/encoder/composables/useCborEncoder.ts +203 -0
- package/src/encoder/composables/useCborIntegerEncoder.ts +188 -0
- package/src/encoder/composables/useCborSimpleEncoder.ts +266 -0
- package/src/encoder/composables/useCborStringEncoder.ts +266 -0
- package/src/encoder/composables/useCborTagEncoder.ts +158 -0
- package/src/encoder/index.ts +35 -0
- package/src/encoder/types.ts +88 -0
- package/src/encoder/utils.ts +80 -0
- package/src/index.ts +434 -0
- package/src/parser/__tests__/ast-tree-structure.test.ts +311 -0
- package/src/parser/__tests__/cbor-collection-errors.test.ts +296 -0
- package/src/parser/__tests__/cbor-collection.test.ts +369 -0
- package/src/parser/__tests__/cbor-deterministic-encoding.test.ts +432 -0
- package/src/parser/__tests__/cbor-diagnostic.test.ts +333 -0
- package/src/parser/__tests__/cbor-duplicate-keys.test.ts +235 -0
- package/src/parser/__tests__/cbor-float-errors.test.ts +222 -0
- package/src/parser/__tests__/cbor-float.test.ts +502 -0
- package/src/parser/__tests__/cbor-integer-errors.test.ts +139 -0
- package/src/parser/__tests__/cbor-integer.test.ts +200 -0
- package/src/parser/__tests__/cbor-map-duplicate-keys.test.ts +403 -0
- package/src/parser/__tests__/cbor-parser-errors.test.ts +126 -0
- package/src/parser/__tests__/cbor-security-dos-protection.test.ts +503 -0
- package/src/parser/__tests__/cbor-sequences.test.ts +150 -0
- package/src/parser/__tests__/cbor-source-map.test.ts +321 -0
- package/src/parser/__tests__/cbor-standard-tags.test.ts +340 -0
- package/src/parser/__tests__/cbor-string-errors.test.ts +227 -0
- package/src/parser/__tests__/cbor-string.test.ts +224 -0
- package/src/parser/__tests__/cbor-tag-advanced.test.ts +500 -0
- package/src/parser/__tests__/cbor-tag-errors.test.ts +447 -0
- package/src/parser/__tests__/cbor-tag-source-map.test.ts +360 -0
- package/src/parser/__tests__/cbor-tag.test.ts +684 -0
- package/src/parser/__tests__/extreme-edge-cases.test.ts +146 -0
- package/src/parser/__tests__/pathBuilder.test.ts +256 -0
- package/src/parser/__tests__/rfc-test-vectors.test.ts +607 -0
- package/src/parser/__tests__/security-limits.test.ts +248 -0
- package/src/parser/__tests__/utils-errors.test.ts +127 -0
- package/src/parser/composables/useCborCollection.ts +509 -0
- package/src/parser/composables/useCborDiagnostic.ts +381 -0
- package/src/parser/composables/useCborFloat.ts +256 -0
- package/src/parser/composables/useCborInteger.ts +114 -0
- package/src/parser/composables/useCborParser.ts +951 -0
- package/src/parser/composables/useCborString.ts +330 -0
- package/src/parser/composables/useCborStringTypes.ts +129 -0
- package/src/parser/composables/useCborTag.ts +739 -0
- package/src/parser/index.ts +56 -0
- package/src/parser/types.ts +371 -0
- package/src/parser/utils/pathBuilder.ts +259 -0
- package/src/parser/utils.ts +398 -0
- package/src/utils/__tests__/logger.test.ts +186 -0
- package/src/utils/logger.ts +96 -0
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CBOR Integer Parser Composable
|
|
3
|
+
* Handles Major Types 0 (Unsigned) and 1 (Negative)
|
|
4
|
+
* Supports BigInt for 64-bit values outside Number.MAX_SAFE_INTEGER
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { ParseResult, ParseOptions } from '../types'
|
|
8
|
+
import { hexToBytes, readByte, readUint, readBigUint, extractCborHeader, validateCanonicalInteger } from '../utils'
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Composable for parsing CBOR integers (Major Types 0 and 1)
|
|
12
|
+
*
|
|
13
|
+
* @returns Object with parseInteger function
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* const { parseInteger } = useCborInteger()
|
|
18
|
+
* const result = parseInteger('1864') // 100
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export function useCborInteger() {
|
|
22
|
+
/**
|
|
23
|
+
* Parses CBOR integer (Major Type 0 or 1)
|
|
24
|
+
*
|
|
25
|
+
* @param hexString - CBOR hex string
|
|
26
|
+
* @param options - Parser options (optional)
|
|
27
|
+
* @returns Parsed integer value and bytes read
|
|
28
|
+
*/
|
|
29
|
+
const parseInteger = (hexString: string, options?: ParseOptions): ParseResult => {
|
|
30
|
+
const buffer = hexToBytes(hexString)
|
|
31
|
+
const initialByte = readByte(buffer, 0)
|
|
32
|
+
|
|
33
|
+
const { majorType, additionalInfo } = extractCborHeader(initialByte)
|
|
34
|
+
|
|
35
|
+
// Get the raw value based on additional info
|
|
36
|
+
let rawValue: number | bigint
|
|
37
|
+
let bytesRead: number
|
|
38
|
+
|
|
39
|
+
if (additionalInfo < 24) {
|
|
40
|
+
// Direct encoding (0-23)
|
|
41
|
+
rawValue = additionalInfo
|
|
42
|
+
bytesRead = 1
|
|
43
|
+
} else if (additionalInfo === 24) {
|
|
44
|
+
// 1 byte follows
|
|
45
|
+
rawValue = readByte(buffer, 1)
|
|
46
|
+
bytesRead = 2
|
|
47
|
+
} else if (additionalInfo === 25) {
|
|
48
|
+
// 2 bytes follow
|
|
49
|
+
rawValue = readUint(buffer, 1, 2)
|
|
50
|
+
bytesRead = 3
|
|
51
|
+
} else if (additionalInfo === 26) {
|
|
52
|
+
// 4 bytes follow
|
|
53
|
+
rawValue = readUint(buffer, 1, 4)
|
|
54
|
+
bytesRead = 5
|
|
55
|
+
} else if (additionalInfo === 27) {
|
|
56
|
+
// 8 bytes follow - use BigInt for large values
|
|
57
|
+
const bigValue = readBigUint(buffer, 1, 8)
|
|
58
|
+
|
|
59
|
+
// Check if value fits in Number.MAX_SAFE_INTEGER
|
|
60
|
+
if (bigValue <= BigInt(Number.MAX_SAFE_INTEGER)) {
|
|
61
|
+
rawValue = Number(bigValue)
|
|
62
|
+
} else {
|
|
63
|
+
rawValue = bigValue
|
|
64
|
+
}
|
|
65
|
+
bytesRead = 9
|
|
66
|
+
} else {
|
|
67
|
+
throw new Error(`Invalid additional info: ${additionalInfo}`)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Validate canonical encoding if requested
|
|
71
|
+
if (options?.validateCanonical) {
|
|
72
|
+
validateCanonicalInteger(rawValue, additionalInfo)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Calculate final value based on major type
|
|
76
|
+
let finalValue: number | bigint
|
|
77
|
+
|
|
78
|
+
if (majorType === 0) {
|
|
79
|
+
// Unsigned integer
|
|
80
|
+
finalValue = rawValue
|
|
81
|
+
} else if (majorType === 1) {
|
|
82
|
+
// Negative integer: -1 - N
|
|
83
|
+
if (typeof rawValue === 'bigint') {
|
|
84
|
+
const negValue = -1n - rawValue
|
|
85
|
+
// Check if result fits in safe integer range
|
|
86
|
+
if (negValue >= BigInt(Number.MIN_SAFE_INTEGER) && negValue <= BigInt(Number.MAX_SAFE_INTEGER)) {
|
|
87
|
+
finalValue = Number(negValue)
|
|
88
|
+
} else {
|
|
89
|
+
finalValue = negValue
|
|
90
|
+
}
|
|
91
|
+
} else {
|
|
92
|
+
const negValue = -1 - rawValue
|
|
93
|
+
// Check if the negative value is still within safe integer range
|
|
94
|
+
if (negValue >= Number.MIN_SAFE_INTEGER) {
|
|
95
|
+
finalValue = negValue
|
|
96
|
+
} else {
|
|
97
|
+
// Convert to BigInt if outside safe range
|
|
98
|
+
finalValue = BigInt(negValue)
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
} else {
|
|
102
|
+
throw new Error(`Expected major type 0 or 1, got ${majorType}`)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return {
|
|
106
|
+
value: finalValue,
|
|
107
|
+
bytesRead
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return {
|
|
112
|
+
parseInteger
|
|
113
|
+
}
|
|
114
|
+
}
|