typo.
[AROS.git] / workbench / c / Unpack / bzip2.c
blobc6818365ff54a465d02ddad8e58e6aade44a0227
1 /*
2 Copyright © 2003, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <proto/exec.h>
7 #include <proto/dos.h>
8 #include <exec/types.h>
9 #include <exec/memory.h>
10 #include <dos/dos.h>
12 #include "modes.h"
13 #include "file.h"
14 #include "bzip2.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 );
26 void free( void *p )
28 FreeVec( p );
31 APTR BZ2_Open( CONST_STRPTR path, LONG mode )
33 struct bzFile *bzf = NULL;
34 int rc;
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;
54 return bzf;
56 error:
57 //printf( "BZ2_Open: Error!\n" );
59 BZ2_Close( bzf );
61 return NULL;
64 void BZ2_Close( APTR file )
66 struct bzFile *bzf = (struct bzFile *) file;
68 if( bzf != NULL )
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;
82 LONG read = 0;
83 int rc;
85 bzf->bzf_Stream.next_out = buffer;
86 bzf->bzf_Stream.avail_out = length;
88 while( TRUE )
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 )
112 return length;
116 error:
118 return -1;