1 | /*
|
---|
2 | minibz2
|
---|
3 | libbz2.dll test program.
|
---|
4 | by Yoshioka Tsuneo(QWF00133@nifty.ne.jp/tsuneo-y@is.aist-nara.ac.jp)
|
---|
5 | This file is Public Domain.
|
---|
6 | welcome any email to me.
|
---|
7 |
|
---|
8 | usage: minibz2 [-d] [-{1,2,..9}] [[srcfilename] destfilename]
|
---|
9 | */
|
---|
10 |
|
---|
11 | #define BZ_IMPORT
|
---|
12 | #include <stdio.h>
|
---|
13 | #include <stdlib.h>
|
---|
14 | #include "bzlib.h"
|
---|
15 | #ifdef _WIN32
|
---|
16 | #include <io.h>
|
---|
17 | #endif
|
---|
18 |
|
---|
19 |
|
---|
20 | #ifdef _WIN32
|
---|
21 |
|
---|
22 | #define BZ2_LIBNAME "libbz2-1.0.2.DLL"
|
---|
23 |
|
---|
24 | #include <windows.h>
|
---|
25 | static int BZ2DLLLoaded = 0;
|
---|
26 | static HINSTANCE BZ2DLLhLib;
|
---|
27 | int BZ2DLLLoadLibrary(void)
|
---|
28 | {
|
---|
29 | HINSTANCE hLib;
|
---|
30 |
|
---|
31 | if(BZ2DLLLoaded==1){return 0;}
|
---|
32 | hLib=LoadLibrary(BZ2_LIBNAME);
|
---|
33 | if(hLib == NULL){
|
---|
34 | fprintf(stderr,"Can't load %s\n",BZ2_LIBNAME);
|
---|
35 | return -1;
|
---|
36 | }
|
---|
37 | BZ2_bzlibVersion=GetProcAddress(hLib,"BZ2_bzlibVersion");
|
---|
38 | BZ2_bzopen=GetProcAddress(hLib,"BZ2_bzopen");
|
---|
39 | BZ2_bzdopen=GetProcAddress(hLib,"BZ2_bzdopen");
|
---|
40 | BZ2_bzread=GetProcAddress(hLib,"BZ2_bzread");
|
---|
41 | BZ2_bzwrite=GetProcAddress(hLib,"BZ2_bzwrite");
|
---|
42 | BZ2_bzflush=GetProcAddress(hLib,"BZ2_bzflush");
|
---|
43 | BZ2_bzclose=GetProcAddress(hLib,"BZ2_bzclose");
|
---|
44 | BZ2_bzerror=GetProcAddress(hLib,"BZ2_bzerror");
|
---|
45 |
|
---|
46 | if (!BZ2_bzlibVersion || !BZ2_bzopen || !BZ2_bzdopen
|
---|
47 | || !BZ2_bzread || !BZ2_bzwrite || !BZ2_bzflush
|
---|
48 | || !BZ2_bzclose || !BZ2_bzerror) {
|
---|
49 | fprintf(stderr,"GetProcAddress failed.\n");
|
---|
50 | return -1;
|
---|
51 | }
|
---|
52 | BZ2DLLLoaded=1;
|
---|
53 | BZ2DLLhLib=hLib;
|
---|
54 | return 0;
|
---|
55 |
|
---|
56 | }
|
---|
57 | int BZ2DLLFreeLibrary(void)
|
---|
58 | {
|
---|
59 | if(BZ2DLLLoaded==0){return 0;}
|
---|
60 | FreeLibrary(BZ2DLLhLib);
|
---|
61 | BZ2DLLLoaded=0;
|
---|
62 | }
|
---|
63 | #endif /* WIN32 */
|
---|
64 |
|
---|
65 | void usage(void)
|
---|
66 | {
|
---|
67 | puts("usage: minibz2 [-d] [-{1,2,..9}] [[srcfilename] destfilename]");
|
---|
68 | }
|
---|
69 |
|
---|
70 | int main(int argc,char *argv[])
|
---|
71 | {
|
---|
72 | int decompress = 0;
|
---|
73 | int level = 9;
|
---|
74 | char *fn_r = NULL;
|
---|
75 | char *fn_w = NULL;
|
---|
76 |
|
---|
77 | #ifdef _WIN32
|
---|
78 | if(BZ2DLLLoadLibrary()<0){
|
---|
79 | fprintf(stderr,"Loading of %s failed. Giving up.\n", BZ2_LIBNAME);
|
---|
80 | exit(1);
|
---|
81 | }
|
---|
82 | printf("Loading of %s succeeded. Library version is %s.\n",
|
---|
83 | BZ2_LIBNAME, BZ2_bzlibVersion() );
|
---|
84 | #endif
|
---|
85 | while(++argv,--argc){
|
---|
86 | if(**argv =='-' || **argv=='/'){
|
---|
87 | char *p;
|
---|
88 |
|
---|
89 | for(p=*argv+1;*p;p++){
|
---|
90 | if(*p=='d'){
|
---|
91 | decompress = 1;
|
---|
92 | }else if('1'<=*p && *p<='9'){
|
---|
93 | level = *p - '0';
|
---|
94 | }else{
|
---|
95 | usage();
|
---|
96 | exit(1);
|
---|
97 | }
|
---|
98 | }
|
---|
99 | }else{
|
---|
100 | break;
|
---|
101 | }
|
---|
102 | }
|
---|
103 | if(argc>=1){
|
---|
104 | fn_r = *argv;
|
---|
105 | argc--;argv++;
|
---|
106 | }else{
|
---|
107 | fn_r = NULL;
|
---|
108 | }
|
---|
109 | if(argc>=1){
|
---|
110 | fn_w = *argv;
|
---|
111 | argc--;argv++;
|
---|
112 | }else{
|
---|
113 | fn_w = NULL;
|
---|
114 | }
|
---|
115 | {
|
---|
116 | int len;
|
---|
117 | char buff[0x1000];
|
---|
118 | char mode[10];
|
---|
119 |
|
---|
120 | if(decompress){
|
---|
121 | BZFILE *BZ2fp_r = NULL;
|
---|
122 | FILE *fp_w = NULL;
|
---|
123 |
|
---|
124 | if(fn_w){
|
---|
125 | if((fp_w = fopen(fn_w,"wb"))==NULL){
|
---|
126 | printf("can't open [%s]\n",fn_w);
|
---|
127 | perror("reason:");
|
---|
128 | exit(1);
|
---|
129 | }
|
---|
130 | }else{
|
---|
131 | fp_w = stdout;
|
---|
132 | }
|
---|
133 | if((fn_r == NULL && (BZ2fp_r = BZ2_bzdopen(fileno(stdin),"rb"))==NULL)
|
---|
134 | || (fn_r != NULL && (BZ2fp_r = BZ2_bzopen(fn_r,"rb"))==NULL)){
|
---|
135 | printf("can't bz2openstream\n");
|
---|
136 | exit(1);
|
---|
137 | }
|
---|
138 | while((len=BZ2_bzread(BZ2fp_r,buff,0x1000))>0){
|
---|
139 | fwrite(buff,1,len,fp_w);
|
---|
140 | }
|
---|
141 | BZ2_bzclose(BZ2fp_r);
|
---|
142 | if(fp_w != stdout) fclose(fp_w);
|
---|
143 | }else{
|
---|
144 | BZFILE *BZ2fp_w = NULL;
|
---|
145 | FILE *fp_r = NULL;
|
---|
146 |
|
---|
147 | if(fn_r){
|
---|
148 | if((fp_r = fopen(fn_r,"rb"))==NULL){
|
---|
149 | printf("can't open [%s]\n",fn_r);
|
---|
150 | perror("reason:");
|
---|
151 | exit(1);
|
---|
152 | }
|
---|
153 | }else{
|
---|
154 | fp_r = stdin;
|
---|
155 | }
|
---|
156 | mode[0]='w';
|
---|
157 | mode[1] = '0' + level;
|
---|
158 | mode[2] = '\0';
|
---|
159 |
|
---|
160 | if((fn_w == NULL && (BZ2fp_w = BZ2_bzdopen(fileno(stdout),mode))==NULL)
|
---|
161 | || (fn_w !=NULL && (BZ2fp_w = BZ2_bzopen(fn_w,mode))==NULL)){
|
---|
162 | printf("can't bz2openstream\n");
|
---|
163 | exit(1);
|
---|
164 | }
|
---|
165 | while((len=fread(buff,1,0x1000,fp_r))>0){
|
---|
166 | BZ2_bzwrite(BZ2fp_w,buff,len);
|
---|
167 | }
|
---|
168 | BZ2_bzclose(BZ2fp_w);
|
---|
169 | if(fp_r!=stdin)fclose(fp_r);
|
---|
170 | }
|
---|
171 | }
|
---|
172 | #ifdef _WIN32
|
---|
173 | BZ2DLLFreeLibrary();
|
---|
174 | #endif
|
---|
175 | return 0;
|
---|
176 | }
|
---|