-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathDMsgPackHelper.pas
More file actions
831 lines (759 loc) · 22.1 KB
/
Copy pathDMsgPackHelper.pas
File metadata and controls
831 lines (759 loc) · 22.1 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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
unit DMsgPackHelper;
interface
uses
Classes, SysUtils;
type
// copy from qmsgPack
TMsgPackValue= packed record
ValueType:Byte;
case Integer of
0:(U8Val:Byte);
1:(I8Val:Shortint);
2:(U16Val:Word);
3:(I16Val:Smallint);
4:(U32Val:Cardinal);
5:(I32Val:Integer);
6:(U64Val:UInt64);
7:(I64Val:Int64);
//8:(F32Val:Single);
//9:(F64Val:Double);
10:(BArray:array[0..16] of Byte);
end;
{$IF RTLVersion<25}
IntPtr=Integer;
{$IFEND IntPtr}
{$if CompilerVersion < 18} //before delphi 2007
TBytes = array of Byte;
{$ifend}
/// <summary>
/// 按照msgPack协议
/// 1.将数据写入到Stream
/// 2.从流中读取出数据
/// </summary>
TDMsgPackHelper = class(TObject)
public
/// <summary>
/// MsgPack协议方式写入一个字符串
/// </summary>
class procedure Write(pvStream: TStream; pvValue: string); overload;
/// <summary>
/// 按照MsgPack协议 写入一个二进制数据
/// </summary>
class procedure Write(pvStream: TStream; pvBuf:Pointer; pvLen:Cardinal); overload;
/// <summary>
/// 按照MsgPack协议 读取一个二进制数据
/// </summary>
class function ReadBinary(pvStream: TStream): TBytes; overload;
/// <summary>
/// 按照MsgPack协议 读取一个二进制数据,写入到另外一个流
/// </summary>
class procedure ReadBinary(pvSourceStream, pvDestStream: TStream); overload;
/// <summary>
/// MsgPack协议读取一个字符串, 如果是null值会返回''
/// </summary>
class function ReadString(pvStream:TStream):String;
/// <summary>
/// MsgPack协议方式写入一个整形数据
/// copy from qmsgPack
/// 2015-09-30 12:57:02
/// 未全面测试
/// </summary>
class procedure Write(pvStream: TStream; pvValue: Int64); overload;
/// <summary>
/// MsgPack协议读取一个整形, 如果是null值会返回0
/// 2015-09-30 12:57:02
/// 未全面测试
/// </summary>
class function ReadInt(pvStream:TStream): Int64;
/// <summary>
/// MsgPack协议方式写入一个浮点数据
/// 2015-09-30 12:57:02
/// 未全面测试
/// </summary>
class procedure Write(pvStream: TStream; pvValue: Double); overload;
/// <summary>
/// MsgPack协议读取一个浮点, 如果是null值会返回0
/// 2015-09-30 12:57:02
/// 未全面测试
/// </summary>
class function ReadFloat(pvStream:TStream): Double;
end;
implementation
resourcestring
strErrorStringData = '[%d]非法的字符串协议格式数据';
strErrorBinaryData = '[%d]非法的二进制协议格式数据';
strErrorIntData = '[%d]非法的整数协议格式数据';
strErrorFloatData = '[%d]非法的浮点格式数据';
function swap16(const v): Word;
begin
// FF, EE : EE->1, FF->2
PByte(@result)^ := PByte(IntPtr(@v) + 1)^;
PByte(IntPtr(@result) + 1)^ := PByte(@v)^;
end;
function swap32(const v): Cardinal;
begin
// FF, EE, DD, CC : CC->1, DD->2, EE->3, FF->4
PByte(@result)^ := PByte(IntPtr(@v) + 3)^;
PByte(IntPtr(@result) + 1)^ := PByte(IntPtr(@v) + 2)^;
PByte(IntPtr(@result) + 2)^ := PByte(IntPtr(@v) + 1)^;
PByte(IntPtr(@result) + 3)^ := PByte(@v)^;
end;
function swap64(const v): Int64;
begin
// FF, EE, DD, CC, BB, AA, 99, 88 : 88->1 ,99->2 ....
PByte(@result)^ := PByte(IntPtr(@v) + 7)^;
PByte(IntPtr(@result) + 1)^ := PByte(IntPtr(@v) + 6)^;
PByte(IntPtr(@result) + 2)^ := PByte(IntPtr(@v) + 5)^;
PByte(IntPtr(@result) + 3)^ := PByte(IntPtr(@v) + 4)^;
PByte(IntPtr(@result) + 4)^ := PByte(IntPtr(@v) + 3)^;
PByte(IntPtr(@result) + 5)^ := PByte(IntPtr(@v) + 2)^;
PByte(IntPtr(@result) + 6)^ := PByte(IntPtr(@v) + 1)^;
PByte(IntPtr(@result) + 7)^ := PByte(@v)^;
end;
// v and outVal is can't the same value
procedure swap64Ex(const v; out outVal);
begin
// FF, EE, DD, CC, BB, AA, 99, 88 : 88->1 ,99->2 ....
PByte(@outVal)^ := PByte(IntPtr(@v) + 7)^;
PByte(IntPtr(@outVal) + 1)^ := PByte(IntPtr(@v) + 6)^;
PByte(IntPtr(@outVal) + 2)^ := PByte(IntPtr(@v) + 5)^;
PByte(IntPtr(@outVal) + 3)^ := PByte(IntPtr(@v) + 4)^;
PByte(IntPtr(@outVal) + 4)^ := PByte(IntPtr(@v) + 3)^;
PByte(IntPtr(@outVal) + 5)^ := PByte(IntPtr(@v) + 2)^;
PByte(IntPtr(@outVal) + 6)^ := PByte(IntPtr(@v) + 1)^;
PByte(IntPtr(@outVal) + 7)^ := PByte(@v)^;
end;
// v and outVal is can't the same value
procedure swap32Ex(const v; out outVal);
begin
// FF, EE, DD, CC : CC->1, DD->2, EE->3, FF->4
PByte(@outVal)^ := PByte(IntPtr(@v) + 3)^;
PByte(IntPtr(@outVal) + 1)^ := PByte(IntPtr(@v) + 2)^;
PByte(IntPtr(@outVal) + 2)^ := PByte(IntPtr(@v) + 1)^;
PByte(IntPtr(@outVal) + 3)^ := PByte(@v)^;
end;
// v and outVal is can't the same value
procedure swap16Ex(const v; out outVal);
begin
// FF, EE : EE->1, FF->2
PByte(@outVal)^ := PByte(IntPtr(@v) + 1)^;
PByte(IntPtr(@outVal) + 1)^ := PByte(@v)^;
end;
// overload swap, result type is integer, because single maybe NaN
function swap(v:Single): Integer; overload;
begin
swap32Ex(v, Result);
end;
// overload swap
function swap(v:word): Word; overload;
begin
swap16Ex(v, Result);
end;
// overload swap
function swap(v:Cardinal):Cardinal; overload;
begin
swap32Ex(v, Result);
end;
// swap , result type is Int64, because Double maybe NaN
function swap(v:Double): Int64; overload;
begin
swap64Ex(v, Result);
end;
// copy from qstring
function BinToHex(p: Pointer; l: Integer; ALowerCase: Boolean): string;
const
B2HConvert: array [0 .. 15] of Char = ('0', '1', '2', '3', '4', '5', '6',
'7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F');
B2HConvertL: array [0 .. 15] of Char = ('0', '1', '2', '3', '4', '5', '6',
'7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
var
pd: PChar;
pb: PByte;
begin
if SizeOf(Char) = 2 then
begin
SetLength(Result, l shl 1);
end else
begin
SetLength(Result, l);
end;
pd := PChar(Result);
pb := p;
if ALowerCase then
begin
while l > 0 do
begin
pd^ := B2HConvertL[pb^ shr 4];
Inc(pd);
pd^ := B2HConvertL[pb^ and $0F];
Inc(pd);
Inc(pb);
Dec(l);
end;
end
else
begin
while l > 0 do
begin
pd^ := B2HConvert[pb^ shr 4];
Inc(pd);
pd^ := B2HConvert[pb^ and $0F];
Inc(pd);
Inc(pb);
Dec(l);
end;
end;
end;
function Utf8DecodeEx(pvValue:{$IFDEF UNICODE}TBytes{$ELSE}AnsiString{$ENDIF}; len:Cardinal):string;
{$IFDEF UNICODE}
var
lvBytes:TBytes;
{$ENDIF}
begin
{$IFDEF UNICODE}
lvBytes := TEncoding.Convert(TEncoding.UTF8, TEncoding.Unicode, pvValue);
SetLength(Result, Length(lvBytes) shr 1);
Move(lvBytes[0], PChar(Result)^, Length(lvBytes));
{$ELSE}
result:= UTF8Decode(pvValue);
{$ENDIF}
end;
function Utf8EncodeEx(pvValue:string):{$IFDEF UNICODE}TBytes{$ELSE}AnsiString{$ENDIF};
{$IFDEF UNICODE}
var
lvBytes:TBytes;
len:Cardinal;
{$ENDIF}
begin
{$IFDEF UNICODE}
len := length(pvValue) shl 1;
SetLength(lvBytes, len);
Move(PChar(pvValue)^, lvBytes[0], len);
Result := TEncoding.Convert(TEncoding.Unicode, TEncoding.UTF8, lvBytes);
{$ELSE}
result:= UTF8Encode(pvValue);
{$ENDIF}
end;
class function TDMsgPackHelper.ReadBinary(pvStream: TStream): TBytes;
var
lvByte:Byte;
l:Cardinal;
lvSavePosition:Int64;
begin
lvSavePosition := pvStream.Position;
pvStream.Read(lvByte, 1);
case lvByte of
$C0: // null
begin
SetLength(Result, 0);
end;
$C4: // 短二进制,最长255字节
begin
l := 0; // fill zero
pvStream.Read(l, 1);
SetLength(Result, l);
pvStream.Read(Result[0], l);
end;
$C5: // 二进制,16位,最长65535B
begin
l := 0; // fill zero
pvStream.Read(l, 2);
l := swap16(l);
SetLength(Result, l);
pvStream.Read(Result[0], l);
end;
$C6: // 二进制,32位,最长2^32-1
begin
l := 0; // fill zero
pvStream.Read(l, 4);
l := swap32(l);
SetLength(Result, l);
pvStream.Read(Result[0], l);
end;
else
begin
pvStream.Position := lvSavePosition;
raise Exception.CreateFmt(strErrorStringData, [lvByte]);
end;
end;
end;
class procedure TDMsgPackHelper.ReadBinary(pvSourceStream, pvDestStream:
TStream);
var
lvByte:Byte;
l:Cardinal;
lvSavePosition:Int64;
begin
lvSavePosition := pvSourceStream.Position;
pvSourceStream.Read(lvByte, 1);
case lvByte of
$C4: // 短二进制,最长255字节
begin
l := 0; // fill zero
pvSourceStream.Read(l, 1);
pvDestStream.CopyFrom(pvSourceStream, l);
end;
$C5: // 二进制,16位,最长65535B
begin
l := 0; // fill zero
pvSourceStream.Read(l, 2);
l := swap16(l);
pvDestStream.CopyFrom(pvSourceStream, l);
end;
$C6: // 二进制,32位,最长2^32-1
begin
l := 0; // fill zero
pvSourceStream.Read(l, 4);
l := swap32(l);
pvDestStream.CopyFrom(pvSourceStream, l);
end;
else
begin
pvSourceStream.Position := lvSavePosition;
raise Exception.CreateFmt(strErrorStringData, [lvByte]);
end;
end;
end;
class function TDMsgPackHelper.ReadFloat(pvStream:TStream): Double;
var
lvByte:Byte;
l:Cardinal;
lvSavePosition:Int64;
lvBData: array[0..15] of Byte;
lvSwapData: array[0..15] of Byte;
begin
lvSavePosition := pvStream.Position;
pvStream.Read(lvByte, 1);
case lvByte of
$C0: // null
begin
Result := 0;
end;
$CA: // float 32
begin
pvStream.Read(lvBData[0], 4);
swap32Ex(lvBData[0], lvSwapData[0]);
Result := PSingle(@lvSwapData[0])^;
end;
$cb: // Float 64
begin
pvStream.Read(lvBData[0], 8);
swap64Ex(lvBData[0], lvSwapData[0]);
Result := PDouble(@lvSwapData[0])^;
end;
else
begin
pvStream.Position := lvSavePosition;
raise Exception.CreateFmt(strErrorFloatData, [lvByte]);
end;
end;
end;
class function TDMsgPackHelper.ReadInt(pvStream:TStream): Int64;
var
lvByte:Byte;
l:Cardinal;
lvSavePosition, i64:Int64;
begin
lvSavePosition := pvStream.Position;
pvStream.Read(lvByte, 1);
if lvByte in [$00 .. $7F] then //positive fixint 0xxxxxxx 0x00 - 0x7f
begin
Result := lvByte;
end else if lvByte in [$E0 .. $FF] then
begin
// negative fixnum stores 5-bit negative integer
// +--------+
// |111YYYYY|
// +--------+
Result := Shortint(lvByte);
end else
begin
case lvByte of
$C0: // null
begin
Result := 0;
end;
$cc: // UInt8
begin
// uint 8 stores a 8-bit unsigned integer
// +--------+--------+
// | 0xcc |ZZZZZZZZ|
// +--------+--------+
l := 0;
pvStream.Read(l, 1);
Result := l;
end;
$cd:
begin
// uint 16 stores a 16-bit big-endian unsigned integer
// +--------+--------+--------+
// | 0xcd |ZZZZZZZZ|ZZZZZZZZ|
// +--------+--------+--------+
l := 0;
pvStream.Read(l, 2);
l := swap16(l);
Result := Word(l);
end;
$ce:
begin
// uint 32 stores a 32-bit big-endian unsigned integer
// +--------+--------+--------+--------+--------+
// | 0xce |ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ
// +--------+--------+--------+--------+--------+
l := 0;
pvStream.Read(l, 4);
l := swap32(l);
Result := Cardinal(l);
end;
$cf:
begin
// uint 64 stores a 64-bit big-endian unsigned integer
// +--------+--------+--------+--------+--------+--------+--------+--------+--------+
// | 0xcf |ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|
// +--------+--------+--------+--------+--------+--------+--------+--------+--------+
i64 := 0;
pvStream.Read(i64, 8);
i64 := swap64(i64);
Result :=UInt64(i64);
end;
$d0: //int 8
begin
// int 8 stores a 8-bit signed integer
// +--------+--------+
// | 0xd0 |ZZZZZZZZ|
// +--------+--------+
l := 0;
pvStream.Read(l, 1);
Result := ShortInt(l);
end;
$d1:
begin
// int 16 stores a 16-bit big-endian signed integer
// +--------+--------+--------+
// | 0xd1 |ZZZZZZZZ|ZZZZZZZZ|
// +--------+--------+--------+
l := 0;
pvStream.Read(l, 2);
l := swap16(l);
Result := SmallInt(l);
end;
$d2:
begin
// int 32 stores a 32-bit big-endian signed integer
// +--------+--------+--------+--------+--------+
// | 0xd2 |ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|
// +--------+--------+--------+--------+--------+
l := 0;
pvStream.Read(l, 4);
l := swap32(l);
Result := Integer(l);
end;
$d3:
begin
// int 64 stores a 64-bit big-endian signed integer
// +--------+--------+--------+--------+--------+--------+--------+--------+--------+
// | 0xd3 |ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|ZZZZZZZZ|
// +--------+--------+--------+--------+--------+--------+--------+--------+--------+
i64 := 0;
pvStream.Read(i64, 8);
i64 := swap64(i64);
Result := Int64(i64);
end;
else
begin
pvStream.Position := lvSavePosition;
raise Exception.CreateFmt(strErrorIntData, [lvByte]);
end;
end;
end;
end;
class function TDMsgPackHelper.ReadString(pvStream: TStream): String;
var
lvByte:Byte;
lvAnsiStr:{$IFDEF UNICODE}TBytes{$ELSE}AnsiString{$ENDIF};
l:Cardinal;
lvSavePosition:Int64;
begin
lvSavePosition := pvStream.Position;
pvStream.Read(lvByte, 1);
if lvByte in [$A0 .. $BF] then //fixstr 101xxxxx 0xa0 - 0xbf
begin
l := lvByte - $A0; // str len
if l > 0 then
begin
SetLength(lvAnsiStr, l);
pvStream.Read(PByte(lvAnsiStr)^, l);
Result :=UTF8DecodeEx(lvAnsiStr, l);
end else
begin
Result :='';
end;
end else
begin
case lvByte of
$C0: // null
begin
Result := '';
end;
$d9: //str 8 , 255
begin
// str 8 stores a byte array whose length is upto (2^8)-1 bytes:
// +--------+--------+========+
// | 0xd9 |YYYYYYYY| data |
// +--------+--------+========+
l := 0;
pvStream.Read(l, 1);
if l > 0 then // check is empty ele
begin
SetLength(lvAnsiStr, l);
pvStream.Read(PByte(lvAnsiStr)^, l);
Result :=UTF8DecodeEx(lvAnsiStr, l);
end else
begin
Result :='';
end;
end;
$da: // str 16
begin
// str 16 stores a byte array whose length is upto (2^16)-1 bytes:
// +--------+--------+--------+========+
// | 0xda |ZZZZZZZZ|ZZZZZZZZ| data |
// +--------+--------+--------+========+
l := 0; // fill zero
pvStream.Read(l, 2);
l := swap16(l);
if l > 0 then // check is empty ele
begin
SetLength(lvAnsiStr, l);
pvStream.Read(PByte(lvAnsiStr)^, l);
Result :=UTF8DecodeEx(lvAnsiStr, l);
end else
begin
Result :='';
end;
// SetLength(lvBytes, l + 1);
// lvBytes[l] := 0;
// pvStream.Read(lvBytes[0], l);
// setAsString(UTF8Decode(PAnsiChar(@lvBytes[0])));
end;
$db: // str 16
begin
// str 32 stores a byte array whose length is upto (2^32)-1 bytes:
// +--------+--------+--------+--------+--------+========+
// | 0xdb |AAAAAAAA|AAAAAAAA|AAAAAAAA|AAAAAAAA| data |
// +--------+--------+--------+--------+--------+========+
l := 0; // fill zero
pvStream.Read(l, 4);
l := swap32(l);
if l > 0 then // check is empty ele
begin
SetLength(lvAnsiStr, l);
pvStream.Read(PByte(lvAnsiStr)^, l);
Result :=UTF8DecodeEx(lvAnsiStr, l);
end else
begin
Result :='';
end;
end;
else
begin
pvStream.Position := lvSavePosition;
raise Exception.CreateFmt(strErrorBinaryData, [lvByte]);
end;
end;
end;
end;
class procedure TDMsgPackHelper.Write(pvStream: TStream; pvValue: string);
var
lvRawData:{$IFDEF UNICODE}TBytes{$ELSE}AnsiString{$ENDIF};
l:Integer;
lvValue:TMsgPackValue;
begin
lvRawData := Utf8EncodeEx(pvValue);
l:=Length(lvRawData);
//
//fixstr stores a byte array whose length is upto 31 bytes:
//+--------+========+
//|101XXXXX| data |
//+--------+========+
//
//str 8 stores a byte array whose length is upto (2^8)-1 bytes:
//+--------+--------+========+
//| 0xd9 |YYYYYYYY| data |
//+--------+--------+========+
//
//str 16 stores a byte array whose length is upto (2^16)-1 bytes:
//+--------+--------+--------+========+
//| 0xda |ZZZZZZZZ|ZZZZZZZZ| data |
//+--------+--------+--------+========+
//
//str 32 stores a byte array whose length is upto (2^32)-1 bytes:
//+--------+--------+--------+--------+--------+========+
//| 0xdb |AAAAAAAA|AAAAAAAA|AAAAAAAA|AAAAAAAA| data |
//+--------+--------+--------+--------+--------+========+
//
//where
//* XXXXX is a 5-bit unsigned integer which represents N
//* YYYYYYYY is a 8-bit unsigned integer which represents N
//* ZZZZZZZZ_ZZZZZZZZ is a 16-bit big-endian unsigned integer which represents N
//* AAAAAAAA_AAAAAAAA_AAAAAAAA_AAAAAAAA is a 32-bit big-endian unsigned integer which represents N
//* N is the length of data
if L<=31 then
begin
lvValue.ValueType:=$A0+Byte(L);
pvStream.WriteBuffer(lvValue.ValueType,1);
end
else if L<=255 then
begin
lvValue.ValueType:=$d9;
lvValue.U8Val:=Byte(L);
pvStream.WriteBuffer(lvValue,2);
end
else if L<=65535 then
begin
lvValue.ValueType:=$da;
lvValue.U16Val:=((L shr 8) and $FF) or ((L shl 8) and $FF00);
pvStream.Write(lvValue,3);
end else
begin
lvValue.ValueType:=$db;
lvValue.BArray[0]:=(L shr 24) and $FF;
lvValue.BArray[1]:=(L shr 16) and $FF;
lvValue.BArray[2]:=(L shr 8) and $FF;
lvValue.BArray[3]:=L and $FF;
pvStream.WriteBuffer(lvValue,5);
end;
pvStream.Write(PByte(lvRawData)^, l);
end;
class procedure TDMsgPackHelper.Write(pvStream: TStream; pvBuf:Pointer;
pvLen:Cardinal);
var
lvValue:TMsgPackValue;
begin
if pvLen <= 255 then
begin
lvValue.ValueType := $C4;
lvValue.U8Val := Byte(pvLen);
pvStream.WriteBuffer(lvValue, 2);
end
else if pvLen <= 65535 then
begin
lvValue.ValueType := $C5;
lvValue.BArray[0] := (pvLen shr 8) and $FF;
lvValue.BArray[1] := pvLen and $FF;
pvStream.WriteBuffer(lvValue, 3);
end
else
begin
lvValue.ValueType := $C6;
lvValue.BArray[0] := (pvLen shr 24) and $FF;
lvValue.BArray[1] := (pvLen shr 16) and $FF;
lvValue.BArray[2] := (pvLen shr 8) and $FF;
lvValue.BArray[3] := pvLen and $FF;
pvStream.WriteBuffer(lvValue, 5);
end;
if pvLen > 0 then
begin
pvStream.WriteBuffer(pvBuf^, pvLen);
end;
end;
class procedure TDMsgPackHelper.Write(pvStream: TStream; pvValue: Int64);
var
lvValue:TMsgPackValue;
begin
if pvValue>=0 then
begin
if pvValue<=127 then
begin
lvValue.U8Val:=Byte(pvValue);
pvStream.WriteBuffer(lvValue.U8Val,1);
end
else if pvValue<=255 then//UInt8
begin
lvValue.ValueType:=$cc;
lvValue.U8Val:=Byte(pvValue);
pvStream.WriteBuffer(lvValue,2);
end
else if pvValue<=65535 then
begin
lvValue.ValueType:=$cd;
lvValue.BArray[0]:=(pvValue shr 8);
lvValue.BArray[1]:=(pvValue and $FF);
pvStream.WriteBuffer(lvValue,3);
end
else if pvValue<=Cardinal($FFFFFFFF) then
begin
lvValue.ValueType:=$ce;
lvValue.BArray[0]:=(pvValue shr 24) and $FF;
lvValue.BArray[1]:=(pvValue shr 16) and $FF;
lvValue.BArray[2]:=(pvValue shr 8) and $FF;
lvValue.BArray[3]:=pvValue and $FF;
pvStream.WriteBuffer(lvValue,5);
end
else
begin
lvValue.ValueType:=$cf;
lvValue.BArray[0]:=(pvValue shr 56) and $FF;
lvValue.BArray[1]:=(pvValue shr 48) and $FF;
lvValue.BArray[2]:=(pvValue shr 40) and $FF;
lvValue.BArray[3]:=(pvValue shr 32) and $FF;
lvValue.BArray[4]:=(pvValue shr 24) and $FF;
lvValue.BArray[5]:=(pvValue shr 16) and $FF;
lvValue.BArray[6]:=(pvValue shr 8) and $FF;
lvValue.BArray[7]:=pvValue and $FF;
pvStream.WriteBuffer(lvValue,9);
end;
end
else//<0
begin
if pvValue<=Low(Integer) then //-2147483648 // 64 bit
begin
lvValue.ValueType:=$d3;
lvValue.BArray[0]:=(pvValue shr 56) and $FF;
lvValue.BArray[1]:=(pvValue shr 48) and $FF;
lvValue.BArray[2]:=(pvValue shr 40) and $FF;
lvValue.BArray[3]:=(pvValue shr 32) and $FF;
lvValue.BArray[4]:=(pvValue shr 24) and $FF;
lvValue.BArray[5]:=(pvValue shr 16) and $FF;
lvValue.BArray[6]:=(pvValue shr 8) and $FF;
lvValue.BArray[7]:=pvValue and $FF;
pvStream.WriteBuffer(lvValue,9);
end
else if pvValue<=Low(SmallInt) then // -32768 // 32 bit
begin
lvValue.ValueType:=$d2;
lvValue.BArray[0]:=(pvValue shr 24) and $FF;
lvValue.BArray[1]:=(pvValue shr 16) and $FF;
lvValue.BArray[2]:=(pvValue shr 8) and $FF;
lvValue.BArray[3]:=pvValue and $FF;
pvStream.WriteBuffer(lvValue,5);
end
else if pvValue<=-128 then
begin
lvValue.ValueType:=$d1;
lvValue.BArray[0]:=(pvValue shr 8);
lvValue.BArray[1]:=(pvValue and $FF);
pvStream.WriteBuffer(lvValue,3);
end
else if pvValue<-32 then
begin
lvValue.ValueType:=$d0;
lvValue.I8Val:=pvValue;
pvStream.WriteBuffer(lvValue,2);
end
else
begin
lvValue.I8Val:=pvValue;
pvStream.Write(lvValue.I8Val,1);
end;
end;//End <0
end;
class procedure TDMsgPackHelper.Write(pvStream: TStream; pvValue: Double);
var
lvValue:TMsgPackValue;
begin
lvValue.i64Val := swap(pvValue);
lvValue.ValueType := $CB;
pvStream.WriteBuffer(lvValue, 9);
end;
end.