-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.c
More file actions
176 lines (126 loc) · 4.12 KB
/
example.c
File metadata and controls
176 lines (126 loc) · 4.12 KB
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
// Example for usage of libriff
//
// We open a potential RIFF file, recursively traverse through all chunks and print the chunk header info with indentation.
//
#include <stdio.h>
#include <string.h>
#include "riff.h"
int nlist = 0; //number of sub lists (LIST chunks)
int nchunk = 0; //number of chunks
void test_traverse_rec(riff_handle *rh){
int err;
char indent[512*8]; //indentation
//identation for pretty output
strcpy(indent, "");
for(int i = 0; i < rh->ls_level; i++)
sprintf(indent, "%s ", indent);
if(rh->ls_level == 0) {
printf("CHUNK_ID: TOTAL_CHUNK_SIZE [CHUNK_DATA_FROM_TO_POS]\n");
//output RIFF file header
printf("%s%s: %d [%d..%d]\n", indent, rh->h_id, rh->h_size, rh->pos_start, rh->pos_start + rh->size);
printf(" %sType: %s\n", indent, rh->h_type);
}
else {
//output type of parent list chunk
struct riff_levelStackE *ls = rh->ls + rh->ls_level - 1;
//type ID of sub list is only read, after stepping into it
printf(" %sType: %s\n", indent, ls->c_type);
}
strcat(indent, " ");
int k = 0;
while(1){
printf("%s%s: %d [%d..%d]\n", indent, rh->c_id, rh->c_size, rh->c_pos_start, rh->c_pos_start + 8 + rh->c_size + rh->pad - 1);
//if current chunk not a chunk list
if(strcmp(rh->c_id, "LIST") != 0 && strcmp(rh->c_id, "RIFF") != 0){
}
else {
//getchar(); //uncomment to press ENTER to continue after a printed chunk
{
err = riff_seekLevelSub(rh);
if (err){
}
else
nlist++;
test_traverse_rec(rh); //recursive call
}
}
k++;
err = riff_seekNextChunk(rh);
if(err >= RIFF_ERROR_CRITICAL) {
printf("%s", riff_errorToString(err));
break;
}
else if (err < RIFF_ERROR_CRITICAL && err != RIFF_ERROR_NONE) {
//printf("last chunk in level %d %d .. %d %s\n", rh->ls_level, rh->c_pos_start, rh->c_pos_start + 8 + rh->c_size + rh->pad, rh->c_id);
//go back from sub level
riff_levelParent(rh);
//file pos has not changed by going a level back, we are now within that parent's data
break;
}
else
nchunk++;
}
}
void test(FILE *f){
//get size
fseek(f, 0, SEEK_END);
int fsize = ftell(f);
fseek(f, 0, SEEK_SET);
//allocate initialized handle struct
riff_handle *rh = riff_handleAllocate();
//after allocation rh->fp_fprintf == fprintf
//you can change the rh->fp_fprintf function pointer here for error output
//rh->fp_fprint = NULL; //set to NULL to disable any error printing
//open file, use build in input wrappers for file
//open RIFF file via file handle -> reads RIFF header and first chunk header
if(riff_open_file(rh, f, fsize) != RIFF_ERROR_NONE){
return;
}
nchunk++; //header can be seen as chunk
test_traverse_rec(rh);
printf("\nlist chunks: %d\nchunks: %d\n", nlist, nchunk);
int r;
//current list level
printf("\n");
printf("Current pos: %d\n", rh->pos);
printf("Current list level: %d\n", rh->ls_level);
//read a byte
printf("Reading a byte of chunk data ...\n");
char buf[1];
r = riff_readInChunk(rh, buf, 1);
printf("Bytes read: %d of %d\n", r, 1);
printf("Current pos: %d\n", rh->pos);
printf("Current list level: %d\n", rh->ls_level);
printf("seeking a byte forward in chunk data ...\n");
r = riff_seekInChunk(rh, rh->c_pos + 1);
if(r != RIFF_ERROR_NONE)
printf("Seek failed!\n");
printf("Current pos: %d\n", rh->pos);
printf("Offset in current chunk data: %d\n", rh->c_pos);
printf("Current list level: %d\n", rh->ls_level);
//rewind to first chunk's data pos 0
printf("Rewind to first chunk in file ...\n");
r = riff_rewind(rh);
if(r != RIFF_ERROR_NONE)
printf("Error: %s\n", riff_errorToString(r));
printf("Current pos: %d (expected: %d)\n", rh->pos, rh->pos_start + RIFF_HEADER_SIZE + RIFF_CHUNK_DATA_OFFSET);
printf("Current list level: %d\n", rh->ls_level);
riff_handleFree(rh);
//find and visit all LIST chunks
//load file to mem and do same again
}
int main(int argc, char *argv[] ){
if(argc < 2){
printf("Need path to input RIFF file!\n");
return -1;
}
printf("Opening file: %s\n", argv[1]);
FILE *f = fopen(argv[1],"rb");
if(f == NULL) {
printf("Failed to open file!\n");
return -1;
}
test(f);
fclose(f);
return 0;
}