2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS cabinet manager
4 * FILE: tools/cabman/cabinet.cpp
5 * PURPOSE: Cabinet routines
6 * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
7 * Colin Finck <mail@colinfinck.de>
8 * NOTES: Define CAB_READ_ONLY for read only version
10 * CSH 21/03-2001 Created
11 * CSH 15/08-2003 Made it portable
12 * CF 04/05-2007 Made it compatible with 64-bit operating systems
14 * - Checksum of datablocks should be calculated
15 * - EXTRACT.EXE complains if a disk is created manually
16 * - Folders that are created manually and span disks will result in a damaged cabinet
23 # include <sys/stat.h>
24 # include <sys/types.h>
31 #define GetSizeOfFile(handle) _GetSizeOfFile(handle)
32 static LONG
_GetSizeOfFile(FILEHANDLE handle
)
34 ULONG size
= GetFileSize(handle
, NULL
);
35 if (size
== INVALID_FILE_SIZE
)
40 #define ReadFileData(handle, buffer, size, bytesread) _ReadFileData(handle, buffer, size, bytesread)
41 static bool _ReadFileData(FILEHANDLE handle
, void* buffer
, ULONG size
, PULONG bytesread
)
43 return ReadFile(handle
, buffer
, size
, (LPDWORD
)bytesread
, NULL
) != 0;
46 #define GetSizeOfFile(handle) _GetSizeOfFile(handle)
47 static LONG
_GetSizeOfFile(FILEHANDLE handle
)
50 fseek(handle
, 0, SEEK_END
);
52 fseek(handle
, 0, SEEK_SET
);
55 #define ReadFileData(handle, buffer, size, bytesread) _ReadFileData(handle, buffer, size, bytesread)
56 static bool _ReadFileData(FILEHANDLE handle
, void* buffer
, ULONG size
, PULONG bytesread
)
58 *bytesread
= fread(buffer
, 1, size
, handle
);
59 return *bytesread
== size
;
68 void DumpBuffer(void* Buffer
, ULONG Size
)
70 FILEHANDLE FileHandle
;
73 /* Create file, overwrite if it already exists */
74 FileHandle
= CreateFile("dump.bin", // Create this file
75 GENERIC_WRITE
, // Open for writing
78 CREATE_ALWAYS
, // Create or overwrite
79 FILE_ATTRIBUTE_NORMAL
, // Normal file
80 NULL
); // No attribute template
81 if (FileHandle
== INVALID_HANDLE_VALUE
)
83 DPRINT(MID_TRACE
, ("ERROR OPENING '%u'.\n", (UINT
)GetLastError()));
87 if (!WriteFile(FileHandle
, Buffer
, Size
, &BytesWritten
, NULL
))
89 DPRINT(MID_TRACE
, ("ERROR WRITING '%u'.\n", (UINT
)GetLastError()));
92 CloseFile(FileHandle
);
100 CCFDATAStorage::CCFDATAStorage()
102 * FUNCTION: Default constructor
109 CCFDATAStorage::~CCFDATAStorage()
111 * FUNCTION: Default destructor
114 ASSERT(!FileCreated
);
118 ULONG
CCFDATAStorage::Create()
120 * FUNCTION: Creates the file
122 * FileName = Pointer to name of file
124 * Status of operation
128 char tmpPath
[MAX_PATH
];
130 ASSERT(!FileCreated
);
133 if (GetTempPath(MAX_PATH
, tmpPath
) == 0)
134 return CAB_STATUS_CANNOT_CREATE
;
135 if(GetTempFileName(tmpPath
, "cab", 0, FullName
) == 0)
136 return CAB_STATUS_CANNOT_CREATE
;
138 /* Create file, overwrite if it already exists */
139 FileHandle
= CreateFile(FullName
, // Create this file
140 GENERIC_READ
| GENERIC_WRITE
, // Open for reading/writing
143 CREATE_ALWAYS
, // Create or overwrite
144 FILE_FLAG_SEQUENTIAL_SCAN
| // Optimize for sequential scans
145 FILE_FLAG_DELETE_ON_CLOSE
| // Delete file when closed
146 FILE_ATTRIBUTE_TEMPORARY
, // Temporary file
147 NULL
); // No attribute template
148 if (FileHandle
== INVALID_HANDLE_VALUE
)
150 DPRINT(MID_TRACE
, ("ERROR '%u'.\n", (UINT
)GetLastError()));
151 return CAB_STATUS_CANNOT_CREATE
;
154 /*if (tmpnam(FullName) == NULL)*/
155 if ((FileHandle
= tmpfile()) == NULL
)
156 return CAB_STATUS_CANNOT_CREATE
;
158 FileHandle = fopen(FullName, "w+b");
159 if (FileHandle == NULL) {
160 DPRINT(MID_TRACE, ("ERROR '%i'.\n", errno));
161 return CAB_STATUS_CANNOT_CREATE;
168 return CAB_STATUS_SUCCESS
;
172 ULONG
CCFDATAStorage::Destroy()
174 * FUNCTION: Destroys the file
176 * Status of operation
181 CloseFile(FileHandle
);
185 return CAB_STATUS_SUCCESS
;
189 ULONG
CCFDATAStorage::Truncate()
191 * FUNCTION: Truncate the scratch file to zero bytes
193 * Status of operation
197 if( SetFilePointer(FileHandle
, 0, NULL
, FILE_BEGIN
) == INVALID_SET_FILE_POINTER
)
198 return CAB_STATUS_FAILURE
;
199 if (!SetEndOfFile(FileHandle
))
200 return CAB_STATUS_FAILURE
;
203 FileHandle
= tmpfile();
204 if (FileHandle
== NULL
)
206 DPRINT(MID_TRACE
, ("ERROR '%i'.\n", errno
));
207 return CAB_STATUS_FAILURE
;
210 return CAB_STATUS_SUCCESS
;
214 ULONG
CCFDATAStorage::Position()
216 * FUNCTION: Returns current position in file
222 return SetFilePointer(FileHandle
, 0, NULL
, FILE_CURRENT
);
224 return (ULONG
)ftell(FileHandle
);
229 ULONG
CCFDATAStorage::Seek(LONG Position
)
231 * FUNCTION: Seeks to an absolute position
233 * Position = Absolute position to seek to
235 * Status of operation
239 if( SetFilePointer(FileHandle
,
242 FILE_BEGIN
) == INVALID_SET_FILE_POINTER
)
243 return CAB_STATUS_FAILURE
;
245 return CAB_STATUS_SUCCESS
;
247 if (fseek(FileHandle
, (off_t
)Position
, SEEK_SET
) != 0)
248 return CAB_STATUS_FAILURE
;
250 return CAB_STATUS_SUCCESS
;
255 ULONG
CCFDATAStorage::ReadBlock(PCFDATA Data
, void* Buffer
, PULONG BytesRead
)
257 * FUNCTION: Reads a CFDATA block from the file
259 * Data = Pointer to CFDATA block for the buffer
260 * Buffer = Pointer to buffer to store data read
261 * BytesWritten = Pointer to buffer to write number of bytes read
263 * Status of operation
267 if (!ReadFile(FileHandle
, Buffer
, Data
->CompSize
, (LPDWORD
)BytesRead
, NULL
))
268 return CAB_STATUS_CANNOT_READ
;
271 *BytesRead
= fread(Buffer
, 1, Data
->CompSize
, FileHandle
);
272 if (*BytesRead
!= Data
->CompSize
)
273 return CAB_STATUS_CANNOT_READ
;
275 return CAB_STATUS_SUCCESS
;
279 ULONG
CCFDATAStorage::WriteBlock(PCFDATA Data
, void* Buffer
, PULONG BytesWritten
)
281 * FUNCTION: Writes a CFDATA block to the file
283 * Data = Pointer to CFDATA block for the buffer
284 * Buffer = Pointer to buffer with data to write
285 * BytesWritten = Pointer to buffer to write number of bytes written
287 * Status of operation
291 if (!WriteFile(FileHandle
, Buffer
, Data
->CompSize
, (LPDWORD
)BytesWritten
, NULL
))
292 return CAB_STATUS_CANNOT_WRITE
;
294 *BytesWritten
= fwrite(Buffer
, 1, Data
->CompSize
, FileHandle
);
295 if (*BytesWritten
!= Data
->CompSize
)
296 return CAB_STATUS_CANNOT_WRITE
;
298 return CAB_STATUS_SUCCESS
;
301 #endif /* CAB_READ_ONLY */
308 * FUNCTION: Default constructor
317 *CabinetReservedFile
= '\0';
320 CabinetReservedFileBuffer
= NULL
;
321 CabinetReservedFileSize
= 0;
323 FolderListHead
= NULL
;
324 FolderListTail
= NULL
;
327 CriteriaListHead
= NULL
;
328 CriteriaListTail
= NULL
;
332 CodecSelected
= false;
337 BlockIsSplit
= false;
340 FolderUncompSize
= 0;
341 BytesLeftInBlock
= 0;
343 CurrentDataNode
= NULL
;
347 CCabinet::~CCabinet()
349 * FUNCTION: Default destructor
352 if (CabinetReservedFileBuffer
!= NULL
)
354 FreeMemory(CabinetReservedFileBuffer
);
355 CabinetReservedFileBuffer
= NULL
;
356 CabinetReservedFileSize
= 0;
363 bool CCabinet::IsSeparator(char Char
)
365 * FUNCTION: Determines if a character is a separator
367 * Char = Character to check
369 * Whether it is a separator
372 if ((Char
== '\\') || (Char
== '/'))
378 char* CCabinet::ConvertPath(char* Path
, bool Allocate
)
380 * FUNCTION: Replaces \ or / with the one used by the host environment
382 * Path = Pointer to string with pathname
383 * Allocate = Specifies whether to allocate memory for the new
384 * string or to change the existing buffer
386 * Pointer to new path
393 newpath
= strdup(Path
);
409 newpath
[i
] = Path
[i
];
419 char* CCabinet::GetFileName(char* Path
)
421 * FUNCTION: Returns a pointer to file name
423 * Path = Pointer to string with pathname
425 * Pointer to filename
430 j
= i
= (Path
[0] ? (Path
[1] == ':' ? 2 : 0) : 0);
433 if (IsSeparator(Path
[i
- 1]))
440 void CCabinet::RemoveFileName(char* Path
)
442 * FUNCTION: Removes a file name from a path
444 * Path = Pointer to string with path
450 i
= (Path
[0] ? (Path
[1] == ':' ? 2 : 0) : 0);
451 FileName
= GetFileName(Path
+ i
);
453 if ((FileName
!= (Path
+ i
)) && (IsSeparator(FileName
[-1])))
455 if ((FileName
== (Path
+ i
)) && (IsSeparator(FileName
[0])))
461 bool CCabinet::NormalizePath(char* Path
,
464 * FUNCTION: Normalizes a path
466 * Path = Pointer to string with pathname
467 * Length = Number of bytes in Path
469 * true if there was enough room in Path, or false
475 if ((n
= (ULONG
)strlen(Path
)) &&
476 (!IsSeparator(Path
[n
- 1])) &&
477 (OK
= ((n
+ 1) < Length
)))
479 Path
[n
] = DIR_SEPARATOR_CHAR
;
486 char* CCabinet::GetCabinetName()
488 * FUNCTION: Returns pointer to cabinet file name
490 * Pointer to string with name of cabinet
497 void CCabinet::SetCabinetName(char* FileName
)
499 * FUNCTION: Sets cabinet file name
501 * FileName = Pointer to string with name of cabinet
504 strcpy(CabinetName
, FileName
);
508 void CCabinet::SetDestinationPath(char* DestinationPath
)
510 * FUNCTION: Sets destination path
512 * DestinationPath = Pointer to string with name of destination path
515 strcpy(DestPath
, DestinationPath
);
516 ConvertPath(DestPath
, false);
517 if (strlen(DestPath
) > 0)
518 NormalizePath(DestPath
, PATH_MAX
);
521 ULONG
CCabinet::AddSearchCriteria(char* SearchCriteria
)
523 * FUNCTION: Adds a criteria to the search criteria list
525 * SearchCriteria = String with the search criteria to add
527 * Status of operation
530 PSEARCH_CRITERIA Criteria
;
532 // Add the criteria to the list of search criteria
533 Criteria
= (PSEARCH_CRITERIA
)AllocateMemory(sizeof(SEARCH_CRITERIA
));
536 DPRINT(MIN_TRACE
, ("Insufficient memory.\n"));
537 return CAB_STATUS_NOMEMORY
;
540 Criteria
->Prev
= CriteriaListTail
;
541 Criteria
->Next
= NULL
;
544 CriteriaListTail
->Next
= Criteria
;
546 CriteriaListHead
= Criteria
;
548 CriteriaListTail
= Criteria
;
550 // Set the actual criteria string
551 Criteria
->Search
= (char*)AllocateMemory(strlen(SearchCriteria
) + 1);
552 if (!Criteria
->Search
)
554 DPRINT(MIN_TRACE
, ("Insufficient memory.\n"));
555 return CAB_STATUS_NOMEMORY
;
558 strcpy(Criteria
->Search
, SearchCriteria
);
560 return CAB_STATUS_SUCCESS
;
563 void CCabinet::DestroySearchCriteria()
565 * FUNCTION: Destroys the list with the search criteria
568 PSEARCH_CRITERIA Criteria
;
569 PSEARCH_CRITERIA NextCriteria
;
571 Criteria
= CriteriaListHead
;
575 NextCriteria
= Criteria
->Next
;
577 FreeMemory(Criteria
->Search
);
578 FreeMemory(Criteria
);
580 Criteria
= NextCriteria
;
583 CriteriaListHead
= NULL
;
584 CriteriaListTail
= NULL
;
587 bool CCabinet::HasSearchCriteria()
589 * FUNCTION: Returns whether we have search criteria
591 * Whether we have search criteria or not.
594 return (CriteriaListHead
!= NULL
);
597 bool CCabinet::SetCompressionCodec(char* CodecName
)
599 * FUNCTION: Selects the codec to use for compression
601 * CodecName = Pointer to a string with the name of the codec
604 if( !strcasecmp(CodecName
, "raw") )
605 SelectCodec(CAB_CODEC_RAW
);
606 else if( !strcasecmp(CodecName
, "mszip") )
607 SelectCodec(CAB_CODEC_MSZIP
);
610 printf("ERROR: Invalid codec specified!\n");
617 char* CCabinet::GetDestinationPath()
619 * FUNCTION: Returns destination path
621 * Pointer to string with name of destination path
628 bool CCabinet::SetCabinetReservedFile(char* FileName
)
630 * FUNCTION: Sets cabinet reserved file
632 * FileName = Pointer to string with name of cabinet reserved file
635 FILEHANDLE FileHandle
;
639 FileHandle
= CreateFile(ConvertPath(FileName
, true), // Open this file
640 GENERIC_READ
, // Open for reading
641 FILE_SHARE_READ
, // Share for reading
643 OPEN_EXISTING
, // Existing file only
644 FILE_ATTRIBUTE_NORMAL
, // Normal file
645 NULL
); // No attribute template
646 if (FileHandle
== INVALID_HANDLE_VALUE
)
648 DPRINT(MID_TRACE
, ("Cannot open cabinet reserved file.\n"));
652 FileHandle
= fopen(ConvertPath(FileName
, true), "rb");
653 if (FileHandle
== NULL
)
655 DPRINT(MID_TRACE
, ("Cannot open cabinet reserved file.\n"));
660 CabinetReservedFileSize
= GetSizeOfFile(FileHandle
);
661 if (CabinetReservedFileSize
== (ULONG
)-1)
663 DPRINT(MIN_TRACE
, ("Cannot read from cabinet reserved file.\n"));
667 if (CabinetReservedFileSize
== 0)
669 CloseFile(FileHandle
);
673 CabinetReservedFileBuffer
= AllocateMemory(CabinetReservedFileSize
);
674 if (!CabinetReservedFileBuffer
)
676 CloseFile(FileHandle
);
680 if (!ReadFileData(FileHandle
, CabinetReservedFileBuffer
, CabinetReservedFileSize
, &BytesRead
))
682 CloseFile(FileHandle
);
686 CloseFile(FileHandle
);
688 strcpy(CabinetReservedFile
, FileName
);
694 char* CCabinet::GetCabinetReservedFile()
696 * FUNCTION: Returns cabionet reserved file
698 * Pointer to string with name of cabinet reserved file
701 return CabinetReservedFile
;
705 ULONG
CCabinet::GetCurrentDiskNumber()
707 * FUNCTION: Returns current disk number
709 * Current disk number
712 return CurrentDiskNumber
;
716 ULONG
CCabinet::Open()
718 * FUNCTION: Opens a cabinet file
720 * Status of operation
723 PCFFOLDER_NODE FolderNode
;
732 OutputBuffer
= AllocateMemory(CAB_BLOCKSIZE
+ 12); // This should be enough
734 return CAB_STATUS_NOMEMORY
;
737 FileHandle
= CreateFile(CabinetName
, // Open this file
738 GENERIC_READ
, // Open for reading
739 FILE_SHARE_READ
, // Share for reading
741 OPEN_EXISTING
, // Existing file only
742 FILE_ATTRIBUTE_NORMAL
, // Normal file
743 NULL
); // No attribute template
745 if (FileHandle
== INVALID_HANDLE_VALUE
)
747 DPRINT(MID_TRACE
, ("Cannot open file.\n"));
748 return CAB_STATUS_CANNOT_OPEN
;
751 FileHandle
= fopen(CabinetName
, "rb");
752 if (FileHandle
== NULL
)
754 DPRINT(MID_TRACE
, ("Cannot open file.\n"));
755 return CAB_STATUS_CANNOT_OPEN
;
761 /* Load CAB header */
762 if ((Status
= ReadBlock(&CABHeader
, sizeof(CFHEADER
), &BytesRead
))
763 != CAB_STATUS_SUCCESS
)
765 DPRINT(MIN_TRACE
, ("Cannot read from file (%u).\n", (UINT
)Status
));
766 return CAB_STATUS_INVALID_CAB
;
770 if ((BytesRead
!= sizeof(CFHEADER
)) ||
771 (CABHeader
.Signature
!= CAB_SIGNATURE
) ||
772 (CABHeader
.Version
!= CAB_VERSION
) ||
773 (CABHeader
.FolderCount
== 0 ) ||
774 (CABHeader
.FileCount
== 0 ) ||
775 (CABHeader
.FileTableOffset
< sizeof(CFHEADER
)))
778 DPRINT(MID_TRACE
, ("File has invalid header.\n"));
779 return CAB_STATUS_INVALID_CAB
;
784 /* Read/skip any reserved bytes */
785 if (CABHeader
.Flags
& CAB_FLAG_RESERVE
)
787 if ((Status
= ReadBlock(&Size
, sizeof(ULONG
), &BytesRead
))
788 != CAB_STATUS_SUCCESS
)
790 DPRINT(MIN_TRACE
, ("Cannot read from file (%u).\n", (UINT
)Status
));
791 return CAB_STATUS_INVALID_CAB
;
793 CabinetReserved
= Size
& 0xFFFF;
794 FolderReserved
= (Size
>> 16) & 0xFF;
795 DataReserved
= (Size
>> 24) & 0xFF;
798 if (SetFilePointer(FileHandle
, CabinetReserved
, NULL
, FILE_CURRENT
) == INVALID_SET_FILE_POINTER
)
800 DPRINT(MIN_TRACE
, ("SetFilePointer() failed, error code is %u.\n", (UINT
)GetLastError()));
801 return CAB_STATUS_FAILURE
;
804 if (fseek(FileHandle
, (off_t
)CabinetReserved
, SEEK_CUR
) != 0)
806 DPRINT(MIN_TRACE
, ("fseek() failed.\n"));
807 return CAB_STATUS_FAILURE
;
812 if ((CABHeader
.Flags
& CAB_FLAG_HASPREV
) > 0)
814 /* Read name of previous cabinet */
815 Status
= ReadString(CabinetPrev
, 256);
816 if (Status
!= CAB_STATUS_SUCCESS
)
818 /* Read label of previous disk */
819 Status
= ReadString(DiskPrev
, 256);
820 if (Status
!= CAB_STATUS_SUCCESS
)
825 strcpy(CabinetPrev
, "");
826 strcpy(DiskPrev
, "");
829 if ((CABHeader
.Flags
& CAB_FLAG_HASNEXT
) > 0)
831 /* Read name of next cabinet */
832 Status
= ReadString(CabinetNext
, 256);
833 if (Status
!= CAB_STATUS_SUCCESS
)
835 /* Read label of next disk */
836 Status
= ReadString(DiskNext
, 256);
837 if (Status
!= CAB_STATUS_SUCCESS
)
842 strcpy(CabinetNext
, "");
843 strcpy(DiskNext
, "");
846 /* Read all folders */
847 for (Index
= 0; Index
< CABHeader
.FolderCount
; Index
++)
849 FolderNode
= NewFolderNode();
852 DPRINT(MIN_TRACE
, ("Insufficient resources.\n"));
853 return CAB_STATUS_NOMEMORY
;
857 FolderNode
->UncompOffset
= FolderUncompSize
;
859 FolderNode
->Index
= Index
;
861 if ((Status
= ReadBlock(&FolderNode
->Folder
,
862 sizeof(CFFOLDER
), &BytesRead
)) != CAB_STATUS_SUCCESS
)
864 DPRINT(MIN_TRACE
, ("Cannot read from file (%u).\n", (UINT
)Status
));
865 return CAB_STATUS_INVALID_CAB
;
869 /* Read file entries */
870 Status
= ReadFileTable();
871 if (Status
!= CAB_STATUS_SUCCESS
)
873 DPRINT(MIN_TRACE
, ("ReadFileTable() failed (%u).\n", (UINT
)Status
));
877 /* Read data blocks for all folders */
878 FolderNode
= FolderListHead
;
879 while (FolderNode
!= NULL
)
881 Status
= ReadDataBlocks(FolderNode
);
882 if (Status
!= CAB_STATUS_SUCCESS
)
884 DPRINT(MIN_TRACE
, ("ReadDataBlocks() failed (%u).\n", (UINT
)Status
));
887 FolderNode
= FolderNode
->Next
;
890 return CAB_STATUS_SUCCESS
;
894 void CCabinet::Close()
896 * FUNCTION: Closes the cabinet file
901 CloseFile(FileHandle
);
907 ULONG
CCabinet::FindFirst(PCAB_SEARCH Search
)
909 * FUNCTION: Finds the first file in the cabinet that matches a search criteria
911 * Search = Pointer to search structure
913 * Status of operation
916 RestartSearch
= false;
917 Search
->Next
= FileListHead
;
918 return FindNext(Search
);
922 ULONG
CCabinet::FindNext(PCAB_SEARCH Search
)
924 * FUNCTION: Finds next file in the cabinet that matches a search criteria
926 * Search = Pointer to search structure
928 * Status of operation
932 PSEARCH_CRITERIA Criteria
;
937 Search
->Next
= FileListHead
;
939 /* Skip split files already extracted */
940 while ((Search
->Next
) &&
941 (Search
->Next
->File
.FileControlID
> CAB_FILE_MAX_FOLDER
) &&
942 (Search
->Next
->File
.FileOffset
<= LastFileOffset
))
944 DPRINT(MAX_TRACE
, ("Skipping file (%s) FileOffset (0x%X) LastFileOffset (0x%X).\n",
945 Search
->Next
->FileName
, (UINT
)Search
->Next
->File
.FileOffset
, (UINT
)LastFileOffset
));
946 Search
->Next
= Search
->Next
->Next
;
949 RestartSearch
= false;
952 /* Check each search criteria against each file */
955 // Some features (like displaying cabinets) don't require search criteria, so we can just break here.
956 // If a feature requires it, handle this in the ParseCmdline() function in "main.cxx".
957 if(!CriteriaListHead
)
960 Criteria
= CriteriaListHead
;
964 if(MatchFileNamePattern(Search
->Next
->FileName
, Criteria
->Search
))
970 Criteria
= Criteria
->Next
;
976 Search
->Next
= Search
->Next
->Next
;
981 if (strlen(DiskNext
) > 0)
985 SetCabinetName(CabinetNext
);
987 OnDiskChange(CabinetNext
, DiskNext
);
990 if (Status
!= CAB_STATUS_SUCCESS
)
993 Search
->Next
= FileListHead
;
995 return CAB_STATUS_NOFILE
;
998 return CAB_STATUS_NOFILE
;
1001 Search
->File
= &Search
->Next
->File
;
1002 Search
->FileName
= Search
->Next
->FileName
;
1003 Search
->Next
= Search
->Next
->Next
;
1004 return CAB_STATUS_SUCCESS
;
1008 ULONG
CCabinet::ExtractFile(char* FileName
)
1010 * FUNCTION: Extracts a file from the cabinet
1012 * FileName = Pointer to buffer with name of file
1014 * Status of operation
1024 ULONG TotalBytesRead
;
1025 ULONG CurrentOffset
;
1027 PUCHAR CurrentBuffer
;
1028 FILEHANDLE DestFile
;
1036 CHAR DestName
[PATH_MAX
];
1037 CHAR TempName
[PATH_MAX
];
1039 Status
= LocateFile(FileName
, &File
);
1040 if (Status
!= CAB_STATUS_SUCCESS
)
1042 DPRINT(MID_TRACE
, ("Cannot locate file (%u).\n", (UINT
)Status
));
1046 LastFileOffset
= File
->File
.FileOffset
;
1048 switch (CurrentFolderNode
->Folder
.CompressionType
& CAB_COMP_MASK
)
1051 SelectCodec(CAB_CODEC_RAW
);
1054 case CAB_COMP_MSZIP
:
1055 SelectCodec(CAB_CODEC_MSZIP
);
1059 return CAB_STATUS_UNSUPPCOMP
;
1062 DPRINT(MAX_TRACE
, ("Extracting file at uncompressed offset (0x%X) Size (%u bytes) AO (0x%X) UO (0x%X).\n",
1063 (UINT
)File
->File
.FileOffset
,
1064 (UINT
)File
->File
.FileSize
,
1065 (UINT
)File
->DataBlock
->AbsoluteOffset
,
1066 (UINT
)File
->DataBlock
->UncompOffset
));
1068 strcpy(DestName
, DestPath
);
1069 strcat(DestName
, FileName
);
1071 /* Create destination file, fail if it already exists */
1073 DestFile
= CreateFile(DestName
, // Create this file
1074 GENERIC_WRITE
, // Open for writing
1076 NULL
, // No security
1077 CREATE_NEW
, // New file only
1078 FILE_ATTRIBUTE_NORMAL
, // Normal file
1079 NULL
); // No attribute template
1080 if (DestFile
== INVALID_HANDLE_VALUE
)
1082 /* If file exists, ask to overwrite file */
1083 if (((Status
= GetLastError()) == ERROR_FILE_EXISTS
) &&
1084 (OnOverwrite(&File
->File
, FileName
)))
1086 /* Create destination file, overwrite if it already exists */
1087 DestFile
= CreateFile(DestName
, // Create this file
1088 GENERIC_WRITE
, // Open for writing
1090 NULL
, // No security
1091 TRUNCATE_EXISTING
, // Truncate the file
1092 FILE_ATTRIBUTE_NORMAL
, // Normal file
1093 NULL
); // No attribute template
1094 if (DestFile
== INVALID_HANDLE_VALUE
)
1095 return CAB_STATUS_CANNOT_CREATE
;
1099 if (Status
== ERROR_FILE_EXISTS
)
1100 return CAB_STATUS_FILE_EXISTS
;
1102 return CAB_STATUS_CANNOT_CREATE
;
1106 DestFile
= fopen(DestName
, "rb");
1107 if (DestFile
!= NULL
)
1110 /* If file exists, ask to overwrite file */
1111 if (OnOverwrite(&File
->File
, FileName
))
1113 DestFile
= fopen(DestName
, "w+b");
1114 if (DestFile
== NULL
)
1115 return CAB_STATUS_CANNOT_CREATE
;
1118 return CAB_STATUS_FILE_EXISTS
;
1122 DestFile
= fopen(DestName
, "w+b");
1123 if (DestFile
== NULL
)
1124 return CAB_STATUS_CANNOT_CREATE
;
1128 if (!DosDateTimeToFileTime(File
->File
.FileDate
, File
->File
.FileTime
, &FileTime
))
1130 CloseFile(DestFile
);
1131 DPRINT(MIN_TRACE
, ("DosDateTimeToFileTime() failed (%u).\n", (UINT
)GetLastError()));
1132 return CAB_STATUS_CANNOT_WRITE
;
1135 SetFileTime(DestFile
, NULL
, &FileTime
, NULL
);
1137 //DPRINT(MIN_TRACE, ("FIXME: DosDateTimeToFileTime\n"));
1139 SetAttributesOnFile(DestName
, File
->File
.Attributes
);
1141 Buffer
= (PUCHAR
)AllocateMemory(CAB_BLOCKSIZE
+ 12); // This should be enough
1144 CloseFile(DestFile
);
1145 DPRINT(MIN_TRACE
, ("Insufficient memory.\n"));
1146 return CAB_STATUS_NOMEMORY
;
1149 /* Call OnExtract event handler */
1150 OnExtract(&File
->File
, FileName
);
1152 /* Search to start of file */
1154 Offset
= SetFilePointer(FileHandle
,
1155 File
->DataBlock
->AbsoluteOffset
,
1158 if (Offset
== INVALID_SET_FILE_POINTER
)
1160 DPRINT(MIN_TRACE
, ("SetFilePointer() failed, error code is %u.\n", (UINT
)GetLastError()));
1161 CloseFile(DestFile
);
1162 return CAB_STATUS_INVALID_CAB
;
1165 if (fseek(FileHandle
, (off_t
)File
->DataBlock
->AbsoluteOffset
, SEEK_SET
) != 0)
1167 DPRINT(MIN_TRACE
, ("fseek() failed.\n"));
1168 CloseFile(DestFile
);
1169 return CAB_STATUS_FAILURE
;
1171 Offset
= ftell(FileHandle
);
1174 Size
= File
->File
.FileSize
;
1175 Offset
= File
->File
.FileOffset
;
1176 CurrentOffset
= File
->DataBlock
->UncompOffset
;
1180 ReuseBlock
= (CurrentDataNode
== File
->DataBlock
);
1185 DPRINT(MAX_TRACE
, ("CO (0x%X) ReuseBlock (%u) Offset (0x%X) Size (%d) BytesLeftInBlock (%d)\n",
1186 (UINT
)File
->DataBlock
->UncompOffset
, (UINT
)ReuseBlock
, (UINT
)Offset
, (UINT
)Size
,
1187 (UINT
)BytesLeftInBlock
));
1189 if (/*(CurrentDataNode != File->DataBlock) &&*/ (!ReuseBlock
) || (BytesLeftInBlock
<= 0))
1191 DPRINT(MAX_TRACE
, ("Filling buffer. ReuseBlock (%u)\n", (UINT
)ReuseBlock
));
1193 CurrentBuffer
= Buffer
;
1197 DPRINT(MAX_TRACE
, ("Size (%u bytes).\n", (UINT
)Size
));
1199 if (((Status
= ReadBlock(&CFData
, sizeof(CFDATA
), &BytesRead
)) !=
1200 CAB_STATUS_SUCCESS
) || (BytesRead
!= sizeof(CFDATA
)))
1202 CloseFile(DestFile
);
1204 DPRINT(MIN_TRACE
, ("Cannot read from file (%u).\n", (UINT
)Status
));
1205 return CAB_STATUS_INVALID_CAB
;
1208 DPRINT(MAX_TRACE
, ("Data block: Checksum (0x%X) CompSize (%u bytes) UncompSize (%u bytes)\n",
1209 (UINT
)CFData
.Checksum
,
1211 CFData
.UncompSize
));
1213 ASSERT(CFData
.CompSize
<= CAB_BLOCKSIZE
+ 12);
1215 BytesToRead
= CFData
.CompSize
;
1217 DPRINT(MAX_TRACE
, ("Read: (0x%lX,0x%lX).\n",
1218 (unsigned long)CurrentBuffer
, (unsigned long)Buffer
));
1220 if (((Status
= ReadBlock(CurrentBuffer
, BytesToRead
, &BytesRead
)) !=
1221 CAB_STATUS_SUCCESS
) || (BytesToRead
!= BytesRead
))
1223 CloseFile(DestFile
);
1225 DPRINT(MIN_TRACE
, ("Cannot read from file (%u).\n", (UINT
)Status
));
1226 return CAB_STATUS_INVALID_CAB
;
1229 /* FIXME: Does not work with files generated by makecab.exe */
1231 if (CFData.Checksum != 0)
1233 ULONG Checksum = ComputeChecksum(CurrentBuffer, BytesRead, 0);
1234 if (Checksum != CFData.Checksum)
1236 CloseFile(DestFile);
1238 DPRINT(MIN_TRACE, ("Bad checksum (is 0x%X, should be 0x%X).\n",
1239 Checksum, CFData.Checksum));
1240 return CAB_STATUS_INVALID_CAB;
1244 TotalBytesRead
+= BytesRead
;
1246 CurrentBuffer
+= BytesRead
;
1248 if (CFData
.UncompSize
== 0)
1250 if (strlen(DiskNext
) == 0)
1251 return CAB_STATUS_NOFILE
;
1253 /* CloseCabinet() will destroy all file entries so in case
1254 FileName refers to the FileName field of a CFFOLDER_NODE
1255 structure, we have to save a copy of the filename */
1256 strcpy(TempName
, FileName
);
1260 SetCabinetName(CabinetNext
);
1262 OnDiskChange(CabinetNext
, DiskNext
);
1265 if (Status
!= CAB_STATUS_SUCCESS
)
1268 /* The first data block of the file will not be
1269 found as it is located in the previous file */
1270 Status
= LocateFile(TempName
, &File
);
1271 if (Status
== CAB_STATUS_NOFILE
)
1273 DPRINT(MID_TRACE
, ("Cannot locate file (%u).\n", (UINT
)Status
));
1277 /* The file is continued in the first data block in the folder */
1278 File
->DataBlock
= CurrentFolderNode
->DataListHead
;
1280 /* Search to start of file */
1282 if( SetFilePointer(FileHandle
,
1283 File
->DataBlock
->AbsoluteOffset
,
1285 FILE_BEGIN
) == INVALID_SET_FILE_POINTER
)
1287 DPRINT(MIN_TRACE
, ("SetFilePointer() failed, error code is %u.\n", (UINT
)GetLastError()));
1288 return CAB_STATUS_INVALID_CAB
;
1291 if (fseek(FileHandle
, (off_t
)File
->DataBlock
->AbsoluteOffset
, SEEK_SET
) != 0)
1293 DPRINT(MIN_TRACE
, ("fseek() failed.\n"));
1294 return CAB_STATUS_INVALID_CAB
;
1298 DPRINT(MAX_TRACE
, ("Continuing extraction of file at uncompressed offset (0x%X) Size (%u bytes) AO (0x%X) UO (0x%X).\n",
1299 (UINT
)File
->File
.FileOffset
,
1300 (UINT
)File
->File
.FileSize
,
1301 (UINT
)File
->DataBlock
->AbsoluteOffset
,
1302 (UINT
)File
->DataBlock
->UncompOffset
));
1304 CurrentDataNode
= File
->DataBlock
;
1307 RestartSearch
= true;
1309 } while (CFData
.UncompSize
== 0);
1311 DPRINT(MAX_TRACE
, ("TotalBytesRead (%u).\n", (UINT
)TotalBytesRead
));
1313 Status
= Codec
->Uncompress(OutputBuffer
, Buffer
, TotalBytesRead
, &BytesToWrite
);
1314 if (Status
!= CS_SUCCESS
)
1316 CloseFile(DestFile
);
1318 DPRINT(MID_TRACE
, ("Cannot uncompress block.\n"));
1319 if (Status
== CS_NOMEMORY
)
1320 return CAB_STATUS_NOMEMORY
;
1321 return CAB_STATUS_INVALID_CAB
;
1324 if (BytesToWrite
!= CFData
.UncompSize
)
1326 DPRINT(MID_TRACE
, ("BytesToWrite (%u) != CFData.UncompSize (%d)\n",
1327 (UINT
)BytesToWrite
, CFData
.UncompSize
));
1328 return CAB_STATUS_INVALID_CAB
;
1331 BytesLeftInBlock
= BytesToWrite
;
1335 DPRINT(MAX_TRACE
, ("Using same buffer. ReuseBlock (%u)\n", (UINT
)ReuseBlock
));
1337 BytesToWrite
= BytesLeftInBlock
;
1339 DPRINT(MAX_TRACE
, ("Seeking to absolute offset 0x%X.\n",
1340 (UINT
)(CurrentDataNode
->AbsoluteOffset
+ sizeof(CFDATA
) + CurrentDataNode
->Data
.CompSize
)));
1342 if (((Status
= ReadBlock(&CFData
, sizeof(CFDATA
), &BytesRead
)) !=
1343 CAB_STATUS_SUCCESS
) || (BytesRead
!= sizeof(CFDATA
)))
1345 CloseFile(DestFile
);
1347 DPRINT(MIN_TRACE
, ("Cannot read from file (%u).\n", (UINT
)Status
));
1348 return CAB_STATUS_INVALID_CAB
;
1351 DPRINT(MAX_TRACE
, ("CFData.CompSize 0x%X CFData.UncompSize 0x%X.\n",
1352 CFData
.CompSize
, CFData
.UncompSize
));
1354 /* Go to next data block */
1356 if( SetFilePointer(FileHandle
,
1357 CurrentDataNode
->AbsoluteOffset
+ sizeof(CFDATA
) +
1358 CurrentDataNode
->Data
.CompSize
,
1360 FILE_BEGIN
) == INVALID_SET_FILE_POINTER
)
1362 DPRINT(MIN_TRACE
, ("SetFilePointer() failed, error code is %u.\n", (UINT
)GetLastError()));
1363 return CAB_STATUS_INVALID_CAB
;
1366 if (fseek(FileHandle
, (off_t
)CurrentDataNode
->AbsoluteOffset
+ sizeof(CFDATA
) +
1367 CurrentDataNode
->Data
.CompSize
, SEEK_SET
) != 0)
1369 DPRINT(MIN_TRACE
, ("fseek() failed.\n"));
1370 return CAB_STATUS_INVALID_CAB
;
1378 BytesSkipped
= (Offset
- CurrentOffset
);
1382 BytesToWrite
-= BytesSkipped
;
1384 if (Size
< BytesToWrite
)
1385 BytesToWrite
= Size
;
1387 DPRINT(MAX_TRACE
, ("Offset (0x%X) CurrentOffset (0x%X) ToWrite (%u) Skipped (%u)(%u) Size (%u).\n",
1389 (UINT
)CurrentOffset
,
1391 (UINT
)BytesSkipped
, (UINT
)Skip
,
1395 if (!WriteFile(DestFile
, (void*)((PUCHAR
)OutputBuffer
+ BytesSkipped
),
1396 BytesToWrite
, (LPDWORD
)&BytesWritten
, NULL
) ||
1397 (BytesToWrite
!= BytesWritten
))
1399 DPRINT(MIN_TRACE
, ("Status 0x%X.\n", (UINT
)GetLastError()));
1401 BytesWritten
= BytesToWrite
;
1402 if (fwrite((void*)((PUCHAR
)OutputBuffer
+ BytesSkipped
),
1403 BytesToWrite
, 1, DestFile
) < 1)
1406 CloseFile(DestFile
);
1408 DPRINT(MIN_TRACE
, ("Cannot write to file.\n"));
1409 return CAB_STATUS_CANNOT_WRITE
;
1411 Size
-= BytesToWrite
;
1413 CurrentOffset
+= BytesToWrite
;
1415 /* Don't skip any more bytes */
1420 CloseFile(DestFile
);
1424 return CAB_STATUS_SUCCESS
;
1427 bool CCabinet::IsCodecSelected()
1429 * FUNCTION: Returns the value of CodecSelected
1431 * Whether a codec is selected
1434 return CodecSelected
;
1437 void CCabinet::SelectCodec(LONG Id
)
1439 * FUNCTION: Selects codec engine to use
1441 * Id = Codec identifier
1449 CodecSelected
= false;
1456 Codec
= new CRawCodec();
1459 case CAB_CODEC_MSZIP
:
1460 Codec
= new CMSZipCodec();
1468 CodecSelected
= true;
1472 #ifndef CAB_READ_ONLY
1474 /* CAB write methods */
1476 ULONG
CCabinet::NewCabinet()
1478 * FUNCTION: Creates a new cabinet
1480 * Status of operation
1485 CurrentDiskNumber
= 0;
1487 OutputBuffer
= AllocateMemory(CAB_BLOCKSIZE
+ 12); // This should be enough
1488 InputBuffer
= AllocateMemory(CAB_BLOCKSIZE
+ 12); // This should be enough
1489 if ((!OutputBuffer
) || (!InputBuffer
))
1491 DPRINT(MIN_TRACE
, ("Insufficient memory.\n"));
1492 return CAB_STATUS_NOMEMORY
;
1494 CurrentIBuffer
= InputBuffer
;
1495 CurrentIBufferSize
= 0;
1497 CABHeader
.Signature
= CAB_SIGNATURE
;
1498 CABHeader
.Reserved1
= 0; // Not used
1499 CABHeader
.CabinetSize
= 0; // Not yet known
1500 CABHeader
.Reserved2
= 0; // Not used
1501 CABHeader
.Reserved3
= 0; // Not used
1502 CABHeader
.Version
= CAB_VERSION
;
1503 CABHeader
.FolderCount
= 0; // Not yet known
1504 CABHeader
.FileCount
= 0; // Not yet known
1505 CABHeader
.Flags
= 0; // Not yet known
1506 // FIXME: Should be random
1507 CABHeader
.SetID
= 0x534F;
1508 CABHeader
.CabinetNumber
= 0;
1511 TotalFolderSize
= 0;
1514 DiskSize
= sizeof(CFHEADER
);
1516 InitCabinetHeader();
1518 // NextFolderNumber is 0-based
1519 NextFolderNumber
= 0;
1521 CurrentFolderNode
= NULL
;
1522 Status
= NewFolder();
1523 if (Status
!= CAB_STATUS_SUCCESS
)
1526 CurrentFolderNode
->Folder
.DataOffset
= DiskSize
- TotalHeaderSize
;
1528 ScratchFile
= new CCFDATAStorage
;
1531 DPRINT(MIN_TRACE
, ("Insufficient memory.\n"));
1532 return CAB_STATUS_NOMEMORY
;
1535 Status
= ScratchFile
->Create();
1537 CreateNewFolder
= false;
1539 CreateNewDisk
= false;
1541 PrevCabinetNumber
= 0;
1547 ULONG
CCabinet::NewDisk()
1549 * FUNCTION: Forces a new disk to be created
1551 * Status of operation
1554 // NextFolderNumber is 0-based
1555 NextFolderNumber
= 1;
1557 CreateNewDisk
= false;
1559 DiskSize
= sizeof(CFHEADER
) + TotalFolderSize
+ TotalFileSize
;
1561 InitCabinetHeader();
1563 CurrentFolderNode
->TotalFolderSize
= 0;
1565 CurrentFolderNode
->Folder
.DataBlockCount
= 0;
1567 return CAB_STATUS_SUCCESS
;
1571 ULONG
CCabinet::NewFolder()
1573 * FUNCTION: Forces a new folder to be created
1575 * Status of operation
1578 DPRINT(MAX_TRACE
, ("Creating new folder.\n"));
1580 CurrentFolderNode
= NewFolderNode();
1581 if (!CurrentFolderNode
)
1583 DPRINT(MIN_TRACE
, ("Insufficient memory.\n"));
1584 return CAB_STATUS_NOMEMORY
;
1589 CurrentFolderNode
->Folder
.CompressionType
= CAB_COMP_NONE
;
1592 case CAB_CODEC_MSZIP
:
1593 CurrentFolderNode
->Folder
.CompressionType
= CAB_COMP_MSZIP
;
1597 return CAB_STATUS_UNSUPPCOMP
;
1600 /* FIXME: This won't work if no files are added to the new folder */
1602 DiskSize
+= sizeof(CFFOLDER
);
1604 TotalFolderSize
+= sizeof(CFFOLDER
);
1608 CABHeader
.FolderCount
++;
1612 return CAB_STATUS_SUCCESS
;
1616 ULONG
CCabinet::WriteFileToScratchStorage(PCFFILE_NODE FileNode
)
1618 * FUNCTION: Writes a file to the scratch file
1620 * FileNode = Pointer to file node
1622 * Status of operation
1632 /* Try to open file */
1634 SourceFile
= CreateFile(
1635 FileNode
->FileName
, // Open this file
1636 GENERIC_READ
, // Open for reading
1637 FILE_SHARE_READ
, // Share for reading
1638 NULL
, // No security
1639 OPEN_EXISTING
, // File must exist
1640 FILE_ATTRIBUTE_NORMAL
, // Normal file
1641 NULL
); // No attribute template
1642 if (SourceFile
== INVALID_HANDLE_VALUE
)
1644 DPRINT(MID_TRACE
, ("File not found (%s).\n", FileNode
->FileName
));
1645 return CAB_STATUS_NOFILE
;
1648 SourceFile
= fopen(FileNode
->FileName
, "rb");
1649 if (SourceFile
== NULL
)
1651 DPRINT(MID_TRACE
, ("Cannot open cabinet reserved file.\n"));
1652 return CAB_STATUS_NOFILE
;
1656 if (CreateNewFolder
)
1658 /* There is always a new folder after
1659 a split file is completely stored */
1660 Status
= NewFolder();
1661 if (Status
!= CAB_STATUS_SUCCESS
)
1663 CreateNewFolder
= false;
1666 /* Call OnAdd event handler */
1667 OnAdd(&FileNode
->File
, FileNode
->FileName
);
1669 TotalBytesLeft
= FileNode
->File
.FileSize
;
1671 FileNode
->File
.FileOffset
= CurrentFolderNode
->UncompOffset
;
1672 CurrentFolderNode
->UncompOffset
+= TotalBytesLeft
;
1673 FileNode
->File
.FileControlID
= (USHORT
)(NextFolderNumber
- 1);
1674 CurrentFolderNode
->Commit
= true;
1675 PrevCabinetNumber
= CurrentDiskNumber
;
1677 Size
= sizeof(CFFILE
) + (ULONG
)strlen(GetFileName(FileNode
->FileName
)) + 1;
1678 CABHeader
.FileTableOffset
+= Size
;
1679 TotalFileSize
+= Size
;
1683 FileNode
->Commit
= true;
1685 if (TotalBytesLeft
> 0)
1689 if (TotalBytesLeft
> (ULONG
)CAB_BLOCKSIZE
- CurrentIBufferSize
)
1690 BytesToRead
= CAB_BLOCKSIZE
- CurrentIBufferSize
;
1692 BytesToRead
= TotalBytesLeft
;
1694 if (!ReadFileData(SourceFile
, CurrentIBuffer
, BytesToRead
, &BytesRead
) || (BytesToRead
!= BytesRead
))
1696 DPRINT(MIN_TRACE
, ("Cannot read from file. BytesToRead (%u) BytesRead (%u) CurrentIBufferSize (%u).\n",
1697 (UINT
)BytesToRead
, (UINT
)BytesRead
, (UINT
)CurrentIBufferSize
));
1698 return CAB_STATUS_INVALID_CAB
;
1701 CurrentIBuffer
= (unsigned char*)CurrentIBuffer
+ BytesRead
;
1702 CurrentIBufferSize
+= (USHORT
)BytesRead
;
1704 if (CurrentIBufferSize
== CAB_BLOCKSIZE
)
1706 Status
= WriteDataBlock();
1707 if (Status
!= CAB_STATUS_SUCCESS
)
1710 TotalBytesLeft
-= BytesRead
;
1711 } while ((TotalBytesLeft
> 0) && (!CreateNewDisk
));
1714 if (TotalBytesLeft
== 0)
1716 CloseFile(SourceFile
);
1717 FileNode
->Delete
= true;
1719 if (FileNode
->File
.FileControlID
> CAB_FILE_MAX_FOLDER
)
1721 FileNode
->File
.FileControlID
= CAB_FILE_CONTINUED
;
1722 CurrentFolderNode
->Delete
= true;
1724 if ((CurrentIBufferSize
> 0) || (CurrentOBufferSize
> 0))
1726 Status
= WriteDataBlock();
1727 if (Status
!= CAB_STATUS_SUCCESS
)
1731 CreateNewFolder
= true;
1736 if (FileNode
->File
.FileControlID
<= CAB_FILE_MAX_FOLDER
)
1737 FileNode
->File
.FileControlID
= CAB_FILE_SPLIT
;
1739 FileNode
->File
.FileControlID
= CAB_FILE_PREV_NEXT
;
1742 return CAB_STATUS_SUCCESS
;
1746 ULONG
CCabinet::WriteDisk(ULONG MoreDisks
)
1748 * FUNCTION: Forces the current disk to be written
1750 * MoreDisks = true if there is one or more disks after this disk
1752 * Status of operation
1755 PCFFILE_NODE FileNode
;
1758 ContinueFile
= false;
1759 FileNode
= FileListHead
;
1760 while (FileNode
!= NULL
)
1762 Status
= WriteFileToScratchStorage(FileNode
);
1763 if (Status
!= CAB_STATUS_SUCCESS
)
1768 /* A data block could span more than two
1769 disks if MaxDiskSize is very small */
1770 while (CreateNewDisk
)
1772 DPRINT(MAX_TRACE
, ("Creating new disk.\n"));
1777 ContinueFile
= true;
1778 CreateNewDisk
= false;
1780 DPRINT(MAX_TRACE
, ("First on new disk. CurrentIBufferSize (%u) CurrentOBufferSize (%u).\n",
1781 (UINT
)CurrentIBufferSize
, (UINT
)CurrentOBufferSize
));
1783 if ((CurrentIBufferSize
> 0) || (CurrentOBufferSize
> 0))
1785 Status
= WriteDataBlock();
1786 if (Status
!= CAB_STATUS_SUCCESS
)
1793 ContinueFile
= false;
1794 FileNode
= FileNode
->Next
;
1798 if ((CurrentIBufferSize
> 0) || (CurrentOBufferSize
> 0))
1800 /* A data block could span more than two
1801 disks if MaxDiskSize is very small */
1803 ASSERT(CreateNewDisk
== false);
1809 DPRINT(MID_TRACE
, ("Creating new disk 2.\n"));
1813 CreateNewDisk
= false;
1815 ASSERT(FileNode
== FileListHead
);
1818 if ((CurrentIBufferSize
> 0) || (CurrentOBufferSize
> 0))
1820 Status
= WriteDataBlock();
1821 if (Status
!= CAB_STATUS_SUCCESS
)
1824 } while (CreateNewDisk
);
1826 CommitDisk(MoreDisks
);
1828 return CAB_STATUS_SUCCESS
;
1832 ULONG
CCabinet::CommitDisk(ULONG MoreDisks
)
1834 * FUNCTION: Commits the current disk
1836 * MoreDisks = true if there is one or more disks after this disk
1838 * Status of operation
1841 PCFFOLDER_NODE FolderNode
;
1844 OnCabinetName(CurrentDiskNumber
, CabinetName
);
1846 /* Create file, fail if it already exists */
1848 FileHandle
= CreateFile(CabinetName
, // Create this file
1849 GENERIC_WRITE
, // Open for writing
1851 NULL
, // No security
1852 CREATE_NEW
, // New file only
1853 FILE_ATTRIBUTE_NORMAL
, // Normal file
1854 NULL
); // No attribute template
1855 if (FileHandle
== INVALID_HANDLE_VALUE
)
1858 /* If file exists, ask to overwrite file */
1859 if (((Status
= GetLastError()) == ERROR_FILE_EXISTS
) &&
1860 (OnOverwrite(NULL
, CabinetName
)))
1863 /* Create cabinet file, overwrite if it already exists */
1864 FileHandle
= CreateFile(CabinetName
, // Create this file
1865 GENERIC_WRITE
, // Open for writing
1867 NULL
, // No security
1868 TRUNCATE_EXISTING
, // Truncate the file
1869 FILE_ATTRIBUTE_NORMAL
, // Normal file
1870 NULL
); // No attribute template
1871 if (FileHandle
== INVALID_HANDLE_VALUE
)
1872 return CAB_STATUS_CANNOT_CREATE
;
1876 if (Status
== ERROR_FILE_EXISTS
)
1877 return CAB_STATUS_FILE_EXISTS
;
1879 return CAB_STATUS_CANNOT_CREATE
;
1883 FileHandle
= fopen(CabinetName
, "rb");
1884 if (FileHandle
!= NULL
)
1887 /* If file exists, ask to overwrite file */
1888 if (OnOverwrite(NULL
, CabinetName
))
1890 FileHandle
= fopen(CabinetName
, "w+b");
1891 if (FileHandle
== NULL
)
1892 return CAB_STATUS_CANNOT_CREATE
;
1895 return CAB_STATUS_FILE_EXISTS
;
1900 FileHandle
= fopen(CabinetName
, "w+b");
1901 if (FileHandle
== NULL
)
1902 return CAB_STATUS_CANNOT_CREATE
;
1906 WriteCabinetHeader(MoreDisks
!= 0);
1908 Status
= WriteFolderEntries();
1909 if (Status
!= CAB_STATUS_SUCCESS
)
1912 /* Write file entries */
1915 /* Write data blocks */
1916 FolderNode
= FolderListHead
;
1917 while (FolderNode
!= NULL
)
1919 if (FolderNode
->Commit
)
1921 Status
= CommitDataBlocks(FolderNode
);
1922 if (Status
!= CAB_STATUS_SUCCESS
)
1924 /* Remove data blocks for folder */
1925 DestroyDataNodes(FolderNode
);
1927 FolderNode
= FolderNode
->Next
;
1930 CloseFile(FileHandle
);
1932 ScratchFile
->Truncate();
1934 return CAB_STATUS_SUCCESS
;
1938 ULONG
CCabinet::CloseDisk()
1940 * FUNCTION: Closes the current disk
1942 * Status of operation
1945 DestroyDeletedFileNodes();
1947 /* Destroy folder nodes that are completely stored */
1948 DestroyDeletedFolderNodes();
1950 CurrentDiskNumber
++;
1952 return CAB_STATUS_SUCCESS
;
1956 ULONG
CCabinet::CloseCabinet()
1958 * FUNCTION: Closes the current cabinet
1960 * Status of operation
1967 DestroyFolderNodes();
1971 FreeMemory(InputBuffer
);
1977 FreeMemory(OutputBuffer
);
1978 OutputBuffer
= NULL
;
1985 Status
= ScratchFile
->Destroy();
1990 return CAB_STATUS_SUCCESS
;
1994 ULONG
CCabinet::AddFile(char* FileName
)
1996 * FUNCTION: Adds a file to the current disk
1998 * FileName = Pointer to string with file name (full path)
2000 * Status of operation
2004 PCFFILE_NODE FileNode
;
2007 NewFileName
= (char*)AllocateMemory(strlen(FileName
) + 1);
2010 DPRINT(MIN_TRACE
, ("Insufficient memory.\n"));
2011 return CAB_STATUS_NOMEMORY
;
2013 strcpy(NewFileName
, FileName
);
2014 ConvertPath(NewFileName
, false);
2016 /* Try to open file */
2018 SrcFile
= CreateFile(
2019 NewFileName
, // Open this file
2020 GENERIC_READ
, // Open for reading
2021 FILE_SHARE_READ
, // Share for reading
2022 NULL
, // No security
2023 OPEN_EXISTING
, // File must exist
2024 FILE_ATTRIBUTE_NORMAL
, // Normal file
2025 NULL
); // No attribute template
2026 if (SrcFile
== INVALID_HANDLE_VALUE
)
2028 DPRINT(MID_TRACE
, ("File not found (%s).\n", NewFileName
));
2029 FreeMemory(NewFileName
);
2030 return CAB_STATUS_CANNOT_OPEN
;
2033 SrcFile
= fopen(NewFileName
, "rb");
2034 if (SrcFile
== NULL
)
2036 DPRINT(MID_TRACE
, ("File not found (%s).\n", NewFileName
));
2037 FreeMemory(NewFileName
);
2038 return CAB_STATUS_CANNOT_OPEN
;
2042 FileNode
= NewFileNode();
2045 DPRINT(MIN_TRACE
, ("Insufficient memory.\n"));
2046 FreeMemory(NewFileName
);
2048 return CAB_STATUS_NOMEMORY
;
2051 FileNode
->FolderNode
= CurrentFolderNode
;
2052 FileNode
->FileName
= NewFileName
;
2054 /* FIXME: Check for and handle large files (>= 2GB) */
2055 FileNode
->File
.FileSize
= GetSizeOfFile(SrcFile
);
2056 if (FileNode
->File
.FileSize
== (ULONG
)-1)
2058 DPRINT(MIN_TRACE
, ("Cannot read from file.\n"));
2059 FreeMemory(NewFileName
);
2061 return CAB_STATUS_CANNOT_READ
;
2064 if (GetFileTimes(SrcFile
, FileNode
) != CAB_STATUS_SUCCESS
)
2066 DPRINT(MIN_TRACE
, ("Cannot read file times.\n"));
2067 FreeMemory(NewFileName
);
2069 return CAB_STATUS_CANNOT_READ
;
2072 if (GetAttributesOnFile(FileNode
) != CAB_STATUS_SUCCESS
)
2074 DPRINT(MIN_TRACE
, ("Cannot read file attributes.\n"));
2075 FreeMemory(NewFileName
);
2077 return CAB_STATUS_CANNOT_READ
;
2082 return CAB_STATUS_SUCCESS
;
2085 bool CCabinet::CreateSimpleCabinet()
2087 * FUNCTION: Create a simple cabinet based on the files in the criteria list
2092 char szFilePath
[PATH_MAX
];
2093 char szFile
[PATH_MAX
];
2094 PSEARCH_CRITERIA Criteria
;
2099 WIN32_FIND_DATA FindFileData
;
2106 // Initialize a new cabinet
2107 Status
= NewCabinet();
2108 if (Status
!= CAB_STATUS_SUCCESS
)
2110 DPRINT(MIN_TRACE
, ("Cannot create cabinet (%u).\n", (UINT
)Status
));
2114 // Add each file in the criteria list
2115 Criteria
= CriteriaListHead
;
2119 // Store the file path with a trailing slash in szFilePath
2120 ConvertPath(Criteria
->Search
, false);
2121 pszFile
= strrchr(Criteria
->Search
, DIR_SEPARATOR_CHAR
);
2125 // Set the pointer to the start of the file name, not the slash
2128 strncpy(szFilePath
, Criteria
->Search
, pszFile
- Criteria
->Search
);
2129 szFilePath
[pszFile
- Criteria
->Search
] = 0;
2133 pszFile
= Criteria
->Search
;
2138 // needed for opendir()
2139 strcpy(szFilePath
, "./");
2144 // Windows: Use the easy FindFirstFile/FindNextFile API for getting all files and checking them against the pattern
2145 hFind
= FindFirstFile(Criteria
->Search
, &FindFileData
);
2147 // Don't stop if a search criteria is not found
2148 if(hFind
== INVALID_HANDLE_VALUE
&& GetLastError() != ERROR_FILE_NOT_FOUND
)
2150 DPRINT(MIN_TRACE
, ("FindFirstFile failed, Criteria: %s, error code is %u\n", Criteria
->Search
, (UINT
)GetLastError()));
2156 if(!(FindFileData
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
))
2158 strcpy(szFile
, szFilePath
);
2159 strcat(szFile
, FindFileData
.cFileName
);
2161 Status
= AddFile(szFile
);
2163 if(Status
!= CAB_STATUS_SUCCESS
)
2165 DPRINT(MIN_TRACE
, ("Cannot add file to cabinet (%u).\n", (UINT
)Status
));
2170 while(FindNextFile(hFind
, &FindFileData
));
2174 // Unix: Use opendir/readdir to loop through all entries, stat to check if it's a file and MatchFileNamePattern to match the file against the pattern
2175 dirp
= opendir(szFilePath
);
2179 while( (dp
= readdir(dirp
)) )
2181 strcpy(szFile
, szFilePath
);
2182 strcat(szFile
, dp
->d_name
);
2184 if(stat(szFile
, &stbuf
) == 0)
2186 if(stbuf
.st_mode
!= S_IFDIR
)
2188 if(MatchFileNamePattern(dp
->d_name
, pszFile
))
2190 Status
= AddFile(szFile
);
2192 if(Status
!= CAB_STATUS_SUCCESS
)
2194 DPRINT(MIN_TRACE
, ("Cannot add file to cabinet (%u).\n", (UINT
)Status
));
2202 DPRINT(MIN_TRACE
, ("stat failed, error code is %i\n", errno
));
2211 Criteria
= Criteria
->Next
;
2214 Status
= WriteDisk(false);
2215 if (Status
== CAB_STATUS_SUCCESS
)
2216 Status
= CloseDisk();
2217 if (Status
!= CAB_STATUS_SUCCESS
)
2219 DPRINT(MIN_TRACE
, ("Cannot write disk (%u).\n", (UINT
)Status
));
2228 DestroySearchCriteria();
2232 void CCabinet::SetMaxDiskSize(ULONG Size
)
2234 * FUNCTION: Sets the maximum size of the current disk
2236 * Size = Maximum size of current disk (0 means no maximum size)
2242 #endif /* CAB_READ_ONLY */
2245 /* Default event handlers */
2247 bool CCabinet::OnOverwrite(PCFFILE File
,
2250 * FUNCTION: Called when extracting a file and it already exists
2252 * File = Pointer to CFFILE for file being extracted
2253 * FileName = Pointer to buffer with name of file (full path)
2255 * true if the file should be overwritten, false if not
2262 void CCabinet::OnExtract(PCFFILE File
,
2265 * FUNCTION: Called just before extracting a file
2267 * File = Pointer to CFFILE for file being extracted
2268 * FileName = Pointer to buffer with name of file (full path)
2274 void CCabinet::OnDiskChange(char* CabinetName
,
2277 * FUNCTION: Called when a new disk is to be processed
2279 * CabinetName = Pointer to buffer with name of cabinet
2280 * DiskLabel = Pointer to buffer with label of disk
2286 #ifndef CAB_READ_ONLY
2288 void CCabinet::OnAdd(PCFFILE File
,
2291 * FUNCTION: Called just before adding a file to a cabinet
2293 * File = Pointer to CFFILE for file being added
2294 * FileName = Pointer to buffer with name of file (full path)
2300 bool CCabinet::OnDiskLabel(ULONG Number
, char* Label
)
2302 * FUNCTION: Called when a disk needs a label
2304 * Number = Cabinet number that needs a label
2305 * Label = Pointer to buffer to place label of disk
2307 * true if a disk label was returned, false if not
2314 bool CCabinet::OnCabinetName(ULONG Number
, char* Name
)
2316 * FUNCTION: Called when a cabinet needs a name
2318 * Number = Disk number that needs a name
2319 * Name = Pointer to buffer to place name of cabinet
2321 * true if a cabinet name was returned, false if not
2327 #endif /* CAB_READ_ONLY */
2329 PCFFOLDER_NODE
CCabinet::LocateFolderNode(ULONG Index
)
2331 * FUNCTION: Locates a folder node
2333 * Index = Folder index
2335 * Pointer to folder node or NULL if the folder node was not found
2338 PCFFOLDER_NODE Node
;
2342 case CAB_FILE_SPLIT
:
2343 return FolderListTail
;
2345 case CAB_FILE_CONTINUED
:
2346 case CAB_FILE_PREV_NEXT
:
2347 return FolderListHead
;
2350 Node
= FolderListHead
;
2351 while (Node
!= NULL
)
2353 if (Node
->Index
== Index
)
2361 ULONG
CCabinet::GetAbsoluteOffset(PCFFILE_NODE File
)
2363 * FUNCTION: Returns the absolute offset of a file
2365 * File = Pointer to CFFILE_NODE structure for file
2367 * Status of operation
2372 DPRINT(MAX_TRACE
, ("FileName '%s' FileOffset (0x%X) FileSize (%u).\n",
2374 (UINT
)File
->File
.FileOffset
,
2375 (UINT
)File
->File
.FileSize
));
2377 Node
= CurrentFolderNode
->DataListHead
;
2378 while (Node
!= NULL
)
2380 DPRINT(MAX_TRACE
, ("GetAbsoluteOffset(): Comparing (0x%X, 0x%X) (%u).\n",
2381 (UINT
)Node
->UncompOffset
,
2382 (UINT
)(Node
->UncompOffset
+ Node
->Data
.UncompSize
),
2383 (UINT
)Node
->Data
.UncompSize
));
2385 /* Node->Data.UncompSize will be 0 if the block is split
2386 (ie. it is the last block in this cabinet) */
2387 if ((Node
->Data
.UncompSize
== 0) ||
2388 ((File
->File
.FileOffset
>= Node
->UncompOffset
) &&
2389 (File
->File
.FileOffset
< Node
->UncompOffset
+
2390 Node
->Data
.UncompSize
)))
2392 File
->DataBlock
= Node
;
2393 return CAB_STATUS_SUCCESS
;
2398 return CAB_STATUS_INVALID_CAB
;
2402 ULONG
CCabinet::LocateFile(char* FileName
,
2405 * FUNCTION: Locates a file in the cabinet
2407 * FileName = Pointer to string with name of file to locate
2408 * File = Address of pointer to CFFILE_NODE structure to fill
2410 * Status of operation
2412 * Current folder is set to the folder of the file
2418 DPRINT(MAX_TRACE
, ("FileName '%s'\n", FileName
));
2420 Node
= FileListHead
;
2421 while (Node
!= NULL
)
2423 if (strcasecmp(FileName
, Node
->FileName
) == 0)
2425 CurrentFolderNode
= LocateFolderNode(Node
->File
.FileControlID
);
2426 if (!CurrentFolderNode
)
2428 DPRINT(MID_TRACE
, ("Folder with index number (%u) not found.\n",
2429 Node
->File
.FileControlID
));
2430 return CAB_STATUS_INVALID_CAB
;
2433 if (Node
->DataBlock
== NULL
)
2434 Status
= GetAbsoluteOffset(Node
);
2436 Status
= CAB_STATUS_SUCCESS
;
2443 return CAB_STATUS_NOFILE
;
2447 ULONG
CCabinet::ReadString(char* String
, LONG MaxLength
)
2449 * FUNCTION: Reads a NULL-terminated string from the cabinet
2451 * String = Pointer to buffer to place string
2452 * MaxLength = Maximum length of string
2454 * Status of operation
2464 Status
= ReadBlock(String
, MaxLength
, &BytesRead
);
2465 if (Status
!= CAB_STATUS_SUCCESS
)
2467 DPRINT(MIN_TRACE
, ("Cannot read from file (%u).\n", (UINT
)Status
));
2468 return CAB_STATUS_INVALID_CAB
;
2471 // Find the terminating NULL character
2472 for (Size
= 0; Size
< MaxLength
; Size
++)
2474 if (String
[Size
] == '\0')
2483 DPRINT(MIN_TRACE
, ("Filename in the cabinet file is too long.\n"));
2484 return CAB_STATUS_INVALID_CAB
;
2487 // Compute the offset of the next CFFILE.
2488 // We have to subtract from the current offset here, because we read MaxLength characters above and most-probably the file name isn't MaxLength characters long.
2489 // + 1 to skip the terminating NULL character as well.
2490 Size
= -(MaxLength
- Size
) + 1;
2493 if( SetFilePointer(FileHandle
,
2496 FILE_CURRENT
) == INVALID_SET_FILE_POINTER
)
2498 DPRINT(MIN_TRACE
, ("SetFilePointer() failed, error code is %u.\n", (UINT
)GetLastError()));
2499 return CAB_STATUS_INVALID_CAB
;
2502 if (fseek(FileHandle
, (off_t
)Size
, SEEK_CUR
) != 0)
2504 DPRINT(MIN_TRACE
, ("fseek() failed.\n"));
2505 return CAB_STATUS_INVALID_CAB
;
2508 return CAB_STATUS_SUCCESS
;
2512 ULONG
CCabinet::ReadFileTable()
2514 * FUNCTION: Reads the file table from the cabinet file
2516 * Status of operation
2524 DPRINT(MAX_TRACE
, ("Reading file table at absolute offset (0x%X).\n",
2525 (UINT
)CABHeader
.FileTableOffset
));
2527 /* Seek to file table */
2529 if( SetFilePointer(FileHandle
,
2530 CABHeader
.FileTableOffset
,
2532 FILE_BEGIN
) == INVALID_SET_FILE_POINTER
)
2534 DPRINT(MIN_TRACE
, ("SetFilePointer() failed, error code is %u.\n", (UINT
)GetLastError()));
2535 return CAB_STATUS_INVALID_CAB
;
2538 if (fseek(FileHandle
, (off_t
)CABHeader
.FileTableOffset
, SEEK_SET
) != 0)
2540 DPRINT(MIN_TRACE
, ("fseek() failed.\n"));
2541 return CAB_STATUS_INVALID_CAB
;
2545 for (i
= 0; i
< CABHeader
.FileCount
; i
++)
2547 File
= NewFileNode();
2550 DPRINT(MIN_TRACE
, ("Insufficient memory.\n"));
2551 return CAB_STATUS_NOMEMORY
;
2554 if ((Status
= ReadBlock(&File
->File
, sizeof(CFFILE
),
2555 &BytesRead
)) != CAB_STATUS_SUCCESS
)
2557 DPRINT(MIN_TRACE
, ("Cannot read from file (%u).\n", (UINT
)Status
));
2558 return CAB_STATUS_INVALID_CAB
;
2561 File
->FileName
= (char*)AllocateMemory(PATH_MAX
);
2562 if (!File
->FileName
)
2564 DPRINT(MIN_TRACE
, ("Insufficient memory.\n"));
2565 return CAB_STATUS_NOMEMORY
;
2568 /* Read file name */
2569 Status
= ReadString(File
->FileName
, PATH_MAX
);
2570 if (Status
!= CAB_STATUS_SUCCESS
)
2573 DPRINT(MAX_TRACE
, ("Found file '%s' at uncompressed offset (0x%X). Size (%u bytes) ControlId (0x%X).\n",
2575 (UINT
)File
->File
.FileOffset
,
2576 (UINT
)File
->File
.FileSize
,
2577 File
->File
.FileControlID
));
2580 return CAB_STATUS_SUCCESS
;
2584 ULONG
CCabinet::ReadDataBlocks(PCFFOLDER_NODE FolderNode
)
2586 * FUNCTION: Reads all CFDATA blocks for a folder from the cabinet file
2588 * FolderNode = Pointer to CFFOLDER_NODE structure for folder
2590 * Status of operation
2593 ULONG AbsoluteOffset
;
2600 DPRINT(MAX_TRACE
, ("Reading data blocks for folder (%u) at absolute offset (0x%X).\n",
2601 (UINT
)FolderNode
->Index
, (UINT
)FolderNode
->Folder
.DataOffset
));
2603 AbsoluteOffset
= FolderNode
->Folder
.DataOffset
;
2604 UncompOffset
= FolderNode
->UncompOffset
;
2606 for (i
= 0; i
< FolderNode
->Folder
.DataBlockCount
; i
++)
2608 Node
= NewDataNode(FolderNode
);
2611 DPRINT(MIN_TRACE
, ("Insufficient memory.\n"));
2612 return CAB_STATUS_NOMEMORY
;
2615 /* Seek to data block */
2617 if( SetFilePointer(FileHandle
,
2620 FILE_BEGIN
) == INVALID_SET_FILE_POINTER
)
2622 DPRINT(MIN_TRACE
, ("SetFilePointer() failed, error code is %u.\n", (UINT
)GetLastError()));
2623 return CAB_STATUS_INVALID_CAB
;
2626 if (fseek(FileHandle
, (off_t
)AbsoluteOffset
, SEEK_SET
) != 0)
2628 DPRINT(MIN_TRACE
, ("fseek() failed.\n"));
2629 return CAB_STATUS_INVALID_CAB
;
2633 if ((Status
= ReadBlock(&Node
->Data
, sizeof(CFDATA
),
2634 &BytesRead
)) != CAB_STATUS_SUCCESS
)
2636 DPRINT(MIN_TRACE
, ("Cannot read from file (%u).\n", (UINT
)Status
));
2637 return CAB_STATUS_INVALID_CAB
;
2640 DPRINT(MAX_TRACE
, ("AbsOffset (0x%X) UncompOffset (0x%X) Checksum (0x%X) CompSize (%u) UncompSize (%u).\n",
2641 (UINT
)AbsoluteOffset
,
2643 (UINT
)Node
->Data
.Checksum
,
2644 Node
->Data
.CompSize
,
2645 Node
->Data
.UncompSize
));
2647 Node
->AbsoluteOffset
= AbsoluteOffset
;
2648 Node
->UncompOffset
= UncompOffset
;
2650 AbsoluteOffset
+= sizeof(CFDATA
) + Node
->Data
.CompSize
;
2651 UncompOffset
+= Node
->Data
.UncompSize
;
2654 FolderUncompSize
= UncompOffset
;
2656 return CAB_STATUS_SUCCESS
;
2660 PCFFOLDER_NODE
CCabinet::NewFolderNode()
2662 * FUNCTION: Creates a new folder node
2664 * Pointer to node if there was enough free memory available, otherwise NULL
2667 PCFFOLDER_NODE Node
;
2669 Node
= (PCFFOLDER_NODE
)AllocateMemory(sizeof(CFFOLDER_NODE
));
2673 memset(Node
, 0, sizeof(CFFOLDER_NODE
));
2675 Node
->Folder
.CompressionType
= CAB_COMP_NONE
;
2677 Node
->Prev
= FolderListTail
;
2679 if (FolderListTail
!= NULL
)
2680 FolderListTail
->Next
= Node
;
2682 FolderListHead
= Node
;
2684 FolderListTail
= Node
;
2690 PCFFILE_NODE
CCabinet::NewFileNode()
2692 * FUNCTION: Creates a new file node
2694 * FolderNode = Pointer to folder node to bind file to
2696 * Pointer to node if there was enough free memory available, otherwise NULL
2701 Node
= (PCFFILE_NODE
)AllocateMemory(sizeof(CFFILE_NODE
));
2705 memset(Node
, 0, sizeof(CFFILE_NODE
));
2707 Node
->Prev
= FileListTail
;
2709 if (FileListTail
!= NULL
)
2710 FileListTail
->Next
= Node
;
2712 FileListHead
= Node
;
2714 FileListTail
= Node
;
2720 PCFDATA_NODE
CCabinet::NewDataNode(PCFFOLDER_NODE FolderNode
)
2722 * FUNCTION: Creates a new data block node
2724 * FolderNode = Pointer to folder node to bind data block to
2726 * Pointer to node if there was enough free memory available, otherwise NULL
2731 Node
= (PCFDATA_NODE
)AllocateMemory(sizeof(CFDATA_NODE
));
2735 memset(Node
, 0, sizeof(CFDATA_NODE
));
2737 Node
->Prev
= FolderNode
->DataListTail
;
2739 if (FolderNode
->DataListTail
!= NULL
)
2740 FolderNode
->DataListTail
->Next
= Node
;
2742 FolderNode
->DataListHead
= Node
;
2744 FolderNode
->DataListTail
= Node
;
2750 void CCabinet::DestroyDataNodes(PCFFOLDER_NODE FolderNode
)
2752 * FUNCTION: Destroys data block nodes bound to a folder node
2754 * FolderNode = Pointer to folder node
2757 PCFDATA_NODE PrevNode
;
2758 PCFDATA_NODE NextNode
;
2760 NextNode
= FolderNode
->DataListHead
;
2761 while (NextNode
!= NULL
)
2763 PrevNode
= NextNode
->Next
;
2764 FreeMemory(NextNode
);
2765 NextNode
= PrevNode
;
2767 FolderNode
->DataListHead
= NULL
;
2768 FolderNode
->DataListTail
= NULL
;
2772 void CCabinet::DestroyFileNodes()
2774 * FUNCTION: Destroys file nodes
2777 PCFFILE_NODE PrevNode
;
2778 PCFFILE_NODE NextNode
;
2780 NextNode
= FileListHead
;
2781 while (NextNode
!= NULL
)
2783 PrevNode
= NextNode
->Next
;
2784 if (NextNode
->FileName
)
2785 FreeMemory(NextNode
->FileName
);
2786 FreeMemory(NextNode
);
2787 NextNode
= PrevNode
;
2789 FileListHead
= NULL
;
2790 FileListTail
= NULL
;
2794 void CCabinet::DestroyDeletedFileNodes()
2796 * FUNCTION: Destroys file nodes that are marked for deletion
2799 PCFFILE_NODE CurNode
;
2800 PCFFILE_NODE NextNode
;
2802 CurNode
= FileListHead
;
2803 while (CurNode
!= NULL
)
2805 NextNode
= CurNode
->Next
;
2807 if (CurNode
->Delete
)
2809 if (CurNode
->Prev
!= NULL
)
2810 CurNode
->Prev
->Next
= CurNode
->Next
;
2813 FileListHead
= CurNode
->Next
;
2815 FileListHead
->Prev
= NULL
;
2818 if (CurNode
->Next
!= NULL
)
2819 CurNode
->Next
->Prev
= CurNode
->Prev
;
2822 FileListTail
= CurNode
->Prev
;
2824 FileListTail
->Next
= NULL
;
2827 DPRINT(MAX_TRACE
, ("Deleting file: '%s'\n", CurNode
->FileName
));
2829 TotalFileSize
-= (sizeof(CFFILE
) + (ULONG
)strlen(GetFileName(CurNode
->FileName
)) + 1);
2831 if (CurNode
->FileName
)
2832 FreeMemory(CurNode
->FileName
);
2833 FreeMemory(CurNode
);
2840 void CCabinet::DestroyFolderNodes()
2842 * FUNCTION: Destroys folder nodes
2845 PCFFOLDER_NODE PrevNode
;
2846 PCFFOLDER_NODE NextNode
;
2848 NextNode
= FolderListHead
;
2849 while (NextNode
!= NULL
)
2851 PrevNode
= NextNode
->Next
;
2852 DestroyDataNodes(NextNode
);
2853 FreeMemory(NextNode
);
2854 NextNode
= PrevNode
;
2856 FolderListHead
= NULL
;
2857 FolderListTail
= NULL
;
2861 void CCabinet::DestroyDeletedFolderNodes()
2863 * FUNCTION: Destroys folder nodes that are marked for deletion
2866 PCFFOLDER_NODE CurNode
;
2867 PCFFOLDER_NODE NextNode
;
2869 CurNode
= FolderListHead
;
2870 while (CurNode
!= NULL
)
2872 NextNode
= CurNode
->Next
;
2874 if (CurNode
->Delete
)
2876 if (CurNode
->Prev
!= NULL
)
2877 CurNode
->Prev
->Next
= CurNode
->Next
;
2880 FolderListHead
= CurNode
->Next
;
2882 FolderListHead
->Prev
= NULL
;
2885 if (CurNode
->Next
!= NULL
)
2886 CurNode
->Next
->Prev
= CurNode
->Prev
;
2889 FolderListTail
= CurNode
->Prev
;
2891 FolderListTail
->Next
= NULL
;
2894 DestroyDataNodes(CurNode
);
2895 FreeMemory(CurNode
);
2897 TotalFolderSize
-= sizeof(CFFOLDER
);
2904 ULONG
CCabinet::ComputeChecksum(void* Buffer
,
2908 * FUNCTION: Computes checksum for data block
2910 * Buffer = Pointer to data buffer
2911 * Size = Length of data buffer
2912 * Seed = Previously computed checksum
2914 * Checksum of buffer
2917 int UlongCount
; // Number of ULONGs in block
2918 ULONG Checksum
; // Checksum accumulator
2922 /* FIXME: Doesn't seem to be correct. EXTRACT.EXE
2923 won't accept checksums computed by this routine */
2925 DPRINT(MIN_TRACE
, ("Checksumming buffer (0x%p) Size (%u)\n", Buffer
, (UINT
)Size
));
2927 UlongCount
= Size
/ 4; // Number of ULONGs
2928 Checksum
= Seed
; // Init checksum
2929 pb
= (unsigned char*)Buffer
; // Start at front of data block
2931 /* Checksum integral multiple of ULONGs */
2932 while (UlongCount
-- > 0)
2934 /* NOTE: Build ULONG in big/little-endian independent manner */
2935 ul
= *pb
++; // Get low-order byte
2936 ul
|= (((ULONG
)(*pb
++)) << 8); // Add 2nd byte
2937 ul
|= (((ULONG
)(*pb
++)) << 16); // Add 3nd byte
2938 ul
|= (((ULONG
)(*pb
++)) << 24); // Add 4th byte
2940 Checksum
^= ul
; // Update checksum
2943 /* Checksum remainder bytes */
2948 ul
|= (((ULONG
)(*pb
++)) << 16); // Add 3rd byte
2950 ul
|= (((ULONG
)(*pb
++)) << 8); // Add 2nd byte
2952 ul
|= *pb
++; // Get low-order byte
2956 Checksum
^= ul
; // Update checksum
2958 /* Return computed checksum */
2963 ULONG
CCabinet::ReadBlock(void* Buffer
,
2967 * FUNCTION: Read a block of data from file
2969 * Buffer = Pointer to data buffer
2970 * Size = Length of data buffer
2971 * BytesRead = Pointer to ULONG that on return will contain
2972 * number of bytes read
2974 * Status of operation
2977 if (!ReadFileData(FileHandle
, Buffer
, Size
, BytesRead
))
2978 return CAB_STATUS_INVALID_CAB
;
2979 return CAB_STATUS_SUCCESS
;
2982 bool CCabinet::MatchFileNamePattern(char* FileName
, char* Pattern
)
2984 * FUNCTION: Matches a wildcard character pattern against a file
2986 * FileName = The file name to check
2987 * Pattern = The pattern
2989 * Whether the pattern matches the file
2992 * This function is based on Busybox code, Copyright (C) 1998 by Erik Andersen, released under GPL2 or any later version.
2993 * Adapted from code written by Ingo Wilken.
2994 * Original location: http://www.busybox.net/cgi-bin/viewcvs.cgi/trunk/busybox/utility.c?rev=5&view=markup
2997 char* retryPattern
= NULL
;
2998 char* retryFileName
= NULL
;
3001 while (*FileName
|| *Pattern
)
3008 retryPattern
= Pattern
;
3009 retryFileName
= FileName
;
3013 if (*FileName
++ == '\0')
3019 if (*FileName
== ch
)
3028 Pattern
= retryPattern
;
3029 FileName
= ++retryFileName
;
3043 #ifndef CAB_READ_ONLY
3045 ULONG
CCabinet::InitCabinetHeader()
3047 * FUNCTION: Initializes cabinet header and optional fields
3049 * Status of operation
3055 CABHeader
.FileTableOffset
= 0; // Not known yet
3056 CABHeader
.FolderCount
= 0; // Not known yet
3057 CABHeader
.FileCount
= 0; // Not known yet
3058 CABHeader
.Flags
= 0; // Not known yet
3060 CABHeader
.CabinetNumber
= (USHORT
)CurrentDiskNumber
;
3062 if ((CurrentDiskNumber
> 0) && (OnCabinetName(PrevCabinetNumber
, CabinetPrev
)))
3064 CABHeader
.Flags
|= CAB_FLAG_HASPREV
;
3065 if (!OnDiskLabel(PrevCabinetNumber
, DiskPrev
))
3066 strcpy(CabinetPrev
, "");
3069 if (OnCabinetName(CurrentDiskNumber
+ 1, CabinetNext
))
3071 CABHeader
.Flags
|= CAB_FLAG_HASNEXT
;
3072 if (!OnDiskLabel(CurrentDiskNumber
+ 1, DiskNext
))
3073 strcpy(DiskNext
, "");
3078 if ((CABHeader
.Flags
& CAB_FLAG_HASPREV
) > 0)
3081 DPRINT(MAX_TRACE
, ("CabinetPrev '%s'.\n", CabinetPrev
));
3083 /* Calculate size of name of previous cabinet */
3084 TotalSize
+= (ULONG
)strlen(CabinetPrev
) + 1;
3086 /* Calculate size of label of previous disk */
3087 TotalSize
+= (ULONG
)strlen(DiskPrev
) + 1;
3090 if ((CABHeader
.Flags
& CAB_FLAG_HASNEXT
) > 0)
3093 DPRINT(MAX_TRACE
, ("CabinetNext '%s'.\n", CabinetNext
));
3095 /* Calculate size of name of next cabinet */
3096 Size
= (ULONG
)strlen(CabinetNext
) + 1;
3098 NextFieldsSize
= Size
;
3100 /* Calculate size of label of next disk */
3101 Size
= (ULONG
)strlen(DiskNext
) + 1;
3103 NextFieldsSize
+= Size
;
3108 /* Add cabinet reserved area size if present */
3109 if (CabinetReservedFileSize
> 0)
3111 CABHeader
.Flags
|= CAB_FLAG_RESERVE
;
3112 TotalSize
+= CabinetReservedFileSize
;
3113 TotalSize
+= sizeof(ULONG
); /* For CabinetResSize, FolderResSize, and FileResSize fields */
3116 DiskSize
+= TotalSize
;
3118 TotalHeaderSize
= sizeof(CFHEADER
) + TotalSize
;
3120 return CAB_STATUS_SUCCESS
;
3124 ULONG
CCabinet::WriteCabinetHeader(bool MoreDisks
)
3126 * FUNCTION: Writes the cabinet header and optional fields
3128 * MoreDisks = true if next cabinet name should be included
3130 * Status of operation
3133 PCFFOLDER_NODE FolderNode
;
3134 PCFFILE_NODE FileNode
;
3140 CABHeader
.Flags
|= CAB_FLAG_HASNEXT
;
3141 Size
= TotalHeaderSize
;
3145 CABHeader
.Flags
&= ~CAB_FLAG_HASNEXT
;
3146 DiskSize
-= NextFieldsSize
;
3147 Size
= TotalHeaderSize
- NextFieldsSize
;
3150 /* Set absolute folder offsets */
3151 BytesWritten
= Size
+ TotalFolderSize
+ TotalFileSize
;
3152 CABHeader
.FolderCount
= 0;
3153 FolderNode
= FolderListHead
;
3154 while (FolderNode
!= NULL
)
3156 FolderNode
->Folder
.DataOffset
= BytesWritten
;
3158 BytesWritten
+= FolderNode
->TotalFolderSize
;
3160 CABHeader
.FolderCount
++;
3162 FolderNode
= FolderNode
->Next
;
3165 /* Set absolute offset of file table */
3166 CABHeader
.FileTableOffset
= Size
+ TotalFolderSize
;
3168 /* Count number of files to be committed */
3169 CABHeader
.FileCount
= 0;
3170 FileNode
= FileListHead
;
3171 while (FileNode
!= NULL
)
3173 if (FileNode
->Commit
)
3174 CABHeader
.FileCount
++;
3175 FileNode
= FileNode
->Next
;
3178 CABHeader
.CabinetSize
= DiskSize
;
3182 if (!WriteFile(FileHandle
, &CABHeader
, sizeof(CFHEADER
), (LPDWORD
)&BytesWritten
, NULL
))
3184 DPRINT(MIN_TRACE
, ("Cannot write to file.\n"));
3185 return CAB_STATUS_CANNOT_WRITE
;
3188 BytesWritten
= sizeof(CFHEADER
);
3189 if (fwrite(&CABHeader
, sizeof(CFHEADER
), 1, FileHandle
) < 1)
3191 DPRINT(MIN_TRACE
, ("Cannot write to file.\n"));
3192 return CAB_STATUS_CANNOT_WRITE
;
3196 /* Write per-cabinet reserved area if present */
3197 if (CABHeader
.Flags
& CAB_FLAG_RESERVE
)
3201 ReservedSize
= CabinetReservedFileSize
& 0xffff;
3202 ReservedSize
|= (0 << 16); /* Folder reserved area size */
3203 ReservedSize
|= (0 << 24); /* Folder reserved area size */
3205 if (!WriteFile(FileHandle
, &ReservedSize
, sizeof(ULONG
), (LPDWORD
)&BytesWritten
, NULL
))
3207 DPRINT(MIN_TRACE
, ("Cannot write to file.\n"));
3208 return CAB_STATUS_CANNOT_WRITE
;
3211 BytesWritten
= sizeof(ULONG
);
3212 if (fwrite(&ReservedSize
, sizeof(ULONG
), 1, FileHandle
) < 1)
3214 DPRINT(MIN_TRACE
, ("Cannot write to file.\n"));
3215 return CAB_STATUS_CANNOT_WRITE
;
3220 if (!WriteFile(FileHandle
, CabinetReservedFileBuffer
, CabinetReservedFileSize
, (LPDWORD
)&BytesWritten
, NULL
))
3222 DPRINT(MIN_TRACE
, ("Cannot write to file.\n"));
3223 return CAB_STATUS_CANNOT_WRITE
;
3226 BytesWritten
= CabinetReservedFileSize
;
3227 if (fwrite(CabinetReservedFileBuffer
, CabinetReservedFileSize
, 1, FileHandle
) < 1)
3229 DPRINT(MIN_TRACE
, ("Cannot write to file.\n"));
3230 return CAB_STATUS_CANNOT_WRITE
;
3235 if ((CABHeader
.Flags
& CAB_FLAG_HASPREV
) > 0)
3237 DPRINT(MAX_TRACE
, ("CabinetPrev '%s'.\n", CabinetPrev
));
3239 /* Write name of previous cabinet */
3240 Size
= (ULONG
)strlen(CabinetPrev
) + 1;
3242 if (!WriteFile(FileHandle
, CabinetPrev
, Size
, (LPDWORD
)&BytesWritten
, NULL
))
3244 DPRINT(MIN_TRACE
, ("Cannot write to file.\n"));
3245 return CAB_STATUS_CANNOT_WRITE
;
3248 BytesWritten
= Size
;
3249 if (fwrite(CabinetPrev
, Size
, 1, FileHandle
) < 1)
3251 DPRINT(MIN_TRACE
, ("Cannot write to file.\n"));
3252 return CAB_STATUS_CANNOT_WRITE
;
3256 DPRINT(MAX_TRACE
, ("DiskPrev '%s'.\n", DiskPrev
));
3258 /* Write label of previous disk */
3259 Size
= (ULONG
)strlen(DiskPrev
) + 1;
3261 if (!WriteFile(FileHandle
, DiskPrev
, Size
, (LPDWORD
)&BytesWritten
, NULL
))
3263 DPRINT(MIN_TRACE
, ("Cannot write to file.\n"));
3264 return CAB_STATUS_CANNOT_WRITE
;
3267 BytesWritten
= Size
;
3268 if (fwrite(DiskPrev
, Size
, 1, FileHandle
) < 1)
3270 DPRINT(MIN_TRACE
, ("Cannot write to file.\n"));
3271 return CAB_STATUS_CANNOT_WRITE
;
3276 if ((CABHeader
.Flags
& CAB_FLAG_HASNEXT
) > 0)
3278 DPRINT(MAX_TRACE
, ("CabinetNext '%s'.\n", CabinetNext
));
3280 /* Write name of next cabinet */
3281 Size
= (ULONG
)strlen(CabinetNext
) + 1;
3283 if (!WriteFile(FileHandle
, CabinetNext
, Size
, (LPDWORD
)&BytesWritten
, NULL
))
3285 DPRINT(MIN_TRACE
, ("Cannot write to file.\n"));
3286 return CAB_STATUS_CANNOT_WRITE
;
3289 BytesWritten
= Size
;
3290 if (fwrite(CabinetNext
, Size
, 1, FileHandle
) < 1)
3292 DPRINT(MIN_TRACE
, ("Cannot write to file.\n"));
3293 return CAB_STATUS_CANNOT_WRITE
;
3297 DPRINT(MAX_TRACE
, ("DiskNext '%s'.\n", DiskNext
));
3299 /* Write label of next disk */
3300 Size
= (ULONG
)strlen(DiskNext
) + 1;
3302 if (!WriteFile(FileHandle
, DiskNext
, Size
, (LPDWORD
)&BytesWritten
, NULL
))
3304 DPRINT(MIN_TRACE
, ("Cannot write to file.\n"));
3305 return CAB_STATUS_CANNOT_WRITE
;
3308 BytesWritten
= Size
;
3309 if (fwrite(DiskNext
, Size
, 1, FileHandle
) < 1)
3311 DPRINT(MIN_TRACE
, ("Cannot write to file.\n"));
3312 return CAB_STATUS_CANNOT_WRITE
;
3317 return CAB_STATUS_SUCCESS
;
3321 ULONG
CCabinet::WriteFolderEntries()
3323 * FUNCTION: Writes folder entries
3325 * Status of operation
3328 PCFFOLDER_NODE FolderNode
;
3331 DPRINT(MAX_TRACE
, ("Writing folder table.\n"));
3333 FolderNode
= FolderListHead
;
3334 while (FolderNode
!= NULL
)
3336 if (FolderNode
->Commit
)
3338 DPRINT(MAX_TRACE
, ("Writing folder entry. CompressionType (0x%X) DataBlockCount (%d) DataOffset (0x%X).\n",
3339 FolderNode
->Folder
.CompressionType
, FolderNode
->Folder
.DataBlockCount
, (UINT
)FolderNode
->Folder
.DataOffset
));
3342 if (!WriteFile(FileHandle
,
3343 &FolderNode
->Folder
,
3345 (LPDWORD
)&BytesWritten
,
3348 DPRINT(MIN_TRACE
, ("Cannot write to file.\n"));
3349 return CAB_STATUS_CANNOT_WRITE
;
3352 BytesWritten
= sizeof(CFFOLDER
);
3353 if (fwrite(&FolderNode
->Folder
, sizeof(CFFOLDER
), 1, FileHandle
) < 1)
3355 DPRINT(MIN_TRACE
, ("Cannot write to file.\n"));
3356 return CAB_STATUS_CANNOT_WRITE
;
3360 FolderNode
= FolderNode
->Next
;
3363 return CAB_STATUS_SUCCESS
;
3367 ULONG
CCabinet::WriteFileEntries()
3369 * FUNCTION: Writes file entries for all files
3371 * Status of operation
3376 bool SetCont
= false;
3378 DPRINT(MAX_TRACE
, ("Writing file table.\n"));
3380 File
= FileListHead
;
3381 while (File
!= NULL
)
3385 /* Remove any continued files that ends in this disk */
3386 if (File
->File
.FileControlID
== CAB_FILE_CONTINUED
)
3387 File
->Delete
= true;
3389 /* The file could end in the last (split) block and should therefore
3390 appear in the next disk too */
3392 if ((File
->File
.FileOffset
+ File
->File
.FileSize
>= LastBlockStart
) &&
3393 (File
->File
.FileControlID
<= CAB_FILE_MAX_FOLDER
) && (BlockIsSplit
))
3395 File
->File
.FileControlID
= CAB_FILE_SPLIT
;
3396 File
->Delete
= false;
3400 DPRINT(MAX_TRACE
, ("Writing file entry. FileControlID (0x%X) FileOffset (0x%X) FileSize (%u) FileName (%s).\n",
3401 File
->File
.FileControlID
, (UINT
)File
->File
.FileOffset
, (UINT
)File
->File
.FileSize
, File
->FileName
));
3404 if (!WriteFile(FileHandle
,
3407 (LPDWORD
)&BytesWritten
,
3409 return CAB_STATUS_CANNOT_WRITE
;
3411 BytesWritten
= sizeof(CFFILE
);
3412 if (fwrite(&File
->File
, sizeof(CFFILE
), 1, FileHandle
) < 1)
3414 DPRINT(MIN_TRACE
, ("Cannot write to file.\n"));
3415 return CAB_STATUS_CANNOT_WRITE
;
3420 if (!WriteFile(FileHandle
,
3421 GetFileName(File
->FileName
),
3422 (DWORD
)strlen(GetFileName(File
->FileName
)) + 1,
3423 (LPDWORD
)&BytesWritten
,
3425 return CAB_STATUS_CANNOT_WRITE
;
3427 BytesWritten
= strlen(GetFileName(File
->FileName
)) + 1;
3428 if (fwrite(GetFileName(File
->FileName
), strlen(GetFileName(File
->FileName
)) + 1, 1, FileHandle
) < 1)
3430 DPRINT(MIN_TRACE
, ("Cannot write to file.\n"));
3431 return CAB_STATUS_CANNOT_WRITE
;
3437 File
->File
.FileControlID
= CAB_FILE_CONTINUED
;
3444 return CAB_STATUS_SUCCESS
;
3448 ULONG
CCabinet::CommitDataBlocks(PCFFOLDER_NODE FolderNode
)
3450 * FUNCTION: Writes data blocks to the cabinet
3452 * FolderNode = Pointer to folder node containing the data blocks
3454 * Status of operation
3457 PCFDATA_NODE DataNode
;
3462 DataNode
= FolderNode
->DataListHead
;
3463 if (DataNode
!= NULL
)
3464 Status
= ScratchFile
->Seek(DataNode
->ScratchFilePosition
);
3466 while (DataNode
!= NULL
)
3468 DPRINT(MAX_TRACE
, ("Reading block at (0x%X) CompSize (%u) UncompSize (%u).\n",
3469 (UINT
)DataNode
->ScratchFilePosition
,
3470 DataNode
->Data
.CompSize
,
3471 DataNode
->Data
.UncompSize
));
3473 /* InputBuffer is free for us to use here, so we use it and avoid a
3474 memory allocation. OutputBuffer can't be used here because it may
3475 still contain valid data (if a data block spans two or more disks) */
3476 Status
= ScratchFile
->ReadBlock(&DataNode
->Data
, InputBuffer
, &BytesRead
);
3477 if (Status
!= CAB_STATUS_SUCCESS
)
3479 DPRINT(MIN_TRACE
, ("Cannot read from scratch file (%u).\n", (UINT
)Status
));
3484 if (!WriteFile(FileHandle
, &DataNode
->Data
,
3485 sizeof(CFDATA
), (LPDWORD
)&BytesWritten
, NULL
))
3487 DPRINT(MIN_TRACE
, ("Cannot write to file.\n"));
3488 return CAB_STATUS_CANNOT_WRITE
;
3491 BytesWritten
= sizeof(CFDATA
);
3492 if (fwrite(&DataNode
->Data
, sizeof(CFDATA
), 1, FileHandle
) < 1)
3494 DPRINT(MIN_TRACE
, ("Cannot write to file.\n"));
3495 return CAB_STATUS_CANNOT_WRITE
;
3500 if (!WriteFile(FileHandle
, InputBuffer
,
3501 DataNode
->Data
.CompSize
, (LPDWORD
)&BytesWritten
, NULL
))
3503 DPRINT(MIN_TRACE
, ("Cannot write to file.\n"));
3504 return CAB_STATUS_CANNOT_WRITE
;
3507 BytesWritten
= DataNode
->Data
.CompSize
;
3508 if (fwrite(InputBuffer
, DataNode
->Data
.CompSize
, 1, FileHandle
) < 1)
3510 DPRINT(MIN_TRACE
, ("Cannot write to file.\n"));
3511 return CAB_STATUS_CANNOT_WRITE
;
3515 DataNode
= DataNode
->Next
;
3517 return CAB_STATUS_SUCCESS
;
3521 ULONG
CCabinet::WriteDataBlock()
3523 * FUNCTION: Writes the current data block to the scratch file
3525 * Status of operation
3530 PCFDATA_NODE DataNode
;
3534 Status
= Codec
->Compress(OutputBuffer
,
3539 DPRINT(MAX_TRACE
, ("Block compressed. CurrentIBufferSize (%u) TotalCompSize(%u).\n",
3540 (UINT
)CurrentIBufferSize
, (UINT
)TotalCompSize
));
3542 CurrentOBuffer
= OutputBuffer
;
3543 CurrentOBufferSize
= TotalCompSize
;
3546 DataNode
= NewDataNode(CurrentFolderNode
);
3549 DPRINT(MIN_TRACE
, ("Insufficient memory.\n"));
3550 return CAB_STATUS_NOMEMORY
;
3553 DiskSize
+= sizeof(CFDATA
);
3555 if (MaxDiskSize
> 0)
3556 /* Disk size is limited */
3557 BlockIsSplit
= (DiskSize
+ CurrentOBufferSize
> MaxDiskSize
);
3559 BlockIsSplit
= false;
3563 DataNode
->Data
.CompSize
= (USHORT
)(MaxDiskSize
- DiskSize
);
3564 DataNode
->Data
.UncompSize
= 0;
3565 CreateNewDisk
= true;
3569 DataNode
->Data
.CompSize
= (USHORT
)CurrentOBufferSize
;
3570 DataNode
->Data
.UncompSize
= (USHORT
)CurrentIBufferSize
;
3573 DataNode
->Data
.Checksum
= 0;
3574 DataNode
->ScratchFilePosition
= ScratchFile
->Position();
3576 // FIXME: MAKECAB.EXE does not like this checksum algorithm
3577 //DataNode->Data.Checksum = ComputeChecksum(CurrentOBuffer, DataNode->Data.CompSize, 0);
3579 DPRINT(MAX_TRACE
, ("Writing block. Checksum (0x%X) CompSize (%u) UncompSize (%u).\n",
3580 (UINT
)DataNode
->Data
.Checksum
,
3581 DataNode
->Data
.CompSize
,
3582 DataNode
->Data
.UncompSize
));
3584 Status
= ScratchFile
->WriteBlock(&DataNode
->Data
,
3585 CurrentOBuffer
, &BytesWritten
);
3586 if (Status
!= CAB_STATUS_SUCCESS
)
3589 DiskSize
+= BytesWritten
;
3591 CurrentFolderNode
->TotalFolderSize
+= (BytesWritten
+ sizeof(CFDATA
));
3592 CurrentFolderNode
->Folder
.DataBlockCount
++;
3594 CurrentOBuffer
= (unsigned char*)CurrentOBuffer
+ DataNode
->Data
.CompSize
;
3595 CurrentOBufferSize
-= DataNode
->Data
.CompSize
;
3597 LastBlockStart
+= DataNode
->Data
.UncompSize
;
3601 CurrentIBufferSize
= 0;
3602 CurrentIBuffer
= InputBuffer
;
3605 return CAB_STATUS_SUCCESS
;
3608 #if !defined(_WIN32)
3610 void CCabinet::ConvertDateAndTime(time_t* Time
,
3614 * FUNCTION: Returns file times of a file
3616 * FileHandle = File handle of file to get file times from
3617 * File = Pointer to CFFILE node for file
3619 * Status of operation
3624 timedef
= localtime(Time
);
3626 DPRINT(MAX_TRACE
, ("day: %d, mon: %d, year:%d, hour: %d, min: %d, sec: %d\n",
3627 timedef
->tm_mday
, timedef
->tm_mon
, timedef
->tm_year
,
3628 timedef
->tm_sec
, timedef
->tm_min
, timedef
->tm_hour
));
3630 *DosDate
= ((timedef
->tm_mday
+ 1) << 0)
3631 | ((timedef
->tm_mon
+ 1) << 5)
3632 | (((timedef
->tm_year
+ 1900) - 1980) << 9);
3634 *DosTime
= (timedef
->tm_sec
<< 0)
3635 | (timedef
->tm_min
<< 5)
3636 | (timedef
->tm_hour
<< 11);
3642 ULONG
CCabinet::GetFileTimes(FILEHANDLE FileHandle
, PCFFILE_NODE File
)
3644 * FUNCTION: Returns file times of a file
3646 * FileHandle = File handle of file to get file times from
3647 * File = Pointer to CFFILE node for file
3649 * Status of operation
3655 if (GetFileTime(FileHandle
, NULL
, NULL
, &FileTime
))
3656 FileTimeToDosDateTime(&FileTime
,
3657 &File
->File
.FileDate
,
3658 &File
->File
.FileTime
);
3663 // Check for an absolute path
3664 if (IsSeparator(File
->FileName
[0]))
3665 strcpy(buf
, File
->FileName
);
3668 if (!getcwd(buf
, sizeof(buf
)))
3669 return CAB_STATUS_CANNOT_READ
;
3670 strcat(buf
, DIR_SEPARATOR_STRING
);
3671 strcat(buf
, File
->FileName
);
3674 if (stat(buf
, &stbuf
) == -1)
3675 return CAB_STATUS_CANNOT_READ
;
3677 ConvertDateAndTime(&stbuf
.st_mtime
, &File
->File
.FileDate
, &File
->File
.FileTime
);
3679 return CAB_STATUS_SUCCESS
;
3683 ULONG
CCabinet::GetAttributesOnFile(PCFFILE_NODE File
)
3685 * FUNCTION: Returns attributes on a file
3687 * File = Pointer to CFFILE node for file
3689 * Status of operation
3695 Attributes
= GetFileAttributes(File
->FileName
);
3696 if (Attributes
== -1)
3697 return CAB_STATUS_CANNOT_READ
;
3699 // 0x37 = READONLY | HIDDEN | SYSTEM | DIRECTORY | ARCHIVE
3700 // The IDs for these attributes are the same in the CAB file and under Windows
3701 // If the file has any other attributes, strip them off by the logical AND.
3702 File
->File
.Attributes
= (USHORT
)(Attributes
& 0x37);
3707 // Check for an absolute path
3708 if (IsSeparator(File
->FileName
[0]))
3709 strcpy(buf
, File
->FileName
);
3712 if (!getcwd(buf
, sizeof(buf
)))
3713 return CAB_STATUS_CANNOT_READ
;
3714 strcat(buf
, DIR_SEPARATOR_STRING
);
3715 strcat(buf
, File
->FileName
);
3718 if (stat(buf
, &stbuf
) == -1)
3719 return CAB_STATUS_CANNOT_READ
;
3722 File
->File
.Attributes
|= CAB_ATTRIB_READONLY
;
3723 File
->File
.Attributes
|= CAB_ATTRIB_HIDDEN
;
3724 File
->File
.Attributes
|= CAB_ATTRIB_SYSTEM
;
3727 if (stbuf
.st_mode
& S_IFDIR
)
3728 File
->File
.Attributes
|= CAB_ATTRIB_DIRECTORY
;
3730 File
->File
.Attributes
|= CAB_ATTRIB_ARCHIVE
;
3733 return CAB_STATUS_SUCCESS
;
3737 ULONG
CCabinet::SetAttributesOnFile(char* FileName
, USHORT FileAttributes
)
3739 * FUNCTION: Sets attributes on a file
3741 * FileName = File name with path
3742 * FileAttributes = Attributes of that file
3744 * Status of operation
3748 // 0x37 = READONLY | HIDDEN | SYSTEM | DIRECTORY | ARCHIVE
3749 // The IDs for these attributes are the same in the CAB file and under Windows
3750 // If the file has any other attributes, strip them off by the logical AND.
3751 SetFileAttributes(FileName
, (DWORD
)(FileAttributes
& 0x37));
3753 return CAB_STATUS_SUCCESS
;
3755 //DPRINT(MIN_TRACE, ("FIXME: SetAttributesOnFile() is unimplemented\n"));
3756 return CAB_STATUS_SUCCESS
;
3760 #endif /* CAB_READ_ONLY */