2009-06-13 20:52:43 +00:00
|
|
|
#include "qcc.h"
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2018-09-29 03:40:50 +00:00
|
|
|
#if defined(__linux__) || defined(__unix__)
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2009-06-13 20:52:43 +00:00
|
|
|
/*
|
|
|
|
==============
|
|
|
|
LoadFile
|
|
|
|
==============
|
|
|
|
*/
|
2018-05-28 21:09:15 +00:00
|
|
|
static void *QCC_ReadFile(const char *fname, unsigned char *(*buf_get)(void *ctx, size_t len), void *buf_ctx, size_t *out_size, pbool issourcefile)
|
2018-01-23 01:49:49 +00:00
|
|
|
//unsigned char *PDECL QCC_ReadFile (const char *fname, void *buffer, int len, size_t *sz)
|
2009-06-13 20:52:43 +00:00
|
|
|
{
|
2018-01-23 01:49:49 +00:00
|
|
|
size_t len;
|
2009-06-13 20:52:43 +00:00
|
|
|
FILE *f;
|
2018-01-23 01:49:49 +00:00
|
|
|
char *buffer;
|
|
|
|
|
2009-06-13 20:52:43 +00:00
|
|
|
f = fopen(fname, "rb");
|
|
|
|
if (!f)
|
2018-01-23 01:49:49 +00:00
|
|
|
{
|
|
|
|
if (out_size)
|
|
|
|
*out_size = 0;
|
2009-06-13 20:52:43 +00:00
|
|
|
return NULL;
|
2018-01-23 01:49:49 +00:00
|
|
|
}
|
2009-06-13 20:52:43 +00:00
|
|
|
|
2018-01-23 01:49:49 +00:00
|
|
|
fseek(f, 0, SEEK_END);
|
|
|
|
len = ftell(f);
|
|
|
|
fseek(f, 0, SEEK_SET);
|
|
|
|
if (buf_get)
|
|
|
|
buffer = buf_get(buf_ctx, len+1);
|
|
|
|
else
|
|
|
|
buffer = malloc(len+1);
|
|
|
|
((char*)buffer)[len] = 0;
|
|
|
|
if (len != fread(buffer, 1, len, f))
|
|
|
|
{
|
|
|
|
if (!buf_get)
|
|
|
|
free(buffer);
|
|
|
|
buffer = NULL;
|
|
|
|
}
|
|
|
|
fclose(f);
|
2009-06-13 20:52:43 +00:00
|
|
|
|
2018-01-23 01:49:49 +00:00
|
|
|
if (out_size)
|
|
|
|
*out_size = len;
|
2009-06-13 20:52:43 +00:00
|
|
|
return buffer;
|
|
|
|
}
|
2018-05-28 21:09:15 +00:00
|
|
|
static int PDECL QCC_FileSize (const char *fname)
|
2009-06-13 20:52:43 +00:00
|
|
|
{
|
|
|
|
long length;
|
|
|
|
FILE *f;
|
|
|
|
f = fopen(fname, "rb");
|
|
|
|
if (!f)
|
|
|
|
return -1;
|
|
|
|
fseek(f, 0, SEEK_END);
|
|
|
|
length = ftell(f);
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2018-05-28 21:09:15 +00:00
|
|
|
static pbool PDECL QCC_WriteFile (const char *name, void *data, int len)
|
2009-06-13 20:52:43 +00:00
|
|
|
{
|
|
|
|
long length;
|
|
|
|
FILE *f;
|
|
|
|
f = fopen(name, "wb");
|
|
|
|
if (!f)
|
|
|
|
return false;
|
|
|
|
length = fwrite(data, 1, len, f);
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
if (length != len)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef printf
|
|
|
|
#undef Sys_Error
|
|
|
|
|
2018-05-28 21:09:15 +00:00
|
|
|
static void PDECL Sys_Error(const char *text, ...)
|
2009-06-13 20:52:43 +00:00
|
|
|
{
|
|
|
|
va_list argptr;
|
|
|
|
static char msg[2048];
|
|
|
|
|
|
|
|
va_start (argptr,text);
|
|
|
|
QC_vsnprintf (msg,sizeof(msg)-1, text,argptr);
|
|
|
|
va_end (argptr);
|
|
|
|
|
|
|
|
QCC_Error(ERR_INTERNAL, "%s", msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-28 21:09:15 +00:00
|
|
|
static FILE *logfile;
|
|
|
|
static int logprintf(const char *format, ...)
|
2009-06-13 20:52:43 +00:00
|
|
|
{
|
|
|
|
va_list argptr;
|
|
|
|
static char string[1024];
|
|
|
|
|
|
|
|
va_start (argptr, format);
|
|
|
|
#ifdef _WIN32
|
|
|
|
_vsnprintf (string,sizeof(string)-1, format,argptr);
|
|
|
|
#else
|
|
|
|
vsnprintf (string,sizeof(string), format,argptr);
|
|
|
|
#endif
|
|
|
|
va_end (argptr);
|
|
|
|
|
|
|
|
printf("%s", string);
|
2015-04-14 12:24:05 +00:00
|
|
|
// fputs(string, stderr);
|
2009-06-13 20:52:43 +00:00
|
|
|
if (logfile)
|
|
|
|
fputs(string, logfile);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-10-26 06:30:35 +00:00
|
|
|
static size_t totalsize, filecount;
|
|
|
|
static void QCC_FileList(const char *name, const void *compdata, size_t compsize, int method, size_t plainsize)
|
|
|
|
{
|
|
|
|
totalsize += plainsize;
|
|
|
|
filecount += 1;
|
|
|
|
if (!method && compsize==plainsize)
|
|
|
|
externs->Printf("%8u %s\n", (unsigned)plainsize, name);
|
|
|
|
else
|
|
|
|
externs->Printf("%8u %3u%% %s\n", (unsigned)plainsize, plainsize?(unsigned)((100*compsize)/plainsize):100u, name);
|
|
|
|
}
|
|
|
|
#include <limits.h>
|
|
|
|
#ifdef __unix__
|
|
|
|
#include <sys/stat.h>
|
|
|
|
void QCC_Mkdir(const char *path)
|
|
|
|
{
|
|
|
|
char buf[MAX_OSPATH], *sl;
|
|
|
|
if (!strchr(path, '/'))
|
|
|
|
return; //no need to create anything
|
|
|
|
memcpy(buf, path, MAX_OSPATH);
|
|
|
|
while((sl=strrchr(buf, '/')))
|
|
|
|
{
|
|
|
|
*sl = 0;
|
|
|
|
mkdir(buf, 0777);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
void QCC_Mkdir(const char *path)
|
|
|
|
{
|
|
|
|
//unsupported.
|
|
|
|
}
|
|
|
|
#endif
|
2021-09-03 08:02:15 +00:00
|
|
|
|
|
|
|
static char qcc_tolower(char c)
|
|
|
|
{
|
|
|
|
if (c >= 'A' && c <= 'Z')
|
|
|
|
return c-'A'+'a';
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
int qcc_wildcmp(const char *wild, const char *string)
|
|
|
|
{
|
|
|
|
while (*string)
|
|
|
|
{
|
|
|
|
if (*wild == '*')
|
|
|
|
{
|
|
|
|
if (*string == '/' || *string == '\\')
|
|
|
|
{
|
|
|
|
//* terminates if we get a match on the char following it, or if its a \ or / char
|
|
|
|
wild++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (qcc_wildcmp(wild+1, string))
|
|
|
|
return true;
|
|
|
|
string++;
|
|
|
|
}
|
|
|
|
else if ((qcc_tolower(*wild) == qcc_tolower(*string)) || (*wild == '?'))
|
|
|
|
{
|
|
|
|
//this char matches
|
|
|
|
wild++;
|
|
|
|
string++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//failure
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (*wild == '*')
|
|
|
|
{
|
|
|
|
wild++;
|
|
|
|
}
|
|
|
|
return !*wild;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-10-26 06:30:35 +00:00
|
|
|
static const char *extractonly;
|
|
|
|
static pbool extractonlyfound;
|
|
|
|
static void QCC_FileExtract(const char *name, const void *compdata, size_t compsize, int method, size_t plainsize)
|
|
|
|
{
|
|
|
|
if (extractonly)
|
|
|
|
{
|
|
|
|
const char *sl = strrchr(extractonly, '/');
|
|
|
|
if (sl && !sl[1])
|
|
|
|
{ //trailing / - extract the entire dir.
|
|
|
|
if (!strcmp(name, extractonly))
|
|
|
|
return; //ignore the dir itself...
|
|
|
|
if (strncmp(name, extractonly, strlen(extractonly)))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
2021-09-03 08:02:15 +00:00
|
|
|
if (!qcc_wildcmp(extractonly, name))
|
2020-10-26 06:30:35 +00:00
|
|
|
return; //ignore it if its not the one we're going for.
|
|
|
|
}
|
|
|
|
extractonlyfound = true;
|
|
|
|
externs->Printf("Extracting %s...", name);
|
|
|
|
if (plainsize <= INT_MAX)
|
|
|
|
{
|
|
|
|
void *buffer = malloc(plainsize);
|
|
|
|
if (buffer && QC_decode(progfuncs, compsize, plainsize, method, compdata, buffer))
|
|
|
|
{
|
|
|
|
QCC_Mkdir(name);
|
|
|
|
if (!QCC_WriteFile(name, buffer, plainsize))
|
|
|
|
externs->Printf(" write failure\n");
|
|
|
|
else
|
|
|
|
externs->Printf(" done\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
externs->Printf(" read failure\n");
|
|
|
|
|
|
|
|
free(buffer);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
externs->Printf(" too large\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void QCC_PR_PackagerMessage(void *userctx, const char *message, ...)
|
|
|
|
{
|
|
|
|
va_list argptr;
|
|
|
|
char string[1024];
|
|
|
|
|
|
|
|
va_start (argptr,message);
|
|
|
|
QC_vsnprintf (string,sizeof(string)-1,message,argptr);
|
|
|
|
va_end (argptr);
|
|
|
|
|
|
|
|
externs->Printf ("%s", string);
|
|
|
|
}
|
|
|
|
|
2019-01-20 01:00:18 +00:00
|
|
|
int main (int argc, const char **argv)
|
2009-06-13 20:52:43 +00:00
|
|
|
{
|
2015-04-14 12:24:05 +00:00
|
|
|
unsigned int i;
|
2014-08-03 14:47:47 +00:00
|
|
|
pbool sucess;
|
2020-05-30 12:12:46 +00:00
|
|
|
#if 0//def _WIN32
|
2020-05-29 10:27:43 +00:00
|
|
|
pbool writelog = true; //spew log files on windows. windows often closes the window as soon as the program ends making its output otherwise unreadable.
|
|
|
|
#else
|
|
|
|
pbool writelog = false; //other systems are sane.
|
|
|
|
#endif
|
2020-07-01 05:32:21 +00:00
|
|
|
int colours = 2; //auto
|
2009-06-13 20:52:43 +00:00
|
|
|
progexterns_t ext;
|
|
|
|
progfuncs_t funcs;
|
|
|
|
progfuncs = &funcs;
|
|
|
|
memset(&funcs, 0, sizeof(funcs));
|
2013-03-12 22:32:25 +00:00
|
|
|
funcs.funcs.parms = &ext;
|
2009-06-13 20:52:43 +00:00
|
|
|
memset(&ext, 0, sizeof(progexterns_t));
|
2013-03-12 22:32:25 +00:00
|
|
|
funcs.funcs.parms->ReadFile = QCC_ReadFile;
|
|
|
|
funcs.funcs.parms->FileSize = QCC_FileSize;
|
|
|
|
funcs.funcs.parms->WriteFile = QCC_WriteFile;
|
|
|
|
funcs.funcs.parms->Printf = logprintf;
|
|
|
|
funcs.funcs.parms->Sys_Error = Sys_Error;
|
2018-09-29 03:40:50 +00:00
|
|
|
|
2020-10-26 06:30:35 +00:00
|
|
|
if ((argc == 3 && !strcmp(argv[1], "-l")) || (argc >= 3 && !strcmp(argv[1], "-x")))
|
|
|
|
{
|
|
|
|
size_t blobsize;
|
|
|
|
void *blob = QCC_ReadFile(argv[2], NULL, NULL, &blobsize, false);
|
|
|
|
if (!blob)
|
|
|
|
{
|
|
|
|
logprintf("Unable to read %s\n", argv[2]);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
if (argc > 3)
|
|
|
|
{
|
|
|
|
for (i = 3; i < argc; i++)
|
|
|
|
{
|
|
|
|
extractonly = argv[i];
|
|
|
|
extractonlyfound = false;
|
|
|
|
QC_EnumerateFilesFromBlob(blob, blobsize, QCC_FileExtract);
|
|
|
|
if (!extractonlyfound)
|
|
|
|
externs->Printf("Unable to find file %s\n", extractonly);
|
|
|
|
}
|
|
|
|
extractonly = NULL;
|
|
|
|
}
|
|
|
|
else if (argv[1][1] == 'x')
|
|
|
|
QC_EnumerateFilesFromBlob(blob, blobsize, QCC_FileExtract);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QC_EnumerateFilesFromBlob(blob, blobsize, QCC_FileList);
|
|
|
|
externs->Printf("Total size %u bytes, %u files\n", (unsigned)totalsize, (unsigned)filecount);
|
|
|
|
}
|
|
|
|
free(blob);
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
if (argc == 3 && (!strncmp(argv[1], "-z", 2) || !strcmp(argv[1], "-0") || !strcmp(argv[1], "-9")))
|
|
|
|
{ //exe -0 foo.pk3dir
|
|
|
|
enum pkgtype_e t;
|
|
|
|
if (argv[1][1] == '9')
|
|
|
|
t = PACKAGER_PK3;
|
|
|
|
else if (argv[1][1] == '0')
|
|
|
|
t = PACKAGER_PAK; //not really any difference but oh well
|
|
|
|
else
|
|
|
|
t = PACKAGER_PK3_SPANNED;
|
|
|
|
|
|
|
|
if (Packager_CompressDir(argv[2], t, QCC_PR_PackagerMessage, NULL))
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
else
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2020-07-01 05:32:21 +00:00
|
|
|
for (i = 0; i < argc; i++)
|
|
|
|
{
|
|
|
|
if (!argv[i])
|
|
|
|
continue;
|
|
|
|
if (!strcmp(argv[i], "-log"))
|
|
|
|
writelog = true;
|
|
|
|
else if (!strcmp(argv[i], "-nolog"))
|
|
|
|
writelog = false;
|
|
|
|
|
|
|
|
//arg consistency with ls
|
|
|
|
else if (!strcmp(argv[i], "--color=always") || !strcmp(argv[i], "--color"))
|
|
|
|
colours = 1;
|
|
|
|
else if (!strcmp(argv[i], "--color=never"))
|
|
|
|
colours = 0;
|
|
|
|
else if (!strcmp(argv[i], "--color=auto"))
|
|
|
|
colours = 2;
|
|
|
|
}
|
2018-09-29 03:40:50 +00:00
|
|
|
#if defined(__linux__) || defined(__unix__)
|
2020-07-01 05:32:21 +00:00
|
|
|
if (colours == 2)
|
|
|
|
colours = isatty(STDOUT_FILENO);
|
|
|
|
if (colours)
|
2018-09-29 03:40:50 +00:00
|
|
|
{ //only use colours if its a tty, and not if we're redirected.
|
|
|
|
col_none = "\e[0;m"; //reset to white
|
|
|
|
col_error = "\e[0;31m"; //red
|
|
|
|
col_symbol = "\e[0;32m"; //green
|
|
|
|
col_warning = "\e[0;33m"; //yellow
|
|
|
|
//col_ = "\e[0;34m"; //blue
|
|
|
|
col_name = "\e[0;35m"; //magenta
|
2020-09-08 04:44:07 +00:00
|
|
|
col_type = "\e[0;36m"; //cyan
|
2018-09-29 03:40:50 +00:00
|
|
|
col_location = "\e[0;1;37m"; //bright white
|
|
|
|
}
|
2020-07-01 05:32:21 +00:00
|
|
|
#else
|
|
|
|
(void)colours;
|
2018-09-29 03:40:50 +00:00
|
|
|
#endif
|
|
|
|
|
2020-05-29 10:27:43 +00:00
|
|
|
logfile = writelog?fopen("fteqcc.log", "wt"):false;
|
2018-09-29 03:40:50 +00:00
|
|
|
|
2018-05-28 10:12:10 +00:00
|
|
|
if (logfile)
|
2015-04-14 12:24:05 +00:00
|
|
|
{
|
2018-05-28 10:12:10 +00:00
|
|
|
fputs("Args:", logfile);
|
|
|
|
for (i = 0; i < argc; i++)
|
|
|
|
{
|
2020-05-29 10:27:43 +00:00
|
|
|
if (!argv[i])
|
|
|
|
continue;
|
2018-05-28 10:12:10 +00:00
|
|
|
if (strchr(argv[i], ' '))
|
|
|
|
fprintf(logfile, " \"%s\"", argv[i]);
|
|
|
|
else
|
|
|
|
fprintf(logfile, " %s", argv[i]);
|
|
|
|
}
|
|
|
|
fprintf(logfile, "\n");
|
2015-04-14 12:24:05 +00:00
|
|
|
}
|
2015-07-07 02:03:31 +00:00
|
|
|
sucess = CompileParams(&funcs, NULL, argc, argv);
|
2009-06-13 20:52:43 +00:00
|
|
|
qccClearHunk();
|
|
|
|
if (logfile)
|
|
|
|
fclose(logfile);
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
// fgetc(stdin); //wait for keypress
|
|
|
|
#endif
|
2014-08-03 14:47:47 +00:00
|
|
|
return sucess?EXIT_SUCCESS:EXIT_FAILURE;
|
2009-06-13 20:52:43 +00:00
|
|
|
}
|