-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathextras.js
More file actions
111 lines (93 loc) · 2.68 KB
/
extras.js
File metadata and controls
111 lines (93 loc) · 2.68 KB
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
const { PartialReadError } = require('../utils')
module.exports = {
loop: [readLoop, writeLoop, sizeOfLoop, require('../../ProtoDef/schemas/extras.json').loop],
restBuffer: [readRestBuffer, writeRestBuffer, sizeOfRestBuffer, require('../../ProtoDef/schemas/extras.json').restBuffer]
}
function readLoop (buffer, offset, typeArgs, rootNode) {
if (!typeArgs) {
throw new Error('typeArgs is required for loop type')
}
const results = []
const startOffset = offset
const nt = typeArgs.nt
const hasTerminator = nt !== null && typeof nt === 'number'
while (offset < buffer.length) {
// Check for terminator if specified
if (hasTerminator) {
if (offset >= buffer.length) break
const terminatorValue = buffer.readInt8(offset)
if (terminatorValue === nt) {
// Found terminator, consume it and return
return {
value: results,
size: offset - startOffset + 1
}
}
}
// Read the next element
try {
const entry = this.read(buffer, offset, typeArgs.type, rootNode)
results.push(entry.value)
offset += entry.size
} catch (error) {
if (error instanceof PartialReadError) {
break
}
throw error
}
}
return {
value: results,
size: offset - startOffset
}
}
function writeLoop (value, buffer, offset, typeArgs, rootNode) {
if (!typeArgs) {
throw new Error('typeArgs is required for loop type')
}
const nt = typeArgs.nt
const hasTerminator = nt !== null && typeof nt === 'number'
// Write each element in the array
for (const item of value) {
offset = this.write(item, buffer, offset, typeArgs.type, rootNode)
}
// Write terminator if specified
if (hasTerminator) {
buffer.writeInt8(nt, offset)
offset++
}
return offset
}
function sizeOfLoop (value, typeArgs, rootNode) {
if (!typeArgs) {
throw new Error('typeArgs is required for loop type')
}
const nt = typeArgs.nt
const hasTerminator = nt !== null && typeof nt === 'number'
let size = hasTerminator ? 1 : 0 // 1 byte for terminator if present
// Calculate size of all elements
for (const item of value) {
size += this.sizeOf(item, typeArgs.type, rootNode)
}
return size
}
function readRestBuffer (buffer, offset) {
const remainingBuffer = buffer.slice(offset)
return {
value: remainingBuffer,
size: remainingBuffer.length
}
}
function writeRestBuffer (value, buffer, offset) {
if (!(value instanceof Buffer)) {
value = Buffer.from(value)
}
value.copy(buffer, offset)
return offset + value.length
}
function sizeOfRestBuffer (value) {
if (!(value instanceof Buffer)) {
value = Buffer.from(value)
}
return value.length
}