1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
|
/*
* riff_dump.c - Command line utility to dump info about RIFF files
*
* riff_dump utility (licensed separately from libInstPatch)
* Copyright (C) 1999-2010 Joshua "Element" Green <jgreen@users.sourceforge.net>
*
* Public Domain, use as you please.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <libinstpatch/libinstpatch.h>
#define _GNU_SOURCE
#include <getopt.h>
void usage (void);
gboolean recurse_riff_chunks (IpatchRiff *riff, char *indent,
GError **err);
void display_chunk (IpatchRiff *riff, char *indent);
gboolean dump_chunk (IpatchRiff *riff, GError **err);
/* chunk index - so commands can be run on specific chunks */
int chunk_index = 0;
int dump_index = -1; /* set to chunk index if chunk dump requested */
char *dump_type = NULL; /* set to 4 char string if dumping a chunk type */
gboolean raw_dump = FALSE; /* set to TRUE for raw byte dumps */
gboolean display = TRUE; /* set to FALSE to not display chunks */
gboolean stop = FALSE; /* set to TRUE to stop recursion */
extern char *optarg;
extern int optind, opterr, optopt;
int
main (int argc, char *argv[])
{
IpatchRiff *riff;
IpatchRiffChunk *chunk;
IpatchFile *file;
IpatchFileHandle *fhandle;
char indent_buf[256] = "";
GError *err = NULL;
char *file_name = NULL;
int option_index = 0;
int c;
static struct option long_options[] =
{
{"dump", 1, 0, 'd'},
{"dump-type", 1, 0, 't'},
{"raw", 0, 0, 'r'},
{NULL, 0, NULL, 0}
};
while (TRUE)
{
c = getopt_long (argc, argv, "rd:t:", long_options, &option_index);
if (c == -1) break;
switch (c)
{
case 'd': /* dump chunk? */
dump_index = atoi (optarg); /* get chunk index */
display = FALSE; /* we enable display when we find chunk */
break;
case 't':
dump_type = g_strndup (optarg, 4);
display = FALSE;
break;
case 'r':
raw_dump = TRUE;
break;
case ':': /* missing option */
fprintf (stderr, "Missing parameter for option '-%c, %s'\n",
(char)(long_options[option_index].val),
long_options[option_index].name);
usage();
exit (1);
break;
case '?': /* unknown switch */
usage();
exit(1);
break;
default:
fprintf (stderr, "Unknown getopt return val '%d'\n", c);
exit (1);
break;
}
}
if (optind >= argc)
{
usage ();
exit (1);
}
file_name = argv[optind];
g_type_init ();
ipatch_init ();
file = ipatch_file_new ();
if (!(fhandle = ipatch_file_open (file, file_name, "r", &err)))
{
fprintf (stderr, "Failed to open file '%s': %s\n",
file_name, err ? err->message : "<no details>");
exit (1);
}
riff = ipatch_riff_new (fhandle);
if (!(chunk = ipatch_riff_start_read (riff, &err)))
{
fprintf (stderr, "Failed to start RIFF parse of file '%s': %s\n",
file_name, err ? err->message : "<no details>");
exit (1);
}
/* if a dump of chunk 0 requested or type matches, display everything */
if (dump_index == 0
|| (dump_type && strncmp (dump_type, chunk->idstr, 4) == 0))
display = TRUE;
if (display) display_chunk (riff, indent_buf);
chunk_index++;
strcat (indent_buf, " ");
if (!recurse_riff_chunks (riff, indent_buf, &err))
{
fprintf (stderr, "%s\n", ipatch_riff_message_detail
(riff, -1, "Error while parsing RIFF file '%s': %s",
file_name, err ? err->message : "<no details>"));
exit (1);
}
exit (0);
}
void
usage (void)
{
fprintf (stderr, "Usage: riff_dump [OPTION]... FILE\n");
fprintf (stderr, " -d, --dump=CHUNK_INDEX "
"Dump a chunk by index\n");
fprintf (stderr, " -t, --dump-type='CHNK' "
"Dump a chunk by RIFF FOURCC ID\n");
fprintf (stderr, " -r, --raw "
"Do raw dump rather than formatted hex dump\n\n");
fprintf (stderr, "CHUNK_INDEX - The chunk index (number in brackets [])\n");
}
gboolean
recurse_riff_chunks (IpatchRiff *riff, char *indent, GError **err)
{
IpatchRiffChunk *chunk;
gboolean retval;
while (!stop && (chunk = ipatch_riff_read_chunk (riff, err)))
{
if (dump_index == chunk_index) /* dump by chunk index match? */
{
if (chunk->type != IPATCH_RIFF_CHUNK_SUB) /* list chunk? */
{
display_chunk (riff, indent);
strcat (indent, " ");
display = TRUE;
retval = recurse_riff_chunks (riff, indent, err);
stop = TRUE;
return (retval);
}
else
{
retval = dump_chunk (riff, err); /* hex dump of sub chunk */
stop = TRUE;
return (retval);
}
} /* dump by type match? */
else if (dump_type && strncmp (dump_type, chunk->idstr, 4) == 0)
{
if (chunk->type != IPATCH_RIFF_CHUNK_SUB) /* list chunk? */
{
display = TRUE;
strcat (indent, " ");
recurse_riff_chunks (riff, indent, err);
indent[strlen (indent) - 2] = '\0';
display = FALSE;
}
else dump_chunk (riff, err); /* hex dump of sub chunk */
}
else /* no dump match, just do stuff */
{
if (display) display_chunk (riff, indent);
chunk_index++; /* advance chunk index */
if (chunk->type != IPATCH_RIFF_CHUNK_SUB) /* list chunk? */
{
strcat (indent, " ");
if (!recurse_riff_chunks (riff, indent, err)) return (FALSE);
indent[strlen (indent) - 2] = '\0';
}
}
if (!ipatch_riff_close_chunk (riff, -1, err)) return (FALSE);
}
return (ipatch_riff_get_error (riff, NULL));
}
void
display_chunk (IpatchRiff *riff, char *indent)
{
IpatchRiffChunk *chunk;
int filepos;
chunk = ipatch_riff_get_chunk (riff, -1);
filepos = ipatch_riff_get_position (riff);
if (chunk->type == IPATCH_RIFF_CHUNK_SUB)
printf ("%s(%.4s)[%4d] (ofs = 0x%x, size = %d)\n", indent,
chunk->idstr, chunk_index,
filepos - (chunk->position + IPATCH_RIFF_HEADER_SIZE),
chunk->size);
else /* list chunk */
printf ("%s<%.4s>[%4d] (ofs = 0x%x, size = %d)\n", indent,
chunk->idstr, chunk_index,
filepos - (chunk->position + IPATCH_RIFF_HEADER_SIZE),
chunk->size);
}
#define BUFFER_SIZE (16 * 1024)
/* hex dump of a sub chunk */
gboolean
dump_chunk (IpatchRiff *riff, GError **err)
{
IpatchRiffChunk *chunk;
guint8 buf[BUFFER_SIZE];
int filepos, read_size, bytes_left, i;
chunk = ipatch_riff_get_chunk (riff, -1);
filepos = ipatch_riff_get_position (riff);
if (!raw_dump)
{
printf ("Dump chunk: (%.4s)[%4d] (ofs = 0x%x, size = %d)",
chunk->idstr, chunk_index,
filepos - (chunk->position + IPATCH_RIFF_HEADER_SIZE),
chunk->size);
i = filepos & ~0xF; /* round down to nearest 16 byte offset */
while (i < filepos) /* advance to start point in 16 byte block */
{
if (!(i & 0xF)) printf ("\n%08u ", i); /* print file position */
else if (!(i & 0x3)) printf (" | "); /* print divider */
printf (" "); /* skip 1 byte character */
i++;
}
}
read_size = BUFFER_SIZE;
bytes_left = chunk->size;
while (bytes_left) /* loop until chunk exhausted */
{
if (bytes_left < BUFFER_SIZE) read_size = bytes_left;
if (!ipatch_file_read (riff->handle, &buf, read_size, err))
return (FALSE);
for (i = 0; i < read_size; i++, filepos++)
{
if (!raw_dump)
{
if (!(filepos & 0xF))
printf ("\n%08u ", filepos); /* print file position */
else if (!(filepos & 0x3)) printf (" | "); /* print divider */
}
printf ("%02X ", buf[i]);
}
bytes_left -= read_size;
}
printf ("\n");
return (TRUE);
}
|