esp-nvs-utils 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/LICENSE +185 -0
- package/README.md +111 -0
- package/bin/esp-nvs-decode.cjs +13 -0
- package/dist/cjs/decoder-C-ve19I_.js +284 -0
- package/dist/cjs/decoder.cjs +248 -0
- package/dist/cjs/esp-nvs-decode.cjs +56 -0
- package/dist/cjs/index.cjs +278 -0
- package/dist/esm/decoder-mrjRj6rx.js +249 -0
- package/dist/esm/decoder.js +219 -0
- package/dist/esm/esp-nvs-decode.js +31 -0
- package/dist/esm/index.js +276 -0
- package/dist/types/cli/esp-nvs-decode.d.ts +1 -0
- package/dist/types/constants.d.ts +49 -0
- package/dist/types/crc.d.ts +9 -0
- package/dist/types/decoder.d.ts +8 -0
- package/dist/types/encoder.d.ts +7 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/types.d.ts +23 -0
- package/package.json +44 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction, and
|
|
10
|
+
distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by the
|
|
13
|
+
copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all other
|
|
16
|
+
entities that control, are controlled by, or are under common control with
|
|
17
|
+
that entity. For the purposes of this definition, "control" means (i) the
|
|
18
|
+
power, direct or indirect, to cause the direction or management of such
|
|
19
|
+
entity, whether by contract or otherwise, or (ii) ownership of fifty percent
|
|
20
|
+
(50%) or more of the outstanding shares, or (iii) beneficial ownership of
|
|
21
|
+
such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity exercising
|
|
24
|
+
permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications, including
|
|
27
|
+
but not limited to software source code, documentation source, and
|
|
28
|
+
configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical transformation or
|
|
31
|
+
translation of a Source form, including but not limited to compiled object
|
|
32
|
+
code, generated documentation, and conversions to other media types.
|
|
33
|
+
|
|
34
|
+
"Work" shall mean the work of authorship, whether in Source or Object form,
|
|
35
|
+
made available under the License, as indicated by a copyright notice that is
|
|
36
|
+
included in or attached to the work (an example is provided in the Appendix
|
|
37
|
+
below).
|
|
38
|
+
|
|
39
|
+
"Derivative Works" shall mean any work, whether in Source or Object form, that
|
|
40
|
+
is based on (or derived from) the Work and for which the editorial revisions,
|
|
41
|
+
annotations, elaborations, or other modifications represent, as a whole, an
|
|
42
|
+
original work of authorship. For the purposes of this License, Derivative
|
|
43
|
+
Works shall not include works that remain separable from, or merely link (or
|
|
44
|
+
bind by name) to the interfaces of, the Work and Derivative Works thereof.
|
|
45
|
+
|
|
46
|
+
"Contribution" shall mean any work of authorship, including the original
|
|
47
|
+
version of the Work and any modifications or additions to that Work or
|
|
48
|
+
Derivative Works thereof, that is intentionally submitted to Licensor for
|
|
49
|
+
inclusion in the Work by the copyright owner or by an individual or Legal
|
|
50
|
+
Entity authorized to submit on behalf of the copyright owner. For the purposes
|
|
51
|
+
of this definition, "submitted" means any form of electronic, verbal, or
|
|
52
|
+
written communication sent to the Licensor or its representatives, including
|
|
53
|
+
but not limited to communication on electronic mailing lists, source code
|
|
54
|
+
control systems, and issue tracking systems that are managed by, or on behalf
|
|
55
|
+
of, the Licensor for the purpose of discussing and improving the Work, but
|
|
56
|
+
excluding communication that is conspicuously marked or otherwise designated in
|
|
57
|
+
writing by the copyright owner as "Not a Contribution."
|
|
58
|
+
|
|
59
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf
|
|
60
|
+
of whom a Contribution has been received by Licensor and subsequently
|
|
61
|
+
incorporated within the Work.
|
|
62
|
+
|
|
63
|
+
2. Grant of Copyright License. Subject to the terms and conditions of this
|
|
64
|
+
License, each Contributor hereby grants to You a perpetual, worldwide,
|
|
65
|
+
non-exclusive, no-charge, royalty-free, irrevocable copyright license to
|
|
66
|
+
reproduce, prepare Derivative Works of, publicly display, publicly perform,
|
|
67
|
+
sublicense, and distribute the Work and such Derivative Works in Source or
|
|
68
|
+
Object form.
|
|
69
|
+
|
|
70
|
+
3. Grant of Patent License. Subject to the terms and conditions of this
|
|
71
|
+
License, each Contributor hereby grants to You a perpetual, worldwide,
|
|
72
|
+
non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this
|
|
73
|
+
section) patent license to make, have made, use, offer to sell, sell, import,
|
|
74
|
+
and otherwise transfer the Work, where such license applies only to those
|
|
75
|
+
patent claims licensable by such Contributor that are necessarily infringed by
|
|
76
|
+
their Contribution(s) alone or by combination of their Contribution(s) with the
|
|
77
|
+
Work to which such Contribution(s) was submitted. If You institute patent
|
|
78
|
+
litigation against any entity (including a cross-claim or counterclaim in a
|
|
79
|
+
lawsuit) alleging that the Work or a Contribution incorporated within the Work
|
|
80
|
+
constitutes direct or contributory patent infringement, then any patent
|
|
81
|
+
licenses granted to You under this License for that Work shall terminate as of
|
|
82
|
+
the date such litigation is filed.
|
|
83
|
+
|
|
84
|
+
4. Redistribution. You may reproduce and distribute copies of the Work or
|
|
85
|
+
Derivative Works thereof in any medium, with or without modifications, and in
|
|
86
|
+
Source or Object form, provided that You meet the following conditions:
|
|
87
|
+
|
|
88
|
+
(a) You must give any other recipients of the Work or Derivative Works a copy
|
|
89
|
+
of this License; and
|
|
90
|
+
|
|
91
|
+
(b) You must cause any modified files to carry prominent notices stating that
|
|
92
|
+
You changed the files; and
|
|
93
|
+
|
|
94
|
+
(c) You must retain, in the Source form of any Derivative Works that You
|
|
95
|
+
distribute, all copyright, patent, trademark, and attribution notices from
|
|
96
|
+
the Source form of the Work, excluding those notices that do not pertain to
|
|
97
|
+
any part of the Derivative Works; and
|
|
98
|
+
|
|
99
|
+
(d) If the Work includes a "NOTICE" text file as part of its distribution,
|
|
100
|
+
then any Derivative Works that You distribute must include a readable copy
|
|
101
|
+
of the attribution notices contained within such NOTICE file, excluding
|
|
102
|
+
those notices that do not pertain to any part of the Derivative Works, in
|
|
103
|
+
at least one of the following places: within a NOTICE text file distributed
|
|
104
|
+
as part of the Derivative Works; within the Source form or documentation,
|
|
105
|
+
if provided along with the Derivative Works; or, within a display generated
|
|
106
|
+
by the Derivative Works, if and wherever such third-party notices normally
|
|
107
|
+
appear. The contents of the NOTICE file are for informational purposes only
|
|
108
|
+
and do not modify the License. You may add Your own attribution notices
|
|
109
|
+
within Derivative Works that You distribute, alongside or as an addendum to
|
|
110
|
+
the NOTICE text from the Work, provided that such additional attribution
|
|
111
|
+
notices cannot be construed as modifying the License.
|
|
112
|
+
|
|
113
|
+
You may add Your own copyright statement to Your modifications and may provide
|
|
114
|
+
additional or different license terms and conditions for use, reproduction, or
|
|
115
|
+
distribution of Your modifications, or for any such Derivative Works as a
|
|
116
|
+
whole, provided Your use, reproduction, and distribution of the Work otherwise
|
|
117
|
+
complies with the conditions stated in this License.
|
|
118
|
+
|
|
119
|
+
5. Submission of Contributions. Unless You explicitly state otherwise, any
|
|
120
|
+
Contribution intentionally submitted for inclusion in the Work by You to the
|
|
121
|
+
Licensor shall be under the terms and conditions of this License, without any
|
|
122
|
+
additional terms or conditions. Notwithstanding the above, nothing herein shall
|
|
123
|
+
supersede or modify the terms of any separate license agreement you may have
|
|
124
|
+
executed with Licensor regarding such Contributions.
|
|
125
|
+
|
|
126
|
+
6. Trademarks. This License does not grant permission to use the trade names,
|
|
127
|
+
trademarks, service marks, or product names of the Licensor, except as
|
|
128
|
+
required for reasonable and customary use in describing the origin of the Work
|
|
129
|
+
and reproducing the content of the NOTICE file.
|
|
130
|
+
|
|
131
|
+
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in
|
|
132
|
+
writing, Licensor provides the Work (and each Contributor provides its
|
|
133
|
+
Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
134
|
+
KIND, either express or implied, including, without limitation, any warranties
|
|
135
|
+
or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
136
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
137
|
+
appropriateness of using or redistributing the Work and assume any risks
|
|
138
|
+
associated with Your exercise of permissions under this License.
|
|
139
|
+
|
|
140
|
+
8. Limitation of Liability. In no event and under no legal theory, whether in
|
|
141
|
+
tort (including negligence), contract, or otherwise, unless required by
|
|
142
|
+
applicable law (such as deliberate and grossly negligent acts) or agreed to in
|
|
143
|
+
writing, shall any Contributor be liable to You for damages, including any
|
|
144
|
+
direct, indirect, special, incidental, or consequential damages of any
|
|
145
|
+
character arising as a result of this License or out of the use or inability to
|
|
146
|
+
use the Work (including but not limited to damages for loss of goodwill, work
|
|
147
|
+
stoppage, computer failure or malfunction, or any and all other commercial
|
|
148
|
+
damages or losses), even if such Contributor has been advised of the
|
|
149
|
+
possibility of such damages.
|
|
150
|
+
|
|
151
|
+
9. Accepting Warranty or Additional Liability. While redistributing the Work or
|
|
152
|
+
Derivative Works thereof, You may choose to offer, and charge a fee for,
|
|
153
|
+
acceptance of support, warranty, indemnity, or other liability obligations
|
|
154
|
+
and/or rights consistent with this License. However, in accepting such
|
|
155
|
+
obligations, You may act only on Your own behalf and on Your sole
|
|
156
|
+
responsibility, not on behalf of any other Contributor, and only if You agree
|
|
157
|
+
to indemnify, defend, and hold each Contributor harmless for any liability
|
|
158
|
+
incurred by, or claims asserted against, such Contributor by reason of your
|
|
159
|
+
accepting any such warranty or additional liability.
|
|
160
|
+
|
|
161
|
+
END OF TERMS AND CONDITIONS
|
|
162
|
+
|
|
163
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
164
|
+
|
|
165
|
+
To apply the Apache License to your work, attach the following boilerplate
|
|
166
|
+
notice, with the fields enclosed by brackets "[]" replaced with your own
|
|
167
|
+
identifying information. (Don't include the brackets!) The text should be
|
|
168
|
+
enclosed in the appropriate comment syntax for the file format. We also
|
|
169
|
+
recommend that a file or class name and description of purpose be included on
|
|
170
|
+
the same "printed page" as the copyright notice for easier identification
|
|
171
|
+
within third-party archives.
|
|
172
|
+
|
|
173
|
+
Copyright [yyyy] [name of copyright owner]
|
|
174
|
+
|
|
175
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
176
|
+
you may not use this file except in compliance with the License.
|
|
177
|
+
You may obtain a copy of the License at
|
|
178
|
+
|
|
179
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
180
|
+
|
|
181
|
+
Unless required by applicable law or agreed to in writing, software
|
|
182
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
183
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
184
|
+
See the License for the specific language governing permissions and
|
|
185
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# esp-nvs-utils-js
|
|
2
|
+
|
|
3
|
+
NVS (Non-volatile storage) partition parser and encoder for [Espressif's](https://www.espressif.com/) ESP32 line of chips. It is written in TypeScript and supports running in the browser and node.js
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
See the official [esp-idf documentation about the NVS library](https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/storage/nvs_flash.html) for more information about NVS.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```sh
|
|
11
|
+
npm i --save esp-nvs-utils
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## API
|
|
15
|
+
|
|
16
|
+
```ts
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Encoding/type of an NVS entry value.
|
|
20
|
+
*/
|
|
21
|
+
export type NVSEncoding = "u8" | "i8" | "u16" | "i16" | "u32" | "i32" | "u64" | "i64" | "string" | "blob_data";
|
|
22
|
+
/**
|
|
23
|
+
* A single key-value entry within a namespace.
|
|
24
|
+
*
|
|
25
|
+
* value type by encoding:
|
|
26
|
+
* u8/i8/u16/i16/u32/i32 → number
|
|
27
|
+
* u64/i64 → bigint (full 64-bit range)
|
|
28
|
+
* string → string
|
|
29
|
+
* blob_data → Uint8Array
|
|
30
|
+
*/
|
|
31
|
+
export interface NVSValue {
|
|
32
|
+
name: string;
|
|
33
|
+
encoding: NVSEncoding;
|
|
34
|
+
value: number | bigint | string | Uint8Array;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* A decoded NVS partition.
|
|
38
|
+
* Keys are namespace names, values are the entries within that namespace.
|
|
39
|
+
*/
|
|
40
|
+
export type NVSNamespaces = Record<string, NVSValue[]>;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Encode an NVSNamespaces object into a binary NVS partition.
|
|
44
|
+
* Output is always V2 (multipage blob support enabled).
|
|
45
|
+
* Partition size is auto-calculated based on data volume.
|
|
46
|
+
*/
|
|
47
|
+
export function encodeNVS(namespaces: NVSNamespaces): Uint8Array;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Decode an NVS partition binary into structured namespaces.
|
|
51
|
+
*
|
|
52
|
+
* Handles V1 (single-page blobs) and V2 (multi-page blobs).
|
|
53
|
+
* Does not support encrypted partitions.
|
|
54
|
+
*/
|
|
55
|
+
export declare function decodeNVS(data: ArrayBuffer | Uint8Array): NVSNamespaces;
|
|
56
|
+
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Usage
|
|
60
|
+
|
|
61
|
+
Consult the following example:
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
import { decodeNVS, encodeNVS, type NVSNamespaces } from "esp-nvs-utils";
|
|
65
|
+
|
|
66
|
+
const input: NVSNamespaces = {
|
|
67
|
+
namespace1: [
|
|
68
|
+
{ name: "my_blob", encoding: "blob_data", value: new Uint8Array([1, 2, 3, 4]) },
|
|
69
|
+
],
|
|
70
|
+
namespace2: [
|
|
71
|
+
{ name: "my_int", encoding: "u32", value: 42 },
|
|
72
|
+
{ name: "a_str", encoding: "string", value: "ExampleNetwork" },
|
|
73
|
+
|
|
74
|
+
],
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const binary = encodeNVS(input);
|
|
78
|
+
|
|
79
|
+
const decoded = decodeNVS(binary);
|
|
80
|
+
|
|
81
|
+
console.log(decoded.namespace1[0]);
|
|
82
|
+
|
|
83
|
+
console.log(decoded.namespace2[0]);
|
|
84
|
+
console.log(decoded.namespace2[1]);
|
|
85
|
+
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## Development
|
|
89
|
+
|
|
90
|
+
```sh
|
|
91
|
+
# Install dev dependenciess
|
|
92
|
+
npm install
|
|
93
|
+
|
|
94
|
+
# Run unit-tests (requires Python and python-cryptography)
|
|
95
|
+
npm run test
|
|
96
|
+
|
|
97
|
+
# Build
|
|
98
|
+
npm run build
|
|
99
|
+
|
|
100
|
+
# Format
|
|
101
|
+
npm run format
|
|
102
|
+
|
|
103
|
+
# Lint
|
|
104
|
+
npm run lint
|
|
105
|
+
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
## License
|
|
110
|
+
|
|
111
|
+
Apache-2.0, see [./LICENSE](./LICENSE). This project includes the original Python NVS parser/generator pulled from esp-idf under `./python_original`, which is used during unit-tests. It is licensed under the same license.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const fs = require("node:fs");
|
|
4
|
+
const path = require("node:path");
|
|
5
|
+
|
|
6
|
+
const cliPath = path.resolve(__dirname, "..", "dist", "cjs", "esp-nvs-decode.cjs");
|
|
7
|
+
|
|
8
|
+
if (!fs.existsSync(cliPath)) {
|
|
9
|
+
console.error("Build output not found. Run `npm run build` first.");
|
|
10
|
+
process.exit(1);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
require(cliPath).main(process.argv.slice(2));
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
//#region src/crc.ts
|
|
2
|
+
/**
|
|
3
|
+
* Pure CRC-32 implementation compatible with zlib.crc32 (polynomial 0xEDB88320).
|
|
4
|
+
* Browser-safe: no Node.js dependencies.
|
|
5
|
+
*/
|
|
6
|
+
const CRC32_TABLE = (() => {
|
|
7
|
+
const table = new Uint32Array(256);
|
|
8
|
+
for (let i = 0; i < 256; i++) {
|
|
9
|
+
let c = i;
|
|
10
|
+
for (let j = 0; j < 8; j++) c = c & 1 ? 3988292384 ^ c >>> 1 : c >>> 1;
|
|
11
|
+
table[i] = c;
|
|
12
|
+
}
|
|
13
|
+
return table;
|
|
14
|
+
})();
|
|
15
|
+
/**
|
|
16
|
+
* Compute CRC-32 of data, matching Python's `zlib.crc32(data, seed)`.
|
|
17
|
+
* The default seed of 0xFFFFFFFF matches the NVS usage pattern.
|
|
18
|
+
*/
|
|
19
|
+
function crc32(data, seed = 4294967295) {
|
|
20
|
+
let crc = seed >>> 0;
|
|
21
|
+
for (let i = 0; i < data.length; i++) crc = CRC32_TABLE[(crc ^ data[i]) & 255] ^ crc >>> 8;
|
|
22
|
+
return crc >>> 0;
|
|
23
|
+
}
|
|
24
|
+
//#endregion
|
|
25
|
+
//#region src/constants.ts
|
|
26
|
+
/** NVS page size in bytes. */
|
|
27
|
+
const PAGE_SIZE = 4096;
|
|
28
|
+
/** Page state: page is currently being written. */
|
|
29
|
+
const PAGE_STATE_ACTIVE = 4294967294;
|
|
30
|
+
/** Page state: page is full, no more entries can be written. */
|
|
31
|
+
const PAGE_STATE_FULL = 4294967292;
|
|
32
|
+
/** Map from NVS encoding string to type code. */
|
|
33
|
+
const ENCODING_TO_TYPE_CODE = {
|
|
34
|
+
u8: 1,
|
|
35
|
+
i8: 17,
|
|
36
|
+
u16: 2,
|
|
37
|
+
i16: 18,
|
|
38
|
+
u32: 4,
|
|
39
|
+
i32: 20,
|
|
40
|
+
u64: 8,
|
|
41
|
+
i64: 24,
|
|
42
|
+
string: 33,
|
|
43
|
+
blob_data: 66
|
|
44
|
+
};
|
|
45
|
+
//#endregion
|
|
46
|
+
//#region src/decoder.ts
|
|
47
|
+
function readKey(entry) {
|
|
48
|
+
let end = 8;
|
|
49
|
+
while (end < 24 && entry[end] !== 0) end++;
|
|
50
|
+
return new TextDecoder().decode(entry.subarray(8, end));
|
|
51
|
+
}
|
|
52
|
+
function entryBitmapState(bitmap, idx) {
|
|
53
|
+
const bitNum = idx * 2;
|
|
54
|
+
const byteIdx = bitNum >> 3;
|
|
55
|
+
const bitOffset = bitNum & 7;
|
|
56
|
+
return bitmap[byteIdx] >> bitOffset & 3;
|
|
57
|
+
}
|
|
58
|
+
function parsePrimitive(typeCode, inlineData) {
|
|
59
|
+
const view = new DataView(inlineData.buffer, inlineData.byteOffset, inlineData.byteLength);
|
|
60
|
+
switch (typeCode) {
|
|
61
|
+
case 1: return view.getUint8(0);
|
|
62
|
+
case 17: return view.getInt8(0);
|
|
63
|
+
case 2: return view.getUint16(0, true);
|
|
64
|
+
case 18: return view.getInt16(0, true);
|
|
65
|
+
case 4: return view.getUint32(0, true);
|
|
66
|
+
case 20: return view.getInt32(0, true);
|
|
67
|
+
case 8: return view.getBigUint64(0, true);
|
|
68
|
+
case 24: return view.getBigInt64(0, true);
|
|
69
|
+
default: throw new Error(`Unknown primitive type code: 0x${typeCode.toString(16)}`);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
const PRIMITIVE_TYPES = new Set([
|
|
73
|
+
1,
|
|
74
|
+
17,
|
|
75
|
+
2,
|
|
76
|
+
18,
|
|
77
|
+
4,
|
|
78
|
+
20,
|
|
79
|
+
8,
|
|
80
|
+
24
|
|
81
|
+
]);
|
|
82
|
+
const VARLEN_TYPES = new Set([
|
|
83
|
+
33,
|
|
84
|
+
65,
|
|
85
|
+
66,
|
|
86
|
+
72
|
|
87
|
+
]);
|
|
88
|
+
/**
|
|
89
|
+
* Parse all valid written entries from a single 4096-byte page.
|
|
90
|
+
* Returns entries in slot order, including namespace entries.
|
|
91
|
+
*/
|
|
92
|
+
function parsePage(pageData) {
|
|
93
|
+
const bitmap = pageData.subarray(32, 64);
|
|
94
|
+
const entries = [];
|
|
95
|
+
let i = 0;
|
|
96
|
+
while (i < 126) {
|
|
97
|
+
if (entryBitmapState(bitmap, i) !== 2) {
|
|
98
|
+
i++;
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
const entryOff = 64 + i * 32;
|
|
102
|
+
const entry = pageData.subarray(entryOff, entryOff + 32);
|
|
103
|
+
const nsIdx = entry[0];
|
|
104
|
+
const typeCode = entry[1];
|
|
105
|
+
let span = entry[2];
|
|
106
|
+
const chunkIndex = entry[3];
|
|
107
|
+
const safeSpan = span === 0 || span === 255 ? 1 : span;
|
|
108
|
+
const parsed = {
|
|
109
|
+
nsIdx,
|
|
110
|
+
typeCode,
|
|
111
|
+
span: safeSpan,
|
|
112
|
+
chunkIndex,
|
|
113
|
+
key: readKey(entry),
|
|
114
|
+
raw: entry,
|
|
115
|
+
inlineData: entry.subarray(24, 32),
|
|
116
|
+
entryIndex: i
|
|
117
|
+
};
|
|
118
|
+
if (VARLEN_TYPES.has(typeCode) && typeCode !== 72) {
|
|
119
|
+
const childCount = safeSpan - 1;
|
|
120
|
+
if (childCount > 0) {
|
|
121
|
+
const childStart = 64 + (i + 1) * 32;
|
|
122
|
+
const childEnd = childStart + childCount * 32;
|
|
123
|
+
parsed.childData = pageData.subarray(childStart, childEnd);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
entries.push(parsed);
|
|
127
|
+
i += safeSpan;
|
|
128
|
+
}
|
|
129
|
+
return entries;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Decode an NVS partition binary into structured namespaces.
|
|
133
|
+
*
|
|
134
|
+
* Handles V1 (single-page blobs) and V2 (multi-page blobs).
|
|
135
|
+
* Does not support encrypted partitions.
|
|
136
|
+
*/
|
|
137
|
+
function decodeNVS(data) {
|
|
138
|
+
const buf = data instanceof Uint8Array ? data : new Uint8Array(data);
|
|
139
|
+
if (buf.length % 4096 !== 0) throw new Error(`Partition data length ${buf.length} is not aligned to page size ${PAGE_SIZE}`);
|
|
140
|
+
const allEntries = [];
|
|
141
|
+
for (let pageOff = 0; pageOff < buf.length; pageOff += PAGE_SIZE) {
|
|
142
|
+
const page = buf.subarray(pageOff, pageOff + PAGE_SIZE);
|
|
143
|
+
const pageStateRaw = (page[0] | page[1] << 8 | page[2] << 16 | page[3] << 24) >>> 0;
|
|
144
|
+
if (pageStateRaw === 4294967295 || pageStateRaw === 0) continue;
|
|
145
|
+
const version = page[8];
|
|
146
|
+
if (version !== 255 && version !== 254) continue;
|
|
147
|
+
allEntries.push(...parsePage(page));
|
|
148
|
+
}
|
|
149
|
+
const nsIdxToName = /* @__PURE__ */ new Map();
|
|
150
|
+
for (const e of allEntries) if (e.nsIdx === 0 && e.typeCode === 1) {
|
|
151
|
+
const assignedIdx = e.inlineData[0];
|
|
152
|
+
nsIdxToName.set(assignedIdx, e.key);
|
|
153
|
+
}
|
|
154
|
+
const blobDataChunks = /* @__PURE__ */ new Map();
|
|
155
|
+
const blobIndex = /* @__PURE__ */ new Map();
|
|
156
|
+
const v1Blobs = /* @__PURE__ */ new Map();
|
|
157
|
+
for (const e of allEntries) {
|
|
158
|
+
if (e.nsIdx === 0) continue;
|
|
159
|
+
const bk = `${e.nsIdx}:${e.key}`;
|
|
160
|
+
if (e.typeCode === 66) {
|
|
161
|
+
if (!blobDataChunks.has(bk)) blobDataChunks.set(bk, /* @__PURE__ */ new Map());
|
|
162
|
+
const payload = e.childData ?? new Uint8Array(0);
|
|
163
|
+
blobDataChunks.get(bk).set(e.chunkIndex, payload);
|
|
164
|
+
} else if (e.typeCode === 72) {
|
|
165
|
+
const view = new DataView(e.inlineData.buffer, e.inlineData.byteOffset, 8);
|
|
166
|
+
blobIndex.set(bk, {
|
|
167
|
+
totalSize: view.getUint32(0, true),
|
|
168
|
+
chunkCount: e.inlineData[4],
|
|
169
|
+
chunkStart: e.inlineData[5]
|
|
170
|
+
});
|
|
171
|
+
} else if (e.typeCode === 65) {
|
|
172
|
+
const size = new DataView(e.inlineData.buffer, e.inlineData.byteOffset, 8).getUint16(0, true);
|
|
173
|
+
const payload = e.childData ?? new Uint8Array(0);
|
|
174
|
+
v1Blobs.set(bk, {
|
|
175
|
+
size,
|
|
176
|
+
data: payload
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
const result = {};
|
|
181
|
+
for (const e of allEntries) {
|
|
182
|
+
if (e.nsIdx === 0) continue;
|
|
183
|
+
if (e.typeCode === 66 || e.typeCode === 72) continue;
|
|
184
|
+
const nsName = nsIdxToName.get(e.nsIdx);
|
|
185
|
+
if (nsName === void 0) continue;
|
|
186
|
+
if (!result[nsName]) result[nsName] = [];
|
|
187
|
+
const bk = `${e.nsIdx}:${e.key}`;
|
|
188
|
+
if (PRIMITIVE_TYPES.has(e.typeCode)) {
|
|
189
|
+
const val = parsePrimitive(e.typeCode, e.inlineData);
|
|
190
|
+
const enc = e.typeCode === 1 ? "u8" : e.typeCode === 17 ? "i8" : e.typeCode === 2 ? "u16" : e.typeCode === 18 ? "i16" : e.typeCode === 4 ? "u32" : e.typeCode === 20 ? "i32" : e.typeCode === 8 ? "u64" : "i64";
|
|
191
|
+
result[nsName].push({
|
|
192
|
+
name: e.key,
|
|
193
|
+
encoding: enc,
|
|
194
|
+
value: val
|
|
195
|
+
});
|
|
196
|
+
} else if (e.typeCode === 33) {
|
|
197
|
+
const size = new DataView(e.inlineData.buffer, e.inlineData.byteOffset, 8).getUint16(0, true);
|
|
198
|
+
const payload = e.childData ?? new Uint8Array(0);
|
|
199
|
+
payload.subarray(0, size).filter((_, idx) => {
|
|
200
|
+
const slice = payload.subarray(0, size);
|
|
201
|
+
let last = slice.length - 1;
|
|
202
|
+
while (last >= 0 && slice[last] === 0) last--;
|
|
203
|
+
return idx <= last;
|
|
204
|
+
});
|
|
205
|
+
const str = new TextDecoder().decode(payload.subarray(0, Math.max(0, size - 1)));
|
|
206
|
+
result[nsName].push({
|
|
207
|
+
name: e.key,
|
|
208
|
+
encoding: "string",
|
|
209
|
+
value: str
|
|
210
|
+
});
|
|
211
|
+
} else if (e.typeCode === 65) {
|
|
212
|
+
const v1 = v1Blobs.get(bk);
|
|
213
|
+
if (v1) {
|
|
214
|
+
const data = v1.data.subarray(0, v1.size);
|
|
215
|
+
result[nsName].push({
|
|
216
|
+
name: e.key,
|
|
217
|
+
encoding: "blob_data",
|
|
218
|
+
value: new Uint8Array(data)
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
for (const [bk, chunks] of blobDataChunks) {
|
|
224
|
+
const [nsIdxStr, ...keyParts] = bk.split(":");
|
|
225
|
+
const nsIdx = parseInt(nsIdxStr);
|
|
226
|
+
const key = keyParts.join(":");
|
|
227
|
+
const nsName = nsIdxToName.get(nsIdx);
|
|
228
|
+
if (nsName === void 0) continue;
|
|
229
|
+
const idx = blobIndex.get(bk);
|
|
230
|
+
const allChunkData = [...chunks.keys()].sort((a, b) => a - b).map((k) => chunks.get(k));
|
|
231
|
+
const totalRaw = allChunkData.reduce((acc, c) => acc + c.length, 0);
|
|
232
|
+
const assembled = new Uint8Array(totalRaw);
|
|
233
|
+
let off = 0;
|
|
234
|
+
for (const c of allChunkData) {
|
|
235
|
+
assembled.set(c, off);
|
|
236
|
+
off += c.length;
|
|
237
|
+
}
|
|
238
|
+
const trimmed = idx ? assembled.subarray(0, idx.totalSize) : assembled;
|
|
239
|
+
if (!result[nsName]) result[nsName] = [];
|
|
240
|
+
result[nsName].push({
|
|
241
|
+
name: key,
|
|
242
|
+
encoding: "blob_data",
|
|
243
|
+
value: new Uint8Array(trimmed)
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
return result;
|
|
247
|
+
}
|
|
248
|
+
//#endregion
|
|
249
|
+
Object.defineProperty(exports, "ENCODING_TO_TYPE_CODE", {
|
|
250
|
+
enumerable: true,
|
|
251
|
+
get: function() {
|
|
252
|
+
return ENCODING_TO_TYPE_CODE;
|
|
253
|
+
}
|
|
254
|
+
});
|
|
255
|
+
Object.defineProperty(exports, "PAGE_SIZE", {
|
|
256
|
+
enumerable: true,
|
|
257
|
+
get: function() {
|
|
258
|
+
return PAGE_SIZE;
|
|
259
|
+
}
|
|
260
|
+
});
|
|
261
|
+
Object.defineProperty(exports, "PAGE_STATE_ACTIVE", {
|
|
262
|
+
enumerable: true,
|
|
263
|
+
get: function() {
|
|
264
|
+
return PAGE_STATE_ACTIVE;
|
|
265
|
+
}
|
|
266
|
+
});
|
|
267
|
+
Object.defineProperty(exports, "PAGE_STATE_FULL", {
|
|
268
|
+
enumerable: true,
|
|
269
|
+
get: function() {
|
|
270
|
+
return PAGE_STATE_FULL;
|
|
271
|
+
}
|
|
272
|
+
});
|
|
273
|
+
Object.defineProperty(exports, "crc32", {
|
|
274
|
+
enumerable: true,
|
|
275
|
+
get: function() {
|
|
276
|
+
return crc32;
|
|
277
|
+
}
|
|
278
|
+
});
|
|
279
|
+
Object.defineProperty(exports, "decodeNVS", {
|
|
280
|
+
enumerable: true,
|
|
281
|
+
get: function() {
|
|
282
|
+
return decodeNVS;
|
|
283
|
+
}
|
|
284
|
+
});
|