| 1 | /* |
| 2 | * Copyright 2011 The Closure Compiler Authors. All rights reserved. |
| 3 | * Redistribution and use in source and binary forms, with or without |
| 4 | * modification, are permitted provided that the following conditions are |
| 5 | * met: |
| 6 | * |
| 7 | * * Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * * Redistributions in binary form must reproduce the above |
| 10 | * copyright notice, this list of conditions and the following |
| 11 | * disclaimer in the documentation and/or other materials provided |
| 12 | * with the distribution. |
| 13 | * * Neither the name of Google Inc. nor the names of its |
| 14 | * contributors may be used to endorse or promote products derived |
| 15 | * from this software without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 20 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 21 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 22 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 23 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | */ |
| 29 | |
| 30 | package com.google.debugging.sourcemap; |
| 31 | |
| 32 | import java.io.IOException; |
| 33 | |
| 34 | /** |
| 35 | * We encode our variable length numbers as base64 encoded strings with |
| 36 | * the least significant digit coming first. Each base64 digit encodes |
| 37 | * a 5-bit value (0-31) and a continuation bit. Signed values can be |
| 38 | * represented by using the least significant bit of the value as the |
| 39 | * sign bit. |
| 40 | * |
| 41 | * @author [email protected] (John Lenz) |
| 42 | */ |
| 43 | final class Base64VLQ { |
| 44 | // Utility class. |
| 45 | private Base64VLQ() {} |
| 46 | |
| 47 | // A Base64 VLQ digit can represent 5 bits, so it is base-32. |
| 48 | private static final int VLQ_BASE_SHIFT = 5; |
| 49 | private static final int VLQ_BASE = 1 << VLQ_BASE_SHIFT; |
| 50 | |
| 51 | // A mask of bits for a VLQ digit (11111), 31 decimal. |
| 52 | private static final int VLQ_BASE_MASK = VLQ_BASE-1; |
| 53 | |
| 54 | // The continuation bit is the 6th bit. |
| 55 | private static final int VLQ_CONTINUATION_BIT = VLQ_BASE; |
| 56 | |
| 57 | /** |
| 58 | * Converts from a two-complement value to a value where the sign bit is |
| 59 | * is placed in the least significant bit. For example, as decimals: |
| 60 | * 1 becomes 2 (10 binary), -1 becomes 3 (11 binary) |
| 61 | * 2 becomes 4 (100 binary), -2 becomes 5 (101 binary) |
| 62 | */ |
| 63 | private static int toVLQSigned(int value) { |
| 64 | if (value < 0) { |
| 65 | return ((-value) << 1) + 1; |
| 66 | } else { |
| 67 | return (value << 1) + 0; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Converts to a two-complement value from a value where the sign bit is |
| 73 | * is 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 | private static int fromVLQSigned(int value) { |
| 78 | boolean negate = (value & 1) == 1; |
| 79 | value = value >> 1; |
| 80 | return negate ? -value : value; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Writes a VLQ encoded value to the provide appendable. |
| 85 | * @throws IOException |
| 86 | */ |
| 87 | public static void encode(Appendable out, int value) |
| 88 | throws IOException { |
| 89 | value = toVLQSigned(value); |
| 90 | do { |
| 91 | int digit = value & VLQ_BASE_MASK; |
| 92 | value >>>= VLQ_BASE_SHIFT; |
| 93 | if (value > 0) { |
| 94 | digit |= VLQ_CONTINUATION_BIT; |
| 95 | } |
| 96 | out.append(Base64.toBase64(digit)); |
| 97 | } while (value > 0); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * A simple interface for advancing through a sequence of characters, that |
| 102 | * communicates that advance back to the source. |
| 103 | */ |
| 104 | interface CharIterator { |
| 105 | boolean hasNext(); |
| 106 | char next(); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Decodes the next VLQValue from the provided CharIterator. |
| 111 | */ |
| 112 | public static int decode(CharIterator in) { |
| 113 | int result = 0; |
| 114 | boolean continuation; |
| 115 | int shift = 0; |
| 116 | do { |
| 117 | char c = in.next(); |
| 118 | int digit = Base64.fromBase64(c); |
| 119 | continuation = (digit & VLQ_CONTINUATION_BIT) != 0; |
| 120 | digit &= VLQ_BASE_MASK; |
| 121 | result = result + (digit << shift); |
| 122 | shift = shift + VLQ_BASE_SHIFT; |
| 123 | } while (continuation); |
| 124 | |
| 125 | return fromVLQSigned(result); |
| 126 | } |
| 127 | } |