2 * $Id: util.c,v 1.8 2007-10-07 06:03:22 nkeynes Exp $
4 * Miscellaneous utility functions.
6 * Copyright (c) 2005 Nathan Keynes.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
22 void fwrite_string( const char *s, FILE *f )
26 fwrite( &len, sizeof(len), 1, f );
29 fwrite( &len, sizeof(len), 1, f );
30 fwrite( s, len, 1, f );
34 int fread_string( char *s, int maxlen, FILE *f )
37 fread( &len, sizeof(len), 1, f );
39 fread( s, len > maxlen ? maxlen : len, 1, f );
44 void fwrite_dump( unsigned char *data, unsigned int length, FILE *f )
47 for( i =0; i<length; i+=16 ) {
48 fprintf( f, "%08X:", i);
49 for( j=i; j<i+16; j++ ) {
53 fprintf( f, " %02X", (unsigned int)(data[j]) );
58 for( j=i; j<i+16 && j<length; j++ ) {
59 fprintf( f, "%c", isprint(data[j]) ? data[j] : '.' );
65 void fwrite_dump32( unsigned int *data, unsigned int length, FILE *f )
67 fwrite_dump32v( data, length, 8, f );
70 void fwrite_dump32v( unsigned int *data, unsigned int length, int wordsPerLine, FILE *f )
73 for( i =0; i<length>>2; i+=wordsPerLine ) {
74 fprintf( f, "%08X:", i);
75 for( j=i; j<i+wordsPerLine; j++ ) {
77 fprintf( f, " %08X", (unsigned int)(data[j]) );
.