encode/decode functions. doencfile
This commit is contained in:
parent
4397e34c35
commit
2e18174388
|
@ -224,13 +224,85 @@ int instead_load(char *game)
|
|||
return 0;
|
||||
}
|
||||
|
||||
typedef struct LoadF {
|
||||
int extraline;
|
||||
unsigned char byte;
|
||||
FILE *f;
|
||||
unsigned char buff[LUAL_BUFFERSIZE];
|
||||
} LoadF;
|
||||
|
||||
static const char *getF (lua_State *L, void *ud, size_t *size) {
|
||||
int i = 0;
|
||||
LoadF *lf = (LoadF *)ud;
|
||||
(void)L;
|
||||
if (lf->extraline) {
|
||||
lf->extraline = 0;
|
||||
*size = 1;
|
||||
return "\n";
|
||||
}
|
||||
if (feof(lf->f)) return NULL;
|
||||
*size = fread(lf->buff, 1, sizeof(lf->buff), lf->f);
|
||||
for (i = 0; i < *size; i ++) {
|
||||
unsigned char b = lf->buff[i];
|
||||
lf->buff[i] ^= lf->byte;
|
||||
lf->buff[i] = (lf->buff[i] >> 3) | (lf->buff[i] << 5);
|
||||
lf->byte = b;
|
||||
}
|
||||
return (*size > 0) ? (char*)lf->buff : NULL;
|
||||
}
|
||||
|
||||
static int errfile (lua_State *L, const char *what, int fnameindex) {
|
||||
const char *serr = strerror(errno);
|
||||
const char *filename = lua_tostring(L, fnameindex) + 1;
|
||||
lua_pushfstring(L, "cannot %s %s: %s", what, filename, serr);
|
||||
lua_remove(L, fnameindex);
|
||||
return LUA_ERRFILE;
|
||||
}
|
||||
|
||||
static int loadfile (lua_State *L, const char *filename) {
|
||||
LoadF lf;
|
||||
int status, readstatus;
|
||||
int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */
|
||||
lf.extraline = 0;
|
||||
lua_pushfstring(L, "@%s", filename);
|
||||
lf.f = fopen(filename, "r");
|
||||
lf.byte = 0xcc;
|
||||
if (lf.f == NULL) return errfile(L, "open", fnameindex);
|
||||
status = lua_load(L, getF, &lf, lua_tostring(L, -1));
|
||||
readstatus = ferror(lf.f);
|
||||
if (filename) fclose(lf.f); /* close file (even in case of errors) */
|
||||
if (readstatus) {
|
||||
lua_settop(L, fnameindex); /* ignore results from `lua_load' */
|
||||
return errfile(L, "read", fnameindex);
|
||||
}
|
||||
lua_remove(L, fnameindex);
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
static int luaB_doencfile (lua_State *L) {
|
||||
const char *fname = luaL_optstring(L, 1, NULL);
|
||||
int n = lua_gettop(L);
|
||||
if (loadfile(L, fname) != 0) lua_error(L);
|
||||
lua_call(L, 0, LUA_MULTRET);
|
||||
return lua_gettop(L) - n;
|
||||
}
|
||||
|
||||
static const luaL_Reg base_funcs[] = {
|
||||
{"doencfile", luaB_doencfile},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
int instead_init(void)
|
||||
{
|
||||
setlocale(LC_ALL,"");
|
||||
// strcpy(curcp, "UTF-8");
|
||||
/* initialize Lua */
|
||||
L = lua_open();
|
||||
if (!L)
|
||||
return -1;
|
||||
luaL_openlibs(L);
|
||||
luaL_register(L, "_G", base_funcs);
|
||||
if (dofile(L,STEAD_PATH"/stead.lua")) {
|
||||
return -1;
|
||||
}
|
||||
|
@ -256,3 +328,37 @@ void instead_done(void)
|
|||
#endif
|
||||
}
|
||||
|
||||
int instead_encode(const char *s, const char *d)
|
||||
{
|
||||
FILE *src;
|
||||
FILE *dst;
|
||||
size_t size;
|
||||
int i = 0;
|
||||
unsigned char byte = 0xcc;
|
||||
unsigned char buff[LUAL_BUFFERSIZE];
|
||||
|
||||
src = fopen(s, "r");
|
||||
if (!src) {
|
||||
fprintf(stderr,"Can't open on read: '%s'.\n", s);
|
||||
return -1;
|
||||
}
|
||||
dst = fopen(d, "w");
|
||||
if (!dst) {
|
||||
fprintf(stderr,"Can't open on write: '%s'.\n", s);
|
||||
return -1;
|
||||
}
|
||||
while ((size = fread(buff, 1, sizeof(buff), src))) {
|
||||
for (i = 0; i < size; i++) {
|
||||
buff[i] = (buff[i] << 3) | (buff[i] >> 5);
|
||||
buff[i] ^= byte;
|
||||
byte = buff[i];
|
||||
}
|
||||
if (fwrite(buff, 1, size, dst) != size) {
|
||||
fprintf(stderr, "Error while writing file: '%s'.\n", d);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
fclose(src);
|
||||
fclose(dst);
|
||||
return 0;
|
||||
}
|
|
@ -7,5 +7,5 @@ extern void instead_done(void);
|
|||
extern char *instead_cmd(char *s);
|
||||
extern char *instead_eval(char *s);
|
||||
char *fromgame(const char *s);
|
||||
|
||||
extern int instead_encode(const char *s, const char *d);
|
||||
#endif
|
||||
|
|
|
@ -10,9 +10,12 @@ char *game_sw = NULL;
|
|||
char *games_sw = NULL;
|
||||
char *theme_sw = NULL;
|
||||
char *themes_sw = NULL;
|
||||
char *encode_sw = NULL;
|
||||
char *encode_output = NULL;
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int err = 0;
|
||||
int i;
|
||||
for (i = 1; i < argc; i++) {
|
||||
if (!strcmp(argv[i],"-alsa"))
|
||||
|
@ -47,14 +50,29 @@ int main(int argc, char **argv)
|
|||
themes_sw = argv[++i];
|
||||
else
|
||||
themes_sw = "";
|
||||
}
|
||||
} else if (!strcmp(argv[i], "-encode")) {
|
||||
if ((i + 1) < argc)
|
||||
encode_sw = argv[++i];
|
||||
else {
|
||||
fprintf(stderr,"No lua file specified.\n");
|
||||
exit(1);
|
||||
}
|
||||
if ((i + 1) < argc)
|
||||
encode_output = argv[++i];
|
||||
else
|
||||
encode_output = "lua.enc";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if (debug_sw) {
|
||||
debug_init();
|
||||
}
|
||||
|
||||
|
||||
if (encode_sw) {
|
||||
err = instead_encode(encode_sw, encode_output);
|
||||
goto out;
|
||||
}
|
||||
menu_langs_lookup(LANG_PATH);
|
||||
|
||||
if (!langs_nr) {
|
||||
|
@ -109,9 +127,10 @@ int main(int argc, char **argv)
|
|||
game_loop();
|
||||
cfg_save();
|
||||
game_done(0);
|
||||
gfx_done();
|
||||
out:
|
||||
if (debug_sw)
|
||||
debug_done();
|
||||
gfx_done();
|
||||
return 0;
|
||||
return err;
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue