This software and its source code (if available) are licensed under the Apache-2.0 license (unless otherwise specified) which DOES NOT APPLY to any third-party code (such as zlib for example) and borrowed Assembler code (ripped code from other software executable files).
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
/*#define DEBUG 1*/
#pragma pack(push, 1)
typedef struct {
uint32_t dwRIFFSign;
uint32_t dwRIFFSize;
uint32_t dwWAVESign;
uint32_t dwfmt_Sign;
uint32_t dwfmt_Size;
/* PCMWAVEFORMAT */
/* WAVEFORMAT */
uint16_t wFormatTag;
uint16_t nChannels;
uint32_t nSamplesPerSec;
uint32_t nAvgBytesPerSec;
uint16_t nBlockAlign;
/* /WAVEFORMAT */
uint16_t wBitsPerSample;
/* /PCMWAVEFORMAT */
uint32_t dwdataSign;
uint32_t dwdataSize;
} wav_head;
typedef struct {
uint32_t sndcount;
uint32_t datasize;
} web_head;
typedef struct {
uint32_t offs;
uint32_t size;
} web_item;
typedef struct {
int32_t sign1; /* -2 */
uint32_t same1; /* same1 == same2 == same3 */
uint32_t dwHz;
uint32_t same2;
uint32_t same3;
uint32_t zero1;
uint16_t unk1;
uint16_t unk2;
char filename[32]; /* 8.3 - "SLODIE~1.WAV" instead of "SLODIERFREED.WEB" */
uint16_t unk3;
uint16_t compr_type;
uint32_t blocks_align;
uint16_t sub_blocks_x4;
uint16_t unk5;
uint32_t zero2;
uint32_t zero3;
uint32_t sign2; /* 0x12345678 */
} web_file;
#pragma pack(pop)
#include "pcsndlib.c"
void FillWaveHeader(wav_head *hdr, uint32_t len, uint32_t sps, uint16_t chn, uint16_t bps) {
if (hdr) {
memset(hdr, 0, sizeof(hdr[0]));
hdr->dwRIFFSign = 0x46464952;
hdr->dwRIFFSize = sizeof(hdr[0]) - 8 + len;
hdr->dwWAVESign = 0x45564157;
hdr->dwfmt_Sign = 0x20746D66;
hdr->dwfmt_Size = 16;
hdr->wFormatTag = 1;
hdr->nChannels = chn;
hdr->nSamplesPerSec = sps;
hdr->wBitsPerSample = bps;
hdr->nBlockAlign = hdr->nChannels * (hdr->wBitsPerSample / 8);
hdr->nAvgBytesPerSec = hdr->nBlockAlign * hdr->nSamplesPerSec;
hdr->dwdataSign = 0x61746164;
hdr->dwdataSize = len;
}
}
void readdata(FILE *fl, void *data, uint32_t size) {
if (data && size) {
memset(data, 0, size);
if (fl) {
fread(data, size, 1, fl);
}
}
}
void dumpdata(char *name, void *data, uint32_t size) {
FILE *f;
if (name) {
f = fopen(name, "wb");
if (f) {
if (data && size) {
fwrite(data, size, 1, f);
}
fclose(f);
}
}
}
uint8_t *decode_web(uint8_t *p) {
uint32_t sz;
snd_data wd;
web_file *wf;
uint8_t *u;
wf = (web_file *) p;
memset(&wd, 0, sizeof(wd));
wd.src_ptr = (uint32_t *) &wf[1];
wd.sub_blocks = wf->sub_blocks_x4 / 4;
wd.compr_type = wf->compr_type;
wd.out_blocks = (wf->same1 + wf->blocks_align - 1) / wf->blocks_align;
/* raw PCM unpacked data size */
sz = wd.out_blocks * wf->blocks_align * 2;
u = (uint8_t *) malloc(sizeof(wav_head) + sz);
if (u) {
wd.dst_ptr = (uint16_t *) &u[sizeof(wav_head)];
memset(wd.dst_ptr, 0, sz);
sndecode(&wd);
FillWaveHeader((wav_head *) u, sz, wf->dwHz, 1, 16);
}
return(u);
}
int main(int argc, char *argv[]) {
uint32_t i, sz, *n;
uint8_t *p, *u;
web_item *list;
web_head head;
char *s, b[16];
FILE *fl;
printf(
"Turok 1/2 sound converter v1.1\n"
"(c) CTPAX-X Team 2019,2026\n"
"http://www.CTPAX-X.org/\n\n"
);
if (argc != 2) {
printf(
"Usage: turoksnd <filename.ext>\n\n"
"Run on files *.11c / *.22c / *.11k / *.22k\n\n"
);
return(1);
}
fl = fopen(argv[1], "rb");
if (!fl) {
printf("Error: can't open input file.\n\n");
return(2);
}
/* read head */
memset(&head, 0, sizeof(head));
fread(&head, sizeof(head), 1, fl);
fseek(fl, 0, SEEK_END);
i = ftell(fl);
if ((!head.sndcount) || (!head.datasize) || (i <= head.datasize)) {
fclose(fl);
printf("Error: invalid input file format.\n\n");
return(3);
}
/* calc header size */
head.datasize = i - head.datasize;
list = (web_item *) malloc(head.datasize - sizeof(head));
if (!list) {
fclose(fl);
printf("Error: not enough memory for contents table.\n\n");
return(4);
}
fseek(fl, sizeof(head), SEEK_SET);
readdata(fl, list, head.datasize - sizeof(head));
/* names offset */
n = (uint32_t *) &list[head.sndcount];
/* names list */
s = (*n) ? ((char *) &n[1]) : b;
/* initialize sound tables */
sndinits();
for (i = 0; i < head.sndcount; i++) {
/* names list missing */
if (!(*n)) { sprintf(b, "%08u.WAV", i); }
printf("%s\n", s);
sz = list[i].size;
p = (uint8_t *) malloc(sz);
if (p) {
fseek(fl, head.datasize + list[i].offs, SEEK_SET);
readdata(fl, p, sz);
/* .WEB format ? */
if ((sz > sizeof(web_file)) && (((web_file *)p)->sign1 == -2)) {
u = decode_web(p);
if (u) {
free(p);
p = u;
sz = (*((uint32_t *) &p[4])) + 8;
/* fix filename */
list[i].offs = strlen(s);
if (list[i].offs > 4) {
strcpy(&s[list[i].offs - 4], ".WAV");
}
}
}
#ifndef DEBUG
dumpdata(s, p, sz);
#endif
free(p);
}
/* names list existed */
if ((*n)) { s += strlen(s) + 1; }
}
free(list);
fclose(fl);
printf("\ndone\n\n");
return(0);
}