-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPV_Streams.pas
557 lines (428 loc) · 10.5 KB
/
PV_Streams.pas
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
unit PV_Streams;
//Lazzy Image Viewer
//github.com/PascalVault
//License: MIT
interface
uses Classes, SysUtils, Dialogs;
type
{ TPV_Reader }
TPV_Reader = class
private
FStream: TStream;
FSize: Integer;
Buf: array of Byte;
FPos: Integer;
procedure SetOffset(Offset: Integer);
function GetOffset: Integer;
procedure SetAtLeast(Amount: Integer);
public
function GetU: Byte; inline;
function GetU2: Word; inline;
function GetU3: Cardinal; inline;
function GetU4: Cardinal; inline;
function GetMU2: Word; inline;
function GetMU4: Cardinal; inline;
function GetI: ShortInt; inline;
function GetI2: Smallint; inline;
function GetI4: LongInt; inline;
function GetMI2: Smallint; inline;
function GetMI4: LongInt; inline;
function GetF: Single; inline;
function GetMF: Single; inline; //Single
function GetV: Int64; inline; //variable-length integer
function GetLn(UntilCh: String = ''): String;
property Offset: Integer read GetOffset write SetOffset;
property AtLeast: Integer write SetAtLeast;
property Size: Integer read FSize;
function Get(var Buffer; Count: Longint): Longint;
function GetC: Char; inline;
function GetNum: Integer;
function GetWhite: String;
function GetS(Count: Integer = -1): String;
procedure Skip(Count: Integer);
constructor Create(Str: TStream; Length: Integer = -1);
end;
{ TPV_Writer }
TPV_Writer = class
private
FStream: TStream;
Buf: array of Byte;
FPos: Integer;
FSize: Integer;
public
procedure Flush;
procedure Put(const Buffer; Count: Integer);
procedure PutU(V: Byte); inline;
procedure PutU2(V: Word); inline;
procedure PutU4(V: Cardinal); inline;
procedure PutMU2(V: Word); inline;
procedure PutMU4(V: Cardinal); inline;
procedure PutI(V: ShortInt); inline;
procedure PutI2(V: Smallint); inline;
procedure PutI4(V: LongInt); inline;
procedure PutMI2(V: Smallint); inline;
procedure PutMI4(V: LongInt); inline;
procedure PutMF(V: Single); inline;
procedure PutF(V: Single); inline;
procedure PutV(V: Word); inline;
procedure Skip(Len: Integer);
procedure PutS(S: String); inline;
procedure CopyFrom(Str: TStream; Count: Integer);
constructor Create(Str: TStream);
destructor Destroy; override;
end;
function Getbits(Val: Word; Index, Count: Integer): Word;
implementation
function Getbits(Val: Word; Index, Count: Integer): Word;
var Res: Word;
begin
Res := Val shr Index;
case Count of
0: Result := 0;
1: Result := Res and 1;
2: Result := Res and 3;
3: Result := Res and 7;
4: Result := Res and 15;
5: Result := Res and 31;
6: Result := Res and 63;
7: Result := Res and 127;
8: Result := Res and 255;
9: Result := Res and 511;
10: Result := Res and 1023;
11: Result := Res and 2047;
12: Result := Res and 4095;
13: Result := Res and 8191;
14: Result := Res and 16383;
15: Result := Res and 32767;
end;
end;
{ TPV_Reader }
procedure TPV_Reader.SetOffset(Offset: Integer);
begin
FPos := Offset;
end;
function TPV_Reader.GetOffset: Integer;
begin
Result := FPos;
end;
procedure TPV_Reader.SetAtLeast(Amount: Integer);
begin
if FSize-FPos < Amount then SetLength(Buf, FPos+Amount);
end;
function TPV_Reader.GetU: Byte;
begin
Result := Buf[FPos];
Inc(FPos);
end;
function TPV_Reader.GetU2: Word;
begin
Move(Buf[FPos], Result, 2);
Inc(FPos, 2);
end;
function TPV_Reader.GetU3: Cardinal;
begin
Move(Buf[FPos], Result, 3);
Inc(FPos, 3);
end;
function TPV_Reader.GetU4: Cardinal;
begin
Move(Buf[FPos], Result, 4);
Inc(FPos, 4);
end;
function TPV_Reader.GetMU2: Word;
begin
Move(Buf[FPos], Result, 2);
Result := SwapEndian(Result);
Inc(FPos, 2);
end;
function TPV_Reader.GetMU4: Cardinal;
begin
Move(Buf[FPos], Result, 4);
Result := SwapEndian(Result);
Inc(FPos, 4);
end;
function TPV_Reader.GetI: ShortInt;
begin
Move(Buf[FPos], Result, 1);
Inc(FPos);
end;
function TPV_Reader.GetI2: Smallint;
begin
Move(Buf[FPos], Result, 2);
Inc(FPos, 2);
end;
function TPV_Reader.GetI4: LongInt;
begin
Move(Buf[FPos], Result, 4);
Inc(FPos, 4);
end;
function TPV_Reader.GetMI2: Smallint;
begin
Move(Buf[FPos], Result, 2);
Result := SwapEndian(Result);
Inc(FPos, 2);
end;
function TPV_Reader.GetMI4: LongInt;
begin
Move(Buf[FPos], Result, 4);
Result := SwapEndian(Result);
Inc(FPos, 4);
end;
function TPV_Reader.GetF: Single;
var Temp: Cardinal absolute Result;
begin
Move(Buf[FPos], Temp, 4);
Inc(FPos, 4);
end;
function TPV_Reader.GetMF: Single;
var Temp: Cardinal absolute Result;
begin
Move(Buf[FPos], Temp, 4);
Temp := SwapEndian(Temp);
Inc(FPos, 4);
end;
function TPV_Reader.GetV: Int64;
var i: Integer;
Val,Cont: Byte;
V: Byte;
begin
Result := 0;
while True do begin
V := Buf[FPos];
Inc(FPos);
Cont := V shr 7;
Val := V and $7F;
Result := (Result shl 7) + Val;
if Cont=0 then Exit;
end;
end;
function TPV_Reader.GetLn(UntilCh: String): String;
var A,B: Integer;
Count: Integer;
i: Integer;
begin
if UntilCh = '' then begin
Count := 10000;
if Count > FSize-FPos then Count := FSize-FPos;
SetLength(Result, Count);
Move(Buf[FPos], Result[1], Count);
A := 0;
for i:=1 to Length(Result) do
if (Result[i] = #13) or (Result[i] = #10) then begin
A := i;
break;
end;
Result := Copy(Result, 1, A-1);
Inc(FPos, A);
Exit;
end;
Count := 10000;
if Count > FSize-FPos then Count := FSize-FPos;
SetLength(Result, Count);
Move(Buf[FPos], Result[1], Count);
A := Pos(UntilCh, Result);
Result := Copy(Result, 1, A-1);
Inc(FPos, A);
Exit;
end;
function TPV_Reader.GetC: Char;
begin
Result := chr(Buf[FPos]);
Inc(FPos);
end;
function TPV_Reader.Get(var Buffer; Count: Longint): Longint;
var Count2: Integer;
i: Integer;
begin
Count2 := FSize-FPos;
if Count2 < Count then Count := Count2;
Move(Buf[FPos], Buffer, Count);
Result := Count;
Inc(FPos, Count);
end;
function TPV_Reader.GetNum: Integer;
var Res: String;
begin
Res := '';
while FPos < FSize do begin
if Buf[FPos] in [48..57] then Res := Res + chr(Buf[FPos])
else break;
Inc(FPos);
end;
Result := StrToInt64Def(Res, 0);
end;
function TPV_Reader.GetWhite: String;
begin
Result := '';
while FPos < FSize do begin
if Buf[FPos] in [32,13,10,09] then Result := Result + chr(Buf[FPos])
else break;
Inc(FPos);
end;
end;
function TPV_Reader.GetS(Count: Integer): String;
begin
if Count = -1 then Count := FSize;
SetLength(Result, Count);
Move(Buf[FPos], Result[1], Count);
Inc(FPos, Count);
end;
procedure TPV_Reader.Skip(Count: Integer);
begin
Inc(FPos, Count);
end;
constructor TPV_Reader.Create(Str: TStream; Length: Integer);
begin
FStream := Str;
if Length = -1 then FSize := Str.Size
else FSize := Length;
SetLength(Buf, FSize);
Str.Read(Buf[0], FSize);
FPos := 0;
end;
{ TPV_Writer }
procedure TPV_Writer.Flush;
begin
if FPos < 1 then Exit;
FStream.Write(Buf[0], FPos);
FPos := 0;
end;
procedure TPV_Writer.Put(const Buffer; Count: Integer);
begin
if FPos+Count > FSize then Flush;
Move(Buffer, Buf[FPos], Count);
Inc(FPos, Count);
end;
procedure TPV_Writer.PutU(V: Byte);
begin
if FPos+1 > FSize then Flush;
Buf[FPos] := V;
Inc(FPos);
end;
procedure TPV_Writer.PutU2(V: Word);
begin
if FPos+2 > FSize then Flush;
Move(V, Buf[FPos], 2);
Inc(FPos, 2);
end;
procedure TPV_Writer.PutU4(V: Cardinal);
begin
if FPos+4 > FSize then Flush;
Move(V, Buf[FPos], 4);
Inc(FPos, 4);
end;
procedure TPV_Writer.PutMU2(V: Word);
begin
if FPos+2 > FSize then Flush;
V := SwapEndian(V);
Move(V, Buf[FPos], 2);
Inc(FPos, 2);
end;
procedure TPV_Writer.PutMU4(V: Cardinal);
begin
if FPos+4 > FSize then Flush;
V := SwapEndian(V);
Move(V, Buf[FPos], 4);
Inc(FPos, 4);
end;
procedure TPV_Writer.PutI(V: ShortInt);
begin
if FPos+1 > FSize then Flush;
Move(V, Buf[FPos], 1);
Inc(FPos, 1);
end;
procedure TPV_Writer.PutI2(V: Smallint);
begin
if FPos+2 > FSize then Flush;
Move(V, Buf[FPos], 2);
Inc(FPos, 2);
end;
procedure TPV_Writer.PutI4(V: LongInt);
begin
if FPos+4 > FSize then Flush;
Move(V, Buf[FPos], 4);
Inc(FPos, 4);
end;
procedure TPV_Writer.PutMI2(V: Smallint);
begin
if FPos+2 > FSize then Flush;
V := SwapEndian(V);
Move(V, Buf[FPos], 2);
Inc(FPos, 2);
end;
procedure TPV_Writer.PutMI4(V: LongInt);
begin
if FPos+4 > FSize then Flush;
V := SwapEndian(V);
Move(V, Buf[FPos], 4);
Inc(FPos, 4);
end;
procedure TPV_Writer.PutMF(V: Single);
var VV: Cardinal;
begin
if FPos+4 > FSize then Flush;
Move(V, VV, 4);
VV := SwapEndian(VV);
Move(VV, Buf[FPos], 4);
Inc(FPos, 4);
end;
procedure TPV_Writer.PutF(V: Single);
var VV: Cardinal;
begin
if FPos+4 > FSize then Flush;
Move(V, VV, 4);
Move(VV, Buf[FPos], 4);
Inc(FPos, 4);
end;
procedure TPV_Writer.PutV(V: Word);
var A,B: Byte;
begin
//not really variable-length
A := (V shr 7) + $80;
B := (V and $7F);
PutU(A);
PutU(B);
end;
procedure TPV_Writer.Skip(Len: Integer);
var i: Integer;
begin
for i:=0 to Len-1 do
PutU(0);
end;
procedure TPV_Writer.PutS(S: String);
var Len: Integer;
begin
Len := Length(S);
if FPos+Len > FSize then Flush;
Move(S[1], Buf[FPos], Len);
Inc(FPos, Len);
end;
procedure TPV_Writer.CopyFrom(Str: TStream; Count: Integer);
var Buff: array of Byte;
BuffSize: Integer;
Len: Integer;
begin
Flush;
FPos := 0;
BuffSize := 40960;
if BuffSize > Count then BuffSize := Count;
SetLength(Buff, BuffSize);
while Count >0 do begin
Len := Str.Read(Buff[0], BuffSize);
FStream.Write(Buff[0], Len);
Dec(Count, Len);
end;
end;
constructor TPV_Writer.Create(Str: TStream);
begin
FStream := Str;
FPos := 0;
FSize := 409600;
SetLength(Buf, FSize);
end;
destructor TPV_Writer.Destroy;
begin
Flush;
inherited Destroy;
end;
end.