Passed
Push — master ( 56b7f2...fe4dd1 )
by Rafael S.
01:22
created

src/bit-parser.js   A

Complexity

Total Complexity 28
Complexity/F 1.17

Size

Lines of Code 218
Function Count 24

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
c 1
b 0
f 0
nc 1
dl 0
loc 218
rs 10
wmc 28
mnd 1
bc 26
fnc 24
bpm 1.0833
cpm 1.1666
noi 0

24 Functions

Rating   Name   Duplication   Size   Complexity  
A BitReader.read40Bit 0 3 1
A BitReader.read32BitFloat 0 4 1
A BitWriter.write32Bit 0 5 1
A bit-parser.js ➔ readBytesAsBits 0 9 2
A BitWriter.write24Bit 0 5 1
A BitReader.read32Bit 0 4 1
A BitReader.read24Bit 0 3 1
A BitReader.read64BitFloat 0 3 1
A BitWriter.writeString 0 4 1
A BitReader.readChar 0 10 2
A BitReader.read16BitFloat 0 3 1
A BitWriter.write32BitFloat 0 5 1
A BitWriter.write8Bit 0 4 1
A BitWriter.write4Bit 0 4 1
A BitReader.read16Bit 0 3 1
A BitWriter.write48Bit 0 5 1
A BitWriter.write16BitFloat 0 6 1
A BitWriter.write1Bit 0 4 2
A BitWriter.write64BitFloat 0 5 1
A BitWriter.write16Bit 0 5 1
A BitReader.read48Bit 0 3 1
A BitWriter.write2Bit 0 4 2
A BitWriter.write40Bit 0 5 1
A BitReader.read8Bit 0 3 1
1
/**
2
 * bit-parser: Functions to read and write bytes.
3
 * Copyright (c) 2017 Rafael da Silva Rocha.
4
 * https://github.com/rochars/byte-data
5
 * Float32 based on int-bits: https://github.com/Jam3/int-bits
6
 */
7
8
const helpers = require("../src/helpers.js");
9
const floats = require("../src/floats.js");
10
let i8 = new Int8Array(4);
11
let i32 = new Int32Array(i8.buffer, 0, 1);
12
let f32 = new Float32Array(i8.buffer, 0, 1);
13
14
/**
15
 * Read a group of bytes by turning it to bits.
16
 * Useful for 40 & 48-bit, but underperform.
17
 * @param {!Array<number>|Uint8Array} bytes An array of bytes.
18
 * @param {number} i The index to read.
19
 * @param {number} numBytes The number of bytes
20
 *      (1 for 8-bit, 2 for 16-bit, etc).
21
 * @return {number}
22
 */
