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