Passed
Branch v15.x (14761d)
by Rafael S.
01:53
created
Severity
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 The byte-data API.
27
 * @see https://github.com/rochars/byte-data
28
 */
29
30
/** @module byteData */
31
32
import endianness from 'endianness';
33
import Packer from './lib/packer.js';
34
import {validateNotUndefined, validateValueType} from './lib/validation.js';
35
import {pack as packUTF8, unpack as unpackUTF8} from 'utf8-buffer';
36
37
/** @type {Packer} */
38
let packer = new Packer();
39
40
/**
41
 * Read a string of UTF-8 characters from a byte buffer.
42
 * @param {!Uint8Array|!Array<!number>} buffer A byte buffer.
43
 * @param {number=} index The index to read.
44
 * @param {?number=} len The number of bytes to read.
45
 *    If len is undefined will read until the end of the buffer.
46
 * @return {string}
47
 */
48
export function unpackString(buffer, index=0, len=undefined) {
49
  return unpackUTF8(buffer, index, len);
50
}
51
52
/**
53
 * Write a string of UTF-8 characters as a byte buffer.
54
 * @param {string} str The string to pack.
55
 * @return {!Uint8Array} The packed string.
56
 */
57
export function packString(str) {
58
  return packUTF8(str);
59
}
60
61
/**
62
 * Write a string of UTF-8 characters to a byte buffer.
63
 * @param {string} str The string to pack.
64
 * @param {!Uint8Array|!Array<number>} buffer The output buffer.
65
 * @param {number=} index The buffer index to start writing.
66
 *   Assumes zero if undefined.
67
 * @return {number} The next index to write in the buffer.
68
 */
69
export function packStringTo(str, buffer, index=0) {
70
  /** @type {!Uint8Array} */
71
  let bytes = packString(str);
72
  for (let i = 0, len = bytes.length; i < len; i++) {
73
    buffer[index++] = bytes[i];
74
  }
75
  return index;
76
}
77
78
// Numbers
79
/**
80
 * Pack a number as a byte buffer.
81
 * @param {number} value The number.
82
 * @param {!Object} theType The type definition.
83
 * @return {!Array<number>} The packed value.
84
 * @throws {Error} If the type definition is not valid.
85
 * @throws {Error} If the value is not valid.
86
 */
87
export function pack(value, theType) {
88
  /** @type {!Array<!number>} */
89
  let output = [];
90
  packTo(value, theType, output);
91
  return output;
92
}
93
94
/**
95
 * Pack a number to a byte buffer.
96
 * @param {number} value The value.
97
 * @param {!Object} theType The type definition.
98
 * @param {!Uint8Array|!Array<number>} buffer The output buffer.
99
 * @param {number=} index The buffer index to write. Assumes 0 if undefined.
100
 * @return {number} The next index to write.
101
 * @throws {Error} If the type definition is not valid.
102
 * @throws {Error} If the value is not valid.
103
 */
104
export function packTo(value, theType, buffer, index=0) {
105
  return packArrayTo([value], theType, buffer, index);
106
}
107
108
/**
109
 * Pack an array of numbers as a byte buffer.
110
 * @param {!Array<number>|!TypedArray} values The values.
111
 * @param {!Object} theType The type definition.
112
 * @return {!Array<number>} The packed values.
113
 * @throws {Error} If the type definition is not valid.
114
 * @throws {Error} If any of the values are not valid.
115
 */
116
export function packArray(values, theType) {
117
  /** @type {!Array<!number>} */
118
  let output = [];
119
  packArrayTo(values, theType, output);
120
  return output;
121
}
122
123
/**
124
 * Pack a array of numbers to a byte buffer.
125
 * @param {!Array<number>|!TypedArray} values The value.
126
 * @param {!Object} theType The type definition.
127
 * @param {!Uint8Array|!Array<number>} buffer The output buffer.
128
 * @param {number=} index The buffer index to start writing.
129
 *   Assumes zero if undefined.
130
 * @return {number} The next index to write.
131
 * @throws {Error} If the type definition is not valid.
132
 * @throws {Error} If the value is not valid.
133
 */
134
export function packArrayTo(values, theType, buffer, index=0) {
135
  packer.setUp(theType);
136
  for (let i = 0, valuesLen = values.length; i < valuesLen; i++) {
137
    validateNotUndefined(values[i]);
138
    validateValueType(values[i]);
139
    /** @type {number} */
140
    let len = index + packer.offset;
141
    while (index < len) {
142
      index = packer.write(buffer, values[i], index);
143
    }
144
    if (theType.be) {
145
      endianness(
146
        buffer, packer.offset, index - packer.offset, index);
147
    }
148
  }
149
  return index;
150
}
151
152
/**
153
 * Unpack a number from a byte buffer.
154
 * @param {!Uint8Array|!Array<!number>} buffer The byte buffer.
155
 * @param {!Object} theType The type definition.
156
 * @param {number=} index The buffer index to read. Assumes zero if undefined.
157
 * @return {number}
158
 * @throws {Error} If the type definition is not valid
159
 * @throws {Error} On bad buffer length.
160
 */
161
export function unpack(buffer, theType, index=0) {
162
  packer.setUp(theType);
163
  if ((packer.offset + index) > buffer.length) {
164
    throw Error('Bad buffer length.');
165
  }
166
  if (theType.be) {
167
    endianness(buffer, packer.offset, index, index + packer.offset);
168
  }
169
  /** @type {number} */
170
  let value = packer.read(buffer, index);
171
  if (theType.be) {
172
    endianness(buffer, packer.offset, index, index + packer.offset);
173
  }
174
  return value;
175
}
176
177
/**
178
 * Unpack an array of numbers from a byte buffer.
179
 * @param {!Uint8Array|!Array<!number>} buffer The byte buffer.
180
 * @param {!Object} theType The type definition.
181
 * @param {number=} index The buffer index to start reading.
182
 *   Assumes zero if undefined.
183
 * @param {number=} end The buffer index to stop reading.
184
 *   Assumes the buffer length if undefined.
185
 * @return {!Array<number>}
186
 * @throws {Error} If the type definition is not valid
187
 */
188
export function unpackArray(buffer, theType, index=0, end=buffer.length) {
189
  /** @type {!Array<!number>} */
190
  let output = [];
191
  unpackArrayTo(buffer, theType, output, index, end);
192
  return output;
193
}
194
195
/**
196
 * Unpack a array of numbers to a typed array.
197
 * @param {!Uint8Array|!Array<!number>} buffer The byte buffer.
198
 * @param {!Object} theType The type definition.
199
 * @param {!TypedArray|!Array<!number>} output The output array.
200
 * @param {number=} index The buffer index to start reading.
201
 *   Assumes zero if undefined.
202
 * @param {number=} end The buffer index to stop reading.
203
 *   Assumes the buffer length if undefined.
204
 * @throws {Error} If the type definition is not valid
205
 */
206
export function unpackArrayTo(
207
    buffer, theType, output, index=0, end=buffer.length) {
208
  packer.setUp(theType);
209
  /** @type {number} */
210
  let originalIndex = index;
211
  while ((end - index) % packer.offset) {
212
      end--;
213
  }
214
  if (theType.be) {
215
    endianness(buffer, packer.offset, index, end);
216
  }
217
  for (let i = 0; index < end; index += packer.offset, i++) {
0 ignored issues
show
The loop variable i is initialized by the loop but not used in the test. Consider using another type of loop if this is the intended behavior.
Loading history...
218
    output[i] = packer.read(buffer, index);
219
  }
220
  if (theType.be) {
221
    endianness(buffer, packer.offset, originalIndex, end);
222
  }
223
}
224