Passed
Push — master ( 2f7ce0...4f126f )
by Rafael S.
02:01
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|!Array<number>} 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
    num = this.sign_(num);
104
    this.overflow_(num);
105
    return num; 
106
  }
107
108
  /**
109
   * Write one integer number to a byte buffer.
110
   * @param {!Uint8Array|!Array<number>} bytes An array of bytes.
111
   * @param {number} num The number.
112
   * @param {number=} j The index being written in the byte buffer.
113
   * @return {number} The next index to write on the byte buffer.
114
   * @throws {Error} If num is NaN.
115
   * @private
116
   */
117
  write(bytes, num, j=0) {
118
    if (num !== num) {
119
      throw new Error('NaN');
120
    }
121
    this.overflow_(num);
122
    bytes[j] = (num < 0 ? num + Math.pow(2, this.bits_) : num) & 255;
123
    j++;
124
    for (let i = 2, len = this.realOffset_; i < len; i++, j++) {
125
      bytes[j] = Math.floor(num / Math.pow(2, ((i - 1) * 8))) & 255;
126
    }
127
    if (this.bits_ > 8) {
128
      bytes[j] = Math.floor(
129
          num / Math.pow(2, ((this.realOffset_ - 1) * 8))) & this.lastByteMask_;
130
      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...
131
    }
132
    return j;
133
  }
134
135
  /**
136
   * Sign a number.
137
   * @param {number} num The number.
138
   * @return {number}
139
   * @private
140
   */
141
  sign_(num) {
142
    if (num > this.max_) {
143
      num -= (this.max_ * 2) + 2;
144
    }
145
    return num;
146
  }
147
148
  /**
149
   * Trows error in case of overflow.
150
   * @param {number} num The number.
151
   * @throws {Error} on overflow.
152
   * @private
153
   */
154
  overflow_(num) {
155
    if (num > this.max_ || num < this.min_) {
156
      throw new Error('Integer overflow');
157
    }
158
  }
159
160
  /**
161
   * Set the mask that should be used when writing the last byte.
162
   * @private
163
   */
164
  setLastByteMask_() {
165
    /** @type {number} */
166
    let r = 8 - ((((this.bits_ - 1) | 7) + 1) - this.bits_);
167
    this.lastByteMask_ = Math.pow(2, r > 0 ? r : 8) - 1;
168
  }
169
}
170