pichunkstream support full uint

This commit is contained in:
2023-05-24 22:13:30 +03:00
parent 1de4304e30
commit 5e4c5b0d47
2 changed files with 24 additions and 20 deletions

View File

@@ -127,8 +127,8 @@ int PIChunkStream::peekVInt(Version version_, uchar * data_, int sz, uint & ret)
switch (version_) {
case Version_1: memcpy(&ret, data_, 4); return 4;
case Version_2: {
PIByteArray hdr(data_, piMini(4, sz));
hdr.resize(4);
PIByteArray hdr(data_, piMini(5, sz));
hdr.resize(5);
uchar hsz = 0;
ret = readVInt(hdr, &hsz);
return hsz;
@@ -222,15 +222,22 @@ uint PIChunkStream::readVInt(PIByteArray & s, uchar * bytes_cnt) {
uchar bytes[4];
uchar abc;
s >> bytes[0];
for (abc = 0; abc < 3; ++abc) {
uchar mask = (0x80 >> abc);
if ((bytes[0] & mask) == mask) {
bytes[0] &= (mask - 1);
s >> bytes[abc + 1];
} else
break;
if (bytes[0] == 0xf0) {
abc = 3;
for (int i = 0; i < 4; ++i)
s >> bytes[i];
if (bytes_cnt) *bytes_cnt = 5;
} else {
for (abc = 0; abc < 3; ++abc) {
uchar mask = (0x80 >> abc);
if ((bytes[0] & mask) == mask) {
bytes[0] &= (mask - 1);
s >> bytes[abc + 1];
} else
break;
}
if (bytes_cnt) *bytes_cnt = (abc + 1);
}
if (bytes_cnt) *bytes_cnt = (abc + 1);
uint ret = 0;
for (int i = 0; i <= abc; ++i) {
ret += (bytes[i] << (8 * ((int)abc - i)));
@@ -240,18 +247,15 @@ uint PIChunkStream::readVInt(PIByteArray & s, uchar * bytes_cnt) {
void PIChunkStream::writeVInt(PIByteArray & s, uint val) {
if (val > 0xfffffff) return;
if (val <= 0x7f) {
s << uchar(val);
return;
}
if (val <= 0x3fff) {
} else if (val <= 0x3fff) {
s << uchar((val >> 8) | 0x80) << uchar(val & 0xff);
return;
}
if (val <= 0x1fffff) {
} else if (val <= 0x1fffff) {
s << uchar((val >> 16) | 0xc0) << uchar((val >> 8) & 0xff) << uchar(val & 0xff);
return;
} else if (val <= 0xfffffff) {
s << uchar((val >> 24) | 0xe0) << uchar((val >> 16) & 0xff) << uchar((val >> 8) & 0xff) << uchar(val & 0xff);
} else {
s << uchar(0xf0) << uchar((val >> 24) & 0xff) << uchar((val >> 16) & 0xff) << uchar((val >> 8) & 0xff) << uchar(val & 0xff);
}
s << uchar((val >> 24) | 0xe0) << uchar((val >> 16) & 0xff) << uchar((val >> 8) & 0xff) << uchar(val & 0xff);
}