Passed
Push — master ( 567ae9...db344a )
by Rafael S.
01:53
created

lib/integer.js (1 issue)

1
/*
2
 * Copyright (c) 2017-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 two's complement ints and unsigned ints.
27
 * @see https://github.com/rochars/byte-data
28
 */
29
30
/**
31
 * A class to pack and unpack two's complement ints and unsigned ints.
32
 */
33
export default class Integer {
34
35
  constructor() {
36
    /**
37
     * @type {number}
38
     */
39
    this.offset = 0;
40
    /**
41
     * @type {number}
42
     */
43
    this.realOffset_ = 0;
44
    /**
45
     * @type {number}
46
     * @private
47
     */
48
    this.bits_ = 0;
49
    /**
50
     * @type {number}
51
     * @private
52
     */
53
    this.lastByteMask_ = 0;
54
    /**
55
     * @type {number}
56
     * @private
57
     */
58
    this.max_ = 0;
59
    /**
60
     * @type {number}
61
     * @private
62
     */
63
    this.min_ = 0;
64
  }
65
66
  /**
67
   * Set up the object to start serializing/deserializing a data type..
68
   * @param {!Object} theType The type definition.
69
   */
70
  setUp(theType) {
71
    /**
72
     * The max number of bits used by the data.
73
     * @type {number}
74
     * @private
75
     */
76
    this.bits_ = theType.bits;
77
    // Set the min and max values according to the number of bits
78
    /** @type {number} */
79
    let max = Math.pow(2, this.bits_);
80
    if (theType.signed) {
81
      this.max_ = max / 2 -1;
82
      this.min_ = -max / 2;
83
    } else {
84
      this.max_ = max - 1;
85
      this.min_ = 0;
86
    }
87
    this.setLastByteMask_();
88
    this.offset = this.bits_ < 8 ? 1 : Math.ceil(this.bits_ / 8);
89
    this.realOffset_ = this.bits_ === 64 ? 4 : this.offset;
90
  }
91
92
  /**
93
   * Read one integer number from a byte buffer.
94
   * @param {!Uint8Array} bytes An array of bytes.
95
   * @param {number=} i The index to read.
96
   * @return {number}
97
   */
98
  read(bytes, i=0) {
99
    let num = 0;
100
    for(let x=0; x<this.realOffset_; x++) {
101
      num += bytes[i + x] * Math.pow(256, x);
102
    }
103
    return this.overflow_(this.sign_(num)); 
104
  }
105
106
  /**
107
   * Write one integer number to a byte buffer.
108
   * @param {!Uint8Array} bytes An array of bytes.
109
   * @param {number} num The number.
110
   * @param {number=} j The index being written in the byte buffer.
111
   * @return {number} The next index to write on the byte buffer.
112
   * @private
113
   */
114
  write(bytes, num, j=0) {
115
    j = this.writeFirstByte_(bytes, this.overflow_(num), j);
116
    for (let i = 2, len = this.realOffset_; i < len; i++, j++) {
117
      bytes[j] = Math.floor(num / Math.pow(2, ((i - 1) * 8))) & 255;
118
    }
119
    if (this.bits_ > 8) {
120
      bytes[j] = Math.floor(
121
          num / Math.pow(2, ((this.realOffset_ - 1) * 8))) & this.lastByteMask_;
122
      j++;
0 ignored issues
show
Complexity Coding Style introduced by
You seem to be assigning a new value to the loop variable j here. Please check if this was indeed your intention. Even if it was, consider using another kind of loop instead.
Loading history...
123
    }
124
    return j;
125
  }
126
127
  /**
128
   * Sign a number.
129
   * @param {number} num The number.
130
   * @return {number}
131
   * @private
132
   */
133
  sign_(num) {
134
    if (num > this.max_) {
135
      num -= (this.max_ * 2) + 2;
136
    }
137
    return num;
138
  }
139
140
  /**
141
   * Trows error in case of underflow or overflow.
142
   * @param {number} num The number.
143
   * @return {number}
144
   * @throws {Error} on overflow or underflow.
145
   * @private
146
   */
147
  overflow_(num) {
148
    if (num > this.max_) {
149
      throw new Error('Overflow.');
150
    } else if (num < this.min_) {
151
      throw new Error('Underflow.');
152
    }
153
    return num;
154
  }
155
156
  /**
157
   * Set the mask that should be used when writing the last byte.
158
   * @private
159
   */
160
  setLastByteMask_() {
161
    /** @type {number} */
162
    let r = 8 - ((((this.bits_ - 1) | 7) + 1) - this.bits_);
163
    this.lastByteMask_ = Math.pow(2, r > 0 ? r : 8) - 1;
164
  }
165
166
  /**
167
   * Write the first byte of a integer number.
168
   * @param {!Uint8Array} bytes An array of bytes.
169
   * @param {number} number The number.
170
   * @param {number} j The index being written in the byte buffer.
171
   * @return {number} The next index to write on the byte buffer.
172
   * @private
173
   */
174
  writeFirstByte_(bytes, number, j) {
175
    if (this.bits_ < 8) {
176
      bytes[j] = number < 0 ? number + Math.pow(2, this.bits_) : number;
177
    } else {
178
      bytes[j] = number & 255;
179
    }
180
    return j + 1;
181
  }
182
}
183