/* aoi2.c - Don Yang (uguu.org) 08/20/02 */ #include #include #include static FILE *infile, *outfile; static int r, state, size, headersize, datasize, tag; static int ParseChunk(void); static int ParseList(int listsize) { int chunksize; fread(&tag, 4, 1, infile); if( tag == 0x6c726468 ) { state |= 1; } else if( tag == 0x69766f6d ) { state |= 8; } chunksize = 0; while( listsize > chunksize ) { chunksize += (r = ParseChunk()); if( r == 0 ) return 0; } return listsize; } static int ParseChunk(void) { char *buffer; fread(&tag, 4, 1, infile); fread(&size, 4, 1, infile); if( tag == 0x5453494c ) return ParseList(size); if( (state & 1) && tag == 0x68697661 ) { state |= 2; fseek(infile, size, 1); } else if( (state & 2) && tag == 0x68727473 ) { fread(&tag, 4, 1, infile); if( tag == 0x73647561 ) state |= 4; fseek(infile, size - 4, 1); } else if( (state & 4) && tag == 0x66727473 ) { fwrite("RIFF\0\0\0\0WAVEfmt \0\0\0\0", 20, 1, outfile); if( (buffer = (char*)alloca(size)) == NULL ) return 0; fread(buffer, size, 1, infile); fwrite(buffer, size, 1, outfile); headersize = size; state |= 16; } else if( (state & 24) && (tag & 0xffff00ff) == 0x62770030 ) { if( (state & 32) == 0 ) { fwrite("data\0\0\0\0", 8, 1, outfile); state |= 32; } if( (buffer = (char*)alloca(size)) == NULL ) return 0; fread(buffer, size, 1, infile); fwrite(buffer, size, 1, outfile); datasize += size; } else { fseek(infile, size, 1); } if( size & 1 ) { fgetc(infile); size++; } return size + 8; } int main(int argc, char **argv) { if( argc < 3 ) return printf("%s \n", *argv); if( (infile = fopen(argv[1], "rb")) == NULL ) { printf("Can not open %s\n", argv[1]); return 1; } if( (outfile = fopen(argv[2], "wb+")) == NULL ) { fclose(infile); printf("Can not create %s\n", argv[2]); return 1; } headersize = datasize = state = 0; fread(&tag, 4, 1, infile); fread(&size, 4, 1, infile); if( tag == 0x46464952 ) { fread(&tag, 4, 1, infile); if( tag == 0x20495641 ) { do { if( !ParseChunk() ) break; } while( (state & 32) == 0 ); } } if( (state & 32) == 0 ) { puts("failed"); } else { fseek(outfile, 16, 0); fwrite(&headersize, 4, 1, outfile); fseek(outfile, headersize + 4, 1); fwrite(&datasize, 4, 1, outfile); datasize += headersize + 20; fseek(outfile, 4, 0); fwrite(&datasize, 4, 1, outfile); } fclose(infile); fclose(outfile); return 0; }