Passed
Push — master ( 83dad7...005d01 )
by Rafael S.
01:26
created

uint-buffer.js ➔ overflow   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
/*
2
 * Copyright (c) 2018 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 Pack and unpack unsigned ints.
27
 * @see https://github.com/rochars/uint-buffer
28
 */
29
30
/**
31
 * @module UintBuffer
32
 * @ignore
33
 */
34
35
/**
36
 * A class to write and read unsigned ints to and from byte buffers.
37
 */
38
export default class UintBuffer {
39
  
40
  /**
41
   * @param {number} bits The number of bits used by the integer.
42
   */
43
  constructor(bits) {
44
    /**
45
     * The number of bits used by one number.
46
     * @type {number}
47
     */
48
    this.bits = bits;
49
    /**
50
     * The number of bytes used by one number.
51
     * @type {number}
52
     */
53
    this.bytes = bits < 8 ? 1 : Math.ceil(bits / 8);
54
    /**
55
     * @type {number}
56
     * @protected
57
     */
58
    this.max = Math.pow(2, bits) - 1;
59
    /**
60
     * @type {number}
61
     * @protected
62
     */
63
    this.min = 0;
64
    /** @type {number} */
65
    let r = 8 - ((((bits - 1) | 7) + 1) - bits);
66
    /**
67
     * @type {number}
68
     * @private
69
     */
70
    this.lastByteMask_ = Math.pow(2, r > 0 ? r : 8) - 1;
71
  }
72
73
  /**
74
   * Write one unsigned integer to a byte buffer.
75
   * @param {!Uint8Array|!Array<number>} buffer An array of bytes.
76
   * @param {number} num The number.
77
   * @param {number=} index The index being written in the byte buffer.
78
   * @return {number} The next index to write on the byte buffer.
79
   * @throws {Error} If num is NaN.
80
   * @throws {Error} On overflow.
81
   */
82
  pack(buffer, num, index=0) {
83
    if (num !== num) {
84
      throw new Error('NaN');
85
    }
86
    this.overflow(num);
87
    buffer[index] = (num < 0 ? num + Math.pow(2, this.bits) : num) & 255;
88
    index++;
89
    /** @type {number} */
90
    let len = this.bytes;
91
    for (let i = 2; i < len; i++) {
92
      buffer[index] = Math.floor(num / Math.pow(2, ((i - 1) * 8))) & 255;
93
      index++;
94
    }
95
    if (this.bits > 8) {
96
      buffer[index] = Math.floor(
97
          num / Math.pow(2, ((this.bytes - 1) * 8))) & this.lastByteMask_;
98
      index++;
99
    }
100
    return index;
101
  }
102
  
103
  /**
104
   * Read one unsigned integer from a byte buffer.
105
   * @param {!Uint8Array|!Array<number>} buffer An array of bytes.
106
   * @param {number=} index The index to read.
107
   * @return {number} The number.
108
   * @throws {Error} On overflow.
109
   */
110
  unpack(buffer, index=0) {
111
    /** @type {number} */
112
    let num = this.unpackUnsafe(buffer, index);
113
    this.overflow(num);
114
    return num; 
115
  }
116
117
  /**
118
   * Read one unsigned integer from a byte buffer.
119
   * Does not check for overflows.
120
   * @param {!Uint8Array|!Array<number>} buffer An array of bytes.
121
   * @param {number} index The index to read.
122
   * @return {number}
123
   * @protected
124
   */
125
  unpackUnsafe(buffer, index) {
126
    /** @type {number} */
127
    let num = 0;
128
    for(let x = 0; x < this.bytes; x++) {
129
      num += buffer[index + x] * Math.pow(256, x);
130
    }
131
    return num;
132
  }
133
134
  /**
135
   * Throws error in case of overflow.
136
   * @param {number} num The number.
137
   * @throws {Error} on overflow.
138
   * @protected
139
   */
140
  overflow(num) {
141
    if (num > this.max || num < this.min) {
142
      throw new Error('Overflow');
143
    }
144
  }
145
}
146