Commit b53773cf authored by Alexandre Julliard's avatar Alexandre Julliard

tools: Move the output buffer functions to the common header.

parent 7f0943dd
......@@ -561,6 +561,86 @@ static inline struct target init_argv0_target( const char *argv0 )
}
/* output buffer management */
extern unsigned char *output_buffer;
extern size_t output_buffer_pos;
extern size_t output_buffer_size;
static inline void check_output_buffer_space( size_t size )
{
if (output_buffer_pos + size >= output_buffer_size)
{
output_buffer_size = max( output_buffer_size * 2, output_buffer_pos + size );
output_buffer = xrealloc( output_buffer, output_buffer_size );
}
}
static inline void init_output_buffer(void)
{
output_buffer_size = 1024;
output_buffer_pos = 0;
output_buffer = xmalloc( output_buffer_size );
}
static inline void put_data( const void *data, size_t size )
{
check_output_buffer_space( size );
memcpy( output_buffer + output_buffer_pos, data, size );
output_buffer_pos += size;
}
static inline void put_byte( unsigned char val )
{
check_output_buffer_space( 1 );
output_buffer[output_buffer_pos++] = val;
}
static inline void put_word( unsigned short val )
{
check_output_buffer_space( 2 );
output_buffer[output_buffer_pos++] = val;
output_buffer[output_buffer_pos++] = val >> 8;
}
static inline void put_dword( unsigned int val )
{
check_output_buffer_space( 4 );
output_buffer[output_buffer_pos++] = val;
output_buffer[output_buffer_pos++] = val >> 8;
output_buffer[output_buffer_pos++] = val >> 16;
output_buffer[output_buffer_pos++] = val >> 24;
}
static inline void put_qword( unsigned int val )
{
put_dword( val );
put_dword( 0 );
}
static inline void align_output( unsigned int align )
{
size_t size = align - (output_buffer_pos % align);
if (size == align) return;
check_output_buffer_space( size );
memset( output_buffer + output_buffer_pos, 0, size );
output_buffer_pos += size;
}
static inline void flush_output_buffer( const char *name )
{
int fd = open( name, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666 );
if (fd == -1 || write( fd, output_buffer, output_buffer_pos ) != output_buffer_pos)
{
perror( name );
exit(1);
}
close( fd );
free( output_buffer );
}
/* command-line option parsing */
/* partly based on the Glibc getopt() implementation */
......
......@@ -222,32 +222,6 @@ static struct resource
} resources[16];
static unsigned int nb_resources;
static void check_output_buffer_space( size_t size )
{
if (output_buffer_pos + size >= output_buffer_size)
{
output_buffer_size = max( output_buffer_size * 2, output_buffer_pos + size );
output_buffer = xrealloc( output_buffer, output_buffer_size );
}
}
void init_output_buffer(void)
{
output_buffer_size = 1024;
output_buffer_pos = 0;
output_buffer = xmalloc( output_buffer_size );
}
void flush_output_buffer( const char *name )
{
int fd = open( name, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666 );
if (fd == -1) error( "Error creating %s\n", name );
if (write( fd, output_buffer, output_buffer_pos ) != output_buffer_pos)
error( "Error writing to %s\n", name );
close( fd );
free( output_buffer );
}
static inline void put_resource_id( const char *str )
{
if (str[0] != '#')
......@@ -335,41 +309,6 @@ void flush_output_resources( const char *name )
free( output_buffer );
}
void put_data( const void *data, size_t size )
{
check_output_buffer_space( size );
memcpy( output_buffer + output_buffer_pos, data, size );
output_buffer_pos += size;
}
void put_byte( unsigned char val )
{
check_output_buffer_space( 1 );
output_buffer[output_buffer_pos++] = val;
}
void put_word( unsigned short val )
{
check_output_buffer_space( 2 );
output_buffer[output_buffer_pos++] = val;
output_buffer[output_buffer_pos++] = val >> 8;
}
void put_dword( unsigned int val )
{
check_output_buffer_space( 4 );
output_buffer[output_buffer_pos++] = val;
output_buffer[output_buffer_pos++] = val >> 8;
output_buffer[output_buffer_pos++] = val >> 16;
output_buffer[output_buffer_pos++] = val >> 24;
}
void put_qword( unsigned int val )
{
put_dword( val );
put_dword( 0 );
}
/* pointer-sized word */
void put_pword( unsigned int val )
{
......@@ -402,13 +341,3 @@ void put_str( int indent, const char *format, ... )
check_output_buffer_space( size );
}
}
void align_output( unsigned int align )
{
size_t size = align - (output_buffer_pos % align);
if (size == align) return;
check_output_buffer_space( size );
memset( output_buffer + output_buffer_pos, 0, size );
output_buffer_pos += size;
}
......@@ -40,22 +40,10 @@ int is_valid_uuid(const char *s);
/* buffer management */
extern unsigned char *output_buffer;
extern size_t output_buffer_pos;
extern size_t output_buffer_size;
extern void init_output_buffer(void);
extern void flush_output_buffer( const char *name );
extern void add_output_to_resources( const char *type, const char *name );
extern void flush_output_resources( const char *name );
extern void put_data( const void *data, size_t size );
extern void put_byte( unsigned char val );
extern void put_word( unsigned short val );
extern void put_dword( unsigned int val );
extern void put_qword( unsigned int val );
extern void put_pword( unsigned int val );
extern void put_str( int indent, const char *format, ... ) __attribute__((format (printf, 2, 3)));
extern void align_output( unsigned int align );
/* typelibs expect the minor version to be stored in the higher bits and
* major to be stored in the lower bits */
......
......@@ -307,23 +307,12 @@ extern const char *input_buffer_filename;
extern const unsigned char *input_buffer;
extern size_t input_buffer_pos;
extern size_t input_buffer_size;
extern unsigned char *output_buffer;
extern size_t output_buffer_pos;
extern size_t output_buffer_size;
extern void init_input_buffer( const char *file );
extern void init_output_buffer(void);
extern void flush_output_buffer(void);
extern unsigned char get_byte(void);
extern unsigned short get_word(void);
extern unsigned int get_dword(void);
extern void put_data( const void *data, size_t size );
extern void put_byte( unsigned char val );
extern void put_word( unsigned short val );
extern void put_dword( unsigned int val );
extern void put_qword( unsigned int val );
extern void put_pword( unsigned int val );
extern void align_output( unsigned int align );
/* global variables */
......
......@@ -668,7 +668,7 @@ void output_res_o_file( DLLSPEC *spec )
/* if the output file name is a .res too, don't run the results through windres */
if (strendswith( output_file_name, ".res"))
{
flush_output_buffer();
flush_output_buffer( output_file_name );
return;
}
......
......@@ -1041,7 +1041,7 @@ void output_fake_module( DLLSPEC *spec )
put_data( pe.sec[i].ptr, pe.sec[i].size );
}
flush_output_buffer();
flush_output_buffer( output_file_name ? output_file_name : spec->file_name );
}
......
......@@ -415,15 +415,6 @@ unsigned char *output_buffer;
size_t output_buffer_pos;
size_t output_buffer_size;
static void check_output_buffer_space( size_t size )
{
if (output_buffer_pos + size >= output_buffer_size)
{
output_buffer_size = max( output_buffer_size * 2, output_buffer_pos + size );
output_buffer = xrealloc( output_buffer, output_buffer_size );
}
}
void init_input_buffer( const char *file )
{
int fd;
......@@ -442,22 +433,6 @@ void init_input_buffer( const char *file )
byte_swapped = 0;
}
void init_output_buffer(void)
{
output_buffer_size = 1024;
output_buffer_pos = 0;
output_buffer = xmalloc( output_buffer_size );
}
void flush_output_buffer(void)
{
open_output_file();
if (fwrite( output_buffer, 1, output_buffer_pos, output_file ) != output_buffer_pos)
fatal_error( "Error writing to %s\n", output_file_name );
close_output_file();
free( output_buffer );
}
unsigned char get_byte(void)
{
if (input_buffer_pos >= input_buffer_size)
......@@ -490,46 +465,6 @@ unsigned int get_dword(void)
return ret;
}
void put_data( const void *data, size_t size )
{
check_output_buffer_space( size );
memcpy( output_buffer + output_buffer_pos, data, size );
output_buffer_pos += size;
}
void put_byte( unsigned char val )
{
check_output_buffer_space( 1 );
output_buffer[output_buffer_pos++] = val;
}
void put_word( unsigned short val )
{
if (byte_swapped) val = (val << 8) | (val >> 8);
put_data( &val, sizeof(val) );
}
void put_dword( unsigned int val )
{
if (byte_swapped)
val = ((val << 24) | ((val << 8) & 0x00ff0000) | ((val >> 8) & 0x0000ff00) | (val >> 24));
put_data( &val, sizeof(val) );
}
void put_qword( unsigned int val )
{
if (byte_swapped)
{
put_dword( 0 );
put_dword( val );
}
else
{
put_dword( val );
put_dword( 0 );
}
}
/* pointer-sized word */
void put_pword( unsigned int val )
{
......@@ -537,16 +472,6 @@ void put_pword( unsigned int val )
else put_dword( val );
}
void align_output( unsigned int align )
{
size_t size = align - (output_buffer_pos % align);
if (size == align) return;
check_output_buffer_space( size );
memset( output_buffer + output_buffer_pos, 0, size );
output_buffer_pos += size;
}
/* output a standard header for generated files */
void output_standard_file_header(void)
{
......
......@@ -433,70 +433,6 @@ WCHAR *codepage_to_unicode( int codepage, const char *src, int srclen, int *dstl
#endif /* _WIN32 */
/*******************************************************************
* buffer management
*
* Function for writing to a memory buffer.
*/
unsigned char *output_buffer;
size_t output_buffer_pos;
size_t output_buffer_size;
static void check_output_buffer_space( size_t size )
{
if (output_buffer_pos + size >= output_buffer_size)
{
output_buffer_size = max( output_buffer_size * 2, output_buffer_pos + size );
output_buffer = xrealloc( output_buffer, output_buffer_size );
}
}
void init_output_buffer(void)
{
output_buffer_size = 1024;
output_buffer_pos = 0;
output_buffer = xmalloc( output_buffer_size );
}
void flush_output_buffer( const char *name )
{
int fd = open( name, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666 );
if (fd == -1) error( "Error creating %s\n", name );
if (write( fd, output_buffer, output_buffer_pos ) != output_buffer_pos)
error( "Error writing to %s\n", name );
close( fd );
free( output_buffer );
}
void put_byte( unsigned char val )
{
check_output_buffer_space( 1 );
output_buffer[output_buffer_pos++] = val;
}
void put_word( unsigned short val )
{
check_output_buffer_space( 2 );
output_buffer[output_buffer_pos++] = val;
output_buffer[output_buffer_pos++] = val >> 8;
}
void put_dword( unsigned int val )
{
check_output_buffer_space( 4 );
output_buffer[output_buffer_pos++] = val;
output_buffer[output_buffer_pos++] = val >> 8;
output_buffer[output_buffer_pos++] = val >> 16;
output_buffer[output_buffer_pos++] = val >> 24;
}
void align_output( unsigned int align )
{
size_t size = align - (output_buffer_pos % align);
if (size == align) return;
check_output_buffer_space( size );
memset( output_buffer + output_buffer_pos, 0, size );
output_buffer_pos += size;
}
......@@ -41,17 +41,4 @@ char *unicode_to_utf8( const WCHAR *src, int srclen, int *dstlen );
int is_valid_codepage(int id);
WCHAR *codepage_to_unicode( int codepage, const char *src, int srclen, int *dstlen );
/* buffer management */
extern unsigned char *output_buffer;
extern size_t output_buffer_pos;
extern size_t output_buffer_size;
extern void init_output_buffer(void);
extern void flush_output_buffer( const char *name );
extern void put_byte( unsigned char val );
extern void put_word( unsigned short val );
extern void put_dword( unsigned int val );
extern void align_output( unsigned int align );
#endif
......@@ -42,64 +42,9 @@
#include "wingdi.h"
#include "winuser.h"
static unsigned char *output_buffer;
static size_t output_buffer_pos;
static size_t output_buffer_size;
static void check_output_buffer_space( size_t size )
{
if (output_buffer_pos + size >= output_buffer_size)
{
output_buffer_size = max( output_buffer_size * 2, output_buffer_pos + size );
output_buffer = xrealloc( output_buffer, output_buffer_size );
}
}
static void init_output_buffer(void)
{
output_buffer_size = 1024;
output_buffer_pos = 0;
output_buffer = xmalloc( output_buffer_size );
}
static void put_data( const void *data, size_t size )
{
check_output_buffer_space( size );
memcpy( output_buffer + output_buffer_pos, data, size );
output_buffer_pos += size;
}
static void put_byte( unsigned char val )
{
check_output_buffer_space( 1 );
output_buffer[output_buffer_pos++] = val;
}
static void put_word( unsigned short val )
{
check_output_buffer_space( 2 );
output_buffer[output_buffer_pos++] = val;
output_buffer[output_buffer_pos++] = val >> 8;
}
static void put_dword( unsigned int val )
{
check_output_buffer_space( 4 );
output_buffer[output_buffer_pos++] = val;
output_buffer[output_buffer_pos++] = val >> 8;
output_buffer[output_buffer_pos++] = val >> 16;
output_buffer[output_buffer_pos++] = val >> 24;
}
static void align_output( unsigned int align )
{
size_t size = align - (output_buffer_pos % align);
if (size == align) return;
check_output_buffer_space( size );
memset( output_buffer + output_buffer_pos, 0, size );
output_buffer_pos += size;
}
unsigned char *output_buffer = NULL;
size_t output_buffer_pos = 0;
size_t output_buffer_size = 0;
static void set_word(int ofs, unsigned int w)
{
......@@ -1202,13 +1147,6 @@ static void dlginit2res(name_id_t *name, dlginit_t *dit)
*/
void write_resfile(char *outname, resource_t *top)
{
FILE *fo;
unsigned int ret;
fo = fopen(outname, "wb");
if(!fo)
fatal_perror("Could not open %s", outname);
init_output_buffer();
if(win32)
......@@ -1256,12 +1194,5 @@ void write_resfile(char *outname, resource_t *top)
if (win32) align_output( 4 );
}
ret = fwrite(output_buffer, 1, output_buffer_pos, fo);
if(ret != output_buffer_pos)
{
fclose(fo);
error("Error writing %s\n", outname);
}
if (fclose(fo))
fatal_perror("Error writing %s", outname);
flush_output_buffer( outname );
}
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