Skip to content

Instantly share code, notes, and snippets.

@sergeevabc
Created October 17, 2014 11:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sergeevabc/e9250a82802dd2713cde to your computer and use it in GitHub Desktop.
Save sergeevabc/e9250a82802dd2713cde to your computer and use it in GitHub Desktop.
Base62 encode, decode https://github.com/odan/base62js 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
if (!$d) {
var $d = {}
}
$d.BitStream = function BitStream(options) {
this.Source = [];
if (typeof options === 'object') {
this.Source = options
}
if (typeof options === 'number') {
var dim = Math.floor(options);
this.Source = new Array(dim)
}
this.Position = 0;
this.Length = function() {
return this.Source.length * 8
};
this.Read = function(buffer, offset, count) {
var tempPos = this.Position;
tempPos += offset;
var readPosCount = 0;
var readPosMod = 0;
var posCount = tempPos >> 3;
var posMod = (tempPos - ((tempPos >> 3) << 3));
while (tempPos < this.Position + offset + count && tempPos < this.Length()) {
if (((this.Source[posCount]) & (0x1 << (7 - posMod))) != 0) {
buffer[readPosCount] = ((buffer[readPosCount]) | (0x1 << (7 - readPosMod)))
} else {
buffer[readPosCount] = ((buffer[readPosCount]) & (0xffffffff - (0x1 << (7 - readPosMod))))
}
tempPos++;
if (posMod == 7) {
posMod = 0;
posCount++
} else {
posMod++
} if (readPosMod == 7) {
readPosMod = 0;
readPosCount++
} else {
readPosMod++
}
}
var bits = (tempPos - this.Position - offset);
this.Position = tempPos;
return bits
};
this.Seek = function(offset, origin) {
switch (origin) {
case (1):
{
this.Position = offset;
break
}
case (2):
{
this.Position += offset;
break
}
case (3):
{
this.Position = this.Length() + offset;
break
}
}
return this.Position
};
this.Write = function(buffer, offset, count) {
var tempPos = this.Position;
var readPosCount = offset >> 3,
readPosMod = offset - ((offset >> 3) << 3);
var posCount = tempPos >> 3;
var posMod = (tempPos - ((tempPos >> 3) << 3));
while (tempPos < this.Position + count && tempPos < this.Length()) {
if (((buffer[readPosCount]) & (0x1 << (7 - readPosMod))) != 0) {
this.Source[posCount] = ((this.Source[posCount]) | (0x1 << (7 - posMod)))
} else {
this.Source[posCount] = ((this.Source[posCount]) & (0xffffffff - (0x1 << (7 - posMod))))
}
tempPos++;
if (posMod == 7) {
posMod = 0;
posCount++
} else {
posMod++
} if (readPosMod == 7) {
readPosMod = 0;
readPosCount++
} else {
readPosMod++
}
}
this.Position = tempPos
}
};
$d.Base62CodingSpace = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
$d.encodeBase62ToString = function encodeBase62ToString(original) {
var sb = [];
var stream = new $d.BitStream(original);
var read = [];
read.push(0);
while (true) {
read[0] = 0;
var length = stream.Read(read, 0, 6);
if (length == 6) {
if ((read[0] >> 3) == 0x1f) {
sb.push($d.Base62CodingSpace.charAt(61));
stream.Seek(-1, 2)
} else if ((read[0] >> 3) == 0x1e) {
sb.push($d.Base62CodingSpace.charAt(60));
stream.Seek(-1, 2)
} else {
sb.push($d.Base62CodingSpace.charAt((read[0] >> 2)))
}
} else {
sb.push($d.Base62CodingSpace.charAt((read[0] >> (8 - length))));
break
}
}
var str = sb.join('');
return str
};
$d.decodeBase62ToArray = function decodeBase62ToArray(base62) {
var count = 0;
var stream = new $d.BitStream(base62.length * 6 / 8);
var len = base62.length;
for (var i = 0; i < len; i++) {
var c = base62.charAt(i);
var index = $d.Base62CodingSpace.indexOf(c);
if (count == base62.length - 1) {
var mod = (stream.Position % 8);
stream.Write([(index << (mod))], 0, 8 - mod)
} else {
if (index == 60) {
stream.Write([0xf0], 0, 5)
} else if (index == 61) {
stream.Write([0xf8], 0, 5)
} else {
stream.Write([index], 2, 6)
}
}
count++
}
var result = new Array(stream.Position / 8);
stream.Seek(0, 1);
stream.Read(result, 0, result.length * 8);
return result
};
$d.encodeBase62 = function encodeBase62(str) {
if (typeof str !== 'string' || str === '') {
return ''
}
str = str.toString();
var bytes = [];
var len = str.length;
for (var i = 0; i < len; i++) {
bytes.push(str.charCodeAt(i))
}
var strReturn = this.encodeBase62ToString(bytes);
return strReturn
};
$d.decodeBase62 = function decodeBase62(str) {
if (typeof str !== 'string' || str === '') {
return ''
}
str = str.toString();
var bytes = this.decodeBase62ToArray(str);
var sb = [];
var len = bytes.length;
for (var i = 0; i < len; i++) {
sb.push(String.fromCharCode(bytes[i]))
}
str = sb.join('');
return str
};
(function($) {
$.encodeBase62 = function encodeBase62(str) {
return $d.encodeBase62(str)
};
$.decodeBase62 = function decodeBase62(str) {
return $d.decodeBase62(str)
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment