base64-vlq.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* -*- Mode: js; js-indent-level: 2; -*- */
  2. /*
  3. * Copyright 2011 Mozilla Foundation and contributors
  4. * Licensed under the New BSD license. See LICENSE or:
  5. * http://opensource.org/licenses/BSD-3-Clause
  6. *
  7. * Based on the Base 64 VLQ implementation in Closure Compiler:
  8. * https://code.google.com/p/closure-compiler/source/browse/trunk/src/com/google/debugging/sourcemap/Base64VLQ.java
  9. *
  10. * Copyright 2011 The Closure Compiler Authors. All rights reserved.
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are
  13. * met:
  14. *
  15. * * Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * * Redistributions in binary form must reproduce the above
  18. * copyright notice, this list of conditions and the following
  19. * disclaimer in the documentation and/or other materials provided
  20. * with the distribution.
  21. * * Neither the name of Google Inc. nor the names of its
  22. * contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  28. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  30. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  31. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  32. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  33. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. /*eslint no-bitwise:0,quotes:0,global-strict:0*/
  38. var charToIntMap = {};
  39. var intToCharMap = {};
  40. 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
  41. .split('')
  42. .forEach(function (ch, index) {
  43. charToIntMap[ch] = index;
  44. intToCharMap[index] = ch;
  45. });
  46. var base64 = {};
  47. /**
  48. * Encode an integer in the range of 0 to 63 to a single base 64 digit.
  49. */
  50. base64.encode = function base64_encode(aNumber) {
  51. if (aNumber in intToCharMap) {
  52. return intToCharMap[aNumber];
  53. }
  54. throw new TypeError("Must be between 0 and 63: " + aNumber);
  55. };
  56. /**
  57. * Decode a single base 64 digit to an integer.
  58. */
  59. base64.decode = function base64_decode(aChar) {
  60. if (aChar in charToIntMap) {
  61. return charToIntMap[aChar];
  62. }
  63. throw new TypeError("Not a valid base 64 digit: " + aChar);
  64. };
  65. // A single base 64 digit can contain 6 bits of data. For the base 64 variable
  66. // length quantities we use in the source map spec, the first bit is the sign,
  67. // the next four bits are the actual value, and the 6th bit is the
  68. // continuation bit. The continuation bit tells us whether there are more
  69. // digits in this value following this digit.
  70. //
  71. // Continuation
  72. // | Sign
  73. // | |
  74. // V V
  75. // 101011
  76. var VLQ_BASE_SHIFT = 5;
  77. // binary: 100000
  78. var VLQ_BASE = 1 << VLQ_BASE_SHIFT;
  79. // binary: 011111
  80. var VLQ_BASE_MASK = VLQ_BASE - 1;
  81. // binary: 100000
  82. var VLQ_CONTINUATION_BIT = VLQ_BASE;
  83. /**
  84. * Converts from a two-complement value to a value where the sign bit is
  85. * placed in the least significant bit. For example, as decimals:
  86. * 1 becomes 2 (10 binary), -1 becomes 3 (11 binary)
  87. * 2 becomes 4 (100 binary), -2 becomes 5 (101 binary)
  88. */
  89. function toVLQSigned(aValue) {
  90. return aValue < 0
  91. ? ((-aValue) << 1) + 1
  92. : (aValue << 1) + 0;
  93. }
  94. /**
  95. * Converts to a two-complement value from a value where the sign bit is
  96. * placed in the least significant bit. For example, as decimals:
  97. * 2 (10 binary) becomes 1, 3 (11 binary) becomes -1
  98. * 4 (100 binary) becomes 2, 5 (101 binary) becomes -2
  99. */
  100. function fromVLQSigned(aValue) {
  101. var isNegative = (aValue & 1) === 1;
  102. var shifted = aValue >> 1;
  103. return isNegative
  104. ? -shifted
  105. : shifted;
  106. }
  107. /**
  108. * Returns the base 64 VLQ encoded value.
  109. */
  110. exports.encode = function base64VLQ_encode(aValue) {
  111. var encoded = "";
  112. var digit;
  113. var vlq = toVLQSigned(aValue);
  114. do {
  115. digit = vlq & VLQ_BASE_MASK;
  116. vlq >>>= VLQ_BASE_SHIFT;
  117. if (vlq > 0) {
  118. // There are still more digits in this value, so we must make sure the
  119. // continuation bit is marked.
  120. digit |= VLQ_CONTINUATION_BIT;
  121. }
  122. encoded += base64.encode(digit);
  123. } while (vlq > 0);
  124. return encoded;
  125. };
  126. /**
  127. * Decodes the next base 64 VLQ value from the given string and returns the
  128. * value and the rest of the string via the out parameter.
  129. */
  130. exports.decode = function base64VLQ_decode(aStr, aOutParam) {
  131. var i = 0;
  132. var strLen = aStr.length;
  133. var result = 0;
  134. var shift = 0;
  135. var continuation, digit;
  136. do {
  137. if (i >= strLen) {
  138. throw new Error("Expected more digits in base 64 VLQ value.");
  139. }
  140. digit = base64.decode(aStr.charAt(i++));
  141. continuation = !!(digit & VLQ_CONTINUATION_BIT);
  142. digit &= VLQ_BASE_MASK;
  143. result = result + (digit << shift);
  144. shift += VLQ_BASE_SHIFT;
  145. } while (continuation);
  146. aOutParam.value = fromVLQSigned(result);
  147. aOutParam.rest = aStr.slice(i);
  148. };