Skip to content

Commit 1563302

Browse files
committed
Use unsigned char for buffers
Replace fors with whiles when appropriate Small cleanups
1 parent ae5f197 commit 1563302

File tree

1 file changed

+47
-41
lines changed

1 file changed

+47
-41
lines changed

extract-xiso.c

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -495,10 +495,10 @@ const xoff_t lseek_offsets[LSEEK_OFFSETS_LEN] = {START_LSEEK_OFFSET, XGD3_LSEEK_
495495
#define XISO_PAD_BYTE 0xff
496496
#define XISO_PAD_SHORT 0xffff
497497

498-
#define XISO_MEDIA_ENABLE "\xe8\xca\xfd\xff\xff\x85\xc0\x7d"
499-
#define XISO_MEDIA_ENABLE_BYTE '\xeb'
500498
#define XISO_MEDIA_ENABLE_LENGTH 8
501499
#define XISO_MEDIA_ENABLE_BYTE_POS 7
500+
const unsigned char xiso_media_enable[XISO_MEDIA_ENABLE_LENGTH] = { 0xe8, 0xca, 0xfd, 0xff, 0xff, 0x85, 0xc0, 0x7d };
501+
const unsigned char xiso_media_enable_byte = 0xeb;
502502

503503
#define n_sectors(size) ( (size) / XISO_SECTOR_SIZE + ( (size) % XISO_SECTOR_SIZE ? 1 : 0 ) )
504504
#define n_dword(offset) ( (offset) / XISO_DWORD_SIZE + ( (offset) % XISO_DWORD_SIZE ? 1 : 0 ) )
@@ -592,8 +592,8 @@ static avl_result avl_insert( dir_node_avl **in_root, dir_node_avl *in_node );
592592
static int avl_traverse_depth_first( dir_node_avl *in_root, traversal_callback in_callback, void *in_context, avl_traversal_method in_method, int in_depth );
593593

594594
static void boyer_moore_done();
595-
static char *boyer_moore_search( char *in_text, size_t in_text_len );
596-
static int boyer_moore_init( const char *in_pattern, size_t in_pat_len, size_t in_alphabet_size );
595+
static unsigned char *boyer_moore_search( unsigned char *in_text, size_t in_text_len );
596+
static int boyer_moore_init( const unsigned char *in_pattern, size_t in_pat_len, size_t in_alphabet_size );
597597

598598
static int free_dir_node_avl(dir_node_avl* in_dir_node_avl, void* in_context, int in_depth);
599599
static int extract_file( int in_xiso, dir_node *in_file, modes in_mode, const char *path );
@@ -625,12 +625,12 @@ static void write_sector( int in_xiso, xoff_t in_start, const char *in_name, con
625625

626626
static size_t s_pat_len;
627627
static bool s_quiet = false;
628-
static const char *s_pattern = NULL;
628+
static const unsigned char *s_pattern = NULL;
629629
static size_t *s_gs_table = NULL;
630630
static size_t *s_bc_table = NULL;
631631
static long long s_total_bytes = 0;
632632
static int s_total_files = 0;
633-
static char *s_copy_buffer = NULL;
633+
static unsigned char *s_copy_buffer = NULL;
634634
static bool s_real_quiet = false;
635635
static bool s_media_enable = true;
636636
static long long s_total_bytes_all_isos = 0;
@@ -742,7 +742,7 @@ int main( int argc, char **argv ) {
742742

743743
exiso_log(banner);
744744

745-
if ((extract || rewrite || create) && (s_copy_buffer = (char*)malloc(READWRITE_BUFFER_SIZE)) == NULL) mem_err();
745+
if ((extract || rewrite || create) && (s_copy_buffer = (unsigned char*)malloc(READWRITE_BUFFER_SIZE)) == NULL) mem_err();
746746

747747
}
748748

@@ -757,7 +757,7 @@ int main( int argc, char **argv ) {
757757
}
758758
}
759759

760-
if ( ! err && ( create || rewrite ) ) err = boyer_moore_init( XISO_MEDIA_ENABLE, XISO_MEDIA_ENABLE_LENGTH, k_default_alphabet_size );
760+
if ( ! err && ( create || rewrite ) ) err = boyer_moore_init( xiso_media_enable, XISO_MEDIA_ENABLE_LENGTH, k_default_alphabet_size );
761761

762762
if ( ! err && create ) {
763763
/* After this loop create will be NULL, so remember not to check it anymore */
@@ -949,7 +949,7 @@ static int log_err(unused_release const char* in_file, unused_release int in_lin
949949

950950
static int verify_xiso( int in_xiso, uint32_t *out_root_dir_sector, uint32_t *out_root_dir_size, const char *in_iso_name ) {
951951
int i, err = 0;
952-
char buffer[XISO_HEADER_DATA_LENGTH];
952+
unsigned char buffer[XISO_HEADER_DATA_LENGTH];
953953

954954
for (i = 0; !err && i < LSEEK_OFFSETS_LEN; i++) {
955955
if (lseek_with_error(in_xiso, (xoff_t)XISO_HEADER_OFFSET + lseek_offsets[i], SEEK_SET) == -1) seek_err();
@@ -967,7 +967,7 @@ static int verify_xiso( int in_xiso, uint32_t *out_root_dir_sector, uint32_t *ou
967967

968968
little32( *out_root_dir_sector );
969969
little32( *out_root_dir_size );
970-
970+
971971
// seek to header tail and verify media tag
972972
if ( ! err && lseek_with_error(in_xiso, (xoff_t)XISO_FILETIME_SIZE + XISO_UNUSED_SIZE, SEEK_CUR) == -1) seek_err();
973973
if ( ! err && read( in_xiso, buffer, XISO_HEADER_DATA_LENGTH ) != XISO_HEADER_DATA_LENGTH ) read_err();
@@ -983,12 +983,12 @@ static int verify_xiso( int in_xiso, uint32_t *out_root_dir_sector, uint32_t *ou
983983

984984

985985
static int create_xiso( char *in_root_directory, const char *in_output_directory, dir_node_avl *in_root, int in_xiso, char **out_iso_path, char *in_name, progress_callback in_progress_callback ) {
986-
xoff_t pos = 0;
986+
xoff_t size = 0;
987987
dir_node_avl root = { 0 };
988988
file_time_t ft = 0;
989989
write_tree_context wt_context = { 0 };
990990
uint32_t start_sector = 0;
991-
int i = 0, n = 0, xiso = -1, err = 0;
991+
int i = 0, n = 0, xiso = -1, err = 0, pad = 0;
992992
char *cwd = NULL, *iso_name = NULL, *xiso_path = NULL, *iso_dir = NULL, *real_path = NULL, *in_dir_path = NULL;
993993

994994
s_total_bytes = s_total_files = 0;
@@ -1118,14 +1118,15 @@ static int create_xiso( char *in_root_directory, const char *in_output_directory
11181118
err = avl_traverse_depth_first( &root, (traversal_callback) write_tree, &wt_context, k_prefix, 0 );
11191119
}
11201120

1121-
if ( ! err && ( pos = lseek( xiso, (xoff_t) 0, SEEK_END ) ) == -1 ) seek_err();
1121+
if ( ! err && ( size = lseek( xiso, (xoff_t) 0, SEEK_END ) ) == -1 ) seek_err();
11221122
if (!err) {
1123-
i = (int)((XISO_FILE_MODULUS - pos % XISO_FILE_MODULUS) % XISO_FILE_MODULUS);
1124-
memset(s_copy_buffer, 0, i);
1125-
if (write(xiso, s_copy_buffer, i) != i) write_err();
1123+
pad = (int)((XISO_FILE_MODULUS - (size % XISO_FILE_MODULUS)) % XISO_FILE_MODULUS);
1124+
size += pad;
1125+
memset(s_copy_buffer, 0, pad);
1126+
if (write(xiso, s_copy_buffer, pad) != pad) write_err();
11261127
}
11271128

1128-
if ( ! err ) err = write_volume_descriptors( xiso, (uint32_t)((pos + (xoff_t)i) / XISO_SECTOR_SIZE) );
1129+
if ( ! err ) err = write_volume_descriptors( xiso, (uint32_t)(size / XISO_SECTOR_SIZE) );
11291130

11301131
if ( ! err && lseek( xiso, (xoff_t) XISO_OPTIMIZED_TAG_OFFSET, SEEK_SET ) == -1 ) seek_err();
11311132
if ( ! err && write( xiso, XISO_OPTIMIZED_TAG, XISO_OPTIMIZED_TAG_LENGTH ) != XISO_OPTIMIZED_TAG_LENGTH ) write_err();
@@ -1267,15 +1268,15 @@ static int traverse_xiso(int in_xiso, xoff_t in_dir_start, uint16_t entry_offset
12671268
}
12681269
else if (strategy != discover_strategy) { // When discovering, the padding means end of sector
12691270
exiso_warn("Invalid node found and skipped!"); // When not discovering, the padding means a bad entry, skip it without failing
1270-
return err; // We're done if not discovering
1271+
return 0; // We're done if not discovering
12711272
}
12721273
// We're discovering, so set the offset to the start of the next sector
1273-
if (!err) entry_offset = n_sectors(entry_offset * XISO_DWORD_SIZE) * XISO_SECTOR_SIZE / XISO_DWORD_SIZE;
1274+
entry_offset = n_sectors(entry_offset * XISO_DWORD_SIZE) * XISO_SECTOR_SIZE / XISO_DWORD_SIZE;
12741275
continue;
12751276
}
12761277

12771278
// Read node
1278-
if (!err) if ((node = calloc(1, sizeof(dir_node))) == NULL) mem_err();
1279+
if (!err && (node = calloc(1, sizeof(dir_node))) == NULL) mem_err();
12791280
if (!err && read(in_xiso, &r_offset, XISO_TABLE_OFFSET_SIZE) != XISO_TABLE_OFFSET_SIZE) read_err();
12801281
if (!err && read(in_xiso, &node->start_sector, XISO_SECTOR_OFFSET_SIZE) != XISO_SECTOR_OFFSET_SIZE) read_err();
12811282
if (!err && read(in_xiso, &node->file_size, XISO_FILESIZE_SIZE) != XISO_FILESIZE_SIZE) read_err();
@@ -1444,15 +1445,15 @@ static int process_node(int in_xiso, dir_node* node, const char* in_path, modes
14441445
static dir_node_avl *avl_fetch( dir_node_avl *in_root, const char *in_filename ) {
14451446
int result;
14461447

1447-
for ( ;; ) {
1448-
if ( in_root == NULL ) return NULL;
1449-
1448+
while (in_root != NULL) {
14501449
result = avl_compare_key( in_filename, in_root->filename_cp1252);
14511450

14521451
if ( result < 0 ) in_root = in_root->left;
14531452
else if ( result > 0 ) in_root = in_root->right;
14541453
else return in_root;
14551454
}
1455+
1456+
return NULL;
14561457
}
14571458

14581459

@@ -1567,20 +1568,19 @@ static void avl_rotate_right( dir_node_avl **in_root ) {
15671568
}
15681569

15691570

1570-
static int avl_compare_key( const char *in_lhs, const char *in_rhs ) {
1571+
static int avl_compare_key(const char* in_lhs, const char* in_rhs) {
15711572
unsigned char a, b;
15721573

1573-
for ( ;; ) {
1574+
do {
15741575
a = (unsigned char)toupperCP1252(*in_lhs++);
15751576
b = (unsigned char)toupperCP1252(*in_rhs++);
1576-
1577-
if ( a ) {
1578-
if ( b ) {
1579-
if ( a < b ) return -1;
1580-
if ( a > b ) return 1;
1581-
} else return 1;
1582-
} else return b ? -1 : 0;
1583-
}
1577+
1578+
if (a < b) return -1;
1579+
if (a > b) return 1;
1580+
} while (a && b);
1581+
1582+
/* If we're here, both a and b are '\0', otherwise we would have returned before. */
1583+
return 0;
15841584
}
15851585

15861586

@@ -1620,7 +1620,7 @@ static int avl_traverse_depth_first( dir_node_avl *in_root, traversal_callback i
16201620
#endif
16211621

16221622

1623-
static int boyer_moore_init( const char *in_pattern, size_t in_pat_len, size_t in_alphabet_size ) {
1623+
static int boyer_moore_init( const unsigned char *in_pattern, size_t in_pat_len, size_t in_alphabet_size ) {
16241624
size_t j, k, t, t1, q, q1, *aux = NULL;
16251625
int err = 0;
16261626

@@ -1635,7 +1635,7 @@ static int boyer_moore_init( const char *in_pattern, size_t in_pat_len, size_t i
16351635
if ((s_bc_table = (size_t*)malloc(in_alphabet_size * sizeof(size_t))) == NULL) mem_err();
16361636
if (!err) {
16371637
for (k = 0; k < in_alphabet_size; k++) s_bc_table[k] = in_pat_len;
1638-
for (k = 0; k < in_pat_len; k++) s_bc_table[(unsigned char)in_pattern[k]] = in_pat_len - 1 - k;
1638+
for (k = 0; k < in_pat_len; k++) s_bc_table[in_pattern[k]] = in_pat_len - 1 - k;
16391639
}
16401640

16411641
// Delta2 table (dd' algorithm with Rytter correction)
@@ -1683,7 +1683,7 @@ static void boyer_moore_done() {
16831683
}
16841684

16851685

1686-
static char* boyer_moore_search(char* in_text, size_t in_text_len) {
1686+
static unsigned char* boyer_moore_search(unsigned char* in_text, size_t in_text_len) {
16871687
size_t i, j;
16881688

16891689
if (s_pat_len == 0) return in_text;
@@ -1694,7 +1694,7 @@ static char* boyer_moore_search(char* in_text, size_t in_text_len) {
16941694
if (j == 0) return in_text + i;
16951695
}
16961696

1697-
i += max(s_bc_table[(unsigned char)in_text[i]], s_gs_table[j]);
1697+
i += max(s_bc_table[in_text[i]], s_gs_table[j]);
16981698
}
16991699
return NULL;
17001700
}
@@ -1815,7 +1815,7 @@ static int write_tree( dir_node_avl *in_avl, write_tree_context *in_context, unu
18151815

18161816

18171817
static int write_file( dir_node_avl *in_avl, write_tree_context *in_context, unused int in_depth ) {
1818-
char *p = NULL;
1818+
unsigned char* p = NULL;
18191819
uint32_t bytes = 0, size = 0;
18201820
int err = 0, fd = -1, n = 0, i = 0;
18211821
size_t len;
@@ -1849,7 +1849,11 @@ static int write_file( dir_node_avl *in_avl, write_tree_context *in_context, unu
18491849
bytes -= n;
18501850
if (s_media_enable && xbe_file) {
18511851
n += i;
1852-
for (p = s_copy_buffer; (p = boyer_moore_search(p, n - (long)(p - s_copy_buffer))) != NULL; p += XISO_MEDIA_ENABLE_LENGTH) p[XISO_MEDIA_ENABLE_BYTE_POS] = XISO_MEDIA_ENABLE_BYTE;
1852+
p = s_copy_buffer;
1853+
while ((p = boyer_moore_search(p, (size_t)n - (p - s_copy_buffer))) != NULL) {
1854+
p[XISO_MEDIA_ENABLE_BYTE_POS] = xiso_media_enable_byte;
1855+
p += XISO_MEDIA_ENABLE_LENGTH;
1856+
}
18531857
if (bytes) {
18541858
i = XISO_MEDIA_ENABLE_LENGTH - 1;
18551859
n -= i;
@@ -2187,7 +2191,9 @@ static void write_sector( int in_xiso, xoff_t in_start, const char *in_name, con
21872191
ssize_t wrote;
21882192
xoff_t curpos = 0;
21892193
int fp = -1, err = 0;
2190-
char *cwd, *sect = NULL, buf[ 256 ];
2194+
char buf[256];
2195+
char *cwd;
2196+
unsigned char *sect = NULL;
21912197

21922198
if ( ( cwd = getcwd( NULL, 0 ) ) == NULL ) mem_err();
21932199
if ( ! err && chdir( DEBUG_DUMP_DIRECTORY ) == -1 ) chdir_err( DEBUG_DUMP_DIRECTORY );
@@ -2198,7 +2204,7 @@ static void write_sector( int in_xiso, xoff_t in_start, const char *in_name, con
21982204
if ( ! err && ( curpos = lseek_with_error( in_xiso, 0, SEEK_CUR ) ) == -1 ) seek_err();
21992205
if ( ! err && lseek_with_error( in_xiso, in_start, SEEK_SET ) == -1 ) seek_err();
22002206

2201-
if ( ! err && ( sect = (char *) malloc( XISO_SECTOR_SIZE ) ) == NULL ) mem_err();
2207+
if ( ! err && ( sect = (unsigned char *) malloc( XISO_SECTOR_SIZE ) ) == NULL ) mem_err();
22022208

22032209
if ( ! err && read( in_xiso, sect, XISO_SECTOR_SIZE ) != XISO_SECTOR_SIZE ) read_err();
22042210
if ( ! err && ( wrote = write( fp, sect, XISO_SECTOR_SIZE ) ) != XISO_SECTOR_SIZE ) write_err();

0 commit comments

Comments
 (0)