ya-struct 0.0.10 → 0.0.11

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.
@@ -1,122 +0,0 @@
1
- import type { TAbi } from "../common.ts";
2
-
3
- type TCFieldType =
4
- "char" | "unsigned char" |
5
- "short" | "unsigned short" |
6
- "int" | "unsigned int" |
7
- "long" | "unsigned long" |
8
- "long long" | "unsigned long long" |
9
- "float" | "double" | "long double";
10
-
11
- type TFieldType = {
12
- readonly type: "integer";
13
- readonly sizeInBits: number;
14
- readonly signed: boolean;
15
- readonly fixedAbi: Partial<TAbi>;
16
- } | {
17
- readonly type: "float";
18
- readonly sizeInBits: number;
19
- readonly fixedAbi: Partial<TAbi>;
20
- } | {
21
- readonly type: "pointer";
22
- readonly fixedAbi: Partial<TAbi>;
23
- } | {
24
- readonly type: "array";
25
- readonly elementType: TFieldType;
26
- readonly length: number;
27
- } | {
28
- readonly type: "struct";
29
- readonly fields: readonly { readonly name: string; readonly definition: TFieldType }[];
30
- readonly packed: boolean;
31
- readonly fixedAbi: Partial<TAbi>;
32
- } | {
33
- readonly type: "string";
34
- readonly charSizeInBits: number;
35
- readonly nullTerminatorMandatory: boolean;
36
- readonly length: number;
37
- } | {
38
- readonly type: "c-type";
39
- readonly cType: TCFieldType;
40
- readonly fixedAbi: Partial<TAbi>;
41
- };
42
-
43
- type TBasicFieldType = Exclude<TFieldType, { type: "c-type" }>;
44
- type TPrimitiveBasicFieldType = Exclude<TBasicFieldType, { type: "array" } | { type: "struct" } | { type: "string" }>;
45
-
46
- const Int16: TFieldType = {
47
- type: "integer",
48
- sizeInBits: 16,
49
- signed: true,
50
- fixedAbi: {}
51
- } as const;
52
-
53
- const UInt16: TFieldType = {
54
- type: "integer",
55
- sizeInBits: 16,
56
- signed: false,
57
- fixedAbi: {}
58
- } as const;
59
-
60
- const Int32: TFieldType = {
61
- type: "integer",
62
- sizeInBits: 32,
63
- signed: true,
64
- fixedAbi: {}
65
- } as const;
66
-
67
- const Int64: TFieldType = {
68
- type: "integer",
69
- sizeInBits: 64,
70
- signed: true,
71
- fixedAbi: {}
72
- } as const;
73
-
74
- const UInt32: TFieldType = {
75
- type: "integer",
76
- sizeInBits: 32,
77
- signed: false,
78
- fixedAbi: {}
79
- } as const;
80
-
81
- const UInt64: TFieldType = {
82
- type: "integer",
83
- sizeInBits: 64,
84
- signed: false,
85
- fixedAbi: {}
86
- } as const;
87
-
88
- const ascii = ({ length }: { length: number }) => {
89
- return {
90
- type: "string",
91
- charSizeInBits: 8,
92
- nullTerminatorMandatory: true,
93
- length
94
- } as const;
95
- };
96
-
97
- const pointer: TFieldType = {
98
- type: "pointer",
99
- fixedAbi: {}
100
- } as const;
101
-
102
- export type {
103
- TBasicFieldType,
104
- TFieldType,
105
- TCFieldType,
106
- TPrimitiveBasicFieldType
107
- };
108
-
109
- const types = {
110
- Int16,
111
- UInt16,
112
- Int32,
113
- Int64,
114
- UInt32,
115
- UInt64,
116
- ascii,
117
- pointer
118
- } as const;
119
-
120
- export {
121
- types
122
- };
@@ -1,160 +0,0 @@
1
- import type { TEndianness } from "../common.ts";
2
- import type { TValueParser } from "./value.ts";
3
-
4
- type TIntegerParser = TValueParser<bigint>;
5
-
6
- const createIntegerParser = ({
7
- sizeInBits,
8
- signed,
9
- endianness
10
- }: {
11
- sizeInBits: number,
12
- signed: boolean,
13
- endianness: TEndianness
14
- }): TIntegerParser => {
15
-
16
- // eslint-disable-next-line max-statements, complexity
17
- const parse: TIntegerParser["parse"] = ({ data, offsetInBits }) => {
18
- if (data.length < sizeInBits / 8) {
19
- throw Error("BUG: not enough data for integer parsing");
20
- }
21
-
22
- if (offsetInBits !== 0) {
23
- throw Error("unaligned data in integer parser, not supported yet");
24
- }
25
-
26
- const view = new DataView(data.buffer, data.byteOffset);
27
-
28
- if (sizeInBits === 8) {
29
- if (signed) {
30
- return BigInt(view.getInt8(0));
31
- } else {
32
- return BigInt(view.getUint8(0));
33
- }
34
- } else if (sizeInBits === 16) {
35
- if (signed) {
36
- if (endianness === "little") {
37
- return BigInt(view.getInt16(0, true));
38
- } else {
39
- return BigInt(view.getInt16(0, false));
40
- }
41
- } else {
42
- // eslint-disable-next-line no-lonely-if
43
- if (endianness === "little") {
44
- return BigInt(view.getUint16(0, true));
45
- } else {
46
- return BigInt(view.getUint16(0, false));
47
- }
48
- }
49
- } else if (sizeInBits === 32) {
50
- if (signed) {
51
- if (endianness === "little") {
52
- return BigInt(view.getInt32(0, true));
53
- } else {
54
- return BigInt(view.getInt32(0, false));
55
- }
56
- } else {
57
- // eslint-disable-next-line no-lonely-if
58
- if (endianness === "little") {
59
- return BigInt(view.getUint32(0, true));
60
- } else {
61
- return BigInt(view.getUint32(0, false));
62
- }
63
- }
64
- } else if (sizeInBits === 64) {
65
- if (signed) {
66
- if (endianness === "little") {
67
- return view.getBigInt64(0, true);
68
- } else {
69
- return view.getBigInt64(0, false);
70
- }
71
- } else {
72
- // eslint-disable-next-line no-lonely-if
73
- if (endianness === "little") {
74
- return view.getBigUint64(0, true);
75
- } else {
76
- return view.getBigUint64(0, false);
77
- }
78
- }
79
- }
80
-
81
- throw Error("not implemented yet");
82
- };
83
-
84
- // eslint-disable-next-line max-statements, complexity
85
- const format: TIntegerParser["format"] = ({ value, target, offsetInBits }) => {
86
- if (offsetInBits !== 0) {
87
- throw Error("unaligned data in integer formatter, not supported yet");
88
- }
89
-
90
- if (typeof value !== "bigint") {
91
- throw Error("invalid value type for integer formatter");
92
- }
93
-
94
- const view = new DataView(target.buffer, target.byteOffset);
95
-
96
- if (sizeInBits === 8) {
97
- if (signed) {
98
- view.setInt8(0, Number(value));
99
- } else {
100
- view.setUint8(0, Number(value));
101
- }
102
- } else if (sizeInBits === 16) {
103
- if (signed) {
104
- if (endianness === "little") {
105
- view.setInt16(0, Number(value), true);
106
- } else {
107
- view.setInt16(0, Number(value), false);
108
- }
109
- } else {
110
- // eslint-disable-next-line no-lonely-if
111
- if (endianness === "little") {
112
- view.setUint16(0, Number(value), true);
113
- } else {
114
- view.setUint16(0, Number(value), false);
115
- }
116
- }
117
- } else if (sizeInBits === 32) {
118
- if (signed) {
119
- if (endianness === "little") {
120
- view.setInt32(0, Number(value), true);
121
- } else {
122
- view.setInt32(0, Number(value), false);
123
- }
124
- } else {
125
- // eslint-disable-next-line no-lonely-if
126
- if (endianness === "little") {
127
- view.setUint32(0, Number(value), true);
128
- } else {
129
- view.setUint32(0, Number(value), false);
130
- }
131
- }
132
- } else if (sizeInBits === 64) {
133
- if (signed) {
134
- if (endianness === "little") {
135
- view.setBigInt64(0, value, true);
136
- } else {
137
- view.setBigInt64(0, value, false);
138
- }
139
- } else {
140
- // eslint-disable-next-line no-lonely-if
141
- if (endianness === "little") {
142
- view.setBigUint64(0, value, true);
143
- } else {
144
- view.setBigUint64(0, value, false);
145
- }
146
- }
147
- } else {
148
- throw Error("not implemented yet");
149
- }
150
- };
151
-
152
- return {
153
- parse,
154
- format
155
- };
156
- };
157
-
158
- export {
159
- createIntegerParser
160
- };
@@ -1,27 +0,0 @@
1
- import type { TEndianness } from "../common.ts";
2
- import { createIntegerParser } from "./integer.ts";
3
- import type { TValueParser } from "./value.ts";
4
-
5
- type TPointerParser = TValueParser<bigint>;
6
-
7
- const createPointerParser = ({ sizeInBits, endianness }: { sizeInBits: number, endianness: TEndianness }): TPointerParser => {
8
-
9
- const integerParser = createIntegerParser({ sizeInBits, signed: false, endianness });
10
-
11
- const parse: TPointerParser["parse"] = ({ data, offsetInBits }) => {
12
- return integerParser.parse({ data, offsetInBits });
13
- };
14
-
15
- const format: TPointerParser["format"] = ({ value, target, offsetInBits }) => {
16
- return integerParser.format({ value, target, offsetInBits });
17
- };
18
-
19
- return {
20
- parse,
21
- format
22
- };
23
- };
24
-
25
- export {
26
- createPointerParser
27
- };
@@ -1,56 +0,0 @@
1
- import type { TValueParser } from "./value.ts";
2
-
3
- type TStringParser = TValueParser<string>;
4
-
5
- const createStringParser = ({ length }: { length: number }): TStringParser => {
6
-
7
- const decoder = new TextDecoder();
8
- const encoder = new TextEncoder();
9
-
10
- const parse: TStringParser["parse"] = ({ data, offsetInBits }) => {
11
- if (offsetInBits !== 0) {
12
- throw Error("unaligned data in string parser, not supported yet");
13
- }
14
-
15
- if (data.length < length) {
16
- throw Error("not enough data for string parsing");
17
- }
18
-
19
- const stringData = data.subarray(0, length);
20
- const nullTerminatorIndex = stringData.indexOf(0);
21
-
22
- if (nullTerminatorIndex < 0) {
23
- return decoder.decode(stringData);
24
- }
25
-
26
- return decoder.decode(stringData.subarray(0, nullTerminatorIndex));
27
- };
28
-
29
- const format: TStringParser["format"] = ({ value, target, offsetInBits }) => {
30
- if (offsetInBits !== 0) {
31
- throw Error("unaligned data in string formatter, not supported yet");
32
- }
33
-
34
- if (target.length < length) {
35
- throw Error("not enough space in target for string formatting");
36
- }
37
-
38
- const encoded = encoder.encode(value);
39
-
40
- if (encoded.length + 1 > length) {
41
- throw Error("string too long to fit in target");
42
- }
43
-
44
- target.set(encoded, 0);
45
- target[encoded.length] = 0;
46
- };
47
-
48
- return {
49
- parse,
50
- format
51
- };
52
- };
53
-
54
- export {
55
- createStringParser
56
- };
@@ -1,146 +0,0 @@
1
- import type { TEndianness } from "../common.ts";
2
- import type { TLayoutedField } from "../layout.ts";
3
- import { createIntegerParser } from "./integer.ts";
4
- import { createPointerParser } from "./pointer.ts";
5
- import { createStringParser } from "./string.ts";
6
- import { createArrayParser } from "./array.ts";
7
- import type { TValueParser } from "./value.ts";
8
- import type { TFieldType } from "./index.ts";
9
-
10
- type TStructParser = TValueParser<Record<string, unknown>>;
11
-
12
- const subData = ({ data, offsetInBits, sizeInBits }: { data: Uint8Array, offsetInBits: number, sizeInBits: number }) => {
13
- return {
14
- data: new Uint8Array(data.buffer, data.byteOffset + Math.floor(offsetInBits / 8), Math.ceil(sizeInBits / 8)),
15
- offsetInBits: offsetInBits % 8
16
- };
17
- };
18
-
19
- const createStructParser = ({
20
- layoutedFields,
21
- structOffsetInBits,
22
- endianness
23
- }: {
24
- layoutedFields: (TLayoutedField & { type: "struct" })["fields"];
25
- structOffsetInBits: number;
26
- endianness: TEndianness
27
- }): TStructParser => {
28
-
29
- // eslint-disable-next-line complexity, @typescript-eslint/no-explicit-any
30
- const fieldParsers: TValueParser<any>[] = layoutedFields.map((field) => {
31
- if (field.definition.type === "integer") {
32
- return createIntegerParser({
33
- sizeInBits: field.definition.sizeInBits,
34
- signed: field.definition.signed,
35
- endianness
36
- });
37
- }
38
-
39
- if (field.definition.type === "pointer") {
40
- return createPointerParser({
41
- sizeInBits: field.definition.sizeInBits,
42
- endianness
43
- });
44
- }
45
-
46
- if (field.definition.type === "struct") {
47
- return createStructParser({
48
- layoutedFields: field.definition.fields,
49
- structOffsetInBits: field.definition.offsetInBits,
50
- endianness
51
- });
52
- }
53
-
54
- if (field.definition.type === "string") {
55
- return createStringParser({
56
- length: field.definition.length,
57
- });
58
- }
59
-
60
- if (field.definition.type === "array") {
61
- return createArrayParser({
62
- // TODO: cast should not be necessary
63
- elementType: field.definition.elementType as TFieldType,
64
- endianness,
65
- length: field.definition.length
66
- });
67
- }
68
-
69
- throw Error("not implemented yet");
70
- });
71
-
72
- const parse: TStructParser["parse"] = ({ data, offsetInBits }) => {
73
- if (offsetInBits !== 0) {
74
- throw Error("unaligned struct parsing not supported yet");
75
- }
76
-
77
- // eslint-disable-next-line prefer-const
78
- let result: Record<string, unknown> = {};
79
-
80
- layoutedFields.forEach((field, idx) => {
81
- const fieldParser = fieldParsers[idx];
82
-
83
- // field definition is absolute, subtracting struct offset gives bit offset inside struct
84
- // adding data bit offset gives bit offset inside provided data
85
- const offsetToProvidedDataInBits = field.definition.offsetInBits - structOffsetInBits + offsetInBits;
86
-
87
- const offsetInBitsInByte = field.definition.offsetInBits % 8;
88
- if (offsetInBitsInByte !== 0) {
89
- throw Error("not implemented yet: unaligned field parsing");
90
- }
91
-
92
- const {
93
- data: fieldData,
94
- offsetInBits: fieldOffsetInBits
95
- } = subData({
96
- data,
97
- offsetInBits: offsetToProvidedDataInBits,
98
- sizeInBits: field.definition.sizeInBits
99
- });
100
-
101
- result[field.name] = fieldParser.parse({ data: fieldData, offsetInBits: fieldOffsetInBits });
102
- });
103
-
104
- return result;
105
- };
106
-
107
- const format: TStructParser["format"] = ({ value, target, offsetInBits }) => {
108
- if (offsetInBits % 8 !== 0) {
109
- throw Error("unaligned struct formatting not supported yet");
110
- }
111
-
112
- layoutedFields.forEach((field, idx) => {
113
- const fieldValue = value[field.name];
114
- const fieldParser = fieldParsers[idx];
115
-
116
- const offsetInBitsInByte = field.definition.offsetInBits % 8;
117
- if (offsetInBitsInByte !== 0) {
118
- throw Error("not implemented yet: unaligned field formatting");
119
- }
120
-
121
- const {
122
- data: fieldTarget,
123
- offsetInBits: fieldOffsetInBits
124
- } = subData({
125
- data: target,
126
- offsetInBits: field.definition.offsetInBits - structOffsetInBits + offsetInBits,
127
- sizeInBits: field.definition.sizeInBits
128
- });
129
-
130
- try {
131
- fieldParser.format({ value: fieldValue, target: fieldTarget, offsetInBits: fieldOffsetInBits });
132
- } catch (ex) {
133
- throw Error(`failed to format field "${field.name}"`, { cause: ex });
134
- }
135
- });
136
- };
137
-
138
- return {
139
- parse,
140
- format
141
- };
142
- };
143
-
144
- export {
145
- createStructParser
146
- };
@@ -1,8 +0,0 @@
1
- type TValueParser<T> = {
2
- parse: ({ data }: { data: Uint8Array, offsetInBits: number }) => T;
3
- format: ({ value, target }: { value: T, target: Uint8Array, offsetInBits: number }) => void;
4
- };
5
-
6
- export type {
7
- TValueParser
8
- };
package/package.npm.json DELETED
@@ -1,25 +0,0 @@
1
- {
2
- "name": "ya-struct",
3
- "description": "Yet Another Node.js Structure API",
4
- "main": "dist/lib/index.js",
5
- "repository": {
6
- "type": "git",
7
- "url": "git+https://github.com/k13-engineering/node-ya-struct.git"
8
- },
9
- "keywords": [
10
- "structure",
11
- "struct",
12
- "abi",
13
- "endian",
14
- "alignment",
15
- "c",
16
- "c++",
17
- "layout"
18
- ],
19
- "author": "Simon Kadisch",
20
- "license": "LGPL 2.1",
21
- "bugs": {
22
- "url": "https://github.com/k13-engineering/node-ya-struct/issues"
23
- },
24
- "homepage": "https://github.com/k13-engineering/node-ya-struct#readme"
25
- }
package/samples/basic.ts DELETED
@@ -1,40 +0,0 @@
1
- import { define, types } from "../lib/index.ts";
2
-
3
- const def = define({
4
- definition: {
5
- type: "struct",
6
- packed: false,
7
- fixedAbi: {},
8
- fields: [
9
- { name: "a", definition: types.Int16 },
10
- { name: "b", definition: types.UInt16 },
11
- { name: "c", definition: types.UInt32 },
12
- ]
13
- }
14
- });
15
-
16
- const parser = def.parser({
17
- abi: {
18
- endianness: "little",
19
- dataModel: "LP64",
20
- compiler: "gcc",
21
- }
22
- });
23
-
24
- const value: ReturnType<typeof parser.parse> = {
25
- a: 0n,
26
- b: 1n,
27
- c: 2n,
28
- };
29
-
30
- console.log("value", value);
31
-
32
- const formatted = parser.format({
33
- value
34
- });
35
-
36
- console.log("formatted", formatted);
37
-
38
- const reparsed = parser.parse({ data: formatted });
39
-
40
- console.log("reparsed", reparsed);