summaryrefslogtreecommitdiff
path: root/lib/Handle.cc
blob: 210186d77b777513ab72f54b1f94e940a8415569 (plain)
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
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
/*
 *  Baltisot
 *  Copyright (C) 1999-2008 Nicolas "Pixel" Noble
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#ifdef HAVE_UCL
#include <ucl/ucl.h>
#endif

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "generic.h"

#if defined HAVE_BYTESWAP_H
#include <byteswap.h>
#else
inline Uint16 bswap_16(Uint16 w) {
    return (w >> 8) | (w << 8);
}

inline Uint32 bswap_32(Uint32 w) {
    return (w >> 24) | ((w >> 8) & 0x0000ff00) | ((w << 8) & 0x00ff0000) | (w << 24);
}

inline Uint64 bswap_64(Uint64 w) {
    union {
        Uint64 v;
        struct { Uint32 v1, v2; };
    } x;
    Uint32 v1, v2;
    x.v = w;
    v1 = bswap_32(x.v1);
    v2 = bswap_32(x.v2);
    x.v1 = v2;
    x.v2 = v1;
    return x.v;
}
#endif

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#else
#include <io.h>
#endif

#if !defined(_WIN32) && !defined(__MIPSEL__)
#include <sys/mman.h>
#endif	      
	      
#include "Handle.h"
#include "Atomic.h"
#include "gettext.h"

enum {
    DEFLATE,
    INFLATE
};

int Handle::nb_handles = 0;

Handle::Handle(const Handle & nh) : itell(0), hFile(0), h(nh.h >= 0 ? nh.ndup() : nh.h), closed(nh.closed), nonblock(nh.closed), zfile(0), z(0), hMapObject(0), mapped(0)
{
#ifdef DEBUG
    printm(M_INFO, String(_("Duplication of handle ")) + nh.h + _(" to ") + h + "\n");
#endif
    if ((h >= 0) && (nh.z)) {
	SetZ(nh.z);
    }
    Atomic::Increment(&nb_handles);
}

Handle::~Handle() {
#ifdef DEBUG
    printm(M_INFO, String(_("Destroying handle ")) + h + "\n");
#endif
    close();
    Atomic::Decrement(&nb_handles);
}

Handle::Handle(int nh) : itell(0), h(nh), closed(false), nonblock(false), zfile(0), z(0), hMapObject(0), mapped(0)
{
#ifdef DEBUG
    printm(M_INFO, String(_("Initialising handle ")) + h + "\n");
#endif
    Atomic::Increment(&nb_handles);
}

int Handle::GetHandle() {
    return h;
}

void * Handle::GetHFile() {
    return hFile;
}

int Handle::GetHandle() const {
    return h;
}

ssize_t Handle::write(const void *cbuf, size_t count) throw (GeneralException) {
    ssize_t r, tr = 0;
    bool done, full = false;
    const char * buf = (const char *)cbuf;

    if (closed) {
        throw IOGeneral("Unable to write: handle `" + GetName() + "' is closed.");
    }
    
    if (!count)
	return 0;

    do {
	done = true;
	errno = 0;
        if ((r = uwrite(buf, count)) < 0) {
    	    if ((!errno) || (errno == EAGAIN) || (errno == EINTR)) {
		// Avant de déclarer une erreur, on vérifie si ce n'est pas un
		// problème lié au fait qu'il n'y a plus de place libre. Cela peut
		// arriver si l'on agit sur un pipe ou un handle. Nous
		// attendons encore une fois avant de déclarer l'erreur,
		// grace au drapeau full.
		if (full) {
		    throw IOException(GetName(), IO_WRITE, count);
		} else {
		    done = false;
		    full = true;
		    if (nonblock) {
#ifdef DEBUG
			printm(M_INFO, _("write: throwing IOAgain for handle ") + GetName() + "\n");
#endif
			throw IOAgain();
		    } else {
#ifdef HAVE_SLEEP
			sleep(1);
#endif
		    }
		}
	    } else {
		throw IOException(GetName(), IO_WRITE, count);
	    }
	} else if (((size_t) r) != count) {
	    if (nonblock) {
		return r;
	    }
	    full = done = false;
	    buf += r;
	    tr += r;
	}
    } while (!done);

    return r + tr;
}

ssize_t Handle::read(void *buf, size_t count) throw (GeneralException) {
    ssize_t r;

    if (closed) {
        throw IOGeneral("Unable to read: handle `" + GetName() + "' is closed.");
    }
    
    if (!count)
	return 0;

#ifdef FULLDEBUG
    printm(M_INFO, String(_("read: reading ")) + count + _(" bytes from handle ") + GetHandle() + "\n");
#endif
    
    errno = 0;
    while ((r = uread(buf, count)) < 0) {
	if ((!errno) || (errno == EAGAIN) || (errno == EINTR)) {
	    // Avant de déclarer une erreur, on vérifie si ce n'est pas un
	    // problème lié au fait qu'il n'y a plus d'octets.
	    if (nonblock) {
#ifdef DEBUG
		printm(M_INFO, _("read: throwing IOAgain for handle ") + GetName() + "\n");
#endif
		throw IOAgain();
	    }
	} else {
	    throw IOException(GetName(), IO_READ, count);
	}
    }
    
    if (!r) {
	close();
    }

    return r;
}

bool Handle::IsClosed(void) const {
    return closed;
}

bool Handle::IsNonBlock(void) const {
    return nonblock;
}

void Handle::SetNonBlock(void) {
#ifdef HAVE_FCNTL
    if ((h >= 0) || !nonblock) {
	fcntl(h, F_SETFL, O_NONBLOCK);
    }
    nonblock = true;
#elif defined _WIN32
    // HACKY HACKY HACK!!
    // Let's think windows will behave correctly on a non-socket here...
    if (!nonblock) {
        u_long iMode = 1;
        ioctlsocket(h, FIONBIO, &iMode);
    }
    nonblock = true;
#endif
}

Handle & operator<<(Handle & h, const String & s) {
    const char * p;
    
    p = s.to_charp();
    h.write(p, s.strlen());
    
    return h;
}

Handle & operator>>(Handle & h, String & s) throw (GeneralException) {
    char t[BUFSIZ];
    int i = 0, r;
    bool got_ioagain = false;

    try {
        while ((r = h.read(&(t[i]), 1)) && (i != (BUFSIZ - 1))) {
	    // Il y a souvent des \r\n dans les sockets par exemple,
	    // ou bien en lisant des fichiers au format MS-DOS. On
	    // ignore le \r pour ne garder que le \n, standard sous Unix.
	    if (t[i] == '\r') {
	        continue;
	    }
	    if (t[i] == '\n') {
	        break;
	    } else {
	        i++;
	    }
        }
    }
    catch (IOAgain e) {
        got_ioagain = true;
    }
    
    t[i] = '\0';
    s = t;
    if (got_ioagain)
        throw IOAgain();
    return h;
}

void Handle::tagclose() {
    closed = 1;
    h = -1;
}

void Handle::close() throw (GeneralException) {
    if (IsClosed()) {
	return;
    }
    
    Flush();
    
    if (h >= 0) {
	if (z >= 10) {
	    int err;
	    if (c == DEFLATE) {
		err = deflateEnd(&zstrm);
		if (err != Z_OK) {
		    throw GeneralException(String(_("Error during deflateEnd: ")) + zstrm.msg);
		}
	    } else {
		err = inflateEnd(&zstrm);
		if (err != Z_OK) {
		    throw GeneralException(String(_("Error during inflateEnd: ")) + zstrm.msg);
		}
	    }
	    err = nclose();
	    if (err) {
		throw GeneralException(String(_("Error during (zstream) close: ")) + strerror(errno));
	    }
	} else if (z) {
#ifdef DEBUG
	    printm(M_INFO, String(_("Performing gzclose on handle ")) + h + "\n");
#endif
	    int err = gzclose(zfile);
#ifdef DEBUG
	    printm(M_INFO, String(_("gzclose returned ")) + err + "\n");
#endif
	    if (err) {
		if (err == Z_ERRNO) {
		    throw GeneralException(String(_("Error during close: ")) + strerror(errno));
		} else {
		    throw GeneralException(String(_("Error in zlib during gzclose: ")) + gzerror(zfile, &err));
		}
	    }
	} else {
#ifndef NO_HFILE
            if (!hFile) {
#endif
	        int err = nclose();
	        if (err) {
		    throw GeneralException(String(_("Error during close: ")) + strerror(errno));
	        }
#ifndef NO_HFILE
            }
#endif
	}
#if defined (_WIN32) && !defined (NO_HFILE)
        if (hFile) {
            _close(GetHandle());
	    hFile = 0;
        }
#endif
    }

    if (mapped) {
	munmap();
    }
    
    h = -1;
    closed = 1;
}

bool Handle::CanRead(void) const {
    return false;
}

bool Handle::CanWrite(void) const {
    return false;
}

String Handle::GetName(void) const {
    return _("Bare Handle - should not happend");
}

ssize_t Handle::GetSize(void) const {
    return -1;
}

time_t Handle::GetModif(void) const {
    return -1;
}

bool Handle::CanWatch(void) const {
    return true;
}

int Handle::Dup() const throw (GeneralException) {
    int d;
    
    if ((d = dup(h)) < 0) {
	throw IOGeneral(String(_("Error dupping file `")) + GetName() + _("' (handle ") + h + "): " + strerror(errno) + " (" + errno + ")");
    }
    return d;
}

void Handle::SetZ(int az) throw (GeneralException) {
    if (z) {
	throw GeneralException(_("Can't SetZ a Handle twice."));
    }
    if (h < 0) {
	throw GeneralException(_("Can't SetZ a virtual Handle."));
    }
    if (az >= 10) {
#ifdef DEBUG
	printm(M_INFO, _("Setting up zstream using inflate/deflate...\n"));
#endif
	int err;
	zstrm.zalloc = Z_NULL;
	zstrm.zfree = Z_NULL;
	if (CanWrite()) {
	    c = DEFLATE;
	    err = deflateInit(&zstrm, az - 10);
	    if (err != Z_OK) {
		throw GeneralException(String(_("Error in deflateInit: ")) + zstrm.msg);
	    }
	} else {
	    c = INFLATE;
	    zstrm.next_in = 0;
	    zstrm.avail_in = 0;
	    err = inflateInit(&zstrm);
	    if (err != Z_OK) {
		throw GeneralException(String(_("Error in inflateInit: ")) + zstrm.msg);
	    }
	}
    } else if (az) {
	char format[5];
	int index = 0;
	if (CanRead()) {
	    format[index++] = 'r';
	}
	if (CanWrite()) {
	    format[index++] = 'w';
	}
	format[index++] = (char) (az + '0');
	format[index++] = 'b';
	format[index] = 0;
#ifdef FULLDEBUG
	printm(M_INFO, String(_("Performing gzdopen on handle ")) + h + _(" with mode \"") + format + "\"\n");
#endif
	if (!(zfile = gzdopen(h, format))) {
	    throw GeneralException(String(_("Was not able to gzdopen: ")) + strerror(errno));
	}
	z = az;
    }
}

ssize_t Handle::uwrite(const void * buf, size_t count) throw (GeneralException) {
    if (z >= 10) {
    } else if (z) {
#ifdef FULLDEBUG
	printm(M_INFO, String(_("Performing gzwrite of ")) + count + _(" byte(s) for handle ") + h + "\n");
#endif
#ifdef HAVE_WD_ZLIB
	int err = gzwrite(zfile, buf, count);
#else
	int err = gzwrite(zfile, (char *) buf, count);
#endif
	if (err == 0) {
	    const char * m = gzerror(zfile, &err);
	    if (err == Z_ERRNO) {
		return -1;
	    } else {
		throw GeneralException(String(_("Error in zlib during gzwrite: ")) + m);
	    }
	}
	itell += err;
	return err;
    } else {
	itell += count = nwrite(buf, count);
	return count;
    }
}

ssize_t Handle::uread(void * buf, size_t count) {
    if (z >= 10) {
    } if (z) {
#ifdef DEBUG
	printm(M_BARE, String(_("Performing gzread of ")) + count + _(" byte(s) for handle ") + h + "\n");
#endif
        ssize_t msize = GetSize();
        if ((msize >= 0) && ((count + itell) >= msize))
            count = msize - itell;
	int err = gzread(zfile, buf, count);
	if (err == -1) {
            throw GeneralException(String("Error reading zstream: ") + gzerror(zfile, &err));
	    if (err == Z_ERRNO) {
		return -1;
	    } else {
		return 0;
	    }
	}
	itell += err;
	return err;
    } else {
#if defined (_WIN32) && !defined (NO_HFILE)
        if (hFile) {
            DWORD rcount;
            if (!ReadFile(hFile, buf, count, &rcount, 0)) {
                // Error..
            }
            itell += count = rcount;
        } else
#endif
	itell += count = nread(buf, count);
	return count;
    }
}

off_t Handle::tell() const {
    if (z) {
	return gztell(zfile);
    } else {
	return itell;
    }
}

bool Handle::CanSeek() const {
    return 0;
}

off_t Handle::seek(off_t offset, int whence) throw(GeneralException) {
    if (z) {
        ssize_t msize = GetSize();
        if (msize >= 0) {
            switch (whence) {
            case SEEK_CUR:
                offset += itell;
                break;
            case SEEK_END:
                offset += msize;
                break;
            }
            return itell = gzseek(zfile, offset, SEEK_SET);
        } else {
            if (whence == SEEK_END) {
                throw IOGeneral("zlib can't seek from the end on a stream.");
            }
            return itell = gzseek(zfile, offset, whence);
	}
    } else {
	throw IOGeneral(_("Handle ") + GetName() + _(" can't seek"));
    }
}

Uint8 Handle::readU8() {
    Uint8 r = 0;
    read(&r, 1);
    return r;
}

int8 Handle::read8() {
    int8 r = 0;
    read(&r, 1);
    return r;
}

Uint16 Handle::readU16() {
    Uint16 r = 0;
    read(&r, 2);
#ifdef WORDS_BIGENDIAN
    return bswap_16(r);
#else
    return r;
#endif
}

int16 Handle::read16() {
    int16 r = 0;
    read(&r, 2);
#ifdef WORDS_BIGENDIAN
    return (int16)bswap_16((Uint16)r);
#else
    return r;
#endif
}

Uint32 Handle::readU32() {
    Uint32 r = 0;
    read(&r, 4);
#ifdef WORDS_BIGENDIAN
    return bswap_32(r);
#else
    return r;
#endif
}

int32 Handle::read32() {
    int32 r = 0;
    read(&r, 4);
#ifdef WORDS_BIGENDIAN
    return (int32)bswap_32((Uint32)r);
#else
    return r;
#endif
}

Uint64 Handle::readU64() {
    Uint64 r = 0;
    read(&r, 8);
#ifdef WORDS_BIGENDIAN
    return bswap_64(r);
#else
    return r;
#endif
}

int64 Handle::read64() {
    int64 r = 0;
    read(&r, 8);
#ifdef WORDS_BIGENDIAN
    return (int64)bswap_64((Uint64)r);
#else
    return r;
#endif
}

float Handle::readFloat() {
    float r = 0;
    read(&r, 4);
#ifdef WORDS_BIGENDIAN
    Uint32 t = bswap_32(*((Uint32 *)&r);
    return *((float *)&t);
#else
    return r;
#endif
}

double Handle::readDouble() {
    double r = 0;
    read(&r, 8);
#ifdef WORDS_BIGENDIAN
    Uint64 t = bswap_64(*((Uint64 *)&r);
    return *((double *)t);
#else
    return r;
#endif
}

void Handle::writeU8(Uint8 v) {
    write(&v, 1);
}

void Handle::write8(int8 v) {
    write(&v, 1);
}

void Handle::writeU16(Uint16 v) {
#ifdef WORDS_BIGENDIAN
    Uint16 t = bswap_16(v);
    write(&t, 2);
#else
    write(&v, 2);
#endif
}

void Handle::write16(int16 v) {
#ifdef WORDS_BIGENDIAN
    Uint16 t = bswap_16((Uint16)v);
    write(&t, 2);
#else
    write(&v, 2);
#endif
}

void Handle::writeU32(Uint32 v) {
#ifdef WORDS_BIGENDIAN
    Uint32 t = bswap_32(v);
    write(&t, 4);
#else
    write(&v, 4);
#endif
}

void Handle::write32(int32 v) {
#ifdef WORDS_BIGENDIAN
    Uint32 t = bswap_32((Uint32)v);
    write(&t, 4);
#else
    write(&v, 4);
#endif
}

void Handle::writeU64(Uint64 v) {
#ifdef WORDS_BIGENDIAN
    Uint64 t = bswap_64(v);
    write(&t, 8);
#else
    write(&v, 8);
#endif
}

void Handle::write64(int64 v) {
#ifdef WORDS_BIGENDIAN
    Uint64 t = bswap_64((Uint64)v);
    write(&t, 8);
#else
    write(&v, 8);
#endif
}

void Handle::writeFloat(float v) {
#ifdef WORDS_BIGENDIAN
    writeU32(*((Uint32 *)&v));
#else
    write(&v, 4);
#endif
}

void Handle::writeDouble(double v) {
#ifdef WORDS_BIGENDIAN
    writeU64(*(((Uint64 *)&v)));
#else
    write(&v, 8);
#endif
}

void Handle::copyto(Handle * dest, ssize_t s) {
    copy(this, dest, s);
}

void Handle::copyfrom(Handle * src, ssize_t s) {
    copy(src, this, s);
}

void copyone(Handle * s, Handle * d, ssize_t size) {
    long i;
    unsigned char c;
    long r;
    
    if (size < 0)
	size = s->GetSize();
    
    for (i = 0; (i < size) || (size < 0); i++) {
	r = s->read(&c, 1);
	if (r == 0) {
	    break;
	}
	d->write(&c, 1);
    }
}

#define BSIZE 20480

void copy(Handle * s, Handle * d, ssize_t size) {
    unsigned char b[BSIZE];
    long r;
    
    if (size < 0)
	size = s->GetSize();
	
    while (size) {
        if ((size > BSIZE) || (size < 0)) {
    	    r = s->read(b, BSIZE);
	    if (r)
		d->write(b, r);
	} else {
	    r = s->read(b, size);
	    if (r)
		d->write(b, r);
	}
	if (!r)
	    break;
        if (size > 0)
	    size -= r;
    }
}

void Handle::Flush() {
    if (!CanWrite())
	return;
    if (h < 0)
	return;
    if (z >= 10) {
	
    } else if (zfile) {
	gzflush(zile, Z_FULL_FLUSH);
    } else {
#ifdef HAVE_FSYNC
	fsync(h);
#endif
    }
}

void * Handle::mmap(off_t offset, size_t length) throw (GeneralException) {
    void * r;

    if (h == -1) {
	throw GeneralException("Can't mmap() a virtual handle");
    }
#ifdef _WIN32
    if (!hFile) {
        throw GeneralException("Can't mmap() a non-hFile handle under windows");
    }
#endif
    if (mapped) {
	throw GeneralException("Handle already mmap()ped");
    }
    mapped = true;
    if (length == -1) {
	length = GetSize();
    }
    maplength = length;
#ifndef _WIN32
#ifdef __MIPSEL__
    throw GeneralException("No mmap in ps2sdk yet.");
#else
    r = ::mmap(0, length, (CanRead() ? PROT_READ : 0) | (CanWrite() ? PROT_WRITE : 0), MAP_SHARED, h, offset);
#endif
    if (!r) {
	throw GeneralException(String("Was not able to mmap(): ") + strerror(errno));
    }
#else
    hMapObject = CreateFileMapping(
	hFile,
        0,
	CanWrite() ? PAGE_READWRITE : PAGE_READONLY,
        0,
        length,
        GetName().to_charp());
    if (hMapObject != NULL) {
	r = MapViewOfFile( 
	    hMapObject,
	    CanWrite() ? FILE_MAP_WRITE : FILE_MAP_READ,
	    0,
	    offset,
	    length);
	if (!r) {
	    CloseHandle(hMapObject);
	    throw GeneralException("Was not able to MapViewOfFile()");
	}
    } else {
	throw GeneralException("Was not able to CreateFileMapping()");
    }
#endif

    mappedarea = r;

    return r;
}

void Handle::munmap() throw (GeneralException) {
    if (!mapped) {
	throw GeneralException("Can't munmap, was not mapped");
    }
#ifndef _WIN32
#ifndef __MIPSEL__
    if (::munmap(mappedarea, maplength)) {
	throw GeneralException(String("Was not able to munmap(): ") + strerror(errno));
    }
#endif
#else
    if (!UnmapViewOfFile(mappedarea)) {
	throw GeneralException("Was not able to UnmapViewOfFile()");
    }
    CloseHandle(hMapObject);
#endif
    mapped = false;
    mappedarea = 0;
    maplength = 0;
}

ssize_t Handle::nwrite(const void * buf, size_t count) throw (GeneralException) {
    return ::write(h, buf, count);
}

ssize_t Handle::nread(void * buf, size_t count) throw (GeneralException) {
    return ::read(h, buf, count);
}

int Handle::ndup() const throw (GeneralException) {
    return dup(h);
}

int Handle::nclose() throw (GeneralException) {
    return ::close(h);
}

int Handle::GetNbHandles() {
    return nb_handles;
}

#define CHUNK 10240
//
// shamelessly ripped from http://www.zlib.net/zpipe.c
//
int Handle::zlib_inflate(Handle * in, Handle * out, bool raw) throw (GeneralException) {
    int ret;
    z_stream s;
    unsigned char b_in[CHUNK];
    unsigned char b_out[CHUNK];
    unsigned int have, total_out;

    s.zalloc = (alloc_func) 0;
    s.zfree = (free_func) 0;
    s.opaque = (voidpf) 0;
    s.next_in = Z_NULL;
    s.next_out = Z_NULL;
    s.avail_in = 0;
    s.avail_out = 0;

    have = total_out = 0;

    ret = inflateInit2(&s, raw ? -15 : 15);
    if (ret != Z_OK) {
	throw GeneralException("zlib: inflateInit() failed: " + String(ret));
    }

    do {
    	s.avail_in = in->read(b_in, CHUNK);

	if (s.avail_in == 0)
	    break;
	
	s.next_in = b_in;

	do {
	    s.avail_out = CHUNK;
	    s.next_out = b_out;
	    ret = inflate(&s, Z_NO_FLUSH);
	    if (ret == Z_STREAM_ERROR) {
	        throw GeneralException("inflate returned Z_STREAM_ERROR: " + String(s.msg));
	    }
	    switch (ret) {
	    case Z_NEED_DICT:
	    	ret = Z_DATA_ERROR;
	    case Z_DATA_ERROR:
	    case Z_MEM_ERROR:
	    	inflateEnd(&s);
		throw GeneralException("inflate returned an erroneous state: " + String(ret) + " - " + String(s.msg));
	    }
	    have = CHUNK - s.avail_out;
	    if (out->write(b_out, have) != have) {
	    	throw GeneralException("Output file not writable.");
	    }
	    total_out += have;
	} while (s.avail_out == 0);
    } while (ret != Z_STREAM_END);

    inflateEnd(&s);

    return total_out;
}

int Handle::zlib_deflate(Handle * in, Handle * out, int level, bool raw, bool gzip) throw (GeneralException) {
    int ret, flush;
    z_stream s;
    unsigned char b_in[CHUNK];
    unsigned char b_out[CHUNK];
    unsigned int have, total_out;

    s.zalloc = (alloc_func) 0;
    s.zfree = (free_func) 0;
    s.opaque = (voidpf) 0;
    s.next_in = Z_NULL;
    s.next_out = Z_NULL;
    s.avail_in = 0;
    s.avail_out = 0;

    have = total_out = 0;

    ret = deflateInit2(&s, level, Z_DEFLATED, gzip ? 31 : (raw ? -15 : 15), 8, Z_DEFAULT_STRATEGY);
    if (ret != Z_OK) {
        throw GeneralException("zlib: deflateInit() failed: " + String(ret));
    }

    do {
        s.avail_in = in->read(b_in, CHUNK);

	flush = s.avail_in == 0 ? Z_FINISH : Z_NO_FLUSH;
	s.next_in = b_in;

        do {
            s.avail_out = CHUNK;
            s.next_out = b_out;
            ret = deflate(&s, flush);
            if (ret == Z_STREAM_ERROR) {
                throw GeneralException("inflate returned Z_STREAM_ERROR: " + String(s.msg));
	    }
	    have = CHUNK - s.avail_out;
	    if (out->write(b_out, have) != have) {
	        deflateEnd(&s);
		throw GeneralException("couldn't write properly to output.");
	    }

            total_out += have;
        } while (s.avail_out == 0);

	if (s.avail_in != 0) {
	    throw GeneralException("All input not used.");
	}
    } while (flush != Z_FINISH);

    deflateEnd(&s);

    return total_out;
}

#ifdef HAVE_UCL
int Handle::ucl_compress(Handle * in, Handle * out, int len_in) throw (GeneralException) {
    if (ucl_init() != UCL_E_OK)
        throw GeneralException("ucl_init failed");
    
    unsigned char * b_in, * b_out;
    unsigned int len_out;
    
    if (len_in < 0) len_in = in->GetSize() - in->tell();
    len_out = len_in + len_in / 8 + 256;
    
    b_in = (unsigned char *) malloc(len_in);
    b_out = (unsigned char *) malloc(len_out);
    in->read(b_in, len_in);
    
    int r = ucl_nrv2e_99_compress(b_in, len_in, b_out, &len_out, NULL, 10, NULL, NULL);
    if (r != UCL_E_OK)
        throw GeneralException("Error happened during ucl_nrv2e_99_compress: " + String(r));
    
    out->write(b_out, len_out);
    
    free(b_out);
    free(b_in);
    
    return len_out;
}

int Handle::ucl_decompress(Handle * in, Handle * out, unsigned int len_out, int len_in) throw (GeneralException) {
    if (ucl_init() != UCL_E_OK)
        throw GeneralException("ucl_init failed");
    
    unsigned char * b_in, * b_out;
    
    if (len_in < 0) len_in = in->GetSize() - in->tell();
    
    b_in = (unsigned char *) malloc(len_in);
    b_out = (unsigned char *) malloc(len_out);
    in->read(b_in, len_in);
    
    int r = ucl_nrv2e_decompress_safe_8(b_in, len_in, b_out, &len_out, NULL);
    if (r != UCL_E_OK)
        throw GeneralException("Error happened during ucl_nrv2e_decompress_8: " + String(r));
    
    out->write(b_out, len_out);
    
    free(b_out);
    free(b_in);
    
    return len_out;
}

int Handle::ucl_overlap(Handle * in, unsigned int len_out, int len_in) throw (GeneralException) {
    if (ucl_init() != UCL_E_OK)
        throw GeneralException("ucl_init failed");

    if (len_in < 0) len_in = in->GetSize() - in->tell();
    
    unsigned char * buff = 0;
    unsigned int len_out_t, pad = 0, offset = len_out - len_in;
    
    while (true) {
        len_out_t = len_out;
        buff = (unsigned char *) realloc(buff, len_out + pad);
        in->read(buff + offset, len_in);
        in->seek(-len_in, SEEK_CUR);
    
        int r = ucl_nrv2e_test_overlap_8(buff, offset, len_in, &len_out_t, NULL);
        switch (r) {
        case UCL_E_OVERLAP_OVERRUN:
            pad++;
            offset++;
            break;
        case UCL_E_OK:
            free(buff);
            return pad;
        default:
            throw GeneralException("Error happened during ucl_nrv2e_test_overlap_8: " + String(r));
        }
    }
    
    throw GeneralException("Shouldn't arrive here.");
}
#endif