ascii_85_stream.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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.Ascii85Stream = void 0;
  27. var _decode_stream = require("./decode_stream.js");
  28. var _core_utils = require("./core_utils.js");
  29. class Ascii85Stream extends _decode_stream.DecodeStream {
  30. constructor(str, maybeLength) {
  31. if (maybeLength) {
  32. maybeLength *= 0.8;
  33. }
  34. super(maybeLength);
  35. this.str = str;
  36. this.dict = str.dict;
  37. this.input = new Uint8Array(5);
  38. }
  39. readBlock() {
  40. const TILDA_CHAR = 0x7e;
  41. const Z_LOWER_CHAR = 0x7a;
  42. const EOF = -1;
  43. const str = this.str;
  44. let c = str.getByte();
  45. while ((0, _core_utils.isWhiteSpace)(c)) {
  46. c = str.getByte();
  47. }
  48. if (c === EOF || c === TILDA_CHAR) {
  49. this.eof = true;
  50. return;
  51. }
  52. const bufferLength = this.bufferLength;
  53. let buffer, i;
  54. if (c === Z_LOWER_CHAR) {
  55. buffer = this.ensureBuffer(bufferLength + 4);
  56. for (i = 0; i < 4; ++i) {
  57. buffer[bufferLength + i] = 0;
  58. }
  59. this.bufferLength += 4;
  60. } else {
  61. const input = this.input;
  62. input[0] = c;
  63. for (i = 1; i < 5; ++i) {
  64. c = str.getByte();
  65. while ((0, _core_utils.isWhiteSpace)(c)) {
  66. c = str.getByte();
  67. }
  68. input[i] = c;
  69. if (c === EOF || c === TILDA_CHAR) {
  70. break;
  71. }
  72. }
  73. buffer = this.ensureBuffer(bufferLength + i - 1);
  74. this.bufferLength += i - 1;
  75. if (i < 5) {
  76. for (; i < 5; ++i) {
  77. input[i] = 0x21 + 84;
  78. }
  79. this.eof = true;
  80. }
  81. let t = 0;
  82. for (i = 0; i < 5; ++i) {
  83. t = t * 85 + (input[i] - 0x21);
  84. }
  85. for (i = 3; i >= 0; --i) {
  86. buffer[bufferLength + i] = t & 0xff;
  87. t >>= 8;
  88. }
  89. }
  90. }
  91. }
  92. exports.Ascii85Stream = Ascii85Stream;