1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
| #define STRICT
#include <windows.h>
#include <stdio.h>
#include <ctype.h>
#include <locale.h>
#include "unrar.h"
enum { EXTRACT, TEST, PRINT, LIST };
void ExtractArchive(char *ArcName,int Mode);
void ListArchive(char *ArcName);
void ShowComment(char *CmtBuf);
void OutHelp(void);
enum ERR_TYPE {ERR_OPEN, ERR_READ, ERR_PROCESS};
void OutError(int Error,char *ArcName,int ErrType);
void ShowArcInfo(unsigned int Flags,char *ArcName);
void OutProcessFileError(int Error);
int CALLBACK CallbackProc(UINT msg,LPARAM UserData,LPARAM P1,LPARAM P2);
int main(int Argc)
{
char* Argv = "C:\\ZI_rar\\HPass.rar";
setlocale(LC_ALL, NULL);
if (Argc!=3)
{
OutHelp();
}
switch(toupper(getchar()))
{
case 'X':
ExtractArchive(Argv,EXTRACT);
break;
case 'T':
ExtractArchive(Argv,TEST);
break;
case 'P':
ExtractArchive(Argv,PRINT);
break;
default:
OutHelp();
}
getchar();
return(0);
}
void ExtractArchive(char *ArcName,int Mode)
{
HANDLE hArcData;
int RHCode,PFCode;
char CmtBuf[16384];
struct RARHeaderData HeaderData;
struct RAROpenArchiveDataEx OpenArchiveData;
memset(&OpenArchiveData,0,sizeof(OpenArchiveData));
OpenArchiveData.ArcName=ArcName;
OpenArchiveData.CmtBuf=CmtBuf;
OpenArchiveData.CmtBufSize=sizeof(CmtBuf);
OpenArchiveData.OpenMode=RAR_OM_EXTRACT;
OpenArchiveData.Callback=CallbackProc;
OpenArchiveData.UserData=Mode; // 0.1.2
hArcData=RAROpenArchiveEx(&OpenArchiveData);
if (OpenArchiveData.OpenResult!=0)
{
OutError(OpenArchiveData.OpenResult,ArcName,ERR_OPEN);
return;
}
ShowArcInfo(OpenArchiveData.Flags,ArcName);
if (OpenArchiveData.CmtState==1)
ShowComment(CmtBuf);
// Obsolete, use RAROpenArchiveDataEx callback fields above.
RARSetCallback(hArcData,CallbackProc,(LPARAM)&Mode);
OutError(OpenArchiveData.OpenResult,ArcName,ERR_OPEN);
HeaderData.CmtBuf=NULL;
memset(&OpenArchiveData.Reserved,0,sizeof(OpenArchiveData.Reserved));
while ((RHCode=RARReadHeader(hArcData,&HeaderData))==0)
{
switch(Mode)
{
case EXTRACT:
printf("\nExtracting %-45s",HeaderData.FileName);
break;
case TEST:
printf("\nTesting %-45s",HeaderData.FileName);
break;
case PRINT:
printf("\nPrinting %-45s\n",HeaderData.FileName);
break;
}
PFCode=RARProcessFile(hArcData,(Mode==EXTRACT) ? RAR_EXTRACT:RAR_TEST,
NULL,NULL);
if (PFCode==0)
printf(" OK");
else
{
OutError(PFCode,ArcName,ERR_PROCESS);
break;
}
}
OutError(RHCode,ArcName,ERR_READ);
RARCloseArchive(hArcData);
getchar();
}
void ShowComment(char *CmtBuf)
{
printf("\nComment:\n%s\n",CmtBuf);
}
void OutHelp(void)
{
printf("\nUNRDLL. This is a simple example of UNRAR.DLL usage\n");
printf("\nSyntax:\n");
printf("\nUNRDLL X <Archive> extract archive contents");
printf("\nUNRDLL T <Archive> test archive contents");
printf("\nUNRDLL P <Archive> print archive contents to stdout\n");
}
void OutError(int Error,char *ArcName,int ErrType)
{
switch(Error)
{
case ERAR_NO_MEMORY:
printf("\nNot enough memory");
break;
case ERAR_BAD_DATA:
printf("\n%s: archive header or data are damaged",ArcName);
break;
case ERAR_BAD_ARCHIVE:
printf("\n%s is not RAR archive",ArcName);
break;
case ERAR_UNKNOWN_FORMAT:
printf("Unknown archive format");
break;
case ERAR_EOPEN:
if (ErrType==ERR_PROCESS) // Returned by RARProcessFile.
printf("Volume open error");
else
printf("\nCannot open %s",ArcName);
break;
case ERAR_ECREATE:
printf("File create error");
break;
case ERAR_ECLOSE:
printf("File close error");
break;
case ERAR_EREAD:
printf("Read error");
break;
case ERAR_EWRITE:
printf("Write error");
break;
case ERAR_SMALL_BUF:
printf("Buffer for archive comment is too small, comment truncated");
break;
case ERAR_UNKNOWN:
printf("Unknown error");
break;
case ERAR_MISSING_PASSWORD:
printf("Password for encrypted file or header is not specified");
break;
case ERAR_EREFERENCE:
printf("Cannot open file source for reference record");
break;
case ERAR_BAD_PASSWORD:
printf("Wrong password is specified");
break;
}
}
void ShowArcInfo(unsigned int Flags,char *ArcName)
{
printf("\nArchive %s\n",ArcName);
printf("\nVolume:\t\t%s",(Flags & 1) ? "yes":"no");
printf("\nComment:\t%s",(Flags & 2) ? "yes":"no");
printf("\nLocked:\t\t%s",(Flags & 4) ? "yes":"no");
printf("\nSolid:\t\t%s",(Flags & 8) ? "yes":"no");
printf("\nNew naming:\t%s",(Flags & 16) ? "yes":"no");
printf("\nRecovery:\t%s",(Flags & 64) ? "yes":"no");
printf("\nEncr.headers:\t%s",(Flags & 128) ? "yes":"no");
printf("\nFirst volume:\t%s",(Flags & 256) ? "yes":"no or older than 3.0");
printf("\n---------------------------\n");
}
int CALLBACK CallbackProc(UINT msg,LPARAM UserData,LPARAM P1,LPARAM P2)
{
switch(msg)
{
case UCM_CHANGEVOLUMEW:
if (P2==RAR_VOL_ASK)
{
printf("\n\nVolume %S is required\nPossible options:\n",(wchar_t *)P1);
printf("\nEnter - try again");
printf("\n'R' - specify a new volume name");
printf("\n'Q' - quit");
printf("\nEnter your choice: ");
switch(toupper(getchar()))
{
case 'Q':
return(-1);
case 'R':
{
wchar_t *eol;
printf("\nEnter new name: ");
fflush(stdin);
// fgetws may fail to read non-English characters from stdin
// in some compilers. In this case use something more
// appropriate for Unicode input.
fgetws((wchar_t *)P1,MAX_PATH,stdin);
eol=wcspbrk((wchar_t *)P1,L"\r\n");
if (eol!=NULL)
*eol=0;
}
return(1);
default:
return(1);
}
}
if (P2==RAR_VOL_NOTIFY)
printf("\n ... volume %S\n",(wchar_t *)P1);
return(1);
case UCM_PROCESSDATA:
if (UserData==PRINT)
{
fflush(stdout);
fwrite((char *)P1,1,P2,stdout);
fflush(stdout);
}
return(1);
case UCM_NEEDPASSWORDW :
{
wchar_t *eol;
printf("\nPassword required: ");
// fgetws may fail to read non-English characters from stdin
// in some compilers. In this case use something more appropriate
// for Unicode input.
fgetws((wchar_t *)P1,P2,stdin);
eol=wcspbrk((wchar_t *)P1,L"\r\n");
if (eol!=NULL)
*eol=0;
}
return(1);
case UCM_NEEDPASSWORD:
{
lstrcpyn((char *)P1, (char *)UserData, P2);
return (1);
}
return (1);
}
return(0);
} |