base64-vlq.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. if (typeof define !== 'function') {
  38. var define = require('amdefine')(module, require);
  39. }
  40. define(function (require, exports, module) {
  41. var base64 = require('./base64');
  42. // A single base 64 digit can contain 6 bits of data. For the base 64 variable
  43. // length quantities we use in the source map spec, the first bit is the sign,
  44. // the next four bits are the actual value, and the 6th bit is the
  45. // continuation bit. The continuation bit tells us whether there are more
  46. // digits in this value following this digit.
  47. //
  48. // Continuation
  49. // | Sign
  50. // | |
  51. // V V
  52. // 101011
  53. var VLQ_BASE_SHIFT = 5;
  54. // binary: 100000
  55. var VLQ_BASE = 1 << VLQ_BASE_SHIFT;
  56. // binary: 011111
  57. var VLQ_BASE_MASK = VLQ_BASE - 1;
  58. // binary: 100000
  59. var VLQ_CONTINUATION_BIT = VLQ_BASE;
  60. /**
  61. * Converts from a two-complement value to a value where the sign bit is
  62. * placed in the least significant bit. For example, as decimals:
  63. * 1 becomes 2 (10 binary), -1 becomes 3 (11 binary)
  64. * 2 becomes 4 (100 binary), -2 becomes 5 (101 binary)
  65. */
  66. function toVLQSigned(aValue) {
  67. return aValue < 0
  68. ? ((-aValue) << 1) + 1
  69. : (aValue << 1) + 0;
  70. }
  71. /**
  72. * Converts to a two-complement value from a value where the sign bit is
  73. * placed in the least significant bit. For example, as decimals:
  74. * 2 (10 binary) becomes 1, 3 (11 binary) becomes -1
  75. * 4 (100 binary) becomes 2, 5 (101 binary) becomes -2
  76. */
  77. function fromVLQSigned(aValue) {
  78. var isNegative = (aValue & 1) === 1;
  79. var shifted = aValue >> 1;
  80. return isNegative
  81. ? -shifted
  82. : shifted;
  83. }
  84. /**
  85. * Returns the base 64 VLQ encoded value.
  86. */
  87. exports.encode = function base64VLQ_encode(aValue) {
  88. var encoded = "";
  89. var digit;
  90. var vlq = toVLQSigned(aValue);
  91. do {
  92. digit = vlq & VLQ_BASE_MASK;
  93. vlq >>>= VLQ_BASE_SHIFT;
  94. if (vlq > 0) {
  95. // There are still more digits in this value, so we must make sure the
  96. // continuation bit is marked.
  97. digit |= VLQ_CONTINUATION_BIT;
  98. }
  99. encoded += base64.encode(digit);
  100. } while (vlq > 0);
  101. return encoded;
  102. };
  103. /**
  104. * Decodes the next base 64 VLQ value from the given string and returns the
  105. * value and the rest of the string via the out parameter.
  106. */
  107. exports.decode = function base64VLQ_decode(aStr, aIndex, aOutParam) {
  108. var strLen = aStr.length;
  109. var result = 0;
  110. var shift = 0;
  111. var continuation, digit;
  112. do {
  113. if (aIndex >= strLen) {
  114. throw new Error("Expected more digits in base 64 VLQ value.");
  115. }
  116. digit = base64.decode(aStr.charCodeAt(aIndex++));
  117. if (digit === -1) {
  118. throw new Error("Invalid base64 digit: " + aStr.charAt(aIndex - 1));
  119. }
  120. continuation = !!(digit & VLQ_CONTINUATION_BIT);
  121. digit &= VLQ_BASE_MASK;
  122. result = result + (digit << shift);
  123. shift += VLQ_BASE_SHIFT;
  124. } while (continuation);
  125. aOutParam.value = fromVLQSigned(result);
  126. aOutParam.rest = aIndex;
  127. };
  128. });