【问题标题】:How to decode a specific byte string of varints in PHP如何在 PHP 中解码 varints 的特定字节字符串
【发布时间】:2020-11-10 11:41:53
【问题描述】:

我正在尝试使用 PHP 以特定格式(炉石牌组代码)解码字符串,如下所示:

AAEBAc2xAgjAAe0E7QX3DdYRh6wC8fsCoIADC8kDqwTLBPsMhRDH0wKW6AK0/ALNiQPXiQOfmwMA

或

AAEBAf0GBAD6DoyAA6CAAw37AZwCigbJB/gHlA+CEIUQrRDy0AL2/QKJgAPRgAMA

规范(original description)是:

数据串是一个base64编码的字节串。

除非另有说明,否则后面的每个值都是一个整数,编码为unsigned varint。

  1. 标题块

    • 保留字节 0x00
    • 版本(1)
    • 格式
  2. 数据块
    数据块被分成四对长度+数组,顺序如下:

    • 英雄
    • 单张卡片
    • 2 份卡片
    • n-copy 卡片

每对都有一个前导 varint 指定数组中的项目数。对于前三个块,它们是varints 的数组。对于最后一个块,它是一对varints 的数组。这种结构的目标是使数据串尽可能紧凑。

我已经开始整理一些东西,但在处理原始字节方面我还是个新手。我的代码是:

    // I found this to decode Variable-length quantity (varint)
    function vlq_decode(array $bytes) {
        $result = [];
        $integer = 0;
        foreach ($bytes as $byte) {
            if ($integer > 0xfffffff - 0x7f) {
                throw new OverflowException('The value exceeds the maximum allowed.');
            }
            $integer <<= 7;
            $integer |= 0x7f & $byte;

            if (($byte & 0x80) === 0) {
                $result[] = $integer;
                $integer = 0;
            }
        }
        if (($byte & 0x80) !== 0) {
            throw new InvalidArgumentException('Incomplete byte sequence.');
        }
        return $result;
    }

    $datastring = 'AAEBAc2xAgjAAe0E7QX3DdYRh6wC8fsCoIADC8kDqwTLBPsMhRDH0wKW6AK0/ALNiQPXiQOfmwMA';

    $raw = base64_decode($datastring);

    $byte_array = unpack('C*', $raw);

    $result = vlq_decode($byte_array);

    print_r($result);

我唯一确定的是base64_decode。我不知道unpack 参数是否正确,或者vlq_decode 函数是否按预期工作,因为不是我自己写的。

original site 上有 Python 和 Javascript 的参考实现,但它们超出了我的想象,我无法使用代码来实现我的工作。

更新:

该代码确实生成了一个array,看起来与我的预期相似,但许多值似乎不正确。我认为来自varint 的转换仍然有些偏离。

// this is the $result I get (wrong)
Array (
    [0] => 0 // this is always 0
    [1] => 1 // Version
    [2] => 1 // Format
    [3] => 1 // What follows is an array of length 1 (data block Heroes)
    [4] => 1267842
    [5] => 8 // What follows is an array of length 8 (data block single-copy cards)
    [6] => 8193
    [7] => 13956
    [8] => 13957
    [9] => 15245
    [10] => 11025
    [11] => 120322
    [12] => 1867138
    [13] => 524291
    [14] => 11 // What follows is an array of length 11 (data block 2-copy cards)
    [15] => 9347
    [16] => 5508
    [17] => 9604
    [18] => 15756
    [19] => 656
    [20] => 1173890
    [21] => 373762
    [22] => 867842
    [23] => 1262723
    [24] => 1426563
    [25] => 511363
    [26] => 0  // What follows is an array of length 0 (data block n-copy cards)
)

Python 实现 (Gist) 生成不同的数字,格式略有不同,与包含 ID 数据的 the database 非常匹配(在 dbfId 字段中)

// this is the expected (correct) $result
Array (
    [0] => 0
    [1] => 1
    [2] => 1
    [3] => 1
    [4] => 39117
    [5] => 8
    [6] => 192 
    [7] => 621 
    [8] => 749 
    [9] => 1783 
    [10] => 2262 
    [11] => 38407 
    [12] => 48625 
    [13] => 49184 
    [14] => 11
    [15] => 457 
    [16] => 555 
    [17] => 587 
    [18] => 1659 
    [19] => 2053 
    [20] => 43463 
    [21] => 46102 
    [22] => 48692 
    [23] => 50381 
    [24] => 50391 
    [25] => 52639
    [26] => 0
)

感谢任何帮助!

已经有一个question about this topic,但它写得很糟糕,没有代码示例,所以我再试一次。

【问题讨论】:

  • 继续阅读规范。您的代码有效,前两个值正是它在盒子上所说的,第三个您从引用的枚举代码中查找 [1 表示“野生],有一个数组长度 [1],然后是 1 项,一个数组长度[8] 然后是 8 个项目,等等。
  • @Sammitch 结果起初看起来是正确的,但数字与预期的输出不匹配。如果您想再看一下,我已将两者都添加到我的问题中。我相信varint 转换在某些时候仍然会出错。
  • 可能是字节序问题

标签: php decoding varint


【解决方案1】:

这是一个endian 问题,也就是您需要以相反的顺序从每个 varint 字节中推送位。线索是低于 128 的值可以顺利通过。

以下是说明性的 hack,不应在实际代码中使用:

str_split(decbin(1267842),7)

产量:

array(3) {
  [0]=>
  string(7) "1001101"
  [1]=>
  string(7) "0110001"
  [2]=>
  string(7) "0000010"
}

超级方便,它已经是 7 位的倍数,但可能也是字节序问题的症状。

反转、内爆、转换回来:

bindec(implode('', array_reverse(str_split(decbin(1267842),7))))

产量:

int(39117)

我重新调整了该函数以解决这个问题:

function vlq_decode(array $bytes, $swap_endian=false) {
    $result = [];
    $segments = [];
    foreach ($bytes as $byte) {
        if( $swap_endian ) {
            array_unshift($segments, 0x7f & $byte);
        } else {
            $segments[] = ( 0x7f & $byte );
        }

        if (($byte & 0x80) === 0) {
            $integer = 0;
            foreach($segments as $segment) {
                $integer <<= 7;
                $integer |= ( 0x7f & $segment );
            }
            $result[] = $integer;
            $segments = [];
        }
    }
    if (($byte & 0x80) !== 0) {
        throw new InvalidArgumentException('Incomplete byte sequence.');
    }
    return $result;
}

然后vlq_decode($byte_array, true); 会得到你想要的。

我删减了双层溢出代码,因为它永远不会真正检测到实际溢出代码,而且还会使您陷入 32 位整数。如果您确实想在解码过程中检测溢出,则需要计算正在解包的位,这只是一个麻烦:P

【讨论】:

  • 天才!做到了。
猜你喜欢
  • 2020-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多