wbi、av2bv、bv2av 的 c++ 实现 (#1035)

* + wbi 添加 c++ demo

* + bv<-->av 算法 c++ 实现

* + App API 签名的 C++ 实现
This commit is contained in:
YuHuanTin
2024-06-13 10:26:03 +08:00
committed by GitHub
parent 11d42851ae
commit e7ab2d770b
3 changed files with 256 additions and 3 deletions

View File

@@ -12,7 +12,7 @@
### 格式
“bvid”恒为长度为 12 的字符串,前个固定为“BV1”后 9 个为 base58 计算结果(不包含数字 `0` 和大写字母 `I``O` 以及小写字母 `l`
“bvid”恒为长度为 12 的字符串,前 3 个固定为“BV1”后 9 个为 base58 计算结果(不包含数字 `0` 和大写字母 `I``O` 以及小写字母 `l`
### 实质
@@ -328,6 +328,64 @@ public class AVBVConverter {
```
### C++
```c++
#include <algorithm>
#include <cassert>
#include <print>
#include <string>
constexpr int64_t XOR_CODE = 0x1552356C4CDB;
constexpr int64_t MAX_AID = 0x8000000000000;
constexpr int64_t MASK_CODE = MAX_AID - 1;
constexpr int64_t BASE = 58;
constexpr char Table[BASE + 1] = "FcwAPNKTMug3GV5Lj7EJnHpWsx4tb8haYeviqBz6rkCy12mUSDQX9RdoZf";
constexpr char ReverseTable[128] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x2c, 0x2d, 0x0b, 0x1a, 0x0e, 0x27, 0x11, 0x1d, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x03, 0x25, 0x2a, 0x31, 0x12, 0x00, 0x0c, 0x15, 0x00, 0x13, 0x06, 0x0f, 0x08, 0x05, 0x00,
0x04, 0x32, 0x35, 0x30, 0x07, 0x2f, 0x0d, 0x17, 0x33, 0x20, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x1f, 0x1c, 0x01, 0x36, 0x21, 0x39, 0x0a, 0x1e, 0x23, 0x10, 0x29, 0x00, 0x2e, 0x14, 0x37,
0x16, 0x24, 0x28, 0x18, 0x1b, 0x09, 0x22, 0x02, 0x19, 0x2b, 0x26, 0x00, 0x00, 0x00, 0x00, 0x00
};
std::string Av2bv(const int64_t Avid) {
assert(Avid > 0 && "Avid must be greater than 0");
std::string bv = "BV1";
bv.resize(12, '\0');
int64_t tmp = (Avid | MAX_AID) ^ XOR_CODE;
for (size_t i = bv.size() - 1; tmp > 0 && i > 2; --i) {
bv[i] = Table[tmp % BASE];
tmp /= BASE;
}
std::ranges::swap(bv.at(3), bv.at(9));
std::ranges::swap(bv.at(4), bv.at(7));
return bv;
}
int64_t Bv2av(const std::string &Bvid) {
assert(Bvid.starts_with("BV1") && "Bvid must start with 'BV1'");
auto Bvid_ = Bvid;
std::ranges::swap(Bvid_.at(3), Bvid_.at(9));
std::ranges::swap(Bvid_.at(4), Bvid_.at(7));
int64_t tmp = 0;
for (int i = 3; i < Bvid_.size(); ++i) {
tmp = ReverseTable[Bvid_.at(i)] + BASE * tmp;
}
return (tmp & MASK_CODE) ^ XOR_CODE;
}
int main() {
assert(Av2bv(1004871019) == "BV16x4y1H7M1");
assert(Bv2av("BV16x4y1H7M1") == 1004871019);
}
```
## 老版算法存档