Skip to content

Commit 0305194

Browse files
authored
Left-align stub arguments to dd (fixes #374) (#598)
This satisfies busybox's non-standard integer argument parsing, and even saves a few bytes.
1 parent 55ddf73 commit 0305194

File tree

2 files changed

+40
-43
lines changed

2 files changed

+40
-43
lines changed

ape/ape.S

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -569,11 +569,11 @@ apesh: .ascii "'\n#'\"\n" # sixth edition shebang
569569
.ascii "t=\"${TMPDIR:-${HOME:-.}}/.ape\"\n"
570570
.ascii "[ -x \"$t\" ] || {\n"
571571
.ascii "mkdir -p \"${t%/*}\" &&\n"
572-
.ascii "dd if=\"$o\" of=\"$t.$$\" skip=\""
572+
.ascii "dd if=\"$o\" of=\"$t.$$\" skip="
573573
.shstub ape_loader_dd_skip,2
574-
.ascii "\" count=\""
574+
.ascii " count="
575575
.shstub ape_loader_dd_count,2
576-
.ascii "\" bs=64 2>/dev/null\n"
576+
.ascii " bs=64 2>/dev/null\n"
577577
#if SupportsXnu()
578578
.ascii "[ -d /Applications ] && "
579579
.ascii "dd if=\"$t.$$\""
@@ -637,11 +637,11 @@ apesh: .ascii "'\n#'\"\n" # sixth edition shebang
637637
.ascii "dd if=\"$o\""
638638
.ascii " of=\"$o\""
639639
.ascii " bs=8"
640-
.ascii " skip=\""
640+
.ascii " skip="
641641
.shstub ape_macho_dd_skip,2
642-
.ascii "\" count=\""
642+
.ascii " count="
643643
.shstub ape_macho_dd_count,2
644-
.ascii "\" conv=notrunc 2>/dev/null\n"
644+
.ascii " conv=notrunc 2>/dev/null\n"
645645
#endif /* XNU */
646646
.ascii "[ x\"$1\" = x--assimilate ] && exit 0\n"
647647
#ifndef APE_NO_MODIFY_SELF

ape/macros.internal.h

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -221,44 +221,41 @@
221221
/**
222222
* Binary coded decimal support.
223223
*
224-
* <p>This allows linker scripts to generate dd commands. Leading spaces
225-
* need to be inserted so Mac doesn't consider them octal; therefore,
226-
* parameters must be quoted; and eight digits should be good enough.
224+
* <p>This allows linker scripts to generate dd commands, e.g. ape.lds.
225+
* There are a few ways to pad each number to the necessary 8 bytes.
226+
* Spaces cannot be prepended because busybox refuses to parse them.
227+
* Zeros cannot be prepended because Mac will take numbers as octal.
228+
* That leaves appending spaces. The user's shell ought to treat any
229+
* unquoted run of spaces as if there was only one, so this is safe.
227230
*/
228-
#define SHSTUB2(SYM, X) \
229-
HIDDEN(SYM##_bcs0 = BCD10K(X)); \
230-
HIDDEN(SYM##_bcs1 = BCD(X))
231-
#define BCD(X) \
232-
((X) == 0 \
233-
? 0x20202030 \
234-
: (X) < 10 ? 0x30202020 + (((X) % 10) << 24) \
235-
: (X) < 100 ? 0x30302020 + (((X) % 10) << 24) + \
236-
(((X) / 10 % 10) << 16) \
237-
: (X) < 1000 ? 0x30303020 + (((X) % 10) << 24) + \
238-
(((X) / 10 % 10) << 16) + \
239-
(((X) / 100 % 10) << 8) \
240-
: 0x30303030 + (((X) % 10) << 24) + \
241-
(((X) / 10 % 10) << 16) + \
242-
(((X) / 100 % 10) << 8) + \
243-
(((X) / 1000 % 10) << 0))
244-
#define BCD10K(X) \
245-
((X) < 10000 \
246-
? 0x20202020 \
247-
: (X) < 100000 \
248-
? 0x30202020 + (((X) / 10000 % 10) << 24) \
249-
: (X) < 1000000 \
250-
? 0x30302020 + (((X) / 10000 % 10) << 24) + \
251-
(((X) / 100000 % 10) << 16) \
252-
: (X) < 10000000 \
253-
? 0x30303020 + (((X) / 10000 % 10) << 24) + \
254-
(((X) / 100000 % 10) << 16) + \
255-
(((X) / 1000000 % 10) << 8) \
256-
: (X) < 100000000 \
257-
? 0x30303030 + (((X) / 10000 % 10) << 24) + \
258-
(((X) / 100000 % 10) << 16) + \
259-
(((X) / 1000000 % 10) << 8) + \
260-
(((X) / 10000000 % 10) << 0) \
261-
: 0xffffffffffffffff)
231+
#define SHSTUB2(SYM, X) \
232+
HIDDEN(SYM##_bcs0 = BCD_LEFT(X)); \
233+
HIDDEN(SYM##_bcs1 = BCD_RIGHT(X))
234+
#define BCD_SMEAR(X) ((X) + (X) * 10000)
235+
#define BCD_LEFT(X) \
236+
(((X)) < 10000 ? BCD_RIGHT(BCD_SMEAR(X)) | 0x10 \
237+
: (X) < 100000 ? BCD_RIGHT(BCD_SMEAR((X) / 10)) \
238+
: (X) < 1000000 ? BCD_RIGHT(BCD_SMEAR((X) / 100)) \
239+
: (X) < 10000000 ? BCD_RIGHT(BCD_SMEAR((X) / 1000)) \
240+
: (X) < 100000000 ? BCD_RIGHT(BCD_SMEAR((X) / 10000)) \
241+
: 0xffffffffffffffff)
242+
#define BCD_RIGHT(X) \
243+
(((X)) < 10000 ? 0x20202020 \
244+
: (X) < 100000 ? 0x20202030 + \
245+
(X) % 10 \
246+
: (X) < 1000000 ? 0x20203030 + \
247+
((X) / 10) % 10 + \
248+
(X) % 10 * 0x100 \
249+
: (X) < 10000000 ? 0x20303030 + \
250+
((X) / 100) % 10 + \
251+
((X) / 10) % 10 * 0x100 + \
252+
(X) % 10 * 0x10000 \
253+
: (X) < 100000000 ? 0x30303030 + \
254+
((X) / 1000) % 10 + \
255+
((X) / 100) % 10 * 0x100 + \
256+
((X) / 10) % 10 * 0x10000 + \
257+
(X) % 10 * 0x1000000 \
258+
: 0xffffffffffffffff)
262259

263260
#endif /* __ASSEMBLER__ */
264261
#endif /* APE_MACROS_H_ */

0 commit comments

Comments
 (0)