-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
119 lines (91 loc) · 2.89 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <fstream>
#include "jsapi.h"
#include "jsopcode.h"
#include "vm/String.h"
#include "vm/Xdr.h"
using namespace js;
/* The class of the global object. */
static JSClass global_class = {
"global",
JSCLASS_GLOBAL_FLAGS,
JS_PropertyStub,
JS_DeletePropertyStub,
JS_PropertyStub,
JS_StrictPropertyStub,
JS_EnumerateStub,
JS_ResolveStub,
JS_ConvertStub,
nullptr,
nullptr,
nullptr,
nullptr,
JS_GlobalObjectTraceHook
};
int sanityTest(JSContext *cx) {
// Scope for our various stack objects (JSAutoRequest, RootedObject), so they all go
// out of scope before we JS_DestroyContext.
JSAutoRequest ar(cx); // In practice, you would want to exit this any
// time you're spinning the event loop
JS::RootedObject global(cx, JS_NewGlobalObject(cx, &global_class, nullptr, JS::FireOnNewGlobalHook));
if (!global)
return 1;
JS::RootedValue rval(cx);
{ // Scope for JSAutoCompartment
JSAutoCompartment ac(cx, global);
JS_InitStandardClasses(cx, global);
const char *script = "'hello'+'world, it is '+new Date()";
const char *filename = "noname";
int lineno = 1;
bool ok = JS_EvaluateScript(cx, global, script, strlen(script), filename, lineno, &rval);
if (!ok)
return 1;
}
JSString *str = rval.toString();
printf("%s\n", JS_EncodeString(cx, str));
}
void decryptFile(JSContext *cx) {
std::ifstream is("game.jsc", std::ifstream::binary);
if (is) {
// get the length of file
is.seekg(0, is.end);
int length = is.tellg();
is.seekg(0, is.beg);
// allocate a buffer
char *buffer = new char[length];
// read in
is.read(buffer, length);
is.close();
JSAutoRequest ar(cx); // In practice, you would want to exit this any
// time you're spinning the event loop
JS::RootedObject global(cx, JS_NewGlobalObject(cx, &global_class, nullptr, JS::FireOnNewGlobalHook));
if (!global)
return;
{ // Scope for JSAutoCompartment
JSAutoCompartment ac(cx, global);
JS_InitStandardClasses(cx, global);
if (buffer) {
RootedScript script(cx);
JSPrincipals *principals = new JSPrincipals();
// decode
script = JS_DecodeScript(cx, buffer, length, principals);
js_DumpScript(cx, script);
}
}
delete[] buffer;
}
}
int main(int argc, const char *argv[]) {
JS_Init();
JSRuntime *rt = JS_NewRuntime(8L * 1024 * 1024);
if (!rt)
return 1;
JSContext *cx = JS_NewContext(rt, 8192);
if (!cx)
return 1;
// sanityTest(cx);
decryptFile(cx);
JS_DestroyContext(cx);
JS_DestroyRuntime(rt);
JS_ShutDown();
return 0;
}