Created
July 13, 2018 12:25
-
-
Save mollyporph/205e970000cc9f223784e3f7f2c812b9 to your computer and use it in GitHub Desktop.
Base62.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import BitStream from './bit-stream' | |
const Base62CharSet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; | |
function encodeBase62ByteArrayToString(original: Array<number>) { | |
var sb = []; | |
var stream = new 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(Base62CharSet.charAt(61)); | |
stream.Seek(-1, 2); | |
} else if ((read[0] >> 3) == 0x1e) | |
{ | |
sb.push(Base62CharSet.charAt(60)); | |
stream.Seek(-1, 2); | |
} else | |
{ | |
sb.push(Base62CharSet.charAt((read[0] >> 2))); | |
} | |
} else if (length == 0) { | |
break; | |
} else { | |
sb.push(Base62CharSet.charAt((read[0] >> (8 - length)))); | |
break; | |
} | |
} | |
var str = sb.join(''); | |
return str; | |
}; | |
function encodeBase62(str: string) { | |
if (typeof str !== 'string' || str === '') { | |
return ''; | |
} | |
var bytes = []; | |
var len = str.length; | |
for (var i = 0; i < len; i++) { | |
bytes.push(str.charCodeAt(i)); | |
} | |
return encodeBase62ByteArrayToString(bytes); | |
}; | |
export default encodeBase62 | |
... | |
class BitStream { | |
Source: any; | |
Position: number; | |
constructor(source: Array<number>) { | |
this.Source = source | |
this.Position = 0; | |
} | |
Length() { | |
return this.Source.length * 8; | |
}; | |
Read (buffer: Array<number>, offset: number, count: number) { | |
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; | |
}; | |
Seek(offset: number, origin: number) { | |
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; | |
}; | |
Write(buffer: Array<number>, offset: number, count: number) { | |
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; | |
}; | |
}; | |
export default BitStream; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment