Skip to content

Commit 066bec3

Browse files
committed
Added base64decode to Tracker (snowplow#36)
1 parent b02625f commit 066bec3

2 files changed

Lines changed: 43 additions & 1 deletion

File tree

CHANGELOG

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Added retrospective tags back in (#22)
55
Restructured folders (#21)
66
Added setPlatform support, thanks @rcs! (#25)
77
Added currency field to ecommerce transactions (#34)
8-
Added base64decode to Tracker (#36) - TODO
8+
Added base64decode to Tracker (#36)
99
Added `value !== null` check to requestStringBuilder() (#40)
1010
Added custom unstructured contexts (#49) - TODO
1111
Added array helpers (#41) - TODO

src/js/lib/base64.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,45 @@ SnowPlow.base64urlencode = function(data) {
6565
.replace(/\+/g, '-')
6666
.replace(/\//g, '_');
6767
};
68+
69+
/*
70+
* Base64 decode data
71+
*/
72+
SnowPlow.base64decode = function(data) {
73+
var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
74+
var output = "";
75+
var chr1, chr2, chr3 = "";
76+
var enc1, enc2, enc3, enc4 = "";
77+
var i = 0;
78+
79+
// remove all characters that are not A-Z, a-z, 0-9, +, /, or =
80+
var base64test = /[^A-Za-z0-9\+\/\=]/g;
81+
82+
data = data.replace(/[^A-Za-z0-9\+\/\=]/g, "");
83+
84+
do {
85+
enc1 = keyStr.indexOf(data.charAt(i++));
86+
enc2 = keyStr.indexOf(data.charAt(i++));
87+
enc3 = keyStr.indexOf(data.charAt(i++));
88+
enc4 = keyStr.indexOf(data.charAt(i++));
89+
90+
chr1 = (enc1 << 2) | (enc2 >> 4);
91+
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
92+
chr3 = ((enc3 & 3) << 6) | enc4;
93+
94+
output = output + String.fromCharCode(chr1);
95+
96+
if (enc3 != 64) {
97+
output = output + String.fromCharCode(chr2);
98+
}
99+
100+
if (enc4 != 64) {
101+
output = output + String.fromCharCode(chr3);
102+
}
103+
104+
chr1 = chr2 = chr3 = "";
105+
enc1 = enc2 = enc3 = enc4 = "";
106+
} while (i < data.length);
107+
108+
return unescape(output);
109+
};

0 commit comments

Comments
 (0)