CODE 项目整体迁移至码云平台(https://gitee.com)公告
冬瓜猫 / protobuf_tools C++

因为工作需要分析protobuf的数据,google了一下,我个人没有找到合适的,就参考google protobuf文档,写了一个分析工具。这个项目用的是C++Builder XE10.2, win64实现。

c++builder protobuf
拷贝地址

项目最近一次提交:3 个月 前 982bc8506

4_zdhsoft 冬瓜猫 Merge branch 'master' of code.csdn.net:zdhsoft/protobuf_tools 2017-08-19 21:29:23
文件 565行 12.0 KB
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
// ---------------------------------------------------------------------------

#ifndef protoitemH
#define protoitemH

#include <fstream>
#include <vector>
#include <set>
#include <vcl.h>

namespace zdh {
typedef unsigned char XByte;
typedef __int64 XInt64;
typedef unsigned __int64 XUInt64;
typedef double XDouble;
typedef __int32 XInt32;
typedef unsigned __int32 XUInt32;


// ---------------------------------------------------------------------------
enum EnumWireType {
EWT_Varint = 0,
EWT_Fix64 = 1,
EWT_LengthLimit = 2,
EWT_StartGroup = 3,
EWT_EndGroup = 4,
EWT_Fix32 = 5,
};


//¾ßÌåµÄÊý¾ÝÀàÐÍ
enum EnumItemType {
EIT_UNKNOW = 0,
EIT_INT32 = 1,
EIT_INT64 = 2,
EIT_UINT32 = 3,
EIT_UINT64 = 4,
EIT_STRING = 5,
EIT_DATA = 6,
EIT_BINARY = 7,
EIT_DOUBLE = 8,
EIT_FLOAT = 9,
EIT_HEX64 = 10,
EIT_HEX32 = 11,
};

inline void GetWireTypeItemTypeSet(int paramWireType, std::set<int> & paramSet) {
paramSet.clear();
switch(paramWireType) {
case EWT_Varint:
paramSet.insert(EIT_INT32);
paramSet.insert(EIT_INT64);
paramSet.insert(EIT_UINT32);
paramSet.insert(EIT_UINT64);
paramSet.insert(EIT_HEX64);
paramSet.insert(EIT_HEX32);
break;
case EWT_Fix64:
paramSet.insert(EIT_INT64);
paramSet.insert(EIT_UINT64);
paramSet.insert(EIT_HEX64);
paramSet.insert(EIT_DOUBLE);
break;
case EWT_LengthLimit:
paramSet.insert(EIT_STRING);
paramSet.insert(EIT_DATA);
paramSet.insert(EIT_BINARY);
break;
case EWT_StartGroup:
break;
case EWT_EndGroup:
break;
case EWT_Fix32:
paramSet.insert(EIT_INT32);
paramSet.insert(EIT_UINT32);
paramSet.insert(EIT_HEX32);
paramSet.insert(EIT_FLOAT);
break;
}
}


class XByteBuffer {
public:
XByteBuffer() : m_Length(0), m_DataSize(0), m_Data(nullptr) {
}

~XByteBuffer() {
ToEmpty();
}

void ToEmpty() {
if (m_Data != nullptr) {
delete[]m_Data;
}
m_Length = 0;
m_DataSize = 0;
m_Data = nullptr;
}

void expandCapacity(int paramMini) {
if (m_DataSize >= paramMini)
return;
if (paramMini <= 0)
return;
XByte * p = new XByte[paramMini];
if (p == nullptr) {
throw "out of memory!";
}
if (m_Length > 0) {
copyData(m_Data, p, m_Length);
}
if (m_Data != nullptr) {
delete[]m_Data;
}
m_Data = p;
m_DataSize = paramMini;
}

void ensureCapacity(int paramCapacity, bool paramRemainData = true) {
if (!paramRemainData)
m_Length = 0;
expandCapacity(paramCapacity);
}

void setData(const XByte * paramData, int paramSize, int paramIndex = 0)
{
if (paramData == nullptr)
return;
ensureCapacity(paramSize, false);
if (paramSize <= 0)
return;
m_Length = paramSize;
copyData(paramData + paramIndex, m_Data, paramSize);
}

void setData(const XByteBuffer & paramBuffer, int paramCopySize,
int paramStartIndex = 0) {
if (this == &paramBuffer) {
XByteBuffer tmp;
tmp.setData(*this, paramStartIndex, paramCopySize);
swap(*this, tmp);
}
else {
setData(paramBuffer.m_Data, paramCopySize, paramStartIndex);
}
}

void setData(const XByteBuffer & paramBuffer) {
setData(paramBuffer, paramBuffer.m_Length, 0);
}

void append(const XByte * paramData, int paramSize, int paramIndex = 0) {
if(paramData == nullptr || paramSize <= 0 || paramIndex < 0) return;
ensureCapacity(m_Length + paramSize);
copyData(paramData + paramIndex, m_Data + m_Length,paramSize);
m_Length += paramSize;
}

int getLength() const {
return m_Length;
}

int setLength(int paramLength) {
ensureCapacity(paramLength);
if (paramLength < 0)
m_Length = 0;
else
m_Length = paramLength;
return m_Length;
}

int getDataSize() const {
return m_DataSize;
}

void Clear() {
m_Length = 0;
}

const XByte * getData() const {
return m_Data;
}

XByte & operator[](int paramIndex) {
return m_Data[paramIndex];
}

XByte operator[](int paramIndex) const {
return m_Data[paramIndex];
}

private:
void copyData(const XByte * paramSrc, XByte * paramDest,
int paramCopySize) {
for (int i = 0; i < paramCopySize; i++) {
*paramDest++ = *paramSrc++;
}
}

void swap(XByteBuffer & param1, XByteBuffer & param2) {
int t1 = param1.m_Length;
int t2 = param1.m_DataSize;
XByte * d = param1.m_Data;

param1.m_Length = param2.m_Length;
param1.m_DataSize = param2.m_DataSize;
param1.m_Data = param2.m_Data;

param2.m_Length = t1;
param2.m_DataSize = t2;
param2.m_Data = d;
}

private:
int m_Length;
int m_DataSize;
XByte * m_Data;
};




class XUtils {
public:
static int LoadFile(std::string paramFileName, XByteBuffer & paramData) {
std::ifstream f;
f.open(paramFileName, f.binary);
f.seekg (0, f.end);
int length = f.tellg();
f.seekg (0, f.beg);
paramData.ensureCapacity(length, false);
XByte tmp[128];
while(!f.eof()){
f.read((char *)tmp, 128);
int iReadCount = f.gcount();
if(iReadCount > 0) paramData.append(tmp, iReadCount);
if(iReadCount < 128) break;
}
f.close();
return 0;
}
static void CopyData(const XByte * paramSrc, XByte * paramDest, int paramCopySize, bool paramFlag = true){
if(paramCopySize <= 1) return;
if(paramFlag) {
for(int i = 0; i < paramCopySize; i++) {
*paramDest ++ = * paramSrc++;
}
}
else {
paramSrc += (paramCopySize-1);
for(int i = 0;i < paramCopySize; i++) {
* paramDest++ = * paramSrc--;
}
}
}
};


union XFix64 {
XInt64 Value64;
double ValueDouble;
};

union XFix32 {
XInt32 Value32;
float ValueFloat;
};

class ProtoItem {
public:
ProtoItem() {
Reset();
}
ProtoItem(const ProtoItem & item) {
m_FieldNumber = item.m_FieldNumber;
m_WireType = item.m_WireType;
m_SIntValue = item.m_SIntValue;
m_StringValue = item.m_StringValue;
m_Fix64.Value64 = item.m_Fix64.Value64;
m_Fix32.Value32 = item.m_Fix32.Value32;
m_EIT = item.m_EIT;
m_ID = item.m_ID;
m_Data.setData(item.m_Data);
SubList = item.SubList;
}

void Reset() {
m_EIT = EIT_UNKNOW;
m_FieldNumber = 0;
m_WireType = 0;
m_SIntValue = 0;
m_StringValue = 0;
m_Fix64.Value64 = 0;
m_Fix32.Value32 = 0;
m_ID = 0;
m_Data.Clear();
SubList.clear();
}

ProtoItem * GetItemByID(int paramID) {
for(auto iter = SubList.begin(); iter != SubList.end(); ++iter) {
if(iter->m_ID == paramID) {
return &(*iter);
}
ProtoItem * pFind = iter->GetItemByID(paramID);
if(pFind != nullptr) {
return pFind;
}
}
return nullptr;
}
UTF8String ToString() {
UTF8String s;
switch(m_WireType) {
case EWT_Varint:
ToStringByVarint(s, m_SIntValue, m_EIT);
break;
case EWT_Fix64:
ToStringByFix64(s,m_Fix64,m_EIT);
break;
case EWT_LengthLimit:
ToStringByLengthLimit(s, m_Data, m_EIT);
break;
case EWT_StartGroup:
break;
case EWT_EndGroup:
break;
case EWT_Fix32:
ToStringByFix32(s,m_Fix32,m_EIT);
break;
}
return IntToStr(m_FieldNumber) + u"," + IntToStr(m_WireType) + u"]" + s;
}
public:
void DecodeSubList();
private:
void ToStringByLengthLimit(UTF8String & s, XByteBuffer & buff, int paramEIT);
void ToStringByVarint(UTF8String & s, XInt64 v, int paramEIT) {
switch(paramEIT) {
case EIT_INT32:
case EIT_INT64:
s = IntToStr(v);
break;
case EIT_UINT32:
case EIT_UINT64:
s = UIntToStr(XUInt64(v));
break;
case EIT_HEX32:
s = u"0x"+IntToHex(XUInt64(XUInt32(v)), 8);
break;
case EIT_HEX64:
s = u"0x"+IntToHex(XUInt64(v), 16);
break;
default:
s = IntToStr(m_SIntValue) + u":" + UIntToStr(XUInt64(m_SIntValue))+u":"+IntToHex(m_SIntValue,16);
}
}

void ToStringByFix64(UTF8String & s, const XFix64 & v, int paramEIT) {
switch(paramEIT) {
case EIT_INT64:
s = IntToStr(v.Value64);
break;
case EIT_UINT64:
s = UIntToStr(XUInt64(v.Value64));
break;
case EIT_HEX64:
s = u"0x"+IntToHex(XUInt64(v.Value64), 16);
break;
case EIT_DOUBLE:
s = FloatToStr(v.ValueDouble);
break;
default:
s = IntToStr(v.Value64) + u":" + FloatToStr(v.ValueDouble);
}
}
void ToStringByFix32(UTF8String & s, const XFix32 & v, int paramEIT) {
switch(paramEIT) {
case EIT_INT32:
s = IntToStr(v.Value32);
break;
case EIT_UINT32:
s = UIntToStr(XUInt32(v.Value32));
break;
case EIT_HEX32:
s = u"0x"+IntToHex(XUInt64(XUInt32(v.Value32)), 8);
break;
case EIT_FLOAT:
s = FloatToStr(v.ValueFloat);
break;
default:
s = IntToStr(v.Value32) + u":" + FloatToStr(v.ValueFloat);
}
}


public:
int m_ID = 0;
int m_EIT = EIT_UNKNOW;
int m_FieldNumber = 0;
int m_WireType = 0;

XInt64 m_SIntValue;
UTF8String m_StringValue;
XFix64 m_Fix64;
XFix32 m_Fix32;
XByteBuffer m_Data;
std::vector<ProtoItem> SubList;
};

class XBufferReader {
public:
XBufferReader() {
}
bool isEof() const {
if(m_Buff == nullptr || m_Pos >= m_Buff->getLength()) return true;
else return false;
}
void SetBuff(XByteBuffer * paramBuffer) {
m_Buff = paramBuffer;
}

void SetPos(int paramPos) {
m_Pos = paramPos;
}

bool ReadByte(XByte & paramValue) {
if(isEof()) return false;
paramValue = (*m_Buff)[m_Pos++];
return true;
}

bool ReadData(XByteBuffer & paramData, int iCnt) {
if(isEof()) return false;
int iLastPos = m_Pos + iCnt;
if(iLastPos > m_Buff->getLength()) return false;
paramData.Clear();
paramData.setData(*m_Buff, iCnt, m_Pos);
m_Pos = iLastPos;
return true;
}

bool ReadFix64(XFix64 paramValue) {
if(isEof()) return false;
int iLastPos = m_Pos + 8;
if(iLastPos > m_Buff->getLength()) return false;
XUtils::CopyData(m_Buff->getData() + m_Pos, (XByte *)&paramValue.Value64, 8);
m_Pos += 8;
return true;
}

bool ReadFix32(XFix32 paramValue) {
if(isEof()) return false;
int iLastPos = m_Pos + 4;
if(iLastPos > m_Buff->getLength()) return false;
XUtils::CopyData(m_Buff->getData() + m_Pos, (XByte *)&paramValue.Value32, 4);
m_Pos += 4;
return true;
}

void PosInc() {
m_Pos++;
}

void PosDec() {
m_Pos--;
}

private:
XByteBuffer *m_Buff = nullptr;
int m_Pos = 0;
};

class ProtoReader {
public:
static int m_ID;
public:

ProtoReader() {
}
void Read(XByteBuffer & paramBuffer, ProtoItem & paramItem) {
//paramItem.Reset();
ProtoItem stItem;
XBufferReader r;
r.SetBuff(&paramBuffer);
r.SetPos(0);
XByte btFlag = 0;
int iFieldNum = 0;
int iWireType = 0;
XInt64 iTmp = 0;
XInt64 nTag = 0;
bool bRun = true;
while(bRun) {

//if(!r.ReadByte(btFlag)) break;
if(!ReadVarint(r, nTag)) break;
stItem.Reset();
iWireType = (int)(nTag & 0x7);
iFieldNum = int((nTag >> 3) & 0x1f);
stItem.m_FieldNumber = iFieldNum;
stItem.m_WireType = iWireType;
switch(iWireType) {
case EWT_Varint:
if(!ReadVarint(r, stItem.m_SIntValue)) {
bRun = false;
}
break;
case EWT_Fix64:
if(!r.ReadFix64(stItem.m_Fix64)) {
bRun = false;
}
break;

case EWT_LengthLimit:
if(!ReadVarint(r, iTmp)) {
bRun = false;
}
if(iTmp > 0) {
if(!r.ReadData(stItem.m_Data, (int)iTmp)) {
bRun = false;
}
}
break;

case EWT_StartGroup:
break;

case EWT_EndGroup:
break;

case EWT_Fix32:
if(!r.ReadFix32(stItem.m_Fix32)) {
bRun = false;
}
break;
}
if(bRun) {
stItem.m_ID = m_ID++;
paramItem.SubList.push_back(stItem);
}
}
}
private:
bool ReadVarint(XBufferReader & r, XInt64 & v) {
XInt64 nTmp;
XByte bt;
v = 0;
for(int i = 0; i < 16; i++) {
if(!r.ReadByte(bt)) return false;
nTmp = bt;
v = v | (nTmp & 0x7f) << (7*i);
if((bt & 0x80) == 0) {
break;
}
}
return true;
}
public:
int m_Pos = 0;
int m_FieldNumber = 1;
};
}
#endif