forked from floft/sprouts
-
Notifications
You must be signed in to change notification settings - Fork 3
/
stream.h~
669 lines (647 loc) · 15.3 KB
/
stream.h~
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
#ifndef STREAM_H
#define STREAM_H
#include <cstdint>
#include <stdexcept>
#include <cwchar>
#include <string>
#include <cstdio>
#include <cstring>
#include <memory>
#include <list>
#include "util.h"
#include "dimension.h"
#ifdef DEBUG_STREAM
#include <iostream>
#define DUMP_V(fn, v) do {cerr << #fn << ": read " << v << endl;} while(false)
#else
#define DUMP_V(fn, v)
#endif
using namespace std;
class IOException : public runtime_error
{
public:
explicit IOException(const string & msg)
: runtime_error(msg)
{
}
explicit IOException(exception * e, bool deleteIt = true)
: runtime_error((dynamic_cast<IOException *>(e) == nullptr) ? string("IO Error : ") + e->what() : string(e->what()))
{
if(deleteIt)
delete e;
}
explicit IOException(exception & e, bool deleteIt = false)
: IOException(&e, deleteIt)
{
}
};
class EOFException final : public IOException
{
public:
explicit EOFException()
: IOException("IO Error : reached end of file")
{
}
};
class NoStreamsLeftException final : public IOException
{
public:
explicit NoStreamsLeftException()
: IOException("IO Error : no streams left")
{
}
};
class UTFDataFormatException final : public IOException
{
public:
explicit UTFDataFormatException()
: IOException("IO Error : invalid UTF data")
{
}
};
class InvalidDataValueException final : public IOException
{
public:
explicit InvalidDataValueException(string msg)
: IOException(msg)
{
}
};
class Reader
{
private:
template <typename T>
static T limitAfterRead(T v, T min, T max)
{
if(v < min || v > max)
{
throw InvalidDataValueException("read value out of range : " + to_string(v));
}
return v;
}
public:
Reader()
{
}
Reader(const Reader &) = delete;
const Reader & operator =(const Reader &) = delete;
virtual ~Reader()
{
}
virtual uint8_t readByte() = 0;
void readBytes(uint8_t * array, size_t count)
{
for(size_t i = 0; i < count; i++)
{
array[i] = readByte();
}
}
uint8_t readU8()
{
uint8_t retval = readByte();
DUMP_V(readU8, (unsigned)retval);
return retval;
}
int8_t readS8()
{
int8_t retval = readByte();
DUMP_V(readS8, (int)retval);
return retval;
}
uint16_t readU16()
{
uint16_t v = readU8();
uint16_t retval = (v << 8) | readU8();
DUMP_V(readU16, retval);
return retval;
}
int16_t readS16()
{
int16_t retval = readU16();
DUMP_V(readS16, retval);
return retval;
}
uint32_t readU32()
{
uint32_t v = readU16();
uint32_t retval = (v << 16) | readU16();
DUMP_V(readU32, retval);
return retval;
}
int32_t readS32()
{
int32_t retval = readU32();
DUMP_V(readS32, retval);
return retval;
}
uint64_t readU64()
{
uint64_t v = readU32();
uint64_t retval = (v << 32) | readU32();
DUMP_V(readU64, retval);
return retval;
}
int64_t readS64()
{
int64_t retval = readU64();
DUMP_V(readS64, retval);
return retval;
}
float readF32()
{
static_assert(sizeof(float) == sizeof(uint32_t), "float is not 32 bits");
union
{
uint32_t ival;
float fval;
};
ival = readU32();
float retval = fval;
DUMP_V(readF32, retval);
return retval;
}
double readF64()
{
static_assert(sizeof(double) == sizeof(uint64_t), "double is not 64 bits");
union
{
uint64_t ival;
double fval;
};
ival = readU64();
double retval = fval;
DUMP_V(readF64, retval);
return retval;
}
bool readBool()
{
return readU8() != 0;
}
wstring readString()
{
wstring retval = L"";
for(;;)
{
uint32_t b1 = readU8();
if(b1 == 0)
{
DUMP_V(readString, "\"" + wcsrtombs(retval) + "\"");
return retval;
}
else if((b1 & 0x80) == 0)
{
retval += (wchar_t)b1;
}
else if((b1 & 0xE0) == 0xC0)
{
uint32_t b2 = readU8();
if((b2 & 0xC0) != 0x80)
throw UTFDataFormatException();
uint32_t v = b2 & 0x3F;
v |= (b1 & 0x1F) << 6;
retval += (wchar_t)v;
}
else if((b1 & 0xF0) == 0xE0)
{
uint32_t b2 = readU8();
if((b2 & 0xC0) != 0x80)
throw UTFDataFormatException();
uint32_t b3 = readU8();
if((b3 & 0xC0) != 0x80)
throw UTFDataFormatException();
uint32_t v = b3 & 0x3F;
v |= (b2 & 0x3F) << 6;
v |= (b1 & 0xF) << 12;
retval += (wchar_t)v;
}
else if((b1 & 0xF8) == 0xF0)
{
uint32_t b2 = readU8();
if((b2 & 0xC0) != 0x80)
throw UTFDataFormatException();
uint32_t b3 = readU8();
if((b3 & 0xC0) != 0x80)
throw UTFDataFormatException();
uint32_t b4 = readU8();
if((b4 & 0xC0) != 0x80)
throw UTFDataFormatException();
uint32_t v = b4 & 0x3F;
v |= (b3 & 0x3F) << 6;
v |= (b2 & 0x3F) << 12;
v |= (b1 & 0x7) << 18;
if(v >= 0x10FFFF)
throw UTFDataFormatException();
retval += (wchar_t)v;
}
else
throw UTFDataFormatException();
}
}
uint8_t readLimitedU8(uint8_t min, uint8_t max)
{
return limitAfterRead(readU8(), min, max);
}
int8_t readLimitedS8(int8_t min, int8_t max)
{
return limitAfterRead(readS8(), min, max);
}
uint16_t readLimitedU16(uint16_t min, uint16_t max)
{
return limitAfterRead(readU16(), min, max);
}
int16_t readLimitedS16(int16_t min, int16_t max)
{
return limitAfterRead(readS16(), min, max);
}
uint32_t readLimitedU32(uint32_t min, uint32_t max)
{
return limitAfterRead(readU32(), min, max);
}
int32_t readLimitedS32(int32_t min, int32_t max)
{
return limitAfterRead(readS32(), min, max);
}
uint64_t readLimitedU64(uint64_t min, uint64_t max)
{
return limitAfterRead(readU64(), min, max);
}
int64_t readLimitedS64(int64_t min, int64_t max)
{
return limitAfterRead(readS64(), min, max);
}
float readFiniteF32()
{
float retval = readF32();
if(!isfinite(retval))
{
throw InvalidDataValueException("read value is not finite");
}
return retval;
}
double readFiniteF64()
{
double retval = readF64();
if(!isfinite(retval))
{
throw InvalidDataValueException("read value is not finite");
}
return retval;
}
float readLimitedF32(float min, float max)
{
return limitAfterRead(readFiniteF32(), min, max);
}
double readLimitedF64(double min, double max)
{
return limitAfterRead(readFiniteF64(), min, max);
}
Dimension readDimension()
{
return (Dimension)readLimitedU8(0, (uint8_t)Dimension::Last - 1);
}
};
class Writer
{
public:
Writer()
{
}
Writer(const Writer &) = delete;
const Writer & operator =(const Writer &) = delete;
virtual ~Writer()
{
}
virtual void writeByte(uint8_t) = 0;
virtual void flush()
{
}
void writeBytes(const uint8_t * array, size_t count)
{
for(size_t i = 0; i < count; i++)
writeByte(array[i]);
}
void writeU8(uint8_t v)
{
writeByte(v);
}
void writeS8(int8_t v)
{
writeByte(v);
}
void writeU16(uint16_t v)
{
writeU8((uint8_t)(v >> 8));
writeU8((uint8_t)(v & 0xFF));
}
void writeS16(int16_t v)
{
writeU16(v);
}
void writeU32(uint32_t v)
{
writeU16((uint16_t)(v >> 16));
writeU16((uint16_t)(v & 0xFFFF));
}
void writeS32(int32_t v)
{
writeU32(v);
}
void writeU64(uint64_t v)
{
writeU32((uint64_t)(v >> 32));
writeU32((uint64_t)(v & 0xFFFFFFFFU));
}
void writeS64(int64_t v)
{
writeU64(v);
}
void writeF32(float v)
{
static_assert(sizeof(float) == sizeof(uint32_t), "float is not 32 bits");
union
{
uint32_t ival;
float fval;
};
fval = v;
writeU32(ival);
}
void writeF64(double v)
{
static_assert(sizeof(double) == sizeof(uint64_t), "double is not 64 bits");
union
{
uint64_t ival;
double fval;
};
fval = v;
writeU64(ival);
}
void writeBool(bool v)
{
writeU8(v ? 1 : 0);
}
void writeString(wstring v)
{
for(size_t i = 0; i < v.length(); i++)
{
uint32_t ch = v[i];
if(ch != 0 && ch < 0x80)
{
writeU8(ch);
}
else if(ch < 0x800)
{
writeU8(0xC0 | ((ch >> 6) & 0x1F));
writeU8(0x80 | ((ch) & 0x3F));
}
else if(ch < 0x1000)
{
writeU8(0xE0 | ((ch >> 12) & 0xF));
writeU8(0x80 | ((ch >> 6) & 0x3F));
writeU8(0x80 | ((ch) & 0x3F));
}
else
{
writeU8(0xF0 | ((ch >> 18) & 0x7));
writeU8(0x80 | ((ch >> 12) & 0x3F));
writeU8(0x80 | ((ch >> 6) & 0x3F));
writeU8(0x80 | ((ch) & 0x3F));
}
}
writeU8(0);
}
void writeDimension(Dimension v)
{
writeU8((uint8_t)v);
}
};
class FileReader final : public Reader
{
private:
FILE * f;
public:
FileReader(wstring fileName)
{
string str = wcsrtombs(fileName);
f = fopen(str.c_str(), "rb");
if(f == nullptr)
throw IOException(string("IO Error : ") + strerror(errno));
}
explicit FileReader(FILE * f)
: f(f)
{
assert(f != nullptr);
}
virtual ~FileReader()
{
fclose(f);
}
virtual uint8_t readByte() override
{
int ch = fgetc(f);
if(ch == EOF)
{
if(ferror(f))
throw IOException("IO Error : can't read from file");
throw EOFException();
}
return ch;
}
};
class FileWriter final : public Writer
{
private:
FILE * f;
public:
FileWriter(wstring fileName)
{
string str = wcsrtombs(fileName);
f = fopen(str.c_str(), "wb");
if(f == nullptr)
throw IOException(string("IO Error : ") + strerror(errno));
}
explicit FileWriter(FILE * f)
: f(f)
{
assert(f != nullptr);
}
virtual ~FileWriter()
{
fclose(f);
}
virtual void writeByte(uint8_t v) override
{
if(fputc(v, f) == EOF)
throw IOException("IO Error : can't write to file");
}
virtual void flush() override
{
if(EOF == fflush(f))
throw IOException("IO Error : can't write to file");
}
};
class MemoryReader final : public Reader
{
private:
const shared_ptr<const uint8_t> mem;
size_t offset;
const size_t length;
public:
explicit MemoryReader(shared_ptr<const uint8_t> mem, size_t length)
: mem(mem), offset(0), length(length)
{
}
template <size_t length>
explicit MemoryReader(const uint8_t a[length])
: MemoryReader(shared_ptr<const uint8_t>(&a[0], [](const uint8_t *){}))
{
}
virtual uint8_t readByte() override
{
if(offset >= length)
throw EOFException();
return mem.get()[offset++];
}
};
class StreamPipe final
{
StreamPipe(const StreamPipe &) = delete;
const StreamPipe & operator =(const StreamPipe &) = delete;
private:
shared_ptr<Reader> readerInternal;
shared_ptr<Writer> writerInternal;
public:
StreamPipe();
Reader & reader()
{
return *readerInternal;
}
Writer & writer()
{
return *writerInternal;
}
shared_ptr<Reader> preader()
{
return readerInternal;
}
shared_ptr<Writer> pwriter()
{
return writerInternal;
}
};
class DumpingReader final : public Reader
{
private:
Reader &reader;
public:
DumpingReader(Reader& reader)
: reader(reader)
{
}
virtual uint8_t readByte() override;
};
struct StreamRW
{
StreamRW()
{
}
StreamRW(const StreamRW &) = delete;
const StreamRW & operator =(const StreamRW &) = delete;
virtual ~StreamRW()
{
}
Reader & reader()
{
return *preader();
}
Writer & writer()
{
return *pwriter();
}
virtual shared_ptr<Reader> preader() = 0;
virtual shared_ptr<Writer> pwriter() = 0;
};
class StreamRWWrapper final : public StreamRW
{
private:
shared_ptr<Reader> preaderInternal;
shared_ptr<Writer> pwriterInternal;
public:
StreamRWWrapper(shared_ptr<Reader> preaderInternal, shared_ptr<Writer> pwriterInternal)
: preaderInternal(preaderInternal), pwriterInternal(pwriterInternal)
{
}
virtual shared_ptr<Reader> preader() override
{
return preaderInternal;
}
virtual shared_ptr<Writer> pwriter() override
{
return pwriterInternal;
}
};
class StreamBidirectionalPipe final
{
private:
StreamPipe pipe1, pipe2;
shared_ptr<StreamRW> port1Internal, port2Internal;
public:
StreamBidirectionalPipe()
{
port1Internal = shared_ptr<StreamRW>(new StreamRWWrapper(pipe1.preader(), pipe2.pwriter()));
port2Internal = shared_ptr<StreamRW>(new StreamRWWrapper(pipe2.preader(), pipe1.pwriter()));
}
shared_ptr<StreamRW> pport1()
{
return port1Internal;
}
shared_ptr<StreamRW> pport2()
{
return port2Internal;
}
StreamRW & port1()
{
return *port1Internal;
}
StreamRW & port2()
{
return *port2Internal;
}
};
struct StreamServer
{
StreamServer()
{
}
StreamServer(const StreamServer &) = delete;
const StreamServer & operator =(const StreamServer &) = delete;
virtual ~StreamServer()
{
}
virtual shared_ptr<StreamRW> accept() = 0;
};
class StreamServerWrapper final : public StreamServer
{
private:
list<shared_ptr<StreamRW>> streams;
shared_ptr<StreamServer> nextServer;
public:
StreamServerWrapper(list<shared_ptr<StreamRW>> streams, shared_ptr<StreamServer> nextServer = nullptr)
: streams(streams), nextServer(nextServer)
{
}
virtual shared_ptr<StreamRW> accept() override
{
if(streams.empty())
{
if(nextServer == nullptr)
throw NoStreamsLeftException();
return nextServer->accept();
}
shared_ptr<StreamRW> retval = streams.front();
streams.pop_front();
return retval;
}
};
#endif // STREAM_H