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 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
|
/*
* riff_dump.c - Command line utility to dump info about RIFF files
*
* riff_dump utility (licensed separately from libInstPatch)
* Copyright (C) 1999-2014 Element Green <element@elementsofsound.org>
* Public Domain, use as you please.
*
* Usage:
* riff_dump [OPTION] file.sf2
*
* Help options:
* -h, --help Display help options
*
* Application options :
* -d, --dump=CHUNK_INDEX Dump a chunk by CHUNK_INDEX index
* -t, --dump-type=CHNK Dump a chunk by RIFF FOURCC CHNK
* -r, --raw Do raw dump rather than formatted hex dump
*
*/
#include <string.h>
#include <libinstpatch/libinstpatch.h>
static gboolean recurse_riff_chunks(IpatchRiff *riff, char *indent,
GError **err);
static void display_chunk(IpatchRiff *riff, char *indent);
static gboolean dump_chunk(IpatchRiff *riff, GError **err);
/* options arguments (global for convenience)*/
static gint dump_index = -1; /* set to chunk index if chunk dump requested */
static gchar *dump_type = NULL; /* set to 4 char string if dumping a chunk type */
static gboolean raw_dump = FALSE; /* set to TRUE for raw byte dumps */
/* dislaying variables (global for convenience)*/
static int chunk_index = 0; /* current index */
static gboolean display = TRUE; /* set to FALSE to not display chunks */
static gboolean stop = FALSE; /* set to TRUE to stop recursion */
int
main(int argc, char *argv[])
{
/* riff file variables */
char *file_name = NULL; /* file name */
IpatchFile *riff_file; /* riff file to open */
IpatchFileHandle *fhandle = NULL; /* riff file file handle */
IpatchRiff *riff;
IpatchRiffChunk *chunk;
char indent_buf[256] = ""; /* indentation buffer */
GError *err = NULL;
static gchar **file_arg = NULL; /* file name argument */
/* parsing context variables */
GOptionContext *context = NULL; /* parsing context */
/* option entries to parse */
static GOptionEntry entries[] =
{
{ "dump", 'd', 0, G_OPTION_ARG_INT, &dump_index, "Dump a chunk by CHUNK_INDEX index", "CHUNK_INDEX" },
{ "dump-type", 't', 0, G_OPTION_ARG_STRING, &dump_type, "Dump a chunk by RIFF FOURCC CHNK", "CHNK" },
{ "raw", 'r', 0, G_OPTION_ARG_NONE, &raw_dump, "Do raw dump rather than formatted hex dump", NULL },
{ G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_FILENAME_ARRAY, &file_arg, NULL, "file.sf2" },
{ NULL }
};
/* parse option in command line */
context = g_option_context_new(NULL);
g_option_context_add_main_entries(context, entries, NULL);
if(!g_option_context_parse(context, &argc, &argv, &err))
{
g_print("option parsing failed: %s\n", err->message);
g_error_free(err);
g_strfreev(file_arg);
g_free(dump_type);
return (1);
}
/* prepare variable for displaying */
if(dump_index >= 0)
{
display = FALSE; /* we enable display when we find chunk */
}
else if(dump_type)
{
display = FALSE;
}
/* take the file name argument */
if(file_arg)
{
file_name = *file_arg;
}
/* libinstpatch initialization */
ipatch_init();
riff_file = ipatch_file_new();
if(file_name)
{
fhandle = ipatch_file_open(riff_file, file_name, "r", &err);
}
if(!(fhandle))
{
fprintf(stderr, "Failed to open file '%s': %s\n",
file_name, err ? err->message : "<no details>");
g_free(dump_type);
g_strfreev(file_arg);
return (1);
}
riff = ipatch_riff_new(fhandle);
/* start reading chunk */
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>");
g_free(dump_type);
g_strfreev(file_arg);
return (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, " ");
/* continue to read by recursion */
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>"));
g_free(dump_type);
g_strfreev(file_arg);
return (1);
}
g_free(dump_type); /* free dump type */
dump_type = NULL; /* not needed but a good practice */
g_strfreev(file_arg); /* free file arguments array */
file_arg = NULL; /* not needed but a good practice */
return (0);
}
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);
}
|