Commit c562952f authored by Alexandre Julliard's avatar Alexandre Julliard

tools: Add a helper function to read the contents of a file.

parent 49326cb2
...@@ -350,6 +350,28 @@ static inline int make_temp_file( const char *prefix, const char *suffix, char * ...@@ -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) static inline struct target get_default_target(void)
{ {
struct target target; struct target target;
......
...@@ -434,18 +434,9 @@ size_t output_buffer_size; ...@@ -434,18 +434,9 @@ size_t output_buffer_size;
void init_input_buffer( const char *file ) void init_input_buffer( const char *file )
{ {
int fd; if (!(input_buffer = read_file( file, &input_buffer_size ))) fatal_perror( "Cannot read %s", file );
struct stat st; if (!input_buffer_size) fatal_error( "%s is an empty file\n", file );
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 );
input_buffer_filename = xstrdup( file ); input_buffer_filename = xstrdup( file );
input_buffer_size = st.st_size;
input_buffer_pos = 0; input_buffer_pos = 0;
byte_swapped = 0; byte_swapped = 0;
} }
......
...@@ -31,7 +31,7 @@ ...@@ -31,7 +31,7 @@
#include "winedump.h" #include "winedump.h"
void *dump_base = NULL; 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 ) void dump_data( const unsigned char *ptr, unsigned int size, const char *prefix )
{ {
...@@ -264,23 +264,14 @@ dumpers[] = ...@@ -264,23 +264,14 @@ dumpers[] =
BOOL dump_analysis(const char *name, file_dumper fn, enum FileSig wanted_sig) BOOL dump_analysis(const char *name, file_dumper fn, enum FileSig wanted_sig)
{ {
int fd;
BOOL ret = TRUE; BOOL ret = TRUE;
struct stat s;
const struct dumper *dpr; const struct dumper *dpr;
setbuf(stdout, NULL); setbuf(stdout, NULL);
fd = open(name, O_RDONLY | O_BINARY); if (!(dump_base = read_file( name, &dump_total_len ))) fatal( "Cannot read file" );
if (fd == -1) fatal("Can't open file");
if (fstat(fd, &s) < 0) fatal("Can't get size"); printf("Contents of %s: %zu bytes\n\n", name, dump_total_len);
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);
for (dpr = dumpers; dpr->kind != SIG_UNKNOWN; dpr++) 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) ...@@ -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); if (ret) printf("Done dumping %s\n", name);
free( dump_base ); free( dump_base );
close(fd);
return ret; return ret;
} }
......
...@@ -139,7 +139,7 @@ typedef struct __globals ...@@ -139,7 +139,7 @@ typedef struct __globals
extern _globals globals; extern _globals globals;
extern void *dump_base; extern void *dump_base;
extern unsigned long dump_total_len; extern size_t dump_total_len;
/* Names to use for output DLL */ /* Names to use for output DLL */
#define OUTPUT_DLL_NAME \ #define OUTPUT_DLL_NAME \
......
...@@ -542,21 +542,13 @@ static void byteswap( unsigned int *data, unsigned int count ) ...@@ -542,21 +542,13 @@ static void byteswap( unsigned int *data, unsigned int count )
static void load_mo_file( const char *name ) static void load_mo_file( const char *name )
{ {
struct stat st; size_t size;
int res, fd;
if (!(mo_file = read_file( name, &size ))) fatal_perror( "Failed to read %s", name );
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 );
/* sanity checks */ /* sanity checks */
if (st.st_size < sizeof(*mo_file)) if (size < sizeof(*mo_file))
error( "%s is not a valid .mo file\n", name ); error( "%s is not a valid .mo file\n", name );
if (mo_file->magic == 0xde120495) if (mo_file->magic == 0xde120495)
byteswap( &mo_file->revision, 4 ); byteswap( &mo_file->revision, 4 );
...@@ -564,9 +556,9 @@ static void load_mo_file( const char *name ) ...@@ -564,9 +556,9 @@ static void load_mo_file( const char *name )
error( "%s is not a valid .mo file\n", name ); error( "%s is not a valid .mo file\n", name );
if ((mo_file->revision >> 16) > 1) if ((mo_file->revision >> 16) > 1)
error( "%s: unsupported file version %x\n", name, mo_file->revision ); error( "%s: unsupported file version %x\n", name, mo_file->revision );
if (mo_file->msgid_off >= st.st_size || if (mo_file->msgid_off >= size ||
mo_file->msgstr_off >= st.st_size || mo_file->msgstr_off >= size ||
st.st_size < sizeof(*mo_file) + 2 * 8 * mo_file->count) size < sizeof(*mo_file) + 2 * 8 * mo_file->count)
error( "%s: corrupted file\n", name ); error( "%s: corrupted file\n", name );
if (mo_file->magic == 0xde120495) if (mo_file->magic == 0xde120495)
......
...@@ -356,11 +356,10 @@ static void init_nls_info( struct nls_info *info, unsigned short *ptr ) ...@@ -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 ) static const struct nls_info *get_nls_info( unsigned int codepage )
{ {
struct stat st;
unsigned short *data; unsigned short *data;
char *path; char *path;
unsigned int i; unsigned int i;
int fd; size_t size;
for (i = 0; i < ARRAY_SIZE(nlsinfo) && nlsinfo[i].codepage; i++) for (i = 0; i < ARRAY_SIZE(nlsinfo) && nlsinfo[i].codepage; i++)
if (nlsinfo[i].codepage == codepage) return &nlsinfo[i]; if (nlsinfo[i].codepage == codepage) return &nlsinfo[i];
...@@ -370,18 +369,15 @@ static const struct nls_info *get_nls_info( unsigned int codepage ) ...@@ -370,18 +369,15 @@ static const struct nls_info *get_nls_info( unsigned int codepage )
for (i = 0; nlsdirs[i]; i++) for (i = 0; nlsdirs[i]; i++)
{ {
path = strmake( "%s/c_%03u.nls", nlsdirs[i], codepage ); 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 ); {
}
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 ); free( path );
init_nls_info( &nlsinfo[i], data ); init_nls_info( &nlsinfo[i], data );
return &nlsinfo[i]; return &nlsinfo[i];
}
free( path );
}
return NULL;
} }
int is_valid_codepage(int cp) int is_valid_codepage(int cp)
......
...@@ -1102,21 +1102,13 @@ static void byteswap( unsigned int *data, unsigned int count ) ...@@ -1102,21 +1102,13 @@ static void byteswap( unsigned int *data, unsigned int count )
static void load_mo_file( const char *name ) static void load_mo_file( const char *name )
{ {
struct stat st; size_t size;
int res, fd;
if (!(mo_file = read_file( name, &size ))) fatal_perror( "Failed to read %s", name );
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 );
/* sanity checks */ /* sanity checks */
if (st.st_size < sizeof(*mo_file)) if (size < sizeof(*mo_file))
error( "%s is not a valid .mo file\n", name ); error( "%s is not a valid .mo file\n", name );
if (mo_file->magic == 0xde120495) if (mo_file->magic == 0xde120495)
byteswap( &mo_file->revision, 4 ); byteswap( &mo_file->revision, 4 );
...@@ -1124,9 +1116,9 @@ static void load_mo_file( const char *name ) ...@@ -1124,9 +1116,9 @@ static void load_mo_file( const char *name )
error( "%s is not a valid .mo file\n", name ); error( "%s is not a valid .mo file\n", name );
if ((mo_file->revision >> 16) > 1) if ((mo_file->revision >> 16) > 1)
error( "%s: unsupported file version %x\n", name, mo_file->revision ); error( "%s: unsupported file version %x\n", name, mo_file->revision );
if (mo_file->msgid_off >= st.st_size || if (mo_file->msgid_off >= size ||
mo_file->msgstr_off >= st.st_size || mo_file->msgstr_off >= size ||
st.st_size < sizeof(*mo_file) + 2 * 8 * mo_file->count) size < sizeof(*mo_file) + 2 * 8 * mo_file->count)
error( "%s: corrupted file\n", name ); error( "%s: corrupted file\n", name );
if (mo_file->magic == 0xde120495) if (mo_file->magic == 0xde120495)
......
...@@ -237,11 +237,10 @@ static void init_nls_info( struct nls_info *info, unsigned short *ptr ) ...@@ -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 ) static const struct nls_info *get_nls_info( unsigned int codepage )
{ {
struct stat st;
unsigned short *data; unsigned short *data;
char *path; char *path;
unsigned int i; unsigned int i;
int fd; size_t size;
for (i = 0; i < ARRAY_SIZE(nlsinfo) && nlsinfo[i].codepage; i++) for (i = 0; i < ARRAY_SIZE(nlsinfo) && nlsinfo[i].codepage; i++)
if (nlsinfo[i].codepage == codepage) return &nlsinfo[i]; if (nlsinfo[i].codepage == codepage) return &nlsinfo[i];
...@@ -251,18 +250,15 @@ static const struct nls_info *get_nls_info( unsigned int codepage ) ...@@ -251,18 +250,15 @@ static const struct nls_info *get_nls_info( unsigned int codepage )
for (i = 0; nlsdirs[i]; i++) for (i = 0; nlsdirs[i]; i++)
{ {
path = strmake( "%s/c_%03u.nls", nlsdirs[i], codepage ); 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 ); {
}
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 ); free( path );
init_nls_info( &nlsinfo[i], data ); init_nls_info( &nlsinfo[i], data );
return &nlsinfo[i]; return &nlsinfo[i];
}
free( path );
}
return NULL;
} }
int is_valid_codepage(int cp) int is_valid_codepage(int cp)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment