Skip to content

Commit b48a469

Browse files
authored
Add string encoding option (#133)
* Update compiler-utils.js Customizable string encoding * implement string encoding option * accept no args for cstring
1 parent aba9ba2 commit b48a469

2 files changed

Lines changed: 12 additions & 12 deletions

File tree

src/datatypes/compiler-utils.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module.exports = {
1414
code += 'if (offset + count > buffer.length) {\n'
1515
code += ' throw new PartialReadError("Missing characters in string, found size is " + buffer.length + " expected size was " + (offset + count))\n'
1616
code += '}\n'
17-
code += 'return { value: buffer.toString(\'utf8\', offset, offset + count), size: count + countSize }'
17+
code += `return { value: buffer.toString("${string.encoding || 'utf8'}", offset, offset + count), size: count + countSize }`
1818
return compiler.wrapCode(code)
1919
}],
2020
buffer: ['parametrizable', (compiler, buffer) => {
@@ -67,13 +67,13 @@ module.exports = {
6767

6868
Write: {
6969
pstring: ['parametrizable', (compiler, string) => {
70-
let code = 'const length = Buffer.byteLength(value, \'utf8\')\n'
70+
let code = `const length = Buffer.byteLength(value, "${string.encoding || 'utf8'}")\n`
7171
if (string.countType) {
7272
code += 'offset = ' + compiler.callType('length', string.countType) + '\n'
7373
} else if (string.count === null) {
7474
throw new Error('pstring must contain either count or countType')
7575
}
76-
code += 'buffer.write(value, offset, length, \'utf8\')\n'
76+
code += `buffer.write(value, offset, length, "${string.encoding || 'utf8'}")\n`
7777
code += 'return offset + length'
7878
return compiler.wrapCode(code)
7979
}],
@@ -125,7 +125,7 @@ module.exports = {
125125

126126
SizeOf: {
127127
pstring: ['parametrizable', (compiler, string) => {
128-
let code = 'let size = Buffer.byteLength(value, \'utf8\')\n'
128+
let code = `let size = Buffer.byteLength(value, "${string.encoding || 'utf8'}")\n`
129129
if (string.countType) {
130130
code += 'size += ' + compiler.callType('size', string.countType) + '\n'
131131
} else if (string.count === null) {

src/datatypes/utils.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,20 +111,20 @@ function readPString (buffer, offset, typeArgs, rootNode) {
111111
}
112112

113113
return {
114-
value: buffer.toString('utf8', cursor, strEnd),
114+
value: buffer.toString(typeArgs.encoding || 'utf8', cursor, strEnd),
115115
size: strEnd - offset
116116
}
117117
}
118118

119119
function writePString (value, buffer, offset, typeArgs, rootNode) {
120120
const length = Buffer.byteLength(value, 'utf8')
121121
offset = sendCount.call(this, length, buffer, offset, typeArgs, rootNode)
122-
buffer.write(value, offset, length, 'utf8')
122+
buffer.write(value, offset, length, typeArgs.encoding || 'utf8')
123123
return offset + length
124124
}
125125

126126
function sizeOfPString (value, typeArgs, rootNode) {
127-
const length = Buffer.byteLength(value, 'utf8')
127+
const length = Buffer.byteLength(value, typeArgs.encoding || 'utf8')
128128
const size = calcCount.call(this, length, typeArgs, rootNode)
129129
return size + length
130130
}
@@ -237,20 +237,20 @@ function sizeOfBitField (value, typeArgs) {
237237
}, 0) / 8)
238238
}
239239

240-
function readCString (buffer, offset) {
240+
function readCString (buffer, offset, typeArgs) {
241241
let size = 0
242242
while (offset + size < buffer.length && buffer[offset + size] !== 0x00) { size++ }
243243
if (buffer.length < offset + size + 1) { throw new PartialReadError() }
244244

245245
return {
246-
value: buffer.toString('utf8', offset, offset + size),
246+
value: buffer.toString(typeArgs?.encoding || 'utf8', offset, offset + size),
247247
size: size + 1
248248
}
249249
}
250250

251-
function writeCString (value, buffer, offset) {
252-
const length = Buffer.byteLength(value, 'utf8')
253-
buffer.write(value, offset, length, 'utf8')
251+
function writeCString (value, buffer, offset, typeArgs) {
252+
const length = Buffer.byteLength(value, typeArgs?.encoding || 'utf8')
253+
buffer.write(value, offset, length, typeArgs?.encoding || 'utf8')
254254
offset += length
255255
buffer.writeInt8(0x00, offset)
256256
return offset + 1

0 commit comments

Comments
 (0)