Created
March 18, 2017 08:49
-
-
Save wareya/8187495fab7733445e5fcdc13abec6db to your computer and use it in GitHub Desktop.
This file contains 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
// Parses a dies irae "script" extracted from memory and dumps the content text | |
#include <stdint.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#define and && | |
#define or || | |
#ifndef NULL | |
#define NULL 0 | |
#endif | |
/* | |
begin audio association # 07 00 08 00 | |
audio file name # 76 00 5F 00 73 00 79 00 30 00 30 00 ........ | |
between audio and text # 00 00 | |
text (a begin quote) # 0C 30 | |
begin ruby text association # 07 00 01 00 | |
text (latin) # 50 00 61 00 74 00 65 00 72 00 20 00 ........ | |
between text and ruby text # 0A 00 | |
ruby text (japanese) # 29 59 6B 30 7E 30 57 30 7E 30 59 30 ........ | |
end of ruby text association # 00 00 | |
text (an end quote) # 0D 30 | |
end audio association # 07 00 09 00 | |
pause for input # 07 00 06 00 | |
pagefeed # 00 00 | |
text (japanese) # 00 30 15 20 15 20 11 5C 74 5E 02 30 | |
pause for input # 07 00 06 00 | |
text (japanese) # DE 30 EB 30 B3 30 88 30 8A 30 82 30 ........ | |
begin ruby text association # 07 00 01 00 | |
text (kanji) # EF 83 62 59 | |
between text and ruby text # 0A 00 | |
ruby text (hiragana) # 4D 30 83 30 57 30 83 30 | |
end ruby text association # 00 00 | |
text (japanese) # 6A 30 53 4F AF 8E 6E 30 11 5C 74 5E ........ | |
pause for input # 07 00 06 00 | |
newline # 0A 00 | |
*/ | |
/* | |
begin audio association # 07 00 08 00 | |
end audio association # 07 00 09 00 | |
pause for input # 07 00 06 00 | |
begin ruby text association # 07 00 01 00 | |
*/ | |
void fread_or_die(void* a, size_t b, size_t c, void* d) | |
{ | |
int got = fread(a, b, c, d); | |
if(feof(d)) {puts("feof"); exit(0);} | |
if(ferror(d)) {puts("ferror"); exit(0);} | |
if(got != c) {exit(0);} | |
} | |
FILE * script; | |
FILE * text; | |
uint16_t data; | |
void pull() | |
{ | |
fread_or_die(&data, sizeof(uint16_t), 1, script); | |
} | |
void push(uint16_t X) | |
{ | |
uint16_t data = X; | |
fwrite(&data, sizeof(uint16_t), 1, text); | |
} | |
int main() | |
{ | |
script = fopen("test.bin", "rb"); | |
if(!script) return puts("failed to open test.bin"), 0; | |
text = fopen("test.txt", "wb+"); | |
if(!text) return puts("failed to open test.txt"), 0; | |
push(0xFEFF); | |
uint8_t header[20]; // header? initial commands? | |
fread(header, 0x20, 1, script); | |
uint8_t commands[0x10000/8]; // one byte per 8 commands | |
int audio_state = 0; // 0 = off, higher numbers = later stages | |
int ruby_state = 0; | |
int command_state = 0; | |
while(1) | |
{ | |
pull(); | |
if(command_state == 0 and data == 0x0007) | |
command_state = 1; | |
else if(command_state == 1) | |
{ | |
if(data == 0x0001) // furigana | |
{ | |
// 3008 〈 | |
// 3009 〉 | |
// 300A 《 | |
// 300B 》 | |
push(0x3008); | |
while(1) | |
{ | |
pull(); | |
if(data == 0x000A) break; | |
push(data); | |
} | |
push(0x3009); | |
push(0x300A); | |
while(1) | |
{ | |
pull(); | |
if(data == 0x0000) break; | |
push(data); | |
} | |
push(0x300B); | |
} | |
if(data == 0x0008) // audio filename is not content text, skip | |
{ | |
while(1) | |
{ | |
pull(); | |
if(data == 0x0000) break; | |
} | |
} | |
command_state = 0; | |
} | |
else | |
{ | |
if(data < 0x0020) | |
{ | |
if(data == 0x0000) | |
{ | |
push(0x000A); | |
push(0x000A); | |
} | |
else if(data == 0x000A) | |
{ | |
push(data); | |
} | |
} | |
else | |
{ | |
push(data); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment