ntsys/ntsys-disassembler.h
2026-03-23 21:29:25 +04:00

85 lines
2.6 KiB
C

/*
* ntsys-disassembler.h
*
* Copyright 2026
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/
#if (!defined(__LIB_mKOdwuwdww332_H_))
#define __LIB_mKOdwuwdww332_H_
int ntsys_unassembly_file(char* filename, char* asm_out) {
FILE* fp = fopen(filename, "rb");
FILE* out = fopen(asm_out, "w");
if (fp == NULL || out == NULL) {
ntsys_error("Cannot open file!");
if (fp != NULL) fclose(fp);
if (out != NULL) fclose(out);
return EXIT_FAILURE;
}
fputs("; ", out);
size_t g = 0;
while (g < 8) {
__e_byte_t c = getc(fp);
fprintf(out, "%X", c);
g ++;
}
fputc('\n', out);
char buf[64];
while (ferror(fp) == 0 && feof(fp) == 0) {
__e_byte_t c = getc(fp);
size_t i = 0;
while (i < ntsys_command_count) {
size_t t = 0;
while (t < strlen(ntsys_asm_table[i])) {
if (ntsys_asm_table[i][t] == '=') break;
t ++;
}
strncpy(buf, ntsys_asm_table[i], t);
buf[t] = '\0';
char* name = buf;
__e_byte_t f = 0;
char* se = &ntsys_asm_table[i][t + 1];
while (strlen(se) > 1) {
char is[3];
strncpy(is, se, 2);
is[2] = '\0';
if (is[0] != ':') {
__e_byte_t d = atoi(is);
if (d == c && f == 0
&& strcmp(name, "exit")
&& strcmp(name, "rcall")
&& strcmp(name, "global")
) {
f = 1;
fprintf(out, "0x%08lX: %s \n", ftell(fp) - 8, name);
}
}
se += sizeof(char) * 2;
}
i ++;
}
}
fclose(fp);
fclose(out);
return EXIT_SUCCESS;
}
#endif /* __LIB_mKOdwuwdww332_H_ */