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