2 Copyright © 2003, 2009, The AROS Development Team. All rights reserved.
6 #include <exec/memory.h>
9 #include <proto/exec.h>
10 #include <proto/dos.h>
11 #include <aros/macros.h>
20 #define PKG_BUFFER_SIZE (32*1024) /* 32kiB */
22 /** Low-level functions *****************************************************/
24 APTR
PKG_Open( CONST_STRPTR filename
, LONG mode
)
26 if( mode
!= MODE_READ
&& mode
!= MODE_WRITE
) return NULL
;
28 return BZ2_Open( filename
, mode
);
31 void PKG_Close( APTR pkg
)
33 if( pkg
!= NULL
) BZ2_Close( pkg
);
36 LONG
PKG_Read( APTR pkg
, APTR buffer
, LONG length
)
38 return BZ2_Read( pkg
, buffer
, length
);
41 /** High-level functions ****************************************************/
43 LONG
/* version */ PKG_ReadHeader( APTR pkg
)
45 UBYTE data
[4] = { 0, 0, 0, 0 };
48 if ( PKG_Read( pkg
, data
, 4 ) != 4 )
50 Printf("E:read header\n");
55 if ( data
[0] != 'P' || data
[1] != 'K' || data
[2] != 'G' )
57 Printf("E:invalid header\n");
63 /* though unused at the moment we still have to read package size */
65 PKG_Read( pkg
, &packageSize
, sizeof( packageSize
) );
66 //packageSize = AROS_BE2LONG(packageSize);
72 LONG
/* error */ PKG_ExtractFile( APTR pkg
)
74 LONG pathLength
, dataLength
, rc
, result
;
79 /* Read the path length */
80 rc
= PKG_Read( pkg
, &pathLength
, sizeof( pathLength
) );
81 pathLength
= AROS_BE2LONG(pathLength
);
83 if( rc
== -1 ) { result
= -1; goto cleanup
; }
84 if( rc
== 0 ) { result
= 0; goto cleanup
; }
87 path
= AllocMem( pathLength
+ 1, MEMF_ANY
);
88 if( path
== NULL
) { result
= -1; goto cleanup
; }
89 rc
= PKG_Read( pkg
, path
, pathLength
+ 1 );
90 if( rc
== -1 || rc
== 0) { result
= -1; goto cleanup
; }
92 /* Read the data length */
93 rc
= PKG_Read( pkg
, &dataLength
, sizeof( dataLength
) );
94 dataLength
= AROS_BE2LONG(dataLength
);
96 if( rc
== -1 || rc
== 0 ) { result
= -1; goto cleanup
; }
98 //Printf( "Extracting %s (%ld bytes)...\n", path, dataLength );
100 /* Make sure the destination directory exists */
101 if( !MakeDirs( path
) ) { Printf("E:makedirs\n"); result
= -1; goto cleanup
; }
103 /* Read and write the data in pieces */
104 buffer
= AllocMem( PKG_BUFFER_SIZE
, MEMF_ANY
);
105 if( buffer
== NULL
) { Printf("E:mem\n"); result
= -1; goto cleanup
; }
106 output
= Open( path
, MODE_NEWFILE
);
107 if( output
== BNULL
) { Printf("E:create\n"); result
= -1; goto cleanup
; }
112 while( total
< dataLength
)
116 if( dataLength
- total
>= PKG_BUFFER_SIZE
)
118 length
= PKG_BUFFER_SIZE
;
122 length
= dataLength
- total
;
125 rc
= PKG_Read( pkg
, buffer
, length
);
126 if( rc
== -1 || rc
== 0 ) { Printf("E:read\n"); result
= -1; goto cleanup
; }
128 rc
= FILE_Write( output
, buffer
, length
);
129 if( rc
== -1 ) { Printf("E:write\n"); result
= -1; goto cleanup
; }
138 if( path
!= NULL
) FreeMem( path
, pathLength
+ 1 );
139 if( buffer
!= NULL
) FreeMem( buffer
, PKG_BUFFER_SIZE
);
140 if( output
!= BNULL
) Close( output
);
145 LONG
/* error */ PKG_ExtractEverything( APTR pkg
)
147 LONG result
= PKG_ReadHeader( pkg
);
151 result
= PKG_ExtractFile( pkg
);
152 while( result
!= -1 && result
!= 0 )
154 result
= PKG_ExtractFile( pkg
);