From a10cd5069eec9250b36aaa3512829c91f38c00c0 Mon Sep 17 00:00:00 2001 From: Vladimir Olteanu Date: Wed, 22 Nov 2023 03:25:48 +0200 Subject: [PATCH 01/11] Switch to C++ --- CMakeLists.txt | 2 +- extract-xiso.c => extract-xiso.cpp | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) rename extract-xiso.c => extract-xiso.cpp (99%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7aec9fa..60d67bf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,7 +4,7 @@ project(extract-xiso) string(TOUPPER __${CMAKE_SYSTEM_NAME}__ TARGET_OS) set(SOURCE_FILES - extract-xiso.c + extract-xiso.cpp ) add_executable(extract-xiso ${SOURCE_FILES}) diff --git a/extract-xiso.c b/extract-xiso.cpp similarity index 99% rename from extract-xiso.c rename to extract-xiso.cpp index 8279769..068c0ef 100644 --- a/extract-xiso.c +++ b/extract-xiso.cpp @@ -647,7 +647,7 @@ int main( int argc, char **argv ) { struct stat sb; create_list *create = nil, *p, *q, **r; int i, fd, opt_char, err = 0, isos = 0; - bool extract = true, rewrite = false, free_user = false, free_pass = false, x_seen = false, delete = false, optimized; + bool extract = true, rewrite = false, free_user = false, free_pass = false, x_seen = false, del = false, optimized; char *cwd = nil, *path = nil, *buf = nil, *new_iso_path = nil, tag[ XISO_OPTIMIZED_TAG_LENGTH * sizeof(long) ]; if ( argc < 2 ) { usage(); exit( 1 ); } @@ -678,7 +678,7 @@ int main( int argc, char **argv ) { } break; case 'D': { - delete = true; + del = true; } break; case 'h': { @@ -817,7 +817,7 @@ int main( int argc, char **argv ) { if ( err ) { err = 0; free( buf ); continue; } } if ( ! err ) err = decode_xiso( buf, path, k_rewrite, &new_iso_path, true ); - if ( ! err && delete && unlink( buf ) == -1 ) log_err( __FILE__, __LINE__, "unable to delete %s\n", buf ); + if ( ! err && del && unlink( buf ) == -1 ) log_err( __FILE__, __LINE__, "unable to delete %s\n", buf ); if ( buf ) free( buf ); } else { From 2be34981b25efd7fca17fc66536dcaf99029df7c Mon Sep 17 00:00:00 2001 From: Vladimir Olteanu Date: Wed, 22 Nov 2023 03:37:03 +0200 Subject: [PATCH 02/11] Fix C++ warning regarding _GNU_SOURCE --- extract-xiso.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/extract-xiso.cpp b/extract-xiso.cpp index 068c0ef..a60eab0 100644 --- a/extract-xiso.cpp +++ b/extract-xiso.cpp @@ -237,9 +237,6 @@ #if defined( __LINUX__ ) #define _LARGEFILE64_SOURCE #endif -#if defined( __GNUC__ ) - #define _GNU_SOURCE -#endif #include From e2851a16e7087ce17bbfa7760eaeb2f3bd3bb492 Mon Sep 17 00:00:00 2001 From: Vladimir Olteanu Date: Wed, 22 Nov 2023 03:41:50 +0200 Subject: [PATCH 03/11] Made s_systemupdate const --- extract-xiso.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extract-xiso.cpp b/extract-xiso.cpp index a60eab0..2cf6067 100644 --- a/extract-xiso.cpp +++ b/extract-xiso.cpp @@ -630,7 +630,7 @@ static int s_total_files_all_isos = 0; static bool s_warned = 0; static bool s_remove_systemupdate = false; -static char *s_systemupdate = "$SystemUpdate"; +static const char *s_systemupdate = "$SystemUpdate"; static xoff_t s_xbox_disc_lseek = 0; From ab29f0642a1b3212bb631841b341c9377dee132e Mon Sep 17 00:00:00 2001 From: Vladimir Olteanu Date: Wed, 22 Nov 2023 04:08:00 +0200 Subject: [PATCH 04/11] Made s_pattern const --- extract-xiso.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/extract-xiso.cpp b/extract-xiso.cpp index 2cf6067..cee1161 100644 --- a/extract-xiso.cpp +++ b/extract-xiso.cpp @@ -588,7 +588,7 @@ int avl_traverse_depth_first( dir_node_avl *in_root, traversal_callback in_callb void boyer_moore_done(); char *boyer_moore_search( char *in_text, long in_text_len ); -int boyer_moore_init( char *in_pattern, long in_pat_len, long in_alphabet_size ); +int boyer_moore_init(const char *in_pattern, long in_pat_len, long in_alphabet_size ); int free_dir_node_avl( void *in_dir_node_avl, void *, long ); int extract_file( int in_xiso, dir_node *in_file, modes in_mode, char *path ); @@ -617,7 +617,7 @@ void write_sector( int in_xiso, xoff_t in_start, char *in_name, char *in_extensi static long s_pat_len; static bool s_quiet = false; -static char *s_pattern = nil; +static const char *s_pattern = nil; static long *s_gs_table = nil; static long *s_bc_table = nil; static xoff_t s_total_bytes = 0; @@ -1562,7 +1562,7 @@ int avl_traverse_depth_first( dir_node_avl *in_root, traversal_callback in_callb #endif -int boyer_moore_init( char *in_pattern, long in_pat_len, long in_alphabet_size ) { +int boyer_moore_init( const char *in_pattern, long in_pat_len, long in_alphabet_size ) { long i, j, k, *backup, err = 0; s_pattern = in_pattern; From 367fe982511a60be8b308a545ebe5c0cb05d452c Mon Sep 17 00:00:00 2001 From: Vladimir Olteanu Date: Wed, 22 Nov 2023 04:22:02 +0200 Subject: [PATCH 05/11] Replace nil with nullptr --- extract-xiso.cpp | 151 +++++++++++++++++++++++------------------------ 1 file changed, 73 insertions(+), 78 deletions(-) diff --git a/extract-xiso.cpp b/extract-xiso.cpp index cee1161..b2d0824 100644 --- a/extract-xiso.cpp +++ b/extract-xiso.cpp @@ -372,11 +372,6 @@ #endif -#ifndef nil - #define nil 0 -#endif - - #define exiso_version "2.7.1 (01.11.14)" #define VERSION_LENGTH 16 @@ -617,12 +612,12 @@ void write_sector( int in_xiso, xoff_t in_start, char *in_name, char *in_extensi static long s_pat_len; static bool s_quiet = false; -static const char *s_pattern = nil; -static long *s_gs_table = nil; -static long *s_bc_table = nil; +static const char *s_pattern = nullptr; +static long *s_gs_table = nullptr; +static long *s_bc_table = nullptr; static xoff_t s_total_bytes = 0; static int s_total_files = 0; -static char *s_copy_buffer = nil; +static char *s_copy_buffer = nullptr; static bool s_real_quiet = false; static bool s_media_enable = true; static xoff_t s_total_bytes_all_isos = 0; @@ -642,10 +637,10 @@ static xoff_t s_xbox_disc_lseek = 0; int main( int argc, char **argv ) { struct stat sb; - create_list *create = nil, *p, *q, **r; + create_list *create = nullptr, *p, *q, **r; int i, fd, opt_char, err = 0, isos = 0; bool extract = true, rewrite = false, free_user = false, free_pass = false, x_seen = false, del = false, optimized; - char *cwd = nil, *path = nil, *buf = nil, *new_iso_path = nil, tag[ XISO_OPTIMIZED_TAG_LENGTH * sizeof(long) ]; + char *cwd = nullptr, *path = nullptr, *buf = nullptr, *new_iso_path = nullptr, tag[ XISO_OPTIMIZED_TAG_LENGTH * sizeof(long) ]; if ( argc < 2 ) { usage(); exit( 1 ); } @@ -657,21 +652,21 @@ int main( int argc, char **argv ) { exit( 1 ); } - for ( r = &create; *r != nil; r = &(*r)->next ) ; + for ( r = &create; *r != nullptr; r = &(*r)->next ) ; - if ( ( *r = (create_list *) malloc( sizeof(create_list) ) ) == nil ) mem_err(); + if ( ( *r = (create_list *) malloc( sizeof(create_list) ) ) == nullptr ) mem_err(); if ( ! err ) { - (*r)->name = nil; - (*r)->next = nil; + (*r)->name = nullptr; + (*r)->next = nullptr; - if ( ( (*r)->path = strdup( optarg ) ) == nil ) mem_err(); + if ( ( (*r)->path = strdup( optarg ) ) == nullptr ) mem_err(); } - if ( ! err && argv[ optind ] && *argv[ optind ] != '-' && *argv[ optind ] && ( (*r)->name = strdup( argv[ optind++ ] ) ) == nil ) mem_err(); + if ( ! err && argv[ optind ] && *argv[ optind ] != '-' && *argv[ optind ] && ( (*r)->name = strdup( argv[ optind++ ] ) ) == nullptr ) mem_err(); } break; case 'd': { if ( path ) free( path ); - if ( ( path = strdup( optarg ) ) == nil ) mem_err(); + if ( ( path = strdup( optarg ) ) == nullptr ) mem_err(); } break; case 'D': { @@ -746,20 +741,20 @@ int main( int argc, char **argv ) { exiso_log( "%s", banner ); - if ( ( extract ) && ( s_copy_buffer = (char *) malloc( READWRITE_BUFFER_SIZE ) ) == nil ) mem_err(); + if ( ( extract ) && ( s_copy_buffer = (char *) malloc( READWRITE_BUFFER_SIZE ) ) == nullptr ) mem_err(); } if ( ! err && ( create || rewrite ) ) err = boyer_moore_init( XISO_MEDIA_ENABLE, XISO_MEDIA_ENABLE_LENGTH, k_default_alphabet_size ); if ( ! err && create ) { - for ( p = create; ! err && p != nil; ) { - char *tmp = nil; + for ( p = create; ! err && p != nullptr; ) { + char *tmp = nullptr; if ( p->name ) { for ( i = (int) strlen( p->name ); i >= 0 && p->name[ i ] != PATH_CHAR; --i ) ; ++i; if ( i ) { - if ( ( tmp = (char *) malloc( i + 1 ) ) == nil ) mem_err(); + if ( ( tmp = (char *) malloc( i + 1 ) ) == nullptr ) mem_err(); if ( ! err ) { strncpy( tmp, p->name, i ); tmp[ i ] = 0; @@ -767,7 +762,7 @@ int main( int argc, char **argv ) { } } - if ( ! err ) err = create_xiso( p->path, tmp, nil, -1, nil, p->name ? p->name + i : nil, nil ); + if ( ! err ) err = create_xiso( p->path, tmp, nullptr, -1, nullptr, p->name ? p->name + i : nullptr, nullptr ); if ( tmp ) free( tmp ); @@ -805,7 +800,7 @@ int main( int argc, char **argv ) { continue; } - if ( ! err && ( buf = (char *) malloc( strlen( argv[ i ] ) + 5 ) ) == nil ) mem_err(); // + 5 magic number is for ".old\0" + if ( ! err && ( buf = (char *) malloc( strlen( argv[ i ] ) + 5 ) ) == nullptr ) mem_err(); // + 5 magic number is for ".old\0" if ( ! err ) { sprintf( buf, "%s.old", argv[ i ] ); if ( stat( buf, &sb ) != -1 ) misc_err( "%s already exists, cannot rewrite %s\n", buf, argv[ i ], 0 ); @@ -819,7 +814,7 @@ int main( int argc, char **argv ) { if ( buf ) free( buf ); } else { // the order of the mutually exclusive options here is important, the extract ? k_extract : k_list test *must* be the final comparison - if ( ! err ) err = decode_xiso( argv[ i ], path, extract ? k_extract : k_list, nil, ! optimized ); + if ( ! err ) err = decode_xiso( argv[ i ], path, extract ? k_extract : k_list, nullptr, ! optimized ); } } } @@ -830,7 +825,7 @@ int main( int argc, char **argv ) { if ( ! err ) exiso_log( "\n%s successfully rewritten%s%s\n", argv[ i ], path ? " as " : ".", path ? new_iso_path : "" ); free( new_iso_path ); - new_iso_path = nil; + new_iso_path = nullptr; } if ( err == err_iso_no_files ) err = 0; @@ -937,17 +932,17 @@ int verify_xiso( int in_xiso, int32_t *out_root_dir_sector, int32_t *out_root_di int create_xiso( char *in_root_directory, char *in_output_directory, dir_node_avl *in_root, int in_xiso, char **out_iso_path, char *in_name, progress_callback in_progress_callback ) { xoff_t pos; dir_node_avl root; - FILE_TIME *ft = nil; + FILE_TIME *ft = nullptr; write_tree_context wt_context; uint32_t start_sector; int i, n, xiso = -1, err = 0; - char *cwd = nil, *buf = nil, *iso_name, *xiso_path, *iso_dir; + char *cwd = nullptr, *buf = nullptr, *iso_name, *xiso_path, *iso_dir; s_total_bytes = s_total_files = 0; memset( &root, 0, sizeof(dir_node_avl) ); - if ( ( cwd = getcwd( nil, 0 ) ) == nil ) mem_err(); + if ( ( cwd = getcwd( nullptr, 0 ) ) == nullptr ) mem_err(); if ( ! err ) { if ( ! in_root ) { if ( chdir( in_root_directory ) == -1 ) chdir_err( in_root_directory ); @@ -982,7 +977,7 @@ int create_xiso( char *in_root_directory, char *in_output_directory, dir_node_av if ( in_root ) { root.subdirectory = in_root; - avl_traverse_depth_first( in_root, (traversal_callback) calculate_total_files_and_bytes, nil, k_prefix, 0 ); + avl_traverse_depth_first( in_root, (traversal_callback) calculate_total_files_and_bytes, nullptr, k_prefix, 0 ); } else { int i, n = 0; @@ -1005,10 +1000,10 @@ int create_xiso( char *in_root_directory, char *in_output_directory, dir_node_av start_sector = root.start_sector; - avl_traverse_depth_first( &root, (traversal_callback) calculate_directory_requirements, nil, k_prefix, 0 ); + avl_traverse_depth_first( &root, (traversal_callback) calculate_directory_requirements, nullptr, k_prefix, 0 ); avl_traverse_depth_first( &root, (traversal_callback) calculate_directory_offsets, &start_sector, k_prefix, 0 ); } - if ( ! err && ( buf = (char *) malloc( n = max( READWRITE_BUFFER_SIZE, XISO_HEADER_OFFSET ) ) ) == nil ) mem_err(); + if ( ! err && ( buf = (char *) malloc( n = max( READWRITE_BUFFER_SIZE, XISO_HEADER_OFFSET ) ) ) == nullptr ) mem_err(); if ( ! err ) { if ( ( xiso = open( xiso_path, WRITEFLAGS, 0644 ) ) == -1 ) open_err( xiso_path ); if ( out_iso_path ) *out_iso_path = xiso_path; @@ -1037,7 +1032,7 @@ int create_xiso( char *in_root_directory, char *in_output_directory, dir_node_av memset( buf, 0, XISO_FILETIME_SIZE ); } else { - if ( ( ft = alloc_filetime_now() ) == nil ) mem_err(); + if ( ( ft = alloc_filetime_now() ) == nullptr ) mem_err(); if ( ! err && write( xiso, ft, XISO_FILETIME_SIZE ) != XISO_FILETIME_SIZE ) write_err(); } } @@ -1047,11 +1042,11 @@ int create_xiso( char *in_root_directory, char *in_output_directory, dir_node_av if ( ! err && ! in_root ) { if ( chdir( ".." ) == -1 ) chdir_err( ".." ); } - if ( ! err && ( root.filename = strdup( iso_dir ) ) == nil ) mem_err(); + if ( ! err && ( root.filename = strdup( iso_dir ) ) == nullptr ) mem_err(); if ( ! err && lseek( xiso, (xoff_t) root.start_sector * XISO_SECTOR_SIZE, SEEK_SET ) == -1 ) seek_err(); if ( ! err ) { - wt_context.path = nil; + wt_context.path = nullptr; wt_context.xiso = xiso; wt_context.from = in_root ? in_xiso : -1; wt_context.progress = in_progress_callback; @@ -1072,7 +1067,7 @@ int create_xiso( char *in_root_directory, char *in_output_directory, dir_node_av else exiso_log( "\nsucessfully created %s%s (%u files totalling %lld bytes added)\n", iso_name ? iso_name : "xiso", iso_name && ! in_name ? ".iso" : "", s_total_files, (long long int) s_total_bytes ); } - if ( root.subdirectory != EMPTY_SUBDIRECTORY ) avl_traverse_depth_first( root.subdirectory, free_dir_node_avl, nil, k_postfix, 0 ); + if ( root.subdirectory != EMPTY_SUBDIRECTORY ) avl_traverse_depth_first( root.subdirectory, free_dir_node_avl, nullptr, k_postfix, 0 ); if ( xiso != -1 ) { close( xiso ); @@ -1093,11 +1088,11 @@ int create_xiso( char *in_root_directory, char *in_output_directory, dir_node_av int decode_xiso( char *in_xiso, char *in_path, modes in_mode, char **out_iso_path, bool in_ll_compat ) { - dir_node_avl *root = nil; + dir_node_avl *root = nullptr; bool repair = false; int32_t root_dir_sect, root_dir_size; int xiso, err = 0, len, path_len = 0, add_slash = 0; - char *buf, *cwd = nil, *name = nil, *short_name = nil, *iso_name, *folder = nil; + char *buf, *cwd = nullptr, *name = nullptr, *short_name = nullptr, *iso_name, *folder = nullptr; if ( ( xiso = open( in_xiso, READFLAGS, 0 ) ) == -1 ) open_err( in_xiso ); @@ -1116,7 +1111,7 @@ int decode_xiso( char *in_xiso, char *in_path, modes in_mode, char **out_iso_pat // create a directory of the same name as the file we are working on, minus the ".iso" portion if (len > 4 && strcasecmp(&name[len - 4], ".iso") == 0) { name[ len -= 4 ] = 0; - if ( ( short_name = strdup( name ) ) == nil ) mem_err(); + if ( ( short_name = strdup( name ) ) == nullptr ) mem_err(); name[ len ] = '.'; } } @@ -1124,7 +1119,7 @@ int decode_xiso( char *in_xiso, char *in_path, modes in_mode, char **out_iso_pat if ( ! err && ! len ) misc_err( "invalid xiso image name: %s\n", in_xiso, 0, 0 ); if ( ! err && in_mode == k_extract && in_path ) { - if ( ( cwd = getcwd( nil, 0 ) ) == nil ) mem_err(); + if ( ( cwd = getcwd( nullptr, 0 ) ) == nullptr ) mem_err(); if ( ! err && mkdir( in_path, 0755 ) ); if ( ! err && chdir( in_path ) == -1 ) chdir_err( in_path ); } @@ -1150,7 +1145,7 @@ int decode_xiso( char *in_xiso, char *in_path, modes in_mode, char **out_iso_pat if ( in_path[ path_len - 1 ] != PATH_CHAR ) ++add_slash; } - if ( ( buf = (char *) malloc( path_len + add_slash + strlen( iso_name ) + 2 ) ) == nil ) mem_err(); + if ( ( buf = (char *) malloc( path_len + add_slash + strlen( iso_name ) + 2 ) ) == nullptr ) mem_err(); if ( ! err ) { sprintf( buf, "%s%s%s%c", in_path ? in_path : "", add_slash && ( ! in_path ) ? PATH_CHAR_STR : "", in_mode != k_list && ( ! in_path ) ? iso_name : "", PATH_CHAR ); @@ -1158,12 +1153,12 @@ int decode_xiso( char *in_xiso, char *in_path, modes in_mode, char **out_iso_pat if ( in_mode == k_rewrite ) { if ( ! err && lseek( xiso, (xoff_t) root_dir_sect * XISO_SECTOR_SIZE + s_xbox_disc_lseek, SEEK_SET ) == -1 ) seek_err(); - if ( ! err ) err = traverse_xiso( xiso, nil, (xoff_t) root_dir_sect * XISO_SECTOR_SIZE + s_xbox_disc_lseek, buf, k_generate_avl, &root, in_ll_compat ); - if ( ! err ) err = create_xiso( iso_name, in_path, root, xiso, out_iso_path, nil, nil ); + if ( ! err ) err = traverse_xiso( xiso, nullptr, (xoff_t) root_dir_sect * XISO_SECTOR_SIZE + s_xbox_disc_lseek, buf, k_generate_avl, &root, in_ll_compat ); + if ( ! err ) err = create_xiso( iso_name, in_path, root, xiso, out_iso_path, nullptr, nullptr ); } else { if ( ! err && lseek( xiso, (xoff_t) root_dir_sect * XISO_SECTOR_SIZE + s_xbox_disc_lseek, SEEK_SET ) == -1) seek_err(); - if ( ! err ) err = traverse_xiso( xiso, nil, (xoff_t) root_dir_sect * XISO_SECTOR_SIZE + s_xbox_disc_lseek, buf, in_mode, nil, in_ll_compat ); + if ( ! err ) err = traverse_xiso( xiso, nullptr, (xoff_t) root_dir_sect * XISO_SECTOR_SIZE + s_xbox_disc_lseek, buf, in_mode, nullptr, in_ll_compat ); } free( buf ); @@ -1196,7 +1191,7 @@ int traverse_xiso( int in_xiso, dir_node *in_dir_node, xoff_t in_dir_start, char int err = 0, sector; uint16_t l_offset = 0, tmp; - if ( in_dir_node == nil ) in_dir_node = &node; + if ( in_dir_node == nullptr ) in_dir_node = &node; memset( dir = in_dir_node, 0, sizeof(dir_node) ); @@ -1234,7 +1229,7 @@ int traverse_xiso( int in_xiso, dir_node *in_dir_node, xoff_t in_dir_start, char little32( dir->file_size ); little32( dir->start_sector ); - if ( ( dir->filename = (char *) malloc( dir->filename_length + 1 ) ) == nil ) mem_err(); + if ( ( dir->filename = (char *) malloc( dir->filename_length + 1 ) ) == nullptr ) mem_err(); } if ( ! err ) { @@ -1251,11 +1246,11 @@ int traverse_xiso( int in_xiso, dir_node *in_dir_node, xoff_t in_dir_start, char } if ( ! err && in_mode == k_generate_avl ) { - if ( ( avl = (dir_node_avl *) malloc( sizeof(dir_node_avl) ) ) == nil ) mem_err(); + if ( ( avl = (dir_node_avl *) malloc( sizeof(dir_node_avl) ) ) == nullptr ) mem_err(); if ( ! err ) { memset( avl, 0, sizeof(dir_node_avl) ); - if ( ( avl->filename = strdup( dir->filename ) ) == nil ) mem_err(); + if ( ( avl->filename = strdup( dir->filename ) ) == nullptr ) mem_err(); } if ( ! err ) { dir->avl_node = avl; @@ -1270,7 +1265,7 @@ int traverse_xiso( int in_xiso, dir_node *in_dir_node, xoff_t in_dir_start, char if ( ! err && l_offset ) { in_ll_compat = false; - if ( ( dir->left = (dir_node *) malloc( sizeof(dir_node) ) ) == nil ) mem_err(); + if ( ( dir->left = (dir_node *) malloc( sizeof(dir_node) ) ) == nullptr ) mem_err(); if ( ! err ) { memset( dir->left, 0, sizeof(dir_node) ); if ( lseek( in_xiso, in_dir_start + (xoff_t) l_offset * XISO_DWORD_SIZE, SEEK_SET ) == -1 ) seek_err(); @@ -1285,20 +1280,20 @@ int traverse_xiso( int in_xiso, dir_node *in_dir_node, xoff_t in_dir_start, char left_processed: - if ( dir->left ) { free( dir->left ); dir->left = nil; } + if ( dir->left ) { free( dir->left ); dir->left = nullptr; } if ( ! err && ( curpos = lseek( in_xiso, 0, SEEK_CUR ) ) == -1 ) seek_err(); if ( ! err ) { if ( dir->attributes & XISO_ATTRIBUTE_DIR ) { if ( in_path ) { - if ( ( path = (char *) malloc( strlen( in_path ) + dir->filename_length + 2 ) ) == nil ) mem_err(); + if ( ( path = (char *) malloc( strlen( in_path ) + dir->filename_length + 2 ) ) == nullptr ) mem_err(); if ( ! err ) { sprintf( path, "%s%s%c", in_path, dir->filename, PATH_CHAR ); if ( lseek( in_xiso, (xoff_t) dir->start_sector * XISO_SECTOR_SIZE + s_xbox_disc_lseek, SEEK_SET ) == -1 ) seek_err(); } - } else path = nil; + } else path = nullptr; if ( ! err ) { if ( !s_remove_systemupdate || !strstr( dir->filename, s_systemupdate ) ) @@ -1317,8 +1312,8 @@ int traverse_xiso( int in_xiso, dir_node *in_dir_node, xoff_t in_dir_start, char if ( ! err ) { memcpy( &subdir, dir, sizeof(dir_node) ); - subdir.parent = nil; - if ( ! err && dir->file_size > 0 ) err = traverse_xiso( in_xiso, &subdir, (xoff_t) dir->start_sector * XISO_SECTOR_SIZE + s_xbox_disc_lseek, path, in_mode, in_mode == k_generate_avl ? &dir->avl_node->subdirectory : nil, in_ll_compat ); + subdir.parent = nullptr; + if ( ! err && dir->file_size > 0 ) err = traverse_xiso( in_xiso, &subdir, (xoff_t) dir->start_sector * XISO_SECTOR_SIZE + s_xbox_disc_lseek, path, in_mode, in_mode == k_generate_avl ? &dir->avl_node->subdirectory : nullptr, in_ll_compat ); if ( !s_remove_systemupdate || !strstr( dir->filename, s_systemupdate ) ) { @@ -1355,7 +1350,7 @@ int traverse_xiso( int in_xiso, dir_node *in_dir_node, xoff_t in_dir_start, char if ( ! err && lseek( in_xiso, in_dir_start + (xoff_t) dir->r_offset * XISO_DWORD_SIZE, SEEK_SET ) == -1 ) seek_err(); if ( ! err ) { - if ( dir->filename ) { free( dir->filename ); dir->filename = nil; } + if ( dir->filename ) { free( dir->filename ); dir->filename = nullptr; } l_offset = dir->r_offset; @@ -1382,7 +1377,7 @@ dir_node_avl *avl_fetch( dir_node_avl *in_root, char *in_filename ) { int result; for ( ;; ) { - if ( in_root == nil ) return nil; + if ( in_root == nullptr ) return nullptr; result = avl_compare_key( in_filename, in_root->filename ); @@ -1397,7 +1392,7 @@ avl_result avl_insert( dir_node_avl **in_root, dir_node_avl *in_node ) { avl_result tmp; int result; - if ( *in_root == nil ) { *in_root = in_node; return k_avl_balanced; } + if ( *in_root == nullptr ) { *in_root = in_node; return k_avl_balanced; } result = avl_compare_key( in_node->filename, (*in_root)->filename ); @@ -1529,7 +1524,7 @@ int avl_compare_key( char *in_lhs, char *in_rhs ) { int avl_traverse_depth_first( dir_node_avl *in_root, traversal_callback in_callback, void *in_context, avl_traversal_method in_method, long in_depth ) { int err; - if ( in_root == nil ) return 0; + if ( in_root == nullptr ) return 0; switch ( in_method ) { case k_prefix: { @@ -1568,13 +1563,13 @@ int boyer_moore_init( const char *in_pattern, long in_pat_len, long in_alphabet_ s_pattern = in_pattern; s_pat_len = in_pat_len; - if ( ( s_bc_table = (long *) malloc( in_alphabet_size * sizeof(long) ) ) == nil ) mem_err(); + if ( ( s_bc_table = (long *) malloc( in_alphabet_size * sizeof(long) ) ) == nullptr ) mem_err(); if ( ! err ) { for ( i = 0; i < in_alphabet_size; ++i ) s_bc_table[ i ] = in_pat_len; for ( i = 0; i < in_pat_len - 1; ++i ) s_bc_table[ (uint8_t) in_pattern[ i ] ] = in_pat_len - i - 1; - if ( ( s_gs_table = (long *) malloc( 2 * ( in_pat_len + 1 ) * sizeof(long) ) ) == nil ) mem_err(); + if ( ( s_gs_table = (long *) malloc( 2 * ( in_pat_len + 1 ) * sizeof(long) ) ) == nullptr ) mem_err(); } if ( ! err ) { @@ -1603,8 +1598,8 @@ int boyer_moore_init( const char *in_pattern, long in_pat_len, long in_alphabet_ void boyer_moore_done() { - if ( s_bc_table ) { free( s_bc_table ); s_bc_table = nil; } - if ( s_gs_table ) { free( s_gs_table ); s_gs_table = nil; } + if ( s_bc_table ) { free( s_bc_table ); s_bc_table = nullptr; } + if ( s_gs_table ) { free( s_gs_table ); s_gs_table = nullptr; } } @@ -1623,7 +1618,7 @@ char *boyer_moore_search( char *in_text, long in_text_len ) { } } - return i < 0 ? in_text + j + 1 : nil; + return i < 0 ? in_text + j + 1 : nullptr; } @@ -1691,7 +1686,7 @@ int extract_file( int in_xiso, dir_node *in_file, modes in_mode , char* path) { int free_dir_node_avl( void *in_dir_node_avl, void *in_context, long in_depth ) { dir_node_avl *avl = (dir_node_avl *) in_dir_node_avl; - if ( avl->subdirectory && avl->subdirectory != EMPTY_SUBDIRECTORY ) avl_traverse_depth_first( avl->subdirectory, free_dir_node_avl, nil, k_postfix, 0 ); + if ( avl->subdirectory && avl->subdirectory != EMPTY_SUBDIRECTORY ) avl_traverse_depth_first( avl->subdirectory, free_dir_node_avl, nullptr, k_postfix, 0 ); free( avl->filename ); free( avl ); @@ -1760,7 +1755,7 @@ int write_file( dir_node_avl *in_avl, write_tree_context *in_context, int in_dep if ( ! in_avl->subdirectory ) { if ( lseek( in_context->xiso, (xoff_t) in_avl->start_sector * XISO_SECTOR_SIZE, SEEK_SET ) == -1 ) seek_err(); - if ( ! err && ( buf = (char *) malloc( ( size = max( XISO_SECTOR_SIZE, READWRITE_BUFFER_SIZE ) ) + 1 ) ) == nil ) mem_err(); + if ( ! err && ( buf = (char *) malloc( ( size = max( XISO_SECTOR_SIZE, READWRITE_BUFFER_SIZE ) ) + 1 ) ) == nullptr ) mem_err(); if ( ! err ) { if ( in_context->from == -1 ) { if ( ( fd = open( in_avl->filename, READFLAGS, 0 ) ) == -1 ) open_err( in_avl->filename ); @@ -1790,7 +1785,7 @@ int write_file( dir_node_avl *in_avl, write_tree_context *in_context, int in_dep } bytes -= n; if (s_media_enable && (len = strlen(in_avl->filename)) >= 4 && strcasecmp(&in_avl->filename[len - 4], ".xbe") == 0) { - for (buf[n += i] = 0, p = buf; (p = boyer_moore_search(p, n - (p - buf))) != nil; p += XISO_MEDIA_ENABLE_LENGTH) p[XISO_MEDIA_ENABLE_BYTE_POS] = XISO_MEDIA_ENABLE_BYTE; + for (buf[n += i] = 0, p = buf; (p = boyer_moore_search(p, n - (p - buf))) != nullptr; p += XISO_MEDIA_ENABLE_LENGTH) p[XISO_MEDIA_ENABLE_BYTE_POS] = XISO_MEDIA_ENABLE_BYTE; if (bytes) { i = XISO_MEDIA_ENABLE_LENGTH - 1; if (write(in_context->xiso, buf, n - i) != (int)n - i) { @@ -1913,7 +1908,7 @@ int write_dir_start_and_file_positions( dir_node_avl *in_avl, wdsafp_context *io int calculate_total_files_and_bytes( dir_node_avl *in_avl, void *in_context, int in_depth ) { if ( in_avl->subdirectory && in_avl->subdirectory != EMPTY_SUBDIRECTORY ) { - avl_traverse_depth_first( in_avl->subdirectory, (traversal_callback) calculate_total_files_and_bytes, nil, k_prefix, 0 ); + avl_traverse_depth_first( in_avl->subdirectory, (traversal_callback) calculate_total_files_and_bytes, nullptr, k_prefix, 0 ); } else { ++s_total_files; s_total_bytes += in_avl->file_size; @@ -1961,13 +1956,13 @@ int generate_avl_tree_local( dir_node_avl **out_root, int *io_n ) { struct dirent *p; struct stat sb; dir_node_avl *avl; - DIR *dir = nil; + DIR *dir = nullptr; int err = 0, i, j; bool empty_dir = true; - if ( ( dir = opendir( "." ) ) == nil ) mem_err(); + if ( ( dir = opendir( "." ) ) == nullptr ) mem_err(); - while ( ! err && ( p = readdir( dir ) ) != nil ) { + while ( ! err && ( p = readdir( dir ) ) != nullptr ) { if ( ! strcmp( p->d_name, "." ) || ! strcmp( p->d_name, ".." ) ) continue; for ( i = *io_n; i; --i ) exiso_log( "\b" ); @@ -1977,10 +1972,10 @@ int generate_avl_tree_local( dir_node_avl **out_root, int *io_n ) { *io_n = i; flush(); - if ( ( avl = (dir_node_avl *) malloc( sizeof(dir_node_avl) ) ) == nil ) mem_err(); + if ( ( avl = (dir_node_avl *) malloc( sizeof(dir_node_avl) ) ) == nullptr ) mem_err(); if ( ! err ) { memset( avl, 0, sizeof(dir_node_avl) ); - if ( ( avl->filename = strdup( p->d_name ) ) == nil ) mem_err(); + if ( ( avl->filename = strdup( p->d_name ) ) == nullptr ) mem_err(); } if ( ! err && stat( avl->filename, &sb ) == -1 ) read_err(); if ( ! err ) { @@ -2031,8 +2026,8 @@ FILE_TIME *alloc_filetime_now( void ) { time_t now; int err = 0; - if ( ( ft = (FILE_TIME *) malloc( sizeof(struct FILE_TIME) ) ) == nil ) mem_err(); - if ( ! err && ( now = time( nil ) ) == -1 ) unknown_err(); + if ( ( ft = (FILE_TIME *) malloc( sizeof(struct FILE_TIME) ) ) == nullptr ) mem_err(); + if ( ! err && ( now = time( nullptr ) ) == -1 ) unknown_err(); if ( ! err ) { tmp = ( (double) now + ( 369.0 * 365.25 * 24 * 60 * 60 - ( 3.0 * 24 * 60 * 60 + 6.0 * 60 * 60 ) ) ) * 1.0e7; @@ -2043,7 +2038,7 @@ FILE_TIME *alloc_filetime_now( void ) { little32( ft->l ); } else if ( ft ) { free( ft ); - ft = nil; + ft = nullptr; } return ft; @@ -2102,9 +2097,9 @@ void write_sector( int in_xiso, xoff_t in_start, char *in_name, char *in_extensi ssize_t wrote; xoff_t curpos; int fp = -1, err = 0; - char *cwd, *sect = nil, buf[ 256 ]; + char *cwd, *sect = nullptr, buf[ 256 ]; - if ( ( cwd = getcwd( nil, 0 ) ) == nil ) mem_err(); + if ( ( cwd = getcwd( nullptr, 0 ) ) == nullptr ) mem_err(); if ( ! err && chdir( DEBUG_DUMP_DIRECTORY ) == -1 ) chdir_err( DEBUG_DUMP_DIRECTORY ); sprintf( buf, "%llu.%s.%s", in_start, in_name, in_extension ? in_extension : "" ); @@ -2113,7 +2108,7 @@ void write_sector( int in_xiso, xoff_t in_start, char *in_name, char *in_extensi if ( ! err && ( curpos = lseek( in_xiso, 0, SEEK_CUR ) ) == -1 ) seek_err(); if ( ! err && lseek( in_xiso, in_start, SEEK_SET ) == -1 ) seek_err(); - if ( ! err && ( sect = (char *) malloc( XISO_SECTOR_SIZE ) ) == nil ) mem_err(); + if ( ! err && ( sect = (char *) malloc( XISO_SECTOR_SIZE ) ) == nullptr ) mem_err(); if ( ! err && read( in_xiso, sect, XISO_SECTOR_SIZE ) != XISO_SECTOR_SIZE ) read_err(); if ( ! err && ( wrote = write( fp, sect, XISO_SECTOR_SIZE ) ) != XISO_SECTOR_SIZE ) write_err(); From d58758bf645d0fd13e361301d422f41cfc407a0c Mon Sep 17 00:00:00 2001 From: Vladimir Olteanu Date: Wed, 22 Nov 2023 04:35:37 +0200 Subject: [PATCH 06/11] Do not define bool --- extract-xiso.cpp | 6 ------ 1 file changed, 6 deletions(-) diff --git a/extract-xiso.cpp b/extract-xiso.cpp index b2d0824..18aab71 100644 --- a/extract-xiso.cpp +++ b/extract-xiso.cpp @@ -366,12 +366,6 @@ #endif -#if ! defined( __cplusplus ) && ! defined( bool ) - typedef int bool; - enum { false, true }; -#endif - - #define exiso_version "2.7.1 (01.11.14)" #define VERSION_LENGTH 16 From 3052693eec32725e8e0a3da1f7d6a3084b1fea24 Mon Sep 17 00:00:00 2001 From: Vladimir Olteanu Date: Wed, 22 Nov 2023 04:47:00 +0200 Subject: [PATCH 07/11] Use std::{min,max} --- extract-xiso.cpp | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/extract-xiso.cpp b/extract-xiso.cpp index 18aab71..1ddc4ed 100644 --- a/extract-xiso.cpp +++ b/extract-xiso.cpp @@ -270,6 +270,10 @@ #include #endif +#include + +using namespace std; + #if defined( __DARWIN__ ) #define exiso_target "macos-x" @@ -428,12 +432,6 @@ #define misc_err( in_format, a, b, c ) { log_err( __FILE__, __LINE__, ( in_format ), ( a ), ( b ), ( c ) ); err = 1; } -#ifndef min - #define min( a , b ) ( ( a ) < ( b ) ? ( a ) : ( b ) ) - #define max( a , b ) ( ( a ) > ( b ) ? ( a ) : ( b ) ) -#endif - - #define GLOBAL_LSEEK_OFFSET 0x0FD90000ul #define XGD3_LSEEK_OFFSET 0x02080000ul #define XGD1_LSEEK_OFFSET 0x18300000ul @@ -442,7 +440,7 @@ #define XISO_HEADER_DATA "MICROSOFT*XBOX*MEDIA" #define XISO_HEADER_DATA_LENGTH 20 -#define XISO_HEADER_OFFSET 0x10000 +constexpr unsigned XISO_HEADER_OFFSET = 0x10000; #define XISO_FILE_MODULUS 0x10000 @@ -462,7 +460,7 @@ #define XISO_DWORD_SIZE 4 #define XISO_FILETIME_SIZE 8 -#define XISO_SECTOR_SIZE 2048 +constexpr unsigned XISO_SECTOR_SIZE = 2048; #define XISO_UNUSED_SIZE 0x7c8 #define XISO_FILENAME_OFFSET 14 @@ -486,7 +484,7 @@ #define EMPTY_SUBDIRECTORY ( (dir_node_avl *) 1 ) -#define READWRITE_BUFFER_SIZE 0x00200000 +constexpr unsigned READWRITE_BUFFER_SIZE = 0x00200000; #define DEBUG_DUMP_DIRECTORY "/Volumes/c/xbox/iso/exiso" From 7a06144674358af05705d3bcbf8b1c56be842ee7 Mon Sep 17 00:00:00 2001 From: Vladimir Olteanu Date: Wed, 22 Nov 2023 05:11:03 +0200 Subject: [PATCH 08/11] Usage no longer a macro --- extract-xiso.cpp | 50 ++++++++++++++++++++---------------------------- 1 file changed, 21 insertions(+), 29 deletions(-) diff --git a/extract-xiso.cpp b/extract-xiso.cpp index 1ddc4ed..931b6c8 100644 --- a/extract-xiso.cpp +++ b/extract-xiso.cpp @@ -375,7 +375,9 @@ using namespace std; #define banner "extract-xiso v" exiso_version " for " exiso_target " - written by in \n" -#define usage() fprintf( stderr, \ +void usage(const char *name) +{ + fprintf( stderr, \ "%s\n\ Usage:\n\ \n\ @@ -411,7 +413,8 @@ using namespace std; -Q Run silent (suppress all output).\n\ -s Skip $SystemUpdate folder.\n\ -v Print version information and exit.\n\ -", banner, argv[ 0 ], argv[ 0 ] ); +", banner, name, name ); +} #define exiso_log if ( ! s_quiet ) printf #define flush() if ( ! s_quiet ) fflush( stdout ) @@ -634,15 +637,14 @@ int main( int argc, char **argv ) { bool extract = true, rewrite = false, free_user = false, free_pass = false, x_seen = false, del = false, optimized; char *cwd = nullptr, *path = nullptr, *buf = nullptr, *new_iso_path = nullptr, tag[ XISO_OPTIMIZED_TAG_LENGTH * sizeof(long) ]; - if ( argc < 2 ) { usage(); exit( 1 ); } + if ( argc < 2 ) + usage( argv[0] ); while ( ! err && ( opt_char = getopt( argc, argv, GETOPT_STRING ) ) != -1 ) { switch ( opt_char ) { case 'c': { - if ( x_seen || rewrite || ! extract ) { - usage(); - exit( 1 ); - } + if ( x_seen || rewrite || ! extract ) + usage( argv[0] ); for ( r = &create; *r != nullptr; r = &(*r)->next ) ; @@ -666,23 +668,18 @@ int main( int argc, char **argv ) { } break; case 'h': { - usage(); - exit( 0 ); + usage( argv[0] ); } break; case 'l': { - if ( x_seen || rewrite || create ) { - usage(); - exit( 1 ); - } + if ( x_seen || rewrite || create ) + usage( argv[0] ); extract = false; } break; case 'm': { - if ( x_seen || ! extract ) { - usage(); - exit( 1 ); - } + if ( x_seen || ! extract ) + usage( argv[0] ); s_media_enable = false; } break; @@ -695,10 +692,8 @@ int main( int argc, char **argv ) { } break; case 'r': { - if ( x_seen || ! extract || create ) { - usage(); - exit( 1 ); - } + if ( x_seen || ! extract || create ) + usage( argv[0] ); rewrite = true; } break; @@ -712,24 +707,21 @@ int main( int argc, char **argv ) { } break; case 'x': { - if ( ! extract || rewrite || create ) { - usage(); - exit( 1 ); - } + if ( ! extract || rewrite || create ) + usage( argv[0] ); x_seen = true; } break; default: { - usage(); - exit( 1 ); + usage( argv[0] ); } break; } } if ( ! err ) { - if ( create ) { if ( optind < argc ) { usage(); exit( 1 ); } } - else if ( optind >= argc ) { usage(); exit( 1 ); } + if ( create ) { if ( optind < argc ) { usage( argv[0] ); } } + else if ( optind >= argc ) { usage( argv[0] ); } exiso_log( "%s", banner ); From 57c8d692b87b282234b283dd37ea34e67bfde18b Mon Sep 17 00:00:00 2001 From: Vladimir Olteanu Date: Wed, 22 Nov 2023 05:33:14 +0200 Subject: [PATCH 09/11] Simplify memory error handling --- extract-xiso.cpp | 174 +++++++++++++++++++++-------------------------- 1 file changed, 78 insertions(+), 96 deletions(-) diff --git a/extract-xiso.cpp b/extract-xiso.cpp index 931b6c8..cca3c9a 100644 --- a/extract-xiso.cpp +++ b/extract-xiso.cpp @@ -419,7 +419,7 @@ void usage(const char *name) #define exiso_log if ( ! s_quiet ) printf #define flush() if ( ! s_quiet ) fflush( stdout ) -#define mem_err() { log_err( __FILE__, __LINE__, "out of memory error\n" ); err = 1; } +#define mem_err() { log_err( __FILE__, __LINE__, "out of memory error\n" ); exit(1); } #define read_err() { log_err( __FILE__, __LINE__, "read error: %s\n", strerror( errno ) ); err = 1; } #define seek_err() { log_err( __FILE__, __LINE__, "seek error: %s\n", strerror( errno ) ); err = 1; } #define write_err() { log_err( __FILE__, __LINE__, "write error: %s\n", strerror( errno ) ); err = 1; } @@ -578,7 +578,7 @@ int avl_traverse_depth_first( dir_node_avl *in_root, traversal_callback in_callb void boyer_moore_done(); char *boyer_moore_search( char *in_text, long in_text_len ); -int boyer_moore_init(const char *in_pattern, long in_pat_len, long in_alphabet_size ); +void boyer_moore_init(const char *in_pattern, long in_pat_len, long in_alphabet_size ); int free_dir_node_avl( void *in_dir_node_avl, void *, long ); int extract_file( int in_xiso, dir_node *in_file, modes in_mode, char *path ); @@ -640,7 +640,7 @@ int main( int argc, char **argv ) { if ( argc < 2 ) usage( argv[0] ); - while ( ! err && ( opt_char = getopt( argc, argv, GETOPT_STRING ) ) != -1 ) { + while (( opt_char = getopt( argc, argv, GETOPT_STRING ) ) != -1 ) { switch ( opt_char ) { case 'c': { if ( x_seen || rewrite || ! extract ) @@ -649,13 +649,11 @@ int main( int argc, char **argv ) { for ( r = &create; *r != nullptr; r = &(*r)->next ) ; if ( ( *r = (create_list *) malloc( sizeof(create_list) ) ) == nullptr ) mem_err(); - if ( ! err ) { - (*r)->name = nullptr; - (*r)->next = nullptr; - - if ( ( (*r)->path = strdup( optarg ) ) == nullptr ) mem_err(); - } - if ( ! err && argv[ optind ] && *argv[ optind ] != '-' && *argv[ optind ] && ( (*r)->name = strdup( argv[ optind++ ] ) ) == nullptr ) mem_err(); + (*r)->name = nullptr; + (*r)->next = nullptr; + + if ( ( (*r)->path = strdup( optarg ) ) == nullptr ) mem_err(); + if ( argv[ optind ] && *argv[ optind ] != '-' && *argv[ optind ] && ( (*r)->name = strdup( argv[ optind++ ] ) ) == nullptr ) mem_err(); } break; case 'd': { @@ -718,19 +716,16 @@ int main( int argc, char **argv ) { } } - if ( ! err ) { + if ( create ) { if ( optind < argc ) { usage( argv[0] ); } } + else if ( optind >= argc ) { usage( argv[0] ); } - if ( create ) { if ( optind < argc ) { usage( argv[0] ); } } - else if ( optind >= argc ) { usage( argv[0] ); } - - exiso_log( "%s", banner ); - - if ( ( extract ) && ( s_copy_buffer = (char *) malloc( READWRITE_BUFFER_SIZE ) ) == nullptr ) mem_err(); - } + exiso_log( "%s", banner ); + + if ( ( extract ) && ( s_copy_buffer = (char *) malloc( READWRITE_BUFFER_SIZE ) ) == nullptr ) mem_err(); - if ( ! err && ( create || rewrite ) ) err = boyer_moore_init( XISO_MEDIA_ENABLE, XISO_MEDIA_ENABLE_LENGTH, k_default_alphabet_size ); + if ( create || rewrite ) boyer_moore_init( XISO_MEDIA_ENABLE, XISO_MEDIA_ENABLE_LENGTH, k_default_alphabet_size ); - if ( ! err && create ) { + if ( create ) { for ( p = create; ! err && p != nullptr; ) { char *tmp = nullptr; @@ -739,14 +734,12 @@ int main( int argc, char **argv ) { if ( i ) { if ( ( tmp = (char *) malloc( i + 1 ) ) == nullptr ) mem_err(); - if ( ! err ) { - strncpy( tmp, p->name, i ); - tmp[ i ] = 0; - } + strncpy( tmp, p->name, i ); + tmp[ i ] = 0; } } - if ( ! err ) err = create_xiso( p->path, tmp, nullptr, -1, nullptr, p->name ? p->name + i : nullptr, nullptr ); + err = create_xiso( p->path, tmp, nullptr, -1, nullptr, p->name ? p->name + i : nullptr, nullptr ); if ( tmp ) free( tmp ); @@ -764,42 +757,39 @@ int main( int argc, char **argv ) { s_total_bytes = s_total_files = 0; - if ( ! err ) { - optimized = false; - - if ( ( fd = open( argv[ i ], READFLAGS, 0 ) ) == -1 ) open_err( argv[ i ] ); - if ( ! err && lseek( fd, (xoff_t) XISO_OPTIMIZED_TAG_OFFSET, SEEK_SET ) == -1 ) seek_err(); - if ( ! err && read( fd, tag, XISO_OPTIMIZED_TAG_LENGTH ) != XISO_OPTIMIZED_TAG_LENGTH ) read_err(); - - if ( fd != -1 ) close( fd ); + optimized = false; - if ( ! err ) { - tag[ XISO_OPTIMIZED_TAG_LENGTH ] = 0; + if ( ( fd = open( argv[ i ], READFLAGS, 0 ) ) == -1 ) open_err( argv[ i ] ); + if ( ! err && lseek( fd, (xoff_t) XISO_OPTIMIZED_TAG_OFFSET, SEEK_SET ) == -1 ) seek_err(); + if ( ! err && read( fd, tag, XISO_OPTIMIZED_TAG_LENGTH ) != XISO_OPTIMIZED_TAG_LENGTH ) read_err(); - if ( ! strncmp( tag, XISO_OPTIMIZED_TAG, XISO_OPTIMIZED_TAG_LENGTH_MIN ) ) optimized = true; + if ( fd != -1 ) close( fd ); - if ( rewrite ) { - if ( optimized ) { - exiso_log( "%s is already optimized, skipping...\n", argv[ i ] ); - continue; - } - - if ( ! err && ( buf = (char *) malloc( strlen( argv[ i ] ) + 5 ) ) == nullptr ) mem_err(); // + 5 magic number is for ".old\0" - if ( ! err ) { - sprintf( buf, "%s.old", argv[ i ] ); - if ( stat( buf, &sb ) != -1 ) misc_err( "%s already exists, cannot rewrite %s\n", buf, argv[ i ], 0 ); - if ( ! err && rename( argv[ i ], buf ) == -1 ) misc_err( "cannot rename %s to %s\n", argv[ i ], buf, 0 ); - - if ( err ) { err = 0; free( buf ); continue; } - } - if ( ! err ) err = decode_xiso( buf, path, k_rewrite, &new_iso_path, true ); - if ( ! err && del && unlink( buf ) == -1 ) log_err( __FILE__, __LINE__, "unable to delete %s\n", buf ); - - if ( buf ) free( buf ); - } else { - // the order of the mutually exclusive options here is important, the extract ? k_extract : k_list test *must* be the final comparison - if ( ! err ) err = decode_xiso( argv[ i ], path, extract ? k_extract : k_list, nullptr, ! optimized ); + if ( ! err ) { + tag[ XISO_OPTIMIZED_TAG_LENGTH ] = 0; + + if ( ! strncmp( tag, XISO_OPTIMIZED_TAG, XISO_OPTIMIZED_TAG_LENGTH_MIN ) ) optimized = true; + + if ( rewrite ) { + if ( optimized ) { + exiso_log( "%s is already optimized, skipping...\n", argv[ i ] ); + continue; } + + if ( ! err && ( buf = (char *) malloc( strlen( argv[ i ] ) + 5 ) ) == nullptr ) mem_err(); // + 5 magic number is for ".old\0" + sprintf( buf, "%s.old", argv[ i ] ); + if ( stat( buf, &sb ) != -1 ) misc_err( "%s already exists, cannot rewrite %s\n", buf, argv[ i ], 0 ); + if ( ! err && rename( argv[ i ], buf ) == -1 ) misc_err( "cannot rename %s to %s\n", argv[ i ], buf, 0 ); + + if ( err ) { err = 0; free( buf ); continue; } + + if ( ! err ) err = decode_xiso( buf, path, k_rewrite, &new_iso_path, true ); + if ( ! err && del && unlink( buf ) == -1 ) log_err( __FILE__, __LINE__, "unable to delete %s\n", buf ); + + if ( buf ) free( buf ); + } else { + // the order of the mutually exclusive options here is important, the extract ? k_extract : k_list test *must* be the final comparison + if ( ! err ) err = decode_xiso( argv[ i ], path, extract ? k_extract : k_list, nullptr, ! optimized ); } } @@ -927,18 +917,16 @@ int create_xiso( char *in_root_directory, char *in_output_directory, dir_node_av memset( &root, 0, sizeof(dir_node_avl) ); if ( ( cwd = getcwd( nullptr, 0 ) ) == nullptr ) mem_err(); - if ( ! err ) { - if ( ! in_root ) { - if ( chdir( in_root_directory ) == -1 ) chdir_err( in_root_directory ); - if ( ! err ) { - if ( in_root_directory[ i = (int) strlen( in_root_directory ) - 1 ] == '/' || in_root_directory[ i ] == '\\' ) in_root_directory[ i-- ] = 0; - for ( iso_dir = &in_root_directory[ i ]; iso_dir >= in_root_directory && *iso_dir != PATH_CHAR; --iso_dir ) ; ++iso_dir; + if ( ! in_root ) { + if ( chdir( in_root_directory ) == -1 ) chdir_err( in_root_directory ); + if ( ! err ) { + if ( in_root_directory[ i = (int) strlen( in_root_directory ) - 1 ] == '/' || in_root_directory[ i ] == '\\' ) in_root_directory[ i-- ] = 0; + for ( iso_dir = &in_root_directory[ i ]; iso_dir >= in_root_directory && *iso_dir != PATH_CHAR; --iso_dir ) ; ++iso_dir; - iso_name = in_name ? in_name : iso_dir; - } - } else { - iso_dir = iso_name = in_root_directory; + iso_name = in_name ? in_name : iso_dir; } + } else { + iso_dir = iso_name = in_root_directory; } if ( ! err ) { if ( ! *iso_dir ) iso_dir = PATH_CHAR_STR; @@ -1017,7 +1005,7 @@ int create_xiso( char *in_root_directory, char *in_output_directory, dir_node_av memset( buf, 0, XISO_FILETIME_SIZE ); } else { if ( ( ft = alloc_filetime_now() ) == nullptr ) mem_err(); - if ( ! err && write( xiso, ft, XISO_FILETIME_SIZE ) != XISO_FILETIME_SIZE ) write_err(); + if ( write( xiso, ft, XISO_FILETIME_SIZE ) != XISO_FILETIME_SIZE ) write_err(); } } if ( ! err && write( xiso, buf, XISO_UNUSED_SIZE ) != XISO_UNUSED_SIZE ) write_err(); @@ -1541,43 +1529,37 @@ int avl_traverse_depth_first( dir_node_avl *in_root, traversal_callback in_callb #endif -int boyer_moore_init( const char *in_pattern, long in_pat_len, long in_alphabet_size ) { - long i, j, k, *backup, err = 0; +void boyer_moore_init( const char *in_pattern, long in_pat_len, long in_alphabet_size ) { + long i, j, k, *backup; s_pattern = in_pattern; s_pat_len = in_pat_len; if ( ( s_bc_table = (long *) malloc( in_alphabet_size * sizeof(long) ) ) == nullptr ) mem_err(); - if ( ! err ) { - for ( i = 0; i < in_alphabet_size; ++i ) s_bc_table[ i ] = in_pat_len; - for ( i = 0; i < in_pat_len - 1; ++i ) s_bc_table[ (uint8_t) in_pattern[ i ] ] = in_pat_len - i - 1; - - if ( ( s_gs_table = (long *) malloc( 2 * ( in_pat_len + 1 ) * sizeof(long) ) ) == nullptr ) mem_err(); - } + for ( i = 0; i < in_alphabet_size; ++i ) s_bc_table[ i ] = in_pat_len; + for ( i = 0; i < in_pat_len - 1; ++i ) s_bc_table[ (uint8_t) in_pattern[ i ] ] = in_pat_len - i - 1; - if ( ! err ) { - backup = s_gs_table + in_pat_len + 1; - - for ( i = 1; i <= in_pat_len; ++i ) s_gs_table[ i ] = 2 * in_pat_len - i; - for ( i = in_pat_len, j = in_pat_len + 1; i; --i, --j ) { - backup[ i ] = j; - - while ( j <= in_pat_len && in_pattern[ i - 1 ] != in_pattern[ j - 1 ] ) { - if ( s_gs_table[ j ] > in_pat_len - i ) s_gs_table[ j ] = in_pat_len - i; - j = backup[ j ]; - } - } - for ( i = 1; i <= j; ++i ) if ( s_gs_table[ i ] > in_pat_len + j - i ) s_gs_table[ i ] = in_pat_len + j - i; - - k = backup[ j ]; - - for ( ; j <= in_pat_len; k = backup[ k ] ) { - for ( ; j <= k; ++j ) if ( s_gs_table[ j ] >= k - j + in_pat_len ) s_gs_table[ j ] = k - j + in_pat_len; + if ( ( s_gs_table = (long *) malloc( 2 * ( in_pat_len + 1 ) * sizeof(long) ) ) == nullptr ) mem_err(); + + backup = s_gs_table + in_pat_len + 1; + + for ( i = 1; i <= in_pat_len; ++i ) s_gs_table[ i ] = 2 * in_pat_len - i; + for ( i = in_pat_len, j = in_pat_len + 1; i; --i, --j ) { + backup[ i ] = j; + + while ( j <= in_pat_len && in_pattern[ i - 1 ] != in_pattern[ j - 1 ] ) { + if ( s_gs_table[ j ] > in_pat_len - i ) s_gs_table[ j ] = in_pat_len - i; + j = backup[ j ]; } } - - return err; + for ( i = 1; i <= j; ++i ) if ( s_gs_table[ i ] > in_pat_len + j - i ) s_gs_table[ i ] = in_pat_len + j - i; + + k = backup[ j ]; + + for ( ; j <= in_pat_len; k = backup[ k ] ) { + for ( ; j <= k; ++j ) if ( s_gs_table[ j ] >= k - j + in_pat_len ) s_gs_table[ j ] = k - j + in_pat_len; + } } From b41efbdeec34a93f27001286743f601c86b1b06f Mon Sep 17 00:00:00 2001 From: Vladimir Olteanu Date: Wed, 22 Nov 2023 06:02:50 +0200 Subject: [PATCH 10/11] Get rid of redundant typedefs --- extract-xiso.cpp | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/extract-xiso.cpp b/extract-xiso.cpp index cca3c9a..f5cbddd 100644 --- a/extract-xiso.cpp +++ b/extract-xiso.cpp @@ -494,21 +494,19 @@ constexpr unsigned READWRITE_BUFFER_SIZE = 0x00200000; #define GETOPT_STRING "c:d:Dhlmp:qQrsvx" -typedef enum avl_skew { k_no_skew , k_left_skew , k_right_skew } avl_skew; -typedef enum avl_result { no_err, k_avl_error, k_avl_balanced } avl_result; -typedef enum avl_traversal_method { k_prefix, k_infix, k_postfix } avl_traversal_method; +enum avl_skew { k_no_skew , k_left_skew , k_right_skew }; +enum avl_result { no_err, k_avl_error, k_avl_balanced }; +enum avl_traversal_method { k_prefix, k_infix, k_postfix }; -typedef enum bm_constants { k_default_alphabet_size = 256 } bm_constants; +enum bm_constants { k_default_alphabet_size = 256 }; -typedef enum modes { k_generate_avl, k_extract, k_list, k_rewrite } modes; -typedef enum errors { err_end_of_sector = -5001, err_iso_rewritten = -5002, err_iso_no_files = -5003 } errors; +enum modes { k_generate_avl, k_extract, k_list, k_rewrite }; +enum errors { err_end_of_sector = -5001, err_iso_rewritten = -5002, err_iso_no_files = -5003 }; typedef void (*progress_callback)( xoff_t in_current_value, xoff_t in_final_value ); typedef int (*traversal_callback)( void *in_node, void *in_context, long in_depth ); -typedef struct dir_node dir_node; -typedef struct create_list create_list; -typedef struct dir_node_avl dir_node_avl; +struct dir_node_avl; struct dir_node { dir_node *left; From 36c3844a689829377c1236f88d8d4e9dc5571e20 Mon Sep 17 00:00:00 2001 From: Vladimir Olteanu Date: Wed, 22 Nov 2023 05:42:33 +0200 Subject: [PATCH 11/11] Replace homebrew Boyer Moore with STL --- extract-xiso.cpp | 73 +++++++----------------------------------------- 1 file changed, 10 insertions(+), 63 deletions(-) diff --git a/extract-xiso.cpp b/extract-xiso.cpp index f5cbddd..b15a7d5 100644 --- a/extract-xiso.cpp +++ b/extract-xiso.cpp @@ -271,6 +271,9 @@ #endif #include +#include +#include +#include using namespace std; @@ -574,9 +577,7 @@ dir_node_avl *avl_fetch( dir_node_avl *in_root, char *in_filename ); avl_result avl_insert( dir_node_avl **in_root, dir_node_avl *in_node ); int avl_traverse_depth_first( dir_node_avl *in_root, traversal_callback in_callback, void *in_context, avl_traversal_method in_method, long in_depth ); -void boyer_moore_done(); char *boyer_moore_search( char *in_text, long in_text_len ); -void boyer_moore_init(const char *in_pattern, long in_pat_len, long in_alphabet_size ); int free_dir_node_avl( void *in_dir_node_avl, void *, long ); int extract_file( int in_xiso, dir_node *in_file, modes in_mode, char *path ); @@ -605,9 +606,7 @@ void write_sector( int in_xiso, xoff_t in_start, char *in_name, char *in_extensi static long s_pat_len; static bool s_quiet = false; -static const char *s_pattern = nullptr; -static long *s_gs_table = nullptr; -static long *s_bc_table = nullptr; +static optional> bm_searcher; static xoff_t s_total_bytes = 0; static int s_total_files = 0; static char *s_copy_buffer = nullptr; @@ -721,7 +720,8 @@ int main( int argc, char **argv ) { if ( ( extract ) && ( s_copy_buffer = (char *) malloc( READWRITE_BUFFER_SIZE ) ) == nullptr ) mem_err(); - if ( create || rewrite ) boyer_moore_init( XISO_MEDIA_ENABLE, XISO_MEDIA_ENABLE_LENGTH, k_default_alphabet_size ); + if ( create || rewrite ) + bm_searcher.emplace(XISO_MEDIA_ENABLE, XISO_MEDIA_ENABLE + XISO_MEDIA_ENABLE_LENGTH); if ( create ) { for ( p = create; ! err && p != nullptr; ) { @@ -806,8 +806,6 @@ int main( int argc, char **argv ) { if ( ! err && isos > 1 ) exiso_log( "\n%u files in %u xiso's total %lld bytes\n", s_total_files_all_isos, isos, (long long int) s_total_bytes_all_isos ); if ( s_warned ) exiso_log( "\nWARNING: Warning(s) were issued during execution--review stderr!\n" ); - boyer_moore_done(); - if ( s_copy_buffer ) free( s_copy_buffer ); if ( path ) free( path ); @@ -1527,62 +1525,11 @@ int avl_traverse_depth_first( dir_node_avl *in_root, traversal_callback in_callb #endif -void boyer_moore_init( const char *in_pattern, long in_pat_len, long in_alphabet_size ) { - long i, j, k, *backup; - - s_pattern = in_pattern; - s_pat_len = in_pat_len; - - if ( ( s_bc_table = (long *) malloc( in_alphabet_size * sizeof(long) ) ) == nullptr ) mem_err(); - - for ( i = 0; i < in_alphabet_size; ++i ) s_bc_table[ i ] = in_pat_len; - for ( i = 0; i < in_pat_len - 1; ++i ) s_bc_table[ (uint8_t) in_pattern[ i ] ] = in_pat_len - i - 1; - - if ( ( s_gs_table = (long *) malloc( 2 * ( in_pat_len + 1 ) * sizeof(long) ) ) == nullptr ) mem_err(); - - backup = s_gs_table + in_pat_len + 1; - - for ( i = 1; i <= in_pat_len; ++i ) s_gs_table[ i ] = 2 * in_pat_len - i; - for ( i = in_pat_len, j = in_pat_len + 1; i; --i, --j ) { - backup[ i ] = j; - - while ( j <= in_pat_len && in_pattern[ i - 1 ] != in_pattern[ j - 1 ] ) { - if ( s_gs_table[ j ] > in_pat_len - i ) s_gs_table[ j ] = in_pat_len - i; - j = backup[ j ]; - } - } - for ( i = 1; i <= j; ++i ) if ( s_gs_table[ i ] > in_pat_len + j - i ) s_gs_table[ i ] = in_pat_len + j - i; - - k = backup[ j ]; - - for ( ; j <= in_pat_len; k = backup[ k ] ) { - for ( ; j <= k; ++j ) if ( s_gs_table[ j ] >= k - j + in_pat_len ) s_gs_table[ j ] = k - j + in_pat_len; - } -} - - -void boyer_moore_done() { - if ( s_bc_table ) { free( s_bc_table ); s_bc_table = nullptr; } - if ( s_gs_table ) { free( s_gs_table ); s_gs_table = nullptr; } -} - - char *boyer_moore_search( char *in_text, long in_text_len ) { - long i, j, k, l; - - for ( i = j = s_pat_len - 1; j < in_text_len && i >= 0; ) { - if ( in_text[ j ] == s_pattern[ i ] ) { --i; --j; } - else { - k = s_gs_table[ i + 1 ]; - l = s_bc_table[ (uint8_t) in_text[ j ] ]; - - j += max( k, l ); - - i = s_pat_len - 1; - } - } - - return i < 0 ? in_text + j + 1 : nullptr; + auto result = (*bm_searcher)(in_text, in_text + in_text_len); + if (result.first == in_text + in_text_len) + return nullptr; + return result.first; }