Implemented URL query parsing for initial token /opa/?token=abcde
[src/app-framework-demo.git] / afb-client / bower_components / jszip / lib / utf8.js
1 'use strict';
2
3 var utils = require('./utils');
4 var support = require('./support');
5 var nodeBuffer = require('./nodeBuffer');
6
7 /**
8  * The following functions come from pako, from pako/lib/utils/strings
9  * released under the MIT license, see pako https://github.com/nodeca/pako/
10  */
11
12 // Table with utf8 lengths (calculated by first byte of sequence)
13 // Note, that 5 & 6-byte values and some 4-byte values can not be represented in JS,
14 // because max possible codepoint is 0x10ffff
15 var _utf8len = new Array(256);
16 for (var i=0; i<256; i++) {
17   _utf8len[i] = (i >= 252 ? 6 : i >= 248 ? 5 : i >= 240 ? 4 : i >= 224 ? 3 : i >= 192 ? 2 : 1);
18 }
19 _utf8len[254]=_utf8len[254]=1; // Invalid sequence start
20
21 // convert string to array (typed, when possible)
22 var string2buf = function (str) {
23     var buf, c, c2, m_pos, i, str_len = str.length, buf_len = 0;
24
25     // count binary size
26     for (m_pos = 0; m_pos < str_len; m_pos++) {
27         c = str.charCodeAt(m_pos);
28         if ((c & 0xfc00) === 0xd800 && (m_pos+1 < str_len)) {
29             c2 = str.charCodeAt(m_pos+1);
30             if ((c2 & 0xfc00) === 0xdc00) {
31                 c = 0x10000 + ((c - 0xd800) << 10) + (c2 - 0xdc00);
32                 m_pos++;
33             }
34         }
35         buf_len += c < 0x80 ? 1 : c < 0x800 ? 2 : c < 0x10000 ? 3 : 4;
36     }
37
38     // allocate buffer
39     if (support.uint8array) {
40         buf = new Uint8Array(buf_len);
41     } else {
42         buf = new Array(buf_len);
43     }
44
45     // convert
46     for (i=0, m_pos = 0; i < buf_len; m_pos++) {
47         c = str.charCodeAt(m_pos);
48         if ((c & 0xfc00) === 0xd800 && (m_pos+1 < str_len)) {
49             c2 = str.charCodeAt(m_pos+1);
50             if ((c2 & 0xfc00) === 0xdc00) {
51                 c = 0x10000 + ((c - 0xd800) << 10) + (c2 - 0xdc00);
52                 m_pos++;
53             }
54         }
55         if (c < 0x80) {
56             /* one byte */
57             buf[i++] = c;
58         } else if (c < 0x800) {
59             /* two bytes */
60             buf[i++] = 0xC0 | (c >>> 6);
61             buf[i++] = 0x80 | (c & 0x3f);
62         } else if (c < 0x10000) {
63             /* three bytes */
64             buf[i++] = 0xE0 | (c >>> 12);
65             buf[i++] = 0x80 | (c >>> 6 & 0x3f);
66             buf[i++] = 0x80 | (c & 0x3f);
67         } else {
68             /* four bytes */
69             buf[i++] = 0xf0 | (c >>> 18);
70             buf[i++] = 0x80 | (c >>> 12 & 0x3f);
71             buf[i++] = 0x80 | (c >>> 6 & 0x3f);
72             buf[i++] = 0x80 | (c & 0x3f);
73         }
74     }
75
76     return buf;
77 };
78
79 // Calculate max possible position in utf8 buffer,
80 // that will not break sequence. If that's not possible
81 // - (very small limits) return max size as is.
82 //
83 // buf[] - utf8 bytes array
84 // max   - length limit (mandatory);
85 var utf8border = function(buf, max) {
86     var pos;
87
88     max = max || buf.length;
89     if (max > buf.length) { max = buf.length; }
90
91     // go back from last position, until start of sequence found
92     pos = max-1;
93     while (pos >= 0 && (buf[pos] & 0xC0) === 0x80) { pos--; }
94
95     // Fuckup - very small and broken sequence,
96     // return max, because we should return something anyway.
97     if (pos < 0) { return max; }
98
99     // If we came to start of buffer - that means vuffer is too small,
100     // return max too.
101     if (pos === 0) { return max; }
102
103     return (pos + _utf8len[buf[pos]] > max) ? pos : max;
104 };
105
106 // convert array to string
107 var buf2string = function (buf) {
108     var str, i, out, c, c_len;
109     var len = buf.length;
110
111     // Reserve max possible length (2 words per char)
112     // NB: by unknown reasons, Array is significantly faster for
113     //     String.fromCharCode.apply than Uint16Array.
114     var utf16buf = new Array(len*2);
115
116     for (out=0, i=0; i<len;) {
117         c = buf[i++];
118         // quick process ascii
119         if (c < 0x80) { utf16buf[out++] = c; continue; }
120
121         c_len = _utf8len[c];
122         // skip 5 & 6 byte codes
123         if (c_len > 4) { utf16buf[out++] = 0xfffd; i += c_len-1; continue; }
124
125         // apply mask on first byte
126         c &= c_len === 2 ? 0x1f : c_len === 3 ? 0x0f : 0x07;
127         // join the rest
128         while (c_len > 1 && i < len) {
129             c = (c << 6) | (buf[i++] & 0x3f);
130             c_len--;
131         }
132
133         // terminated by end of string?
134         if (c_len > 1) { utf16buf[out++] = 0xfffd; continue; }
135
136         if (c < 0x10000) {
137             utf16buf[out++] = c;
138         } else {
139             c -= 0x10000;
140             utf16buf[out++] = 0xd800 | ((c >> 10) & 0x3ff);
141             utf16buf[out++] = 0xdc00 | (c & 0x3ff);
142         }
143     }
144
145     // shrinkBuf(utf16buf, out)
146     if (utf16buf.length !== out) {
147         if(utf16buf.subarray) {
148             utf16buf = utf16buf.subarray(0, out);
149         } else {
150             utf16buf.length = out;
151         }
152     }
153
154     // return String.fromCharCode.apply(null, utf16buf);
155     return utils.applyFromCharCode(utf16buf);
156 };
157
158
159 // That's all for the pako functions.
160
161
162 /**
163  * Transform a javascript string into an array (typed if possible) of bytes,
164  * UTF-8 encoded.
165  * @param {String} str the string to encode
166  * @return {Array|Uint8Array|Buffer} the UTF-8 encoded string.
167  */
168 exports.utf8encode = function utf8encode(str) {
169     if (support.nodebuffer) {
170         return nodeBuffer(str, "utf-8");
171     }
172
173     return string2buf(str);
174 };
175
176
177 /**
178  * Transform a bytes array (or a representation) representing an UTF-8 encoded
179  * string into a javascript string.
180  * @param {Array|Uint8Array|Buffer} buf the data de decode
181  * @return {String} the decoded string.
182  */
183 exports.utf8decode = function utf8decode(buf) {
184     if (support.nodebuffer) {
185         return utils.transformTo("nodebuffer", buf).toString("utf-8");
186     }
187
188     buf = utils.transformTo(support.uint8array ? "uint8array" : "array", buf);
189
190     // return buf2string(buf);
191     // Chrome prefers to work with "small" chunks of data
192     // for the method buf2string.
193     // Firefox and Chrome has their own shortcut, IE doesn't seem to really care.
194     var result = [], k = 0, len = buf.length, chunk = 65536;
195     while (k < len) {
196         var nextBoundary = utf8border(buf, Math.min(k + chunk, len));
197         if (support.uint8array) {
198             result.push(buf2string(buf.subarray(k, nextBoundary)));
199         } else {
200             result.push(buf2string(buf.slice(k, nextBoundary)));
201         }
202         k = nextBoundary;
203     }
204     return result.join("");
205
206 };
207 // vim: set shiftwidth=4 softtabstop=4: