Skip to content

Commit 8a74f7b

Browse files
committed
Support quickjs across all files.
1 parent 3d115d9 commit 8a74f7b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+17581
-17055
lines changed

.eslintrc.json

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"rizin": true,
3737
"atob": true,
3838
"btoa": true,
39+
"unit": true,
3940
"console": true
4041
}
4142
}

.reuse/dep5

-4
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ Files: subprojects/**/*
3939
Copyright: 2023 Giovanni Dante Grazioli <[email protected]>
4040
License: BSD-3-Clause
4141

42-
Files: meson.build
43-
Copyright: 2021 Giovanni Dante Grazioli <[email protected]>
44-
License: BSD-3-Clause
45-
4642
Files: meson_options.txt
4743
Copyright: 2021 Giovanni Dante Grazioli <[email protected]>
4844
License: BSD-3-Clause

LICENSES/MIT.txt

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
MIT License
2+
3+
Copyright (c) <year> <copyright holders>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

c/base64.c

+403
Large diffs are not rendered by default.

c/base64.h

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// SPDX-FileCopyrightText: 2013 William Sherif
2+
// SPDX-License-Identifier: MIT
3+
4+
#ifndef BASE64_H
5+
#define BASE64_H
6+
7+
#include <stdio.h>
8+
#include <stdlib.h>
9+
10+
// The COMPILE-TIME SETTING SAFEBASE64 is really important.
11+
// You need to decide if PARANOIA is more important to you than speed.
12+
//
13+
// SAFEBASE64: Remove this def to NOT check the validity of base64 ascii strings
14+
// before unbase64'ing that string. If you don't #define SAFEBASE64,
15+
// then the program assumes that all characters in the string sent to unbase64()
16+
// are in the base64 alphabet. As such if a character is NOT in the base64 alphabet
17+
// your data will be wrong (it will be turned to 0 (as if it were just a base64 'A')).
18+
// Removing this test greatly speeds up unbase64'ing (by about 3-4x).
19+
#define SAFEBASE64
20+
#define isMultipleOf(a, x) (!((a) % x))
21+
22+
// Converts any binary data to base64 characters.
23+
// Length of the resultant string is stored in flen
24+
// (you must pass pointer flen).
25+
char *base64(const void *binaryData, int len, int *flen);
26+
27+
// Convert your base64 string haJIh/+ back to binary data.
28+
// len is the string length and should NOT include the null terminator.
29+
// Final size will be stored in flen
30+
// (you must pass pointer flen).
31+
unsigned char *unbase64(const char *ascii, int len, int *flen);
32+
33+
// Checks the integrity of a base64 string to make sure it is
34+
// made up of only characters in the base64 alphabet (array b64)
35+
#define isbase64ValidChr(ch) (('0' <= ch && ch <= '9') || \
36+
('A' <= ch && ch <= 'Z') || ('a' <= ch && ch <= 'z') || \
37+
ch == '+' || ch == '/') // other 2 valid chars, + ending chrs
38+
// = is NOT considered a valid base64 chr, it's only valid at the end for padding
39+
40+
// Tells you if a string is valid base64, which means it's length is
41+
// a multiple of 4, and it contains only valid base64 chrs.
42+
int base64integrity(const char *ascii, int len);
43+
44+
#endif

c/jsdec-plugin.c

+25-21
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ typedef struct rz_core_t RzCore;
1717
typedef struct exec_context_t {
1818
RzCore *core;
1919
void *bed;
20+
JSValue shared;
2021
} ExecContext;
2122

2223
#undef RZ_API
@@ -130,21 +131,21 @@ static JSValue js_command(JSContext *ctx, JSValueConst jsThis, int argc, JSValue
130131
if (argc != 1) {
131132
return JS_EXCEPTION;
132133
}
133-
ExecContext *ectx = (ExecContext *)JS_GetContextOpaque(ctx);
134-
RzCore *core = ectx->core;
135-
rz_cons_sleep_end(ectx->bed);
136134

137135
const char *command = JS_ToCString(ctx, argv[0]);
138136
if (!command) {
139-
ectx->bed = rz_cons_sleep_begin();
140137
return JS_EXCEPTION;
141138
}
142139

140+
ExecContext *ectx = (ExecContext *)JS_GetContextOpaque(ctx);
141+
RzCore *core = ectx->core;
142+
rz_cons_sleep_end(ectx->bed);
143+
143144
char *output = rz_core_cmd_str(core, command);
144145
JS_FreeCString(ctx, command);
145-
146-
JSValue result = JS_NewString(ctx, output);
146+
JSValue result = JS_NewString(ctx, output ? output : "");
147147
free(output);
148+
148149
ectx->bed = rz_cons_sleep_begin();
149150
return result;
150151
}
@@ -190,26 +191,24 @@ static JSValue js_console_log(JSContext *ctx, JSValueConst jsThis, int argc, JSV
190191
return JS_UNDEFINED;
191192
}
192193

193-
static jsdec_t *jsdec_create(void *opaque, const char *arg) {
194+
static JSValue js_get_global(JSContext *ctx, JSValueConst jsThis, int argc, JSValueConst *argv) {
195+
ExecContext *ectx = (ExecContext *)JS_GetContextOpaque(ctx);
196+
return JS_GetPropertyStr(ctx, ectx->shared, "Shared");
197+
}
198+
199+
static jsdec_t *jsdec_create(ExecContext *ec, const char *arg) {
194200
jsdec_t *dec = jsdec_new();
195201
if (!dec) {
196202
return NULL;
197203
}
198204

199205
JSContext *ctx = jsdec_context(dec);
200-
JS_SetContextOpaque(ctx, opaque);
206+
JS_SetContextOpaque(ctx, ec);
207+
ec->shared = JS_NewObject(ctx);
208+
JS_SetPropertyStr(ctx, ec->shared, "Shared", JS_NewObject(ctx));
201209

202210
JSValue global = JS_GetGlobalObject(ctx);
203-
JS_SetPropertyStr(ctx, global, "Global", JS_NewObject(ctx));
204-
205-
JSValue limits = JS_NewObject(ctx);
206-
JS_SetPropertyStr(ctx, global, "Limits", limits);
207-
JS_SetPropertyStr(ctx, limits, "UT16_MAX", JS_NewBigUint64(ctx, UT16_MAX));
208-
JS_SetPropertyStr(ctx, limits, "UT32_MAX", JS_NewBigUint64(ctx, UT32_MAX));
209-
JS_SetPropertyStr(ctx, limits, "UT64_MAX", JS_NewBigUint64(ctx, UT64_MAX));
210-
JS_SetPropertyStr(ctx, limits, "ST16_MAX", JS_NewBigInt64(ctx, ST16_MAX));
211-
JS_SetPropertyStr(ctx, limits, "ST32_MAX", JS_NewBigInt64(ctx, ST32_MAX));
212-
JS_SetPropertyStr(ctx, limits, "ST64_MAX", JS_NewBigInt64(ctx, ST64_MAX));
211+
JS_SetPropertyStr(ctx, global, "Global", JS_NewCFunction(ctx, js_get_global, "Global", 1));
213212

214213
JSValue console = JS_NewObject(ctx);
215214
JS_SetPropertyStr(ctx, global, "console", console);
@@ -232,6 +231,12 @@ static jsdec_t *jsdec_create(void *opaque, const char *arg) {
232231
return dec;
233232
}
234233

234+
static void jsdec_destroy(jsdec_t *dec, ExecContext *ec) {
235+
JSContext *ctx = jsdec_context(dec);
236+
JS_FreeValue(ctx, ec->shared);
237+
jsdec_free(dec);
238+
}
239+
235240
static bool jsdec_main(RzCore *core, const char *arg) {
236241
ExecContext ectx;
237242
ectx.core = core;
@@ -245,16 +250,15 @@ static bool jsdec_main(RzCore *core, const char *arg) {
245250
bool ret = jsdec_run(dec);
246251
rz_cons_sleep_end(ectx.bed);
247252

248-
jsdec_free(dec);
253+
jsdec_destroy(dec, &ectx);
249254
return ret;
250255
}
251256

252257
static const RzCmdDescHelp pdd_usage = {
253258
.summary = "Core plugin for jsdec",
254259
};
255260

256-
257-
//static_description_no_args(cmd_pdd_star,"decompiled code is returned to rizin as comment (via CCu)");
261+
// static_description_no_args(cmd_pdd_star,"decompiled code is returned to rizin as comment (via CCu)");
258262
static_description_no_args(pdd, "decompile current function");
259263
static_description_no_args(pddt, "lists the supported architectures");
260264
static_description_no_args(pddc, "decompiled code is returned to rizin as 'file:line code' (via CL)");

0 commit comments

Comments
 (0)