2
0

ascii_hex_stream.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * JavaScript code in this page
  21. */
  22. "use strict";
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.AsciiHexStream = void 0;
  27. var _decode_stream = require("./decode_stream.js");
  28. class AsciiHexStream extends _decode_stream.DecodeStream {
  29. constructor(str, maybeLength) {
  30. if (maybeLength) {
  31. maybeLength *= 0.5;
  32. }
  33. super(maybeLength);
  34. this.str = str;
  35. this.dict = str.dict;
  36. this.firstDigit = -1;
  37. }
  38. readBlock() {
  39. const UPSTREAM_BLOCK_SIZE = 8000;
  40. const bytes = this.str.getBytes(UPSTREAM_BLOCK_SIZE);
  41. if (!bytes.length) {
  42. this.eof = true;
  43. return;
  44. }
  45. const maxDecodeLength = bytes.length + 1 >> 1;
  46. const buffer = this.ensureBuffer(this.bufferLength + maxDecodeLength);
  47. let bufferLength = this.bufferLength;
  48. let firstDigit = this.firstDigit;
  49. for (const ch of bytes) {
  50. let digit;
  51. if (ch >= 0x30 && ch <= 0x39) {
  52. digit = ch & 0x0f;
  53. } else if (ch >= 0x41 && ch <= 0x46 || ch >= 0x61 && ch <= 0x66) {
  54. digit = (ch & 0x0f) + 9;
  55. } else if (ch === 0x3e) {
  56. this.eof = true;
  57. break;
  58. } else {
  59. continue;
  60. }
  61. if (firstDigit < 0) {
  62. firstDigit = digit;
  63. } else {
  64. buffer[bufferLength++] = firstDigit << 4 | digit;
  65. firstDigit = -1;
  66. }
  67. }
  68. if (firstDigit >= 0 && this.eof) {
  69. buffer[bufferLength++] = firstDigit << 4;
  70. firstDigit = -1;
  71. }
  72. this.firstDigit = firstDigit;
  73. this.bufferLength = bufferLength;
  74. }
  75. }
  76. exports.AsciiHexStream = AsciiHexStream;