1 /* 2 * Unit tests for the File Decompression Interface 3 * 4 * Copyright (C) 2006 James Hawkins 5 * Copyright (C) 2013 Dmitry Timoshkov 6 * 7 * This library is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2.1 of the License, or (at your option) any later version. 11 * 12 * This library is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public 18 * License along with this library; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA 20 */ 21 22 #include <stdio.h> 23 #include <windows.h> 24 #include "fci.h" 25 #include "fdi.h" 26 #include "wine/test.h" 27 28 /* make the max size large so there is only one cab file */ 29 #define MEDIA_SIZE 999999999 30 #define FOLDER_THRESHOLD 900000 31 32 static CHAR CURR_DIR[MAX_PATH]; 33 34 /* avoid including CRT headers */ 35 #ifndef _O_BINARY 36 # define _O_BINARY 0x8000 37 #endif 38 39 #ifndef _S_IREAD 40 # define _S_IREAD 0x0100 41 # define _S_IWRITE 0x0080 42 #endif 43 44 #include <pshpack1.h> 45 46 struct CFHEADER 47 { 48 UCHAR signature[4]; /* file signature */ 49 ULONG reserved1; /* reserved */ 50 ULONG cbCabinet; /* size of this cabinet file in bytes */ 51 ULONG reserved2; /* reserved */ 52 ULONG coffFiles; /* offset of the first CFFILE entry */ 53 ULONG reserved3; /* reserved */ 54 UCHAR versionMinor; /* cabinet file format version, minor */ 55 UCHAR versionMajor; /* cabinet file format version, major */ 56 USHORT cFolders; /* number of CFFOLDER entries in this cabinet */ 57 USHORT cFiles; /* number of CFFILE entries in this cabinet */ 58 USHORT flags; /* cabinet file option indicators */ 59 USHORT setID; /* must be the same for all cabinets in a set */ 60 USHORT iCabinet; /* number of this cabinet file in a set */ 61 #if 0 62 USHORT cbCFHeader; /* (optional) size of per-cabinet reserved area */ 63 UCHAR cbCFFolder; /* (optional) size of per-folder reserved area */ 64 UCHAR cbCFData; /* (optional) size of per-datablock reserved area */ 65 UCHAR abReserve; /* (optional) per-cabinet reserved area */ 66 UCHAR szCabinetPrev; /* (optional) name of previous cabinet file */ 67 UCHAR szDiskPrev; /* (optional) name of previous disk */ 68 UCHAR szCabinetNext; /* (optional) name of next cabinet file */ 69 UCHAR szDiskNext; /* (optional) name of next disk */ 70 #endif 71 }; 72 73 struct CFFOLDER 74 { 75 ULONG coffCabStart; /* offset of the first CFDATA block in this folder */ 76 USHORT cCFData; /* number of CFDATA blocks in this folder */ 77 USHORT typeCompress; /* compression type indicator */ 78 #if 0 79 UCHAR abReserve[]; /* (optional) per-folder reserved area */ 80 #endif 81 }; 82 83 struct CFFILE 84 { 85 ULONG cbFile; /* uncompressed size of this file in bytes */ 86 ULONG uoffFolderStart; /* uncompressed offset of this file in the folder */ 87 USHORT iFolder; /* index into the CFFOLDER area */ 88 USHORT date; /* date stamp for this file */ 89 USHORT time; /* time stamp for this file */ 90 USHORT attribs; /* attribute flags for this file */ 91 #if 0 92 UCHAR szName[]; /* name of this file */ 93 #endif 94 }; 95 96 struct CFDATA 97 { 98 ULONG csum; /* checksum of this CFDATA entry */ 99 USHORT cbData; /* number of compressed bytes in this block */ 100 USHORT cbUncomp; /* number of uncompressed bytes in this block */ 101 #if 0 102 UCHAR abReserve[]; /* (optional) per-datablock reserved area */ 103 UCHAR ab[cbData]; /* compressed data bytes */ 104 #endif 105 }; 106 107 static const struct 108 { 109 struct CFHEADER header; 110 struct CFFOLDER folder; 111 struct CFFILE file; 112 UCHAR szName[sizeof("file.dat")]; 113 struct CFDATA data; 114 UCHAR ab[sizeof("Hello World!")-1]; 115 } cab_data = 116 { 117 { {'M','S','C','F'}, 0, 0x59, 0, sizeof(struct CFHEADER) + sizeof(struct CFFOLDER), 0, 3,1, 1, 1, 0, 0x1225, 0x2013 }, 118 { sizeof(struct CFHEADER) + sizeof(struct CFFOLDER) + sizeof(struct CFFILE) + sizeof("file.dat"), 1, tcompTYPE_NONE }, 119 { sizeof("Hello World!")-1, 0, 0x1234, 0x1225, 0x2013, 0xa114 }, 120 { 'f','i','l','e','.','d','a','t',0 }, 121 { 0, sizeof("Hello World!")-1, sizeof("Hello World!")-1 }, 122 { 'H','e','l','l','o',' ','W','o','r','l','d','!' } 123 }; 124 125 #include <poppack.h> 126 127 struct mem_data 128 { 129 const char *base; 130 LONG size, pos; 131 }; 132 133 /* FDI callbacks */ 134 135 static void * CDECL fdi_alloc(ULONG cb) 136 { 137 return HeapAlloc(GetProcessHeap(), 0, cb); 138 } 139 140 static void * CDECL fdi_alloc_bad(ULONG cb) 141 { 142 return NULL; 143 } 144 145 static void CDECL fdi_free(void *pv) 146 { 147 HeapFree(GetProcessHeap(), 0, pv); 148 } 149 150 static INT_PTR CDECL fdi_open(char *pszFile, int oflag, int pmode) 151 { 152 HANDLE handle; 153 handle = CreateFileA(pszFile, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, 154 OPEN_EXISTING, 0, NULL ); 155 if (handle == INVALID_HANDLE_VALUE) 156 return 0; 157 return (INT_PTR) handle; 158 } 159 160 static UINT CDECL fdi_read(INT_PTR hf, void *pv, UINT cb) 161 { 162 HANDLE handle = (HANDLE) hf; 163 DWORD dwRead; 164 if (ReadFile(handle, pv, cb, &dwRead, NULL)) 165 return dwRead; 166 return 0; 167 } 168 169 static UINT CDECL fdi_write(INT_PTR hf, void *pv, UINT cb) 170 { 171 HANDLE handle = (HANDLE) hf; 172 DWORD dwWritten; 173 if (WriteFile(handle, pv, cb, &dwWritten, NULL)) 174 return dwWritten; 175 return 0; 176 } 177 178 static int CDECL fdi_close(INT_PTR hf) 179 { 180 HANDLE handle = (HANDLE) hf; 181 return CloseHandle(handle) ? 0 : -1; 182 } 183 184 static LONG CDECL fdi_seek(INT_PTR hf, LONG dist, int seektype) 185 { 186 HANDLE handle = (HANDLE) hf; 187 return SetFilePointer(handle, dist, NULL, seektype); 188 } 189 190 /* Callbacks for testing FDIIsCabinet with hf == 0 */ 191 static INT_PTR static_fdi_handle; 192 193 static INT_PTR CDECL fdi_open_static(char *pszFile, int oflag, int pmode) 194 { 195 ok(0, "FDIIsCabinet shouldn't call pfnopen\n"); 196 return 1; 197 } 198 199 static UINT CDECL fdi_read_static(INT_PTR hf, void *pv, UINT cb) 200 { 201 ok(hf == 0, "unexpected hf %lx\n", hf); 202 return fdi_read(static_fdi_handle, pv, cb); 203 } 204 205 static UINT CDECL fdi_write_static(INT_PTR hf, void *pv, UINT cb) 206 { 207 ok(0, "FDIIsCabinet shouldn't call pfnwrite\n"); 208 return 0; 209 } 210 211 static int CDECL fdi_close_static(INT_PTR hf) 212 { 213 ok(0, "FDIIsCabinet shouldn't call pfnclose\n"); 214 return 0; 215 } 216 217 static LONG CDECL fdi_seek_static(INT_PTR hf, LONG dist, int seektype) 218 { 219 ok(hf == 0, "unexpected hf %lx\n", hf); 220 return fdi_seek(static_fdi_handle, dist, seektype); 221 } 222 223 static void test_FDICreate(void) 224 { 225 HFDI hfdi; 226 ERF erf; 227 228 /* native crashes if pfnalloc is NULL */ 229 230 /* FDICreate does not crash with a NULL pfnfree, 231 * but FDIDestroy will crash when it tries to access it. 232 */ 233 if (0) 234 { 235 SetLastError(0xdeadbeef); 236 erf.erfOper = 0x1abe11ed; 237 erf.erfType = 0x5eed1e55; 238 erf.fError = 0x1ead1e55; 239 hfdi = FDICreate(fdi_alloc, NULL, fdi_open, fdi_read, 240 fdi_write, fdi_close, fdi_seek, 241 cpuUNKNOWN, &erf); 242 ok(hfdi != NULL, "Expected non-NULL context\n"); 243 ok(GetLastError() == 0xdeadbeef, 244 "Expected 0xdeadbeef, got %d\n", GetLastError()); 245 ok(erf.erfOper == 0x1abe11ed, "Expected 0x1abe11ed, got %d\n", erf.erfOper); 246 ok(erf.erfType == 0x5eed1e55, "Expected 0x5eed1e55, got %d\n", erf.erfType); 247 ok(erf.fError == 0x1ead1e55, "Expected 0x1ead1e55, got %d\n", erf.fError); 248 249 FDIDestroy(hfdi); 250 } 251 252 SetLastError(0xdeadbeef); 253 erf.erfOper = 0x1abe11ed; 254 erf.erfType = 0x5eed1e55; 255 erf.fError = 0x1ead1e55; 256 hfdi = FDICreate(fdi_alloc, fdi_free, NULL, fdi_read, 257 fdi_write, fdi_close, fdi_seek, 258 cpuUNKNOWN, &erf); 259 ok(hfdi != NULL, "Expected non-NULL context\n"); 260 ok(GetLastError() == 0xdeadbeef, 261 "Expected 0xdeadbeef, got %d\n", GetLastError()); 262 ok((erf.erfOper == 0x1abe11ed || erf.erfOper == 0 /* Vista */), "Expected 0x1abe11ed or 0, got %d\n", erf.erfOper); 263 ok((erf.erfType == 0x5eed1e55 || erf.erfType == 0 /* Vista */), "Expected 0x5eed1e55 or 0, got %d\n", erf.erfType); 264 ok((erf.fError == 0x1ead1e55 || erf.fError == 0 /* Vista */), "Expected 0x1ead1e55 or 0, got %d\n", erf.fError); 265 266 FDIDestroy(hfdi); 267 268 SetLastError(0xdeadbeef); 269 erf.erfOper = 0x1abe11ed; 270 erf.erfType = 0x5eed1e55; 271 erf.fError = 0x1ead1e55; 272 hfdi = FDICreate(fdi_alloc, fdi_free, fdi_open, NULL, 273 fdi_write, fdi_close, fdi_seek, 274 cpuUNKNOWN, &erf); 275 ok(hfdi != NULL, "Expected non-NULL context\n"); 276 ok(GetLastError() == 0xdeadbeef, 277 "Expected 0xdeadbeef, got %d\n", GetLastError()); 278 ok((erf.erfOper == 0x1abe11ed || erf.erfOper == 0 /* Vista */), "Expected 0x1abe11ed or 0, got %d\n", erf.erfOper); 279 ok((erf.erfType == 0x5eed1e55 || erf.erfType == 0 /* Vista */), "Expected 0x5eed1e55 or 0, got %d\n", erf.erfType); 280 ok((erf.fError == 0x1ead1e55 || erf.fError == 0 /* Vista */), "Expected 0x1ead1e55 or 0, got %d\n", erf.fError); 281 282 FDIDestroy(hfdi); 283 284 SetLastError(0xdeadbeef); 285 erf.erfOper = 0x1abe11ed; 286 erf.erfType = 0x5eed1e55; 287 erf.fError = 0x1ead1e55; 288 hfdi = FDICreate(fdi_alloc, fdi_free, fdi_open, fdi_read, 289 NULL, fdi_close, fdi_seek, 290 cpuUNKNOWN, &erf); 291 ok(hfdi != NULL, "Expected non-NULL context\n"); 292 ok(GetLastError() == 0xdeadbeef, 293 "Expected 0xdeadbeef, got %d\n", GetLastError()); 294 ok((erf.erfOper == 0x1abe11ed || erf.erfOper == 0 /* Vista */), "Expected 0x1abe11ed or 0, got %d\n", erf.erfOper); 295 ok((erf.erfType == 0x5eed1e55 || erf.erfType == 0 /* Vista */), "Expected 0x5eed1e55 or 0, got %d\n", erf.erfType); 296 ok((erf.fError == 0x1ead1e55 || erf.fError == 0 /* Vista */), "Expected 0x1ead1e55 or 0, got %d\n", erf.fError); 297 298 FDIDestroy(hfdi); 299 300 SetLastError(0xdeadbeef); 301 erf.erfOper = 0x1abe11ed; 302 erf.erfType = 0x5eed1e55; 303 erf.fError = 0x1ead1e55; 304 hfdi = FDICreate(fdi_alloc, fdi_free, fdi_open, fdi_read, 305 fdi_write, NULL, fdi_seek, 306 cpuUNKNOWN, &erf); 307 ok(hfdi != NULL, "Expected non-NULL context\n"); 308 ok(GetLastError() == 0xdeadbeef, 309 "Expected 0xdeadbeef, got %d\n", GetLastError()); 310 ok((erf.erfOper == 0x1abe11ed || erf.erfOper == 0 /* Vista */), "Expected 0x1abe11ed or 0, got %d\n", erf.erfOper); 311 ok((erf.erfType == 0x5eed1e55 || erf.erfType == 0 /* Vista */), "Expected 0x5eed1e55 or 0, got %d\n", erf.erfType); 312 ok((erf.fError == 0x1ead1e55 || erf.fError == 0 /* Vista */), "Expected 0x1ead1e55 or 0, got %d\n", erf.fError); 313 314 FDIDestroy(hfdi); 315 316 SetLastError(0xdeadbeef); 317 erf.erfOper = 0x1abe11ed; 318 erf.erfType = 0x5eed1e55; 319 erf.fError = 0x1ead1e55; 320 hfdi = FDICreate(fdi_alloc, fdi_free, fdi_open, fdi_read, 321 fdi_write, fdi_close, NULL, 322 cpuUNKNOWN, &erf); 323 ok(hfdi != NULL, "Expected non-NULL context\n"); 324 ok(GetLastError() == 0xdeadbeef, 325 "Expected 0xdeadbeef, got %d\n", GetLastError()); 326 ok((erf.erfOper == 0x1abe11ed || erf.erfOper == 0 /* Vista */), "Expected 0x1abe11ed or 0, got %d\n", erf.erfOper); 327 ok((erf.erfType == 0x5eed1e55 || erf.erfType == 0 /* Vista */), "Expected 0x5eed1e55 or 0, got %d\n", erf.erfType); 328 ok((erf.fError == 0x1ead1e55 || erf.fError == 0 /* Vista */), "Expected 0x1ead1e55 or 0, got %d\n", erf.fError); 329 330 FDIDestroy(hfdi); 331 332 SetLastError(0xdeadbeef); 333 erf.erfOper = 0x1abe11ed; 334 erf.erfType = 0x5eed1e55; 335 erf.fError = 0x1ead1e55; 336 hfdi = FDICreate(fdi_alloc, fdi_free, fdi_open, fdi_read, 337 fdi_write, fdi_close, fdi_seek, 338 cpuUNKNOWN, NULL); 339 /* XP sets hfdi to a non-NULL value, but Vista sets it to NULL! */ 340 ok(GetLastError() == 0xdeadbeef, 341 "Expected 0xdeadbeef, got %d\n", GetLastError()); 342 /* NULL is passed to FDICreate instead of &erf, so don't retest the erf member values. */ 343 344 FDIDestroy(hfdi); 345 346 /* bad cpu type */ 347 SetLastError(0xdeadbeef); 348 erf.erfOper = 0x1abe11ed; 349 erf.erfType = 0x5eed1e55; 350 erf.fError = 0x1ead1e55; 351 hfdi = FDICreate(fdi_alloc, fdi_free, fdi_open, fdi_read, 352 fdi_write, fdi_close, fdi_seek, 353 0xcafebabe, &erf); 354 ok(hfdi != NULL, "Expected non-NULL context\n"); 355 ok(GetLastError() == 0xdeadbeef, 356 "Expected 0xdeadbeef, got %d\n", GetLastError()); 357 ok((erf.erfOper == 0x1abe11ed || erf.erfOper == 0 /* Vista */), "Expected 0x1abe11ed or 0, got %d\n", erf.erfOper); 358 ok((erf.erfType == 0x5eed1e55 || erf.erfType == 0 /* Vista */), "Expected 0x5eed1e55 or 0, got %d\n", erf.erfType); 359 ok((erf.fError == 0x1ead1e55 || erf.fError == 0 /* Vista */), "Expected 0x1ead1e55 or 0, got %d\n", erf.fError); 360 361 FDIDestroy(hfdi); 362 363 /* pfnalloc fails */ 364 SetLastError(0xdeadbeef); 365 erf.erfOper = 0x1abe11ed; 366 erf.erfType = 0x5eed1e55; 367 erf.fError = 0x1ead1e55; 368 hfdi = FDICreate(fdi_alloc_bad, fdi_free, fdi_open, fdi_read, 369 fdi_write, fdi_close, fdi_seek, 370 cpuUNKNOWN, &erf); 371 ok(hfdi == NULL, "Expected NULL context, got %p\n", hfdi); 372 ok(erf.erfOper == FDIERROR_ALLOC_FAIL, 373 "Expected FDIERROR_ALLOC_FAIL, got %d\n", erf.erfOper); 374 ok(erf.fError == TRUE, "Expected TRUE, got %d\n", erf.fError); 375 ok(GetLastError() == 0xdeadbeef, 376 "Expected 0xdeadbeef, got %d\n", GetLastError()); 377 ok(erf.erfType == 0, "Expected 0, got %d\n", erf.erfType); 378 } 379 380 static void test_FDIDestroy(void) 381 { 382 HFDI hfdi; 383 ERF erf; 384 BOOL ret; 385 386 /* native crashes if hfdi is NULL or invalid */ 387 388 hfdi = FDICreate(fdi_alloc, fdi_free, fdi_open, fdi_read, 389 fdi_write, fdi_close, fdi_seek, 390 cpuUNKNOWN, &erf); 391 ok(hfdi != NULL, "Expected non-NULL context\n"); 392 393 /* successfully destroy hfdi */ 394 ret = FDIDestroy(hfdi); 395 ok(ret == TRUE, "Expected TRUE, got %d\n", ret); 396 397 /* native crashes if you try to destroy hfdi twice */ 398 if (0) 399 { 400 /* try to destroy hfdi again */ 401 ret = FDIDestroy(hfdi); 402 ok(ret == TRUE, "Expected TRUE, got %d\n", ret); 403 } 404 } 405 406 static void createTestFile(const CHAR *name) 407 { 408 HANDLE file; 409 DWORD written; 410 411 file = CreateFileA(name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL); 412 ok(file != INVALID_HANDLE_VALUE, "Failure to open file %s\n", name); 413 WriteFile(file, name, strlen(name), &written, NULL); 414 WriteFile(file, "\n", strlen("\n"), &written, NULL); 415 CloseHandle(file); 416 } 417 418 static void create_test_files(void) 419 { 420 DWORD len; 421 422 len = GetCurrentDirectoryA(MAX_PATH, CURR_DIR); 423 424 if(len && (CURR_DIR[len-1] == '\\')) 425 CURR_DIR[len-1] = 0; 426 427 createTestFile("a.txt"); 428 createTestFile("b.txt"); 429 CreateDirectoryA("testdir", NULL); 430 createTestFile("testdir\\c.txt"); 431 createTestFile("testdir\\d.txt"); 432 } 433 434 static void delete_test_files(void) 435 { 436 DeleteFileA("a.txt"); 437 DeleteFileA("b.txt"); 438 DeleteFileA("testdir\\c.txt"); 439 DeleteFileA("testdir\\d.txt"); 440 RemoveDirectoryA("testdir"); 441 442 DeleteFileA("extract.cab"); 443 } 444 445 /* FCI callbacks */ 446 447 static void * CDECL mem_alloc(ULONG cb) 448 { 449 return HeapAlloc(GetProcessHeap(), 0, cb); 450 } 451 452 static void CDECL mem_free(void *memory) 453 { 454 HeapFree(GetProcessHeap(), 0, memory); 455 } 456 457 static BOOL CDECL get_next_cabinet(PCCAB pccab, ULONG cbPrevCab, void *pv) 458 { 459 return TRUE; 460 } 461 462 static LONG CDECL progress(UINT typeStatus, ULONG cb1, ULONG cb2, void *pv) 463 { 464 return 0; 465 } 466 467 static int CDECL file_placed(PCCAB pccab, char *pszFile, LONG cbFile, 468 BOOL fContinuation, void *pv) 469 { 470 return 0; 471 } 472 473 static INT_PTR CDECL fci_open(char *pszFile, int oflag, int pmode, int *err, void *pv) 474 { 475 HANDLE handle; 476 DWORD dwAccess = 0; 477 DWORD dwShareMode = 0; 478 DWORD dwCreateDisposition = OPEN_EXISTING; 479 480 dwAccess = GENERIC_READ | GENERIC_WRITE; 481 /* FILE_SHARE_DELETE is not supported by Windows Me/98/95 */ 482 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE; 483 484 if (GetFileAttributesA(pszFile) != INVALID_FILE_ATTRIBUTES) 485 dwCreateDisposition = OPEN_EXISTING; 486 else 487 dwCreateDisposition = CREATE_NEW; 488 489 handle = CreateFileA(pszFile, dwAccess, dwShareMode, NULL, 490 dwCreateDisposition, 0, NULL); 491 492 ok(handle != INVALID_HANDLE_VALUE, "Failed to CreateFile %s\n", pszFile); 493 494 return (INT_PTR)handle; 495 } 496 497 static UINT CDECL fci_read(INT_PTR hf, void *memory, UINT cb, int *err, void *pv) 498 { 499 HANDLE handle = (HANDLE)hf; 500 DWORD dwRead; 501 BOOL res; 502 503 res = ReadFile(handle, memory, cb, &dwRead, NULL); 504 ok(res, "Failed to ReadFile\n"); 505 506 return dwRead; 507 } 508 509 static UINT CDECL fci_write(INT_PTR hf, void *memory, UINT cb, int *err, void *pv) 510 { 511 HANDLE handle = (HANDLE)hf; 512 DWORD dwWritten; 513 BOOL res; 514 515 res = WriteFile(handle, memory, cb, &dwWritten, NULL); 516 ok(res, "Failed to WriteFile\n"); 517 518 return dwWritten; 519 } 520 521 static int CDECL fci_close(INT_PTR hf, int *err, void *pv) 522 { 523 HANDLE handle = (HANDLE)hf; 524 ok(CloseHandle(handle), "Failed to CloseHandle\n"); 525 526 return 0; 527 } 528 529 static LONG CDECL fci_seek(INT_PTR hf, LONG dist, int seektype, int *err, void *pv) 530 { 531 HANDLE handle = (HANDLE)hf; 532 DWORD ret; 533 534 ret = SetFilePointer(handle, dist, NULL, seektype); 535 ok(ret != INVALID_SET_FILE_POINTER, "Failed to SetFilePointer\n"); 536 537 return ret; 538 } 539 540 static int CDECL fci_delete(char *pszFile, int *err, void *pv) 541 { 542 BOOL ret = DeleteFileA(pszFile); 543 ok(ret, "Failed to DeleteFile %s\n", pszFile); 544 545 return 0; 546 } 547 548 static BOOL CDECL get_temp_file(char *pszTempName, int cbTempName, void *pv) 549 { 550 LPSTR tempname; 551 552 tempname = HeapAlloc(GetProcessHeap(), 0, MAX_PATH); 553 GetTempFileNameA(".", "xx", 0, tempname); 554 555 if (tempname && (strlen(tempname) < (unsigned)cbTempName)) 556 { 557 lstrcpyA(pszTempName, tempname); 558 HeapFree(GetProcessHeap(), 0, tempname); 559 return TRUE; 560 } 561 562 HeapFree(GetProcessHeap(), 0, tempname); 563 564 return FALSE; 565 } 566 567 static INT_PTR CDECL get_open_info(char *pszName, USHORT *pdate, USHORT *ptime, 568 USHORT *pattribs, int *err, void *pv) 569 { 570 BY_HANDLE_FILE_INFORMATION finfo; 571 FILETIME filetime; 572 HANDLE handle; 573 DWORD attrs; 574 BOOL res; 575 576 handle = CreateFileA(pszName, GENERIC_READ, FILE_SHARE_READ, NULL, 577 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL); 578 579 ok(handle != INVALID_HANDLE_VALUE, "Failed to CreateFile %s\n", pszName); 580 581 res = GetFileInformationByHandle(handle, &finfo); 582 ok(res, "Expected GetFileInformationByHandle to succeed\n"); 583 584 FileTimeToLocalFileTime(&finfo.ftLastWriteTime, &filetime); 585 FileTimeToDosDateTime(&filetime, pdate, ptime); 586 587 attrs = GetFileAttributesA(pszName); 588 ok(attrs != INVALID_FILE_ATTRIBUTES, "Failed to GetFileAttributes\n"); 589 /* fixme: should convert attrs to *pattribs, make sure 590 * have a test that catches the fact that we don't? 591 */ 592 593 return (INT_PTR)handle; 594 } 595 596 static void add_file(HFCI hfci, char *file) 597 { 598 char path[MAX_PATH]; 599 BOOL res; 600 601 lstrcpyA(path, CURR_DIR); 602 lstrcatA(path, "\\"); 603 lstrcatA(path, file); 604 605 res = FCIAddFile(hfci, path, file, FALSE, get_next_cabinet, progress, 606 get_open_info, tcompTYPE_MSZIP); 607 ok(res, "Expected FCIAddFile to succeed\n"); 608 } 609 610 static void set_cab_parameters(PCCAB pCabParams) 611 { 612 ZeroMemory(pCabParams, sizeof(CCAB)); 613 614 pCabParams->cb = MEDIA_SIZE; 615 pCabParams->cbFolderThresh = FOLDER_THRESHOLD; 616 pCabParams->setID = 0xbeef; 617 lstrcpyA(pCabParams->szCabPath, CURR_DIR); 618 lstrcatA(pCabParams->szCabPath, "\\"); 619 lstrcpyA(pCabParams->szCab, "extract.cab"); 620 } 621 622 static void create_cab_file(void) 623 { 624 CCAB cabParams; 625 HFCI hfci; 626 ERF erf; 627 static CHAR a_txt[] = "a.txt", 628 b_txt[] = "b.txt", 629 testdir_c_txt[] = "testdir\\c.txt", 630 testdir_d_txt[] = "testdir\\d.txt"; 631 BOOL res; 632 633 set_cab_parameters(&cabParams); 634 635 hfci = FCICreate(&erf, file_placed, mem_alloc, mem_free, fci_open, 636 fci_read, fci_write, fci_close, fci_seek, fci_delete, 637 get_temp_file, &cabParams, NULL); 638 639 ok(hfci != NULL, "Failed to create an FCI context\n"); 640 641 add_file(hfci, a_txt); 642 add_file(hfci, b_txt); 643 add_file(hfci, testdir_c_txt); 644 add_file(hfci, testdir_d_txt); 645 646 res = FCIFlushCabinet(hfci, FALSE, get_next_cabinet, progress); 647 ok(res, "Failed to flush the cabinet\n"); 648 649 res = FCIDestroy(hfci); 650 ok(res, "Failed to destroy the cabinet\n"); 651 } 652 653 static void test_FDIIsCabinet(void) 654 { 655 ERF erf; 656 BOOL ret; 657 HFDI hfdi; 658 INT_PTR fd; 659 FDICABINETINFO cabinfo; 660 char temp[] = "temp.txt"; 661 char extract[] = "extract.cab"; 662 663 create_test_files(); 664 create_cab_file(); 665 666 hfdi = FDICreate(fdi_alloc, fdi_free, fdi_open, fdi_read, 667 fdi_write, fdi_close, fdi_seek, 668 cpuUNKNOWN, &erf); 669 ok(hfdi != NULL, "Expected non-NULL context\n"); 670 671 /* native crashes if hfdi or cabinfo are NULL or invalid */ 672 673 /* invalid file handle */ 674 ZeroMemory(&cabinfo, sizeof(FDICABINETINFO)); 675 SetLastError(0xdeadbeef); 676 ret = FDIIsCabinet(hfdi, -1, &cabinfo); 677 ok(ret == FALSE, "Expected FALSE, got %d\n", ret); 678 ok(GetLastError() == ERROR_INVALID_HANDLE, 679 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError()); 680 ok(cabinfo.cbCabinet == 0, "Expected 0, got %d\n", cabinfo.cbCabinet); 681 ok(cabinfo.cFiles == 0, "Expected 0, got %d\n", cabinfo.cFiles); 682 ok(cabinfo.cFolders == 0, "Expected 0, got %d\n", cabinfo.cFolders); 683 ok(cabinfo.iCabinet == 0, "Expected 0, got %d\n", cabinfo.iCabinet); 684 ok(cabinfo.setID == 0, "Expected 0, got %d\n", cabinfo.setID); 685 686 createTestFile("temp.txt"); 687 fd = fdi_open(temp, 0, 0); 688 689 /* file handle doesn't point to a cabinet */ 690 ZeroMemory(&cabinfo, sizeof(FDICABINETINFO)); 691 SetLastError(0xdeadbeef); 692 ret = FDIIsCabinet(hfdi, fd, &cabinfo); 693 ok(ret == FALSE, "Expected FALSE, got %d\n", ret); 694 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError()); 695 ok(cabinfo.cbCabinet == 0, "Expected 0, got %d\n", cabinfo.cbCabinet); 696 ok(cabinfo.cFiles == 0, "Expected 0, got %d\n", cabinfo.cFiles); 697 ok(cabinfo.cFolders == 0, "Expected 0, got %d\n", cabinfo.cFolders); 698 ok(cabinfo.iCabinet == 0, "Expected 0, got %d\n", cabinfo.iCabinet); 699 ok(cabinfo.setID == 0, "Expected 0, got %d\n", cabinfo.setID); 700 701 fdi_close(fd); 702 DeleteFileA("temp.txt"); 703 704 /* try a real cab */ 705 fd = fdi_open(extract, 0, 0); 706 ZeroMemory(&cabinfo, sizeof(FDICABINETINFO)); 707 SetLastError(0xdeadbeef); 708 ret = FDIIsCabinet(hfdi, fd, &cabinfo); 709 ok(ret == TRUE, "Expected TRUE, got %d\n", ret); 710 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError()); 711 ok(cabinfo.cFiles == 4, "Expected 4, got %d\n", cabinfo.cFiles); 712 ok(cabinfo.cFolders == 1, "Expected 1, got %d\n", cabinfo.cFolders); 713 ok(cabinfo.setID == 0xbeef, "Expected 0xbeef, got %d\n", cabinfo.setID); 714 ok(cabinfo.cbCabinet == 182, "Expected 182, got %d\n", cabinfo.cbCabinet); 715 ok(cabinfo.iCabinet == 0, "Expected 0, got %d\n", cabinfo.iCabinet); 716 717 fdi_close(fd); 718 FDIDestroy(hfdi); 719 720 hfdi = FDICreate(fdi_alloc, fdi_free, fdi_open_static, fdi_read_static, 721 fdi_write_static, fdi_close_static, fdi_seek_static, 722 cpuUNKNOWN, &erf); 723 ok(hfdi != NULL, "Expected non-NULL context\n"); 724 725 /* FDIIsCabinet accepts hf == 0 even though it's not a valid result of pfnopen */ 726 static_fdi_handle = fdi_open(extract, 0, 0); 727 ZeroMemory(&cabinfo, sizeof(FDICABINETINFO)); 728 SetLastError(0xdeadbeef); 729 ret = FDIIsCabinet(hfdi, 0, &cabinfo); 730 ok(ret == TRUE, "Expected TRUE, got %d\n", ret); 731 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError()); 732 ok(cabinfo.cFiles == 4, "Expected 4, got %d\n", cabinfo.cFiles); 733 ok(cabinfo.cFolders == 1, "Expected 1, got %d\n", cabinfo.cFolders); 734 ok(cabinfo.setID == 0xbeef, "Expected 0xbeef, got %d\n", cabinfo.setID); 735 ok(cabinfo.cbCabinet == 182, "Expected 182, got %d\n", cabinfo.cbCabinet); 736 ok(cabinfo.iCabinet == 0, "Expected 0, got %d\n", cabinfo.iCabinet); 737 738 fdi_close(static_fdi_handle); 739 FDIDestroy(hfdi); 740 741 delete_test_files(); 742 } 743 744 745 static INT_PTR __cdecl CopyProgress(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin) 746 { 747 return 37; /* doc says 0, but anything != -1 apparently means success as well */ 748 } 749 750 static INT_PTR CDECL fdi_mem_open(char *name, int oflag, int pmode) 751 { 752 static const char expected[] = "memory\\block"; 753 struct mem_data *data; 754 755 ok(!strcmp(name, expected), "expected %s, got %s\n", expected, name); 756 ok(oflag == _O_BINARY, "expected _O_BINARY, got %x\n", oflag); 757 ok(pmode == (_S_IREAD | _S_IWRITE), "expected _S_IREAD | _S_IWRITE, got %x\n", pmode); 758 759 data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data)); 760 if (!data) return -1; 761 762 data->base = (const char *)&cab_data; 763 data->size = sizeof(cab_data); 764 data->pos = 0; 765 766 trace("mem_open(%s,%x,%x) => %p\n", name, oflag, pmode, data); 767 return (INT_PTR)data; 768 } 769 770 static UINT CDECL fdi_mem_read(INT_PTR hf, void *pv, UINT cb) 771 { 772 struct mem_data *data = (struct mem_data *)hf; 773 UINT available, cb_read; 774 775 available = data->size - data->pos; 776 cb_read = (available >= cb) ? cb : available; 777 778 memcpy(pv, data->base + data->pos, cb_read); 779 data->pos += cb_read; 780 781 /*trace("mem_read(%p,%p,%u) => %u\n", hf, pv, cb, cb_read);*/ 782 return cb_read; 783 } 784 785 static UINT CDECL fdi_mem_write(INT_PTR hf, void *pv, UINT cb) 786 { 787 static const char expected[12] = "Hello World!"; 788 789 trace("mem_write(%#lx,%p,%u)\n", hf, pv, cb); 790 791 ok(hf == 0x12345678, "expected 0x12345678, got %#lx\n", hf); 792 ok(cb == 12, "expected 12, got %u\n", cb); 793 ok(!memcmp(pv, expected, 12), "expected %s, got %s\n", expected, (const char *)pv); 794 795 return cb; 796 } 797 798 static int CDECL fdi_mem_close(INT_PTR hf) 799 { 800 HeapFree(GetProcessHeap(), 0, (void *)hf); 801 return 0; 802 } 803 804 static LONG CDECL fdi_mem_seek(INT_PTR hf, LONG dist, int seektype) 805 { 806 struct mem_data *data = (struct mem_data *)hf; 807 808 switch (seektype) 809 { 810 case SEEK_SET: 811 data->pos = dist; 812 break; 813 814 case SEEK_CUR: 815 data->pos += dist; 816 break; 817 818 case SEEK_END: 819 default: 820 ok(0, "seek: not expected type %d\n", seektype); 821 return -1; 822 } 823 824 if (data->pos < 0) data->pos = 0; 825 if (data->pos > data->size) data->pos = data->size; 826 827 /*mem_seek(%p,%d,%d) => %u\n", hf, dist, seektype, data->pos);*/ 828 return data->pos; 829 } 830 831 static INT_PTR CDECL fdi_mem_notify(FDINOTIFICATIONTYPE fdint, FDINOTIFICATION *info) 832 { 833 static const char expected[9] = "file.dat\0"; 834 835 switch (fdint) 836 { 837 case fdintCLOSE_FILE_INFO: 838 trace("mem_notify: CLOSE_FILE_INFO %s, handle %#lx\n", info->psz1, info->hf); 839 840 ok(!strcmp(info->psz1, expected), "expected %s, got %s\n", expected, info->psz1); 841 ok(info->date == 0x1225, "expected 0x1225, got %#x\n", info->date); 842 ok(info->time == 0x2013, "expected 0x2013, got %#x\n", info->time); 843 ok(info->attribs == 0xa114, "expected 0xa114, got %#x\n", info->attribs); 844 ok(info->iFolder == 0x1234, "expected 0x1234, got %#x\n", info->iFolder); 845 return 1; 846 847 case fdintCOPY_FILE: 848 { 849 trace("mem_notify: COPY_FILE %s, %d bytes\n", info->psz1, info->cb); 850 851 ok(info->cb == 12, "expected 12, got %u\n", info->cb); 852 ok(!strcmp(info->psz1, expected), "expected %s, got %s\n", expected, info->psz1); 853 ok(info->iFolder == 0x1234, "expected 0x1234, got %#x\n", info->iFolder); 854 return 0x12345678; /* call write() callback */ 855 } 856 857 default: 858 trace("mem_notify(%d,%p)\n", fdint, info); 859 return 0; 860 } 861 862 return 0; 863 } 864 865 static void test_FDICopy(void) 866 { 867 CCAB cabParams; 868 HFDI hfdi; 869 HFCI hfci; 870 ERF erf; 871 BOOL ret; 872 char name[] = "extract.cab"; 873 char path[MAX_PATH + 1]; 874 char memory_block[] = "memory\\block"; 875 char memory[] = "memory\\"; 876 char block[] = "block"; 877 FDICABINETINFO info; 878 INT_PTR fd; 879 880 set_cab_parameters(&cabParams); 881 882 hfci = FCICreate(&erf, file_placed, mem_alloc, mem_free, fci_open, 883 fci_read, fci_write, fci_close, fci_seek, 884 fci_delete, get_temp_file, &cabParams, NULL); 885 886 ret = FCIFlushCabinet(hfci, FALSE, get_next_cabinet, progress); 887 ok(ret, "Failed to flush the cabinet\n"); 888 889 FCIDestroy(hfci); 890 891 lstrcpyA(path, CURR_DIR); 892 893 /* path doesn't have a trailing backslash */ 894 if (lstrlenA(path) > 2) 895 { 896 hfdi = FDICreate(fdi_alloc, fdi_free, fdi_open, fdi_read, 897 fdi_write, fdi_close, fdi_seek, 898 cpuUNKNOWN, &erf); 899 900 SetLastError(0xdeadbeef); 901 ret = FDICopy(hfdi, name, path, 0, CopyProgress, NULL, 0); 902 ok(ret == FALSE, "Expected FALSE, got %d\n", ret); 903 ok(GetLastError() == ERROR_INVALID_HANDLE, 904 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError()); 905 906 FDIDestroy(hfdi); 907 } 908 else 909 skip("Running on a root drive directory.\n"); 910 911 lstrcatA(path, "\\"); 912 913 hfdi = FDICreate(fdi_alloc, fdi_free, fdi_open, fdi_read, 914 fdi_write, fdi_close, fdi_seek, 915 cpuUNKNOWN, &erf); 916 917 /* cabinet with no files or folders */ 918 SetLastError(0xdeadbeef); 919 ret = FDICopy(hfdi, name, path, 0, CopyProgress, NULL, 0); 920 ok(ret == TRUE, "Expected TRUE, got %d\n", ret); 921 ok(GetLastError() == 0, "Expected 0f, got %d\n", GetLastError()); 922 923 FDIDestroy(hfdi); 924 925 DeleteFileA(name); 926 927 /* test extracting from a memory block */ 928 hfdi = FDICreate(fdi_alloc, fdi_free, fdi_mem_open, fdi_mem_read, 929 fdi_mem_write, fdi_mem_close, fdi_mem_seek, cpuUNKNOWN, &erf); 930 ok(hfdi != NULL, "FDICreate error %d\n", erf.erfOper); 931 932 fd = fdi_mem_open(memory_block, _O_BINARY, _S_IREAD | _S_IWRITE); 933 ok(fd != -1, "fdi_open failed\n"); 934 935 memset(&info, 0, sizeof(info)); 936 ret = FDIIsCabinet(hfdi, fd, &info); 937 ok(ret, "FDIIsCabinet error %d\n", erf.erfOper); 938 ok(info.cbCabinet == 0x59, "expected 0x59, got %#x\n", info.cbCabinet); 939 ok(info.cFiles == 1, "expected 1, got %d\n", info.cFiles); 940 ok(info.cFolders == 1, "expected 1, got %d\n", info.cFolders); 941 ok(info.setID == 0x1225, "expected 0x1225, got %#x\n", info.setID); 942 ok(info.iCabinet == 0x2013, "expected 0x2013, got %#x\n", info.iCabinet); 943 944 fdi_mem_close(fd); 945 946 ret = FDICopy(hfdi, block, memory, 0, fdi_mem_notify, NULL, 0); 947 ok(ret, "FDICopy error %d\n", erf.erfOper); 948 949 FDIDestroy(hfdi); 950 } 951 952 953 START_TEST(fdi) 954 { 955 test_FDICreate(); 956 test_FDIDestroy(); 957 test_FDIIsCabinet(); 958 test_FDICopy(); 959 } 960