From 9c232dfd4f353d50de2a87ac0b97c4de04bcee3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herm=C3=A8s=20B=C3=A9lusca-Ma=C3=AFto?= Date: Fri, 8 Jan 2016 20:25:30 +0000 Subject: [PATCH] [CDMAKE] - #if _WIN32 --> #ifdef _WIN32 - Define the "max" macro only if needed. - Fix a comment. - Plug a memory leak ('case_name' not freed in the "!*case_name" case of the dir_hash_create_dir function). - Return a non-zero value (the file structure pointer) from dir_hash_add_file in case we succeed in creating the file in the ISO structure, and NULL otherwise (i.e. when the filename is empty). In that latter case we fail the hard way. No-named files are completely invalid in ISO archives. Now, the following testcase (e.g. to be added into bootcd.lst): reactos/reactos.exe/=C:/test/reactos.exe fails as expected (before, we were able to create a directory named "reactos/reactos.exe", and inside, a no-named file with the contents of C:/test/reactos.exe). svn path=/trunk/; revision=70553 --- reactos/tools/cdmake/cdmake.c | 36 ++++++++++++++++++---------------- reactos/tools/cdmake/dirhash.c | 21 +++++++++++++++++--- reactos/tools/cdmake/dirhash.h | 7 ++++++- 3 files changed, 43 insertions(+), 21 deletions(-) diff --git a/reactos/tools/cdmake/cdmake.c b/reactos/tools/cdmake/cdmake.c index d94dabeb549..73910afd7f1 100644 --- a/reactos/tools/cdmake/cdmake.c +++ b/reactos/tools/cdmake/cdmake.c @@ -619,7 +619,7 @@ This function checks to see if there's a cdname conflict. #if defined(_WIN32) && !defined(strcasecmp) #define strcasecmp stricmp -#endif//_WIN32 +#endif // _WIN32 static BOOL cdname_exists(PDIR_RECORD d) { @@ -696,7 +696,7 @@ static void parse_filename_into_dirrecord(const char* filename, PDIR_RECORD d, B filename_counter = 1; while (cdname_exists(d)) { - // the file name must be least 8 char long + // the file name must be at least 8 chars long if (strlen(d->name_on_cd)<8) error_exit("'%s' is a duplicate file name, aborting...", filename); @@ -704,7 +704,9 @@ static void parse_filename_into_dirrecord(const char* filename, PDIR_RECORD d, B error_exit("'%s' is a duplicate file name, aborting...", filename); // max 255 times for equal short filename - if (filename_counter>255) error_exit("'%s' is a duplicate file name, aborting...", filename); + if (filename_counter>255) + error_exit("'%s' is a duplicate file name, aborting...", filename); + d->name_on_cd[8] = '~'; memset(&d->name_on_cd[9],0,5); sprintf(&d->name_on_cd[9],"%d",filename_counter); @@ -729,7 +731,7 @@ specified ffblk. It links it into the beginning of the directory list for the specified parent and returns a pointer to the new record. -----------------------------------------------------------------------------*/ -#if _WIN32 +#ifdef _WIN32 /* Win32 version */ PDIR_RECORD @@ -1167,7 +1169,7 @@ new_empty_dirrecord(PDIR_RECORD d, BOOL directory) return new_d; } -#if _WIN32 +#ifdef _WIN32 static BOOL get_cd_file_time(HANDLE handle, PDATE_AND_TIME cd_time_info) { @@ -1195,7 +1197,7 @@ static void scan_specified_files(PDIR_RECORD d, struct target_dir_entry *dir) { PDIR_RECORD new_d; -#if _WIN32 +#ifdef _WIN32 HANDLE open_file; LARGE_INTEGER file_size; #else @@ -1210,7 +1212,7 @@ scan_specified_files(PDIR_RECORD d, struct target_dir_entry *dir) { if (strcmp(file->target_name, DIRECTORY_TIMESTAMP) == 0) { -#if _WIN32 +#ifdef _WIN32 if ((open_file = CreateFileA(file->source_name, GENERIC_READ, FILE_SHARE_READ, @@ -1246,7 +1248,7 @@ scan_specified_files(PDIR_RECORD d, struct target_dir_entry *dir) } new_d = new_empty_dirrecord(d, FALSE); parse_filename_into_dirrecord(file->target_name, new_d, FALSE); -#if _WIN32 +#ifdef _WIN32 if ((open_file = CreateFileA(file->source_name, GENERIC_READ, FILE_SHARE_READ, @@ -1271,7 +1273,7 @@ scan_specified_files(PDIR_RECORD d, struct target_dir_entry *dir) #else if (stat(file->source_name, &stbuf) == -1) { - error_exit("Cannot find '%s' (target %s)\n", + error_exit("Cannot find '%s' (target '%s')\n", file->source_name, file->target_name); } @@ -2241,7 +2243,7 @@ int main(int argc, char **argv) } else { - char *trimmedline, *targetname, *srcname, *eq, *normdir; + char *trimmedline, *targetname, *normdir, *srcname, *eq; char lineread[1024]; FILE *f = fopen(source+1, "r"); @@ -2268,17 +2270,17 @@ int main(int argc, char **argv) targetname = strtok(lineread, "="); srcname = strtok(NULL, ""); -#if _WIN32 +#ifdef _WIN32 if (_access(srcname, R_OK) == 0) - dir_hash_add_file(&specified_files, srcname, targetname); - else - error_exit("Cannot access file '%s' (target %s)\n", srcname, targetname); #else if (access(srcname, R_OK) == 0) - dir_hash_add_file(&specified_files, srcname, targetname); - else - error_exit("Cannot access file '%s' (target %s)\n", srcname, targetname); #endif + { + if (!dir_hash_add_file(&specified_files, srcname, targetname)) + error_exit("Target '%s' (file '%s') is invalid\n", targetname, srcname); + } + else + error_exit("Cannot access file '%s' (target '%s')\n", srcname, targetname); } } fclose(f); diff --git a/reactos/tools/cdmake/dirhash.c b/reactos/tools/cdmake/dirhash.c index 8a083f26f11..a44bad57e01 100644 --- a/reactos/tools/cdmake/dirhash.c +++ b/reactos/tools/cdmake/dirhash.c @@ -12,7 +12,9 @@ #include "config.h" #include "dirhash.h" +#ifndef max #define max(a, b) ((a) > (b) ? (a) : (b)) +#endif /* This is the famous DJB hash */ static unsigned int @@ -148,7 +150,11 @@ dir_hash_create_dir(struct target_dir_hash *dh, const char *casename, const char free(parentcase); /* See the remark above */ - if (!*case_name) return parent_de; + if (!*case_name) + { + free(case_name); + return parent_de; + } /* Now create the directory */ de = calloc(1, sizeof(*de)); @@ -168,7 +174,8 @@ dir_hash_create_dir(struct target_dir_hash *dh, const char *casename, const char return de; } -void dir_hash_add_file(struct target_dir_hash *dh, const char *source, const char *target) +struct target_file * +dir_hash_add_file(struct target_dir_hash *dh, const char *source, const char *target) { struct target_file *tf; struct target_dir_entry *de; @@ -176,8 +183,14 @@ void dir_hash_add_file(struct target_dir_hash *dh, const char *source, const cha char *targetfile = NULL; char *targetnorm; - /* Create first the directory */ + /* First create the directory; check whether the file name is valid and bail out if not */ split_path(target, &targetdir, &targetfile); + if (!*targetfile) + { + free(targetdir); + free(targetfile); + return NULL; + } targetnorm = strdup(targetdir); normalize_dirname(targetnorm); de = dir_hash_create_dir(dh, targetdir, targetnorm); @@ -190,6 +203,8 @@ void dir_hash_add_file(struct target_dir_hash *dh, const char *source, const cha de->head = tf; tf->source_name = strdup(source); tf->target_name = targetfile; + + return tf; } static void diff --git a/reactos/tools/cdmake/dirhash.h b/reactos/tools/cdmake/dirhash.h index 1825532e2c8..d06427f0401 100644 --- a/reactos/tools/cdmake/dirhash.h +++ b/reactos/tools/cdmake/dirhash.h @@ -5,6 +5,7 @@ * PURPOSE: CD-ROM Premastering Utility - Directory names hashing * PROGRAMMERS: Art Yerkes */ + #ifndef _DIRHASH_H_ #define _DIRHASH_H_ @@ -37,9 +38,13 @@ struct target_dir_hash }; void normalize_dirname(char *filename); -void dir_hash_add_file(struct target_dir_hash *dh, const char *source, const char *target); + struct target_dir_entry * dir_hash_create_dir(struct target_dir_hash *dh, const char *casename, const char *targetnorm); + +struct target_file * +dir_hash_add_file(struct target_dir_hash *dh, const char *source, const char *target); + void dir_hash_destroy(struct target_dir_hash *dh); #endif // _DIRHASH_H_