Skip to content

Instantly share code, notes, and snippets.

@harieamjari
Last active May 31, 2021 07:46
Show Gist options
  • Save harieamjari/75fb101d6689e7a59d1715ef6ccdc70d to your computer and use it in GitHub Desktop.
Save harieamjari/75fb101d6689e7a59d1715ef6ccdc70d to your computer and use it in GitHub Desktop.
/* This is free and unencumbered software released into the public domain.
*
* Anyone is free to copy, modify, publish, use, compile, sell, or
* distribute this software, either in source code form or as a compiled
* binary, for any purpose, commercial or non-commercial, and by any
* means.
* In jurisdictions that recognize copyright laws, the author or authors
* of this software dedicate any and all copyright interest in the
* software to the public domain. We make this dedication for the benefit
* of the public at large and to the detriment of our heirs and
* suvcessors. We intend this dedication to be an overt act of
* relinquishment in perpetuity of all present and future rights to this
* software under copyright law.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
int main(int argc, char *argv[]) {
if (argc == 2) {
printf("read riff file\n\n");
printf("Usage: %s chunk ... FILE\n", argv[0]);
printf("example: %s \"fmt \" \"data\" t.wav\n", argv[argc - 1]);
return 1;
}
int offset = 0;
FILE *fp = fopen(argv[argc - 1], "rb");
if (fp == NULL) {
perror(argv[argc - 1]);
return 1;
}
char RIFFTag[4];
fread(RIFFTag, 1, 4, fp);
if (strncmp(RIFFTag, "RIFF", 4)) {
fprintf(stderr, "%s: Not a RIFF file", argv[argc - 1]);
return 1;
}
uint32_t RIFFsize = 0;
fread(&RIFFsize, sizeof(uint32_t), 1, fp);
printf("OFFSET(hex) CHUNK SIZE\n\n");
printf("%-d RIFF %" PRIu32 "\n", offset, RIFFsize);
offset += 8;
char RIFFid[5] = {0};
if (fread(RIFFid, 1, 4, fp) != 4) {
fprintf(stderr, "%s: corrupted file\n", argv[argc - 1]);
return 1;
}
printf("%-d +--%s\n", offset, RIFFid);
offset += 4;
int chunk_ctr = 1;
int chunk_found = 0;
int subchunk_ctr = 1;
int subchunk_found = 0;
for (int i = 1; i < argc - 1; i++) {
char chunk[4];
char *tok = argv[i];
while (1) {
if (fread(chunk, 1, 4, fp) == 4) {
if (!strncmp(chunk, tok, 4)) {
printf("%-10x %-4s #%-3d ", offset, tok, chunk_ctr);
fflush(stdout);
uint32_t chunksize = 0;
fread(&chunksize, sizeof(uint32_t), 1, fp);
offset += 4;
printf("%" PRIu32 "\n", chunksize);
chunk_ctr++;
chunk_found = 1;
}
offset += 4;
fseek(fp, -3, SEEK_CUR);
offset -= 3;
} else {
if (!chunk_found) {
printf(" %-9s ", tok);
fflush(stdout);
printf("not found\n");
}
break;
}
}
fseek(fp, 0, SEEK_SET);
offset = 0;
chunk_ctr = 1;
chunk_found = 0;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment