-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMain.cpp
More file actions
626 lines (564 loc) · 21.2 KB
/
Main.cpp
File metadata and controls
626 lines (564 loc) · 21.2 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
#include <windows.h>
#include <zlib.h>
#include "ncbind.hpp"
#define COPY_BUFFER_SIZE (1024*1024)
#define COMPRESS_BUFFER_SIZE (1024*1024)
// XP3ArchiveAttractFilter_v2
typedef void (__stdcall *FilterProc)(tjs_uint32 hash, tjs_uint64 offset, void * buffer, long bufferlen);
class BinaryStream
{
iTJSBinaryStream *stream;
ttstr storage;
int mode;
HMODULE filterDLL;
bool hasCallback;
tTJSVariantClosure callback;
template <typename T>
void _swap(T *p, int cnt) {
for (; cnt > 0; --cnt,p+=2) {
T sw = p[0];
p[0] = p[1];
p[1] = sw;
}
}
class CopyCommand {
BinaryStream *owner;
ttstr name;
tjs_uint rbufsize;
tjs_int64 rcount, wcount;
tjs_uint32 adler;
tjs_int64 inputOffset, inputMaxsize;
FilterProc filterProc;
tjs_uint32 filterValue;
TVP_md5_state_t md5;
bool noCopy, calcMD5;
public:
CopyCommand(BinaryStream *owner, tjs_char const *name)
: owner(owner), name(name),
rbufsize(COPY_BUFFER_SIZE),
rcount(0), wcount(0),
inputOffset(0), inputMaxsize(0),
filterProc(0), filterValue(0),
noCopy(false), calcMD5(false)
{
adler = ::adler32(0L, Z_NULL, 0);
}
virtual ~CopyCommand() {}
inline void calcSum(tjs_uint8 const *buf, tjs_uint len) {
adler = ::adler32(adler, buf, len);
}
void filter(tjs_int64 ofs, tjs_uint8 *buf, tjs_uint len) {
if (filterProc && len > 0) filterProc(filterValue, (tjs_uint64)ofs, buf, len);
}
tjs_error execute(tTJSVariant *r, tjs_int n, tTJSVariant **p) {
if (n < 1) return TJS_E_BADPARAMCOUNT;
ttstr storage = *p[0];
bool proc = false;
if (n >= 2) {
ncbPropAccessor elm(*p[1]);
if (elm.IsValid()) {
detailOptionLoad(elm);
proc = true;
main(storage);
detailOptionSave(elm);
}
}
if (!proc) main(storage);
if (r) *r = noCopy ? 0 : wcount;
return TJS_S_OK;
}
private:
void main(ttstr file) {
iTJSBinaryStream *in = TVPCreateStream(file, TJS_BS_READ);
if (!in) error(file, TJS_W(": storage not found"));
try {
if (inputOffset > 0) {
in->Seek(inputOffset, TJS_BS_SEEK_SET);
}
tjs_uint8 *buf = new tjs_uint8[rbufsize];
try {
tjs_int64 remain = inputMaxsize;
wcount += beforeCopy();
while (true) {
tjs_uint len = rbufsize;
if (remain > 0 && remain < len) len = (tjs_uint)remain;
len = owner->streamRead(in, buf, len);
if (!len) break;
wcount += doCopy(rcount, buf, len);
rcount += len;
if (owner->progress(file, rcount)) break;
if (remain > 0) {
if (remain <= len) break;
remain -= len;
}
}
wcount += afterCopy();
} catch (...) {
delete[] buf;
throw;
}
delete[] buf;
} catch (...) {
in->Destruct();
throw;
}
in->Destruct();
}
protected:
void error(ttstr const &msg, tjs_char const *tag = 0) { owner->error((tag ? (name + tag) : name) + (msg.length() > 0 ? TJS_W(": ") + msg : TJS_W(""))); }
virtual void detailOptionLoad(ncbPropAccessor &elm) {
inputOffset = elm.GetValue(TJS_W("offset"), ncbTypedefs::Tag<tjs_int64>());
inputMaxsize = elm.GetValue(TJS_W("length"), ncbTypedefs::Tag<tjs_int64>());
filterProc = 0;
{ // filterProc取得
tTJSVariantType type = tvtVoid;
const tjs_char *key = TJS_W("filter");
if (elm.HasValue(key, 0, &type)) {
switch (type) {
case tvtVoid: break;
case tvtString: filterProc = owner->getFilterProc( elm.GetValue(key, ncbTypedefs::Tag<ttstr >())); break;
case tvtInteger: filterProc = reinterpret_cast<FilterProc>(elm.GetValue(key, ncbTypedefs::Tag<tTVInteger>())); break;
default:
error(TJS_W("invalid filter type"));
break;
}
}
}
filterValue = elm.getIntValue(TJS_W("fparam"));
noCopy = !!elm.getIntValue(TJS_W("nocopy"));
calcMD5 = !!elm.getIntValue(TJS_W("md5"));
if (calcMD5) TVP_md5_init(&md5);
}
virtual void detailOptionSave(ncbPropAccessor &elm) {
elm.SetValue(TJS_W("read"), rcount);
elm.SetValue(TJS_W("hash"), adler);
if (calcMD5) {
tjs_uint8 digest[16];
TVP_md5_finish(&md5, digest);
tTJSVariantOctet *oct = TJSAllocVariantOctet(digest, 16);
tTJSVariant voct;
voct = oct;
elm.SetValue(TJS_W("digest"), voct);
oct->Release();
}
}
tjs_int64 write(tjs_uint8 *buf, tjs_uint len) {
onWrite(wcount, buf, len);
return noCopy ? len : owner->streamWrite(owner->stream, buf, len);
}
virtual void onWrite(tjs_int64 ofs, tjs_uint8 *buf, tjs_uint len) {
if (calcMD5) TVP_md5_append(&md5, buf, len);
}
virtual tjs_int64 doCopy(tjs_int64 ofs, tjs_uint8 *buf, tjs_uint len) {
calcSum(buf, len);
filter(ofs, buf, len);
return write(buf, len);
}
virtual tjs_int64 beforeCopy() { return 0; }
virtual tjs_int64 afterCopy() { return 0; }
};
class ZlibCommand : public CopyCommand {
bool initialized;
tjs_uint8 *zbuf;
tjs_uint zlen, wbufsize;
z_stream z;
public:
ZlibCommand(BinaryStream *owner, tjs_char const *name) : CopyCommand(owner, name), initialized(false), zbuf(0), zlen(0), wbufsize(COMPRESS_BUFFER_SIZE) {}
virtual ~ZlibCommand() { finish(true); }
protected:
void setup() {
initialized = true;
::memset(&z, 0, sizeof(z));
if (zinit(&z) != Z_OK) error(z.msg, TJS_W(": setup"));
zlen = wbufsize;
zbuf = new tjs_uint8[zlen];
oprepare();
}
void finish(bool ignoreError = false) {
if (zbuf) delete[] zbuf;
zbuf = 0;
if (initialized && zdone(&z) != Z_OK && !ignoreError) error(z.msg, TJS_W(": finish"));
initialized = false;
}
void iprepare(tjs_uint8 *buf, tjs_uint len) { z.next_in = buf; z.avail_in = len; }
void oprepare() { z.next_out = zbuf; z.avail_out = zlen; }
tjs_int64 flush() {
tjs_int64 r = (zlen - z.avail_out > 0) ? write(zbuf, zlen - z.avail_out) : 0;
oprepare();
return r;
}
tjs_int64 procedure(int flag = Z_NO_FLUSH) {
tjs_int64 out = 0;
do {
switch (zmain(&z, flag)) {
case Z_OK:
if (z.avail_out == 0) out += flush();
break;
case Z_STREAM_END:
out += flush();
break;
default:
error(z.msg, TJS_W(": procedure"));
break;
}
} while (z.avail_in > 0);
return out;
}
virtual int zinit(z_stream *z) = 0;
virtual int zmain(z_stream *z, int flag) = 0;
virtual int zdone(z_stream *z) = 0;
virtual tjs_int64 beforeCopy() { setup(); return 0; }
virtual tjs_int64 afterCopy() { tjs_int64 r = procedure(Z_FINISH); finish(); return r;}
};
class CompressCommand : public ZlibCommand {
int complv;
public:
CompressCommand(BinaryStream *owner, tjs_char const *name) : ZlibCommand(owner, name), complv(Z_BEST_COMPRESSION) {}
protected:
virtual int zinit(z_stream *z) { return ::deflateInit(z, complv); }
virtual int zmain(z_stream *z, int flag) { return ::deflate(z, flag); }
virtual int zdone(z_stream *z) { return ::deflateEnd(z); }
virtual tjs_int64 doCopy(tjs_int64 ofs, tjs_uint8 *buf, tjs_uint len) {
calcSum(buf, len);
filter(ofs, buf, len);
iprepare(buf, len);
return procedure();
}
virtual void detailOptionLoad(ncbPropAccessor &elm) {
ZlibCommand::detailOptionLoad(elm);
complv = elm.getIntValue(TJS_W("comp_lv"), complv);
}
};
class DecompressCommand : public ZlibCommand {
public:
DecompressCommand(BinaryStream *owner, tjs_char const *name) : ZlibCommand(owner, name) {}
protected:
virtual int zinit(z_stream *z) { return ::inflateInit(z); }
virtual int zmain(z_stream *z, int flag) { return ::inflate(z, flag); }
virtual int zdone(z_stream *z) { return ::inflateEnd(z); }
virtual tjs_int64 doCopy(tjs_int64 ofs, tjs_uint8 *buf, tjs_uint len) {
iprepare(buf, len);
return procedure();
}
virtual void onWrite(tjs_int64 ofs, tjs_uint8 *buf, tjs_uint len) {
filter(ofs, buf, len);
calcSum(buf, len);
ZlibCommand::onWrite(ofs, buf, len);
}
};
//--------------------------------------------------------------
public:
BinaryStream() : stream(0), storage(), mode(-1), filterDLL(0), hasCallback(false), callback(0,0) {}
virtual ~BinaryStream() {
resetCopyFilter();
close();
}
static tjs_error TJS_INTF_METHOD Factory(BinaryStream **inst, tjs_int n, tTJSVariant **p, iTJSDispatch2*) {
if (inst) {
BinaryStream *self = *inst = new BinaryStream();
if (n >= 1) {
tjs_int mode = (n >= 2) ? (tjs_int)*p[1] : TJS_BS_READ;
try {
self->open(p[0]->GetString(), mode);
} catch (...) {
delete self;
*inst = 0;
throw;
}
}
}
return TJS_S_OK;
}
/**
* ストレージを開く
* @param storage 対象ストレージ
* @param mode モード指定(bsRead, bsWrite, bsAppend, bsUpdate)
*/
void open(tjs_char const *storage, int mode) {
close();
stream = TVPCreateStream(storage, mode & TJS_BS_ACCESS_MASK);
if (stream) {
this->storage = storage;
this->mode = mode;
} else {
error(ttstr(TJS_W("cannot open : ")) + storage);
}
}
/**
* オープン中のストレージを閉じる
*/
void close() {
if (stream) {
storage = TJS_W("");
mode = -1;
stream->Destruct();
stream = 0;
}
}
/**
* ストリームのポジションを変更する
* @param pos 位置
* @param whence 基準位置(bsSeekSet, bsSeekCur, bsSeekEnd)
* @return 移動後の位置
*/
tjs_int64 seek(tjs_int64 pos, int whence) {
if (whence < TJS_BS_SEEK_SET || whence > TJS_BS_SEEK_END) {
error(TJS_W("invalid whence value."));
}
return (tjs_int64)stream->Seek(pos, whence);
}
/**
* ストリームの現在のポジションを取得する
* @return 位置
*/
tjs_int64 tell() { return seek(0, TJS_BS_SEEK_CUR); }
/**
* 現在開いているストレージ/モードを取得
*/
int getMode() const { return mode; }
ttstr getStorage() const { return storage; }
//--------------------------------------------------------------
/**
* 指定バイト読み込む
* @param size 読み込むサイズ(バイト)
* @return 読み込んだデータ(octet形式) 終端などで読めなかった場合はvoid(length=0のoctetではないので注意)
*/
static tjs_error TJS_INTF_METHOD read(tTJSVariant *r, tjs_int n, tTJSVariant **p, BinaryStream *self) {
if (n < 1) return TJS_E_BADPARAMCOUNT;
if (!self) return TJS_E_NATIVECLASSCRASH;
tTJSVariantOctet *oct = self->_read(p[0]->AsInteger());
if (oct) {
if (r) *r = oct;
oct->Release();
} else {
if (r) r->Clear();
}
return TJS_S_OK;
}
tTJSVariantOctet* _read(tjs_int64 size) {
tjs_uint32 sz = (tjs_uint32)size;
if (size <= 0) return 0;
if (size != (tjs_int64)sz) error(TJS_W("too large read size."));
tTJSVariantOctet *ret = 0;
tjs_uint8 *data = 0;
try {
data = new tjs_uint8[sz];
tjs_uint read = streamRead(stream, data, sz);
if (read > 0) ret = TJSAllocVariantOctet(data, read);
} catch (...) {
if (data) delete[] data;
if (ret) ret->Release();
throw;
}
delete[] data;
return ret;
}
/**
* {1,2,4,8}バイトを数値として読み込む
* @return 読み込んだ数値 / 終端などで読めなかった場合はvoid
* ※voidを返すのは1byteも読めなかった場合のみ
* 1byte以上指定byte未満しか読めなかった場合は足りない部分を0で埋めた値が返されるので注意
*/
static tjs_error TJS_INTF_METHOD readI8 (tTJSVariant *r, tjs_int n, tTJSVariant **p, BinaryStream *self) { return self ? self->_readByte(r) : TJS_E_NATIVECLASSCRASH; }
static tjs_error TJS_INTF_METHOD readI16LE(tTJSVariant *r, tjs_int n, tTJSVariant **p, BinaryStream *self) { return self ? self->_readInt(r,2,0) : TJS_E_NATIVECLASSCRASH; }
static tjs_error TJS_INTF_METHOD readI32LE(tTJSVariant *r, tjs_int n, tTJSVariant **p, BinaryStream *self) { return self ? self->_readInt(r,4,0) : TJS_E_NATIVECLASSCRASH; }
static tjs_error TJS_INTF_METHOD readI64LE(tTJSVariant *r, tjs_int n, tTJSVariant **p, BinaryStream *self) { return self ? self->_readInt(r,8,0) : TJS_E_NATIVECLASSCRASH; }
static tjs_error TJS_INTF_METHOD readI16BE(tTJSVariant *r, tjs_int n, tTJSVariant **p, BinaryStream *self) { return self ? self->_readInt(r,2,1) : TJS_E_NATIVECLASSCRASH; }
static tjs_error TJS_INTF_METHOD readI32BE(tTJSVariant *r, tjs_int n, tTJSVariant **p, BinaryStream *self) { return self ? self->_readInt(r,4,1) : TJS_E_NATIVECLASSCRASH; }
static tjs_error TJS_INTF_METHOD readI64BE(tTJSVariant *r, tjs_int n, tTJSVariant **p, BinaryStream *self) { return self ? self->_readInt(r,8,1) : TJS_E_NATIVECLASSCRASH; }
tjs_error _readByte(tTJSVariant *r) {
tjs_uint8 num = 0;
tjs_uint read = streamRead(stream, &num, 1);
if (r) {
if (read) *r = (tjs_int64)num;
else r->Clear();
}
return TJS_S_OK;
}
tjs_error _readInt(tTJSVariant *r, int byte, int bigendian) {
tjs_int64 num = 0;
tjs_uint8 buf[8] = { 0,0,0,0,0,0,0,0 };
tjs_uint read = streamRead(stream, buf, byte);
if (read) {
if (bigendian) switch (byte) {
case 8: _swap((tjs_uint32*)buf, byte>>3);
case 4: _swap((tjs_uint16*)buf, byte>>2);
case 2: _swap((tjs_uint8 *)buf, byte>>1);
}
if (r) *r = *(tjs_int64*)buf;
} else {
if (r) r->Clear();
}
return TJS_S_OK;
}
//--------------------------------------------------------------
/**
* データを書き込む
* @param data 書き込むデータ(octet形式)
* @return 書き込んだバイト数
*/
static tjs_error TJS_INTF_METHOD write(tTJSVariant *r, tjs_int n, tTJSVariant **p, BinaryStream *self) {
if (n < 1) return TJS_E_BADPARAMCOUNT;
if (!self) return TJS_E_NATIVECLASSCRASH;
tjs_uint write = 0;
switch (p[0]->Type()) {
case tvtString: {
tTJSVariantString *str = p[0]->AsStringNoAddRef();
if (str) {
tjs_int len = str->GetLength();
if (len >= 0) {
len = (len+1)*2;
write = self->_write((tjs_uint8 const*)str->operator const tjs_char*(), len);
}
}
} break;
case tvtOctet: {
tTJSVariantOctet *oct = p[0]->AsOctetNoAddRef();
if (oct) write = self->_write(oct->GetData(), oct->GetLength());
} break;
default:
error(TJS_W("invalid data type."));
}
if (r) *r = (tjs_int64)write;
return TJS_S_OK;
}
tjs_uint _write(tjs_uint8 const *data, tjs_uint length) {
return streamWrite(stream, data, length);
}
/**
* {1,2,4,8}バイトで数値を書き込む
* @param num 書き込む数値
* @return 書き込んだバイト数
*/
static tjs_error TJS_INTF_METHOD writeI8 (tTJSVariant *r, tjs_int n, tTJSVariant **p, BinaryStream *self) { return self ? self->_writeByte(r,p[0]) : TJS_E_NATIVECLASSCRASH; }
static tjs_error TJS_INTF_METHOD writeI16LE(tTJSVariant *r, tjs_int n, tTJSVariant **p, BinaryStream *self) { return self ? self->_writeInt(r,p[0],2,0) : TJS_E_NATIVECLASSCRASH; }
static tjs_error TJS_INTF_METHOD writeI32LE(tTJSVariant *r, tjs_int n, tTJSVariant **p, BinaryStream *self) { return self ? self->_writeInt(r,p[0],4,0) : TJS_E_NATIVECLASSCRASH; }
static tjs_error TJS_INTF_METHOD writeI64LE(tTJSVariant *r, tjs_int n, tTJSVariant **p, BinaryStream *self) { return self ? self->_writeInt(r,p[0],8,0) : TJS_E_NATIVECLASSCRASH; }
static tjs_error TJS_INTF_METHOD writeI16BE(tTJSVariant *r, tjs_int n, tTJSVariant **p, BinaryStream *self) { return self ? self->_writeInt(r,p[0],2,1) : TJS_E_NATIVECLASSCRASH; }
static tjs_error TJS_INTF_METHOD writeI32BE(tTJSVariant *r, tjs_int n, tTJSVariant **p, BinaryStream *self) { return self ? self->_writeInt(r,p[0],4,1) : TJS_E_NATIVECLASSCRASH; }
static tjs_error TJS_INTF_METHOD writeI64BE(tTJSVariant *r, tjs_int n, tTJSVariant **p, BinaryStream *self) { return self ? self->_writeInt(r,p[0],8,1) : TJS_E_NATIVECLASSCRASH; }
tjs_error _writeByte(tTJSVariant *r, tTJSVariant *p) {
tjs_uint8 num = (tjs_uint8)(p->AsInteger());
tjs_uint write = streamWrite(stream, &num, 1);
if (r) *r = (tjs_int64)write;
return TJS_S_OK;
}
tjs_error _writeInt(tTJSVariant *r, tTJSVariant *p, int byte, int bigendian) {
tjs_int64 num = p->AsInteger();
if (bigendian) {
tjs_uint8 buf[8];
*(tjs_int64*)buf = num;
switch (byte) {
case 8: _swap((tjs_uint32*)buf, byte>>3);
case 4: _swap((tjs_uint16*)buf, byte>>2);
case 2: _swap((tjs_uint8 *)buf, byte>>1);
}
num = *(tjs_int64*)buf;
}
tjs_uint write = streamWrite(stream, (tjs_uint8*)&num, byte);
if (r) *r = (tjs_int64)write;
return TJS_S_OK;
}
//--------------------------------------------------------------
static tjs_error TJS_INTF_METHOD copy(tTJSVariant *r, tjs_int n, tTJSVariant **p, BinaryStream *self) {
if (!self) return TJS_E_NATIVECLASSCRASH;
CopyCommand cmd(self, TJS_W("copy"));
return cmd.execute(r, n, p);
}
static tjs_error TJS_INTF_METHOD compress(tTJSVariant *r, tjs_int n, tTJSVariant **p, BinaryStream *self) {
if (!self) return TJS_E_NATIVECLASSCRASH;
CompressCommand cmd(self, TJS_W("compress"));
return cmd.execute(r, n, p);
}
static tjs_error TJS_INTF_METHOD decompress(tTJSVariant *r, tjs_int n, tTJSVariant **p, BinaryStream *self) {
if (!self) return TJS_E_NATIVECLASSCRASH;
DecompressCommand cmd(self, TJS_W("decompress"));
return cmd.execute(r, n, p);
}
void setProgressCallback(tTJSVariant cb) {
hasCallback = (cb.Type() == tvtObject && cb.AsObjectNoAddRef() != 0);
callback = hasCallback ? cb.AsObjectClosureNoAddRef() : tTJSVariantClosure(0, 0);
}
bool progress(ttstr const &file, tjs_int64 read) {
if (!hasCallback) return false;
tTJSVariant result, vf = file, vr = read;
tTJSVariant *p[] = { &vf, &vr };
return TJS_SUCCEEDED(callback.FuncCall(0, NULL, NULL, &result, 2, p, NULL)) && result.operator bool();
}
void setFilter(tjs_char const *dll) {
resetCopyFilter();
if (!dll || dll[0] == 0) return;
ttstr file = TVPGetPlacedPath(dll);
TVPGetLocalName(file);
filterDLL = ::LoadLibraryW((const wchar_t*)file.c_str());
if (!filterDLL) error(ttstr(TJS_W("filter DLL not found: ")) + dll);
}
FilterProc getFilterProc(ttstr const &proc) {
if (!filterDLL || !proc.length()) return 0;
tjs_nchar tmp[256] = {0};
proc.ToNarrowStr(tmp, sizeof(tmp));
tmp[sizeof(tmp)-1] = 0;
FilterProc r = (FilterProc)::GetProcAddress(filterDLL, tmp);
if (!r) error(ttstr(TJS_W("filter proc not found: ")) + proc);
return r;
}
static void error(ttstr const &message) {
ttstr concat = ttstr(TJS_W("BinaryStream: ")) + message;
TVPThrowExceptionMessage(concat.c_str());
}
protected:
void resetCopyFilter() {
if (filterDLL) ::FreeLibrary(filterDLL);
/**/filterDLL = 0;
}
static inline tjs_uint streamRead (iTJSBinaryStream *s, tjs_uint8 *buf, tjs_uint len) {
if (!s) error(TJS_W("stream not opened."));
if (!len) return 0;
return s->Read(buf, len);
}
static inline tjs_uint streamWrite(iTJSBinaryStream *s, tjs_uint8 const *buf, tjs_uint len) {
if (!s) error(TJS_W("stream not opened."));
if (!len) return 0;
tjs_uint written = s->Write(buf, len);
if (written != len) error(TJS_W("write failed."));
return written;
}
};
NCB_REGISTER_CLASS(BinaryStream)
{
Factory(&Class::Factory);
Method(TJS_W("open"), &Class::open);
Method(TJS_W("close"), &Class::close);
Method(TJS_W("seek"), &Class::seek);
Method(TJS_W("tell"), &Class::tell);
Property(TJS_W("storage"), &Class::getStorage, (int)0);
Property(TJS_W("mode"), &Class::getMode, (int)0);
RawCallback(TJS_W("read"), &Class::read, 0);
RawCallback(TJS_W("write"), &Class::write, 0);
RawCallback(TJS_W("copy"), &Class::copy, 0);
RawCallback(TJS_W("compress"), &Class::compress, 0);
RawCallback(TJS_W("decompress"), &Class::decompress, 0);
Method(TJS_W("setProgressCallback"), &Class::setProgressCallback);
Method(TJS_W("setFilter"), &Class::setFilter);
RawCallback(TJS_W("readI8"), &Class::readI8, 0);
RawCallback(TJS_W("readI16LE"), &Class::readI16LE, 0);
RawCallback(TJS_W("readI32LE"), &Class::readI32LE, 0);
RawCallback(TJS_W("readI64LE"), &Class::readI64LE, 0);
RawCallback(TJS_W("readI16BE"), &Class::readI16BE, 0);
RawCallback(TJS_W("readI32BE"), &Class::readI32BE, 0);
RawCallback(TJS_W("readI64BE"), &Class::readI64BE, 0);
RawCallback(TJS_W("writeI8"), &Class::writeI8, 0);
RawCallback(TJS_W("writeI16LE"), &Class::writeI16LE, 0);
RawCallback(TJS_W("writeI32LE"), &Class::writeI32LE, 0);
RawCallback(TJS_W("writeI64LE"), &Class::writeI64LE, 0);
RawCallback(TJS_W("writeI16BE"), &Class::writeI16BE, 0);
RawCallback(TJS_W("writeI32BE"), &Class::writeI32BE, 0);
RawCallback(TJS_W("writeI64BE"), &Class::writeI64BE, 0);
Variant(TJS_W("bsRead"), (tjs_int)TJS_BS_READ, 0);
Variant(TJS_W("bsWrite"), (tjs_int)TJS_BS_WRITE, 0);
Variant(TJS_W("bsAppend"), (tjs_int)TJS_BS_APPEND, 0);
Variant(TJS_W("bsUpdate"), (tjs_int)TJS_BS_UPDATE, 0);
Variant(TJS_W("bsSeekSet"), (tjs_int)TJS_BS_SEEK_SET, 0);
Variant(TJS_W("bsSeekCur"), (tjs_int)TJS_BS_SEEK_CUR, 0);
Variant(TJS_W("bsSeekEnd"), (tjs_int)TJS_BS_SEEK_END, 0);
}