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

Signed-off-by: Alexandre Julliard <julliard@winehq.org>
This commit is contained in:
Alexandre Julliard 2022-01-19 10:57:30 +01:00
parent 49326cb258
commit c562952f92
8 changed files with 56 additions and 77 deletions

View File

@ -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;

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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 \

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)