Skip to content

Instantly share code, notes, and snippets.

@zerobias
Created December 27, 2019 10:17
Show Gist options
  • Save zerobias/50a34aecc58204162cc05ee935cb636f to your computer and use it in GitHub Desktop.
Save zerobias/50a34aecc58204162cc05ee935cb636f to your computer and use it in GitHub Desktop.
decompress effector repl sources
const keyStrUriSafe =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-$'
const baseReverseDic = {}
const charAt = (str, i) => str.charAt(i)
for (let i = 0; i < keyStrUriSafe.length; i++) {
baseReverseDic[charAt(keyStrUriSafe, i)] = i
}
console.log(decompress(localStorage.getItem('code-compressed')))
function decompress(input) {
if (input == null) return ''
if (input == '') return null
input = input.replace(/ /g, '+')
const dictionary = [0, 1, 2]
let enlargeIn = 4
let dictSize = 4
let numBits = 3
let entry = ''
const result = []
let w
let bits = 0
let resb
let maxpower = 2 ** 2
let power = 1
let c
let dataVal = baseReverseDic[charAt(input, 0)]
let dataPosition = 32
let dataIndex = 1
const updateData = () => {
while (power != maxpower) {
resb = dataVal & dataPosition
dataPosition >>= 1
if (dataPosition == 0) {
dataPosition = 32
dataVal = baseReverseDic[charAt(input, dataIndex++)]
}
bits |= (resb > 0 ? 1 : 0) * power
power <<= 1
}
}
const updateDataPower = exp => {
bits = 0
maxpower = 2 ** exp
power = 1
updateData()
return String.fromCharCode(bits)
}
updateData()
switch (bits) {
case 0:
c = updateDataPower(8)
break
case 1:
c = updateDataPower(16)
break
case 2:
return ''
}
dictionary[3] = c
w = c
result.push(c)
while (true) {
if (dataIndex > input.length) {
return ''
}
updateDataPower(numBits)
switch ((c = bits)) {
case 0:
dictionary[dictSize++] = updateDataPower(8)
c = dictSize - 1
enlargeIn--
break
case 1:
dictionary[dictSize++] = updateDataPower(16)
c = dictSize - 1
enlargeIn--
break
case 2:
return result.join('')
}
if (enlargeIn == 0) {
enlargeIn = 2 ** numBits
numBits++
}
if (dictionary[c]) {
entry = dictionary[c]
} else {
if (c === dictSize) {
//$off
entry = w + charAt(w, 0)
} else {
return null
}
}
result.push(entry)
// Add w+entry[0] to the dictionary.
//$off
dictionary[dictSize++] = w + charAt(entry, 0)
enlargeIn--
w = entry
if (enlargeIn == 0) {
enlargeIn = 2 ** numBits
numBits++
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment