| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988 |
- {******************************************************************************}
- { }
- { Library: Fundamentals 4.00 }
- { File name: cProtoBufUtils.pas }
- { File version: 0.03 }
- { Description: Protocol Buffer utilities. }
- { }
- { Copyright: Copyright (c) 2012-2013, David J Butler }
- { All rights reserved. }
- { This file is licensed under the BSD License. }
- { See http://www.opensource.org/licenses/bsd-license.php }
- { Redistribution and use in source and binary forms, with }
- { or without modification, are permitted provided that }
- { the following conditions are met: }
- { Redistributions of source code must retain the above }
- { copyright notice, this list of conditions and the }
- { following disclaimer. }
- { THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND }
- { CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED }
- { WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED }
- { WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A }
- { PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL }
- { THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, }
- { INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR }
- { CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, }
- { PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF }
- { USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) }
- { HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER }
- { IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING }
- { NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE }
- { USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE }
- { POSSIBILITY OF SUCH DAMAGE. }
- { }
- { Home page: http://fundementals.sourceforge.net }
- { Forum: http://sourceforge.net/forum/forum.php?forum_id=2117 }
- { E-mail: fundamentals.library@gmail.com }
- { }
- { Revision history: }
- { }
- { 2012/04/14 0.01 VarInt encoding and decoding }
- { 2012/04/15 0.02 SInt32 encoding and decoding }
- { 2012/04/25 0.03 Improvements }
- { }
- {******************************************************************************}
- {$INCLUDE cProtoBuf.inc}
- unit cProtoBufUtils;
- interface
- uses
- { System }
- SysUtils,
- { Fundamentals }
- cUtils;
- { SInt32 / SInt64 }
- function pbSInt32ToUInt32(const A: LongInt): LongWord;
- function pbUInt32ToSInt32(const A: LongWord): LongInt;
- function pbSInt64ToUInt64(const A: Int64): UInt64;
- function pbUInt64ToSInt64(const A: UInt64): Int64;
- { VarInt }
- const
- pbMaxVarIntSizeEnc = 10;
- type
- EpbVarIntError = class(Exception);
- TpbVarInt = record
- EncSize : Byte;
- EncData : array[0..pbMaxVarIntSizeEnc - 1] of Byte;
- end;
- function pbVarIntEquals(const A, B: TpbVarInt): Boolean;
- function pbVarIntIsZero(var A: TpbVarInt): Boolean;
- procedure pbVarIntInitZero(var A: TpbVarInt);
- procedure pbVarIntInitVarInt(var A: TpbVarInt; const B: TpbVarInt);
- function pbVarIntInitEncBuf(var A: TpbVarInt; const Buf; const BufSize: Integer): Integer;
- procedure pbVarIntInitBinBuf(var A: TpbVarInt; const Buf; const BufSize: Integer);
- procedure pbVarIntInitUInt32(var A: TpbVarInt; const B: LongWord);
- procedure pbVarIntInitSInt32(var A: TpbVarInt; const B: LongInt);
- procedure pbVarIntInitUInt64(var A: TpbVarInt; const B: UInt64);
- procedure pbVarIntInitInt64(var A: TpbVarInt; const B: Int64);
- procedure pbVarIntInitSInt64(var A: TpbVarInt; const B: Int64);
- function pbVarIntToEncBuf(const A: TpbVarInt; var Buf; const BufSize: Integer): Integer;
- procedure pbVarIntToBinBuf(const A: TpbVarInt; var Buf; const BufSize: Integer);
- function pbVarIntToUInt32(const A: TpbVarInt): LongWord;
- function pbVarIntToSInt32(const A: TpbVarInt): LongInt;
- function pbVarIntToUInt64(const A: TpbVarInt): UInt64;
- function pbVarIntToInt64(const A: TpbVarInt): Int64;
- function pbVarIntToSInt64(const A: TpbVarInt): Int64;
- { Wire type }
- type
- TpbWireType = (
- pwtVarInt = 0,
- pwt64Bit = 1,
- pwtVarBytes = 2,
- pwtStartGroup = 3, // deprecated
- pwtEndGroup = 4, // deprecated
- pwt32Bit = 5,
- pwtNotUsed6 = 6,
- pwtNotUsed7 = 7
- );
- { Field key }
- function pbFieldKeyToUInt32(const FieldNumber: LongInt; const WireType: TpbWireType): LongWord;
- procedure pbUInt32ToFieldKey(const A: LongWord; var FieldNumber: LongInt; var WireType: TpbWireType);
- { Encode }
- type
- EpbEncodeError = class(Exception);
- function pbEncodeValueVarInt(var Buf; const BufSize: Integer; const Value: TpbVarInt): Integer;
- function pbEncodeValueInt32(var Buf; const BufSize: Integer; const Value: LongInt): Integer;
- function pbEncodeValueInt64(var Buf; const BufSize: Integer; const Value: Int64): Integer;
- function pbEncodeValueUInt32(var Buf; const BufSize: Integer; const Value: LongWord): Integer;
- function pbEncodeValueUInt64(var Buf; const BufSize: Integer; const Value: UInt64): Integer;
- function pbEncodeValueSInt32(var Buf; const BufSize: Integer; const Value: LongInt): Integer;
- function pbEncodeValueSInt64(var Buf; const BufSize: Integer; const Value: Int64): Integer;
- function pbEncodeValueBool(var Buf; const BufSize: Integer; const Value: Boolean): Integer;
- function pbEncodeValueDouble(var Buf; const BufSize: Integer; const Value: Double): Integer;
- function pbEncodeValueFloat(var Buf; const BufSize: Integer; const Value: Single): Integer;
- function pbEncodeValueFixed32(var Buf; const BufSize: Integer; const Value: LongWord): Integer;
- function pbEncodeValueFixed64(var Buf; const BufSize: Integer; const Value: UInt64): Integer;
- function pbEncodeValueSFixed32(var Buf; const BufSize: Integer; const Value: LongInt): Integer;
- function pbEncodeValueSFixed64(var Buf; const BufSize: Integer; const Value: Int64): Integer;
- function pbEncodeValueString(var Buf; const BufSize: Integer; const Value: AnsiString): Integer;
- function pbEncodeValueBytes(var Buf; const BufSize: Integer; const ValueBuf; const ValueBufSize: Integer): Integer;
- function pbEncodeFieldKey(var Buf; const BufSize: Integer; const FieldNumber: LongInt; const WireType: TpbWireType): Integer;
- function pbEncodeFieldVarBytesHdr(var Buf; const BufSize: Integer; const FieldNumber: LongInt; const VarBytesSize: Integer): Integer;
- function pbEncodeFieldVarInt(var Buf; const BufSize: Integer; const FieldNumber: LongInt; const Value: TpbVarInt): Integer;
- function pbEncodeFieldInt32(var Buf; const BufSize: Integer; const FieldNumber: LongInt; const Value: LongInt): Integer;
- function pbEncodeFieldInt64(var Buf; const BufSize: Integer; const FieldNumber: LongInt; const Value: Int64): Integer;
- function pbEncodeFieldUInt32(var Buf; const BufSize: Integer; const FieldNumber: LongInt; const Value: LongWord): Integer;
- function pbEncodeFieldUInt64(var Buf; const BufSize: Integer; const FieldNumber: LongInt; const Value: UInt64): Integer;
- function pbEncodeFieldSInt32(var Buf; const BufSize: Integer; const FieldNumber: LongInt; const Value: LongInt): Integer;
- function pbEncodeFieldSInt64(var Buf; const BufSize: Integer; const FieldNumber: LongInt; const Value: Int64): Integer;
- function pbEncodeFieldBool(var Buf; const BufSize: Integer; const FieldNumber: LongInt; const Value: Boolean): Integer;
- function pbEncodeFieldDouble(var Buf; const BufSize: Integer; const FieldNumber: LongInt; const Value: Double): Integer;
- function pbEncodeFieldFloat(var Buf; const BufSize: Integer; const FieldNumber: LongInt; const Value: Single): Integer;
- function pbEncodeFieldFixed32(var Buf; const BufSize: Integer; const FieldNumber: LongInt; const Value: LongWord): Integer;
- function pbEncodeFieldFixed64(var Buf; const BufSize: Integer; const FieldNumber: LongInt; const Value: UInt64): Integer;
- function pbEncodeFieldSFixed32(var Buf; const BufSize: Integer; const FieldNumber: LongInt; const Value: LongInt): Integer;
- function pbEncodeFieldSFixed64(var Buf; const BufSize: Integer; const FieldNumber: LongInt; const Value: Int64): Integer;
- function pbEncodeFieldString(var Buf; const BufSize: Integer; const FieldNumber: LongInt; const Value: AnsiString): Integer;
- function pbEncodeFieldBytes(var Buf; const BufSize: Integer; const FieldNumber: LongInt; const ValueBuf; const ValueBufSize: Integer): Integer;
- { Decode }
- type
- EpbDecodeError = class(Exception);
- function pbDecodeValueInt32(const Buf; const BufSize: Integer; var Value: LongInt): Integer;
- function pbDecodeValueInt64(const Buf; const BufSize: Integer; var Value: Int64): Integer;
- function pbDecodeValueUInt32(const Buf; const BufSize: Integer; var Value: LongWord): Integer;
- function pbDecodeValueUInt64(const Buf; const BufSize: Integer; var Value: UInt64): Integer;
- function pbDecodeValueSInt32(const Buf; const BufSize: Integer; var Value: LongInt): Integer;
- function pbDecodeValueSInt64(const Buf; const BufSize: Integer; var Value: Int64): Integer;
- function pbDecodeValueDouble(const Buf; const BufSize: Integer; var Value: Double): Integer;
- function pbDecodeValueFloat(const Buf; const BufSize: Integer; var Value: Single): Integer;
- function pbDecodeValueFixed32(const Buf; const BufSize: Integer; var Value: LongWord): Integer;
- function pbDecodeValueFixed64(const Buf; const BufSize: Integer; var Value: UInt64): Integer;
- function pbDecodeValueSFixed32(const Buf; const BufSize: Integer; var Value: LongInt): Integer;
- function pbDecodeValueSFixed64(const Buf; const BufSize: Integer; var Value: Int64): Integer;
- function pbDecodeValueBool(const Buf; const BufSize: Integer; var Value: Boolean): Integer;
- function pbDecodeValueString(const Buf; const BufSize: Integer; var Value: AnsiString): Integer;
- function pbDecodeValueBytes(const Buf; const BufSize: Integer; var ValueBuf; const ValueBufSize: Integer): Integer;
- type
- TpbProtoBufDecodeField = record
- FieldNum : LongInt;
- WireType : TpbWireType;
- WireDataPtr : Pointer;
- WireDataSize : LongInt;
- ValueVarInt : TpbVarInt;
- ValueVarBytesPtr : Pointer;
- ValueVarBytesLen : LongInt;
- end;
- PpbProtoBufDecodeField = ^TpbProtoBufDecodeField;
- TpbProtoBufDecodeFieldCallbackProc =
- procedure (const Field: TpbProtoBufDecodeField;
- const Data: Pointer);
- procedure pbDecodeProtoBuf(
- const Buf; const BufSize: Integer;
- const CallbackProc: TpbProtoBufDecodeFieldCallbackProc;
- const CallbackData: Pointer);
- procedure pbDecodeFieldInt32(const Field: TpbProtoBufDecodeField; var Value: LongInt);
- procedure pbDecodeFieldInt64(const Field: TpbProtoBufDecodeField; var Value: Int64);
- procedure pbDecodeFieldUInt32(const Field: TpbProtoBufDecodeField; var Value: LongWord);
- procedure pbDecodeFieldUInt64(const Field: TpbProtoBufDecodeField; var Value: UInt64);
- procedure pbDecodeFieldSInt32(const Field: TpbProtoBufDecodeField; var Value: LongInt);
- procedure pbDecodeFieldSInt64(const Field: TpbProtoBufDecodeField; var Value: Int64);
- procedure pbDecodeFieldDouble(const Field: TpbProtoBufDecodeField; var Value: Double);
- procedure pbDecodeFieldFloat(const Field: TpbProtoBufDecodeField; var Value: Single);
- procedure pbDecodeFieldFixed32(const Field: TpbProtoBufDecodeField; var Value: LongWord);
- procedure pbDecodeFieldFixed64(const Field: TpbProtoBufDecodeField; var Value: UInt64);
- procedure pbDecodeFieldSFixed32(const Field: TpbProtoBufDecodeField; var Value: LongInt);
- procedure pbDecodeFieldSFixed64(const Field: TpbProtoBufDecodeField; var Value: Int64);
- procedure pbDecodeFieldBool(const Field: TpbProtoBufDecodeField; var Value: Boolean);
- function pbDecodeFieldString(const Field: TpbProtoBufDecodeField; var Value: AnsiString): Integer;
- function pbDecodeFieldBytes(const Field: TpbProtoBufDecodeField; var ValueBuf; const ValueBufSize: Integer): Integer;
- { }
- { Test cases }
- { }
- {$IFDEF PROTOBUF_SELFTEST}
- procedure SelfTest;
- {$ENDIF}
- implementation
- { Strings }
- const
- SErr_BufferTooSmall = 'Buffer too small';
- SErr_Overflow = 'Overflow';
- SErr_InvalidVarInt = 'Invalid VarInt';
- SErr_InvalidBuffer = 'Invalid buffer';
- SErr_InvalidWireType = 'Invalid wire type';
- { SInt32 }
- // returns SInt32 encoded to LongWord using 'ZigZag' encoding
- function pbSInt32ToUInt32(const A: LongInt): LongWord;
- var I : Int64;
- begin
- if A < 0 then
- begin
- // use Int64 value to negate A without overflow
- I := A;
- I := -I;
- // encode ZigZag
- Result := (LongWord(I) - 1) * 2 + 1
- end
- else
- Result := LongWord(A) * 2;
- end;
- // returns SInt32 decoded from LongWord using 'ZigZag' encoding
- function pbUInt32ToSInt32(const A: LongWord): LongInt;
- begin
- if A and 1 <> 0 then
- Result := -(A div 2) - 1
- else
- Result := A div 2;
- end;
- // returns SInt64 encoded to UInt64 using 'ZigZag' encoding
- function pbSInt64ToUInt64(const A: Int64): UInt64;
- var I : UInt64;
- begin
- if A < 0 then
- begin
- // use two's complement to negate A without overflow
- I := not A;
- Inc(I);
- // encode ZigZag
- Dec(I);
- I := I * 2;
- Inc(I);
- Result := I;
- end
- else
- Result := UInt64(A) * 2;
- end;
- // returns SInt64 decoded from UInt64 using 'ZigZag' encoding
- function pbUInt64ToSInt64(const A: UInt64): Int64;
- var I : Int64;
- begin
- if A and 1 <> 0 then
- begin
- I := A shr 1;
- I := -I;
- Dec(I);
- Result := I;
- end
- else
- begin
- I := A shr 1;
- Result := I;
- end;
- end;
- { VarInt }
- // returns true if two VarInts have equal values
- function pbVarIntEquals(const A, B: TpbVarInt): Boolean;
- var I, L : Integer;
- begin
- L := A.EncSize;
- Result := L = B.EncSize;
- if not Result then
- exit;
- for I := 0 to L - 1 do
- if A.EncData[I] <> B.EncData[I] then
- begin
- Result := False;
- exit;
- end;
- end;
- function pbVarIntIsZero(var A: TpbVarInt): Boolean;
- begin
- Result :=
- (A.EncSize = 1) and
- (A.EncData[0] = 0);
- end;
- // initialises VarInt to zero
- procedure pbVarIntInitZero(var A: TpbVarInt);
- begin
- A.EncSize := 1;
- A.EncData[0] := 0;
- end;
- // initialises VarInt from another VarInt
- procedure pbVarIntInitVarInt(var A: TpbVarInt; const B: TpbVarInt);
- begin
- A.EncSize := B.EncSize;
- A.EncData := B.EncData;
- end;
- // initialises VarInt from a VarInt encoded buffer
- // encoded buffer may be larger than the VarInt encoded in it
- // returns the size of the VarInt in the encoded buffer
- function pbVarIntInitEncBuf(var A: TpbVarInt; const Buf; const BufSize: Integer): Integer;
- var L, I : Integer;
- P : PByte;
- C : Byte;
- begin
- L := BufSize;
- if L <= 0 then
- begin
- // no buffer
- pbVarIntInitZero(A);
- Result := 0;
- exit;
- end;
- // copy and find size
- I := 0;
- P := @Buf;
- while I < L do
- begin
- if I + 1 > pbMaxVarIntSizeEnc then
- raise EpbVarIntError.Create(SErr_Overflow);
- C := P^;
- A.EncData[I] := C;
- if C and $80 = 0 then
- break;
- Inc(I);
- Inc(P);
- end;
- if I >= L then
- raise EpbVarIntError.Create(SErr_InvalidBuffer);
- Inc(I);
- A.EncSize := I;
- Result := I;
- end;
- // initialises VarInt from integer buffer
- procedure pbVarIntInitBinBuf(var A: TpbVarInt; const Buf; const BufSize: Integer);
- var L : Integer;
- P : PByte;
- I : Integer;
- BinBuf : Word;
- BinBufBits : ShortInt;
- BinByte : Byte;
- EncSize : Byte;
- procedure EncodeFromBuffer;
- var EncByte : Byte;
- begin
- // encode 7 bits
- EncByte := Byte(BinBuf and $7F);
- if EncSize < pbMaxVarIntSizeEnc then
- A.EncData[EncSize] := EncByte
- else
- if EncByte <> 0 then
- raise EpbVarIntError.Create(SErr_Overflow);
- Inc(EncSize);
- // remove 7 bits from buffer
- BinBuf := BinBuf shr 7;
- Dec(BinBufBits, 7);
- end;
- begin
- L := BufSize;
- if L <= 0 then
- begin
- // no buffer
- pbVarIntInitZero(A);
- exit;
- end;
- // skip most significant zero bytes
- P := @Buf;
- Inc(P, L - 1);
- while (L > 0) and (P^ = 0) do
- begin
- Dec(P);
- Dec(L);
- end;
- if L <= 0 then
- begin
- // all zero values
- pbVarIntInitZero(A);
- exit;
- end;
- // encode
- P := @Buf;
- EncSize := 0;
- BinBuf := 0;
- BinBufBits := 0;
- for I := 0 to L - 1 do
- begin
- // buffer 8 bits
- BinByte := P^;
- BinBuf := BinBuf or (Word(BinByte) shl BinBufBits);
- Inc(BinBufBits, 8);
- Inc(P);
- // encode 7 bits
- EncodeFromBuffer;
- // encode further 7 bits if available
- if BinBufBits >= 7 then
- EncodeFromBuffer;
- end;
- // encode last partial byte
- if BinBufBits > 0 then
- EncodeFromBuffer;
- // find final encoding byte
- while (EncSize > 1) and (A.EncData[EncSize - 1] = 0) do
- Dec(EncSize);
- A.EncSize := EncSize;
- // mark non-final bytes
- if EncSize > 1 then
- begin
- P := @A.EncData[0];
- for I := 0 to EncSize - 2 do
- begin
- P^ := P^ or $80;
- Inc(P);
- end;
- end;
- end;
- // initialises VarInt from LongWord
- procedure pbVarIntInitUInt32(var A: TpbVarInt; const B: LongWord);
- begin
- if B < $80 then
- begin
- A.EncSize := 1;
- A.EncData[0] := Byte(B);
- end
- else
- if B < $4000 then
- begin
- A.EncSize := 2;
- A.EncData[0] := Byte(B and $7F) or $80;
- A.EncData[1] := Byte(B shr 7);
- end
- else
- if B < $200000 then
- begin
- A.EncSize := 3;
- A.EncData[0] := Byte(B and $7F) or $80;
- A.EncData[1] := Byte((B shr 7) and $7F) or $80;
- A.EncData[2] := Byte( B shr 14 );
- end
- else
- if B < $10000000 then
- begin
- A.EncSize := 4;
- A.EncData[0] := Byte(B and $7F) or $80;
- A.EncData[1] := Byte((B shr 7) and $7F) or $80;
- A.EncData[2] := Byte((B shr 14) and $7F) or $80;
- A.EncData[3] := Byte( B shr 21 );
- end
- else
- begin
- A.EncSize := 5;
- A.EncData[0] := Byte(B and $7F) or $80;
- A.EncData[1] := Byte((B shr 7) and $7F) or $80;
- A.EncData[2] := Byte((B shr 14) and $7F) or $80;
- A.EncData[3] := Byte((B shr 21) and $7F) or $80;
- A.EncData[4] := Byte( B shr 28 );
- end;
- end;
- procedure pbVarIntInitSInt32(var A: TpbVarInt; const B: LongInt);
- begin
- pbVarIntInitUInt32(A, pbSInt32ToUInt32(B));
- end;
- procedure pbVarIntInitUInt64(var A: TpbVarInt; const B: UInt64);
- begin
- pbVarIntInitBinBuf(A, B, SizeOf(B));
- end;
- procedure pbVarIntInitInt64(var A: TpbVarInt; const B: Int64);
- begin
- pbVarIntInitBinBuf(A, B, SizeOf(B));
- end;
- procedure pbVarIntInitSInt64(var A: TpbVarInt; const B: Int64);
- begin
- pbVarIntInitUInt64(A, pbSInt64ToUInt64(B));
- end;
- // converts VarInt to encoded buffer
- // returns size of the encoded VarInt
- function pbVarIntToEncBuf(const A: TpbVarInt; var Buf; const BufSize: Integer): Integer;
- var L : Integer;
- begin
- L := A.EncSize;
- if BufSize >= L then
- Move(A.EncData[0], Buf, L);
- Result := L;
- end;
- // converts VarInt to integer buffer
- procedure pbVarIntToBinBuf(const A: TpbVarInt; var Buf; const BufSize: Integer);
- var BinBuf : Word;
- BinBufBits : ShortInt;
- BinBufP : PByte;
- BinBufLeft : Integer;
- I : Integer;
- EncByte : Byte;
- EncByteFin : Boolean;
- EncFin : Boolean;
- procedure EncodeToBuffer;
- var BinBufByte : Byte;
- begin
- BinBufByte := Byte(BinBuf and $FF);
- if BinBufLeft <= 0 then
- if BinBufByte = 0 then
- exit
- else
- raise EpbVarIntError.Create(SErr_Overflow);
- BinBufP^ := BinBufByte;
- Inc(BinBufP);
- Dec(BinBufLeft);
- BinBuf := BinBuf shr 8;
- Dec(BinBufBits, 8);
- end;
- begin
- if BufSize <= 0 then
- raise EpbVarIntError.Create(SErr_Overflow);
- BinBufP := @Buf;
- BinBufLeft := BufSize;
- BinBuf := 0;
- BinBufBits := 0;
- for I := 0 to A.EncSize - 1 do
- begin
- // decode 7 bits from encoded buffer
- EncByte := A.EncData[I];
- EncFin := (I = A.EncSize - 1);
- EncByteFin := (EncByte and $80 = 0);
- if (EncFin and not EncByteFin) or
- (not EncFin and EncByteFin) then
- raise EpbVarIntError.Create(SErr_InvalidVarInt);
- BinBuf := BinBuf or (Word(EncByte and $7F) shl BinBufBits);
- Inc(BinBufBits, 7);
- // encode 8 bits to binary buffer if available
- if BinBufBits >= 8 then
- EncodeToBuffer;
- end;
- // encode final partial byte
- if BinBufBits > 0 then
- EncodeToBuffer;
- // fill rest of binary buffer with zero
- while BinBufLeft > 0 do
- begin
- BinBufP^ := 0;
- Inc(BinBufP);
- Dec(BinBufLeft);
- end;
- end;
- // returns VarInt value as LongWord
- function pbVarIntToUInt32(const A: TpbVarInt): LongWord;
- var B : LongWord;
- I : Byte;
- EncByte : Byte;
- begin
- Assert(A.EncSize > 0);
- Assert(A.EncSize <= pbMaxVarIntSizeEnc);
- // decode
- B := 0;
- I := 0;
- while I < A.EncSize do
- begin
- EncByte := A.EncData[I];
- if I = 4 then
- if EncByte and $F0 <> 0 then
- raise EpbVarIntError.Create(SErr_Overflow);
- B := B or (LongWord(EncByte and $7F) shl (I * 7));
- Inc(I);
- if EncByte and $80 = 0 then // final byte marker
- begin
- if I < A.EncSize then // final byte marker before final byte reached
- raise EpbVarIntError.Create(SErr_InvalidVarInt);
- // finished
- Result := B;
- exit;
- end;
- end;
- // final byte reached without final byte marker
- raise EpbVarIntError.Create(SErr_InvalidVarInt);
- end;
- function pbVarIntToSInt32(const A: TpbVarInt): LongInt;
- begin
- Result := pbUInt32ToSInt32(pbVarIntToUInt32(A));
- end;
- function pbVarIntToUInt64(const A: TpbVarInt): UInt64;
- begin
- pbVarIntToBinBuf(A, Result, SizeOf(Result));
- end;
- function pbVarIntToInt64(const A: TpbVarInt): Int64;
- begin
- pbVarIntToBinBuf(A, Result, SizeOf(Result));
- end;
- function pbVarIntToSInt64(const A: TpbVarInt): Int64;
- begin
- Result := pbUInt64ToSInt64(pbVarIntToUInt64(A));
- end;
- { Field key }
- function pbFieldKeyToUInt32(const FieldNumber: LongInt; const WireType: TpbWireType): LongWord;
- begin
- Result := Ord(WireType) or (FieldNumber shl 3);
- end;
- procedure pbUInt32ToFieldKey(const A: LongWord; var FieldNumber: LongInt; var WireType: TpbWireType);
- begin
- WireType := TpbWireType(A and 7);
- FieldNumber := A shr 3;
- end;
- { Encode }
- function pbEncodeValueVarInt(var Buf; const BufSize: Integer; const Value: TpbVarInt): Integer;
- begin
- Result := pbVarIntToEncBuf(Value, Buf, BufSize);
- end;
- function pbEncodeValueInt32(var Buf; const BufSize: Integer; const Value: LongInt): Integer;
- var
- A : TpbVarInt;
- begin
- pbVarIntInitUInt32(A, LongWord(Value));
- Result := pbVarIntToEncBuf(A, Buf, BufSize);
- end;
- function pbEncodeValueInt64(var Buf; const BufSize: Integer; const Value: Int64): Integer;
- var
- A : TpbVarInt;
- begin
- pbVarIntInitBinBuf(A, Value, SizeOf(Value));
- Result := pbVarIntToEncBuf(A, Buf, BufSize);
- end;
- function pbEncodeValueUInt32(var Buf; const BufSize: Integer; const Value: LongWord): Integer;
- var
- A : TpbVarInt;
- begin
- pbVarIntInitUInt32(A, Value);
- Result := pbVarIntToEncBuf(A, Buf, BufSize);
- end;
- function pbEncodeValueUInt64(var Buf; const BufSize: Integer; const Value: UInt64): Integer;
- var
- A : TpbVarInt;
- begin
- pbVarIntInitBinBuf(A, Value, SizeOf(Value));
- Result := pbVarIntToEncBuf(A, Buf, BufSize);
- end;
- function pbEncodeValueSInt32(var Buf; const BufSize: Integer; const Value: LongInt): Integer;
- var
- A : TpbVarInt;
- begin
- pbVarIntInitSInt32(A, Value);
- Result := pbVarIntToEncBuf(A, Buf, BufSize);
- end;
- function pbEncodeValueSInt64(var Buf; const BufSize: Integer; const Value: Int64): Integer;
- var
- A : TpbVarInt;
- begin
- pbVarIntInitSInt64(A, Value);
- Result := pbVarIntToEncBuf(A, Buf, BufSize);
- end;
- function pbEncodeValueBool(var Buf; const BufSize: Integer; const Value: Boolean): Integer;
- var
- A : TpbVarInt;
- begin
- pbVarIntInitUInt32(A, Ord(Value));
- Result := pbVarIntToEncBuf(A, Buf, BufSize);
- end;
- function pbEncodeValueDouble(var Buf; const BufSize: Integer; const Value: Double): Integer;
- begin
- if BufSize >= SizeOf(Value) then
- PDouble(@Buf)^ := Value;
- Result := SizeOf(Value);
- end;
- function pbEncodeValueFloat(var Buf; const BufSize: Integer; const Value: Single): Integer;
- begin
- if BufSize >= SizeOf(Value) then
- PSingle(@Buf)^ := Value;
- Result := SizeOf(Value);
- end;
- function pbEncodeValueFixed32(var Buf; const BufSize: Integer; const Value: LongWord): Integer;
- begin
- if BufSize >= SizeOf(Value) then
- PLongWord(@Buf)^ := Value;
- Result := SizeOf(Value);
- end;
- function pbEncodeValueFixed64(var Buf; const BufSize: Integer; const Value: UInt64): Integer;
- begin
- if BufSize >= SizeOf(Value) then
- PUInt64(@Buf)^ := Value;
- Result := SizeOf(Value);
- end;
- function pbEncodeValueSFixed32(var Buf; const BufSize: Integer; const Value: LongInt): Integer;
- begin
- if BufSize >= SizeOf(Value) then
- PLongInt(@Buf)^ := Value;
- Result := SizeOf(Value);
- end;
- function pbEncodeValueSFixed64(var Buf; const BufSize: Integer; const Value: Int64): Integer;
- begin
- if BufSize >= SizeOf(Value) then
- PInt64(@Buf)^ := Value;
- Result := SizeOf(Value);
- end;
- function pbEncodeValueString(var Buf; const BufSize: Integer; const Value: AnsiString): Integer;
- var
- P : PByte;
- L, N, J : Integer;
- A : TpbVarInt;
- begin
- P := @Buf;
- L := BufSize;
- // string length as VarInt
- N := Length(Value);
- pbVarIntInitUInt32(A, LongWord(N));
- J := pbVarIntToEncBuf(A, P^, L);
- Inc(P, J);
- Dec(L, J);
- // string content
- if N > 0 then
- begin
- if L >= N then
- Move(PAnsiChar(Value)^, P^, N);
- Dec(L, N);
- end;
- Result := BufSize - L;
- end;
- function pbEncodeValueBytes(var Buf; const BufSize: Integer; const ValueBuf; const ValueBufSize: Integer): Integer;
- var
- P : PByte;
- L, N, J : Integer;
- A : TpbVarInt;
- begin
- P := @Buf;
- L := BufSize;
- // buffer size as VarInt
- N := ValueBufSize;
- pbVarIntInitUInt32(A, LongWord(N));
- J := pbVarIntToEncBuf(A, P^, L);
- Inc(P, J);
- Dec(L, J);
- // buffer content
- if N > 0 then
- begin
- if L >= N then
- Move(ValueBuf, P^, N);
- Dec(L, N);
- end;
- Result := BufSize - L;
- end;
- function pbEncodeFieldKey(var Buf; const BufSize: Integer;
- const FieldNumber: LongInt; const WireType: TpbWireType): Integer;
- var
- FieldKey : LongWord;
- A : TpbVarInt;
- begin
- FieldKey := pbFieldKeyToUInt32(FieldNumber, WireType);
- pbVarIntInitUInt32(A, FieldKey);
- Result := pbVarIntToEncBuf(A, Buf, BufSize);
- end;
- function pbEncodeFieldVarBytesHdr(var Buf; const BufSize: Integer;
- const FieldNumber: LongInt; const VarBytesSize: Integer): Integer;
- var
- P : PByte;
- L : Integer;
- I : Integer;
- A : TpbVarInt;
- begin
- if VarBytesSize < 0 then
- raise EpbEncodeError.Create(SErr_InvalidBuffer);
- P := @Buf;
- L := BufSize;
- I := pbEncodeFieldKey(P^, L, FieldNumber, pwtVarBytes);
- Dec(L, I);
- Inc(P, I);
- pbVarIntInitUInt32(A, LongWord(VarBytesSize));
- I := pbVarIntToEncBuf(A, P^, L);
- Dec(L, I);
- Result := BufSize - L;
- end;
- function pbEncodeFieldVarInt(var Buf; const BufSize: Integer;
- const FieldNumber: LongInt; const Value: TpbVarInt): Integer;
- var
- P : PByte;
- L : Integer;
- I : Integer;
- begin
- P := @Buf;
- L := BufSize;
- I := pbEncodeFieldKey(P^, L, FieldNumber, pwtVarInt);
- Inc(P, I);
- Dec(L, I);
- I := pbVarIntToEncBuf(Value, P^, L);
- Dec(L, I);
- Result := BufSize - L;
- end;
- function pbEncodeFieldInt32(var Buf; const BufSize: Integer;
- const FieldNumber: LongInt; const Value: LongInt): Integer;
- var
- A : TpbVarInt;
- begin
- pbVarIntInitUInt32(A, LongWord(Value));
- Result := pbEncodeFieldVarInt(Buf, BufSize, FieldNumber, A);
- end;
- function pbEncodeFieldInt64(var Buf; const BufSize: Integer;
- const FieldNumber: LongInt; const Value: Int64): Integer;
- var
- A : TpbVarInt;
- begin
- pbVarIntInitBinBuf(A, Value, SizeOf(Value));
- Result := pbEncodeFieldVarInt(Buf, BufSize, FieldNumber, A);
- end;
- function pbEncodeFieldUInt32(var Buf; const BufSize: Integer;
- const FieldNumber: LongInt; const Value: LongWord): Integer;
- var
- A : TpbVarInt;
- begin
- pbVarIntInitUInt32(A, Value);
- Result := pbEncodeFieldVarInt(Buf, BufSize, FieldNumber, A);
- end;
- function pbEncodeFieldUInt64(var Buf; const BufSize: Integer;
- const FieldNumber: LongInt; const Value: UInt64): Integer;
- var
- A : TpbVarInt;
- begin
- pbVarIntInitBinBuf(A, Value, SizeOf(Value));
- Result := pbEncodeFieldVarInt(Buf, BufSize, FieldNumber, A);
- end;
- function pbEncodeFieldSInt32(var Buf; const BufSize: Integer;
- const FieldNumber: LongInt; const Value: LongInt): Integer;
- var
- A : TpbVarInt;
- begin
- pbVarIntInitSInt32(A, Value);
- Result := pbEncodeFieldVarInt(Buf, BufSize, FieldNumber, A);
- end;
- function pbEncodeFieldSInt64(var Buf; const BufSize: Integer;
- const FieldNumber: LongInt; const Value: Int64): Integer;
- var
- A : TpbVarInt;
- begin
- pbVarIntInitSInt64(A, Value);
- Result := pbEncodeFieldVarInt(Buf, BufSize, FieldNumber, A);
- end;
- function pbEncodeFieldBool(var Buf; const BufSize: Integer;
- const FieldNumber: LongInt; const Value: Boolean): Integer;
- var
- A : TpbVarInt;
- begin
- pbVarIntInitUInt32(A, Ord(Value));
- Result := pbEncodeFieldVarInt(Buf, BufSize, FieldNumber, A);
- end;
- function pbEncodeFieldDouble(var Buf; const BufSize: Integer;
- const FieldNumber: LongInt; const Value: Double): Integer;
- var
- P : PByte;
- L : Integer;
- I : Integer;
- begin
- P := @Buf;
- L := BufSize;
- I := pbEncodeFieldKey(P^, L, FieldNumber, pwt64Bit);
- Inc(P, I);
- Dec(L, I);
- I := pbEncodeValueDouble(P^, L, Value);
- Dec(L, I);
- Result := BufSize - L;
- end;
- function pbEncodeFieldFloat(var Buf; const BufSize: Integer;
- const FieldNumber: LongInt; const Value: Single): Integer;
- var
- P : PByte;
- L : Integer;
- I : Integer;
- begin
- P := @Buf;
- L := BufSize;
- I := pbEncodeFieldKey(P^, L, FieldNumber, pwt32Bit);
- Inc(P, I);
- Dec(L, I);
- I := pbEncodeValueFloat(P^, L, Value);
- Dec(L, I);
- Result := BufSize - L;
- end;
- function pbEncodeFieldFixed32(var Buf; const BufSize: Integer;
- const FieldNumber: LongInt; const Value: LongWord): Integer;
- var
- P : PByte;
- L : Integer;
- I : Integer;
- begin
- P := @Buf;
- L := BufSize;
- I := pbEncodeFieldKey(P^, L, FieldNumber, pwt32Bit);
- Inc(P, I);
- Dec(L, I);
- I := pbEncodeValueFixed32(P^, L, Value);
- Dec(L, I);
- Result := BufSize - L;
- end;
- function pbEncodeFieldFixed64(var Buf; const BufSize: Integer;
- const FieldNumber: LongInt; const Value: UInt64): Integer;
- var
- P : PByte;
- L : Integer;
- I : Integer;
- begin
- P := @Buf;
- L := BufSize;
- I := pbEncodeFieldKey(P^, L, FieldNumber, pwt64Bit);
- Inc(P, I);
- Dec(L, I);
- I := pbEncodeValueFixed64(P^, L, Value);
- Dec(L, I);
- Result := BufSize - L;
- end;
- function pbEncodeFieldSFixed32(var Buf; const BufSize: Integer;
- const FieldNumber: LongInt; const Value: LongInt): Integer;
- var
- P : PByte;
- L : Integer;
- I : Integer;
- begin
- P := @Buf;
- L := BufSize;
- I := pbEncodeFieldKey(P^, L, FieldNumber, pwt32Bit);
- Inc(P, I);
- Dec(L, I);
- I := pbEncodeValueSFixed32(P^, L, Value);
- Dec(L, I);
- Result := BufSize - L;
- end;
- function pbEncodeFieldSFixed64(var Buf; const BufSize: Integer;
- const FieldNumber: LongInt; const Value: Int64): Integer;
- var
- P : PByte;
- L : Integer;
- I : Integer;
- begin
- P := @Buf;
- L := BufSize;
- I := pbEncodeFieldKey(P^, L, FieldNumber, pwt64Bit);
- Inc(P, I);
- Dec(L, I);
- I := pbEncodeValueSFixed64(P^, L, Value);
- Dec(L, I);
- Result := BufSize - L;
- end;
- function pbEncodeFieldString(var Buf; const BufSize: Integer;
- const FieldNumber: LongInt; const Value: AnsiString): Integer;
- var
- P : PByte;
- L : Integer;
- I : Integer;
- begin
- P := @Buf;
- L := BufSize;
- I := pbEncodeFieldKey(P^, L, FieldNumber, pwtVarBytes);
- Inc(P, I);
- Dec(L, I);
- I := pbEncodeValueString(P^, L, Value);
- Dec(L, I);
- Result := BufSize - L;
- end;
- function pbEncodeFieldBytes(var Buf; const BufSize: Integer;
- const FieldNumber: LongInt; const ValueBuf; const ValueBufSize: Integer): Integer;
- var
- P : PByte;
- L : Integer;
- I : Integer;
- begin
- if ValueBufSize < 0 then
- raise EpbEncodeError.Create(SErr_InvalidBuffer);
- P := @Buf;
- L := BufSize;
- I := pbEncodeFieldKey(P^, L, FieldNumber, pwtVarBytes);
- Inc(P, I);
- Dec(L, I);
- I := pbEncodeValueBytes(P^, L, ValueBuf, ValueBufSize);
- Dec(L, I);
- Result := BufSize - L;
- end;
- { Decode }
- function pbDecodeValueInt32(const Buf; const BufSize: Integer; var Value: LongInt): Integer;
- var
- A : TpbVarInt;
- begin
- Result := pbVarIntInitEncBuf(A, Buf, BufSize);
- pbVarIntToBinBuf(A, Value, SizeOf(Value));
- end;
- function pbDecodeValueInt64(const Buf; const BufSize: Integer; var Value: Int64): Integer;
- var
- A : TpbVarInt;
- begin
- Result := pbVarIntInitEncBuf(A, Buf, BufSize);
- pbVarIntToBinBuf(A, Value, SizeOf(Value));
- end;
- function pbDecodeValueUInt32(const Buf; const BufSize: Integer; var Value: LongWord): Integer;
- var
- A : TpbVarInt;
- begin
- Result := pbVarIntInitEncBuf(A, Buf, BufSize);
- pbVarIntToBinBuf(A, Value, SizeOf(Value));
- end;
- function pbDecodeValueUInt64(const Buf; const BufSize: Integer; var Value: UInt64): Integer;
- var
- A : TpbVarInt;
- begin
- Result := pbVarIntInitEncBuf(A, Buf, BufSize);
- pbVarIntToBinBuf(A, Value, SizeOf(Value));
- end;
- function pbDecodeValueSInt32(const Buf; const BufSize: Integer; var Value: LongInt): Integer;
- var
- A : TpbVarInt;
- begin
- Result := pbVarIntInitEncBuf(A, Buf, BufSize);
- Value := pbVarIntToSInt32(A);
- end;
- function pbDecodeValueSInt64(const Buf; const BufSize: Integer; var Value: Int64): Integer;
- var
- A : TpbVarInt;
- begin
- Result := pbVarIntInitEncBuf(A, Buf, BufSize);
- Value := pbVarIntToSInt64(A);
- end;
- function pbDecodeValueDouble(const Buf; const BufSize: Integer; var Value: Double): Integer;
- begin
- if BufSize < SizeOf(Value) then
- raise EpbDecodeError.Create(SErr_InvalidBuffer);
- Value := PDouble(@Buf)^;
- Result := SizeOf(Value);
- end;
- function pbDecodeValueFloat(const Buf; const BufSize: Integer; var Value: Single): Integer;
- begin
- if BufSize < SizeOf(Value) then
- raise EpbDecodeError.Create(SErr_InvalidBuffer);
- Value := PSingle(@Buf)^;
- Result := SizeOf(Value);
- end;
- function pbDecodeValueFixed32(const Buf; const BufSize: Integer; var Value: LongWord): Integer;
- begin
- if BufSize < SizeOf(Value) then
- raise EpbDecodeError.Create(SErr_InvalidBuffer);
- Value := PLongWord(@Buf)^;
- Result := SizeOf(Value);
- end;
- function pbDecodeValueFixed64(const Buf; const BufSize: Integer; var Value: UInt64): Integer;
- begin
- if BufSize < SizeOf(Value) then
- raise EpbDecodeError.Create(SErr_InvalidBuffer);
- Value := PUInt64(@Buf)^;
- Result := SizeOf(Value);
- end;
- function pbDecodeValueSFixed32(const Buf; const BufSize: Integer; var Value: LongInt): Integer;
- begin
- if BufSize < SizeOf(Value) then
- raise EpbDecodeError.Create(SErr_InvalidBuffer);
- Value := PLongInt(@Buf)^;
- Result := SizeOf(Value);
- end;
- function pbDecodeValueSFixed64(const Buf; const BufSize: Integer; var Value: Int64): Integer;
- begin
- if BufSize < SizeOf(Value) then
- raise EpbDecodeError.Create(SErr_InvalidBuffer);
- Value := PInt64(@Buf)^;
- Result := SizeOf(Value);
- end;
- function pbDecodeValueBool(const Buf; const BufSize: Integer; var Value: Boolean): Integer;
- var
- A : TpbVarInt;
- begin
- Result := pbVarIntInitEncBuf(A, Buf, BufSize);
- Value := not pbVarIntIsZero(A);
- end;
- function pbDecodeValueString(const Buf; const BufSize: Integer; var Value: AnsiString): Integer;
- var
- P : PByte;
- L, I, N : Integer;
- S : AnsiString;
- begin
- P := @Buf;
- L := BufSize;
- I := pbDecodeValueInt32(P^, L, N);
- Dec(L, I);
- Inc(P, I);
- if L < N then
- raise EpbDecodeError.Create(SErr_InvalidBuffer);
- SetLength(S, N);
- if N > 0 then
- begin
- Move(P^, PAnsiChar(S)^, N);
- Dec(L, N);
- end;
- Value := S;
- Result:= BufSize - L;
- end;
- function pbDecodeValueBytes(const Buf; const BufSize: Integer; var ValueBuf; const ValueBufSize: Integer): Integer;
- var
- P : PByte;
- L, I, N : Integer;
- begin
- P := @Buf;
- L := BufSize;
- I := pbDecodeValueInt32(P^, L, N);
- Dec(L, I);
- Inc(P, I);
- if L < N then
- raise EpbDecodeError.Create(SErr_InvalidBuffer);
- if N > 0 then
- begin
- if ValueBufSize < N then
- raise EpbDecodeError.Create(SErr_InvalidBuffer);
- Move(P^, ValueBuf, N);
- Dec(L, N);
- end;
- Result:= BufSize - L;
- end;
- procedure pbDecodeProtoBuf(
- const Buf; const BufSize: Integer;
- const CallbackProc: TpbProtoBufDecodeFieldCallbackProc;
- const CallbackData: Pointer);
- var
- P : PByte;
- L : Integer;
- I : Integer;
- A : TpbVarInt;
- Q : PByte;
- WireSize : Integer;
- Key32 : LongWord;
- Field : TpbProtoBufDecodeField;
- begin
- P := @Buf;
- L := BufSize;
- while L > 0 do
- begin
- // decode field key
- I := pbVarIntInitEncBuf(A, P^, L);
- Assert(I > 0);
- Inc(P, I);
- Dec(L, I);
- Key32 := pbVarIntToUInt32(A);
- pbUInt32ToFieldKey(Key32, Field.FieldNum, Field.WireType);
- // decode wire data
- Field.WireDataPtr := P;
- case Field.WireType of
- pwt32Bit : WireSize := 4;
- pwt64Bit : WireSize := 8;
- pwtVarInt : WireSize := pbVarIntInitEncBuf(Field.ValueVarInt, P^, L);
- pwtVarBytes :
- begin
- // VarBytes size
- I := pbVarIntInitEncBuf(A, P^, L);
- Field.ValueVarBytesLen := pbVarIntToUInt32(A);
- // VarBytes data
- Q := P;
- Inc(Q, I);
- Field.ValueVarBytesPtr := Q;
- // wire size
- WireSize := I + Field.ValueVarBytesLen;
- end;
- else
- raise EpbDecodeError.Create(SErr_InvalidBuffer); // unrecognised wire type
- end;
- Field.WireDataSize := WireSize;
- if WireSize > L then
- raise EpbDecodeError.Create(SErr_InvalidBuffer); // incomplete/bad buffer
- Inc(P, WireSize);
- Dec(L, WireSize);
- // field callback
- if Assigned(CallbackProc) then
- CallbackProc(Field, CallbackData);
- end;
- end;
- procedure pbDecodeFieldInt32(const Field: TpbProtoBufDecodeField; var Value: LongInt);
- begin
- case Field.WireType of
- pwt32Bit : Value := PLongInt(Field.WireDataPtr)^;
- pwtVarInt : pbVarIntToBinBuf(Field.ValueVarInt, Value, SizeOf(Value));
- else
- raise EpbDecodeError.Create(SErr_InvalidWireType);
- end;
- end;
- procedure pbDecodeFieldInt64(const Field: TpbProtoBufDecodeField; var Value: Int64);
- begin
- case Field.WireType of
- pwt32Bit : Value := PLongInt(Field.WireDataPtr)^;
- pwt64Bit : Value := PInt64(Field.WireDataPtr)^;
- pwtVarInt : pbVarIntToBinBuf(Field.ValueVarInt, Value, SizeOf(Value));
- else
- raise EpbDecodeError.Create(SErr_InvalidWireType);
- end;
- end;
- procedure pbDecodeFieldUInt32(const Field: TpbProtoBufDecodeField; var Value: LongWord);
- begin
- case Field.WireType of
- pwt32Bit : Value := PLongWord(Field.WireDataPtr)^;
- pwtVarInt : Value := pbVarIntToUInt32(Field.ValueVarInt);
- else
- raise EpbDecodeError.Create(SErr_InvalidWireType);
- end;
- end;
- procedure pbDecodeFieldUInt64(const Field: TpbProtoBufDecodeField; var Value: UInt64);
- begin
- case Field.WireType of
- pwt32Bit : Value := PLongWord(Field.WireDataPtr)^;
- pwt64Bit : Value := PUInt64(Field.WireDataPtr)^;
- pwtVarInt : pbVarIntToBinBuf(Field.ValueVarInt, Value, SizeOf(Value));
- else
- raise EpbDecodeError.Create(SErr_InvalidWireType);
- end;
- end;
- procedure pbDecodeFieldSInt32(const Field: TpbProtoBufDecodeField; var Value: LongInt);
- begin
- case Field.WireType of
- pwt32Bit : Value := PLongInt(Field.WireDataPtr)^;
- pwtVarInt : Value := pbVarIntToSInt32(Field.ValueVarInt);
- else
- raise EpbDecodeError.Create(SErr_InvalidWireType);
- end;
- end;
- procedure pbDecodeFieldSInt64(const Field: TpbProtoBufDecodeField; var Value: Int64);
- begin
- case Field.WireType of
- pwt32Bit : Value := PLongInt(Field.WireDataPtr)^;
- pwt64Bit : Value := PInt64(Field.WireDataPtr)^;
- pwtVarInt : Value := pbVarIntToSInt64(Field.ValueVarInt);
- else
- raise EpbDecodeError.Create(SErr_InvalidWireType);
- end;
- end;
- procedure pbDecodeFieldDouble(const Field: TpbProtoBufDecodeField; var Value: Double);
- begin
- case Field.WireType of
- pwt32Bit : Value := PSingle(Field.WireDataPtr)^;
- pwt64Bit : Value := PDouble(Field.WireDataPtr)^;
- else
- raise EpbDecodeError.Create(SErr_InvalidWireType);
- end;
- end;
- procedure pbDecodeFieldFloat(const Field: TpbProtoBufDecodeField; var Value: Single);
- begin
- case Field.WireType of
- pwt32Bit : Value := PSingle(Field.WireDataPtr)^;
- else
- raise EpbDecodeError.Create(SErr_InvalidWireType);
- end;
- end;
- procedure pbDecodeFieldFixed32(const Field: TpbProtoBufDecodeField; var Value: LongWord);
- begin
- case Field.WireType of
- pwt32Bit : Value := PLongWord(Field.WireDataPtr)^;
- pwtVarInt : Value := pbVarIntToUInt32(Field.ValueVarInt);
- else
- raise EpbDecodeError.Create(SErr_InvalidWireType);
- end;
- end;
- procedure pbDecodeFieldFixed64(const Field: TpbProtoBufDecodeField; var Value: UInt64);
- begin
- case Field.WireType of
- pwt32Bit : Value := PLongWord(Field.WireDataPtr)^;
- pwt64Bit : Value := PUInt64(Field.WireDataPtr)^;
- else
- raise EpbDecodeError.Create(SErr_InvalidWireType);
- end;
- end;
- procedure pbDecodeFieldSFixed32(const Field: TpbProtoBufDecodeField; var Value: LongInt);
- begin
- case Field.WireType of
- pwt32Bit : Value := PLongInt(Field.WireDataPtr)^;
- else
- raise EpbDecodeError.Create(SErr_InvalidWireType);
- end;
- end;
- procedure pbDecodeFieldSFixed64(const Field: TpbProtoBufDecodeField; var Value: Int64);
- begin
- case Field.WireType of
- pwt32Bit : Value := PLongInt(Field.WireDataPtr)^;
- pwt64Bit : Value := PInt64(Field.WireDataPtr)^;
- else
- raise EpbDecodeError.Create(SErr_InvalidWireType);
- end;
- end;
- procedure pbDecodeFieldBool(const Field: TpbProtoBufDecodeField; var Value: Boolean);
- begin
- case Field.WireType of
- pwt32Bit : Value := PLongInt(Field.WireDataPtr)^ <> 0;
- pwt64Bit : Value := PInt64(Field.WireDataPtr)^ <> 0;
- pwtVarInt : Value := pbVarIntToUInt32(Field.ValueVarInt) <> 0;
- else
- raise EpbDecodeError.Create(SErr_InvalidWireType);
- end;
- end;
- function pbDecodeFieldString(const Field: TpbProtoBufDecodeField; var Value: AnsiString): Integer;
- var
- S : AnsiString;
- L : Integer;
- begin
- case Field.WireType of
- pwtVarBytes :
- begin
- L := Field.ValueVarBytesLen;
- SetLength(S, L);
- if L > 0 then
- Move(Field.ValueVarBytesPtr^, PAnsiChar(S)^, L);
- Value := S;
- Result := L;
- end;
- else
- raise EpbDecodeError.Create(SErr_InvalidWireType);
- end;
- end;
- function pbDecodeFieldBytes(const Field: TpbProtoBufDecodeField; var ValueBuf; const ValueBufSize: Integer): Integer;
- var
- L : Integer;
- begin
- case Field.WireType of
- pwtVarBytes :
- begin
- L := Field.ValueVarBytesLen;
- if ValueBufSize < L then
- raise EpbDecodeError.Create(SErr_BufferTooSmall);
- if L > 0 then
- Move(Field.ValueVarBytesPtr^, ValueBuf, L);
- Result := L;
- end;
- else
- raise EpbDecodeError.Create(SErr_InvalidWireType);
- end;
- end;
- { }
- { Test cases }
- { }
- {$IFDEF PROTOBUF_SELFTEST}
- {$ASSERTIONS ON}
- {$IFNDEF SupportsUInt64}{$WARNINGS OFF}{$ENDIF}
- procedure SelfTest_VarInt;
- var I, J : TpbVarInt;
- A : LongWord;
- begin
- // InitZero
- pbVarIntInitZero(I);
- Assert(I.EncSize = 1);
- Assert(I.EncData[0] = 0);
- Assert(pbVarIntToUInt32(I) = 0);
- // pbVarIntEquals
- pbVarIntInitUInt32(I, 0);
- pbVarIntInitZero(J);
- Assert(pbVarIntEquals(I, J));
- pbVarIntInitUInt32(I, 0);
- pbVarIntInitUInt32(J, 1);
- Assert(not pbVarIntEquals(I, J));
- // pbVarIntInitLongWord
- pbVarIntInitUInt32(I, 0);
- Assert(I.EncSize = 1);
- Assert(I.EncData[0] = 0);
- Assert(pbVarIntToUInt32(I) = 0);
- pbVarIntInitUInt32(I, 1);
- Assert(I.EncSize = 1);
- Assert(I.EncData[0] = 1);
- Assert(pbVarIntToUInt32(I) = 1);
- pbVarIntInitUInt32(I, $7F);
- Assert(I.EncSize = 1);
- Assert(I.EncData[0] = $7F);
- Assert(pbVarIntToUInt32(I) = $7F);
- pbVarIntInitUInt32(I, $80);
- Assert(I.EncSize = 2);
- Assert(I.EncData[0] = $80);
- Assert(I.EncData[1] = $01);
- Assert(pbVarIntToUInt32(I) = $80);
- pbVarIntInitUInt32(I, $81);
- Assert(I.EncSize = 2);
- Assert(I.EncData[0] = $81);
- Assert(I.EncData[1] = $01);
- Assert(pbVarIntToUInt32(I) = $81);
- pbVarIntInitUInt32(I, $100);
- Assert(I.EncSize = 2);
- Assert(I.EncData[0] = $80);
- Assert(I.EncData[1] = $02);
- Assert(pbVarIntToUInt32(I) = $100);
- pbVarIntInitUInt32(I, $3FFF);
- Assert(I.EncSize = 2);
- Assert(I.EncData[0] = $FF);
- Assert(I.EncData[1] = $7F);
- Assert(pbVarIntToUInt32(I) = $3FFF);
- pbVarIntInitUInt32(I, $4000);
- Assert(I.EncSize = 3);
- Assert(I.EncData[0] = $80);
- Assert(I.EncData[1] = $80);
- Assert(I.EncData[2] = $01);
- Assert(pbVarIntToUInt32(I) = $4000);
- pbVarIntInitUInt32(I, $4001);
- Assert(I.EncSize = 3);
- Assert(I.EncData[0] = $81);
- Assert(I.EncData[1] = $80);
- Assert(I.EncData[2] = $01);
- Assert(pbVarIntToUInt32(I) = $4001);
- pbVarIntInitUInt32(I, $1FFFFF);
- Assert(I.EncSize = 3);
- Assert(I.EncData[0] = $FF);
- Assert(I.EncData[1] = $FF);
- Assert(I.EncData[2] = $7F);
- Assert(pbVarIntToUInt32(I) = $1FFFFF);
- pbVarIntInitUInt32(I, $200000);
- Assert(I.EncSize = 4);
- Assert(I.EncData[0] = $80);
- Assert(I.EncData[1] = $80);
- Assert(I.EncData[2] = $80);
- Assert(I.EncData[3] = $01);
- Assert(pbVarIntToUInt32(I) = $200000);
- pbVarIntInitUInt32(I, $FFFFFFF);
- Assert(I.EncSize = 4);
- Assert(I.EncData[0] = $FF);
- Assert(I.EncData[1] = $FF);
- Assert(I.EncData[2] = $FF);
- Assert(I.EncData[3] = $7F);
- Assert(pbVarIntToUInt32(I) = $FFFFFFF);
- pbVarIntInitUInt32(I, $10000000);
- Assert(I.EncSize = 5);
- Assert(I.EncData[0] = $80);
- Assert(I.EncData[1] = $80);
- Assert(I.EncData[2] = $80);
- Assert(I.EncData[3] = $80);
- Assert(I.EncData[4] = $01);
- Assert(pbVarIntToUInt32(I) = $10000000);
- pbVarIntInitUInt32(I, $12345678);
- Assert(I.EncSize = 5);
- Assert(I.EncData[0] = $F8);
- Assert(I.EncData[1] = $AC);
- Assert(I.EncData[2] = $D1);
- Assert(I.EncData[3] = $91);
- Assert(I.EncData[4] = $01);
- Assert(pbVarIntToUInt32(I) = $12345678);
- pbVarIntInitUInt32(I, $FFFFFFFF);
- Assert(I.EncSize = 5);
- Assert(I.EncData[0] = $FF);
- Assert(I.EncData[1] = $FF);
- Assert(I.EncData[2] = $FF);
- Assert(I.EncData[3] = $FF);
- Assert(I.EncData[4] = $0F);
- Assert(pbVarIntToUInt32(I) = $FFFFFFFF);
- // pbVarIntInitBinBuf
- A := 0;
- pbVarIntInitBinBuf(I, A, SizeOf(A));
- Assert(I.EncSize = 1);
- Assert(I.EncData[0] = 0);
- Assert(pbVarIntToUInt32(I) = 0);
- A := 1;
- pbVarIntInitBinBuf(I, A, SizeOf(A));
- Assert(I.EncSize = 1);
- Assert(I.EncData[0] = 1);
- Assert(pbVarIntToUInt32(I) = 1);
- A := $7F;
- pbVarIntInitBinBuf(I, A, SizeOf(A));
- Assert(I.EncSize = 1);
- Assert(I.EncData[0] = $7F);
- Assert(pbVarIntToUInt32(I) = $7F);
- A := $80;
- pbVarIntInitBinBuf(I, A, SizeOf(A));
- Assert(I.EncSize = 2);
- Assert(I.EncData[0] = $80);
- Assert(I.EncData[1] = $01);
- Assert(pbVarIntToUInt32(I) = $80);
- A := $81;
- pbVarIntInitBinBuf(I, A, SizeOf(A));
- Assert(I.EncSize = 2);
- Assert(I.EncData[0] = $81);
- Assert(I.EncData[1] = $01);
- Assert(pbVarIntToUInt32(I) = $81);
- A := $100;
- pbVarIntInitBinBuf(I, A, SizeOf(A));
- Assert(I.EncSize = 2);
- Assert(I.EncData[0] = $80);
- Assert(I.EncData[1] = $02);
- Assert(pbVarIntToUInt32(I) = $100);
- A := $3FFF;
- pbVarIntInitBinBuf(I, A, SizeOf(A));
- Assert(I.EncSize = 2);
- Assert(I.EncData[0] = $FF);
- Assert(I.EncData[1] = $7F);
- Assert(pbVarIntToUInt32(I) = $3FFF);
- A := $4000;
- pbVarIntInitBinBuf(I, A, SizeOf(A));
- Assert(I.EncSize = 3);
- Assert(I.EncData[0] = $80);
- Assert(I.EncData[1] = $80);
- Assert(I.EncData[2] = $01);
- Assert(pbVarIntToUInt32(I) = $4000);
- A := $4001;
- pbVarIntInitBinBuf(I, A, SizeOf(A));
- Assert(I.EncSize = 3);
- Assert(I.EncData[0] = $81);
- Assert(I.EncData[1] = $80);
- Assert(I.EncData[2] = $01);
- Assert(pbVarIntToUInt32(I) = $4001);
- A := $200000;
- pbVarIntInitBinBuf(I, A, SizeOf(A));
- Assert(I.EncSize = 4);
- Assert(I.EncData[0] = $80);
- Assert(I.EncData[1] = $80);
- Assert(I.EncData[2] = $80);
- Assert(I.EncData[3] = $01);
- Assert(pbVarIntToUInt32(I) = $200000);
- A := $10000000;
- pbVarIntInitBinBuf(I, A, SizeOf(A));
- Assert(I.EncSize = 5);
- Assert(I.EncData[0] = $80);
- Assert(I.EncData[1] = $80);
- Assert(I.EncData[2] = $80);
- Assert(I.EncData[3] = $80);
- Assert(I.EncData[4] = $01);
- Assert(pbVarIntToUInt32(I) = $10000000);
- A := $12345678;
- pbVarIntInitBinBuf(I, A, SizeOf(A));
- Assert(I.EncSize = 5);
- Assert(I.EncData[0] = $F8);
- Assert(I.EncData[1] = $AC);
- Assert(I.EncData[2] = $D1);
- Assert(I.EncData[3] = $91);
- Assert(I.EncData[4] = $01);
- Assert(pbVarIntToUInt32(I) = $12345678);
- A := $FFFFFFFF;
- pbVarIntInitBinBuf(I, A, SizeOf(A));
- Assert(I.EncSize = 5);
- Assert(I.EncData[0] = $FF);
- Assert(I.EncData[1] = $FF);
- Assert(I.EncData[2] = $FF);
- Assert(I.EncData[3] = $FF);
- Assert(I.EncData[4] = $0F);
- Assert(pbVarIntToUInt32(I) = $FFFFFFFF);
- // pbVarIntInitEncBuf
- pbVarIntInitUInt32(I, 0);
- Assert(pbVarIntInitEncBuf(J, I.EncData[0], SizeOf(I.EncData)) = I.EncSize);
- Assert(pbVarIntToUInt32(J) = 0);
- pbVarIntInitUInt32(I, 1);
- Assert(pbVarIntInitEncBuf(J, I.EncData[0], SizeOf(I.EncData)) = I.EncSize);
- Assert(pbVarIntToUInt32(J) = 1);
- pbVarIntInitUInt32(I, $80);
- Assert(pbVarIntInitEncBuf(J, I.EncData[0], SizeOf(I.EncData)) = I.EncSize);
- Assert(pbVarIntToUInt32(J) = $80);
- pbVarIntInitUInt32(I, $3FFF);
- Assert(pbVarIntInitEncBuf(J, I.EncData[0], SizeOf(I.EncData)) = I.EncSize);
- Assert(pbVarIntToUInt32(J) = $3FFF);
- pbVarIntInitUInt32(I, $4000);
- Assert(pbVarIntInitEncBuf(J, I.EncData[0], SizeOf(I.EncData)) = I.EncSize);
- Assert(pbVarIntToUInt32(J) = $4000);
- pbVarIntInitUInt32(I, $4001);
- Assert(pbVarIntInitEncBuf(J, I.EncData[0], SizeOf(I.EncData)) = I.EncSize);
- Assert(pbVarIntToUInt32(J) = $4001);
- pbVarIntInitUInt32(I, $12345678);
- Assert(pbVarIntInitEncBuf(J, I.EncData[0], SizeOf(I.EncData)) = I.EncSize);
- Assert(pbVarIntToUInt32(J) = $12345678);
- pbVarIntInitUInt32(I, $FFFFFFFF);
- Assert(pbVarIntInitEncBuf(J, I.EncData[0], SizeOf(I.EncData)) = I.EncSize);
- Assert(pbVarIntToUInt32(J) = $FFFFFFFF);
- // pbVarIntToBinBuf
- pbVarIntInitUInt32(I, 0);
- pbVarIntToBinBuf(I, A, SizeOf(A));
- Assert(A = 0);
- pbVarIntInitUInt32(I, 1);
- pbVarIntToBinBuf(I, A, SizeOf(A));
- Assert(A = 1);
- pbVarIntInitUInt32(I, $7F);
- pbVarIntToBinBuf(I, A, SizeOf(A));
- Assert(A = $7F);
- pbVarIntInitUInt32(I, $80);
- pbVarIntToBinBuf(I, A, SizeOf(A));
- Assert(A = $80);
- pbVarIntInitUInt32(I, $81);
- pbVarIntToBinBuf(I, A, SizeOf(A));
- Assert(A = $81);
- pbVarIntInitUInt32(I, $3FFF);
- pbVarIntToBinBuf(I, A, SizeOf(A));
- Assert(A = $3FFF);
- pbVarIntInitUInt32(I, $4000);
- pbVarIntToBinBuf(I, A, SizeOf(A));
- Assert(A = $4000);
- pbVarIntInitUInt32(I, $4001);
- pbVarIntToBinBuf(I, A, SizeOf(A));
- Assert(A = $4001);
- pbVarIntInitUInt32(I, $200000);
- pbVarIntToBinBuf(I, A, SizeOf(A));
- Assert(A = $200000);
- pbVarIntInitUInt32(I, $10000000);
- pbVarIntToBinBuf(I, A, SizeOf(A));
- Assert(A = $10000000);
- pbVarIntInitUInt32(I, $12345678);
- pbVarIntToBinBuf(I, A, SizeOf(A));
- Assert(A = $12345678);
- pbVarIntInitUInt32(I, $FFFFFFFF);
- pbVarIntToBinBuf(I, A, SizeOf(A));
- Assert(A = $FFFFFFFF);
- // pbSInt32ToUInt32
- Assert(pbSInt32ToUInt32(0) = 0);
- Assert(pbSInt32ToUInt32(-1) = 1);
- Assert(pbSInt32ToUInt32(1) = 2);
- Assert(pbSInt32ToUInt32(-2) = 3);
- Assert(pbSInt32ToUInt32(2) = 4);
- Assert(pbSInt32ToUInt32(-3) = 5);
- Assert(pbSInt32ToUInt32(2147483647) = $FFFFFFFE);
- Assert(pbSInt32ToUInt32(-2147483648) = $FFFFFFFF);
- // pbUInt32ToSInt32
- Assert(pbUInt32ToSInt32(0) = 0);
- Assert(pbUInt32ToSInt32(1) = -1);
- Assert(pbUInt32ToSInt32(2) = 1);
- Assert(pbUInt32ToSInt32(3) = -2);
- Assert(pbUInt32ToSInt32(4) = 2);
- Assert(pbUInt32ToSInt32(5) = -3);
- Assert(pbUInt32ToSInt32($FFFFFFFE) = 2147483647);
- Assert(pbUInt32ToSInt32($FFFFFFFF) = -2147483648);
- // pbVarIntInitSInt32
- pbVarIntInitSInt32(I, 0);
- Assert(pbVarIntToSInt32(I) = 0);
- pbVarIntInitSInt32(I, -1);
- Assert(pbVarIntToSInt32(I) = -1);
- pbVarIntInitSInt32(I, 1);
- Assert(pbVarIntToSInt32(I) = 1);
- pbVarIntInitSInt32(I, 2147483647);
- Assert(pbVarIntToSInt32(I) = 2147483647);
- pbVarIntInitSInt32(I, -2147483648);
- Assert(pbVarIntToSInt32(I) = -2147483648);
- // pbSInt64ToUInt64
- Assert(pbSInt64ToUInt64(0) = 0);
- Assert(pbSInt64ToUInt64(-1) = 1);
- Assert(pbSInt64ToUInt64(1) = 2);
- Assert(pbSInt64ToUInt64(-2) = 3);
- Assert(pbSInt64ToUInt64(2) = 4);
- Assert(pbSInt64ToUInt64(-3) = 5);
- Assert(pbSInt64ToUInt64(2147483647) = $FFFFFFFE);
- Assert(pbSInt64ToUInt64(-2147483648) = $FFFFFFFF);
- Assert(pbSInt64ToUInt64(2147483648) = $100000000);
- Assert(pbSInt64ToUInt64(-2147483649) = $100000001);
- Assert(pbSInt64ToUInt64(4611686018427387903) = $7FFFFFFFFFFFFFFE);
- Assert(pbSInt64ToUInt64(-4611686018427387904) = $7FFFFFFFFFFFFFFF);
- Assert(pbSInt64ToUInt64(4611686018427387904) = $8000000000000000);
- Assert(pbSInt64ToUInt64(-4611686018427387905) = $8000000000000001);
- Assert(pbSInt64ToUInt64(9223372036854775807) = $FFFFFFFFFFFFFFFE);
- Assert(pbSInt64ToUInt64(-9223372036854775808) = $FFFFFFFFFFFFFFFF);
- // pbUInt64ToSInt64
- Assert(pbUInt64ToSInt64(0) = 0);
- Assert(pbUInt64ToSInt64(1) = -1);
- Assert(pbUInt64ToSInt64(2) = 1);
- Assert(pbUInt64ToSInt64(3) = -2);
- Assert(pbUInt64ToSInt64(4) = 2);
- Assert(pbUInt64ToSInt64(5) = -3);
- Assert(pbUInt64ToSInt64($FFFFFFFE) = 2147483647);
- Assert(pbUInt64ToSInt64($FFFFFFFF) = -2147483648);
- Assert(pbUInt64ToSInt64($100000000) = 2147483648);
- Assert(pbUInt64ToSInt64($100000001) = -2147483649);
- Assert(pbUInt64ToSInt64($7FFFFFFFFFFFFFFE) = 4611686018427387903);
- Assert(pbUInt64ToSInt64($7FFFFFFFFFFFFFFF) = -4611686018427387904);
- Assert(pbUInt64ToSInt64($8000000000000000) = 4611686018427387904);
- Assert(pbUInt64ToSInt64($8000000000000001) = -4611686018427387905);
- Assert(pbUInt64ToSInt64($FFFFFFFFFFFFFFFE) = 9223372036854775807);
- Assert(pbUInt64ToSInt64($FFFFFFFFFFFFFFFF) = -9223372036854775808);
- // pbVarIntInitSInt64
- pbVarIntInitSInt64(I, 0);
- Assert(pbVarIntToSInt64(I) = 0);
- pbVarIntInitSInt64(I, -1);
- Assert(pbVarIntToSInt64(I) = -1);
- pbVarIntInitSInt64(I, 1);
- Assert(pbVarIntToSInt64(I) = 1);
- pbVarIntInitSInt64(I, 2147483647);
- Assert(pbVarIntToSInt64(I) = 2147483647);
- pbVarIntInitSInt64(I, -2147483648);
- Assert(pbVarIntToSInt64(I) = -2147483648);
- pbVarIntInitSInt64(I, 2147483648);
- Assert(pbVarIntToSInt64(I) = 2147483648);
- pbVarIntInitSInt64(I, -2147483649);
- Assert(pbVarIntToSInt64(I) = -2147483649);
- pbVarIntInitSInt64(I, 9223372036854775807);
- Assert(pbVarIntToSInt64(I) = 9223372036854775807);
- pbVarIntInitSInt64(I, -9223372036854775808);
- Assert(pbVarIntToSInt64(I) = -9223372036854775808);
- end;
- procedure SelfTest_Encode;
- var
- B : array[0..1023] of Byte;
- begin
- FillChar(B, SizeOf(B), 0);
- Assert(pbEncodeFieldKey(B[0], SizeOf(B), 3, pwtVarBytes) = 1);
- Assert(B[0] = $1A);
- Assert(pbEncodeFieldString(B[0], SizeOf(B), 2, 'testing') = 9);
- Assert(B[0] = $12);
- Assert(B[1] = $07);
- Assert(B[2] = $74);
- Assert(B[8] = $67);
- Assert(pbEncodeFieldUInt32(B[0], SizeOf(B), 2, 12) = 2);
- Assert(B[0] = $10);
- Assert(B[1] = $0C);
- FillChar(B, SizeOf(B), 0);
- Assert(pbEncodeFieldString(B[0], 0, 2, 'testing') = 9);
- Assert(B[0] = 0);
- Assert(B[1] = 0);
- Assert(B[2] = 0);
- Assert(B[8] = 0);
- Assert(pbEncodeFieldUInt32(B[0], 0, 2, 12) = 2);
- Assert(B[0] = 0);
- Assert(B[1] = 0);
- end;
- procedure SelfTest_Decode_CallbackProc(const Field: TpbProtoBufDecodeField; const Data: Pointer);
- var
- A : LongWord;
- B : Int64;
- C : AnsiString;
- begin
- case Integer(Data) of
- 1 : case Field.FieldNum of
- 4 : begin
- Assert(Field.WireType = pwtVarInt);
- Assert(pbVarIntToUInt32(Field.ValueVarInt) = 13);
- A := 0;
- pbDecodeFieldUInt32(Field, A);
- Assert(A = 13);
- end;
- 5 : begin
- Assert(Field.WireType = pwtVarInt);
- Assert(pbVarIntToSInt64(Field.ValueVarInt) = -1);
- B := 0;
- pbDecodeFieldSInt64(Field, B);
- Assert(B = -1);
- end;
- 6 : begin
- Assert(Field.WireType = pwtVarBytes);
- pbDecodeFieldString(Field, C);
- Assert(C = 'ABC');
- end;
- else
- Assert(False);
- end;
- else
- Assert(False);
- end;
- end;
- procedure SelfTest_Decode;
- var
- B : array[0..1023] of Byte;
- P : PByte;
- L, N : Integer;
- begin
- P := @B[0];
- L := SizeOf(B);
- N := pbEncodeFieldInt32(P^, L, 4, 13);
- Dec(L, N);
- Inc(P, N);
- N := pbEncodeFieldSInt64(P^, L, 5, -1);
- Dec(L, N);
- Inc(P, N);
- N := pbEncodeFieldString(P^, L, 6, 'ABC');
- Dec(L, N);
- pbDecodeProtoBuf(B[0], SizeOf(B) - L, SelfTest_Decode_CallbackProc, Ptr(1));
- end;
- procedure SelfTest;
- begin
- SelfTest_VarInt;
- SelfTest_Encode;
- SelfTest_Decode;
- end;
- {$ENDIF}
- end.
|