forked from LordVonAdel/structron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.d.ts
263 lines (215 loc) · 6.58 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
interface ValueType<T> {
read(buffer: BufferSource, offset: number): T
write?(value: any, context: WriteContext, offset: number): void
SIZE: number
}
type Rule = (...params: any) => (dataObj: any, buffer: BufferSource) => boolean
/**
* Represents the structure of a C-like Struct
*/
declare class Struct implements ValueType<Struct> {
/**
* Creates a new struct definition
* @param name Optional name. Only used for debugging purposes
*/
constructor(name?: string)
/**
* Adds a member to load
* @param type Datatype of the member to load
* @param name Name of the member
*/
addMember(type: ValueType<any>, name: string): this
/**
* Adds a an array to load from values inside this struct. The order is not important
* @param type Datatype of the member to load
* @param name Name of the member
* @param offset Number or name of the member which stores the address of the array
* @param count Number or name of the member which stores the length of the array
* @param relative Is the address in the target member relative to the structs address?
*/
addArray(type: ValueType<any>, name: string, offset: string | number, count: string | number, relative?: boolean): this
/**
* Adds a reference. This marks a member as pointer. References will appear as own members in the out data. Recursive references are allowed and will produce an circular structure.
* @param type Type of the reference
* @param name Name of the new data member
* @param offset Number or name of member containing the offset
* @param relative Is the address relative to the structs address?
*/
addReference(type: ValueType<any>, name: string, offset: string | number, relative?: boolean): this
/**
* Adds a rule. Rules give extra validation options
* @param rule The rule to add. Rules can be generated with the static methods of Struct.RULES
*/
addRule(rule: Rule): this
/**
* Adds a static value. Will be attached to every read object. Statics have no have any influence on the binary data!
* @param name
* @param value
*/
addStatic(name, value): this
/**
* Converts a buffer to an object with the structs structure
* @param buffer Buffer to read from
* @param offset Offset byte to start reading from
* @returns {Object} The data that was read
*/
read(buffer: BufferSource, offset: number, readContext?: ReadContext): any
/**
* Parses an givien buffer. Returns the read context. It will contain the extracted data as well as some statistics like how many bytes were read and what errors occoured.
* @param buffer Buffer to read from
* @param offset Offset byte to start reading from
* @param options Parsing options
* @returns The report
*/
readContext(buffer: BufferSource, offset: number, options: ReadOptions): ReadContext
/**
* Writes data to an buffer, using this structure
* @param any Data holding object
* @param WriteContext Internally used for the write process. Will create automatically a new one if none is given
* @param number Offset, for start writing
*/
write(object: object, context?: WriteContext = null, offset?: number = 1): WriteContext
/**
* Validates a structure
* @param buffer Buffer to read from
* @param offset Byte offset in the buffer to begin reading from
* @returns True when valid
*/
validate(buffer: BufferSource, offset?: number): boolean
/**
* Returns the relative offset of an attribute in this struct definition
* @param name Name of the attribute
* @returns Relative offset in bytes
*/
getOffsetByName(name: string): number
/**
* The size, an instance of this struct will occupy. This does not contain the content of arrays.
*/
get SIZE(): number
/**
* Inbuilt rules
*/
static RULES: {
EQUAL: Rule
}
/**
* Inbuilt types
*/
static TYPES: {
/*
* Signed 4 byte little-endian Integer
*/
INT: ValueType<number>
INT_LE: ValueType<number>
/**
* Signed 4 byte big-endian Integer
*/
INT_BE: ValueType<number>
/**
* Unsigned 4 byte little-endian integer
*/
UINT: ValueType<number>
UINT_LE: ValueType<number>
/**
* Unsigned 4 byte big-endian Integer
*/
UINT_BE: ValueType<number>
/**
* Signed 16 bit little-endian integer
*/
SHORT: ValueType<number>
SHORT_LE: ValueType<number>
/**
* Signed 16 bit big-endian integer
*/
SHORT_BE: ValueType<number>
/**
* Unsigned 16 bit little-endian integer
*/
USHORT: ValueType<number>
USHORT_LE: ValueType<number>
/**
* Unsigned 16 bit big-endian integer
*/
USHORT_BE: ValueType<number>
/**
* 4 Byte little-endian float
*/
FLOAT: ValueType<number>
FLOAT_LE: ValueType<number>
/**
* 4 Byte big-endian float
*/
FLOAT_BE: ValueType<number>
/**
* 1 ASCII character
*/
CHAR: ValueType<string>
/**
* Unsigned 8 bit int
*/
BYTE: ValueType<number>
/**
* String with fixed Length
* @param {number} length Length to read
* @param {=string} encoding Codec to use. Anything that is supported by buffer.toString()
*/
STRING(length: number, encoding: string|"ascii"): ValueType<string>
/**
* Null terminated string. Don't use this type in a struct. Only as reference!
* @param encoding Codec to use. Anything that is supported by buffer.toString()
*/
NULL_TERMINATED_STRING(encoding: string|"ascii"): ValueType<string>
/**
* Skips a given amount of bytes
* @param length Number of bytes to skip
*/
SKIP(length: number): ValueType<void>
}
}
type ReadOptions = {
/**
* Calculate how much of the source buffer was read
*/
monitorUsage?: boolean
/**
* Remove reference fields (array offset, array length and reference offset fields) from output data.
*/
hideReferenceValues?: boolean
}
/**
* Stores the result of an import/read.
*/
declare class ReadContext {
constructor(buffer: BufferSource, options: ReadOptions)
/**
* Returns a formatted string containing the reports result.
*/
toString(): string
/**
* Returns the number of read bytes. Returns NaN, if monitorUsage is false
*/
getUsage(): number
/**
* Returns true if the imported data had errors
*/
hasErrors(): boolean
/**
* Raw data read
*/
data: object
}
type WriteOptions = {
/**
* Output buffer size in bytes
*/
bufferSize?: number
}
declare class WriteContext {
/**
* Output buffer
*/
public buffer: Buffer;
constructor(options: WriteOptions)
}
export = Struct;