bit_stream.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.BitStream = void 0;
  4. const bit_1 = require("./bit");
  5. const byte_stream_1 = require("./byte_stream");
  6. class BitStream {
  7. constructor(parameters) {
  8. this.buffer = new ArrayBuffer(0);
  9. this.view = new Uint8Array(this.buffer);
  10. this.bitsCount = 0;
  11. if (parameters) {
  12. if ("byteStream" in parameters) {
  13. this.fromByteStream(parameters.byteStream);
  14. }
  15. if ("view" in parameters) {
  16. this.fromUint8Array(parameters.view);
  17. }
  18. if ("buffer" in parameters) {
  19. this.fromArrayBuffer(parameters.buffer);
  20. }
  21. if ("string" in parameters) {
  22. this.fromString(parameters.string);
  23. }
  24. if ("uint32" in parameters) {
  25. this.fromUint32(parameters.uint32);
  26. }
  27. if ("bitsCount" in parameters && parameters.bitsCount) {
  28. this.bitsCount = parameters.bitsCount;
  29. }
  30. }
  31. }
  32. clear() {
  33. this.buffer = new ArrayBuffer(0);
  34. this.view = new Uint8Array(this.buffer);
  35. this.bitsCount = 0;
  36. }
  37. fromByteStream(stream) {
  38. this.fromUint8Array(stream.view);
  39. }
  40. fromArrayBuffer(array) {
  41. this.buffer = array;
  42. this.view = new Uint8Array(array);
  43. this.bitsCount = this.view.length << 3;
  44. }
  45. fromUint8Array(array) {
  46. this.fromArrayBuffer(new Uint8Array(array).buffer);
  47. }
  48. fromString(string) {
  49. const stringLength = string.length;
  50. this.buffer = new ArrayBuffer((stringLength >> 3) + ((stringLength % 8) ? 1 : 0));
  51. this.view = new Uint8Array(this.buffer);
  52. this.bitsCount = ((stringLength >> 3) + 1) << 3;
  53. let byteIndex = 0;
  54. for (let i = 0; i < stringLength; i++) {
  55. if (string[i] == "1")
  56. this.view[byteIndex] |= 1 << (7 - (i % 8));
  57. if (i && (((i + 1) % 8) == 0))
  58. byteIndex++;
  59. }
  60. if (stringLength % 8)
  61. this.shiftRight(8 - (stringLength % 8));
  62. this.bitsCount = stringLength;
  63. }
  64. fromUint32(uint32) {
  65. this.buffer = new ArrayBuffer(4);
  66. this.view = new Uint8Array(this.buffer);
  67. const value = new Uint32Array([uint32]);
  68. const view = new Uint8Array(value.buffer);
  69. for (let i = 3; i >= 0; i--)
  70. this.view[i] = view[3 - i];
  71. this.bitsCount = 32;
  72. }
  73. toString(start, length) {
  74. if (start == null) {
  75. start = 0;
  76. }
  77. if ((start >= this.view.length) || (start < 0)) {
  78. start = 0;
  79. }
  80. if (length == null) {
  81. length = this.view.length - start;
  82. }
  83. if ((length >= this.view.length) || (length < 0)) {
  84. length = this.view.length - start;
  85. }
  86. const result = [];
  87. for (let i = start; i < (start + length); i++) {
  88. result.push(bit_1.bitsToStringArray[this.view[i]]);
  89. }
  90. return result.join("").substring((this.view.length << 3) - this.bitsCount);
  91. }
  92. shiftRight(shift, needShrink = true) {
  93. if (this.view.length == 0) {
  94. return;
  95. }
  96. if ((shift < 0) || (shift > 8)) {
  97. throw new Error("The \"shift\" parameter must be in range 0-8");
  98. }
  99. if (shift > this.bitsCount) {
  100. throw new Error("The \"shift\" parameter can not be bigger than \"this.bitsCount\"");
  101. }
  102. const shiftMask = 0xFF >> (8 - shift);
  103. this.view[this.view.length - 1] >>= shift;
  104. for (let i = (this.view.length - 2); i >= 0; i--) {
  105. this.view[i + 1] |= (this.view[i] & shiftMask) << (8 - shift);
  106. this.view[i] >>= shift;
  107. }
  108. this.bitsCount -= shift;
  109. if (this.bitsCount == 0) {
  110. this.clear();
  111. }
  112. if (needShrink) {
  113. this.shrink();
  114. }
  115. }
  116. shiftLeft(shift) {
  117. if (this.view.length == 0) {
  118. return;
  119. }
  120. if ((shift < 0) || (shift > 8)) {
  121. throw new Error("The \"shift\" parameter must be in range 0-8");
  122. }
  123. if (shift > this.bitsCount) {
  124. throw new Error("The \"shift\" parameter can not be bigger than \"this.bitsCount\"");
  125. }
  126. const bitsOffset = this.bitsCount & 0x07;
  127. if (bitsOffset > shift) {
  128. this.view[0] &= 0xFF >> (bitsOffset + shift);
  129. }
  130. else {
  131. const view = this.view.slice(1);
  132. view[0] &= 0xFF >> (shift - bitsOffset);
  133. this.buffer = view.buffer;
  134. this.view = view;
  135. }
  136. this.bitsCount -= shift;
  137. if (this.bitsCount == 0) {
  138. this.clear();
  139. }
  140. }
  141. slice(start = 0, end = 0) {
  142. let valueShift = 0;
  143. if (this.bitsCount % 8) {
  144. valueShift = (8 - (this.bitsCount % 8));
  145. }
  146. start += valueShift;
  147. end += valueShift;
  148. const maxEnd = (this.view.length << 3) - 1;
  149. if ((start < 0) || (start > maxEnd)) {
  150. return new BitStream();
  151. }
  152. if (!end) {
  153. end = maxEnd;
  154. }
  155. if ((end < 0) || (end > maxEnd)) {
  156. return new BitStream();
  157. }
  158. if ((end - start + 1) > this.bitsCount) {
  159. return new BitStream();
  160. }
  161. const startIndex = start >> 3;
  162. const startOffset = start & 0x07;
  163. const endIndex = end >> 3;
  164. const endOffset = end & 0x07;
  165. const bitsLength = ((endIndex - startIndex) == 0) ? 1 : (endIndex - startIndex + 1);
  166. const result = new BitStream({
  167. buffer: this.buffer.slice(startIndex, startIndex + bitsLength),
  168. bitsCount: bitsLength << 3,
  169. });
  170. result.view[0] &= (0xFF >> startOffset);
  171. result.view[bitsLength] &= (0xFF << (7 - endOffset));
  172. if (7 - endOffset) {
  173. result.shiftRight(7 - endOffset, false);
  174. }
  175. result.bitsCount = (end - start + 1);
  176. result.shrink();
  177. return result;
  178. }
  179. copy(start = 0, length = 0) {
  180. const maxEnd = (this.view.length << 3) - 1;
  181. if ((start < 0) || (start > maxEnd)) {
  182. return new BitStream();
  183. }
  184. if (!length) {
  185. length = (this.view.length << 3) - start - 1;
  186. }
  187. if (length > this.bitsCount) {
  188. return new BitStream();
  189. }
  190. return this.slice(start, start + length - 1);
  191. }
  192. shrink() {
  193. const currentLength = (this.bitsCount >> 3) + ((this.bitsCount % 8) ? 1 : 0);
  194. if (currentLength < this.view.length) {
  195. const view = this.view.slice(this.view.length - currentLength, (this.view.length - currentLength) + currentLength);
  196. this.view = view;
  197. this.buffer = view.buffer;
  198. }
  199. }
  200. reverseBytes() {
  201. for (let i = 0; i < this.view.length; i++) {
  202. this.view[i] = ((this.view[i] * 0x0802 & 0x22110) | (this.view[i] * 0x8020 & 0x88440)) * 0x10101 >> 16;
  203. }
  204. if (this.bitsCount % 8) {
  205. const currentLength = (this.bitsCount >> 3) + ((this.bitsCount % 8) ? 1 : 0);
  206. this.view[this.view.length - currentLength] >>= (8 - (this.bitsCount & 0x07));
  207. }
  208. }
  209. reverseValue() {
  210. const initialValue = this.toString();
  211. const initialValueLength = initialValue.length;
  212. const reversedValue = new Array(initialValueLength);
  213. for (let i = 0; i < initialValueLength; i++) {
  214. reversedValue[initialValueLength - 1 - i] = initialValue[i];
  215. }
  216. this.fromString(reversedValue.join(""));
  217. }
  218. getNumberValue() {
  219. const byteLength = (this.view.length - 1);
  220. if (byteLength > 3) {
  221. return (-1);
  222. }
  223. if (byteLength == (-1)) {
  224. return 0;
  225. }
  226. const value = new Uint32Array(1);
  227. const view = new Uint8Array(value.buffer);
  228. for (let i = byteLength; i >= 0; i--) {
  229. view[byteLength - i] = this.view[i];
  230. }
  231. return value[0];
  232. }
  233. findPattern(pattern, start, length, backward) {
  234. const stringStream = new byte_stream_1.ByteStream({
  235. string: this.toString(),
  236. });
  237. const stringPattern = new byte_stream_1.ByteStream({
  238. string: pattern.toString()
  239. });
  240. return stringStream.findPattern(stringPattern, start, length, backward);
  241. }
  242. findFirstIn(patterns, start, length, backward) {
  243. const stringStream = new byte_stream_1.ByteStream({
  244. string: this.toString(),
  245. });
  246. const stringPatterns = new Array(patterns.length);
  247. for (let i = 0; i < patterns.length; i++) {
  248. stringPatterns[i] = new byte_stream_1.ByteStream({
  249. string: patterns[i].toString()
  250. });
  251. }
  252. return stringStream.findFirstIn(stringPatterns, start, length, backward);
  253. }
  254. findAllIn(patterns, start, length) {
  255. const stringStream = new byte_stream_1.ByteStream({
  256. string: this.toString()
  257. });
  258. const stringPatterns = new Array(patterns.length);
  259. for (let i = 0; i < patterns.length; i++) {
  260. stringPatterns[i] = new byte_stream_1.ByteStream({
  261. string: patterns[i].toString()
  262. });
  263. }
  264. return stringStream.findAllIn(stringPatterns, start, length);
  265. }
  266. findAllPatternIn(pattern, start, length) {
  267. const stringStream = new byte_stream_1.ByteStream({
  268. string: this.toString()
  269. });
  270. const stringPattern = new byte_stream_1.ByteStream({
  271. string: pattern.toString()
  272. });
  273. return stringStream.findAllPatternIn(stringPattern, start, length);
  274. }
  275. findFirstNotIn(patterns, start, length, backward) {
  276. const stringStream = new byte_stream_1.ByteStream({
  277. string: this.toString()
  278. });
  279. const stringPatterns = new Array(patterns.length);
  280. for (let i = 0; i < patterns.length; i++) {
  281. stringPatterns[i] = new byte_stream_1.ByteStream({
  282. string: patterns[i].toString()
  283. });
  284. }
  285. return stringStream.findFirstNotIn(stringPatterns, start, length, backward);
  286. }
  287. findAllNotIn(patterns, start, length) {
  288. const stringStream = new byte_stream_1.ByteStream({
  289. string: this.toString()
  290. });
  291. const stringPatterns = new Array(patterns.length);
  292. for (let i = 0; i < patterns.length; i++) {
  293. stringPatterns[i] = new byte_stream_1.ByteStream({
  294. string: patterns[i].toString()
  295. });
  296. }
  297. return stringStream.findAllNotIn(stringPatterns, start, length);
  298. }
  299. findFirstSequence(patterns, start, length, backward) {
  300. const stringStream = new byte_stream_1.ByteStream({
  301. string: this.toString()
  302. });
  303. const stringPatterns = new Array(patterns.length);
  304. for (let i = 0; i < patterns.length; i++) {
  305. stringPatterns[i] = new byte_stream_1.ByteStream({
  306. string: patterns[i].toString()
  307. });
  308. }
  309. return stringStream.findFirstSequence(stringPatterns, start, length, backward);
  310. }
  311. findAllSequences(patterns, start, length) {
  312. const stringStream = new byte_stream_1.ByteStream({
  313. string: this.toString()
  314. });
  315. const stringPatterns = new Array(patterns.length);
  316. for (let i = 0; i < patterns.length; i++) {
  317. stringPatterns[i] = new byte_stream_1.ByteStream({
  318. string: patterns[i].toString()
  319. });
  320. }
  321. return stringStream.findAllSequences(stringPatterns, start, length);
  322. }
  323. findPairedPatterns(leftPattern, rightPattern, start, length) {
  324. const stringStream = new byte_stream_1.ByteStream({
  325. string: this.toString()
  326. });
  327. const stringLeftPattern = new byte_stream_1.ByteStream({
  328. string: leftPattern.toString()
  329. });
  330. const stringRightPattern = new byte_stream_1.ByteStream({
  331. string: rightPattern.toString()
  332. });
  333. return stringStream.findPairedPatterns(stringLeftPattern, stringRightPattern, start, length);
  334. }
  335. findPairedArrays(inputLeftPatterns, inputRightPatterns, start, length) {
  336. const stringStream = new byte_stream_1.ByteStream({
  337. string: this.toString()
  338. });
  339. const stringLeftPatterns = new Array(inputLeftPatterns.length);
  340. for (let i = 0; i < inputLeftPatterns.length; i++) {
  341. stringLeftPatterns[i] = new byte_stream_1.ByteStream({
  342. string: inputLeftPatterns[i].toString()
  343. });
  344. }
  345. const stringRightPatterns = new Array(inputRightPatterns.length);
  346. for (let i = 0; i < inputRightPatterns.length; i++) {
  347. stringRightPatterns[i] = new byte_stream_1.ByteStream({
  348. string: inputRightPatterns[i].toString()
  349. });
  350. }
  351. return stringStream.findPairedArrays(stringLeftPatterns, stringRightPatterns, start, length);
  352. }
  353. replacePattern(searchPattern, replacePattern, start, length) {
  354. const stringStream = new byte_stream_1.ByteStream({
  355. string: this.toString()
  356. });
  357. const stringSearchPattern = new byte_stream_1.ByteStream({
  358. string: searchPattern.toString()
  359. });
  360. const stringReplacePattern = new byte_stream_1.ByteStream({
  361. string: replacePattern.toString()
  362. });
  363. if (stringStream.replacePattern(stringSearchPattern, stringReplacePattern, start, length)) {
  364. this.fromString(stringStream.toString());
  365. return true;
  366. }
  367. return false;
  368. }
  369. skipPatterns(patterns, start, length, backward) {
  370. const stringStream = new byte_stream_1.ByteStream({
  371. string: this.toString()
  372. });
  373. const stringPatterns = new Array(patterns.length);
  374. for (let i = 0; i < patterns.length; i++) {
  375. stringPatterns[i] = new byte_stream_1.ByteStream({
  376. string: patterns[i].toString()
  377. });
  378. }
  379. return stringStream.skipPatterns(stringPatterns, start, length, backward);
  380. }
  381. skipNotPatterns(patterns, start, length, backward) {
  382. const stringStream = new byte_stream_1.ByteStream({
  383. string: this.toString()
  384. });
  385. const stringPatterns = new Array(patterns.length);
  386. for (let i = 0; i < patterns.length; i++) {
  387. stringPatterns[i] = new byte_stream_1.ByteStream({
  388. string: patterns[i].toString()
  389. });
  390. }
  391. return stringStream.skipNotPatterns(stringPatterns, start, length, backward);
  392. }
  393. append(stream) {
  394. this.fromString([
  395. this.toString(),
  396. stream.toString()
  397. ].join(""));
  398. }
  399. }
  400. exports.BitStream = BitStream;