From c562952f924fd1debc75e860ad45e65f66556552 Mon Sep 17 00:00:00 2001 From: Alexandre Julliard Date: Wed, 19 Jan 2022 10:57:30 +0100 Subject: [PATCH] tools: Add a helper function to read the contents of a file. Signed-off-by: Alexandre Julliard --- tools/tools.h | 22 ++++++++++++++++++++++ tools/winebuild/utils.c | 13 ++----------- tools/winedump/dump.c | 16 +++------------- tools/winedump/winedump.h | 2 +- tools/wmc/po.c | 20 ++++++-------------- tools/wmc/utils.c | 20 ++++++++------------ tools/wrc/po.c | 20 ++++++-------------- tools/wrc/utils.c | 20 ++++++++------------ 8 files changed, 56 insertions(+), 77 deletions(-) diff --git a/tools/tools.h b/tools/tools.h index 1065d25c6d6..1e86e38356d 100644 --- a/tools/tools.h +++ b/tools/tools.h @@ -350,6 +350,28 @@ static inline int make_temp_file( const char *prefix, const char *suffix, char * } +static inline void *read_file( const char *name, size_t *size ) +{ + struct stat st; + int res, fd; + void *data; + + if ((fd = open( name, O_RDONLY | O_BINARY )) == -1) return NULL; + fstat( fd, &st ); + data = xmalloc( st.st_size ); + res = read( fd, data, st.st_size ); + if (res == -1) + { + free( data ); + data = NULL; + *size = 0; + } + else *size = res; + close( fd ); + return data; +} + + static inline struct target get_default_target(void) { struct target target; diff --git a/tools/winebuild/utils.c b/tools/winebuild/utils.c index 42c5dca8516..5549691ef10 100644 --- a/tools/winebuild/utils.c +++ b/tools/winebuild/utils.c @@ -434,18 +434,9 @@ size_t output_buffer_size; void init_input_buffer( const char *file ) { - int fd; - struct stat st; - unsigned char *buffer; - - if ((fd = open( file, O_RDONLY | O_BINARY )) == -1) fatal_perror( "Cannot open %s", file ); - if ((fstat( fd, &st ) == -1)) fatal_perror( "Cannot stat %s", file ); - if (!st.st_size) fatal_error( "%s is an empty file\n", file ); - input_buffer = buffer = xmalloc( st.st_size ); - if (read( fd, buffer, st.st_size ) != st.st_size) fatal_error( "Cannot read %s\n", file ); - close( fd ); + if (!(input_buffer = read_file( file, &input_buffer_size ))) fatal_perror( "Cannot read %s", file ); + if (!input_buffer_size) fatal_error( "%s is an empty file\n", file ); input_buffer_filename = xstrdup( file ); - input_buffer_size = st.st_size; input_buffer_pos = 0; byte_swapped = 0; } diff --git a/tools/winedump/dump.c b/tools/winedump/dump.c index 33c98648d8c..512525fda7f 100644 --- a/tools/winedump/dump.c +++ b/tools/winedump/dump.c @@ -31,7 +31,7 @@ #include "winedump.h" void *dump_base = NULL; -unsigned long dump_total_len = 0; +size_t dump_total_len = 0; void dump_data( const unsigned char *ptr, unsigned int size, const char *prefix ) { @@ -264,23 +264,14 @@ dumpers[] = BOOL dump_analysis(const char *name, file_dumper fn, enum FileSig wanted_sig) { - int fd; BOOL ret = TRUE; - struct stat s; const struct dumper *dpr; setbuf(stdout, NULL); - fd = open(name, O_RDONLY | O_BINARY); - if (fd == -1) fatal("Can't open file"); + if (!(dump_base = read_file( name, &dump_total_len ))) fatal( "Cannot read file" ); - if (fstat(fd, &s) < 0) fatal("Can't get size"); - dump_total_len = s.st_size; - - dump_base = xmalloc( dump_total_len ); - if ((unsigned long)read( fd, dump_base, dump_total_len ) != dump_total_len) fatal( "Cannot read file" ); - - printf("Contents of %s: %ld bytes\n\n", name, dump_total_len); + printf("Contents of %s: %zu bytes\n\n", name, dump_total_len); for (dpr = dumpers; dpr->kind != SIG_UNKNOWN; dpr++) { @@ -299,7 +290,6 @@ BOOL dump_analysis(const char *name, file_dumper fn, enum FileSig wanted_sig) if (ret) printf("Done dumping %s\n", name); free( dump_base ); - close(fd); return ret; } diff --git a/tools/winedump/winedump.h b/tools/winedump/winedump.h index 6013d3caae6..c84a98946b3 100644 --- a/tools/winedump/winedump.h +++ b/tools/winedump/winedump.h @@ -139,7 +139,7 @@ typedef struct __globals extern _globals globals; extern void *dump_base; -extern unsigned long dump_total_len; +extern size_t dump_total_len; /* Names to use for output DLL */ #define OUTPUT_DLL_NAME \ diff --git a/tools/wmc/po.c b/tools/wmc/po.c index ae4df7ad733..174da69759e 100644 --- a/tools/wmc/po.c +++ b/tools/wmc/po.c @@ -542,21 +542,13 @@ static void byteswap( unsigned int *data, unsigned int count ) static void load_mo_file( const char *name ) { - struct stat st; - int res, fd; + size_t size; - fd = open( name, O_RDONLY | O_BINARY ); - if (fd == -1) fatal_perror( "Failed to open %s", name ); - fstat( fd, &st ); - mo_file = xmalloc( st.st_size ); - res = read( fd, mo_file, st.st_size ); - if (res == -1) fatal_perror( "Failed to read %s", name ); - else if (res != st.st_size) error( "Failed to read %s\n", name ); - close( fd ); + if (!(mo_file = read_file( name, &size ))) fatal_perror( "Failed to read %s", name ); /* sanity checks */ - if (st.st_size < sizeof(*mo_file)) + if (size < sizeof(*mo_file)) error( "%s is not a valid .mo file\n", name ); if (mo_file->magic == 0xde120495) byteswap( &mo_file->revision, 4 ); @@ -564,9 +556,9 @@ static void load_mo_file( const char *name ) error( "%s is not a valid .mo file\n", name ); if ((mo_file->revision >> 16) > 1) error( "%s: unsupported file version %x\n", name, mo_file->revision ); - if (mo_file->msgid_off >= st.st_size || - mo_file->msgstr_off >= st.st_size || - st.st_size < sizeof(*mo_file) + 2 * 8 * mo_file->count) + if (mo_file->msgid_off >= size || + mo_file->msgstr_off >= size || + size < sizeof(*mo_file) + 2 * 8 * mo_file->count) error( "%s: corrupted file\n", name ); if (mo_file->magic == 0xde120495) diff --git a/tools/wmc/utils.c b/tools/wmc/utils.c index 26becacfe87..4454add2f7d 100644 --- a/tools/wmc/utils.c +++ b/tools/wmc/utils.c @@ -356,11 +356,10 @@ static void init_nls_info( struct nls_info *info, unsigned short *ptr ) static const struct nls_info *get_nls_info( unsigned int codepage ) { - struct stat st; unsigned short *data; char *path; unsigned int i; - int fd; + size_t size; for (i = 0; i < ARRAY_SIZE(nlsinfo) && nlsinfo[i].codepage; i++) if (nlsinfo[i].codepage == codepage) return &nlsinfo[i]; @@ -370,18 +369,15 @@ static const struct nls_info *get_nls_info( unsigned int codepage ) for (i = 0; nlsdirs[i]; i++) { path = strmake( "%s/c_%03u.nls", nlsdirs[i], codepage ); - if ((fd = open( path, O_RDONLY )) != -1) break; + if ((data = read_file( path, &size ))) + { + free( path ); + init_nls_info( &nlsinfo[i], data ); + return &nlsinfo[i]; + } free( path ); } - if (!nlsdirs[i]) return NULL; - - fstat( fd, &st ); - data = xmalloc( st.st_size ); - if (read( fd, data, st.st_size ) != st.st_size) error( "failed to load %s\n", path ); - close( fd ); - free( path ); - init_nls_info( &nlsinfo[i], data ); - return &nlsinfo[i]; + return NULL; } int is_valid_codepage(int cp) diff --git a/tools/wrc/po.c b/tools/wrc/po.c index 42e06f93c10..b2e52c38c22 100644 --- a/tools/wrc/po.c +++ b/tools/wrc/po.c @@ -1102,21 +1102,13 @@ static void byteswap( unsigned int *data, unsigned int count ) static void load_mo_file( const char *name ) { - struct stat st; - int res, fd; + size_t size; - fd = open( name, O_RDONLY | O_BINARY ); - if (fd == -1) fatal_perror( "Failed to open %s", name ); - fstat( fd, &st ); - mo_file = xmalloc( st.st_size ); - res = read( fd, mo_file, st.st_size ); - if (res == -1) fatal_perror( "Failed to read %s", name ); - else if (res != st.st_size) error( "Failed to read %s\n", name ); - close( fd ); + if (!(mo_file = read_file( name, &size ))) fatal_perror( "Failed to read %s", name ); /* sanity checks */ - if (st.st_size < sizeof(*mo_file)) + if (size < sizeof(*mo_file)) error( "%s is not a valid .mo file\n", name ); if (mo_file->magic == 0xde120495) byteswap( &mo_file->revision, 4 ); @@ -1124,9 +1116,9 @@ static void load_mo_file( const char *name ) error( "%s is not a valid .mo file\n", name ); if ((mo_file->revision >> 16) > 1) error( "%s: unsupported file version %x\n", name, mo_file->revision ); - if (mo_file->msgid_off >= st.st_size || - mo_file->msgstr_off >= st.st_size || - st.st_size < sizeof(*mo_file) + 2 * 8 * mo_file->count) + if (mo_file->msgid_off >= size || + mo_file->msgstr_off >= size || + size < sizeof(*mo_file) + 2 * 8 * mo_file->count) error( "%s: corrupted file\n", name ); if (mo_file->magic == 0xde120495) diff --git a/tools/wrc/utils.c b/tools/wrc/utils.c index f6f36260617..b37bb226319 100644 --- a/tools/wrc/utils.c +++ b/tools/wrc/utils.c @@ -237,11 +237,10 @@ static void init_nls_info( struct nls_info *info, unsigned short *ptr ) static const struct nls_info *get_nls_info( unsigned int codepage ) { - struct stat st; unsigned short *data; char *path; unsigned int i; - int fd; + size_t size; for (i = 0; i < ARRAY_SIZE(nlsinfo) && nlsinfo[i].codepage; i++) if (nlsinfo[i].codepage == codepage) return &nlsinfo[i]; @@ -251,18 +250,15 @@ static const struct nls_info *get_nls_info( unsigned int codepage ) for (i = 0; nlsdirs[i]; i++) { path = strmake( "%s/c_%03u.nls", nlsdirs[i], codepage ); - if ((fd = open( path, O_RDONLY )) != -1) break; + if ((data = read_file( path, &size ))) + { + free( path ); + init_nls_info( &nlsinfo[i], data ); + return &nlsinfo[i]; + } free( path ); } - if (!nlsdirs[i]) return NULL; - - fstat( fd, &st ); - data = xmalloc( st.st_size ); - if (read( fd, data, st.st_size ) != st.st_size) error( "failed to load %s\n", path ); - close( fd ); - free( path ); - init_nls_info( &nlsinfo[i], data ); - return &nlsinfo[i]; + return NULL; } int is_valid_codepage(int cp)