/* aoi3.c - Don Yang (uguu.org) 08/24/02 */ #include #include #include static FILE *infile, *outfile; static int t, state, size, headersize, datasize, tag; static int ParseChunk(void); static int ParseList(int listsize) { int chunksize; fread(&tag, 4, 1, infile); state |= (tag == 0x6c726468) ? 1 : (tag == 0x69766f6d) ? 8 : 0; for(chunksize = 0; listsize > chunksize && (t = ParseChunk()); chunksize += t); 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) || ((state & 24) && (tag & 0xffff00ff) == 0x62770030) ) { if( tag & 1 ) { fwrite("RIFF....WAVEfmt ....", 20, 1, outfile); state |= 16; headersize = size; } else { if( !(state & 32) ) { fwrite("data....", 8, 1, outfile); state |= 32; } datasize += size; } if( (buffer = (char*)alloca(size)) ) { fread(buffer, size, 1, infile); fwrite(buffer, size, 1, outfile); } else { size = -8; } } else { fseek(infile, size, 1); } if( (size += 8) & 1 ) { fgetc(infile); size++; } return size; } 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 ) for(; !(state & 32) && ParseChunk();); } if( (state & 32) ) { 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); } else { puts("failed"); } fclose(infile); fclose(outfile); return 0; }