23
function readBytesAsBits(bytes, i, numBytes) {
24
    let j = numBytes-1;
25
    let bits = "";
26
    while (j >= 0) {
27
        bits += helpers.bytePadding(bytes[j + i].toString(2), 2);
28
        j--;
29
    }
30
    return parseInt(bits, 2);
31
}
32
33
let BitReader = {
34
35
    /**
36
     * Read 1 8-bit int from from bytes.
37
     * @param {!Array<number>|Uint8Array} bytes An array of bytes.
38
     * @param {number} i The index to read.
39
     * @return {number}
40
     */
41
    "read8Bit": function (bytes, i) {
42
        return bytes[i];
43
    },
44
45
    /**
46
     * Read 1 16-bit int from from bytes.
47
     * @param {!Array<number>|Uint8Array} bytes An array of bytes.
48
     * @param {number} i The index to read.
49
     * @return {number}
50
     */
51
    "read16Bit": function (bytes, i) {
52
        return bytes[1 + i] << 8 | bytes[i];
53
    },
54
55
    /**
56
     * Read 1 16-bit float from from bytes.
57
     * @param {!Array<number>|Uint8Array} bytes An array of bytes.
58
     * @param {number} i The index to read.
59
     * @return {number}
60
     */
61
    "read16BitFloat": function (bytes, i) {
62
        return floats.decodeFloat16(bytes.slice(i,i+2));
63
    },
64
65
    /**
66
     * Read 1 24-bit int from from bytes.
67
     * @param {!Array<number>|Uint8Array} bytes An array of bytes.
68
     * @param {number} i The index to read.
69
     * @return {number}
70
     */
71
    "read24Bit": function (bytes, i) {
72
        return bytes[2 + i] << 16 | BitReader["read16Bit"](bytes, i);
73
    },
74
75
    /**
76
     * Read 1 32-bit int from from bytes.
77
     * @param {!Array<number>|Uint8Array} bytes An array of bytes.
78
     * @param {number} i The index to read.
79
     * @return {number}
80
     */
81
    "read32Bit": function (bytes, i) {
82
        return (bytes[3 + i] << 24 |
83
            BitReader["read24Bit"](bytes, i)) >>> 0;
84
    },
85
86
    /**
87
     * Read 1 32-bit float from from bytes.
88
     * @param {!Array<number>|Uint8Array} bytes An array of bytes.
89
     * @param {number} i The index to read.
90
     * @return {number}
91
     */
92
    "read32BitFloat": function (bytes, i) {
93
        i32[0] = BitReader["read32Bit"](bytes, i);
94
        return f32[0];
95
    },
96
97
    /**
98
     * Read 1 40-bit int from from bytes.
99
     * @param {!Array<number>|Uint8Array} bytes An array of bytes.
100
     * @param {number} i The index to read.
101
     * @return {number}
102
     */
103
    "read40Bit": function (bytes, i) {
104
        return readBytesAsBits(bytes, i, 5);
105
    },
106
107
    /**
108
     * Read 1 48-bit int from bytes.
109
     * @param {!Array<number>|Uint8Array} bytes An array of bytes.
110
     * @param {number} i The index to read.
111
     * @return {number}
112
     */
113
    "read48Bit": function (bytes, i) {
114
        return readBytesAsBits(bytes, i, 6);
115
    },
116
117
    /**
118
     * Read 1 64-bit double from bytes.
119
     * @param {!Array<number>|Uint8Array} bytes An array of bytes.
120
     * @param {number} i The index to read.
121
     * @return {number}
122
     */
123
    "read64BitFloat": function (bytes, i) {
124
        return floats.decodeFloat64(bytes.slice(i,i+8));
125
    },
126
127
    /**
128
     * Read 1 char from bytes.
129
     * @param {!Array<number>|Uint8Array} bytes An array of bytes.
130
     * @param {number} i The index to read.
131
     * @return {string}
132
     */
133
    "readChar": function (bytes, i, type) {
134
        let chrs = "";
135
        let j = 0;
136
        let len = type.bits / 8;
137
        while(j < len) {
138
            chrs += String.fromCharCode(bytes[i+j]);
139
            j++;
140
        }
141
        return chrs;
142
    }
143
};
144
145
let BitWriter = {
146
147
    "write64BitFloat": function(bytes, number, j) {
148
        let bits = floats.toFloat64(number);
149
        j = BitWriter["write32Bit"](bytes, bits[1], j);
150
        return BitWriter["write32Bit"](bytes, bits[0], j);
151
    },
152
153
    // Thanks https://github.com/majimboo/c-struct
154
    "write48Bit": function (bytes, number, j) {
155
        j = BitWriter["write40Bit"](bytes, number, j);
156
        bytes[j++] = number / 0x10000000000 & 0xFF;
157
        return j;
158
    },
159
160
    // Thanks https://github.com/majimboo/c-struct
161
    "write40Bit": function (bytes, number, j) {
162
        j = BitWriter["write32Bit"](bytes, number, j);
163
        bytes[j++] = number / 0x100000000 & 0xFF;
164
        return j;
165
    },
166
167
    "write32BitFloat": function (bytes, number, j) {
168
        f32[0] = number;
169
        j = BitWriter["write32Bit"](bytes, i32[0], j);
170
        return j;
171
    },
172
173
    "write32Bit": function (bytes, number, j) {
174
        j = BitWriter["write24Bit"](bytes, number, j);
175
        bytes[j++] = number >>> 24 & 0xFF;
176
        return j;
177
    },
178
179
    "write24Bit": function (bytes, number, j) {
180
        j = BitWriter["write16Bit"](bytes, number, j);
181
        bytes[j++] = number >>> 16 & 0xFF;
182
        return j;
183
    },
184
185
    "write16Bit": function (bytes, number, j) {
186
        bytes[j++] = number & 0xFF;
187
        bytes[j++] = number >>> 8 & 0xFF;
188
        return j;
189
    },
190
191
    "write16BitFloat": function (bytes, number, j) {
192
        let bits = floats.toHalf(number);
193
        bytes[j++] = bits >>> 8 & 0xFF;
194
        bytes[j++] = bits & 0xFF;
195
        return j;
196
    },
197
198
    "write8Bit": function (bytes, number, j) {
199
        bytes[j++] = number & 0xFF;
200
        return j;
201
    },
202
203
    "write4Bit": function (bytes, number, j) {
204
        bytes[j++] = number & 0xF;
205
        return j;
206
    },
207
208
    "write2Bit": function (bytes, number, j) {
209
        bytes[j++] = number < 0 ? number + 4 : number;
210
        return j;
211
    },
212
213
    "write1Bit": function (bytes, number, j) {
214
        bytes[j++] = number ? 1 : 0;
215
        return j;
216
    },
217
218
    "writeString": function (bytes, string, j) {
219
        bytes[j++] = string.charCodeAt(0);
220
        return j;
221
    }
222
};
223
224
module.exports.BitWriter = BitWriter;
225
module.exports.BitReader = BitReader;
226