Added rtti-reader and made various cleanups, switching to Shutdown_M for error handling

This commit is contained in:
Fatbag 2012-12-03 12:31:24 -06:00
parent 5444c9aea6
commit 6dddbd2efa
25 changed files with 771 additions and 194 deletions

View file

@ -1,7 +1,5 @@
/*
FileHandler - General-purpose file handling library for Niotso
utkdecode.c - Copyright (c) 2011-2012 Niotso Project <http://niotso.org/>
Author(s): Fatbag <X-Fi6@phppoll.org>
utkdecode.c - Copyright (c) 2011-2012 Fatbag <X-Fi6@phppoll.org>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
@ -24,22 +22,34 @@
#include "read_utk.h"
#ifndef write_int32
#define write_uint32(dest, src) \
#define write_uint32(dest, src) do {\
(dest)[0] = ((src)&0x000000FF)>>(8*0); \
(dest)[1] = ((src)&0x0000FF00)>>(8*1); \
(dest)[2] = ((src)&0x00FF0000)>>(8*2); \
(dest)[3] = ((src)&0xFF000000)>>(8*3)
#define write_uint16(dest, src) \
(dest)[3] = ((src)&0xFF000000)>>(8*3); \
} while(0)
#define write_uint16(dest, src) do {\
(dest)[0] = ((src)&0x00FF)>>(8*0); \
(dest)[1] = ((src)&0xFF00)>>(8*1)
(dest)[1] = ((src)&0xFF00)>>(8*1); \
} while(0)
#endif
static FILE * hFile = NULL;
static uint8_t * UTKData = NULL;
static uint8_t * WaveData = NULL;
static void Shutdown_M(const char * Message){
fprintf(stderr, "utkdecode: error: %s.", Message);
free(WaveData);
free(UTKData);
fclose(hFile);
exit(EXIT_FAILURE);
}
int main(int argc, char *argv[]){
int overwrite = 0;
char *InFile, *OutFile;
FILE * hFile;
const char *InFile, *OutFile;
size_t FileSize;
uint8_t * UTKData;
utkheader_t UTKHeader;
clock_t BeginningTime, EndingTime;
@ -69,55 +79,41 @@ int main(int argc, char *argv[]){
*/
hFile = fopen(InFile, "rb");
if(hFile == NULL){
printf("%sThe specified input file does not exist or could not be opened for reading.", "utkdecode: error: ");
return -1;
}
if(hFile == NULL)
Shutdown_M("The specified input file does not exist or could not be opened for reading");
fseek(hFile, 0, SEEK_END);
FileSize = ftell(hFile);
if(FileSize < 24){
printf("%sNot a valid UTK file.", "utkdecode: error: ");
return -1;
}
if(FileSize < 32)
Shutdown_M("Not a valid UTK file");
fseek(hFile, 0, SEEK_SET);
UTKData = malloc(FileSize);
if(UTKData == NULL){
printf("%sMemory for this file could not be allocated.", "utkdecode: error: ");
return -1;
}
if(!fread(UTKData, FileSize, 1, hFile)){
printf("%sThe input file could not be read.", "utkdecode: error: ");
return -1;
}
fclose(hFile);
if(UTKData == NULL)
Shutdown_M("Memory for this file could not be allocated");
if(fread(UTKData, 1, FileSize, hFile) != FileSize)
Shutdown_M("The input file could not be read");
fclose(hFile); hFile = NULL;
/****
** Transcode the data from UTK to LPCM
*/
if(!utk_read_header(&UTKHeader, UTKData, FileSize)){
printf("%sNot a valid UTK file.", "utkdecode: error: ");
return -1;
}
if(!utk_read_header(&UTKHeader, UTKData, FileSize))
Shutdown_M("Not a valid UTK file");
if(argc >= 3){ /* Transcode */
uint8_t * WaveData = malloc(44+UTKHeader.dwOutSize);
if(WaveData == NULL){
printf("%sMemory for this file could not be allocated.", "utkdecode: error: ");
return -1;
}
WaveData = malloc(44+UTKHeader.dwOutSize);
if(WaveData == NULL)
Shutdown_M("Memory for this file could not be allocated");
UTKGenerateTables();
BeginningTime = clock();
if(!utk_decode(UTKData+32, WaveData+44, UTKHeader.Frames)){
printf("%sMemory for this file could not be allocated.", "utkdecode: error: ");
return -1;
}
if(!utk_decode(UTKData+32, WaveData+44, UTKHeader.Frames))
Shutdown_M("Memory for this file could not be allocated");
EndingTime = clock();
free(UTKData);
free(UTKData); UTKData = NULL;
/****
** Write the Microsoft WAV header
@ -146,23 +142,22 @@ int main(int argc, char *argv[]){
if(hFile != NULL){
/* File exists */
char c;
fclose(hFile);
fclose(hFile); hFile = NULL;
printf("File \"%s\" exists.\nContinue anyway? (y/n) ", OutFile);
c = getchar();
if(c != 'y' && c != 'Y'){
printf("\nAborted.\n");
return -1;
printf("\n");
Shutdown_M("Aborted");
}
}
}
hFile = fopen(OutFile, "wb");
if(hFile == NULL){
printf("%sThe output file could not be opened for writing.", "utkdecode: error: ");
return -1;
}
if(hFile == NULL)
Shutdown_M("The output file could not be opened for writing");
printf("Extracted %u bytes in %.2f seconds.\n", (unsigned) UTKHeader.dwOutSize,
((float)(EndingTime - BeginningTime))/CLOCKS_PER_SEC);
fwrite(WaveData, 1, 44+UTKHeader.dwOutSize, hFile);
free(WaveData);
fclose(hFile);
}