Passed
Branch master (b78143)
by Rafael S.
01:32
created

UintBuffer.packUnsafe   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
/*
2
 * Copyright (c) 2018-2019 Rafael da Silva Rocha.
3
 *
4
 * Permission is hereby granted, free of charge, to any person obtaining
5
 * a copy of this software and associated documentation files (the
6
 * "Software"), to deal in the Software without restriction, including
7
 * without limitation the rights to use, copy, modify, merge, publish,
8
 * distribute, sublicense, and/or sell copies of the Software, and to
9
 * permit persons to whom the Software is furnished to do so, subject to
10
 * the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be
13
 * included in all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
 *
23
 */
24
25
/**
26
 * @fileoverview Encode and decode unsigned integers to and from byte buffers.
27
 * @see https://github.com/rochars/uint-buffer
28
 */
29
30
/** @module uint-buffer */
31
32
/**
33
 * A class to write and read unsigned ints to and from byte buffers.
34
 */
35
export class UintBuffer {
36
  
37
  /**
38
   * @param {number} bits The number of bits used by the integer.
39
   */
40
  constructor(bits) {
41
    /**
42
     * The number of bits used by one number.
43
     * @type {number}
44
     */
45
    this.bits = bits;
46
    /**
47
     * The number of bytes used by one number.
48
     * @type {number}
49
     */
50
    this.bytes = bits < 8 ? 1 : Math.ceil(bits / 8);
51
    /**
52
     * @type {number}
53
     * @protected
54
     */
55
    this.max = Math.pow(2, bits) - 1;
56
    /**
57
     * @type {number}
58
     * @protected
59
     */
60
    this.min = 0;
61
    /** @type {number} */
62
    let r = 8 - ((((bits - 1) | 7) + 1) - bits);
63
    /**
64
     * @type {number}
65
     * @private
66
     */
67
    this.lastByteMask_ = Math.pow(2, r > 0 ? r : 8) - 1;
68
  }
69
70
  /**
71
   * Write one unsigned integer to a byte buffer.
72
   * @param {!Uint8Array|!Array<number>} buffer An array of bytes.
73
   * @param {number} num The number.
74
   * @param {number=} index The index being written in the byte buffer.
75
   * @return {number} The next index to write on the byte buffer.
76
   * @throws {TypeError} If num is not a number.
77
   * @throws {RangeError} On overflow.
78
   */
79
  pack(buffer, num, index=0) {
80
    if (num !== num || typeof num != 'number') {
81
      throw new TypeError();
82
    }
83
    this.overflow(num);
84
    return this.pack_(buffer, num, index);
85
  }
86
87
  /**
88
   * Write one unsigned integer to a byte buffer.
89
   * This method assumes the input has already been validated
90
   * and should be used only if you know what you are doing.
91
   * @param {!Uint8Array|!Array<number>} buffer An array of bytes.
92
   * @param {number} num The number.
93
   * @param {number=} index The index being written in the byte buffer.
94
   * @return {number} The next index to write on the byte buffer.
95
   */
96
  packUnsafe(buffer, num, index=0) {
97
    return this.pack_(buffer, num, index);
98
  }
99
  
100
  /**
101
   * Read one unsigned integer from a byte buffer.
102
   * @param {!Uint8Array|!Array<number>} buffer An array of bytes.
103
   * @param {number=} index The index to read.
104
   * @return {number} The number.
105
   * @throws {RangeError} On overflow.
106
   */
107
  unpack(buffer, index=0) {
108
    /** @type {number} */
109
    let num = this.unpackUnsafe(buffer, index);
110
    this.overflow(num);
111
    return num; 
112
  }
113
114
  /**
115
   * Read one unsigned integer from a byte buffer.
116
   * Does not check for overflows.
117
   * @param {!Uint8Array|!Array<number>} buffer An array of bytes.
118
   * @param {number=} index The index to read.
119
   * @return {number}
120
   */
121
  unpackUnsafe(buffer, index=0) {
122
    /** @type {number} */
123
    let num = 0;
124
    for(let x = 0; x < this.bytes; x++) {
125
      num += buffer[index + x] * Math.pow(256, x);
126
    }
127
    return num;
128
  }
129
130
  /**
131
   * Throws range error in case of overflow.
132
   * @param {number} num The number.
133
   * @throws {RangeError} On overflow.
134
   * @protected
135
   * @ignore
136
   */
137
  overflow(num) {
138
    if (num > this.max || num < this.min) {
139
      throw new RangeError();
140
    }
141
  }
142
143
  /**
144
   * Write one unsigned integer to a byte buffer.
145
   * @param {!Uint8Array|!Array<number>} buffer An array of bytes.
146
   * @param {number} num The number.
147
   * @param {number=} index The index being written in the byte buffer.
148
   * @return {number} The next index to write on the byte buffer.
149
   * @throws {TypeError} If num is not a number.
150
   * @throws {RangeError} On overflow.
151
   * @private
152
   */
153
  pack_(buffer, num, index=0) {
154
    buffer[index] = (num < 0 ? num + Math.pow(2, this.bits) : num) & 255;
155
    index++;
156
    /** @type {number} */
157
    let len = this.bytes;
158
    for (let i = 2; i < len; i++) {
159
      buffer[index] = Math.floor(num / Math.pow(2, ((i - 1) * 8))) & 255;
160
      index++;
161
    }
162
    if (this.bits > 8) {
163
      buffer[index] = Math.floor(
164
          num / Math.pow(2, ((this.bytes - 1) * 8))) & this.lastByteMask_;
165
      index++;
166
    }
167
    return index;
168
  }
169
}
170