1 | "use strict";
|
2 |
|
3 | var pow2 = [1, 2, 4, 8, 16, 32, 64, 128, 256];
|
4 | var neg = [127, 191, 223, 239, 247, 251, 253, 254];
|
5 |
|
6 |
|
7 | function writeBits(target, source, bitoffset, sourcebitLength) {
|
8 | if(sourcebitLength === undefined)
|
9 | sourcebitLength = source.length << 3;
|
10 |
|
11 | if(!sourcebitLength)
|
12 | return 0;
|
13 |
|
14 | var soucebitStart = sourcebitLength < 0 ? (source.length << 3) + sourcebitLength : 0;
|
15 | sourcebitLength = Math.min(Math.abs(sourcebitLength), (target.length << 3) - bitoffset);
|
16 | var sourceBitEnd = soucebitStart + sourcebitLength - 1;
|
17 |
|
18 | for(var i = soucebitStart, j = bitoffset; i <= sourceBitEnd; i++, j++) {
|
19 | if(source[i >> 3] & pow2[7 - i % 8])
|
20 | target[j >> 3] |= pow2[7 - j % 8];
|
21 | else
|
22 | target[j >> 3] &= neg[j % 8];
|
23 | }
|
24 |
|
25 | return sourcebitLength;
|
26 | }
|
27 |
|
28 |
|
29 | module.exports = writeBits;
|