PageSourceSearch

https://guscost.github.io/spacefrack/js/vendor/base64-binary.js

js guscost.github.io collected 2026-10-03 09:16:11 UTC 3,113 bytes, 88 lines download raw bytes

1/*
2Copyright (c) 2011, Daniel Guerrero
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are met:
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 copyright
10      notice, this list of conditions and the following disclaimer in the
11      documentation and/or other materials provided with the distribution.
12
13THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16DISCLAIMED. IN NO EVENT SHALL DANIEL GUERRERO BE LIABLE FOR ANY
17DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25/**
26 * Uses the new array typed in javascript to binary base64 encode/decode
27 * at the moment just decodes a binary base64 encoded
28 * into either an ArrayBuffer (decodeArrayBuffer)
29 * or into an Uint8Array (decode)
30 * 
31 * References:
32 * https://developer.mozilla.org/en/JavaScript_typed_arrays/ArrayBuffer
33 * https://developer.mozilla.org/en/JavaScript_typed_arrays/Uint8Array
34 */
35
36var Base64Binary = {
37	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
38
39	/* will return a  Uint8Array type */
40	decodeArrayBuffer: function(input) {
41		var bytes = (input.length/4) * 3;
42		var ab = new ArrayBuffer(bytes);
43		this.decode(input, ab);
44
45		return ab;
46	},
47
48	decode: function(input, arrayBuffer) {
49		//get last chars to see if are valid
50		var lkey1 = this._keyStr.indexOf(input.charAt(input.length-1));		 
51		var lkey2 = this._keyStr.indexOf(input.charAt(input.length-2));		 
52
53		var bytes = (input.length/4) * 3;
54		if (lkey1 == 64) bytes--; //padding chars, so skip
55		if (lkey2 == 64) bytes--; //padding chars, so skip
56
57		var uarray;
58		var chr1, chr2, chr3;
59		var enc1, enc2, enc3, enc4;
60		var i = 0;
61		var j = 0;
62
63		if (arrayBuffer)
64			uarray = new Uint8Array(arrayBuffer);
65		else
66			uarray = new Uint8Array(bytes);
67
68		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
69
70		for (i=0; i<bytes; i+=3) {	
71			//get the 3 octects in 4 ascii chars
72			enc1 = this._keyStr.indexOf(input.charAt(j++));
73			enc2 = this._keyStr.indexOf(input.charAt(j++));
74			enc3 = this._keyStr.indexOf(input.charAt(j++));
75			enc4 = this._keyStr.indexOf(input.charAt(j++));
76
77			chr1 = (enc1 << 2) | (enc2 >> 4);
78			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
79			chr3 = ((enc3 & 3) << 6) | enc4;
80
81			uarray[i] = chr1;			
82			if (enc3 != 64) uarray[i+1] = chr2;
83			if (enc4 != 64) uarray[i+2] = chr3;
84		}
85
86		return uarray;	
87	}
88}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.