UNPKG

4.92 kBJavaScriptView Raw
1// Copyright 2012 The Obvious Corporation.
2
3/*
4 * bufs: Buffer utilities.
5 */
6
7/*
8 * Module variables
9 */
10
11/** Pool of buffers, where `bufPool[x].length === x`. */
12var bufPool = [];
13/** Maximum length of kept temporary buffers. */
14
15var TEMP_BUF_MAXIMUM_LENGTH = 20;
16/** Minimum exactly-representable 64-bit int. */
17
18var MIN_EXACT_INT64 = -0x8000000000000000;
19/** Maximum exactly-representable 64-bit int. */
20
21var MAX_EXACT_INT64 = 0x7ffffffffffffc00;
22/** Maximum exactly-representable 64-bit uint. */
23
24var MAX_EXACT_UINT64 = 0xfffffffffffff800;
25/**
26 * The int value consisting just of a 1 in bit #32 (that is, one more
27 * than the maximum 32-bit unsigned value).
28 */
29
30var BIT_32 = 0x100000000;
31/**
32 * The int value consisting just of a 1 in bit #64 (that is, one more
33 * than the maximum 64-bit unsigned value).
34 */
35
36var BIT_64 = 0x10000000000000000;
37/*
38 * Helper functions
39 */
40
41/**
42 * Masks off all but the lowest bit set of the given number.
43 */
44
45function lowestBit(num) {
46 return num & -num;
47}
48/**
49 * Gets whether trying to add the second number to the first is lossy
50 * (inexact). The first number is meant to be an accumulated result.
51 */
52
53
54function isLossyToAdd(accum, num) {
55 if (num === 0) {
56 return false;
57 }
58
59 var lowBit = lowestBit(num);
60 var added = accum + lowBit;
61
62 if (added === accum) {
63 return true;
64 }
65
66 if (added - lowBit !== accum) {
67 return true;
68 }
69
70 return false;
71}
72/*
73 * Exported functions
74 */
75
76/**
77 * Allocates a buffer of the given length, which is initialized
78 * with all zeroes. This returns a buffer from the pool if it is
79 * available, or a freshly-allocated buffer if not.
80 */
81
82
83export function alloc(length) {
84 var result = bufPool[length];
85
86 if (result) {
87 bufPool[length] = undefined;
88 } else {
89 result = new Uint8Array(length);
90 }
91
92 result.fill(0);
93 return result;
94}
95/**
96 * Releases a buffer back to the pool.
97 */
98
99export function free(buffer) {
100 var length = buffer.length;
101
102 if (length < TEMP_BUF_MAXIMUM_LENGTH) {
103 bufPool[length] = buffer;
104 }
105}
106/**
107 * Resizes a buffer, returning a new buffer. Returns the argument if
108 * the length wouldn't actually change. This function is only safe to
109 * use if the given buffer was allocated within this module (since
110 * otherwise the buffer might possibly be shared externally).
111 */
112
113export function resize(buffer, length) {
114 if (length === buffer.length) {
115 return buffer;
116 }
117
118 var newBuf = alloc(length);
119
120 for (var i = 0; i <= buffer.length; i++) {
121 newBuf[i] = buffer[i];
122 }
123
124 free(buffer);
125 return newBuf;
126}
127/**
128 * Reads an arbitrary signed int from a buffer.
129 */
130
131export function readInt(buffer) {
132 var length = buffer.length;
133 var positive = buffer[length - 1] < 0x80;
134 var result = positive ? 0 : -1;
135 var lossy = false; // Note: We can't use bit manipulation here, since that stops
136 // working if the result won't fit in a 32-bit int.
137
138 if (length < 7) {
139 // Common case which can't possibly be lossy (because the result has
140 // no more than 48 bits, and loss only happens with 54 or more).
141 for (var i = length - 1; i >= 0; i--) {
142 result = result * 0x100 + buffer[i];
143 }
144 } else {
145 for (var _i = length - 1; _i >= 0; _i--) {
146 var one = buffer[_i];
147 result *= 0x100;
148
149 if (isLossyToAdd(result, one)) {
150 lossy = true;
151 }
152
153 result += one;
154 }
155 }
156
157 return {
158 value: result,
159 lossy: lossy
160 };
161}
162/**
163 * Reads an arbitrary unsigned int from a buffer.
164 */
165
166export function readUInt(buffer) {
167 var length = buffer.length;
168 var result = 0;
169 var lossy = false; // Note: See above in re bit manipulation.
170
171 if (length < 7) {
172 // Common case which can't possibly be lossy (see above).
173 for (var i = length - 1; i >= 0; i--) {
174 result = result * 0x100 + buffer[i];
175 }
176 } else {
177 for (var _i2 = length - 1; _i2 >= 0; _i2--) {
178 var one = buffer[_i2];
179 result *= 0x100;
180
181 if (isLossyToAdd(result, one)) {
182 lossy = true;
183 }
184
185 result += one;
186 }
187 }
188
189 return {
190 value: result,
191 lossy: lossy
192 };
193}
194/**
195 * Writes a little-endian 64-bit signed int into a buffer.
196 */
197
198export function writeInt64(value, buffer) {
199 if (value < MIN_EXACT_INT64 || value > MAX_EXACT_INT64) {
200 throw new Error("Value out of range.");
201 }
202
203 if (value < 0) {
204 value += BIT_64;
205 }
206
207 writeUInt64(value, buffer);
208}
209/**
210 * Writes a little-endian 64-bit unsigned int into a buffer.
211 */
212
213export function writeUInt64(value, buffer) {
214 if (value < 0 || value > MAX_EXACT_UINT64) {
215 throw new Error("Value out of range.");
216 }
217
218 var lowWord = value % BIT_32;
219 var highWord = Math.floor(value / BIT_32);
220 buffer[0] = lowWord & 0xff;
221 buffer[1] = lowWord >> 8 & 0xff;
222 buffer[2] = lowWord >> 16 & 0xff;
223 buffer[3] = lowWord >> 24 & 0xff;
224 buffer[4] = highWord & 0xff;
225 buffer[5] = highWord >> 8 & 0xff;
226 buffer[6] = highWord >> 16 & 0xff;
227 buffer[7] = highWord >> 24 & 0xff;
228}
\No newline at end of file