File: UnAlzBzip2.cpp

package info (click to toggle)
unalz 0.65-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 964 kB
  • sloc: ansic: 6,892; cpp: 1,868; makefile: 82
file content (190 lines) | stat: -rwxr-xr-x 5,265 bytes parent folder | download | duplicates (6)
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
////////////////////////////////////////////////////////////////////////////////////////////////////
/// 
/// bzip2  file i/o Լ -  UnAlzbzlib.c (bzlib.c)  ִ Լ   ó ؼ
/// ణ   ҽ̴..
/// FILE* handle  κе Ǿ.
/// 
/// @author   kippler@gmail.com
/// @date     2004-10-03  3:09:11
/// 
////////////////////////////////////////////////////////////////////////////////////////////////////

//#include "stdafx.h"
#include <zlib.h>
#include <bzlib.h>
#include "UnAlz.h"

// from bzlib_private.h of bzip2
typedef char            Char;
typedef unsigned char   Bool;
typedef unsigned char   UChar;
typedef int             Int32;
typedef unsigned int    UInt32;
typedef short           Int16;
typedef unsigned short  UInt16;

#define True  ((Bool)1)
#define False ((Bool)0)

typedef struct {
  CUnAlz*    handle;
  Char      buf[BZ_MAX_UNUSED];
  Int32     bufN;
  Bool      writing;
  bz_stream strm;
  Int32     lastErr;
  Bool      initialisedOk;
}
MybzFile;

#define BZ_SETERR(eee){if (bzerror != NULL) *bzerror = eee;if (bzf != NULL) bzf->lastErr = eee;}



////////////////////////////////////////////////////////////////////////////////////////////////////
///          BZIP2  ó Լ 
/// @param   bzerror  
/// @param   f  
/// @param   verbosity  
/// @param   _small  
/// @param   unused  
/// @param   nUnused  
/// @return  
/// @date    2004-10-03  3:16:45
////////////////////////////////////////////////////////////////////////////////////////////////////
CUnAlz::MYBZFILE* CUnAlz::BZ2_bzReadOpen(int* bzerror, CUnAlz* f, int verbosity, int _small,void* unused, int   nUnused)
{
   MybzFile* bzf = NULL;
   int     ret;

   BZ_SETERR(BZ_OK);

   if (f == NULL || 
       (_small != 0 && _small != 1) ||
       (verbosity < 0 || verbosity > 4) ||
       (unused == NULL && nUnused != 0) ||
       (unused != NULL && (nUnused < 0 || nUnused > BZ_MAX_UNUSED)))
      { BZ_SETERR(BZ_PARAM_ERROR); return NULL; };

//   if (f->FEof())
//      { BZ_SETERR(BZ_IO_ERROR); return NULL; };

   bzf = (MybzFile*)malloc ( sizeof(MybzFile) );
   if (bzf == NULL) 
      { BZ_SETERR(BZ_MEM_ERROR); return NULL; };

   BZ_SETERR(BZ_OK);

   bzf->initialisedOk = False;
   bzf->handle        = f;
   bzf->bufN          = 0;
   bzf->writing       = False;
   bzf->strm.bzalloc  = NULL;
   bzf->strm.bzfree   = NULL;
   bzf->strm.opaque   = NULL;
   
   while (nUnused > 0) {
      bzf->buf[bzf->bufN] = *((UChar*)(unused)); bzf->bufN++;
      unused = ((void*)( 1 + ((UChar*)(unused))  ));
      nUnused--;
   }

   ret = BZ2_bzDecompressInit ( &(bzf->strm), verbosity, _small );
   if (ret != BZ_OK)
      { BZ_SETERR(ret); free(bzf); return NULL; };

   bzf->strm.avail_in = bzf->bufN;
   bzf->strm.next_in  = bzf->buf;

   bzf->initialisedOk = True;
   return bzf;   
}


int CUnAlz::BZ2_bzread(MYBZFILE* b, void* buf, int len )
{
   int bzerr, nread;
   if (((MybzFile*)b)->lastErr == BZ_STREAM_END) return 0;
   nread = BZ2_bzRead(&bzerr,b,buf,len);
   if (bzerr == BZ_OK || bzerr == BZ_STREAM_END) {
      return nread;
   } else {
      return -1;
   }
}


int CUnAlz::BZ2_bzRead(int* bzerror, MYBZFILE* b, void* buf, int len)
{
   Int32   n, ret;
   MybzFile* bzf = (MybzFile*)b;
   BOOL	bIsEncrypted = 	bzf->handle->IsEncryptedFile();		// ȣɸ ΰ?

   BZ_SETERR(BZ_OK);

   if (bzf == NULL || buf == NULL || len < 0)
      { BZ_SETERR(BZ_PARAM_ERROR); return 0; };

   if (bzf->writing)
      { BZ_SETERR(BZ_SEQUENCE_ERROR); return 0; };

   if (len == 0)
      { BZ_SETERR(BZ_OK); return 0; };

   bzf->strm.avail_out = len;
   bzf->strm.next_out = (char*)buf;

   while (True) {

//      if (ferror(bzf->handle)) 
//         { BZ_SETERR(BZ_IO_ERROR); return 0; };

      if (bzf->strm.avail_in == 0 && !bzf->handle->FEof()) {
         bzf->handle->FRead(bzf->buf, sizeof(UChar)*BZ_MAX_UNUSED, &n);

		 if(bIsEncrypted)
			bzf->handle->DecryptingData(n, (BYTE *)bzf->buf); // xf86 NOT tested -> tested

         if(n==0)
            { BZ_SETERR(BZ_IO_ERROR); return 0; };
         bzf->bufN = n;
         bzf->strm.avail_in = bzf->bufN;
         bzf->strm.next_in = bzf->buf;
      }

      ret = BZ2_bzDecompress ( &(bzf->strm) );

      if (ret != BZ_OK && ret != BZ_STREAM_END)
         { BZ_SETERR(ret); return 0; };

      if (ret == BZ_OK && bzf->handle->FEof() && 
          bzf->strm.avail_in == 0 && bzf->strm.avail_out > 0)
         { BZ_SETERR(BZ_UNEXPECTED_EOF); return 0; };

      if (ret == BZ_STREAM_END)
         { BZ_SETERR(BZ_STREAM_END);
           return len - bzf->strm.avail_out; };
      if (bzf->strm.avail_out == 0)
         { BZ_SETERR(BZ_OK); return len; };
      
   }

   return 0; /*not reached*/
}

void CUnAlz::BZ2_bzReadClose( int *bzerror, MYBZFILE *b )
{
   MybzFile* bzf = (MybzFile*)b;

   BZ_SETERR(BZ_OK);
   if (bzf == NULL)
      { BZ_SETERR(BZ_OK); return; };

   if (bzf->writing)
      { BZ_SETERR(BZ_SEQUENCE_ERROR); return; };

   if (bzf->initialisedOk)
      (void)BZ2_bzDecompressEnd ( &(bzf->strm) );
   free ( bzf );
}