2 Copyright © 2003, The AROS Development Team. All rights reserved.
6 #include <proto/exec.h>
8 #include <exec/types.h>
9 #include <exec/memory.h>
15 #include "bzip2_private.h"
17 #define BZIP2_BUFFER_SIZE (32*1024) /* 32 kiB */
19 int bz_internal_error
;
21 void *malloc( size_t size
)
23 return AllocVec( size
, MEMF_ANY
);
31 APTR
BZ2_Open( CONST_STRPTR path
, LONG mode
)
33 struct bzFile
*bzf
= NULL
;
36 if( mode
!= MODE_READ
) goto error
;
38 bzf
= AllocMem( sizeof( struct bzFile
), MEMF_ANY
| MEMF_CLEAR
);
39 if( bzf
== NULL
) goto error
;
41 bzf
->bzf_Buffer
= AllocMem( BZIP2_BUFFER_SIZE
, MEMF_ANY
);
42 if( bzf
->bzf_Buffer
== NULL
) goto error
;
43 bzf
->bzf_BufferAmount
= 0;
45 bzf
->bzf_File
= FILE_Open( path
, MODE_READ
);
46 if( bzf
->bzf_File
== BNULL
) goto error
;
48 rc
= BZ2_bzDecompressInit( &(bzf
->bzf_Stream
), 0, 0 );
49 if( rc
!= BZ_OK
) goto error
;
51 bzf
->bzf_Stream
.next_in
= bzf
->bzf_Buffer
;
52 bzf
->bzf_Stream
.avail_in
= bzf
->bzf_BufferAmount
;
57 //printf( "BZ2_Open: Error!\n" );
64 void BZ2_Close( APTR file
)
66 struct bzFile
*bzf
= (struct bzFile
*) file
;
70 if( bzf
->bzf_File
) Close( bzf
->bzf_File
);
71 if( bzf
->bzf_Buffer
) FreeMem( bzf
->bzf_Buffer
, BZIP2_BUFFER_SIZE
);
73 BZ2_bzDecompressEnd( &(bzf
->bzf_Stream
) );
75 FreeMem( bzf
, sizeof( struct bzFile
) );
79 LONG
BZ2_Read( APTR file
, APTR buffer
, LONG length
)
81 struct bzFile
*bzf
= (struct bzFile
*) file
;
85 bzf
->bzf_Stream
.next_out
= buffer
;
86 bzf
->bzf_Stream
.avail_out
= length
;
90 if( bzf
->bzf_Stream
.avail_in
== 0 )
92 read
= FILE_Read( bzf
->bzf_File
, bzf
->bzf_Buffer
, BZIP2_BUFFER_SIZE
);
93 if( read
== -1 ) goto error
;
94 if( read
== 0 ) return 0; /* EOF */
95 bzf
->bzf_BufferAmount
= read
;
97 bzf
->bzf_Stream
.next_in
= bzf
->bzf_Buffer
;
98 bzf
->bzf_Stream
.avail_in
= bzf
->bzf_BufferAmount
;
101 rc
= BZ2_bzDecompress( &(bzf
->bzf_Stream
) );
103 if( rc
!= BZ_OK
&& rc
!= BZ_STREAM_END
) goto error
;
105 if( rc
== BZ_STREAM_END
)
107 return length
- bzf
->bzf_Stream
.avail_out
;
110 if( bzf
->bzf_Stream
.avail_out
== 0 )