Skip to content

Commit 9fb4918

Browse files
committed
Packaging: use base91 instead of base85 everywhere
1 parent 575cc55 commit 9fb4918

File tree

6 files changed

+78
-112
lines changed

6 files changed

+78
-112
lines changed

README.md

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -715,27 +715,6 @@ fn game() {
715715

716716
## Open Source Attributions
717717

718-
[base85](https://github.com/rafagafe/base85/blob/master/base85.c)
719-
```
720-
Copyright (c) 2016-2018 Rafa Garcia <rafagarcia77@gmail.com>.
721-
722-
Permission is hereby granted, free of charge, to any person obtaining a copy
723-
of this software and associated documentation files (the "Software"), to deal
724-
in the Software without restriction, including without limitation the rights
725-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
726-
copies of the Software, and to permit persons to whom the Software is
727-
furnished to do so, subject to the following conditions:
728-
The above copyright notice and this permission notice shall be included in all
729-
copies or substantial portions of the Software.
730-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
731-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
732-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
733-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
734-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
735-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
736-
SOFTWARE.
737-
```
738-
739718
[MINT64 OS](https://github.com/kkamagui/mint64os/blob/master/02.Kernel64/Source/Loader.c)
740719
```
741720
/**

scripts/static-pie-gen.py

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -83,24 +83,21 @@
8383

8484
code_b91 = base91.encode(code).decode('ascii')
8585
code_b91_len = len(code_b91)
86-
code_b91 = '"' + code_b91 + '"'
87-
88-
code = bytearray(code)
89-
while len(code) % 4 != 0:
90-
code.append(0)
91-
code_b85 = base64.b85encode(code, pad=False).decode('ascii') + ']'
86+
code_b91_quoted = '"' + code_b91 + '"'
9287

9388
if lang_name == "C":
9489
L = 4095
9590
s = []
96-
for i in range(0, len(code_b85), L):
97-
x = code_b85[i:min(i+L,len(code_b85))]
98-
x = x.replace("?", "\\?")
91+
for i in range(0, len(code_b91), L):
92+
x = code_b91[i:min(i+L,len(code_b91))]
93+
# Escape '\' and '?'
94+
x = x.replace('\\', '\\\\')
95+
x = x.replace('?', '\\?')
9996
x = '"' + x + '",\n'
10097
s.append(x)
10198
r = "{\n" + "".join(s) + "}"
10299
else:
103-
r = '"' + code_b85 + '"'
100+
r = '"' + code_b91 + '"'
104101

105102
# stub
106103
with open(stub_path, "rb") as f:
@@ -112,17 +109,14 @@
112109

113110
stub_b91 = base91.encode(stub).decode('ascii')
114111
stub_b91_len = len(stub_b91)
112+
if lang_name == "C":
113+
stub_b91 = stub_b91.replace("?", "\\?")
115114
stub_b91 = '"' + stub_b91 + '"'
116115

117116
stub = bytearray(stub)
118117
while len(stub) % 4 != 0:
119118
stub.append(0)
120119
stub_raw = '"' + "".join("\\x{:02x}".format(x) for x in stub) + '"'
121-
stub_b85 = base64.b85encode(stub, pad=False).decode('ascii') + ']'
122-
stub_b85_len = len(stub_b85)
123-
if lang_name == "C":
124-
stub_b85 = stub_b85.replace("?", "\\?")
125-
stub_b85 = '"' + stub_b85 + '"'
126120

127121
# template
128122
template_candidates = [template_path]
@@ -146,18 +140,15 @@
146140
out_candidate = utils.multiple_replace(template, {
147141
"$$$$solution_src$$$$": sol,
148142
"$$$$stub_raw$$$$": stub_raw,
149-
"$$$$stub_base85$$$$": stub_b85,
150143
"$$$$stub_len$$$$": str(len(stub)),
151-
"$$$$stub_base85_len$$$$": str(stub_b85_len),
152144
"$$$$stub_base91$$$$": stub_b91,
153145
"$$$$stub_base91_len$$$$": str(stub_b91_len),
154-
"$$$$binary_base85$$$$": r,
155-
"$$$$binary_base85_len$$$$": str(len(code_b85)),
156-
"$$$$binary_base91$$$$": code_b91,
146+
"$$$$binary_base91$$$$": code_b91_quoted,
157147
"$$$$binary_base91_len$$$$": str(code_b91_len),
158148
"$$$$binary_raw_base91$$$$": code_raw_b91,
159149
"$$$$binary_raw_base91_len$$$$": str(code_raw_b91_len),
160-
"$$$$min_len_4096$$$$": str(min(len(code_b85)+1, 4096)),
150+
"$$$$binary_base91_chunked$$$$": r,
151+
"$$$$min_len_4096$$$$": str(min(len(code_b91)+1, 4096)),
161152
"$$$$entrypoint_offset$$$$": str(loader_fdict['entrypoint_offset']),
162153
"$$$$exports_cpp$$$$": exports_cpp
163154
})

scripts/templates/static-pie-template-amd64-fn-impl.cpp

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,21 @@ typedef unsigned long long uint64_t;
5353
#define BASMCALL
5454
#endif
5555

56-
// Base85 decoder. Code adapted from:
57-
// https://github.com/rafagafe/base85/blob/master/base85.c
58-
const char *b85 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>\?@^_`{|}~";
59-
void b85tobin(void *dest, char const *src) {
60-
uint32_t *p = (uint32_t *)dest;
61-
uint8_t digittobin[256];
62-
for (uint8_t i=0; i<85; i++) digittobin[(uint8_t)b85[i]] = i;
56+
void b91tobin(void *dest, char const *src) {
57+
uint8_t *p = (uint8_t *)dest;
58+
uint32_t eax = 0x1f;
6359
while (1) {
6460
while (*src == '\0') src++;
65-
if (*src == ']') break;
66-
uint32_t value = 0;
67-
for (uint32_t i=0; i<5; i++) {
68-
value *= 85;
69-
value += digittobin[(uint8_t)*src++];
70-
}
71-
*p++ = (value >> 24) | ((value >> 8) & 0xff00) | ((value << 8) & 0xff0000) | (value << 24);
61+
uint32_t x = (uint32_t) *src++;
62+
if (x < 0x24) return;
63+
while (*src == '\0') src++;
64+
uint32_t y = (uint32_t) *src++;
65+
eax <<= 13;
66+
eax += (y - 0x24) * 91 + (x - 0x24);
67+
do {
68+
*p++ = (uint8_t) eax;
69+
eax >>= 8;
70+
} while (eax & (1 << 12));
7271
}
7372
}
7473

@@ -148,7 +147,7 @@ stub_ptr get_stub() {
148147
return (stub_ptr) stub;
149148
}
150149
#endif
151-
char payload[][$$$$min_len_4096$$$$] = $$$$binary_base85$$$$;
150+
char payload[][$$$$min_len_4096$$$$] = $$$$binary_base91_chunked$$$$;
152151

153152
static int g_loaded = 0;
154153
static PLATFORM_DATA g_pd;
@@ -181,7 +180,7 @@ size_t basm_load_module() {
181180
g_pd.ptr_write_stdio = (void *) svc_write_stdio;
182181
#endif
183182
stub_ptr stub = get_stub();
184-
b85tobin(payload, (char const *)payload);
183+
b91tobin(payload, (char const *)payload);
185184
stub(&g_pd, payload);
186185
g_loaded = 1;
187186
basm_on_loaded();

scripts/templates/static-pie-template-amd64-short.c

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,21 @@ typedef unsigned char u8;
88
typedef unsigned int u32;
99
typedef unsigned long long u64;
1010
#define BASMCALL __attribute__((ms_abi))
11-
// Base85 decoder. Code adapted from:
12-
// https://github.com/rafagafe/base85/blob/master/base85.c
13-
const char *b85 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>\?@^_`{|}~";
14-
void b85tobin(void *dest, char const *src) {
15-
u32 *p = (u32 *)dest;
16-
u8 digittobin[256];
17-
for (u8 i=0; i<85; i++) digittobin[(u8)b85[i]] = i;
11+
void b91tobin(void *dest, char const *src) {
12+
u8 *p = (u8 *)dest;
13+
u32 eax = 0x1f;
1814
while (1) {
1915
while (*src == '\0') src++;
20-
if (*src == ']') break;
21-
u32 value = 0;
22-
for (u32 i=0; i<5; i++) {
23-
value *= 85;
24-
value += digittobin[(u8)*src++];
25-
}
26-
*p++ = (value >> 24) | ((value >> 8) & 0xff00) | ((value << 8) & 0xff0000) | (value << 24);
16+
u32 x = (u32) *src++;
17+
if (x < 0x24) return;
18+
while (*src == '\0') src++;
19+
u32 y = (u32) *src++;
20+
eax <<= 13;
21+
eax += (y - 0x24) * 91 + (x - 0x24);
22+
do {
23+
*p++ = (u32) eax;
24+
eax >>= 8;
25+
} while (eax & (1 << 12));
2726
}
2827
}
2928
#pragma pack(push, 1)
@@ -35,7 +34,7 @@ typedef struct {
3534
} PLATFORM_DATA;
3635
#pragma pack(pop)
3736
typedef int (BASMCALL *stub_ptr)(void *, void *);
38-
char payload[][$$$$min_len_4096$$$$] = $$$$binary_base85$$$$;
37+
char payload[][$$$$min_len_4096$$$$] = $$$$binary_base91_chunked$$$$;
3938
int main() {}
4039
#ifdef __cplusplus
4140
extern "C"
@@ -52,8 +51,8 @@ int __libc_start_main(
5251
pd.env_id = 2;
5352
pd.env_flags = 1;
5453
u8 stubbuf[68 + $$$$stub_len$$$$];
55-
b85tobin(stubbuf, "QMd~L002n8@6D@;XGJ3cz5oya01pLO>naZmS5~+Q0000n|450>x(5IN07=KfA^-pYO)<bp|Hw@-$qxlyU&9Xz]");
56-
b85tobin(stubbuf + 68, $$$$stub_base85$$$$);
54+
b91tobin(stubbuf, "H;|DR:$$$|7x6E69i$6',&%Q$$?@GjeBmVodz$C?$$c7h{.>j<g9%Q$$Q80&F$$$f5U$5L@=aT8S92:|1&.C!");
55+
b91tobin(stubbuf + 68, $$$$stub_base91$$$$);
5756
size_t base = ((size_t)main) & 0xFFFFFFFFFFFFF000ULL;
5857
*(u64 *)(stubbuf + 0x08) = (u64) base;
5958
*(u32 *)(stubbuf + 0x11) = (u32) 4096;
@@ -62,6 +61,6 @@ int __libc_start_main(
6261
len = ((len + 0xFFF) >> 12) << 12;
6362
syscall(10, base, len, 0x7);
6463
pd.fn_table[0] = (void *) (stubbuf + 0x1c);
65-
b85tobin(payload, (char const *)payload);
64+
b91tobin(payload, (char const *)payload);
6665
return ((stub_ptr) stubbuf)(&pd, payload);
6766
}

scripts/templates/static-pie-template-amd64.c

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,21 @@ typedef unsigned long long uint64_t;
4444
#define BASMCALL
4545
#endif
4646

47-
// Base85 decoder. Code adapted from:
48-
// https://github.com/rafagafe/base85/blob/master/base85.c
49-
const char *b85 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>\?@^_`{|}~";
50-
void b85tobin(void *dest, char const *src) {
51-
uint32_t *p = (uint32_t *)dest;
52-
uint8_t digittobin[256];
53-
for (uint8_t i=0; i<85; i++) digittobin[(uint8_t)b85[i]] = i;
47+
void b91tobin(void *dest, char const *src) {
48+
uint8_t *p = (uint8_t *)dest;
49+
uint32_t eax = 0x1f;
5450
while (1) {
5551
while (*src == '\0') src++;
56-
if (*src == ']') break;
57-
uint32_t value = 0;
58-
for (uint32_t i=0; i<5; i++) {
59-
value *= 85;
60-
value += digittobin[(uint8_t)*src++];
61-
}
62-
*p++ = (value >> 24) | ((value >> 8) & 0xff00) | ((value << 8) & 0xff0000) | (value << 24);
52+
uint32_t x = (uint32_t) *src++;
53+
if (x < 0x24) return;
54+
while (*src == '\0') src++;
55+
uint32_t y = (uint32_t) *src++;
56+
eax <<= 13;
57+
eax += (y - 0x24) * 91 + (x - 0x24);
58+
do {
59+
*p++ = (uint8_t) eax;
60+
eax >>= 8;
61+
} while (eax & (1 << 12));
6362
}
6463
}
6564

@@ -139,7 +138,7 @@ stub_ptr get_stub() {
139138
return (stub_ptr) stub;
140139
}
141140
#endif
142-
char payload[][$$$$min_len_4096$$$$] = $$$$binary_base85$$$$;
141+
char payload[][$$$$min_len_4096$$$$] = $$$$binary_base91_chunked$$$$;
143142

144143
#if defined(__linux__) && (defined(BOJ) || defined(BASM_CI))
145144
int main() {}
@@ -189,8 +188,8 @@ int main(int argc, char *argv[]) {
189188

190189
stub_ptr stub = get_stub();
191190
#if defined(__linux__)
192-
uint8_t stubbuf[68 + $$$$stub_len$$$$] = "QMd~L002n8@6D@;XGJ3cz5oya01pLO>naZmS5~+Q0000n|450>x(5IN07=KfA^-pYO)<bp|Hw@-$qxlyU&9Xz]";
193-
b85tobin(stubbuf, (char const *)stubbuf);
191+
uint8_t stubbuf[68 + $$$$stub_len$$$$] = "H;|DR:$$$|7x6E69i$6',&%Q$$?@GjeBmVodz$C?$$c7h{.>j<g9%Q$$Q80&F$$$f5U$5L@=aT8S92:|1&.C!";
192+
b91tobin(stubbuf, (char const *)stubbuf);
194193
/* prepend thunk and relocate stub onto stack */
195194
for (size_t i = 0; i < $$$$stub_len$$$$; i++) stubbuf[68 + i] = (uint8_t)stub_raw[i];
196195
size_t base = ((size_t)stub_raw) & 0xFFFFFFFFFFFFF000ULL; // page-aligned pointer to munmap in thunk
@@ -205,7 +204,7 @@ int main(int argc, char *argv[]) {
205204
pd.ptr_alloc_rwx = (void *) (stubbuf + 0x1c); // thunk implements its own svc_alloc_rwx
206205
stub = (stub_ptr) stubbuf;
207206
#endif
208-
b85tobin(payload, (char const *)payload);
207+
b91tobin(payload, (char const *)payload);
209208
return stub(&pd, payload);
210209
}
211210
// LOADER END

scripts/templates/static-pie-template-i686.c

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,21 @@
2020
#endif
2121
#endif
2222

23-
// Base85 decoder. Code adapted from:
24-
// https://github.com/rafagafe/base85/blob/master/base85.c
25-
const char *b85 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>\?@^_`{|}~";
26-
void b85tobin(void *dest, char const *src) {
27-
uint32_t *p = (uint32_t *)dest;
28-
uint8_t digittobin[256];
29-
for (uint8_t i=0; i<85; i++) digittobin[(uint8_t)b85[i]] = i;
23+
void b91tobin(void *dest, char const *src) {
24+
uint8_t *p = (uint8_t *)dest;
25+
uint32_t eax = 0x1f;
3026
while (1) {
3127
while (*src == '\0') src++;
32-
if (*src == ']') break;
33-
uint32_t value = 0;
34-
for (uint32_t i=0; i<5; i++) {
35-
value *= 85;
36-
value += digittobin[(uint8_t)*src++];
37-
}
38-
*p++ = (value >> 24) | ((value >> 8) & 0xff00) | ((value << 8) & 0xff0000) | (value << 24);
28+
uint32_t x = (uint32_t) *src++;
29+
if (x < 0x24) return;
30+
while (*src == '\0') src++;
31+
uint32_t y = (uint32_t) *src++;
32+
eax <<= 13;
33+
eax += (y - 0x24) * 91 + (x - 0x24);
34+
do {
35+
*p++ = (uint8_t) eax;
36+
eax >>= 8;
37+
} while (eax & (1 << 12));
3938
}
4039
}
4140

@@ -99,8 +98,8 @@ void *svc_alloc_rwx(size_t size) {
9998

10099
typedef int (*stub_ptr)(void *, void *);
101100

102-
const char *stub_base85 = $$$$stub_base85$$$$;
103-
char payload[][$$$$min_len_4096$$$$] = $$$$binary_base85$$$$;
101+
const char *stub_base91 = $$$$stub_base91$$$$;
102+
char payload[][$$$$min_len_4096$$$$] = $$$$binary_base91_chunked$$$$;
104103

105104
int main(int argc, char *argv[]) {
106105
PLATFORM_DATA pd;
@@ -128,8 +127,8 @@ int main(int argc, char *argv[]) {
128127
pd.ptr_write_stdio = (void *) svc_write_stdio;
129128

130129
stub_ptr stub = (stub_ptr) svc_alloc_rwx(4096);
131-
b85tobin((void *) stub, stub_base85);
132-
b85tobin(payload, (char const *)payload);
130+
b91tobin((void *) stub, stub_base91);
131+
b91tobin(payload, (char const *)payload);
133132
return stub(&pd, payload);
134133
}
135134
// LOADER END

0 commit comments

Comments
 (0)