wbrobot
62 天前
php 是最好的语言
<?php
define('BASE36_ENCODE_TABLE_DEFAULT', array_merge(range('0', '9'), range('a', 'z')));
define('BASE36_DECODE_TABLE_DEFAULT', array_fill(0, 256, 0) + array_combine(range(ord('0'), ord('9')), range(0, 9)) + array_combine(range(ord('a'), ord('z')), range(10, 35)));
function base36_encode_block($plain, $encode_table) {
$lo = 0;
$hi = $plain[8];
for ($i = 0; $i < 8; $i++) {
$lo |= ($plain[$i] << ($i * 8));
}
$D = 36 * 36;
$remainder = $hi * (PHP_INT_MAX % $D + 1) + ($lo % $D);
$code = [];
for ($i = 0; $i < 2; $i++) {
$code[$i] = $encode_table[$remainder % 36];
$remainder = intdiv($remainder, 36);
}
$quotient = $hi * (PHP_INT_MAX / $D) + intdiv($lo, $D) + $remainder;
for ($i = 2; $i < 14; $i++) {
$code[$i] = $encode_table[$quotient % 36];
$quotient = intdiv($quotient, 36);
}
return $code;
}
function base36_decode_block($code, $decode_table) {
$lo = 0;
$hi = 0;
for ($i = 11; $i >= 0; $i--) {
$lo = $lo * 36 + $decode_table[$code[$i]];
}
for ($i = 13; $i >= 12; $i--) {
$hi = $hi * 36 + $decode_table[$code[$i]];
}
$plain = [];
$plain[0] = $lo;
$value = $hi * 18509302102818816 + intdiv($lo, 256);
for ($i = 1; $i < 9; $i++) {
$plain[$i] = ($value >> (($i - 1) * 8)) & 0xFF;
}
return $plain;
}
function base36_encode_last_block($plain, $len, $encode_table) {
$plain_tmp = array_merge(array_fill(0, 9, 0), $plain);
$code = base36_encode_block($plain_tmp, $encode_table);
$code[13] = $encode_table[27 + $len];
return $code;
}
function base36_decode_last_block($code, $decode_table) {
$flag = $decode_table[$code[13]];
if ($flag >= 28) {
$code_tmp = array_slice($code, 0, 13);
$plain_tmp = base36_decode_block($code_tmp, $decode_table);
$len = $flag - 27;
if ($len > 8) {
$len = 8;
}
return array_slice($plain_tmp, 0, $len);
}
return base36_decode_block($code, $decode_table);
}
function base36_encode($plain, $encode_table) {
$code = [];
$len = count($plain);
$src = 0;
$dst = 0;
while ($len >= 9) {
$code_part = base36_encode_block(array_slice($plain, $src, 9), $encode_table);
array_splice($code, $dst, 14, $code_part);
$src += 9;
$dst += 14;
$len -= 9;
}
if ($len > 0) {
$code_part = base36_encode_last_block(array_slice($plain, $src, $len), $len, $encode_table);
array_splice($code, $dst, 14, $code_part);
$dst += 14;
}
return array_slice($code, 0, $dst);
}
function base36_decode($code, $decode_table) {
$plain = [];
$len = count($code);
if ($len <= 0) {
return $plain;
}
$src_last = $len - 14;
$src = 0;
$dst = 0;
while ($src < $src_last) {
$plain_part = base36_decode_block(array_slice($code, $src, 14), $decode_table);
array_splice($plain, $dst, 9, $plain_part);
$src += 14;
$dst += 9;
}
$plain_part = base36_decode_last_block(array_slice($code, $src, 14), $decode_table);
array_splice($plain, $dst, count($plain_part), $plain_part);
return $plain;
}
// Example usage
$plain = array_fill(0, 9, 1);
$encoded = base36_encode($plain, BASE36_ENCODE_TABLE_DEFAULT);
$decoded = base36_decode($encoded, BASE36_DECODE_TABLE_DEFAULT);
print_r($encoded);
print_r($decoded);
?>