|
| 1 | +-------------------------------------------------------------------------------- |
| 2 | +-- https://github.com/aiq/basexx/releases/tag/v0.4.1 |
| 3 | +-- util functions |
| 4 | +-------------------------------------------------------------------------------- |
| 5 | + |
| 6 | +local function divide_string( str, max ) |
| 7 | + local result = {} |
| 8 | + |
| 9 | + local start = 1 |
| 10 | + for i = 1, #str do |
| 11 | + if i % max == 0 then |
| 12 | + table.insert( result, str:sub( start, i ) ) |
| 13 | + start = i + 1 |
| 14 | + elseif i == #str then |
| 15 | + table.insert( result, str:sub( start, i ) ) |
| 16 | + end |
| 17 | + end |
| 18 | + |
| 19 | + return result |
| 20 | +end |
| 21 | + |
| 22 | +local function number_to_bit( num, length ) |
| 23 | + local bits = {} |
| 24 | + |
| 25 | + while num > 0 do |
| 26 | + local rest = math.floor( math.fmod( num, 2 ) ) |
| 27 | + table.insert( bits, rest ) |
| 28 | + num = ( num - rest ) / 2 |
| 29 | + end |
| 30 | + |
| 31 | + while #bits < length do |
| 32 | + table.insert( bits, "0" ) |
| 33 | + end |
| 34 | + |
| 35 | + return string.reverse( table.concat( bits ) ) |
| 36 | +end |
| 37 | + |
| 38 | +local function ignore_set( str, set ) |
| 39 | + if set then |
| 40 | + str = str:gsub( "["..set.."]", "" ) |
| 41 | + end |
| 42 | + return str |
| 43 | +end |
| 44 | + |
| 45 | +local function pure_from_bit( str ) |
| 46 | + return ( str:gsub( '........', function ( cc ) |
| 47 | + return string.char( tonumber( cc, 2 ) ) |
| 48 | + end ) ) |
| 49 | +end |
| 50 | + |
| 51 | +local function unexpected_char_error( str, pos ) |
| 52 | + local c = string.sub( str, pos, pos ) |
| 53 | + return string.format( "unexpected character at position %d: '%s'", pos, c ) |
| 54 | +end |
| 55 | + |
| 56 | +-------------------------------------------------------------------------------- |
| 57 | + |
| 58 | +local basexx = {} |
| 59 | + |
| 60 | +-------------------------------------------------------------------------------- |
| 61 | +-- base2(bitfield) decode and encode function |
| 62 | +-------------------------------------------------------------------------------- |
| 63 | + |
| 64 | +local bitMap = { o = "0", i = "1", l = "1" } |
| 65 | + |
| 66 | +function basexx.from_bit( str, ignore ) |
| 67 | + str = ignore_set( str, ignore ) |
| 68 | + str = string.lower( str ) |
| 69 | + str = str:gsub( '[ilo]', function( c ) return bitMap[ c ] end ) |
| 70 | + local pos = string.find( str, "[^01]" ) |
| 71 | + if pos then return nil, unexpected_char_error( str, pos ) end |
| 72 | + |
| 73 | + return pure_from_bit( str ) |
| 74 | +end |
| 75 | + |
| 76 | +function basexx.to_bit( str ) |
| 77 | + return ( str:gsub( '.', function ( c ) |
| 78 | + local byte = string.byte( c ) |
| 79 | + local bits = {} |
| 80 | + for _ = 1,8 do |
| 81 | + table.insert( bits, byte % 2 ) |
| 82 | + byte = math.floor( byte / 2 ) |
| 83 | + end |
| 84 | + return table.concat( bits ):reverse() |
| 85 | + end ) ) |
| 86 | +end |
| 87 | + |
| 88 | +-------------------------------------------------------------------------------- |
| 89 | +-- base16(hex) decode and encode function |
| 90 | +-------------------------------------------------------------------------------- |
| 91 | + |
| 92 | +function basexx.from_hex( str, ignore ) |
| 93 | + str = ignore_set( str, ignore ) |
| 94 | + local pos = string.find( str, "[^%x]" ) |
| 95 | + if pos then return nil, unexpected_char_error( str, pos ) end |
| 96 | + |
| 97 | + return ( str:gsub( '..', function ( cc ) |
| 98 | + return string.char( tonumber( cc, 16 ) ) |
| 99 | + end ) ) |
| 100 | +end |
| 101 | + |
| 102 | +function basexx.to_hex( str ) |
| 103 | + return ( str:gsub( '.', function ( c ) |
| 104 | + return string.format('%02X', string.byte( c ) ) |
| 105 | + end ) ) |
| 106 | +end |
| 107 | + |
| 108 | +-------------------------------------------------------------------------------- |
| 109 | +-- generic function to decode and encode base32/base64 |
| 110 | +-------------------------------------------------------------------------------- |
| 111 | + |
| 112 | +local function from_basexx( str, alphabet, bits ) |
| 113 | + local result = {} |
| 114 | + for i = 1, #str do |
| 115 | + local c = string.sub( str, i, i ) |
| 116 | + if c ~= '=' then |
| 117 | + local index = string.find( alphabet, c, 1, true ) |
| 118 | + if not index then |
| 119 | + return nil, unexpected_char_error( str, i ) |
| 120 | + end |
| 121 | + table.insert( result, number_to_bit( index - 1, bits ) ) |
| 122 | + end |
| 123 | + end |
| 124 | + |
| 125 | + local value = table.concat( result ) |
| 126 | + local pad = #value % 8 |
| 127 | + return pure_from_bit( string.sub( value, 1, #value - pad ) ) |
| 128 | +end |
| 129 | + |
| 130 | +local function to_basexx( str, alphabet, bits, pad ) |
| 131 | + local bitString = basexx.to_bit( str ) |
| 132 | + |
| 133 | + local chunks = divide_string( bitString, bits ) |
| 134 | + local result = {} |
| 135 | + for _,value in ipairs( chunks ) do |
| 136 | + if ( #value < bits ) then |
| 137 | + value = value .. string.rep( '0', bits - #value ) |
| 138 | + end |
| 139 | + local pos = tonumber( value, 2 ) + 1 |
| 140 | + table.insert( result, alphabet:sub( pos, pos ) ) |
| 141 | + end |
| 142 | + |
| 143 | + table.insert( result, pad ) |
| 144 | + return table.concat( result ) |
| 145 | +end |
| 146 | + |
| 147 | +-------------------------------------------------------------------------------- |
| 148 | +-- rfc 3548: http://www.rfc-editor.org/rfc/rfc3548.txt |
| 149 | +-------------------------------------------------------------------------------- |
| 150 | + |
| 151 | +local base32Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567" |
| 152 | +local base32PadMap = { "", "======", "====", "===", "=" } |
| 153 | + |
| 154 | +function basexx.from_base32( str, ignore ) |
| 155 | + str = ignore_set( str, ignore ) |
| 156 | + return from_basexx( string.upper( str ), base32Alphabet, 5 ) |
| 157 | +end |
| 158 | + |
| 159 | +function basexx.to_base32( str ) |
| 160 | + return to_basexx( str, base32Alphabet, 5, base32PadMap[ #str % 5 + 1 ] ) |
| 161 | +end |
| 162 | + |
| 163 | +-------------------------------------------------------------------------------- |
| 164 | +-- crockford: http://www.crockford.com/wrmg/base32.html |
| 165 | +-------------------------------------------------------------------------------- |
| 166 | + |
| 167 | +local crockfordAlphabet = "0123456789ABCDEFGHJKMNPQRSTVWXYZ" |
| 168 | +local crockfordMap = { O = "0", I = "1", L = "1" } |
| 169 | + |
| 170 | +function basexx.from_crockford( str, ignore ) |
| 171 | + str = ignore_set( str, ignore ) |
| 172 | + str = string.upper( str ) |
| 173 | + str = str:gsub( '[ILOU]', function( c ) return crockfordMap[ c ] end ) |
| 174 | + return from_basexx( str, crockfordAlphabet, 5 ) |
| 175 | +end |
| 176 | + |
| 177 | +function basexx.to_crockford( str ) |
| 178 | + return to_basexx( str, crockfordAlphabet, 5, "" ) |
| 179 | +end |
| 180 | + |
| 181 | +-------------------------------------------------------------------------------- |
| 182 | +-- base64 decode and encode function |
| 183 | +-------------------------------------------------------------------------------- |
| 184 | + |
| 185 | +local base64Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".. |
| 186 | + "abcdefghijklmnopqrstuvwxyz".. |
| 187 | + "0123456789+/" |
| 188 | +local base64PadMap = { "", "==", "=" } |
| 189 | + |
| 190 | +function basexx.from_base64( str, ignore ) |
| 191 | + str = ignore_set( str, ignore ) |
| 192 | + return from_basexx( str, base64Alphabet, 6 ) |
| 193 | +end |
| 194 | + |
| 195 | +function basexx.to_base64( str ) |
| 196 | + return to_basexx( str, base64Alphabet, 6, base64PadMap[ #str % 3 + 1 ] ) |
| 197 | +end |
| 198 | + |
| 199 | +-------------------------------------------------------------------------------- |
| 200 | +-- URL safe base64 decode and encode function |
| 201 | +-------------------------------------------------------------------------------- |
| 202 | + |
| 203 | +local url64Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".. |
| 204 | + "abcdefghijklmnopqrstuvwxyz".. |
| 205 | + "0123456789-_" |
| 206 | + |
| 207 | +function basexx.from_url64( str, ignore ) |
| 208 | + str = ignore_set( str, ignore ) |
| 209 | + return from_basexx( str, url64Alphabet, 6 ) |
| 210 | +end |
| 211 | + |
| 212 | +function basexx.to_url64( str ) |
| 213 | + return to_basexx( str, url64Alphabet, 6, "" ) |
| 214 | +end |
| 215 | + |
| 216 | +-------------------------------------------------------------------------------- |
| 217 | +-- |
| 218 | +-------------------------------------------------------------------------------- |
| 219 | + |
| 220 | +local function length_error( len, d ) |
| 221 | + return string.format( "invalid length: %d - must be a multiple of %d", len, d ) |
| 222 | +end |
| 223 | + |
| 224 | +local z85Decoder = { 0x00, 0x44, 0x00, 0x54, 0x53, 0x52, 0x48, 0x00, |
| 225 | + 0x4B, 0x4C, 0x46, 0x41, 0x00, 0x3F, 0x3E, 0x45, |
| 226 | + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 227 | + 0x08, 0x09, 0x40, 0x00, 0x49, 0x42, 0x4A, 0x47, |
| 228 | + 0x51, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, |
| 229 | + 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, |
| 230 | + 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, |
| 231 | + 0x3B, 0x3C, 0x3D, 0x4D, 0x00, 0x4E, 0x43, 0x00, |
| 232 | + 0x00, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, |
| 233 | + 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, |
| 234 | + 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, |
| 235 | + 0x21, 0x22, 0x23, 0x4F, 0x00, 0x50, 0x00, 0x00 } |
| 236 | + |
| 237 | +function basexx.from_z85( str, ignore ) |
| 238 | + str = ignore_set( str, ignore ) |
| 239 | + if ( #str % 5 ) ~= 0 then |
| 240 | + return nil, length_error( #str, 5 ) |
| 241 | + end |
| 242 | + |
| 243 | + local result = {} |
| 244 | + |
| 245 | + local value = 0 |
| 246 | + for i = 1, #str do |
| 247 | + local index = string.byte( str, i ) - 31 |
| 248 | + if index < 1 or index >= #z85Decoder then |
| 249 | + return nil, unexpected_char_error( str, i ) |
| 250 | + end |
| 251 | + value = ( value * 85 ) + z85Decoder[ index ] |
| 252 | + if ( i % 5 ) == 0 then |
| 253 | + local divisor = 256 * 256 * 256 |
| 254 | + while divisor ~= 0 do |
| 255 | + local b = math.floor( value / divisor ) % 256 |
| 256 | + table.insert( result, string.char( b ) ) |
| 257 | + divisor = math.floor( divisor / 256 ) |
| 258 | + end |
| 259 | + value = 0 |
| 260 | + end |
| 261 | + end |
| 262 | + |
| 263 | + return table.concat( result ) |
| 264 | +end |
| 265 | + |
| 266 | +local z85Encoder = "0123456789".. |
| 267 | + "abcdefghijklmnopqrstuvwxyz".. |
| 268 | + "ABCDEFGHIJKLMNOPQRSTUVWXYZ".. |
| 269 | + ".-:+=^!/*?&<>()[]{}@%$#" |
| 270 | + |
| 271 | +function basexx.to_z85( str ) |
| 272 | + if ( #str % 4 ) ~= 0 then |
| 273 | + return nil, length_error( #str, 4 ) |
| 274 | + end |
| 275 | + |
| 276 | + local result = {} |
| 277 | + |
| 278 | + local value = 0 |
| 279 | + for i = 1, #str do |
| 280 | + local b = string.byte( str, i ) |
| 281 | + value = ( value * 256 ) + b |
| 282 | + if ( i % 4 ) == 0 then |
| 283 | + local divisor = 85 * 85 * 85 * 85 |
| 284 | + while divisor ~= 0 do |
| 285 | + local index = ( math.floor( value / divisor ) % 85 ) + 1 |
| 286 | + table.insert( result, z85Encoder:sub( index, index ) ) |
| 287 | + divisor = math.floor( divisor / 85 ) |
| 288 | + end |
| 289 | + value = 0 |
| 290 | + end |
| 291 | + end |
| 292 | + |
| 293 | + return table.concat( result ) |
| 294 | +end |
| 295 | + |
| 296 | +-------------------------------------------------------------------------------- |
| 297 | + |
| 298 | +return basexx |
0 commit comments