-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_to_json.ts
More file actions
141 lines (125 loc) · 3.5 KB
/
binary_to_json.ts
File metadata and controls
141 lines (125 loc) · 3.5 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
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
// Copyright 2023 @ykisii. All rights reserved. MIT license.
import { BinaryReader } from "https://deno.land/x/binary_reader@v0.1.6/mod.ts";
type Obj = {
[prop: string]: any;
}
type SetParam = {
br: BinaryReader,
buf: Uint8Array,
dataArray: object[],
formats: any[]
}
export class BinaryToJSON {
#array_size:number = 0;
#littlEndian: boolean = false;
convert(buf: Uint8Array, format: any[], littleEndian?: boolean): {} {
const br = new BinaryReader(buf);
this.#littlEndian = littleEndian ?? this.#littlEndian;
this.#array_size = 0;
return this.bufferToJSON(br, buf, format);
}
private bufferToJSON(br: BinaryReader, buf: Uint8Array, formats:any[]): {} {
// needs array. when read binary sequentially.
if (Array.isArray(formats) === false ||
buf.length === 0) {
return {};
}
const output: Obj = {};
const array: object[] = [];
for (const format of formats) {
const [key, val] = Object.entries(format)[0];
if (Array.isArray(val)) {
this.setObjects({br:br, buf:buf, dataArray:array, formats:val});
output[key] = structuredClone(array);
this.#array_size = 0;
array.splice(0);
}
else {
const value = this.getValue(br, format);
if (value !== null) {
output[key] = value;
}
}
}
return output;
}
private setObjects(param: SetParam)
{
for (let i = 0; i < this.#array_size; i++) {
const obj = this.bufferToJSON(param.br, param.buf, param.formats);
//console.log(i, this.#array_size, formats, obj);
if (Object.keys(obj).length > 0) {
param.dataArray.push(obj);
}
}
}
private getValue(br: BinaryReader, format:{}) : number | null | string {
const [key, val] = Object.entries(format)[0];
if (key === "__reserve" || key === "__skip") {
br.seek(br.position + Number(val));
return null;
}
if (key == "__repeat") {
this.#array_size = Number(val);
return null;
}
if (/^__string/.test(key)) {
return this.getString(br, Number(val));
}
let value: number = this.readBytes(br, Number(val));
if (/^__repeat/.test(key)) {
this.#array_size = value;
}
return value;
}
private readBytes(br: BinaryReader, length: number): number {
const surplusLength = length >= 4 ? length - 4 : 0;
let val = 0;
if (length >= 4) {
val = br.readUint32(this.#littlEndian);
}
else {
const bytes = br.readBytes(length);
val = this.convolutionBytes(bytes);
}
if (surplusLength > 0) {
for (let i = 0; i < surplusLength * 2; i++) {
// bit shift
val *= 16;
}
val += this.convolutionBytes(br.readBytes(surplusLength));
}
return val;
}
private getString(br: BinaryReader, length: number): string {
let ret: string = "";
for (let i = 0; i < length; i++) {
const value = br.readBytes(1)[0];
let char = '';
// support ascii
if ((0 <= value) && (value <= 127)) {
char = String.fromCharCode(value);
}
else {
char = value.toString(16);
}
ret += char;
}
return ret;
}
private convolutionBytes(bytes: any): number {
let shift = 0;
return bytes.reduce((acc: number, val: number): number => {
let ret = 0;
if (this.#littlEndian) {
ret = acc | (val << shift)
shift += 0x08;
}
else {
ret = (acc << shift) | val;
shift = 0x08;
}
return ret;
}, 0);
}
}