dll.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. #include "rar.hpp"
  2. static int RarErrorToDll(RAR_EXIT ErrCode);
  3. struct DataSet
  4. {
  5. CommandData Cmd;
  6. Archive Arc;
  7. CmdExtract Extract;
  8. int OpenMode;
  9. int HeaderSize;
  10. DataSet():Arc(&Cmd),Extract(&Cmd) {};
  11. };
  12. HANDLE PASCAL RAROpenArchive(struct RAROpenArchiveData *r)
  13. {
  14. RAROpenArchiveDataEx rx;
  15. memset(&rx,0,sizeof(rx));
  16. rx.ArcName=r->ArcName;
  17. rx.OpenMode=r->OpenMode;
  18. rx.CmtBuf=r->CmtBuf;
  19. rx.CmtBufSize=r->CmtBufSize;
  20. HANDLE hArc=RAROpenArchiveEx(&rx);
  21. r->OpenResult=rx.OpenResult;
  22. r->CmtSize=rx.CmtSize;
  23. r->CmtState=rx.CmtState;
  24. return hArc;
  25. }
  26. HANDLE PASCAL RAROpenArchiveEx(struct RAROpenArchiveDataEx *r)
  27. {
  28. DataSet *Data=NULL;
  29. try
  30. {
  31. ErrHandler.Clean();
  32. r->OpenResult=0;
  33. Data=new DataSet;
  34. Data->Cmd.DllError=0;
  35. Data->OpenMode=r->OpenMode;
  36. Data->Cmd.FileArgs.AddString(L"*");
  37. Data->Cmd.KeepBroken=(r->OpFlags&ROADOF_KEEPBROKEN)!=0;
  38. char AnsiArcName[NM];
  39. *AnsiArcName=0;
  40. if (r->ArcName!=NULL)
  41. {
  42. strncpyz(AnsiArcName,r->ArcName,ASIZE(AnsiArcName));
  43. #ifdef _WIN_ALL
  44. if (!AreFileApisANSI())
  45. {
  46. OemToCharBuffA(r->ArcName,AnsiArcName,ASIZE(AnsiArcName));
  47. AnsiArcName[ASIZE(AnsiArcName)-1]=0;
  48. }
  49. #endif
  50. }
  51. wchar ArcName[NM];
  52. GetWideName(AnsiArcName,r->ArcNameW,ArcName,ASIZE(ArcName));
  53. Data->Cmd.AddArcName(ArcName);
  54. Data->Cmd.Overwrite=OVERWRITE_ALL;
  55. Data->Cmd.VersionControl=1;
  56. Data->Cmd.Callback=r->Callback;
  57. Data->Cmd.UserData=r->UserData;
  58. // Open shared mode is added by request of dll users, who need to
  59. // browse and unpack archives while downloading.
  60. Data->Cmd.OpenShared = true;
  61. if (!Data->Arc.Open(ArcName,FMF_OPENSHARED))
  62. {
  63. r->OpenResult=ERAR_EOPEN;
  64. delete Data;
  65. return NULL;
  66. }
  67. if (!Data->Arc.IsArchive(true))
  68. {
  69. if (Data->Cmd.DllError!=0)
  70. r->OpenResult=Data->Cmd.DllError;
  71. else
  72. {
  73. RAR_EXIT ErrCode=ErrHandler.GetErrorCode();
  74. if (ErrCode!=RARX_SUCCESS && ErrCode!=RARX_WARNING)
  75. r->OpenResult=RarErrorToDll(ErrCode);
  76. else
  77. r->OpenResult=ERAR_BAD_ARCHIVE;
  78. }
  79. delete Data;
  80. return NULL;
  81. }
  82. r->Flags=0;
  83. if (Data->Arc.Volume)
  84. r->Flags|=ROADF_VOLUME;
  85. if (Data->Arc.MainComment)
  86. r->Flags|=ROADF_COMMENT;
  87. if (Data->Arc.Locked)
  88. r->Flags|=ROADF_LOCK;
  89. if (Data->Arc.Solid)
  90. r->Flags|=ROADF_SOLID;
  91. if (Data->Arc.NewNumbering)
  92. r->Flags|=ROADF_NEWNUMBERING;
  93. if (Data->Arc.Signed)
  94. r->Flags|=ROADF_SIGNED;
  95. if (Data->Arc.Protected)
  96. r->Flags|=ROADF_RECOVERY;
  97. if (Data->Arc.Encrypted)
  98. r->Flags|=ROADF_ENCHEADERS;
  99. if (Data->Arc.FirstVolume)
  100. r->Flags|=ROADF_FIRSTVOLUME;
  101. Array<wchar> CmtDataW;
  102. if (r->CmtBufSize!=0 && Data->Arc.GetComment(&CmtDataW))
  103. {
  104. if (r->CmtBufW!=NULL)
  105. {
  106. CmtDataW.Push(0);
  107. size_t Size=wcslen(&CmtDataW[0])+1;
  108. r->CmtState=Size>r->CmtBufSize ? ERAR_SMALL_BUF:1;
  109. r->CmtSize=(uint)Min(Size,r->CmtBufSize);
  110. memcpy(r->CmtBufW,&CmtDataW[0],(r->CmtSize-1)*sizeof(*r->CmtBufW));
  111. r->CmtBufW[r->CmtSize-1]=0;
  112. }
  113. else
  114. if (r->CmtBuf!=NULL)
  115. {
  116. Array<char> CmtData(CmtDataW.Size()*4+1);
  117. memset(&CmtData[0],0,CmtData.Size());
  118. WideToChar(&CmtDataW[0],&CmtData[0],CmtData.Size()-1);
  119. size_t Size=strlen(&CmtData[0])+1;
  120. r->CmtState=Size>r->CmtBufSize ? ERAR_SMALL_BUF:1;
  121. r->CmtSize=(uint)Min(Size,r->CmtBufSize);
  122. memcpy(r->CmtBuf,&CmtData[0],r->CmtSize-1);
  123. r->CmtBuf[r->CmtSize-1]=0;
  124. }
  125. }
  126. else
  127. r->CmtState=r->CmtSize=0;
  128. Data->Extract.ExtractArchiveInit(Data->Arc);
  129. return (HANDLE)Data;
  130. }
  131. catch (RAR_EXIT ErrCode)
  132. {
  133. if (Data!=NULL && Data->Cmd.DllError!=0)
  134. r->OpenResult=Data->Cmd.DllError;
  135. else
  136. r->OpenResult=RarErrorToDll(ErrCode);
  137. if (Data != NULL)
  138. delete Data;
  139. return NULL;
  140. }
  141. catch (std::bad_alloc&) // Catch 'new' exception.
  142. {
  143. r->OpenResult=ERAR_NO_MEMORY;
  144. if (Data != NULL)
  145. delete Data;
  146. }
  147. return NULL; // To make compilers happy.
  148. }
  149. int PASCAL RARCloseArchive(HANDLE hArcData)
  150. {
  151. DataSet *Data=(DataSet *)hArcData;
  152. try
  153. {
  154. bool Success=Data==NULL ? false:Data->Arc.Close();
  155. delete Data;
  156. return Success ? ERAR_SUCCESS : ERAR_ECLOSE;
  157. }
  158. catch (RAR_EXIT ErrCode)
  159. {
  160. return Data->Cmd.DllError!=0 ? Data->Cmd.DllError : RarErrorToDll(ErrCode);
  161. }
  162. }
  163. int PASCAL RARReadHeader(HANDLE hArcData,struct RARHeaderData *D)
  164. {
  165. struct RARHeaderDataEx X;
  166. memset(&X,0,sizeof(X));
  167. int Code=RARReadHeaderEx(hArcData,&X);
  168. strncpyz(D->ArcName,X.ArcName,ASIZE(D->ArcName));
  169. strncpyz(D->FileName,X.FileName,ASIZE(D->FileName));
  170. D->Flags=X.Flags;
  171. D->PackSize=X.PackSize;
  172. D->UnpSize=X.UnpSize;
  173. D->HostOS=X.HostOS;
  174. D->FileCRC=X.FileCRC;
  175. D->FileTime=X.FileTime;
  176. D->UnpVer=X.UnpVer;
  177. D->Method=X.Method;
  178. D->FileAttr=X.FileAttr;
  179. D->CmtSize=0;
  180. D->CmtState=0;
  181. return Code;
  182. }
  183. int PASCAL RARReadHeaderEx(HANDLE hArcData,struct RARHeaderDataEx *D)
  184. {
  185. DataSet *Data=(DataSet *)hArcData;
  186. try
  187. {
  188. if ((Data->HeaderSize=(int)Data->Arc.SearchBlock(HEAD_FILE))<=0)
  189. {
  190. if (Data->Arc.Volume && Data->Arc.GetHeaderType()==HEAD_ENDARC &&
  191. Data->Arc.EndArcHead.NextVolume)
  192. if (MergeArchive(Data->Arc,NULL,false,'L'))
  193. {
  194. Data->Arc.Seek(Data->Arc.CurBlockPos,SEEK_SET);
  195. return RARReadHeaderEx(hArcData,D);
  196. }
  197. else
  198. return ERAR_EOPEN;
  199. if (Data->Arc.BrokenHeader)
  200. return ERAR_BAD_DATA;
  201. // Might be necessary if RARSetPassword is still called instead of
  202. // open callback for RAR5 archives and if password is invalid.
  203. if (Data->Arc.FailedHeaderDecryption)
  204. return ERAR_BAD_PASSWORD;
  205. return ERAR_END_ARCHIVE;
  206. }
  207. FileHeader *hd=&Data->Arc.FileHead;
  208. if (Data->OpenMode==RAR_OM_LIST && hd->SplitBefore)
  209. {
  210. int Code=RARProcessFile(hArcData,RAR_SKIP,NULL,NULL);
  211. if (Code==0)
  212. return RARReadHeaderEx(hArcData,D);
  213. else
  214. return Code;
  215. }
  216. wcsncpy(D->ArcNameW,Data->Arc.FileName,ASIZE(D->ArcNameW));
  217. WideToChar(D->ArcNameW,D->ArcName,ASIZE(D->ArcName));
  218. wcsncpy(D->FileNameW,hd->FileName,ASIZE(D->FileNameW));
  219. WideToChar(D->FileNameW,D->FileName,ASIZE(D->FileName));
  220. #ifdef _WIN_ALL
  221. CharToOemA(D->FileName,D->FileName);
  222. #endif
  223. D->Flags=0;
  224. if (hd->SplitBefore)
  225. D->Flags|=RHDF_SPLITBEFORE;
  226. if (hd->SplitAfter)
  227. D->Flags|=RHDF_SPLITAFTER;
  228. if (hd->Encrypted)
  229. D->Flags|=RHDF_ENCRYPTED;
  230. if (hd->Solid)
  231. D->Flags|=RHDF_SOLID;
  232. if (hd->Dir)
  233. D->Flags|=RHDF_DIRECTORY;
  234. D->PackSize=uint(hd->PackSize & 0xffffffff);
  235. D->PackSizeHigh=uint(hd->PackSize>>32);
  236. D->UnpSize=uint(hd->UnpSize & 0xffffffff);
  237. D->UnpSizeHigh=uint(hd->UnpSize>>32);
  238. D->HostOS=hd->HSType==HSYS_WINDOWS ? HOST_WIN32:HOST_UNIX;
  239. D->UnpVer=Data->Arc.FileHead.UnpVer;
  240. D->FileCRC=hd->FileHash.CRC32;
  241. D->FileTime=hd->mtime.GetDos();
  242. uint64 MRaw=hd->mtime.GetWin();
  243. D->MtimeLow=(uint)MRaw;
  244. D->MtimeHigh=(uint)(MRaw>>32);
  245. uint64 CRaw=hd->ctime.GetWin();
  246. D->CtimeLow=(uint)CRaw;
  247. D->CtimeHigh=(uint)(CRaw>>32);
  248. uint64 ARaw=hd->atime.GetWin();
  249. D->AtimeLow=(uint)ARaw;
  250. D->AtimeHigh=(uint)(ARaw>>32);
  251. D->Method=hd->Method+0x30;
  252. D->FileAttr=hd->FileAttr;
  253. D->CmtSize=0;
  254. D->CmtState=0;
  255. D->DictSize=uint(hd->WinSize/1024);
  256. switch (hd->FileHash.Type)
  257. {
  258. case HASH_RAR14:
  259. case HASH_CRC32:
  260. D->HashType=RAR_HASH_CRC32;
  261. break;
  262. case HASH_BLAKE2:
  263. D->HashType=RAR_HASH_BLAKE2;
  264. memcpy(D->Hash,hd->FileHash.Digest,BLAKE2_DIGEST_SIZE);
  265. break;
  266. default:
  267. D->HashType=RAR_HASH_NONE;
  268. break;
  269. }
  270. D->RedirType=hd->RedirType;
  271. // RedirNameSize sanity check is useful in case some developer
  272. // did not initialize Reserved area with 0 as required in docs.
  273. // We have taken 'Redir*' fields from Reserved area. We may remove
  274. // this RedirNameSize check sometimes later.
  275. if (hd->RedirType!=FSREDIR_NONE && D->RedirName!=NULL &&
  276. D->RedirNameSize>0 && D->RedirNameSize<100000)
  277. wcsncpyz(D->RedirName,hd->RedirName,D->RedirNameSize);
  278. D->DirTarget=hd->DirTarget;
  279. }
  280. catch (RAR_EXIT ErrCode)
  281. {
  282. return Data->Cmd.DllError!=0 ? Data->Cmd.DllError : RarErrorToDll(ErrCode);
  283. }
  284. return ERAR_SUCCESS;
  285. }
  286. int PASCAL ProcessFile(HANDLE hArcData,int Operation,char *DestPath,char *DestName,wchar *DestPathW,wchar *DestNameW)
  287. {
  288. DataSet *Data=(DataSet *)hArcData;
  289. try
  290. {
  291. Data->Cmd.DllError=0;
  292. if (Data->OpenMode==RAR_OM_LIST || Data->OpenMode==RAR_OM_LIST_INCSPLIT ||
  293. Operation==RAR_SKIP && !Data->Arc.Solid)
  294. {
  295. if (Data->Arc.Volume && Data->Arc.GetHeaderType()==HEAD_FILE &&
  296. Data->Arc.FileHead.SplitAfter)
  297. if (MergeArchive(Data->Arc,NULL,false,'L'))
  298. {
  299. Data->Arc.Seek(Data->Arc.CurBlockPos,SEEK_SET);
  300. return ERAR_SUCCESS;
  301. }
  302. else
  303. return ERAR_EOPEN;
  304. Data->Arc.SeekToNext();
  305. }
  306. else
  307. {
  308. Data->Cmd.DllOpMode=Operation;
  309. *Data->Cmd.ExtrPath=0;
  310. *Data->Cmd.DllDestName=0;
  311. if (DestPath!=NULL)
  312. {
  313. char ExtrPathA[NM];
  314. strncpyz(ExtrPathA,DestPath,ASIZE(ExtrPathA)-2);
  315. #ifdef _WIN_ALL
  316. // We must not apply OemToCharBuffA directly to DestPath,
  317. // because we do not know DestPath length and OemToCharBuffA
  318. // does not stop at 0.
  319. OemToCharA(ExtrPathA,ExtrPathA);
  320. #endif
  321. CharToWide(ExtrPathA,Data->Cmd.ExtrPath,ASIZE(Data->Cmd.ExtrPath));
  322. AddEndSlash(Data->Cmd.ExtrPath,ASIZE(Data->Cmd.ExtrPath));
  323. }
  324. if (DestName!=NULL)
  325. {
  326. char DestNameA[NM];
  327. strncpyz(DestNameA,DestName,ASIZE(DestNameA)-2);
  328. #ifdef _WIN_ALL
  329. // We must not apply OemToCharBuffA directly to DestName,
  330. // because we do not know DestName length and OemToCharBuffA
  331. // does not stop at 0.
  332. OemToCharA(DestNameA,DestNameA);
  333. #endif
  334. CharToWide(DestNameA,Data->Cmd.DllDestName,ASIZE(Data->Cmd.DllDestName));
  335. }
  336. if (DestPathW!=NULL)
  337. {
  338. wcsncpy(Data->Cmd.ExtrPath,DestPathW,ASIZE(Data->Cmd.ExtrPath));
  339. AddEndSlash(Data->Cmd.ExtrPath,ASIZE(Data->Cmd.ExtrPath));
  340. }
  341. if (DestNameW!=NULL)
  342. wcsncpyz(Data->Cmd.DllDestName,DestNameW,ASIZE(Data->Cmd.DllDestName));
  343. wcsncpyz(Data->Cmd.Command,Operation==RAR_EXTRACT ? L"X":L"T",ASIZE(Data->Cmd.Command));
  344. Data->Cmd.Test=Operation!=RAR_EXTRACT;
  345. bool Repeat=false;
  346. Data->Extract.ExtractCurrentFile(Data->Arc,Data->HeaderSize,Repeat);
  347. // Now we process extra file information if any.
  348. //
  349. // Archive can be closed if we process volumes, next volume is missing
  350. // and current one is already removed or deleted. So we need to check
  351. // if archive is still open to avoid calling file operations on
  352. // the invalid file handle. Some of our file operations like Seek()
  353. // process such invalid handle correctly, some not.
  354. while (Data->Arc.IsOpened() && Data->Arc.ReadHeader()!=0 &&
  355. Data->Arc.GetHeaderType()==HEAD_SERVICE)
  356. {
  357. Data->Extract.ExtractCurrentFile(Data->Arc,Data->HeaderSize,Repeat);
  358. Data->Arc.SeekToNext();
  359. }
  360. Data->Arc.Seek(Data->Arc.CurBlockPos,SEEK_SET);
  361. }
  362. }
  363. catch (std::bad_alloc&)
  364. {
  365. return ERAR_NO_MEMORY;
  366. }
  367. catch (RAR_EXIT ErrCode)
  368. {
  369. return Data->Cmd.DllError!=0 ? Data->Cmd.DllError : RarErrorToDll(ErrCode);
  370. }
  371. return Data->Cmd.DllError;
  372. }
  373. int PASCAL RARProcessFile(HANDLE hArcData,int Operation,char *DestPath,char *DestName)
  374. {
  375. return ProcessFile(hArcData,Operation,DestPath,DestName,NULL,NULL);
  376. }
  377. int PASCAL RARProcessFileW(HANDLE hArcData,int Operation,wchar *DestPath,wchar *DestName)
  378. {
  379. return ProcessFile(hArcData,Operation,NULL,NULL,DestPath,DestName);
  380. }
  381. void PASCAL RARSetChangeVolProc(HANDLE hArcData,CHANGEVOLPROC ChangeVolProc)
  382. {
  383. DataSet *Data=(DataSet *)hArcData;
  384. Data->Cmd.ChangeVolProc=ChangeVolProc;
  385. }
  386. void PASCAL RARSetCallback(HANDLE hArcData,UNRARCALLBACK Callback,LPARAM UserData)
  387. {
  388. DataSet *Data=(DataSet *)hArcData;
  389. Data->Cmd.Callback=Callback;
  390. Data->Cmd.UserData=UserData;
  391. }
  392. void PASCAL RARSetProcessDataProc(HANDLE hArcData,PROCESSDATAPROC ProcessDataProc)
  393. {
  394. DataSet *Data=(DataSet *)hArcData;
  395. Data->Cmd.ProcessDataProc=ProcessDataProc;
  396. }
  397. void PASCAL RARSetPassword(HANDLE hArcData,char *Password)
  398. {
  399. #ifndef RAR_NOCRYPT
  400. DataSet *Data=(DataSet *)hArcData;
  401. wchar PasswordW[MAXPASSWORD];
  402. GetWideName(Password,NULL,PasswordW,ASIZE(PasswordW));
  403. Data->Cmd.Password.Set(PasswordW);
  404. cleandata(PasswordW,sizeof(PasswordW));
  405. #endif
  406. }
  407. int PASCAL RARGetDllVersion()
  408. {
  409. return RAR_DLL_VERSION;
  410. }
  411. static int RarErrorToDll(RAR_EXIT ErrCode)
  412. {
  413. switch(ErrCode)
  414. {
  415. case RARX_FATAL:
  416. case RARX_READ:
  417. return ERAR_EREAD;
  418. case RARX_CRC:
  419. return ERAR_BAD_DATA;
  420. case RARX_WRITE:
  421. return ERAR_EWRITE;
  422. case RARX_OPEN:
  423. return ERAR_EOPEN;
  424. case RARX_CREATE:
  425. return ERAR_ECREATE;
  426. case RARX_MEMORY:
  427. return ERAR_NO_MEMORY;
  428. case RARX_BADPWD:
  429. return ERAR_BAD_PASSWORD;
  430. case RARX_SUCCESS:
  431. return ERAR_SUCCESS; // 0.
  432. default:
  433. return ERAR_UNKNOWN;
  434. }
  435. }