mirror of
https://github.com/simtactics/niotso.git
synced 2025-07-04 13:47:05 -04:00
Added rtti-reader and made various cleanups, switching to Shutdown_M for error handling
This commit is contained in:
parent
5444c9aea6
commit
6dddbd2efa
25 changed files with 771 additions and 194 deletions
|
@ -45,9 +45,9 @@ uint8_t * ReadFile(const char * Filename){
|
|||
}
|
||||
|
||||
size_t bytestransferred = fread(InData, 1, FileSize, hFile);
|
||||
fclose(hFile);
|
||||
if(bytestransferred != FileSize){
|
||||
free(InData);
|
||||
fclose(hFile);
|
||||
File::Error = FERR_READ;
|
||||
return NULL;
|
||||
}
|
||||
|
|
|
@ -68,9 +68,9 @@ struct Sound_t {
|
|||
|
||||
namespace File {
|
||||
|
||||
inline unsigned GetFileSize(FILE * hFile){
|
||||
inline size_t GetFileSize(FILE * hFile){
|
||||
fseek(hFile, 0, SEEK_END);
|
||||
unsigned FileSize = ftell(hFile);
|
||||
size_t FileSize = ftell(hFile);
|
||||
fseek(hFile, 0, SEEK_SET);
|
||||
return FileSize;
|
||||
}
|
||||
|
|
|
@ -127,7 +127,7 @@ static int fill_mem_input_buffer(j_decompress_ptr cinfo){
|
|||
}
|
||||
static void skip_input_data(j_decompress_ptr cinfo, long bytes)
|
||||
{
|
||||
struct jpeg_source_mgr * src = cinfo->src;
|
||||
jpeg_source_mgr * src = cinfo->src;
|
||||
|
||||
if(bytes > (long) src->bytes_in_buffer){
|
||||
ERREXIT(cinfo, JERR_FILE_READ);
|
||||
|
|
|
@ -34,7 +34,7 @@ enum {
|
|||
|
||||
int main(int argc, char *argv[]){
|
||||
int profile = 0, overwrite = 0;
|
||||
char infile[256] = "", outdirectory[256] = "";
|
||||
const char * InFile = "", * OutDirectory;
|
||||
FILE * hFile;
|
||||
size_t ArchiveSize;
|
||||
uint8_t * ArchiveData;
|
||||
|
@ -66,7 +66,7 @@ int main(int argc, char *argv[]){
|
|||
return 0;
|
||||
}
|
||||
|
||||
for(i=1; !infile[0] && i != argc-1; i++){
|
||||
for(i=1; !InFile[0] && i != argc-1; i++){
|
||||
/* Match for options */
|
||||
if(!profile){
|
||||
if(!strcmp(argv[i], "-ts1")){ profile = profile_ts1; continue; }
|
||||
|
@ -82,18 +82,18 @@ int main(int argc, char *argv[]){
|
|||
}
|
||||
/* Not an option */
|
||||
if(!strcmp(argv[i], "-")){
|
||||
printf("%sReading from standard input is not yet implemented.", "farextract: error: ");
|
||||
fprintf(stderr, "%sReading from standard input is not yet implemented.", "farextract: error: ");
|
||||
return -1;
|
||||
}
|
||||
strcpy(infile, argv[i]);
|
||||
InFile = argv[i];
|
||||
continue;
|
||||
}
|
||||
/* We're left with the out directory */
|
||||
if(!infile[0]){
|
||||
printf("%sReading from standard input is not yet implemented.", "farextract: error: ");
|
||||
if(!InFile[0]){
|
||||
fprintf(stderr, "%sReading from standard input is not yet implemented.", "farextract: error: ");
|
||||
return -1;
|
||||
}
|
||||
strcpy(outdirectory, argv[i]);
|
||||
OutDirectory = argv[i];
|
||||
|
||||
/****
|
||||
** Handle profile settings
|
||||
|
@ -107,26 +107,26 @@ int main(int argc, char *argv[]){
|
|||
** Open the file and read in the entire contents to memory
|
||||
*/
|
||||
|
||||
hFile = fopen(infile, "rb");
|
||||
hFile = fopen(InFile, "rb");
|
||||
if(hFile == NULL){
|
||||
printf("%sThe specified input file does not exist or could not be opened for reading.", "farextract: error: ");
|
||||
fprintf(stderr, "%sThe specified input file does not exist or could not be opened for reading.", "farextract: error: ");
|
||||
return -1;
|
||||
}
|
||||
fseek(hFile, 0, SEEK_END);
|
||||
ArchiveSize = ftell(hFile);
|
||||
if(ArchiveSize < 24){
|
||||
printf("%sNot a valid archive.", "farextract: error: ");
|
||||
fprintf(stderr, "%sNot a valid archive.", "farextract: error: ");
|
||||
return -1;
|
||||
}
|
||||
fseek(hFile, 0, SEEK_SET);
|
||||
|
||||
ArchiveData = malloc(ArchiveSize);
|
||||
if(ArchiveData == NULL){
|
||||
printf("%sMemory for this archive could not be allocated.", "farextract: error: ");
|
||||
fprintf(stderr, "%sMemory for this archive could not be allocated.", "farextract: error: ");
|
||||
return -1;
|
||||
}
|
||||
if(!fread(ArchiveData, ArchiveSize, 1, hFile)){
|
||||
printf("%sThe input file could not be read.", "farextract: error: ");
|
||||
if(fread(ArchiveData, 1, ArchiveSize, hFile) != ArchiveSize){
|
||||
fprintf(stderr, "%sThe input file could not be read.", "farextract: error: ");
|
||||
return -1;
|
||||
}
|
||||
fclose(hFile);
|
||||
|
@ -137,7 +137,7 @@ int main(int argc, char *argv[]){
|
|||
|
||||
ArchiveType = far_identify(ArchiveData, ArchiveSize);
|
||||
if(ArchiveType == FAR_TYPE_INVALID){
|
||||
printf("%sNot a valid archive.", "farextract: error: ");
|
||||
fprintf(stderr, "%sNot a valid archive.", "farextract: error: ");
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -151,11 +151,11 @@ int main(int argc, char *argv[]){
|
|||
|
||||
FARFile * FARFileInfo = far_create_archive(ArchiveType);
|
||||
if(FARFileInfo == NULL){
|
||||
printf("%sMemory for this archive could not be allocated.", "farextract: error: ");
|
||||
fprintf(stderr, "%sMemory for this archive could not be allocated.", "farextract: error: ");
|
||||
return -1;
|
||||
}
|
||||
if(!far_read_header(FARFileInfo, ArchiveData, ArchiveSize)){
|
||||
printf("%sNot a valid archive.", "farextract: error: ");
|
||||
fprintf(stderr, "%sNot a valid archive.", "farextract: error: ");
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -169,7 +169,7 @@ int main(int argc, char *argv[]){
|
|||
|
||||
if(!far_enumerate_entries(FARFileInfo, ArchiveData+FARFileInfo->IndexOffset,
|
||||
ArchiveSize-FARFileInfo->IndexOffset, ArchiveSize)){
|
||||
printf("%sEntry data is corrupt.", "farextract: error: ");
|
||||
fprintf(stderr, "%sEntry data is corrupt.", "farextract: error: ");
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -181,9 +181,9 @@ int main(int argc, char *argv[]){
|
|||
|
||||
file++;
|
||||
if(EntryNode->Entry.Filename)
|
||||
sprintf(destination, "%s/%s", outdirectory, EntryNode->Entry.Filename);
|
||||
sprintf(destination, "%s/%s", OutDirectory, EntryNode->Entry.Filename);
|
||||
else
|
||||
sprintf(destination, "%s/%08x-%08x-%08x.dat", outdirectory,
|
||||
sprintf(destination, "%s/%08x-%08x-%08x.dat", OutDirectory,
|
||||
EntryNode->Entry.TypeID, EntryNode->Entry.GroupID, EntryNode->Entry.FileID);
|
||||
|
||||
if(!far_read_entry_data(FARFileInfo, &(EntryNode->Entry), ArchiveData)){
|
||||
|
@ -233,7 +233,7 @@ int main(int argc, char *argv[]){
|
|||
/* Persist file */
|
||||
PersistFile * PersistInfo;
|
||||
char destination[256];
|
||||
sprintf(destination, "%s/%s.out", outdirectory, infile);
|
||||
sprintf(destination, "%s/%s.out", OutDirectory, InFile);
|
||||
|
||||
/****
|
||||
** Load header information
|
||||
|
@ -241,11 +241,11 @@ int main(int argc, char *argv[]){
|
|||
|
||||
PersistInfo = far_create_persist();
|
||||
if(PersistInfo == NULL){
|
||||
printf("%sMemory for this archive could not be allocated.", "farextract: error: ");
|
||||
fprintf(stderr, "%sMemory for this archive could not be allocated.", "farextract: error: ");
|
||||
return -1;
|
||||
}
|
||||
if(!far_read_persist_header(PersistInfo, ArchiveData, ArchiveSize)){
|
||||
printf("%sNot a valid archive.", "farextract: error: ");
|
||||
fprintf(stderr, "%sNot a valid archive.", "farextract: error: ");
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -255,7 +255,7 @@ int main(int argc, char *argv[]){
|
|||
printf("Extracting\n");
|
||||
BeginningTime = clock();
|
||||
if(!far_read_persist_data(PersistInfo, ArchiveData+18)){
|
||||
printf("%sNot a valid archive.", "farextract: error: ");
|
||||
fprintf(stderr, "%sNot a valid archive.", "farextract: error: ");
|
||||
return -1;
|
||||
}
|
||||
EndingTime = clock();
|
||||
|
@ -265,14 +265,14 @@ int main(int argc, char *argv[]){
|
|||
if(hFile != NULL){
|
||||
/* File exists */
|
||||
fclose(hFile);
|
||||
printf("%sFile exists.", "farextract: error: ");
|
||||
fprintf(stderr, "%sFile exists.", "farextract: error: ");
|
||||
libfar_free(PersistInfo->DecompressedData);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
hFile = fopen(destination, "wb");
|
||||
if(hFile == NULL){
|
||||
printf("%sCould not open.", "farextract: error: ");
|
||||
fprintf(stderr, "%sCould not open.", "farextract: error: ");
|
||||
libfar_free(PersistInfo->DecompressedData);
|
||||
return -1;
|
||||
}
|
||||
|
|
|
@ -23,7 +23,21 @@
|
|||
#include <time.h>
|
||||
#include "iff.h"
|
||||
|
||||
int charmatches(char c, const char * filter){
|
||||
static FILE * hFile = NULL;
|
||||
static uint8_t * IFFData = NULL;
|
||||
static IFFFile IFFFileInfo;
|
||||
static int iffcreated = 0;
|
||||
|
||||
static void Shutdown_M(const char * Message){
|
||||
fprintf(stderr, "iffexport: error: %s.", Message);
|
||||
if(iffcreated)
|
||||
iff_delete(&IFFFileInfo);
|
||||
free(IFFData);
|
||||
fclose(hFile);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
static int charmatches(char c, const char * filter){
|
||||
while(*filter){
|
||||
if(c == *filter) return 1;
|
||||
filter++;
|
||||
|
@ -32,18 +46,15 @@ int charmatches(char c, const char * filter){
|
|||
}
|
||||
|
||||
int main(int argc, char *argv[]){
|
||||
FILE * hFile;
|
||||
int overwrite = 0;
|
||||
char *InFile, *OutDirectory;
|
||||
const char *InFile, *OutDirectory;
|
||||
size_t FileSize;
|
||||
uint8_t * IFFData;
|
||||
clock_t BeginningTime;
|
||||
unsigned chunkcount, chunk;
|
||||
unsigned exported = 0;
|
||||
IFFFile IFFFileInfo;
|
||||
IFFChunk * ChunkData;
|
||||
|
||||
if(argc == 1 || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")){
|
||||
if(argc < 3 || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")){
|
||||
printf("Usage: iffexport [-f] infile outdirectory\n"
|
||||
"Export the resources of an EA IFF file.\n"
|
||||
"Use -f to force overwriting without confirmation.\n"
|
||||
|
@ -68,50 +79,39 @@ 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.", "iffexport: 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 IFF file.", "iffexport: error: ");
|
||||
return -1;
|
||||
}
|
||||
if(FileSize < 24)
|
||||
Shutdown_M("Not a valid IFF file");
|
||||
fseek(hFile, 0, SEEK_SET);
|
||||
|
||||
IFFData = malloc(FileSize);
|
||||
if(IFFData == NULL){
|
||||
printf("%sMemory for this file could not be allocated.", "iffexport: error: ");
|
||||
return -1;
|
||||
}
|
||||
if(!fread(IFFData, FileSize, 1, hFile)){
|
||||
printf("%sThe input file could not be read.", "iffexport: error: ");
|
||||
return -1;
|
||||
}
|
||||
fclose(hFile);
|
||||
if(IFFData == NULL)
|
||||
Shutdown_M("Memory for this file could not be allocated");
|
||||
if(fread(IFFData, 1, FileSize, hFile) != FileSize)
|
||||
Shutdown_M("The input file could not be read");
|
||||
fclose(hFile); hFile = NULL;
|
||||
|
||||
/****
|
||||
** Load header information
|
||||
*/
|
||||
|
||||
if(!iff_create(&IFFFileInfo)){
|
||||
printf("%sMemory for this file could not be allocated.", "iffexport: error: ");
|
||||
return -1;
|
||||
}
|
||||
if(!iff_read_header(&IFFFileInfo, IFFData, FileSize)){
|
||||
printf("%sNot a valid IFF file.", "iffexport: error: ");
|
||||
return -1;
|
||||
}
|
||||
if(!iff_create(&IFFFileInfo))
|
||||
Shutdown_M("Memory for this file could not be allocated");
|
||||
iffcreated++;
|
||||
if(!iff_read_header(&IFFFileInfo, IFFData, FileSize))
|
||||
Shutdown_M("Not a valid IFF file");
|
||||
|
||||
/****
|
||||
** Load entry information
|
||||
*/
|
||||
|
||||
if(!iff_enumerate_chunks(&IFFFileInfo, IFFData+64, FileSize-64)){
|
||||
printf("%sChunk data is corrupt.", "iffexport: error: ");
|
||||
return -1;
|
||||
}
|
||||
if(!iff_enumerate_chunks(&IFFFileInfo, IFFData+64, FileSize-64))
|
||||
Shutdown_M("Chunk data is corrupt");
|
||||
|
||||
free(IFFData); IFFData = NULL;
|
||||
|
||||
chunkcount = IFFFileInfo.ChunkCount;
|
||||
printf("This IFF file contains %u chunks.\n\nExporting\n", chunkcount);
|
||||
|
@ -144,21 +144,19 @@ 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) ", destination);
|
||||
c = getchar();
|
||||
if(c != 'y' && c != 'Y'){
|
||||
printf("\nAborted.");
|
||||
return -1;
|
||||
printf("\n");
|
||||
Shutdown_M("Aborted");
|
||||
}
|
||||
}
|
||||
overwrite++;
|
||||
}
|
||||
hFile = fopen(destination, "wb");
|
||||
if(hFile == NULL){
|
||||
printf("%sThe output file could not be opened for writing.", "iffexport: error: ");
|
||||
return -1;
|
||||
}
|
||||
if(hFile == NULL)
|
||||
Shutdown_M("The output file could not be opened for writing");
|
||||
fwrite(ChunkData->Data, 1, ChunkData->Size, hFile);
|
||||
fclose(hFile);
|
||||
|
||||
|
@ -168,5 +166,7 @@ int main(int argc, char *argv[]){
|
|||
printf("\nExported %u of %u chunks in %.2f seconds.", exported, chunkcount,
|
||||
((float) (clock() - BeginningTime))/CLOCKS_PER_SEC);
|
||||
|
||||
iff_delete(&IFFFileInfo);
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -26,9 +26,10 @@
|
|||
#define read_uint16(x) (unsigned)(((x)[0]<<(8*0)) | ((x)[1]<<(8*1)))
|
||||
#endif
|
||||
#ifndef write_int32
|
||||
#define write_uint16(dest, src) \
|
||||
#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
|
||||
|
||||
#ifndef round
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -22,22 +22,34 @@
|
|||
#include "read_xa.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 * XAData = NULL;
|
||||
static uint8_t * WaveData = NULL;
|
||||
|
||||
static void Shutdown_M(const char * Message){
|
||||
fprintf(stderr, "xadecode: error: %s.", Message);
|
||||
free(WaveData);
|
||||
free(XAData);
|
||||
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 * XAData;
|
||||
xaheader_t XAHeader;
|
||||
clock_t BeginningTime, EndingTime;
|
||||
|
||||
|
@ -67,53 +79,39 @@ 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.", "xadecode: 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 XA file.", "xadecode: error: ");
|
||||
return -1;
|
||||
}
|
||||
if(FileSize < 24)
|
||||
Shutdown_M("Not a valid XA file");
|
||||
fseek(hFile, 0, SEEK_SET);
|
||||
|
||||
XAData = malloc(FileSize);
|
||||
if(XAData == NULL){
|
||||
printf("%sMemory for this file could not be allocated.", "xadecode: error: ");
|
||||
return -1;
|
||||
}
|
||||
if(!fread(XAData, FileSize, 1, hFile)){
|
||||
printf("%sThe input file could not be read.", "xadecode: error: ");
|
||||
return -1;
|
||||
}
|
||||
fclose(hFile);
|
||||
if(XAData == NULL)
|
||||
Shutdown_M("Memory for this file could not be allocated");
|
||||
if(fread(XAData, 1, FileSize, hFile) != FileSize)
|
||||
Shutdown_M("The input file could not be read");
|
||||
fclose(hFile); hFile = NULL;
|
||||
|
||||
/****
|
||||
** Transcode the data from XA to LPCM
|
||||
*/
|
||||
|
||||
if(!xa_read_header(&XAHeader, XAData, FileSize)){
|
||||
printf("%sNot a valid XA file.", "xadecode: error: ");
|
||||
return -1;
|
||||
}
|
||||
if(!xa_read_header(&XAHeader, XAData, FileSize))
|
||||
Shutdown_M("Not a valid XA file");
|
||||
|
||||
if(argc >= 3){ /* Transcode */
|
||||
uint8_t * WaveData = malloc(44+XAHeader.dwOutSize);
|
||||
if(WaveData == NULL){
|
||||
printf("%sMemory for this file could not be allocated.", "xadecode: error: ");
|
||||
return -1;
|
||||
}
|
||||
WaveData = malloc(44+XAHeader.dwOutSize);
|
||||
if(WaveData == NULL)
|
||||
Shutdown_M("Memory for this file could not be allocated");
|
||||
|
||||
BeginningTime = clock();
|
||||
if(!xa_decode(XAData+24, WaveData+44, XAHeader.Frames, XAHeader.nChannels)){
|
||||
printf("%sMemory for this file could not be allocated.", "xadecode: error: ");
|
||||
return -1;
|
||||
}
|
||||
if(!xa_decode(XAData+24, WaveData+44, XAHeader.Frames, XAHeader.nChannels))
|
||||
Shutdown_M("Memory for this file could not be allocated");
|
||||
EndingTime = clock();
|
||||
|
||||
free(XAData);
|
||||
free(XAData); XAData = NULL;
|
||||
|
||||
/****
|
||||
** Write the Microsoft WAV header
|
||||
|
@ -142,23 +140,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.", "xadecode: 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) XAHeader.dwOutSize,
|
||||
((float)(EndingTime - BeginningTime))/CLOCKS_PER_SEC);
|
||||
fwrite(WaveData, 1, 44+XAHeader.dwOutSize, hFile);
|
||||
free(WaveData);
|
||||
fclose(hFile);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue