@partisiablockchain/sections 6.8.0 → 6.11.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/package.json +2 -2
- package/target/main/PbcFile.d.ts +39 -12
- package/target/main/PbcFile.js +67 -42
- package/target/main/PbcFile.js.map +1 -1
- package/target/main/Sections.d.ts +6 -42
- package/target/main/Sections.js +33 -79
- package/target/main/Sections.js.map +1 -1
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@partisiablockchain/sections",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.11.0",
|
|
4
4
|
"license": "AGPL-3.0",
|
|
5
5
|
"dependencies": {
|
|
6
|
-
"@secata-public/bitmanipulation-ts": "3.3.0"
|
|
6
|
+
"@secata-public/bitmanipulation-ts": "^3.3.0"
|
|
7
7
|
},
|
|
8
8
|
"devDependencies": {
|
|
9
9
|
"@eslint/compat": "^1.2.6",
|
package/target/main/PbcFile.d.ts
CHANGED
|
@@ -22,17 +22,7 @@ export declare class PbcFile {
|
|
|
22
22
|
static readonly WASM_IDENTIFIER_BYTE: number;
|
|
23
23
|
/** Identifier byte indicating the beginning of an ZKBC section of a pbc file. */
|
|
24
24
|
static readonly ZK_IDENTIFIER_BYTE: number;
|
|
25
|
-
|
|
26
|
-
readonly isZk: boolean;
|
|
27
|
-
/**
|
|
28
|
-
* Identifiers given in the order corresponding to the <a
|
|
29
|
-
* href="https://partisiablockchain.gitlab.io/documentation/smart-contracts/smart-contract-binary-formats.html#partisia-blockchain-file-format">".pbc
|
|
30
|
-
* format"</a>.
|
|
31
|
-
*/
|
|
32
|
-
private static readonly IDENTIFIERS;
|
|
33
|
-
private readonly abiBytes;
|
|
34
|
-
private readonly wasmBytes;
|
|
35
|
-
private readonly zkbcBytes;
|
|
25
|
+
private readonly sections;
|
|
36
26
|
private constructor();
|
|
37
27
|
/**
|
|
38
28
|
* Create a PbcFile from a byte array following the PBC <a
|
|
@@ -44,7 +34,38 @@ export declare class PbcFile {
|
|
|
44
34
|
* section or a WASM section or if the sectioned part of the bytes is ill-formed.
|
|
45
35
|
*/
|
|
46
36
|
static fromBytes(pbcBytes: Buffer): PbcFile;
|
|
47
|
-
|
|
37
|
+
/**
|
|
38
|
+
* Create the pbc file from the sections it consists of.
|
|
39
|
+
*
|
|
40
|
+
* @param sections the sections to create it from
|
|
41
|
+
* @return the created pbc file
|
|
42
|
+
* @throws Error if the sections doesn't contain an ABI section or a WASM section.
|
|
43
|
+
*/
|
|
44
|
+
static fromSections(sections: Map<number, Buffer>): PbcFile;
|
|
45
|
+
/**
|
|
46
|
+
* Create the pbc file from an abi and wasm file.
|
|
47
|
+
*
|
|
48
|
+
* @param abiBytes bytes of the abi
|
|
49
|
+
* @param wasmBytes bytes of the wasm
|
|
50
|
+
* @return the created pbc file
|
|
51
|
+
*/
|
|
52
|
+
static fromAbiAndWasm(abiBytes: Buffer, wasmBytes: Buffer): PbcFile;
|
|
53
|
+
/**
|
|
54
|
+
* Create the pbc file from an abi and zkwa file.
|
|
55
|
+
*
|
|
56
|
+
* @param abiBytes bytes of the abi
|
|
57
|
+
* @param zkwaBytes bytes of the zkwa encoded sections
|
|
58
|
+
* @return the created pbc file
|
|
59
|
+
* @throws Error if zkwa bytes doesn't encode valid sections or if the zkwa sections
|
|
60
|
+
* doesn't contain a WASM section
|
|
61
|
+
*/
|
|
62
|
+
static fromAbiAndZkwa(abiBytes: Buffer, zkwaBytes: Buffer): PbcFile;
|
|
63
|
+
/**
|
|
64
|
+
* Get the serialized bytes for this pbc file.
|
|
65
|
+
*
|
|
66
|
+
* @return the serialized bytes
|
|
67
|
+
*/
|
|
68
|
+
getSerializedBytes(): Buffer;
|
|
48
69
|
/**
|
|
49
70
|
* Build ZKWA formatted bytes, following the definition of the ZKWA format <a
|
|
50
71
|
* href="https://partisiablockchain.gitlab.io/documentation/smart-contracts/smart-contract-binary-formats.html#zkwa-format">here</a>.
|
|
@@ -74,4 +95,10 @@ export declare class PbcFile {
|
|
|
74
95
|
* @return the ZKBC content bytes.
|
|
75
96
|
*/
|
|
76
97
|
getZkbcBytes(): Buffer;
|
|
98
|
+
/**
|
|
99
|
+
* Whether this is the .pbc file of a ZK-contract.
|
|
100
|
+
*
|
|
101
|
+
* @return whether this is the .pbc file of a ZK-contract.
|
|
102
|
+
*/
|
|
103
|
+
isZk(): boolean;
|
|
77
104
|
}
|
package/target/main/PbcFile.js
CHANGED
|
@@ -36,11 +36,8 @@ const Sections_1 = require("./Sections");
|
|
|
36
36
|
* contains the circuit to be executed in a ZK-computation.
|
|
37
37
|
*/
|
|
38
38
|
class PbcFile {
|
|
39
|
-
constructor(
|
|
40
|
-
this.
|
|
41
|
-
this.wasmBytes = wasmBytes;
|
|
42
|
-
this.zkbcBytes = zkbcBytes;
|
|
43
|
-
this.isZk = isZk;
|
|
39
|
+
constructor(sections) {
|
|
40
|
+
this.sections = sections;
|
|
44
41
|
}
|
|
45
42
|
/**
|
|
46
43
|
* Create a PbcFile from a byte array following the PBC <a
|
|
@@ -57,31 +54,65 @@ class PbcFile {
|
|
|
57
54
|
if (!inputHeaderBytes.equals(PbcFile.PBC_HEADER_BYTES)) {
|
|
58
55
|
throw Error("Given bytes do not contain PBC header!");
|
|
59
56
|
}
|
|
60
|
-
let sections;
|
|
57
|
+
let sections = new Map();
|
|
61
58
|
try {
|
|
62
|
-
sections = Sections_1.Sections.fromBytes(inputStream
|
|
59
|
+
sections = Sections_1.Sections.fromBytes(inputStream);
|
|
63
60
|
}
|
|
64
61
|
catch (e) {
|
|
65
62
|
throw Error("Exception encountered while parsing sectioned bytes:\n" + e);
|
|
66
63
|
}
|
|
67
64
|
return this.fromSections(sections);
|
|
68
65
|
}
|
|
66
|
+
/**
|
|
67
|
+
* Create the pbc file from the sections it consists of.
|
|
68
|
+
*
|
|
69
|
+
* @param sections the sections to create it from
|
|
70
|
+
* @return the created pbc file
|
|
71
|
+
* @throws Error if the sections doesn't contain an ABI section or a WASM section.
|
|
72
|
+
*/
|
|
69
73
|
static fromSections(sections) {
|
|
70
|
-
if (!sections.
|
|
74
|
+
if (!sections.has(this.ABI_IDENTIFIER_BYTE)) {
|
|
71
75
|
throw Error("Given bytes contain no ABI section!");
|
|
72
76
|
}
|
|
73
|
-
if (!sections.
|
|
77
|
+
if (!sections.has(this.WASM_IDENTIFIER_BYTE)) {
|
|
74
78
|
throw Error("Given bytes contain no WASM section!");
|
|
75
79
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
80
|
+
return new PbcFile(sections);
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Create the pbc file from an abi and wasm file.
|
|
84
|
+
*
|
|
85
|
+
* @param abiBytes bytes of the abi
|
|
86
|
+
* @param wasmBytes bytes of the wasm
|
|
87
|
+
* @return the created pbc file
|
|
88
|
+
*/
|
|
89
|
+
static fromAbiAndWasm(abiBytes, wasmBytes) {
|
|
90
|
+
return this.fromSections(new Map([
|
|
91
|
+
[PbcFile.ABI_IDENTIFIER_BYTE, abiBytes],
|
|
92
|
+
[PbcFile.WASM_IDENTIFIER_BYTE, wasmBytes],
|
|
93
|
+
]));
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Create the pbc file from an abi and zkwa file.
|
|
97
|
+
*
|
|
98
|
+
* @param abiBytes bytes of the abi
|
|
99
|
+
* @param zkwaBytes bytes of the zkwa encoded sections
|
|
100
|
+
* @return the created pbc file
|
|
101
|
+
* @throws Error if zkwa bytes doesn't encode valid sections or if the zkwa sections
|
|
102
|
+
* doesn't contain a WASM section
|
|
103
|
+
*/
|
|
104
|
+
static fromAbiAndZkwa(abiBytes, zkwaBytes) {
|
|
105
|
+
const sections = Sections_1.Sections.fromBytes(zkwaBytes);
|
|
106
|
+
sections.set(PbcFile.ABI_IDENTIFIER_BYTE, abiBytes);
|
|
107
|
+
return this.fromSections(sections);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Get the serialized bytes for this pbc file.
|
|
111
|
+
*
|
|
112
|
+
* @return the serialized bytes
|
|
113
|
+
*/
|
|
114
|
+
getSerializedBytes() {
|
|
115
|
+
return Buffer.concat([PbcFile.PBC_HEADER_BYTES, Sections_1.Sections.serializeSections(this.sections)]);
|
|
85
116
|
}
|
|
86
117
|
/**
|
|
87
118
|
* Build ZKWA formatted bytes, following the definition of the ZKWA format <a
|
|
@@ -94,17 +125,13 @@ class PbcFile {
|
|
|
94
125
|
* @throws Error if no ZKBC section is present.
|
|
95
126
|
*/
|
|
96
127
|
getZkwaBytes() {
|
|
97
|
-
if (!this.isZk) {
|
|
128
|
+
if (!this.isZk()) {
|
|
98
129
|
throw Error("Could not construct ZKWA file, the ZKBC section was missing in the given bytes.");
|
|
99
130
|
}
|
|
100
|
-
return
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
bigEndianByteOutput.writeU8(PbcFile.ZK_IDENTIFIER_BYTE);
|
|
105
|
-
bigEndianByteOutput.writeU32(this.zkbcBytes.length);
|
|
106
|
-
bigEndianByteOutput.writeBytes(this.zkbcBytes);
|
|
107
|
-
});
|
|
131
|
+
return Sections_1.Sections.serializeSections(new Map([
|
|
132
|
+
[PbcFile.WASM_IDENTIFIER_BYTE, this.getWasmBytes()],
|
|
133
|
+
[PbcFile.ZK_IDENTIFIER_BYTE, this.getZkbcBytes()],
|
|
134
|
+
]));
|
|
108
135
|
}
|
|
109
136
|
/**
|
|
110
137
|
* Returns the content bytes of the ABI section.
|
|
@@ -112,7 +139,7 @@ class PbcFile {
|
|
|
112
139
|
* @return the ABI content bytes.
|
|
113
140
|
*/
|
|
114
141
|
getAbiBytes() {
|
|
115
|
-
return this.
|
|
142
|
+
return this.sections.get(PbcFile.ABI_IDENTIFIER_BYTE);
|
|
116
143
|
}
|
|
117
144
|
/**
|
|
118
145
|
* Returns the content bytes of the WASM section.
|
|
@@ -120,7 +147,7 @@ class PbcFile {
|
|
|
120
147
|
* @return the WASM content bytes.
|
|
121
148
|
*/
|
|
122
149
|
getWasmBytes() {
|
|
123
|
-
return this.
|
|
150
|
+
return this.sections.get(PbcFile.WASM_IDENTIFIER_BYTE);
|
|
124
151
|
}
|
|
125
152
|
/**
|
|
126
153
|
* Returns the content bytes of the ZKBC section.
|
|
@@ -128,29 +155,27 @@ class PbcFile {
|
|
|
128
155
|
* @return the ZKBC content bytes.
|
|
129
156
|
*/
|
|
130
157
|
getZkbcBytes() {
|
|
131
|
-
if (!this.isZk) {
|
|
158
|
+
if (!this.isZk()) {
|
|
132
159
|
throw Error("Cannot get ZKBC bytes from a public contract");
|
|
133
160
|
}
|
|
134
|
-
return this.
|
|
161
|
+
return this.sections.get(PbcFile.ZK_IDENTIFIER_BYTE);
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Whether this is the .pbc file of a ZK-contract.
|
|
165
|
+
*
|
|
166
|
+
* @return whether this is the .pbc file of a ZK-contract.
|
|
167
|
+
*/
|
|
168
|
+
isZk() {
|
|
169
|
+
return this.sections.has(PbcFile.ZK_IDENTIFIER_BYTE);
|
|
135
170
|
}
|
|
136
171
|
}
|
|
137
172
|
exports.PbcFile = PbcFile;
|
|
138
173
|
/** The header bytes of the .pbc format. */
|
|
139
|
-
PbcFile.PBC_HEADER_BYTES =
|
|
174
|
+
PbcFile.PBC_HEADER_BYTES = Buffer.from([0x50, 0x42, 0x53, 0x43]);
|
|
140
175
|
/** Identifier byte indicating the beginning of an ABI section of a pbc file. */
|
|
141
176
|
PbcFile.ABI_IDENTIFIER_BYTE = 0x1;
|
|
142
177
|
/** Identifier byte indicating the beginning of an WASM section of a pbc file. */
|
|
143
178
|
PbcFile.WASM_IDENTIFIER_BYTE = 0x2;
|
|
144
179
|
/** Identifier byte indicating the beginning of an ZKBC section of a pbc file. */
|
|
145
180
|
PbcFile.ZK_IDENTIFIER_BYTE = 0x3;
|
|
146
|
-
/**
|
|
147
|
-
* Identifiers given in the order corresponding to the <a
|
|
148
|
-
* href="https://partisiablockchain.gitlab.io/documentation/smart-contracts/smart-contract-binary-formats.html#partisia-blockchain-file-format">".pbc
|
|
149
|
-
* format"</a>.
|
|
150
|
-
*/
|
|
151
|
-
PbcFile.IDENTIFIERS = Buffer.from([
|
|
152
|
-
PbcFile.ABI_IDENTIFIER_BYTE,
|
|
153
|
-
PbcFile.WASM_IDENTIFIER_BYTE,
|
|
154
|
-
PbcFile.ZK_IDENTIFIER_BYTE,
|
|
155
|
-
]);
|
|
156
181
|
//# sourceMappingURL=PbcFile.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PbcFile.js","sourceRoot":"","sources":["../../src/main/PbcFile.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;GAgBG;;;AAEH,
|
|
1
|
+
{"version":3,"file":"PbcFile.js","sourceRoot":"","sources":["../../src/main/PbcFile.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;GAgBG;;;AAEH,0EAAuE;AACvE,yCAAsC;AAEtC;;;;;;;;;;;;;;GAcG;AACH,MAAa,OAAO;IAelB,YAAoB,QAA6B;QAC/C,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC3B,CAAC;IAED;;;;;;;;OAQG;IACI,MAAM,CAAC,SAAS,CAAC,QAAgB;QACtC,MAAM,WAAW,GAAuB,IAAI,uCAAkB,CAAC,QAAQ,CAAC,CAAC;QACzE,MAAM,gBAAgB,GAAG,WAAW,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;QAElD,IAAI,CAAC,gBAAgB,CAAC,MAAM,CAAC,OAAO,CAAC,gBAAgB,CAAC,EAAE,CAAC;YACvD,MAAM,KAAK,CAAC,wCAAwC,CAAC,CAAC;QACxD,CAAC;QAED,IAAI,QAAQ,GAAwB,IAAI,GAAG,EAAE,CAAC;QAC9C,IAAI,CAAC;YACH,QAAQ,GAAG,mBAAQ,CAA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AAG,CAAC;AAEzD,iFAAiF;AAC1D,4BAAoB,GAAW,GAAG,CAAC;AAE1D,iFAAiF;AAC1D,0BAAkB,GAAW,GAAG,CAAC"}
|
|
@@ -10,57 +10,21 @@ import { BigEndianByteInput } from "@secata-public/bitmanipulation-ts";
|
|
|
10
10
|
* href="https://partisiablockchain.gitlab.io/documentation/smart-contracts/smart-contract-binary-formats.html#partisia-blockchain-file-format">.pbc
|
|
11
11
|
* file</a>.
|
|
12
12
|
*/
|
|
13
|
-
export declare
|
|
14
|
-
/** Map from section identifiers to content bytes. */
|
|
15
|
-
private readonly sections;
|
|
16
|
-
/** Ordered set of allowed identifiers for the given sections. */
|
|
17
|
-
readonly allowedIdentifiers: Buffer;
|
|
18
|
-
/**
|
|
19
|
-
* Creates an empty collection of sections.
|
|
20
|
-
*
|
|
21
|
-
* @param allowedIdentifiers byte array containing the allowed identifiers for the given sections.
|
|
22
|
-
*/
|
|
23
|
-
constructor(allowedIdentifiers: Buffer);
|
|
24
|
-
/**
|
|
25
|
-
* Returns the bytes of the section corresponding to the given identifier.
|
|
26
|
-
*
|
|
27
|
-
* @param identifier identifier of the section to be returned
|
|
28
|
-
* @return the section content bytes
|
|
29
|
-
*/
|
|
30
|
-
getSection(identifier: number): Buffer | undefined;
|
|
13
|
+
export declare const Sections: {
|
|
31
14
|
/**
|
|
32
15
|
* Serializes the added sections into a byte array. The sections are ordered by their identifiers
|
|
33
16
|
* from lowest to highest.
|
|
34
17
|
*
|
|
18
|
+
* @param sections section to write
|
|
35
19
|
* @return the serialized sections.
|
|
36
20
|
*/
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Adds a section of bytes with a given header byte to the collection of sections. If a section
|
|
40
|
-
* with the provided identifier has already been added then the content of that section is
|
|
41
|
-
* overwritten.
|
|
42
|
-
*
|
|
43
|
-
* @param identifier identifier byte indicating the type of the section to be added.
|
|
44
|
-
* @param sectionBytes the binary data of the section to be added.
|
|
45
|
-
* @throws Error if the given identifier is not in the set of allowed identifiers.
|
|
46
|
-
*/
|
|
47
|
-
addSection(identifier: number, sectionBytes: Buffer): void;
|
|
48
|
-
/**
|
|
49
|
-
* Check if the sections contains a section with the given identifier.
|
|
50
|
-
*
|
|
51
|
-
* @param identifier identifier byte indicating the type of the section.
|
|
52
|
-
* @return whether a section with the given identifier is contained.
|
|
53
|
-
*/
|
|
54
|
-
hasSection(identifier: number): boolean;
|
|
21
|
+
serializeSections(sections: Map<number, Buffer>): Buffer;
|
|
55
22
|
/**
|
|
56
23
|
* Creates Sections by parsing sectioned binary data. Sections must come in ascending order by id.
|
|
57
24
|
*
|
|
58
|
-
* @param
|
|
59
|
-
* @param allowedIdentifiers List of allowed section identifiers
|
|
25
|
+
* @param bytes Stream of bytes to parse.
|
|
60
26
|
* @return the parsed sections.
|
|
61
27
|
* @exception Error If the input bytes are encoded incorrectly.
|
|
62
28
|
*/
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
private static readSectionContent;
|
|
66
|
-
}
|
|
29
|
+
fromBytes(bytes: BigEndianByteInput | Buffer): Map<number, Buffer>;
|
|
30
|
+
};
|
package/target/main/Sections.js
CHANGED
|
@@ -30,84 +30,39 @@ const bitmanipulation_ts_1 = require("@secata-public/bitmanipulation-ts");
|
|
|
30
30
|
* href="https://partisiablockchain.gitlab.io/documentation/smart-contracts/smart-contract-binary-formats.html#partisia-blockchain-file-format">.pbc
|
|
31
31
|
* file</a>.
|
|
32
32
|
*/
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Creates an empty collection of sections.
|
|
36
|
-
*
|
|
37
|
-
* @param allowedIdentifiers byte array containing the allowed identifiers for the given sections.
|
|
38
|
-
*/
|
|
39
|
-
constructor(allowedIdentifiers) {
|
|
40
|
-
this.sections = new Map();
|
|
41
|
-
this.allowedIdentifiers = allowedIdentifiers;
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* Returns the bytes of the section corresponding to the given identifier.
|
|
45
|
-
*
|
|
46
|
-
* @param identifier identifier of the section to be returned
|
|
47
|
-
* @return the section content bytes
|
|
48
|
-
*/
|
|
49
|
-
getSection(identifier) {
|
|
50
|
-
return this.sections.get(identifier);
|
|
51
|
-
}
|
|
33
|
+
exports.Sections = {
|
|
52
34
|
/**
|
|
53
35
|
* Serializes the added sections into a byte array. The sections are ordered by their identifiers
|
|
54
36
|
* from lowest to highest.
|
|
55
37
|
*
|
|
38
|
+
* @param sections section to write
|
|
56
39
|
* @return the serialized sections.
|
|
57
40
|
*/
|
|
58
|
-
|
|
59
|
-
return bitmanipulation_ts_1.BigEndianByteOutput.serialize((
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
41
|
+
serializeSections(sections) {
|
|
42
|
+
return bitmanipulation_ts_1.BigEndianByteOutput.serialize((byteOutput) => {
|
|
43
|
+
const entries = Array.from(sections.entries());
|
|
44
|
+
entries.sort((a, b) => a[0] - b[0]);
|
|
45
|
+
for (const [id, section] of entries) {
|
|
46
|
+
byteOutput.writeU8(id);
|
|
47
|
+
byteOutput.writeU32(section.length);
|
|
48
|
+
byteOutput.writeBytes(section);
|
|
66
49
|
}
|
|
67
50
|
});
|
|
68
|
-
}
|
|
69
|
-
/**
|
|
70
|
-
* Adds a section of bytes with a given header byte to the collection of sections. If a section
|
|
71
|
-
* with the provided identifier has already been added then the content of that section is
|
|
72
|
-
* overwritten.
|
|
73
|
-
*
|
|
74
|
-
* @param identifier identifier byte indicating the type of the section to be added.
|
|
75
|
-
* @param sectionBytes the binary data of the section to be added.
|
|
76
|
-
* @throws Error if the given identifier is not in the set of allowed identifiers.
|
|
77
|
-
*/
|
|
78
|
-
addSection(identifier, sectionBytes) {
|
|
79
|
-
if (this.allowedIdentifiers.indexOf(identifier) === -1) {
|
|
80
|
-
throw Error(`Section identifier ${identifier} is not allowed.`);
|
|
81
|
-
}
|
|
82
|
-
this.sections.set(identifier, sectionBytes);
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* Check if the sections contains a section with the given identifier.
|
|
86
|
-
*
|
|
87
|
-
* @param identifier identifier byte indicating the type of the section.
|
|
88
|
-
* @return whether a section with the given identifier is contained.
|
|
89
|
-
*/
|
|
90
|
-
hasSection(identifier) {
|
|
91
|
-
return this.sections.has(identifier);
|
|
92
|
-
}
|
|
51
|
+
},
|
|
93
52
|
/**
|
|
94
53
|
* Creates Sections by parsing sectioned binary data. Sections must come in ascending order by id.
|
|
95
54
|
*
|
|
96
|
-
* @param
|
|
97
|
-
* @param allowedIdentifiers List of allowed section identifiers
|
|
55
|
+
* @param bytes Stream of bytes to parse.
|
|
98
56
|
* @return the parsed sections.
|
|
99
57
|
* @exception Error If the input bytes are encoded incorrectly.
|
|
100
58
|
*/
|
|
101
|
-
|
|
102
|
-
const
|
|
59
|
+
fromBytes(bytes) {
|
|
60
|
+
const byteInput = Buffer.isBuffer(bytes) ? new bitmanipulation_ts_1.BigEndianByteInput(bytes) : bytes;
|
|
61
|
+
const sections = new Map();
|
|
103
62
|
let previousSectionId = 0;
|
|
104
|
-
let sectionId =
|
|
63
|
+
let sectionId = readNextSectionId(byteInput);
|
|
105
64
|
while (sectionId !== null) {
|
|
106
|
-
const
|
|
107
|
-
if (maxIdReached) {
|
|
108
|
-
throw Error(`Invalid section ${sectionId}: Sections past a section with the id 255 are not allowed.`);
|
|
109
|
-
}
|
|
110
|
-
const duplicateSectionId = sections.hasSection(sectionId);
|
|
65
|
+
const duplicateSectionId = sections.has(sectionId);
|
|
111
66
|
if (duplicateSectionId) {
|
|
112
67
|
throw Error(`Invalid section ${sectionId}: Duplicated section id`);
|
|
113
68
|
}
|
|
@@ -116,27 +71,26 @@ class Sections {
|
|
|
116
71
|
throw Error(`Invalid section ${sectionId}: incorrectly ordered. Expected section` +
|
|
117
72
|
` with id of at least ${previousSectionId + 1}`);
|
|
118
73
|
}
|
|
119
|
-
const sectionContent =
|
|
120
|
-
sections.
|
|
74
|
+
const sectionContent = readSectionContent(byteInput);
|
|
75
|
+
sections.set(sectionId, sectionContent);
|
|
121
76
|
previousSectionId = sectionId;
|
|
122
|
-
sectionId =
|
|
77
|
+
sectionId = readNextSectionId(byteInput);
|
|
123
78
|
}
|
|
124
79
|
return sections;
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
function readNextSectionId(byteInput) {
|
|
83
|
+
try {
|
|
84
|
+
return byteInput.readU8();
|
|
125
85
|
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
return byteInput.readU8();
|
|
129
|
-
}
|
|
130
|
-
catch (_a) {
|
|
131
|
-
return null;
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
static readSectionContent(byteInput) {
|
|
135
|
-
// Read length of the section's content
|
|
136
|
-
const sectionLen = byteInput.readU32();
|
|
137
|
-
// Try to read and return the indicated number of bytes
|
|
138
|
-
return byteInput.readBytes(sectionLen);
|
|
86
|
+
catch (_a) {
|
|
87
|
+
return null;
|
|
139
88
|
}
|
|
140
89
|
}
|
|
141
|
-
|
|
90
|
+
function readSectionContent(byteInput) {
|
|
91
|
+
// Read length of the section's content
|
|
92
|
+
const sectionLen = byteInput.readU32();
|
|
93
|
+
// Try to read and return the indicated number of bytes
|
|
94
|
+
return byteInput.readBytes(sectionLen);
|
|
95
|
+
}
|
|
142
96
|
//# sourceMappingURL=Sections.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Sections.js","sourceRoot":"","sources":["../../src/main/Sections.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;GAgBG;;;AAEH,0EAA4F;AAE5F;;;;;;;;;;GAUG;
|
|
1
|
+
{"version":3,"file":"Sections.js","sourceRoot":"","sources":["../../src/main/Sections.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;GAgBG;;;AAEH,0EAA4F;AAE5F;;;;;;;;;;GAUG;AACU,QAAA,QAAQ,GAAG;IACtB;;;;;;OAMG;IACH,iBAAiB,CAAC,QAA6B;QAC7C,OAAO,wCAAmB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