buffer-more-ints.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. 'use strict';
  2. // JavaScript is numerically challenged
  3. var SHIFT_LEFT_32 = (1 << 16) * (1 << 16);
  4. var SHIFT_RIGHT_32 = 1 / SHIFT_LEFT_32;
  5. // The maximum contiguous integer that can be held in a IEEE754 double
  6. var MAX_INT = 0x1fffffffffffff;
  7. function isContiguousInt(val) {
  8. return val <= MAX_INT && val >= -MAX_INT;
  9. }
  10. function assertContiguousInt(val) {
  11. if (!isContiguousInt(val)) {
  12. throw new TypeError("number cannot be represented as a contiguous integer");
  13. }
  14. }
  15. module.exports.isContiguousInt = isContiguousInt;
  16. module.exports.assertContiguousInt = assertContiguousInt;
  17. // Fill in the regular procedures
  18. ['UInt', 'Int'].forEach(function (sign) {
  19. var suffix = sign + '8';
  20. module.exports['read' + suffix] =
  21. Buffer.prototype['read' + suffix].call;
  22. module.exports['write' + suffix] =
  23. Buffer.prototype['write' + suffix].call;
  24. ['16', '32'].forEach(function (size) {
  25. ['LE', 'BE'].forEach(function (endian) {
  26. var suffix = sign + size + endian;
  27. var read = Buffer.prototype['read' + suffix];
  28. module.exports['read' + suffix] =
  29. function (buf, offset) {
  30. return read.call(buf, offset);
  31. };
  32. var write = Buffer.prototype['write' + suffix];
  33. module.exports['write' + suffix] =
  34. function (buf, val, offset) {
  35. return write.call(buf, val, offset);
  36. };
  37. });
  38. });
  39. });
  40. // Check that a value is an integer within the given range
  41. function check_value(val, min, max) {
  42. val = +val;
  43. if (typeof(val) != 'number' || val < min || val > max || Math.floor(val) !== val) {
  44. throw new TypeError("\"value\" argument is out of bounds");
  45. }
  46. return val;
  47. }
  48. // Check that something is within the Buffer bounds
  49. function check_bounds(buf, offset, len) {
  50. if (offset < 0 || offset + len > buf.length) {
  51. throw new RangeError("Index out of range");
  52. }
  53. }
  54. function readUInt24BE(buf, offset) {
  55. return buf.readUInt8(offset) << 16 | buf.readUInt16BE(offset + 1);
  56. }
  57. module.exports.readUInt24BE = readUInt24BE;
  58. function writeUInt24BE(buf, val, offset) {
  59. val = check_value(val, 0, 0xffffff);
  60. check_bounds(buf, offset, 3);
  61. buf.writeUInt8(val >>> 16, offset);
  62. buf.writeUInt16BE(val & 0xffff, offset + 1);
  63. }
  64. module.exports.writeUInt24BE = writeUInt24BE;
  65. function readUInt40BE(buf, offset) {
  66. return (buf.readUInt8(offset) || 0) * SHIFT_LEFT_32 + buf.readUInt32BE(offset + 1);
  67. }
  68. module.exports.readUInt40BE = readUInt40BE;
  69. function writeUInt40BE(buf, val, offset) {
  70. val = check_value(val, 0, 0xffffffffff);
  71. check_bounds(buf, offset, 5);
  72. buf.writeUInt8(Math.floor(val * SHIFT_RIGHT_32), offset);
  73. buf.writeInt32BE(val & -1, offset + 1);
  74. }
  75. module.exports.writeUInt40BE = writeUInt40BE;
  76. function readUInt48BE(buf, offset) {
  77. return buf.readUInt16BE(offset) * SHIFT_LEFT_32 + buf.readUInt32BE(offset + 2);
  78. }
  79. module.exports.readUInt48BE = readUInt48BE;
  80. function writeUInt48BE(buf, val, offset) {
  81. val = check_value(val, 0, 0xffffffffffff);
  82. check_bounds(buf, offset, 6);
  83. buf.writeUInt16BE(Math.floor(val * SHIFT_RIGHT_32), offset);
  84. buf.writeInt32BE(val & -1, offset + 2);
  85. }
  86. module.exports.writeUInt48BE = writeUInt48BE;
  87. function readUInt56BE(buf, offset) {
  88. return ((buf.readUInt8(offset) || 0) << 16 | buf.readUInt16BE(offset + 1)) * SHIFT_LEFT_32 + buf.readUInt32BE(offset + 3);
  89. }
  90. module.exports.readUInt56BE = readUInt56BE;
  91. function writeUInt56BE(buf, val, offset) {
  92. val = check_value(val, 0, 0xffffffffffffff);
  93. check_bounds(buf, offset, 7);
  94. if (val < 0x100000000000000) {
  95. var hi = Math.floor(val * SHIFT_RIGHT_32);
  96. buf.writeUInt8(hi >>> 16, offset);
  97. buf.writeUInt16BE(hi & 0xffff, offset + 1);
  98. buf.writeInt32BE(val & -1, offset + 3);
  99. } else {
  100. // Special case because 2^56-1 gets rounded up to 2^56
  101. buf[offset] = 0xff;
  102. buf[offset+1] = 0xff;
  103. buf[offset+2] = 0xff;
  104. buf[offset+3] = 0xff;
  105. buf[offset+4] = 0xff;
  106. buf[offset+5] = 0xff;
  107. buf[offset+6] = 0xff;
  108. }
  109. }
  110. module.exports.writeUInt56BE = writeUInt56BE;
  111. function readUInt64BE(buf, offset) {
  112. return buf.readUInt32BE(offset) * SHIFT_LEFT_32 + buf.readUInt32BE(offset + 4);
  113. }
  114. module.exports.readUInt64BE = readUInt64BE;
  115. function writeUInt64BE(buf, val, offset) {
  116. val = check_value(val, 0, 0xffffffffffffffff);
  117. check_bounds(buf, offset, 8);
  118. if (val < 0x10000000000000000) {
  119. buf.writeUInt32BE(Math.floor(val * SHIFT_RIGHT_32), offset);
  120. buf.writeInt32BE(val & -1, offset + 4);
  121. } else {
  122. // Special case because 2^64-1 gets rounded up to 2^64
  123. buf[offset] = 0xff;
  124. buf[offset+1] = 0xff;
  125. buf[offset+2] = 0xff;
  126. buf[offset+3] = 0xff;
  127. buf[offset+4] = 0xff;
  128. buf[offset+5] = 0xff;
  129. buf[offset+6] = 0xff;
  130. buf[offset+7] = 0xff;
  131. }
  132. }
  133. module.exports.writeUInt64BE = writeUInt64BE;
  134. function readUInt24LE(buf, offset) {
  135. return buf.readUInt8(offset + 2) << 16 | buf.readUInt16LE(offset);
  136. }
  137. module.exports.readUInt24LE = readUInt24LE;
  138. function writeUInt24LE(buf, val, offset) {
  139. val = check_value(val, 0, 0xffffff);
  140. check_bounds(buf, offset, 3);
  141. buf.writeUInt16LE(val & 0xffff, offset);
  142. buf.writeUInt8(val >>> 16, offset + 2);
  143. }
  144. module.exports.writeUInt24LE = writeUInt24LE;
  145. function readUInt40LE(buf, offset) {
  146. return (buf.readUInt8(offset + 4) || 0) * SHIFT_LEFT_32 + buf.readUInt32LE(offset);
  147. }
  148. module.exports.readUInt40LE = readUInt40LE;
  149. function writeUInt40LE(buf, val, offset) {
  150. val = check_value(val, 0, 0xffffffffff);
  151. check_bounds(buf, offset, 5);
  152. buf.writeInt32LE(val & -1, offset);
  153. buf.writeUInt8(Math.floor(val * SHIFT_RIGHT_32), offset + 4);
  154. }
  155. module.exports.writeUInt40LE = writeUInt40LE;
  156. function readUInt48LE(buf, offset) {
  157. return buf.readUInt16LE(offset + 4) * SHIFT_LEFT_32 + buf.readUInt32LE(offset);
  158. }
  159. module.exports.readUInt48LE = readUInt48LE;
  160. function writeUInt48LE(buf, val, offset) {
  161. val = check_value(val, 0, 0xffffffffffff);
  162. check_bounds(buf, offset, 6);
  163. buf.writeInt32LE(val & -1, offset);
  164. buf.writeUInt16LE(Math.floor(val * SHIFT_RIGHT_32), offset + 4);
  165. }
  166. module.exports.writeUInt48LE = writeUInt48LE;
  167. function readUInt56LE(buf, offset) {
  168. return ((buf.readUInt8(offset + 6) || 0) << 16 | buf.readUInt16LE(offset + 4)) * SHIFT_LEFT_32 + buf.readUInt32LE(offset);
  169. }
  170. module.exports.readUInt56LE = readUInt56LE;
  171. function writeUInt56LE(buf, val, offset) {
  172. val = check_value(val, 0, 0xffffffffffffff);
  173. check_bounds(buf, offset, 7);
  174. if (val < 0x100000000000000) {
  175. buf.writeInt32LE(val & -1, offset);
  176. var hi = Math.floor(val * SHIFT_RIGHT_32);
  177. buf.writeUInt16LE(hi & 0xffff, offset + 4);
  178. buf.writeUInt8(hi >>> 16, offset + 6);
  179. } else {
  180. // Special case because 2^56-1 gets rounded up to 2^56
  181. buf[offset] = 0xff;
  182. buf[offset+1] = 0xff;
  183. buf[offset+2] = 0xff;
  184. buf[offset+3] = 0xff;
  185. buf[offset+4] = 0xff;
  186. buf[offset+5] = 0xff;
  187. buf[offset+6] = 0xff;
  188. }
  189. }
  190. module.exports.writeUInt56LE = writeUInt56LE;
  191. function readUInt64LE(buf, offset) {
  192. return buf.readUInt32LE(offset + 4) * SHIFT_LEFT_32 + buf.readUInt32LE(offset);
  193. }
  194. module.exports.readUInt64LE = readUInt64LE;
  195. function writeUInt64LE(buf, val, offset) {
  196. val = check_value(val, 0, 0xffffffffffffffff);
  197. check_bounds(buf, offset, 8);
  198. if (val < 0x10000000000000000) {
  199. buf.writeInt32LE(val & -1, offset);
  200. buf.writeUInt32LE(Math.floor(val * SHIFT_RIGHT_32), offset + 4);
  201. } else {
  202. // Special case because 2^64-1 gets rounded up to 2^64
  203. buf[offset] = 0xff;
  204. buf[offset+1] = 0xff;
  205. buf[offset+2] = 0xff;
  206. buf[offset+3] = 0xff;
  207. buf[offset+4] = 0xff;
  208. buf[offset+5] = 0xff;
  209. buf[offset+6] = 0xff;
  210. buf[offset+7] = 0xff;
  211. }
  212. }
  213. module.exports.writeUInt64LE = writeUInt64LE;
  214. function readInt24BE(buf, offset) {
  215. return (buf.readInt8(offset) << 16) + buf.readUInt16BE(offset + 1);
  216. }
  217. module.exports.readInt24BE = readInt24BE;
  218. function writeInt24BE(buf, val, offset) {
  219. val = check_value(val, -0x800000, 0x7fffff);
  220. check_bounds(buf, offset, 3);
  221. buf.writeInt8(val >> 16, offset);
  222. buf.writeUInt16BE(val & 0xffff, offset + 1);
  223. }
  224. module.exports.writeInt24BE = writeInt24BE;
  225. function readInt40BE(buf, offset) {
  226. return (buf.readInt8(offset) || 0) * SHIFT_LEFT_32 + buf.readUInt32BE(offset + 1);
  227. }
  228. module.exports.readInt40BE = readInt40BE;
  229. function writeInt40BE(buf, val, offset) {
  230. val = check_value(val, -0x8000000000, 0x7fffffffff);
  231. check_bounds(buf, offset, 5);
  232. buf.writeInt8(Math.floor(val * SHIFT_RIGHT_32), offset);
  233. buf.writeInt32BE(val & -1, offset + 1);
  234. }
  235. module.exports.writeInt40BE = writeInt40BE;
  236. function readInt48BE(buf, offset) {
  237. return buf.readInt16BE(offset) * SHIFT_LEFT_32 + buf.readUInt32BE(offset + 2);
  238. }
  239. module.exports.readInt48BE = readInt48BE;
  240. function writeInt48BE(buf, val, offset) {
  241. val = check_value(val, -0x800000000000, 0x7fffffffffff);
  242. check_bounds(buf, offset, 6);
  243. buf.writeInt16BE(Math.floor(val * SHIFT_RIGHT_32), offset);
  244. buf.writeInt32BE(val & -1, offset + 2);
  245. }
  246. module.exports.writeInt48BE = writeInt48BE;
  247. function readInt56BE(buf, offset) {
  248. return (((buf.readInt8(offset) || 0) << 16) + buf.readUInt16BE(offset + 1)) * SHIFT_LEFT_32 + buf.readUInt32BE(offset + 3);
  249. }
  250. module.exports.readInt56BE = readInt56BE;
  251. function writeInt56BE(buf, val, offset) {
  252. val = check_value(val, -0x800000000000000, 0x7fffffffffffff);
  253. check_bounds(buf, offset, 7);
  254. if (val < 0x80000000000000) {
  255. var hi = Math.floor(val * SHIFT_RIGHT_32);
  256. buf.writeInt8(hi >> 16, offset);
  257. buf.writeUInt16BE(hi & 0xffff, offset + 1);
  258. buf.writeInt32BE(val & -1, offset + 3);
  259. } else {
  260. // Special case because 2^55-1 gets rounded up to 2^55
  261. buf[offset] = 0x7f;
  262. buf[offset+1] = 0xff;
  263. buf[offset+2] = 0xff;
  264. buf[offset+3] = 0xff;
  265. buf[offset+4] = 0xff;
  266. buf[offset+5] = 0xff;
  267. buf[offset+6] = 0xff;
  268. }
  269. }
  270. module.exports.writeInt56BE = writeInt56BE;
  271. function readInt64BE(buf, offset) {
  272. return buf.readInt32BE(offset) * SHIFT_LEFT_32 + buf.readUInt32BE(offset + 4);
  273. }
  274. module.exports.readInt64BE = readInt64BE;
  275. function writeInt64BE(buf, val, offset) {
  276. val = check_value(val, -0x800000000000000000, 0x7fffffffffffffff);
  277. check_bounds(buf, offset, 8);
  278. if (val < 0x8000000000000000) {
  279. buf.writeInt32BE(Math.floor(val * SHIFT_RIGHT_32), offset);
  280. buf.writeInt32BE(val & -1, offset + 4);
  281. } else {
  282. // Special case because 2^63-1 gets rounded up to 2^63
  283. buf[offset] = 0x7f;
  284. buf[offset+1] = 0xff;
  285. buf[offset+2] = 0xff;
  286. buf[offset+3] = 0xff;
  287. buf[offset+4] = 0xff;
  288. buf[offset+5] = 0xff;
  289. buf[offset+6] = 0xff;
  290. buf[offset+7] = 0xff;
  291. }
  292. }
  293. module.exports.writeInt64BE = writeInt64BE;
  294. function readInt24LE(buf, offset) {
  295. return (buf.readInt8(offset + 2) << 16) + buf.readUInt16LE(offset);
  296. }
  297. module.exports.readInt24LE = readInt24LE;
  298. function writeInt24LE(buf, val, offset) {
  299. val = check_value(val, -0x800000, 0x7fffff);
  300. check_bounds(buf, offset, 3);
  301. buf.writeUInt16LE(val & 0xffff, offset);
  302. buf.writeInt8(val >> 16, offset + 2);
  303. }
  304. module.exports.writeInt24LE = writeInt24LE;
  305. function readInt40LE(buf, offset) {
  306. return (buf.readInt8(offset + 4) || 0) * SHIFT_LEFT_32 + buf.readUInt32LE(offset);
  307. }
  308. module.exports.readInt40LE = readInt40LE;
  309. function writeInt40LE(buf, val, offset) {
  310. val = check_value(val, -0x8000000000, 0x7fffffffff);
  311. check_bounds(buf, offset, 5);
  312. buf.writeInt32LE(val & -1, offset);
  313. buf.writeInt8(Math.floor(val * SHIFT_RIGHT_32), offset + 4);
  314. }
  315. module.exports.writeInt40LE = writeInt40LE;
  316. function readInt48LE(buf, offset) {
  317. return buf.readInt16LE(offset + 4) * SHIFT_LEFT_32 + buf.readUInt32LE(offset);
  318. }
  319. module.exports.readInt48LE = readInt48LE;
  320. function writeInt48LE(buf, val, offset) {
  321. val = check_value(val, -0x800000000000, 0x7fffffffffff);
  322. check_bounds(buf, offset, 6);
  323. buf.writeInt32LE(val & -1, offset);
  324. buf.writeInt16LE(Math.floor(val * SHIFT_RIGHT_32), offset + 4);
  325. }
  326. module.exports.writeInt48LE = writeInt48LE;
  327. function readInt56LE(buf, offset) {
  328. return (((buf.readInt8(offset + 6) || 0) << 16) + buf.readUInt16LE(offset + 4)) * SHIFT_LEFT_32 + buf.readUInt32LE(offset);
  329. }
  330. module.exports.readInt56LE = readInt56LE;
  331. function writeInt56LE(buf, val, offset) {
  332. val = check_value(val, -0x80000000000000, 0x7fffffffffffff);
  333. check_bounds(buf, offset, 7);
  334. if (val < 0x80000000000000) {
  335. buf.writeInt32LE(val & -1, offset);
  336. var hi = Math.floor(val * SHIFT_RIGHT_32);
  337. buf.writeUInt16LE(hi & 0xffff, offset + 4);
  338. buf.writeInt8(hi >> 16, offset + 6);
  339. } else {
  340. // Special case because 2^55-1 gets rounded up to 2^55
  341. buf[offset] = 0xff;
  342. buf[offset+1] = 0xff;
  343. buf[offset+2] = 0xff;
  344. buf[offset+3] = 0xff;
  345. buf[offset+4] = 0xff;
  346. buf[offset+5] = 0xff;
  347. buf[offset+6] = 0x7f;
  348. }
  349. }
  350. module.exports.writeInt56LE = writeInt56LE;
  351. function readInt64LE(buf, offset) {
  352. return buf.readInt32LE(offset + 4) * SHIFT_LEFT_32 + buf.readUInt32LE(offset);
  353. }
  354. module.exports.readInt64LE = readInt64LE;
  355. function writeInt64LE(buf, val, offset) {
  356. val = check_value(val, -0x8000000000000000, 0x7fffffffffffffff);
  357. check_bounds(buf, offset, 8);
  358. if (val < 0x8000000000000000) {
  359. buf.writeInt32LE(val & -1, offset);
  360. buf.writeInt32LE(Math.floor(val * SHIFT_RIGHT_32), offset + 4);
  361. } else {
  362. // Special case because 2^55-1 gets rounded up to 2^55
  363. buf[offset] = 0xff;
  364. buf[offset+1] = 0xff;
  365. buf[offset+2] = 0xff;
  366. buf[offset+3] = 0xff;
  367. buf[offset+4] = 0xff;
  368. buf[offset+5] = 0xff;
  369. buf[offset+6] = 0xff;
  370. buf[offset+7] = 0x7f;
  371. }
  372. }
  373. module.exports.writeInt64LE = writeInt64LE;