Skip to content

Instantly share code, notes, and snippets.

@SonoSooS
Last active July 3, 2016 00:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save SonoSooS/dd2fee1b0f8725a4f167d1e37efc9665 to your computer and use it in GitHub Desktop.
Save SonoSooS/dd2fee1b0f8725a4f167d1e37efc9665 to your computer and use it in GitHub Desktop.
Converts SmileBASIC .DAT files to real little-endian binary files
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef unsigned long long u64;
typedef unsigned int u32;
typedef unsigned short u16;
typedef unsigned char u8;
int main(int argc, char** argv)
{
if(argc < 2)
{
printf("Usage: %s <filename> [output]\n", argv[0]);
return 1;
}
FILE* f = fopen(argv[1], "rb");
if(f<=0)
{
printf("Error: can't open file \"%s\"\n", argv[1]);
return 1;
}
char* of;
if(argc > 2)
{
of = argv[2];
}
else
{
of = malloc((strlen(argv[1]) + 4) * sizeof(char));
memset(of, 0, sizeof(of));
strcpy(of, argv[1]);
strcat(of, ".bin");
}
FILE* o = fopen(of, "wb");
if(o<=0)
{
fclose(f);
printf("Error: can't open output \"%s\"\n", of);
if(argc < 2) free(of);
return 1;
}
u8* buf = malloc(8);
u32 cnt;
fseek(f, 0x5C, SEEK_SET);
fread(buf, 4, 1, f);
cnt = (buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24));
printf("Output filesize: %d bytes\n", cnt);
fseek(f, 0x6C, SEEK_SET);
int testflag = 1;
int bigflag = !*(u8*)&testflag;
printf("Your system is %s-endian\n", bigflag ? "big" : "little");
u32 read = 0;
while(read != cnt)
{
fread(buf, 8, 1, f);
if(bigflag)
{
do
{
u8 tmp;
int i;
for(i = 0; i != 4; i++)
{
tmp = buf[i];
buf[i] = buf[7 - i];
buf[7 - i] = tmp;
}
}
while(0);
}
double dbl = *(double*)buf;
u16 on = (u16)dbl;
buf[0] = on & 0xFF;
buf[1] = (on >> 8) & 0xFF;
fwrite(buf,2,1,o);
read++;
}
fflush(o);
fclose(o);
fclose(f);
free(of);
free(buf);
puts("Successful conversion");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment