-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathSystem.Convert.debug.js
More file actions
277 lines (262 loc) · 10.3 KB
/
System.Convert.debug.js
File metadata and controls
277 lines (262 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
//=============================================================================
// Jocys.com JavaScript.NET Classes (In C# Object Oriented Style)
// Created by Evaldas Jocys <evaldas@jocys.com>
//=============================================================================
/// <reference path="System.debug.js" />
//=============================================================================
// Namespaces
//-----------------------------------------------------------------------------
// <PropertyGroup>
// <RootNamespace>System.Convert</RootNamespace>
// <PropertyGroup>
//-----------------------------------------------------------------------------
/// <reference path="System.debug.js" />
System.Convert = System.Convert ? System.Convert : {};
System.Type.RegisterNamespace("System.Convert");
//=============================================================================
// System.Base64FormattingOptions Enum
//-----------------------------------------------------------------------------
System.Base64FormattingOptions = function () {
/// <summary>Specifies whether relevant methods insert line breaks in their output.</summary>
/// <field name="None" type="Number" integer="true" static="true">Does not insert line breaks after every 76 characters in the string representation.</field>
/// <field name="InsertLineBreaks" type="Number" integer="true" static="true">Inserts line breaks after every 76 characters in the string representation.</field>
};
System.Base64FormattingOptions.prototype = {
None: 0,
InsertLineBreaks: 1
};
System.Type.RegisterEnum("System.Base64FormattingOptions");
//=============================================================================
System.Convert.Base64Array = function () {
/// <summary>
/// Array which makes base64 encoding and decoding faster.
/// </ summary>
// Declare string of available chars inside base64.
this.S = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
this.CA = [];
this.IA = [];
//---------------------------------------------------------
// INIT: Class
//---------------------------------------------------------
this.InitializeClass = function () {
var c = "";
for (var i = 0; i < this.S.length; i++) {
c = this.S.charAt(i);
this.CA[i] = c;
this.IA[c] = i;
}
};
this.InitializeClass();
};
System.Convert.ToBase64String = function (b, options) {
/// <summary>
/// Converts the value of an array of 8-bit unsigned integers to its equivalent
/// System.String representation encoded with base 64 digits.
/// </summary>
/// <param type="byte[]" name="b">An array of 8-bit unsigned integers.</param>
/// <param type="int" name="options">Specify: 1 - to insert a line break every 76 characters, 0 - to not insert line breaks.</param>
/// <returns type="string">
/// The System.String representation, in base 64, of the contents of inArray.
/// </returns>
/// <remarks>
/// A very fast and memory efficient class to encode and decode to and from BASE64
/// in full accordance with RFC 2045. Based on http://migbase64.sourceforge.net/
/// Converted to JavaScript by Evaldas Jocys [evaldas@jocys.com], https://www.jocys.com
/// </remarks>
var insertBreaks = options === System.Base64FormattingOptions.InsertLineBreaks || options === true;
var B64 = new System.Convert.Base64Array();
// Check special case
var bLen = b ? b.length : 0;
if (bLen === 0) return new Array(0);
// Length of even 24-bits.
var eLen = Math.floor(bLen / 3) * 3;
// Returned character count.
var cCnt = (bLen - 1) / 3 + 1 << 2;
var dLen = cCnt + (insertBreaks ? (cCnt - 1) / 76 << 1 : 0); // Length of returned array
var dArr = new Array(dLen);
// Encode even 24-bits.
for (var s = 0, d = 0, cc = 0; s < eLen;) {
// Copy next three bytes into lower 24 bits of int, paying attension to sign.
var i = (b[s++] & 0xff) << 16 | (b[s++] & 0xff) << 8 | b[s++] & 0xff;
// Encode the int into four chars.
dArr[d++] = B64.CA[i >>> 18 & 0x3f];
dArr[d++] = B64.CA[i >>> 12 & 0x3f];
dArr[d++] = B64.CA[i >>> 6 & 0x3f];
dArr[d++] = B64.CA[i & 0x3f];
// Add optional line separator as specified in RFC 2045.
if (insertBreaks && ++cc === 19 && d < dLen - 2) {
dArr[d++] = '\r';
dArr[d++] = '\n';
cc = 0;
}
}
// Pad and encode last bits if source isn't even 24 bits.
var left = bLen - eLen; // 0 - 2.
if (left > 0) {
// Prepare the int.
var j = (b[eLen] & 0xff) << 10 | (left === 2 ? (b[bLen - 1] & 0xff) << 2 : 0);
// Set last four chars.
dArr[dLen - 4] = B64.CA[j >> 12];
dArr[dLen - 3] = B64.CA[j >>> 6 & 0x3f];
dArr[dLen - 2] = left === 2 ? B64.CA[j & 0x3f] : '=';
dArr[dLen - 1] = '=';
}
return dArr.join("");
};
System.Convert.FromBase64String = function (s, fix) {
/// <summary>
/// Converts the specified System.String, which encodes binary data as base 64
/// digits, to an equivalent 8-bit unsigned integer array.
/// </summary>
/// <param type="string" name="s">A string.</param>
/// <param type="bool" name="fix">Fix base64 string by removing all ilegal chars.</param>
/// <returns type="byte[]">
/// An array of 8-bit unsigned integers equivalent to s.
/// </returns>
/// <remarks>
/// A very fast and memory efficient class to encode and decode to and from BASE64
/// in full accordance with RFC 2045. Based on http://migbase64.sourceforge.net/
/// Converted to JavaScript by Evaldas Jocys [evaldas@jocys.com], https://www.jocys.com
/// </remarks>
var B64 = new System.Convert.Base64Array();
// Check special case
if (fix) {
// Remove illegal chars
var regex = new RegExp("[^" + B64.S + "]", "g");
s = s.replace(regex, "");
}
var sLen = s.length;
if (sLen === 0) return new Array(0);
// Start and end index after trimming.
var sIx = 0, eIx = sLen - 1;
// Get the padding count (=) (0, 1 or 2).
var pad = s.charAt(eIx) === '=' ? s.charAt(eIx - 1) === '=' ? 2 : 1 : 0; // Count '=' at end.
// Content count including possible separators.
var cCnt = eIx - sIx + 1;
var sepLn = s.charAt(76) === '\r' ? cCnt / 78 : 0;
var sepCnt = sLen > 76 ? sepLn << 1 : 0;
// The number of decoded bytes.
var len = ((cCnt - sepCnt) * 6 >> 3) - pad;
// Preallocate byte[] of exact length.
var bytes = new Array(len);
// Decode all but the last 0 - 2 bytes.
var d = 0;
var eLen = Math.floor(len / 3) * 3;
var i;
for (var cc = 0; d < eLen;) {
// Assemble three bytes into an var from four "valid" characters.
i = B64.IA[s.charAt(sIx++)] << 18 |
B64.IA[s.charAt(sIx++)] << 12 |
B64.IA[s.charAt(sIx++)] << 6 |
B64.IA[s.charAt(sIx++)];
// Add the bytes
bytes[d++] = i >> 16;
bytes[d++] = (i & 0xFFFF) >> 8;
bytes[d++] = i & 0xFF;
// If line separator, jump over it.
if (sepCnt > 0 && ++cc === 19) {
sIx += 2;
cc = 0;
}
}
if (d < len) {
// Decode last 1-3 bytes (incl '=') into 1-3 bytes.
i = 0;
for (var j = 0; sIx <= eIx - pad; j++) {
i |= B64.IA[s.charAt(sIx++)] << 18 - j * 6;
}
for (var r = 16; d < len; r -= 8) {
var cropBits = Math.pow(2, r + 8) - 1;
bytes[d++] = (i & cropBits) >> r;
}
}
return bytes;
};
System.Convert.ToBase64UrlString = function (b, options) {
/// <summary>
/// Converts the value of an array of 8-bit unsigned integers to its equivalent
/// System.String representation encoded with Base64URL digits.
/// </summary>
/// <param type="byte[]" name="b">An array of 8-bit unsigned integers.</param>
/// <param type="int" name="options">Specify: 1 - to insert a line break every 76 characters, 0 - to not insert line breaks.</param>
/// <returns type="string">
/// The System.String representation, in Base64URL, of the contents of inArray.
/// </returns>
//
// Use standard base64 encoder.
var s = System.Convert.ToBase64String(b, options);
// Remove trailing '='.
s = s.replace(new RegExp("[=]+$", "g"), "");
// Replace base64 characters to be URL compatible.
s = s.replace(new RegExp("[+]", "g"), "-");
s = s.replace(new RegExp("[/]", "g"), "_");
return s;
};
System.Convert.FromBase64UrlString = function (s, fix) {
/// <summary>
/// Converts the specified System.String, which encodes binary data as Base64URL
/// digits, to an equivalent 8-bit unsigned integer array.
/// </summary>
/// <param type="string" name="s">A string.</param>
/// <param type="bool" name="fix">Fix base64 string by removing all ilegal chars.</param>
/// <returns type="byte[]">
//
// Restore base64 characters.
s = s.replace(new RegExp("[-]", "g"), "+");
s = s.replace(new RegExp("[_]", "g"), "/");
// Restore trailing '='.
var len = s.length % 4;
if (len === 2)
s += "==";
if (len === 3)
s += "=";
// Use standard base64 encoder.
var b = System.Convert.FromBase64String(s);
return b;
};
System.Type.RegisterNamespace("System.Web.HttpServerUtility");
System.Web.HttpServerUtility.UrlTokenDecode = System.Convert.FromBase64UrlString;
System.Web.HttpServerUtility.UrlTokenEncode = System.Convert.ToBase64UrlString;
System.Convert.HexStringToBytes = function (s) {
/// <summary>
/// Convert hex string to array of bytes.
/// </summary>
/// <param type="string" name="s">Hex string.</param>
/// <returns type="byte[]">
/// An array of 8-bit integers.
/// </returns>
// If hex prefix exists then...
if (s.indexOf("0x") === 0 || s.indexOf("0X") === 0) {
// Remove hex prefix.
s = s.substring(2);
}
// if not even length. Then add leading zero.
if (s.length % 2 === 1) s = "0" + s;
var bytes = [];
for (var i = 0; i < s.length; i += 2) {
bytes[i / 2] = parseInt(s.slice(i, i + 2), 16);
}
return bytes;
};
System.Convert.BytesToHexString = function (bytes, separator) {
/// <summary>
/// Array of bytes to hex string.
/// </summary>
/// <param type="byte[]" name="bytes">An array of 8-bit integers.</param>
/// <returns type="string">
/// Hex string.
/// </returns>
var sb = [];
var s = "";
if (!bytes) return;
for (var i = 0; i < bytes.length; i++) {
var b = bytes[i];
if (b <= 0xF) sb.push('0' + b.toString(16));
else sb.push(b.toString(16));
}
var sep = separator ? separator : "";
return sb.join(sep);
};
//==============================================================================
// END
//------------------------------------------------------------------------------