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