-
-
Save ihaveamac/69eb6ed190621511e589df1a5f7f2010 to your computer and use it in GitHub Desktop.
Converts SmileBASIC .DAT files to real little-endian binary files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
u32 on = (u32)dbl; | |
buf[0] = on & 0xFF; | |
buf[1] = (on >> 8) & 0xFF; | |
buf[2] = (on >> 16) & 0xFF; | |
buf[3] = (on >> 24) & 0xFF; | |
fwrite(buf,4,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