main
Last change
on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago |
Update repo after prototype presentation
|
-
Property mode
set to
100644
|
File size:
1.5 KB
|
Line | |
---|
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 |
|
---|
8 | var intToCharMap = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.split('');
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * Encode an integer in the range of 0 to 63 to a single base 64 digit.
|
---|
12 | */
|
---|
13 | exports.encode = function (number) {
|
---|
14 | if (0 <= number && number < intToCharMap.length) {
|
---|
15 | return intToCharMap[number];
|
---|
16 | }
|
---|
17 | throw new TypeError("Must be between 0 and 63: " + number);
|
---|
18 | };
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Decode a single base 64 character code digit to an integer. Returns -1 on
|
---|
22 | * failure.
|
---|
23 | */
|
---|
24 | exports.decode = function (charCode) {
|
---|
25 | var bigA = 65; // 'A'
|
---|
26 | var bigZ = 90; // 'Z'
|
---|
27 |
|
---|
28 | var littleA = 97; // 'a'
|
---|
29 | var littleZ = 122; // 'z'
|
---|
30 |
|
---|
31 | var zero = 48; // '0'
|
---|
32 | var nine = 57; // '9'
|
---|
33 |
|
---|
34 | var plus = 43; // '+'
|
---|
35 | var slash = 47; // '/'
|
---|
36 |
|
---|
37 | var littleOffset = 26;
|
---|
38 | var numberOffset = 52;
|
---|
39 |
|
---|
40 | // 0 - 25: ABCDEFGHIJKLMNOPQRSTUVWXYZ
|
---|
41 | if (bigA <= charCode && charCode <= bigZ) {
|
---|
42 | return (charCode - bigA);
|
---|
43 | }
|
---|
44 |
|
---|
45 | // 26 - 51: abcdefghijklmnopqrstuvwxyz
|
---|
46 | if (littleA <= charCode && charCode <= littleZ) {
|
---|
47 | return (charCode - littleA + littleOffset);
|
---|
48 | }
|
---|
49 |
|
---|
50 | // 52 - 61: 0123456789
|
---|
51 | if (zero <= charCode && charCode <= nine) {
|
---|
52 | return (charCode - zero + numberOffset);
|
---|
53 | }
|
---|
54 |
|
---|
55 | // 62: +
|
---|
56 | if (charCode == plus) {
|
---|
57 | return 62;
|
---|
58 | }
|
---|
59 |
|
---|
60 | // 63: /
|
---|
61 | if (charCode == slash) {
|
---|
62 | return 63;
|
---|
63 | }
|
---|
64 |
|
---|
65 | // Invalid base64 digit.
|
---|
66 | return -1;
|
---|
67 | };
|
---|
Note:
See
TracBrowser
for help on using the repository browser.