Compare commits
420
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fc71374dfc | ||
|
|
ead17e7808 | ||
|
|
25b756f789 | ||
|
|
3b3d0fc535 | ||
|
|
48c7b48f95 | ||
|
|
4f11a4b25d | ||
|
|
6908229d4f | ||
|
|
fb10ad3b5c | ||
|
|
c8eb861406 | ||
|
|
fdca28aaf4 | ||
|
|
a0a98f0700 | ||
|
|
7e2b9a4d9c | ||
|
|
a588fb9703 | ||
|
|
a9e7466e5c | ||
|
|
de26b74b1d | ||
|
|
0519429ea1 | ||
|
|
2f3b16fc1b | ||
|
|
bee252057c | ||
|
|
764df2555d | ||
|
|
60221ded38 | ||
|
|
ca0eb211b0 | ||
|
|
d368f3f783 | ||
|
|
42202bfb1e | ||
|
|
e527bd22db | ||
|
|
ea01f254b4 | ||
|
|
b59b003231 | ||
|
|
599f5dd8fa | ||
|
|
138ec0e0d8 | ||
|
|
1068aa8220 | ||
|
|
ca3f4c6639 | ||
|
|
f51819afa5 | ||
|
|
ad88be9e7e | ||
|
|
c920ecd73d | ||
|
|
1ecee61787 | ||
|
|
c0037a9974 | ||
|
|
b86f0198d6 | ||
|
|
4bdbb7cc0a | ||
|
|
0ac0492b37 | ||
|
|
87fcfe7dfe | ||
|
|
3017f2becf | ||
|
|
da09ae3451 | ||
|
|
8738c3102f | ||
|
|
44e86427f4 | ||
|
|
20387b703b | ||
|
|
dc6e92c990 | ||
|
|
ef892681f9 | ||
|
|
22b66febbd | ||
|
|
7773faa878 | ||
|
|
2ab2c25847 | ||
|
|
5a1bcdf036 | ||
|
|
456abad825 | ||
|
|
e17bc84d4c | ||
|
|
657fbcd62c | ||
|
|
61055ef909 | ||
|
|
d972f56837 | ||
|
|
b668e52c90 | ||
|
|
9e8f8a1968 | ||
|
|
8424dc436a | ||
|
|
391fdea73b | ||
|
|
8289b34626 | ||
|
|
91075de0a7 | ||
|
|
1380df7a5a | ||
|
|
9693daaaef | ||
|
|
79d35b5a0e | ||
|
|
5de7222395 | ||
|
|
3620c4ee67 | ||
|
|
80ce9e3998 | ||
|
|
c8cbd3516d | ||
|
|
9b2a50efe2 | ||
|
|
89f37ebe8c | ||
|
|
fddd93edbd | ||
|
|
3d572a8cc3 | ||
|
|
e33bd4aaa2 | ||
|
|
cdcb4ce314 | ||
|
|
7310339e73 | ||
|
|
72fbc81224 | ||
|
|
8aba5f6347 | ||
|
|
36eed984b8 | ||
|
|
93b684fe9f | ||
|
|
3e80101963 | ||
|
|
4855fbe6a8 | ||
|
|
705c9b3487 | ||
|
|
4e815cd7b3 | ||
|
|
3b19867d97 | ||
|
|
47775e8e6a | ||
|
|
7ac2bd6917 | ||
|
|
405afbd551 | ||
|
|
2bfe3753d9 | ||
|
|
8463ce06ca | ||
|
|
8a347fd31a | ||
|
|
3fb6b31c71 | ||
|
|
768ab5384c | ||
|
|
df1ef312a0 | ||
|
|
03d6d1014a | ||
|
|
56143ea63f | ||
|
|
758c948a63 | ||
|
|
77d242d7ac | ||
|
|
2051c69358 | ||
|
|
e05b922c89 | ||
|
|
c9af9073b8 | ||
|
|
b715e82e1f | ||
|
|
dee0168219 | ||
|
|
97a7813971 | ||
|
|
e4f4bf95ac | ||
|
|
22b96c0ddf | ||
|
|
28e4d2b854 | ||
|
|
4e82d99460 | ||
|
|
4da1badf13 | ||
|
|
052b253696 | ||
|
|
1db8aa2b59 | ||
|
|
ea7d310ad6 | ||
|
|
898cffe5f9 | ||
|
|
a2f8c89aec | ||
|
|
5cba59e9ce | ||
|
|
43982ed499 | ||
|
|
1a64fe3594 | ||
|
|
927ff335c5 | ||
|
|
5d33dbb67c | ||
|
|
a5f120b620 | ||
|
|
01ea35e81d | ||
|
|
2d78809b74 | ||
|
|
04d8729adb | ||
|
|
d8059bd827 | ||
|
|
c1ed0c4049 | ||
|
|
52a88341d6 | ||
|
|
f0a63eefae | ||
|
|
851992f070 | ||
|
|
649da41ca4 | ||
|
|
3f36fe9c2f | ||
|
|
5ac9d0a865 | ||
|
|
71c703c26b | ||
|
|
58b46b7d67 | ||
|
|
38721685af | ||
|
|
b96342799f | ||
|
|
3a8d2db1ce | ||
|
|
1ef0c69db4 | ||
|
|
afb92adf47 | ||
|
|
fd2ce3fa27 | ||
|
|
6ba6f5f6ab | ||
|
|
c21d8ff8c5 | ||
|
|
c385dac31c | ||
|
|
6f43f948bc | ||
|
|
993d416d02 | ||
|
|
fc9d352b4c | ||
|
|
3bfbce8312 | ||
|
|
26e9afe22d | ||
|
|
3b785b18d2 | ||
|
|
d755846906 | ||
|
|
b143b3f1d4 | ||
|
|
8d7ae80eda | ||
|
|
b8cf048880 | ||
|
|
4ee218a2d2 | ||
|
|
d549c8be86 | ||
|
|
8cafd7a908 | ||
|
|
782e3fc2b4 | ||
|
|
b99a529177 | ||
|
|
3ff3cb483d | ||
|
|
3621595939 | ||
|
|
a0017a5b50 | ||
|
|
3cc2460952 | ||
|
|
552003dbd6 | ||
|
|
9443b0bc5e | ||
|
|
f5e1bf966b | ||
|
|
381a40a049 | ||
|
|
3681b5f618 | ||
|
|
106546961d | ||
|
|
b4cedd4c9a | ||
|
|
5d66c8a602 | ||
|
|
5c2c2916fc | ||
|
|
2bdec03def | ||
|
|
2ccb891819 | ||
|
|
84d4ac3754 | ||
|
|
96466951bf | ||
|
|
a30882dec5 | ||
|
|
36f1b16b05 | ||
|
|
1a947654b5 | ||
|
|
94cb769b65 | ||
|
|
7423bbbe98 | ||
|
|
5ba6ad7733 | ||
|
|
5806d2a3db | ||
|
|
3e766dce5f | ||
|
|
0d1f6a54f7 | ||
|
|
a8f20420a5 | ||
|
|
6cc3380105 | ||
|
|
cad5a79bd4 | ||
|
|
4343ca9aa8 | ||
|
|
279371471e | ||
|
|
d029e80187 | ||
|
|
2bb8b9b2a4 | ||
|
|
438b92efdd | ||
|
|
f29ab91763 | ||
|
|
187f24b490 | ||
|
|
a5cf673c9c | ||
|
|
1847fee2d4 | ||
|
|
f161bc628f | ||
|
|
2b0b74e8b4 | ||
|
|
5ecbed0e26 | ||
|
|
ab1de7df99 | ||
|
|
1cd53c15a2 | ||
|
|
0da6c813e1 | ||
|
|
d18c9eadf0 | ||
|
|
e660eab458 | ||
|
|
ea2022efb3 | ||
|
|
f6fb3c603a | ||
|
|
9b67dde8cd | ||
|
|
81274d8b30 | ||
|
|
4ce8afe6b2 | ||
|
|
df154028dc | ||
|
|
9f490785e0 | ||
|
|
53b3854e81 | ||
|
|
c92755133b | ||
|
|
4eb4141b22 | ||
|
|
6d44762589 | ||
|
|
98b1a000c9 | ||
|
|
fa82ef1a2e | ||
|
|
5b3405594a | ||
|
|
422f4ede6f | ||
|
|
34948f1858 | ||
|
|
abab146b82 | ||
|
|
7648dc721e | ||
|
|
558ef4962d | ||
|
|
8f5d890071 | ||
|
|
0b05a4e71e | ||
|
|
47d27cdaff | ||
|
|
40c2d01da2 | ||
|
|
8bde71eb15 | ||
|
|
1bd8d714ca | ||
|
|
2363938883 | ||
|
|
5a8c03039f | ||
|
|
d16bde623c | ||
|
|
58b2353baa | ||
|
|
3aa2bee269 | ||
|
|
1044533ea9 | ||
|
|
d647d8aaba | ||
|
|
c229982b33 | ||
|
|
c1b5c35246 | ||
|
|
5f3a986c9e | ||
|
|
a33ce612b5 | ||
|
|
d7d8ffed87 | ||
|
|
02b518db1c | ||
|
|
58a54fbd5a | ||
|
|
778fce3e70 | ||
|
|
dba57344b5 | ||
|
|
4539262b6a | ||
|
|
26fdc7c6ef | ||
|
|
c2ef7c6dad | ||
|
|
cdbb6957f1 | ||
|
|
e591316e02 | ||
|
|
7d782e8813 | ||
|
|
91d8b4eb5c | ||
|
|
2d7ff2c909 | ||
|
|
4f43bda947 | ||
|
|
0fea54a983 | ||
|
|
e95086b2fa | ||
|
|
4f3a56dc12 | ||
|
|
bb18473216 | ||
|
|
551bfdb97f | ||
|
|
a98b95b715 | ||
|
|
41f677ec5d | ||
|
|
427f808732 | ||
|
|
65deb9f7dd | ||
|
|
dcbb69e0a5 | ||
|
|
628a7b2f47 | ||
|
|
e022ff88d1 | ||
|
|
ed22c5bd4a | ||
|
|
d23ad55904 | ||
|
|
77a49a61b2 | ||
|
|
c6b9e76373 | ||
|
|
6979467a62 | ||
|
|
51b45ce28a | ||
|
|
94591a1030 | ||
|
|
ee2ef17cc7 | ||
|
|
27e1fc679e | ||
|
|
504f44436f | ||
|
|
d2ef2c75c5 | ||
|
|
dde90f1d47 | ||
|
|
b823735b7e | ||
|
|
abe0b45cd7 | ||
|
|
e62bfbcaed | ||
|
|
95f169f3bb | ||
|
|
11d4c08d75 | ||
|
|
c305c81c94 | ||
|
|
e9c21c5c3a | ||
|
|
7b313c3465 | ||
|
|
0930708794 | ||
|
|
d0f0874d57 | ||
|
|
78bd8a44b7 | ||
|
|
d18ac080e4 | ||
|
|
8466c3e78f | ||
|
|
f8e9bd30d7 | ||
|
|
e5d3a87633 | ||
|
|
7798f651a4 | ||
|
|
67b98c7b1b | ||
|
|
e3484095f9 | ||
|
|
5e9b95ff40 | ||
|
|
479d8db99f | ||
|
|
75758c7360 | ||
|
|
63025e8bca | ||
|
|
f464be22bd | ||
|
|
c11730490a | ||
|
|
1a0913d57c | ||
|
|
e2091c9842 | ||
|
|
a2a5db41a3 | ||
|
|
53de6e6150 | ||
|
|
5e17e3c6f4 | ||
|
|
f3a1b7be72 | ||
|
|
0574928c85 | ||
|
|
aebe6f7132 | ||
|
|
447eb6bf71 | ||
|
|
82bbbd2e53 | ||
|
|
23b5527f5c | ||
|
|
f5af8aefea | ||
|
|
b8f7600166 | ||
|
|
9c4cd75d12 | ||
|
|
0893bc3bac | ||
|
|
b21cd481db | ||
|
|
62abd47815 | ||
|
|
4e5a6b6dbb | ||
|
|
ffac25d056 | ||
|
|
8be6dafe94 | ||
|
|
736589f877 | ||
|
|
85d3c48217 | ||
|
|
1c977008e0 | ||
|
|
1524540613 | ||
|
|
9b6e03fd09 | ||
|
|
e189464fa7 | ||
|
|
3d9b965b4a | ||
|
|
bf8231d11d | ||
|
|
10196929da | ||
|
|
be327bed9d | ||
|
|
444a91abc9 | ||
|
|
80e676664e | ||
|
|
84766710f4 | ||
|
|
142ac2d796 | ||
|
|
8d3d078917 | ||
|
|
9c46a06885 | ||
|
|
36ac283682 | ||
|
|
bbaa56f737 | ||
|
|
8c835540ef | ||
|
|
244f5c4795 | ||
|
|
dcd8df258a | ||
|
|
4b1f1da358 | ||
|
|
e6317e72dd | ||
|
|
b8ce2084cd | ||
|
|
f5c7cae55f | ||
|
|
73c67d75ef | ||
|
|
58adc7b527 | ||
|
|
952a7b9cec | ||
|
|
37abfb919d | ||
|
|
5f07327251 | ||
|
|
f13f68288f | ||
|
|
c61fdadf97 | ||
|
|
636315ccb9 | ||
|
|
9a8796436b | ||
|
|
fc466720b5 | ||
|
|
d2d23c848a | ||
|
|
a8349b115d | ||
|
|
f9274e8d6e | ||
|
|
887295686d | ||
|
|
4115439496 | ||
|
|
779cca899f | ||
|
|
ef21820dc9 | ||
|
|
14f57f5357 | ||
|
|
1ea89fa98a | ||
|
|
b03f5162ac | ||
|
|
dd40b40ee5 | ||
|
|
e2304d47a9 | ||
|
|
ee8b94acbf | ||
|
|
0bd84c9472 | ||
|
|
ba9651b803 | ||
|
|
e8622f0d85 | ||
|
|
b25ea3f156 | ||
|
|
d48400d8fb | ||
|
|
7624077772 | ||
|
|
f4f8dc6864 | ||
|
|
ed6561685c | ||
|
|
96a230f17b | ||
|
|
9adafbc118 | ||
|
|
a4894eaf71 | ||
|
|
9e364b16d1 | ||
|
|
a208fa03de | ||
|
|
b402b13fab | ||
|
|
38a3c0ad32 | ||
|
|
103139d0e6 | ||
|
|
470f009552 | ||
|
|
55fc6a18da | ||
|
|
1220b1519d | ||
|
|
808155ebf3 | ||
|
|
478ac90f2c | ||
|
|
ad12ab439b | ||
|
|
73e9d25d75 | ||
|
|
f020414413 | ||
|
|
50e2b59370 | ||
|
|
87e60dcf0f | ||
|
|
175b8dda19 | ||
|
|
e6f3a3b381 | ||
|
|
23db689683 | ||
|
|
75ca8d074b | ||
|
|
49d9e06fba | ||
|
|
8c4bccb83e | ||
|
|
baa49bdc1b | ||
|
|
0929a129fc | ||
|
|
96b0607302 | ||
|
|
d26e958725 | ||
|
|
4c1d645c86 | ||
|
|
860b3d066f | ||
|
|
748b168154 | ||
|
|
bb5525613e | ||
|
|
222802e833 | ||
|
|
8953429a82 | ||
|
|
bf57f732a5 | ||
|
|
dff444bc37 | ||
|
|
24aa93d538 | ||
|
|
39c5c08b28 | ||
|
|
26895db35d | ||
|
|
480a07bd68 | ||
|
|
dbd14c4a42 | ||
|
|
2c232f1e60 | ||
|
|
e5b0547ac8 | ||
|
|
a023f39265 |
No files matched your search
@@ -204,6 +204,13 @@ config FEATURE_INSTALLER
|
||||
busybox at runtime to create hard links or symlinks for all the
|
||||
applets that are compiled into busybox.
|
||||
|
||||
config FEATURE_VERSION
|
||||
bool "Support --version"
|
||||
default y
|
||||
depends on BUSYBOX
|
||||
help
|
||||
Enable 'busybox --version' support.
|
||||
|
||||
config INSTALL_NO_USR
|
||||
bool "Don't use /usr"
|
||||
default n
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
VERSION = 1
|
||||
PATCHLEVEL = 37
|
||||
PATCHLEVEL = 38
|
||||
SUBLEVEL = 0
|
||||
EXTRAVERSION =
|
||||
NAME = Unnamed
|
||||
@@ -476,6 +476,7 @@ libs-y := \
|
||||
findutils/ \
|
||||
init/ \
|
||||
libbb/ \
|
||||
libbb/yescrypt/ \
|
||||
libpwdgrp/ \
|
||||
loginutils/ \
|
||||
mailutils/ \
|
||||
@@ -612,8 +613,12 @@ quiet_cmd_busybox__ ?= LINK $@
|
||||
"$(core-y)" \
|
||||
"$(libs-y)" \
|
||||
"$(LDLIBS)" \
|
||||
"$(CONFIG_EXTRA_LDLIBS)" \
|
||||
$(CONFIG_EXTRA_LDLIBS) \
|
||||
&& $(srctree)/scripts/generate_BUFSIZ.sh --post include/common_bufsiz.h
|
||||
# ^^^ note: CONFIG_xyz strings already have double quotes: their value
|
||||
# is '"LIB LIB2"', therefore $(CONFIG_EXTRA_LDLIBS) above must NOT be written
|
||||
# as "$(CONFIG_EXTRA_LDLIBS)", it would be passed as ""LIB LIB2"",
|
||||
# and LIB2 would end up in $9, not $8 (and lost or misinterpreted).
|
||||
|
||||
# Generate System.map
|
||||
quiet_cmd_sysmap = SYSMAP
|
||||
|
||||
+2
-2
@@ -156,12 +156,12 @@ docs/busybox.pod: $(srctree)/docs/busybox_header.pod \
|
||||
docs/BusyBox.txt: docs/busybox.pod
|
||||
$(disp_doc)
|
||||
$(Q)-mkdir -p docs
|
||||
$(Q)-pod2text $< > $@
|
||||
$(Q)-pod2text --quotes=none $< > $@
|
||||
|
||||
docs/busybox.1: docs/busybox.pod
|
||||
$(disp_doc)
|
||||
$(Q)-mkdir -p docs
|
||||
$(Q)-pod2man --center=busybox --release="version $(KERNELVERSION)" $< > $@
|
||||
$(Q)-pod2man --quotes=none --center=busybox --release="version $(KERNELVERSION)" $< > $@
|
||||
|
||||
docs/BusyBox.html: docs/busybox.net/BusyBox.html
|
||||
$(disp_doc)
|
||||
|
||||
@@ -336,6 +336,7 @@ setuidgid - noexec. spawner
|
||||
sha1sum - noexec. runner
|
||||
sha256sum - noexec. runner
|
||||
sha3sum - noexec. runner
|
||||
sha384sum - noexec. runner
|
||||
sha512sum - noexec. runner
|
||||
showkey - interactive, longterm
|
||||
shred - runner
|
||||
|
||||
+12
-5
@@ -67,30 +67,37 @@ int main(void)
|
||||
}
|
||||
if (col == 0) {
|
||||
col = 6;
|
||||
printf("\t");
|
||||
} else {
|
||||
printf(", ");
|
||||
}
|
||||
printf("%s", usage_array[i].aname);
|
||||
if (usage_array[i].usage[0] != NOUSAGE_STR[0]) {
|
||||
/*
|
||||
* If the applet usage string will be included in the final document
|
||||
* optimistically link to its header (which is just the applet name).
|
||||
*/
|
||||
printf("L<C<%1$s>|/\"%1$s\">", usage_array[i].aname);
|
||||
} else {
|
||||
/* Without a usage string, just output the applet name with no link. */
|
||||
printf("C<%s>", usage_array[i].aname);
|
||||
}
|
||||
col += len2;
|
||||
}
|
||||
printf("\n\n");
|
||||
|
||||
printf("=head1 COMMAND DESCRIPTIONS\n\n");
|
||||
printf("=over 4\n\n");
|
||||
|
||||
for (i = 0; i < num_messages; i++) {
|
||||
if (usage_array[i].aname[0] >= 'a' && usage_array[i].aname[0] <= 'z'
|
||||
&& usage_array[i].usage[0] != NOUSAGE_STR[0]
|
||||
) {
|
||||
printf("=item B<%s>\n\n", usage_array[i].aname);
|
||||
/* This is the heading that will be linked from the command list. */
|
||||
printf("=head2 %s\n\n", usage_array[i].aname);
|
||||
if (usage_array[i].usage[0])
|
||||
printf("%s %s\n\n", usage_array[i].aname, usage_array[i].usage);
|
||||
else
|
||||
printf("%s\n\n", usage_array[i].aname);
|
||||
}
|
||||
}
|
||||
printf("=back\n\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -35,4 +35,15 @@ config FEATURE_LZMA_FAST
|
||||
This option reduces decompression time by about 25% at the cost of
|
||||
a 1K bigger binary.
|
||||
|
||||
config FEATURE_PATH_TRAVERSAL_PROTECTION
|
||||
bool "Prevent extraction of filenames with /../ path component"
|
||||
default n
|
||||
help
|
||||
busybox tar and unzip remove "PREFIX/../" (if it exists)
|
||||
from extracted names.
|
||||
This option enables this behavior for all other unpacking applets,
|
||||
such as cpio, ar, rpm.
|
||||
GNU cpio 2.15 has NO such sanity check.
|
||||
# try other archivers and document their behavior?
|
||||
|
||||
endmenu
|
||||
+3
-3
@@ -71,8 +71,8 @@ int FAST_FUNC bbunpack(char **argv,
|
||||
goto err;
|
||||
} else {
|
||||
/* "clever zcat" with FILE */
|
||||
/* fail_if_not_compressed because zcat refuses uncompressed input */
|
||||
int fd = open_zipped(filename, /*fail_if_not_compressed:*/ 1);
|
||||
/* die_if_not_compressed because zcat refuses uncompressed input */
|
||||
int fd = open_zipped(filename, /*die_if_not_compressed:*/ 1);
|
||||
if (fd < 0)
|
||||
goto err_name;
|
||||
xmove_fd(fd, STDIN_FILENO);
|
||||
@@ -80,7 +80,7 @@ int FAST_FUNC bbunpack(char **argv,
|
||||
} else
|
||||
if (option_mask32 & BBUNPK_SEAMLESS_MAGIC) {
|
||||
/* "clever zcat" on stdin */
|
||||
if (setup_unzip_on_fd(STDIN_FILENO, /*fail_if_not_compressed*/ 1))
|
||||
if (setup_unzip_on_fd(STDIN_FILENO, /*die_if_not_compressed*/ 1))
|
||||
goto err;
|
||||
}
|
||||
|
||||
|
||||
@@ -350,6 +350,12 @@ static NOINLINE int cpio_o(void)
|
||||
st.st_dev = st.st_rdev = 0;
|
||||
#endif
|
||||
|
||||
if (sizeof(st.st_size) > 4
|
||||
&& st.st_size > (off_t)0xffffffff
|
||||
) {
|
||||
bb_error_msg_and_die("error: file '%s' is larger than 4GB", name);
|
||||
}
|
||||
|
||||
bytes += printf("070701"
|
||||
"%08X%08X%08X%08X%08X%08X%08X"
|
||||
"%08X%08X%08X%08X" /* GNU cpio uses uppercase hex */
|
||||
@@ -421,6 +427,7 @@ int cpio_main(int argc UNUSED_PARAM, char **argv)
|
||||
#endif
|
||||
#endif
|
||||
"owner\0" Required_argument "R"
|
||||
"file\0" Required_argument "F"
|
||||
"verbose\0" No_argument "v"
|
||||
"null\0" No_argument "0"
|
||||
"quiet\0" No_argument "\xff"
|
||||
|
||||
+1
-1
@@ -1689,7 +1689,7 @@ static void unpack_package(deb_file_t *deb_file)
|
||||
accept_list = NULL;
|
||||
i = 0;
|
||||
while (i < ARRAY_SIZE(all_control_files)) {
|
||||
char *c = xasprintf("./%s", all_control_files[i]);
|
||||
char *c = concat_path_file(".", all_control_files[i]);
|
||||
llist_add_to(&accept_list, c);
|
||||
i++;
|
||||
}
|
||||
|
||||
@@ -2215,6 +2215,9 @@ int gzip_main(int argc UNUSED_PARAM, char **argv)
|
||||
};
|
||||
#endif
|
||||
|
||||
// TODO: use less ugly "split-globals" trick via SET_OFFSET_PTR_TO_GLOBALS().
|
||||
// The problem is, the current method strategically places G2.heap[]
|
||||
// (~24 references) so that it has zero offset.
|
||||
SET_PTR_TO_GLOBALS((char *)xzalloc(sizeof(struct globals)+sizeof(struct globals2))
|
||||
+ sizeof(struct globals));
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ COMMON_FILES:= \
|
||||
data_extract_all.o \
|
||||
data_extract_to_stdout.o \
|
||||
\
|
||||
unsafe_prefix.o \
|
||||
unsafe_symlink_target.o \
|
||||
\
|
||||
filter_accept_all.o \
|
||||
@@ -32,7 +33,6 @@ COMMON_FILES:= \
|
||||
DPKG_FILES:= \
|
||||
unpack_ar_archive.o \
|
||||
filter_accept_list_reassign.o \
|
||||
unsafe_prefix.o \
|
||||
get_header_ar.o \
|
||||
get_header_tar.o \
|
||||
get_header_tar_gz.o \
|
||||
@@ -47,7 +47,7 @@ lib-$(CONFIG_DPKG_DEB) += $(DPKG_FILES)
|
||||
|
||||
lib-$(CONFIG_AR) += get_header_ar.o unpack_ar_archive.o
|
||||
lib-$(CONFIG_CPIO) += get_header_cpio.o
|
||||
lib-$(CONFIG_TAR) += get_header_tar.o unsafe_prefix.o
|
||||
lib-$(CONFIG_TAR) += get_header_tar.o
|
||||
lib-$(CONFIG_FEATURE_TAR_TO_COMMAND) += data_extract_to_command.o
|
||||
lib-$(CONFIG_LZOP) += lzo1x_1.o lzo1x_1o.o lzo1x_d.o
|
||||
lib-$(CONFIG_UNLZOP) += lzo1x_1.o lzo1x_1o.o lzo1x_d.o
|
||||
@@ -67,7 +67,7 @@ lib-$(CONFIG_FEATURE_UNZIP_XZ) += open_transformer.o decompress_unxz.o
|
||||
# 'gzip -d', gunzip or zcat selects FEATURE_GZIP_DECOMPRESS
|
||||
lib-$(CONFIG_FEATURE_GZIP_DECOMPRESS) += open_transformer.o decompress_gunzip.o
|
||||
lib-$(CONFIG_UNCOMPRESS) += open_transformer.o decompress_uncompress.o
|
||||
lib-$(CONFIG_UNZIP) += open_transformer.o decompress_gunzip.o unsafe_prefix.o
|
||||
lib-$(CONFIG_UNZIP) += open_transformer.o decompress_gunzip.o
|
||||
lib-$(CONFIG_RPM2CPIO) += open_transformer.o decompress_gunzip.o get_header_cpio.o
|
||||
lib-$(CONFIG_RPM) += open_transformer.o decompress_gunzip.o get_header_cpio.o
|
||||
lib-$(CONFIG_GZIP) += open_transformer.o
|
||||
|
||||
@@ -65,6 +65,18 @@ void FAST_FUNC data_extract_all(archive_handle_t *archive_handle)
|
||||
} while (--n != 0);
|
||||
}
|
||||
#endif
|
||||
#if ENABLE_FEATURE_PATH_TRAVERSAL_PROTECTION
|
||||
/* Skip leading "/" and past last ".." path component */
|
||||
# if ENABLE_FEATURE_TAR_LONG_OPTIONS
|
||||
dst_name = (char *)skip_unsafe_prefix(dst_name);
|
||||
# else /* dst_name is file_header->name, which is free()d by e.g. cpio - must copy, can't just advance the ptr */
|
||||
strip_unsafe_prefix(dst_name);
|
||||
# endif
|
||||
#endif
|
||||
// ^^^ This may be a problem if some applets do need to extract absolute names.
|
||||
// (Probably will need to invent ARCHIVE_ALLOW_UNSAFE_NAME flag).
|
||||
// You might think that rpm needs it, but in my tests rpm's internal cpio
|
||||
// archive has names like "./usr/bin/FOO", not "/usr/bin/FOO".
|
||||
|
||||
if (archive_handle->ah_flags & ARCHIVE_CREATE_LEADING_DIRS) {
|
||||
char *slash = strrchr(dst_name, '/');
|
||||
@@ -177,8 +189,7 @@ void FAST_FUNC data_extract_all(archive_handle_t *archive_handle)
|
||||
|
||||
/* To avoid a directory traversal attack via symlinks,
|
||||
* do not restore symlinks with ".." components
|
||||
* or symlinks starting with "/", unless a magic
|
||||
* envvar is set.
|
||||
* or symlinks starting with "/"
|
||||
*
|
||||
* For example, consider a .tar created via:
|
||||
* $ tar cvf bug.tar anything.txt
|
||||
|
||||
@@ -454,8 +454,15 @@ char FAST_FUNC get_header_tar(archive_handle_t *archive_handle)
|
||||
#endif
|
||||
|
||||
/* Everything up to and including last ".." component is stripped */
|
||||
overlapping_strcpy(file_header->name, strip_unsafe_prefix(file_header->name));
|
||||
//TODO: do the same for file_header->link_target?
|
||||
strip_unsafe_prefix(file_header->name);
|
||||
if (file_header->link_target && !S_ISLNK(file_header->mode)) {
|
||||
/* GNU tar 1.34 examples:
|
||||
* tar: Removing leading '/' from hard link targets
|
||||
* tar: Removing leading '../' from hard link targets
|
||||
* tar: Removing leading 'etc/../' from hard link targets
|
||||
*/
|
||||
strip_unsafe_prefix(file_header->link_target);
|
||||
}
|
||||
|
||||
/* Strip trailing '/' in directories */
|
||||
/* Must be done after mode is set as '/' is used to check if it's a directory */
|
||||
|
||||
@@ -8,5 +8,5 @@
|
||||
void FAST_FUNC header_list(const file_header_t *file_header)
|
||||
{
|
||||
//TODO: cpio -vp DIR should output "DIR/NAME", not just "NAME" */
|
||||
puts(file_header->name);
|
||||
puts(printable_string(file_header->name));
|
||||
}
|
||||
@@ -57,13 +57,13 @@ void FAST_FUNC header_verbose_list(const file_header_t *file_header)
|
||||
ptm->tm_hour,
|
||||
ptm->tm_min,
|
||||
ptm->tm_sec,
|
||||
file_header->name);
|
||||
printable_string(file_header->name));
|
||||
|
||||
#endif /* FEATURE_TAR_UNAME_GNAME */
|
||||
|
||||
/* NB: GNU tar shows "->" for symlinks and "link to" for hardlinks */
|
||||
if (file_header->link_target) {
|
||||
printf(" -> %s", file_header->link_target);
|
||||
printf(" -> %s", printable_string(file_header->link_target));
|
||||
}
|
||||
bb_putchar('\n');
|
||||
}
|
||||
@@ -797,7 +797,6 @@ static int lzo1x_999_compress_internal(const uint8_t *in, unsigned in_len,
|
||||
if (swd->use_best_off)
|
||||
better_match(swd, &m_len, &m_off);
|
||||
#endif
|
||||
|
||||
/* shall we try a lazy match ? */
|
||||
ahead = 0;
|
||||
if (m_len >= max_lazy) {
|
||||
@@ -811,7 +810,6 @@ static int lzo1x_999_compress_internal(const uint8_t *in, unsigned in_len,
|
||||
max_ahead = LZO_MIN(2, (unsigned)l1 - 1);
|
||||
}
|
||||
|
||||
|
||||
while (ahead < max_ahead && c->look > m_len) {
|
||||
int lazy_match_min_gain;
|
||||
|
||||
|
||||
@@ -157,7 +157,7 @@ void FAST_FUNC fork_transformer(int fd, const char *transform_prog)
|
||||
/* Used by e.g. rpm which gives us a fd without filename,
|
||||
* thus we can't guess the format from filename's extension.
|
||||
*/
|
||||
static transformer_state_t *setup_transformer_on_fd(int fd, int fail_if_not_compressed)
|
||||
static transformer_state_t *setup_transformer_on_fd(int fd, int die_if_not_compressed)
|
||||
{
|
||||
transformer_state_t *xstate;
|
||||
|
||||
@@ -204,7 +204,7 @@ static transformer_state_t *setup_transformer_on_fd(int fd, int fail_if_not_comp
|
||||
}
|
||||
|
||||
/* No known magic seen */
|
||||
if (fail_if_not_compressed)
|
||||
if (die_if_not_compressed)
|
||||
bb_simple_error_msg_and_die("no gzip"
|
||||
IF_FEATURE_SEAMLESS_BZ2("/bzip2")
|
||||
IF_FEATURE_SEAMLESS_XZ("/xz")
|
||||
@@ -240,13 +240,15 @@ static void fork_transformer_and_free(transformer_state_t *xstate)
|
||||
/* Used by e.g. rpm which gives us a fd without filename,
|
||||
* thus we can't guess the format from filename's extension.
|
||||
*/
|
||||
int FAST_FUNC setup_unzip_on_fd(int fd, int fail_if_not_compressed)
|
||||
int FAST_FUNC setup_unzip_on_fd(int fd, int die_if_not_compressed)
|
||||
{
|
||||
transformer_state_t *xstate = setup_transformer_on_fd(fd, fail_if_not_compressed);
|
||||
transformer_state_t *xstate = setup_transformer_on_fd(fd, die_if_not_compressed);
|
||||
|
||||
if (!xstate->xformer) {
|
||||
/* Not compressed */
|
||||
int retval = xstate->signature_skipped; /* never zero */
|
||||
free(xstate);
|
||||
return 1;
|
||||
return retval;
|
||||
}
|
||||
|
||||
fork_transformer_and_free(xstate);
|
||||
@@ -264,7 +266,7 @@ void FAST_FUNC setup_lzma_on_fd(int fd)
|
||||
}
|
||||
#endif
|
||||
|
||||
static transformer_state_t *open_transformer(const char *fname, int fail_if_not_compressed)
|
||||
static transformer_state_t *open_transformer(const char *fname, int die_if_not_compressed)
|
||||
{
|
||||
transformer_state_t *xstate;
|
||||
int fd;
|
||||
@@ -284,18 +286,18 @@ static transformer_state_t *open_transformer(const char *fname, int fail_if_not_
|
||||
}
|
||||
}
|
||||
|
||||
xstate = setup_transformer_on_fd(fd, fail_if_not_compressed);
|
||||
xstate = setup_transformer_on_fd(fd, die_if_not_compressed);
|
||||
|
||||
return xstate;
|
||||
}
|
||||
|
||||
int FAST_FUNC open_zipped(const char *fname, int fail_if_not_compressed)
|
||||
int FAST_FUNC open_zipped(const char *fname, int die_if_not_compressed)
|
||||
{
|
||||
int fd;
|
||||
transformer_state_t *xstate;
|
||||
|
||||
xstate = open_transformer(fname, fail_if_not_compressed);
|
||||
if (!xstate)
|
||||
xstate = open_transformer(fname, die_if_not_compressed);
|
||||
if (!xstate) /* open error */
|
||||
return -1;
|
||||
|
||||
fd = xstate->src_fd;
|
||||
@@ -326,7 +328,7 @@ void* FAST_FUNC xmalloc_open_zipped_read_close(const char *fname, size_t *maxsz_
|
||||
transformer_state_t *xstate;
|
||||
char *image;
|
||||
|
||||
xstate = open_transformer(fname, /*fail_if_not_compressed:*/ 0);
|
||||
xstate = open_transformer(fname, /*die_if_not_compressed:*/ 0);
|
||||
if (!xstate) /* file open error */
|
||||
return NULL;
|
||||
|
||||
@@ -371,7 +373,7 @@ void* FAST_FUNC xmalloc_open_zipped_read_close(const char *fname, size_t *maxsz_
|
||||
int fd;
|
||||
char *image;
|
||||
|
||||
fd = open_zipped(fname, /*fail_if_not_compressed:*/ 0);
|
||||
fd = open_zipped(fname, /*die_if_not_compressed:*/ 0);
|
||||
if (fd < 0)
|
||||
return NULL;
|
||||
|
||||
|
||||
@@ -5,23 +5,42 @@
|
||||
#include "libbb.h"
|
||||
#include "bb_archive.h"
|
||||
|
||||
const char* FAST_FUNC strip_unsafe_prefix(const char *str)
|
||||
const char* FAST_FUNC skip_unsafe_prefix(const char *str)
|
||||
{
|
||||
const char *cp = str;
|
||||
while (1) {
|
||||
char *cp2;
|
||||
const char *cp2;
|
||||
if (*cp == '/') {
|
||||
cp++;
|
||||
continue;
|
||||
}
|
||||
if (is_prefixed_with(cp, "/../"+1)) {
|
||||
/* We are called lots of times.
|
||||
* is_prefixed_with(cp, "../") is slower than open-coding it,
|
||||
* with minimal code growth (~few bytes).
|
||||
*/
|
||||
if (cp[0] == '.' && cp[1] == '.' && cp[2] == '/') {
|
||||
cp += 3;
|
||||
continue;
|
||||
}
|
||||
cp2 = strstr(cp, "/../");
|
||||
cp2 = cp;
|
||||
find_dotdot:
|
||||
cp2 = strstr(cp2, "/..");
|
||||
if (!cp2)
|
||||
break;
|
||||
cp = cp2 + 4;
|
||||
break; /* No (more) malicious components */
|
||||
|
||||
/* We found "/..something" */
|
||||
cp2 += 3;
|
||||
if (*cp2 != '/') {
|
||||
if (*cp2 == '\0') {
|
||||
/* Trailing "/..": malicious, return "" */
|
||||
/* (causes harmless errors trying to create or hardlink a file named "") */
|
||||
return cp2;
|
||||
}
|
||||
/* "/..name" is not malicious, look for next "/.." */
|
||||
goto find_dotdot;
|
||||
}
|
||||
/* Found "/../": malicious, advance past it */
|
||||
cp = cp2 + 1;
|
||||
}
|
||||
if (cp != str) {
|
||||
static smallint warned = 0;
|
||||
@@ -33,3 +52,8 @@ const char* FAST_FUNC strip_unsafe_prefix(const char *str)
|
||||
}
|
||||
return cp;
|
||||
}
|
||||
|
||||
void FAST_FUNC strip_unsafe_prefix(char *str)
|
||||
{
|
||||
overlapping_strcpy(str, skip_unsafe_prefix(str));
|
||||
}
|
||||
@@ -36,6 +36,7 @@ void FAST_FUNC create_links_from_list(llist_t *list)
|
||||
*list->data ? "hard" : "sym",
|
||||
list->data + 1, target
|
||||
);
|
||||
/* Note: GNU tar 1.34 errors out only _after_ all links are (attempted to be) created */
|
||||
}
|
||||
list = list->link;
|
||||
}
|
||||
|
||||
@@ -208,7 +208,6 @@ static NOINLINE int lzo1x_optimize(uint8_t *in, unsigned in_len,
|
||||
lit = 0;
|
||||
goto match_done;
|
||||
|
||||
|
||||
/* handle matches */
|
||||
do {
|
||||
if (t < 16) { /* a M1 match */
|
||||
|
||||
+7
-2
@@ -316,7 +316,7 @@ static void extract_cpio(int fd, const char *source_rpm)
|
||||
archive_handle->src_fd = fd;
|
||||
/*archive_handle->offset = 0; - init_handle() did it */
|
||||
|
||||
setup_unzip_on_fd(archive_handle->src_fd, /*fail_if_not_compressed:*/ 1);
|
||||
setup_unzip_on_fd(archive_handle->src_fd, /*die_if_not_compressed:*/ 1);
|
||||
while (get_header_cpio(archive_handle) == EXIT_SUCCESS)
|
||||
continue;
|
||||
}
|
||||
@@ -533,6 +533,7 @@ int rpm2cpio_main(int argc UNUSED_PARAM, char **argv)
|
||||
// /* We need to know whether child (gzip/bzip/etc) exits abnormally */
|
||||
// signal(SIGCHLD, check_errors_in_children);
|
||||
|
||||
str = NULL;
|
||||
if (ENABLE_FEATURE_SEAMLESS_LZMA
|
||||
&& (str = rpm_getstr0(TAG_PAYLOADCOMPRESSOR)) != NULL
|
||||
&& strcmp(str, "lzma") == 0
|
||||
@@ -541,7 +542,11 @@ int rpm2cpio_main(int argc UNUSED_PARAM, char **argv)
|
||||
// set up decompressor without detection
|
||||
setup_lzma_on_fd(rpm_fd);
|
||||
} else {
|
||||
setup_unzip_on_fd(rpm_fd, /*fail_if_not_compressed:*/ 1);
|
||||
int signature_bytes = setup_unzip_on_fd(rpm_fd, /*die_if_not_compressed:*/ 0);
|
||||
if (signature_bytes != 0) {
|
||||
xlseek(rpm_fd, - signature_bytes, SEEK_CUR);
|
||||
bb_error_msg("warning, unknown compression '%s'", str);
|
||||
}
|
||||
}
|
||||
|
||||
if (bb_copyfd_eof(rpm_fd, STDOUT_FILENO) < 0)
|
||||
|
||||
+6
-2
@@ -119,6 +119,7 @@
|
||||
#include "libbb.h"
|
||||
#include "common_bufsiz.h"
|
||||
#include "bb_archive.h"
|
||||
#include "unicode.h"
|
||||
/* FIXME: Stop using this non-standard feature */
|
||||
#ifndef FNM_LEADING_DIR
|
||||
# define FNM_LEADING_DIR 0
|
||||
@@ -475,7 +476,7 @@ static int FAST_FUNC writeFileToTarball(struct recursive_state *state,
|
||||
DBG("writeFileToTarball('%s')", fileName);
|
||||
|
||||
/* Strip leading '/' and such (must be before memorizing hardlink's name) */
|
||||
header_name = strip_unsafe_prefix(fileName);
|
||||
header_name = skip_unsafe_prefix(fileName);
|
||||
|
||||
if (header_name[0] == '\0')
|
||||
return TRUE;
|
||||
@@ -1133,6 +1134,9 @@ int tar_main(int argc UNUSED_PARAM, char **argv)
|
||||
if (tar_handle->accept || tar_handle->reject)
|
||||
tar_handle->filter = filter_accept_reject_list;
|
||||
|
||||
/* For "tar xvf/tvf" output and Unicode in names */
|
||||
init_unicode();
|
||||
|
||||
/* Open the tar file */
|
||||
{
|
||||
int tar_fd = STDIN_FILENO;
|
||||
@@ -1164,7 +1168,7 @@ int tar_main(int argc UNUSED_PARAM, char **argv)
|
||||
* on e.g. tarball with 1st file named "BZh5".
|
||||
*/
|
||||
) {
|
||||
tar_handle->src_fd = open_zipped(tar_filename, /*fail_if_not_compressed:*/ 0);
|
||||
tar_handle->src_fd = open_zipped(tar_filename, /*die_if_not_compressed:*/ 0);
|
||||
if (tar_handle->src_fd < 0)
|
||||
bb_perror_msg_and_die("can't open '%s'", tar_filename);
|
||||
} else {
|
||||
|
||||
+1
-1
@@ -860,7 +860,7 @@ int unzip_main(int argc, char **argv)
|
||||
|
||||
/* Guard against "/abspath", "/../" and similar attacks */
|
||||
// NB: UnZip 6.00 has option -: to disable this
|
||||
overlapping_strcpy(dst_fn, strip_unsafe_prefix(dst_fn));
|
||||
strip_unsafe_prefix(dst_fn);
|
||||
|
||||
/* Filter zip entries */
|
||||
if (find_list_entry(zreject, dst_fn)
|
||||
|
||||
@@ -70,8 +70,8 @@ int showkey_main(int argc UNUSED_PARAM, char **argv)
|
||||
|
||||
INIT_G();
|
||||
|
||||
// FIXME: aks are all mutually exclusive
|
||||
getopt32(argv, "aks");
|
||||
// a,k,s are all mutually exclusive
|
||||
getopt32(argv, "^" "aks" "\0" "a--ks:k--as:s--ak");
|
||||
|
||||
// prepare for raw mode
|
||||
xget1(&tio, &tio0);
|
||||
|
||||
+2
-1
@@ -215,7 +215,8 @@ int cp_main(int argc, char **argv)
|
||||
(flags & FILEUTILS_DEREFERENCE) ? stat : lstat);
|
||||
if (s_flags < 0) /* error other than ENOENT */
|
||||
return EXIT_FAILURE;
|
||||
d_flags = cp_mv_stat(last, &dest_stat);
|
||||
d_flags = cp_mv_stat2(last, &dest_stat,
|
||||
(flags & FILEUTILS_NO_TARGET_DIR) ? lstat : stat);
|
||||
if (d_flags < 0) /* error other than ENOENT */
|
||||
return EXIT_FAILURE;
|
||||
|
||||
|
||||
+270
-166
@@ -27,21 +27,34 @@
|
||||
//kbuild:lib-$(CONFIG_CUT) += cut.o
|
||||
|
||||
//usage:#define cut_trivial_usage
|
||||
//usage: "[OPTIONS] [FILE]..."
|
||||
//usage: "{-b|c LIST | -f"IF_FEATURE_CUT_REGEX("|F")" LIST [-d SEP] [-s]} [-D] [-O SEP] [FILE]..."
|
||||
// --output-delimiter SEP is too long to fit into 80 char-wide help ----------------^^^^^^^^
|
||||
//usage:#define cut_full_usage "\n\n"
|
||||
//usage: "Print selected fields from FILEs to stdout\n"
|
||||
//usage: "\n -b LIST Output only bytes from LIST"
|
||||
//usage: "\n -c LIST Output only characters from LIST"
|
||||
//usage: "\n -d SEP Field delimiter for input (default -f TAB, -F run of whitespace)"
|
||||
//usage: "\n -O SEP Field delimeter for output (default = -d for -f, one space for -F)"
|
||||
//usage: "\n -D Don't sort/collate sections or match -fF lines without delimeter"
|
||||
//usage: IF_FEATURE_CUT_REGEX(
|
||||
//usage: "\n -d SEP Input field delimiter (default -f TAB, -F run of whitespace)"
|
||||
//usage: ) IF_NOT_FEATURE_CUT_REGEX(
|
||||
//usage: "\n -d SEP Input field delimiter (default TAB)"
|
||||
//usage: )
|
||||
//usage: "\n -f LIST Print only these fields (-d is single char)"
|
||||
//usage: IF_FEATURE_CUT_REGEX(
|
||||
//usage: "\n -F LIST Print only these fields (-d is regex)"
|
||||
//usage: )
|
||||
//usage: "\n -s Output only lines containing delimiter"
|
||||
//usage: "\n -s Drop lines with no delimiter (else print them in full)"
|
||||
//usage: "\n -D Don't sort ranges; line without delimiters has one field"
|
||||
//usage: IF_LONG_OPTS(
|
||||
//usage: "\n --output-delimiter SEP Output field delimeter"
|
||||
//usage: ) IF_NOT_LONG_OPTS(
|
||||
//usage: IF_FEATURE_CUT_REGEX(
|
||||
//usage: "\n -O SEP Output field delimeter (default = -d for -f, one space for -F)"
|
||||
//usage: ) IF_NOT_FEATURE_CUT_REGEX(
|
||||
//usage: "\n -O SEP Output field delimeter (default = -d)"
|
||||
//usage: )
|
||||
//usage: )
|
||||
//usage: "\n -n Ignored"
|
||||
//(manpage:-n with -b: don't split multibyte characters)
|
||||
//(manpage:-n with -b: don't split multibyte characters)
|
||||
//usage:
|
||||
//usage:#define cut_example_usage
|
||||
//usage: "$ echo \"Hello world\" | cut -f 1 -d ' '\n"
|
||||
@@ -53,11 +66,6 @@
|
||||
|
||||
#if ENABLE_FEATURE_CUT_REGEX
|
||||
#include "xregex.h"
|
||||
#else
|
||||
#define regex_t int
|
||||
typedef struct { int rm_eo, rm_so; } regmatch_t;
|
||||
#define xregcomp(x, ...) *(x) = 0
|
||||
#define regexec(...) 0
|
||||
#endif
|
||||
|
||||
/* This is a NOEXEC applet. Be very careful! */
|
||||
@@ -65,250 +73,346 @@ typedef struct { int rm_eo, rm_so; } regmatch_t;
|
||||
|
||||
/* option vars */
|
||||
#define OPT_STR "b:c:f:d:O:sD"IF_FEATURE_CUT_REGEX("F:")"n"
|
||||
#define CUT_OPT_BYTE_FLGS (1 << 0)
|
||||
#define CUT_OPT_CHAR_FLGS (1 << 1)
|
||||
#define CUT_OPT_FIELDS_FLGS (1 << 2)
|
||||
#define CUT_OPT_DELIM_FLGS (1 << 3)
|
||||
#define CUT_OPT_ODELIM_FLGS (1 << 4)
|
||||
#define CUT_OPT_SUPPRESS_FLGS (1 << 5)
|
||||
#define CUT_OPT_NOSORT_FLGS (1 << 6)
|
||||
#define CUT_OPT_REGEX_FLGS ((1 << 7) * ENABLE_FEATURE_CUT_REGEX)
|
||||
#define OPT_BYTE (1 << 0)
|
||||
#define OPT_CHAR (1 << 1)
|
||||
#define OPT_FIELDS (1 << 2)
|
||||
#define OPT_DELIM (1 << 3)
|
||||
#define OPT_ODELIM (1 << 4)
|
||||
#define OPT_SUPPRESS (1 << 5)
|
||||
#define OPT_NOSORT (1 << 6)
|
||||
#define OPT_REGEX ((1 << 7) * ENABLE_FEATURE_CUT_REGEX)
|
||||
|
||||
struct cut_list {
|
||||
int startpos;
|
||||
int endpos;
|
||||
#define opt_REGEX (option_mask32 & OPT_REGEX)
|
||||
|
||||
struct cut_range {
|
||||
unsigned startpos;
|
||||
unsigned endpos;
|
||||
};
|
||||
|
||||
static int cmpfunc(const void *a, const void *b)
|
||||
{
|
||||
return (((struct cut_list *) a)->startpos -
|
||||
((struct cut_list *) b)->startpos);
|
||||
const struct cut_range *aa = a;
|
||||
const struct cut_range *bb = b;
|
||||
return aa->startpos - bb->startpos;
|
||||
}
|
||||
|
||||
#define END_OF_LIST(list_elem) ((list_elem).startpos == UINT_MAX)
|
||||
#define NOT_END_OF_LIST(list_elem) ((list_elem).startpos != UINT_MAX)
|
||||
|
||||
static void cut_file(FILE *file, const char *delim, const char *odelim,
|
||||
const struct cut_list *cut_lists, unsigned nlists)
|
||||
const struct cut_range *cut_list)
|
||||
{
|
||||
char *line;
|
||||
unsigned linenum = 0; /* keep these zero-based to be consistent */
|
||||
regex_t reg;
|
||||
int spos, shoe = option_mask32 & CUT_OPT_REGEX_FLGS;
|
||||
|
||||
if (shoe) xregcomp(®, delim, REG_EXTENDED);
|
||||
int first_print = 1;
|
||||
|
||||
/* go through every line in the file */
|
||||
while ((line = xmalloc_fgetline(file)) != NULL) {
|
||||
|
||||
/* set up a list so we can keep track of what's been printed */
|
||||
int linelen = strlen(line);
|
||||
char *printed = xzalloc(linelen + 1);
|
||||
char *orig_line = line;
|
||||
unsigned linelen = strlen(line);
|
||||
unsigned cl_pos = 0;
|
||||
|
||||
/* cut based on chars/bytes XXX: only works when sizeof(char) == byte */
|
||||
if (option_mask32 & (CUT_OPT_CHAR_FLGS | CUT_OPT_BYTE_FLGS)) {
|
||||
/* Cut based on chars/bytes XXX: only works when sizeof(char) == byte */
|
||||
if (option_mask32 & (OPT_CHAR | OPT_BYTE)) {
|
||||
char *printed = xzalloc(linelen + 1);
|
||||
int need_odelim = 0;
|
||||
|
||||
/* print the chars specified in each cut list */
|
||||
for (; cl_pos < nlists; cl_pos++) {
|
||||
for (spos = cut_lists[cl_pos].startpos; spos < linelen;) {
|
||||
for (; NOT_END_OF_LIST(cut_list[cl_pos]); cl_pos++) {
|
||||
unsigned spos = cut_list[cl_pos].startpos;
|
||||
while (spos < linelen) {
|
||||
if (!printed[spos]) {
|
||||
printed[spos] = 'X';
|
||||
if (need_odelim && spos != 0 && !printed[spos-1]) {
|
||||
need_odelim = 0;
|
||||
fputs_stdout(odelim);
|
||||
}
|
||||
putchar(line[spos]);
|
||||
}
|
||||
if (++spos > cut_lists[cl_pos].endpos) {
|
||||
spos++;
|
||||
if (spos > cut_list[cl_pos].endpos) {
|
||||
/* will print OSEP (if not empty) */
|
||||
need_odelim = (odelim && odelim[0]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (*delim == '\n') { /* cut by lines */
|
||||
spos = cut_lists[cl_pos].startpos;
|
||||
free(printed);
|
||||
/* Cut by lines */
|
||||
} else if (!opt_REGEX && *delim == '\n') {
|
||||
unsigned spos = cut_list[cl_pos].startpos;
|
||||
|
||||
/* get out if we have no more lists to process or if the lines
|
||||
linenum++;
|
||||
/* get out if we have no more ranges to process or if the lines
|
||||
* are lower than what we're interested in */
|
||||
if (((int)linenum < spos) || (cl_pos >= nlists))
|
||||
if (linenum <= spos || END_OF_LIST(cut_list[cl_pos]))
|
||||
goto next_line;
|
||||
|
||||
/* if the line we're looking for is lower than the one we were
|
||||
* passed, it means we displayed it already, so move on */
|
||||
while (spos < (int)linenum) {
|
||||
spos++;
|
||||
while (++spos < linenum) {
|
||||
/* go to the next list if we're at the end of this one */
|
||||
if (spos > cut_lists[cl_pos].endpos) {
|
||||
if (spos > cut_list[cl_pos].endpos) {
|
||||
cl_pos++;
|
||||
/* get out if there's no more lists to process */
|
||||
if (cl_pos >= nlists)
|
||||
/* get out if there's no more ranges to process */
|
||||
if (END_OF_LIST(cut_list[cl_pos]))
|
||||
goto next_line;
|
||||
spos = cut_lists[cl_pos].startpos;
|
||||
spos = cut_list[cl_pos].startpos;
|
||||
/* get out if the current line is lower than the one
|
||||
* we just became interested in */
|
||||
if ((int)linenum < spos)
|
||||
if (linenum <= spos)
|
||||
goto next_line;
|
||||
}
|
||||
}
|
||||
|
||||
/* If we made it here, it means we've found the line we're
|
||||
* looking for, so print it */
|
||||
puts(line);
|
||||
if (first_print) {
|
||||
first_print = 0;
|
||||
fputs_stdout(line);
|
||||
} else
|
||||
printf("%s%s", odelim, line);
|
||||
goto next_line;
|
||||
} else { /* cut by fields */
|
||||
unsigned uu = 0, start = 0, end = 0, out = 0;
|
||||
int dcount = 0;
|
||||
/* Cut by fields */
|
||||
} else {
|
||||
unsigned next = 0, start = 0, end = 0;
|
||||
unsigned dcount = 0; /* we saw Nth delimiter (0 - didn't see any yet) */
|
||||
|
||||
/* Blank line? Check -s (later check for -s does not catch empty lines) */
|
||||
if (linelen == 0) {
|
||||
if (option_mask32 & OPT_SUPPRESS)
|
||||
goto next_line;
|
||||
}
|
||||
|
||||
if (!odelim)
|
||||
odelim = "\t";
|
||||
first_print = 1;
|
||||
|
||||
/* Loop through bytes, finding next delimiter */
|
||||
for (;;) {
|
||||
/* End of current range? */
|
||||
if (end == linelen || dcount > cut_lists[cl_pos].endpos) {
|
||||
if (++cl_pos >= nlists) break;
|
||||
if (option_mask32 & CUT_OPT_NOSORT_FLGS)
|
||||
start = dcount = uu = 0;
|
||||
end = 0;
|
||||
if (end == linelen || dcount > cut_list[cl_pos].endpos) {
|
||||
end_of_range:
|
||||
cl_pos++;
|
||||
if (END_OF_LIST(cut_list[cl_pos]))
|
||||
break;
|
||||
if (option_mask32 & OPT_NOSORT)
|
||||
start = dcount = next = 0;
|
||||
end = 0; /* (why?) */
|
||||
//bb_error_msg("End of current range");
|
||||
}
|
||||
/* End of current line? */
|
||||
if (uu == linelen) {
|
||||
/* If we've seen no delimiters, check -s */
|
||||
if (!cl_pos && !dcount && !shoe) {
|
||||
if (option_mask32 & CUT_OPT_SUPPRESS_FLGS)
|
||||
if (next == linelen) {
|
||||
end = linelen; /* print up to end */
|
||||
/* If we've seen no delimiters, and no -D, check -s */
|
||||
if (!(option_mask32 & OPT_NOSORT) && cl_pos == 0 && dcount == 0) {
|
||||
if (option_mask32 & OPT_SUPPRESS)
|
||||
goto next_line;
|
||||
} else if (dcount < cut_lists[cl_pos].startpos)
|
||||
start = linelen;
|
||||
end = linelen;
|
||||
/* else: will print entire line */
|
||||
} else if (dcount < cut_list[cl_pos].startpos) {
|
||||
/* echo 1.2 | cut -d. -f1,3: prints "1", not "1." */
|
||||
//break;
|
||||
/* ^^^ this fails a case with -D:
|
||||
* echo 1 2 | cut -DF 1,3,2:
|
||||
* do not end line processing when didn't find field#3
|
||||
*/
|
||||
//if (option_mask32 & OPT_NOSORT) - no, just do it always
|
||||
goto end_of_range;
|
||||
}
|
||||
//bb_error_msg("End of current line: s:%d e:%d", start, end);
|
||||
} else {
|
||||
/* Find next delimiter */
|
||||
if (shoe) {
|
||||
regmatch_t rr = {-1, -1};
|
||||
#if ENABLE_FEATURE_CUT_REGEX
|
||||
if (opt_REGEX) {
|
||||
regmatch_t rr;
|
||||
regex_t *reg = (void*) delim;
|
||||
|
||||
if (!regexec(®, line+uu, 1, &rr, REG_NOTBOL|REG_NOTEOL)) {
|
||||
end = uu + rr.rm_so;
|
||||
uu += rr.rm_eo;
|
||||
} else {
|
||||
uu = linelen;
|
||||
if (regexec(reg, line + next, 1, &rr, REG_NOTBOL|REG_NOTEOL) != 0) {
|
||||
/* not found, go to "end of line" logic */
|
||||
next = linelen;
|
||||
continue;
|
||||
}
|
||||
} else if (line[end = uu++] != *delim)
|
||||
continue;
|
||||
|
||||
/* Got delimiter. Loop if not yet within range. */
|
||||
if (dcount++ < cut_lists[cl_pos].startpos) {
|
||||
start = uu;
|
||||
end = next + rr.rm_so;
|
||||
next += (rr.rm_eo ? rr.rm_eo : 1);
|
||||
/* ^^^ advancing by at least 1 prevents infinite loops */
|
||||
/* testcase: echo "no at sign" | cut -d'@*' -F 1- */
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
end = next++;
|
||||
if (line[end] != *delim)
|
||||
continue;
|
||||
}
|
||||
/* Got delimiter */
|
||||
dcount++;
|
||||
if (dcount <= cut_list[cl_pos].startpos) {
|
||||
/* Not yet within range - loop */
|
||||
start = next;
|
||||
continue;
|
||||
}
|
||||
/* -F N-M preserves intermediate delimiters: */
|
||||
//printf "1 2 3 4 5 6 7\n" | toybox cut -O: -F2,4-6,7
|
||||
//2:4 5 6:7
|
||||
if (opt_REGEX && dcount <= cut_list[cl_pos].endpos)
|
||||
continue;
|
||||
// NB: toybox does the above for -f too, but it's a compatibility bug:
|
||||
//printf "1 2 3 4 5 6 7 8\n" | toybox cut -d' ' -O: -f2,4-6,7
|
||||
//2:4 5 6:7 // WRONG!
|
||||
//printf "1 2 3 4 5 6 7 8\n" | cut -d' ' --output-delimiter=: -f2,4-6,7
|
||||
//2:4:5:6:7 // GNU coreutils 9.1
|
||||
}
|
||||
if (end != start || !shoe)
|
||||
printf("%s%.*s", out++ ? odelim : "", end-start, line + start);
|
||||
start = uu;
|
||||
if (!dcount)
|
||||
break;
|
||||
}
|
||||
#if ENABLE_FEATURE_CUT_REGEX
|
||||
if (end != start || !opt_REGEX)
|
||||
#endif
|
||||
{
|
||||
if (first_print) {
|
||||
first_print = 0;
|
||||
printf("%.*s", end - start, line + start);
|
||||
} else
|
||||
printf("%s%.*s", odelim, end - start, line + start);
|
||||
}
|
||||
start = next;
|
||||
//if (dcount == 0)
|
||||
// break; - why?
|
||||
} /* byte loop */
|
||||
}
|
||||
/* if we printed anything, finish with newline */
|
||||
putchar('\n');
|
||||
next_line:
|
||||
linenum++;
|
||||
free(printed);
|
||||
free(orig_line);
|
||||
}
|
||||
free(line);
|
||||
} /* while (got line) */
|
||||
|
||||
/* For -d$'\n' --output-delimiter=^, the overall output is still terminated with \n, not ^ */
|
||||
if (!opt_REGEX && *delim == '\n' && !first_print)
|
||||
putchar('\n');
|
||||
}
|
||||
|
||||
int cut_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
||||
int cut_main(int argc UNUSED_PARAM, char **argv)
|
||||
{
|
||||
/* growable array holding a series of lists */
|
||||
struct cut_list *cut_lists = NULL;
|
||||
unsigned nlists = 0; /* number of elements in above list */
|
||||
char *sopt, *ltok;
|
||||
/* growable array holding a series of ranges */
|
||||
struct cut_range *cut_list = NULL;
|
||||
unsigned nranges = 0; /* number of elements in above list */
|
||||
char *LIST, *ltok;
|
||||
const char *delim = NULL;
|
||||
const char *odelim = NULL;
|
||||
unsigned opt;
|
||||
#if ENABLE_FEATURE_CUT_REGEX
|
||||
regex_t reg;
|
||||
#endif
|
||||
#if ENABLE_LONG_OPTS
|
||||
static const char cut_longopts[] ALIGN1 =
|
||||
"output-delimiter\0" Required_argument "O"
|
||||
;
|
||||
#endif
|
||||
|
||||
#define ARG "bcf"IF_FEATURE_CUT_REGEX("F")
|
||||
opt = getopt32(argv, "^"
|
||||
#if ENABLE_LONG_OPTS
|
||||
opt = getopt32long
|
||||
#else
|
||||
opt = getopt32
|
||||
#endif
|
||||
(argv, "^"
|
||||
OPT_STR // = "b:c:f:d:O:sD"IF_FEATURE_CUT_REGEX("F:")"n"
|
||||
"\0" "b--"ARG":c--"ARG":f--"ARG IF_FEATURE_CUT_REGEX("F--"ARG),
|
||||
&sopt, &sopt, &sopt, &delim, &odelim IF_FEATURE_CUT_REGEX(, &sopt)
|
||||
);
|
||||
if (!delim || !*delim)
|
||||
delim = (opt & CUT_OPT_REGEX_FLGS) ? "[[:space:]]+" : "\t";
|
||||
if (!odelim) odelim = (opt & CUT_OPT_REGEX_FLGS) ? " " : delim;
|
||||
"\0" "b:c:f:" IF_FEATURE_CUT_REGEX("F:") /* one of -bcfF is required */
|
||||
"b--"ARG":c--"ARG":f--"ARG IF_FEATURE_CUT_REGEX(":F--"ARG), /* they are mutually exclusive */
|
||||
IF_LONG_OPTS(cut_longopts,)
|
||||
&LIST, &LIST, &LIST, &delim, &odelim IF_FEATURE_CUT_REGEX(, &LIST)
|
||||
);
|
||||
if (!odelim)
|
||||
odelim = (opt & OPT_REGEX) ? " " : delim;
|
||||
if (!delim)
|
||||
delim = (opt & OPT_REGEX) ? "[[:space:]]+" : "\t";
|
||||
|
||||
// argc -= optind;
|
||||
argv += optind;
|
||||
if (!(opt & (CUT_OPT_BYTE_FLGS | CUT_OPT_CHAR_FLGS | CUT_OPT_FIELDS_FLGS | CUT_OPT_REGEX_FLGS)))
|
||||
bb_simple_error_msg_and_die("expected a list of bytes, characters, or fields");
|
||||
//if (!(opt & (OPT_BYTE | OPT_CHAR | OPT_FIELDS | OPT_REGEX)))
|
||||
// bb_simple_error_msg_and_die("expected a list of bytes, characters, or fields");
|
||||
//^^^ handled by getopt32
|
||||
|
||||
/* non-field (char or byte) cutting has some special handling */
|
||||
if (!(opt & (CUT_OPT_FIELDS_FLGS|CUT_OPT_REGEX_FLGS))) {
|
||||
static const char _op_on_field[] ALIGN1 = " only when operating on fields";
|
||||
|
||||
if (opt & CUT_OPT_SUPPRESS_FLGS) {
|
||||
bb_error_msg_and_die
|
||||
("suppressing non-delimited lines makes sense%s", _op_on_field);
|
||||
}
|
||||
if (opt & CUT_OPT_DELIM_FLGS) {
|
||||
bb_error_msg_and_die
|
||||
("a delimiter may be specified%s", _op_on_field);
|
||||
}
|
||||
/* non-field (char or byte) cutting has some special handling */
|
||||
if (!(opt & (OPT_FIELDS|OPT_REGEX))) {
|
||||
static const char requires_f[] ALIGN1 = " requires -f"
|
||||
IF_FEATURE_CUT_REGEX(" or -F");
|
||||
if (opt & OPT_SUPPRESS)
|
||||
bb_error_msg_and_die("-s%s", requires_f);
|
||||
if (opt & OPT_DELIM)
|
||||
bb_error_msg_and_die("-d DELIM%s", requires_f);
|
||||
}
|
||||
|
||||
/*
|
||||
* parse list and put values into startpos and endpos.
|
||||
* valid list formats: N, N-, N-M, -M
|
||||
* more than one list can be separated by commas
|
||||
* valid range formats: N, N-, N-M, -M
|
||||
* more than one range can be separated by commas
|
||||
*/
|
||||
{
|
||||
/* take apart the ranges, one by one (separated with commas) */
|
||||
while ((ltok = strsep(&LIST, ",")) != NULL) {
|
||||
char *ntok;
|
||||
int s = 0, e = 0;
|
||||
int s, e;
|
||||
|
||||
/* take apart the lists, one by one (they are separated with commas) */
|
||||
while ((ltok = strsep(&sopt, ",")) != NULL) {
|
||||
/* it's actually legal to pass an empty list */
|
||||
//if (!ltok[0])
|
||||
// continue;
|
||||
//^^^ testcase?
|
||||
|
||||
/* it's actually legal to pass an empty list */
|
||||
if (!ltok[0])
|
||||
continue;
|
||||
|
||||
/* get the start pos */
|
||||
ntok = strsep(<ok, "-");
|
||||
if (!ntok[0]) {
|
||||
s = 0;
|
||||
} else {
|
||||
s = xatoi_positive(ntok);
|
||||
/* account for the fact that arrays are zero based, while
|
||||
* the user expects the first char on the line to be char #1 */
|
||||
if (s != 0)
|
||||
s--;
|
||||
}
|
||||
|
||||
/* get the end pos */
|
||||
if (ltok == NULL) {
|
||||
e = s;
|
||||
} else if (!ltok[0]) {
|
||||
e = INT_MAX;
|
||||
} else {
|
||||
e = xatoi_positive(ltok);
|
||||
/* if the user specified and end position of 0,
|
||||
* that means "til the end of the line" */
|
||||
if (!*ltok)
|
||||
e = INT_MAX;
|
||||
else if (e < s)
|
||||
bb_error_msg_and_die("%d<%d", e, s);
|
||||
e--; /* again, arrays are zero based, lines are 1 based */
|
||||
}
|
||||
|
||||
/* add the new list */
|
||||
cut_lists = xrealloc_vector(cut_lists, 4, nlists);
|
||||
/* NB: startpos is always >= 0 */
|
||||
cut_lists[nlists].startpos = s;
|
||||
cut_lists[nlists].endpos = e;
|
||||
nlists++;
|
||||
/* get the start pos */
|
||||
ntok = strsep(<ok, "-");
|
||||
if (!ntok[0]) {
|
||||
if (!ltok) /* testcase: -f '' */
|
||||
bb_show_usage();
|
||||
if (!ltok[0]) /* testcase: -f - */
|
||||
bb_show_usage();
|
||||
s = 0; /* "-M" means "1-M" */
|
||||
} else {
|
||||
/* "N" or "N-[M]" */
|
||||
/* arrays are zero based, while the user expects
|
||||
* the first field/char on the line to be char #1 */
|
||||
s = xatoi_positive(ntok) - 1;
|
||||
}
|
||||
|
||||
/* make sure we got some cut positions out of all that */
|
||||
if (nlists == 0)
|
||||
bb_simple_error_msg_and_die("missing list of positions");
|
||||
/* get the end pos */
|
||||
if (!ltok) {
|
||||
e = s; /* "N" means "N-N" */
|
||||
} else if (!ltok[0]) {
|
||||
/* "N-" means "until the end of the line" */
|
||||
e = INT_MAX;
|
||||
} else {
|
||||
/* again, arrays are zero based, fields are 1 based */
|
||||
e = xatoi_positive(ltok) - 1;
|
||||
}
|
||||
|
||||
/* now that the lists are parsed, we need to sort them to make life
|
||||
* easier on us when it comes time to print the chars / fields / lines
|
||||
*/
|
||||
if (!(opt & CUT_OPT_NOSORT_FLGS))
|
||||
qsort(cut_lists, nlists, sizeof(cut_lists[0]), cmpfunc);
|
||||
if (s < 0 || e < s)
|
||||
bb_error_msg_and_die("invalid range %s-%s", ntok, ltok ?: ntok);
|
||||
|
||||
/* add the new range */
|
||||
cut_list = xrealloc_vector(cut_list, 4, nranges);
|
||||
/* NB: s is always >= 0 */
|
||||
cut_list[nranges].startpos = s;
|
||||
cut_list[nranges].endpos = e;
|
||||
nranges++;
|
||||
}
|
||||
cut_list[nranges].startpos = UINT_MAX; /* end indicator */
|
||||
|
||||
/* make sure we got some cut positions out of all that */
|
||||
//if (nranges == 0)
|
||||
// bb_simple_error_msg_and_die("missing list of positions");
|
||||
//^^^ this is impossible since one of -bcfF is required,
|
||||
// they populate LIST with non-NULL string and when it is parsed,
|
||||
// cut_list[] gets at least one element.
|
||||
|
||||
/* now that the lists are parsed, we need to sort them to make life
|
||||
* easier on us when it comes time to print the chars / fields / lines
|
||||
*/
|
||||
if (!(opt & OPT_NOSORT))
|
||||
qsort(cut_list, nranges, sizeof(cut_list[0]), cmpfunc);
|
||||
|
||||
#if ENABLE_FEATURE_CUT_REGEX
|
||||
if (opt & OPT_REGEX) {
|
||||
xregcomp(®, delim, REG_EXTENDED);
|
||||
delim = (void*) ®
|
||||
}
|
||||
#endif
|
||||
|
||||
{
|
||||
exitcode_t retval = EXIT_SUCCESS;
|
||||
@@ -322,12 +426,12 @@ int cut_main(int argc UNUSED_PARAM, char **argv)
|
||||
retval = EXIT_FAILURE;
|
||||
continue;
|
||||
}
|
||||
cut_file(file, delim, odelim, cut_lists, nlists);
|
||||
cut_file(file, delim, odelim, cut_list);
|
||||
fclose_if_not_stdin(file);
|
||||
} while (*++argv);
|
||||
|
||||
if (ENABLE_FEATURE_CLEAN_UP)
|
||||
free(cut_lists);
|
||||
free(cut_list);
|
||||
fflush_stdout_and_exit(retval);
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -289,7 +289,7 @@ int date_main(int argc UNUSED_PARAM, char **argv)
|
||||
|
||||
/* if setting time, set it */
|
||||
if ((opt & OPT_SET) && clock_settime(CLOCK_REALTIME, &ts) < 0) {
|
||||
bb_simple_perror_msg("can't set date");
|
||||
bb_simple_perror_msg_and_die("can't set date");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+3
-9
@@ -272,12 +272,6 @@ static bool write_and_stats(const void *buf, size_t len, size_t obs,
|
||||
return 1;
|
||||
}
|
||||
|
||||
#if ENABLE_LFS
|
||||
# define XATOU_SFX xatoull_sfx
|
||||
#else
|
||||
# define XATOU_SFX xatoul_sfx
|
||||
#endif
|
||||
|
||||
#if ENABLE_FEATURE_DD_IBS_OBS
|
||||
static int parse_comma_flags(char *val, const char *words, const char *error_in)
|
||||
{
|
||||
@@ -457,15 +451,15 @@ int dd_main(int argc UNUSED_PARAM, char **argv)
|
||||
/* These can be large: */
|
||||
if (what == OP_count) {
|
||||
G.flags |= FLAG_COUNT;
|
||||
count = XATOU_SFX(val, cwbkMG_suffixes);
|
||||
count = XATOOFF_SFX(val, cwbkMG_suffixes);
|
||||
/*continue;*/
|
||||
}
|
||||
if (what == OP_seek) {
|
||||
seek = XATOU_SFX(val, cwbkMG_suffixes);
|
||||
seek = XATOOFF_SFX(val, cwbkMG_suffixes);
|
||||
/*continue;*/
|
||||
}
|
||||
if (what == OP_skip) {
|
||||
skip = XATOU_SFX(val, cwbkMG_suffixes);
|
||||
skip = XATOOFF_SFX(val, cwbkMG_suffixes);
|
||||
/*continue;*/
|
||||
}
|
||||
if (what == OP_if) {
|
||||
|
||||
+5
-2
@@ -155,6 +155,9 @@ int df_main(int argc UNUSED_PARAM, char **argv)
|
||||
, &opt_t
|
||||
IF_FEATURE_DF_FANCY(, &chp)
|
||||
);
|
||||
/* -k overrides $POSIXLY_CORRECT: */
|
||||
if (opt & OPT_KILO)
|
||||
df_disp_hr = 1024;
|
||||
if (opt & OPT_MEGA)
|
||||
df_disp_hr = 1024*1024;
|
||||
|
||||
@@ -185,8 +188,8 @@ int df_main(int argc UNUSED_PARAM, char **argv)
|
||||
if (disp_units_hdr == NULL) {
|
||||
#if ENABLE_FEATURE_HUMAN_READABLE
|
||||
disp_units_hdr = xasprintf("%s-blocks",
|
||||
/* print df_disp_hr, show no fractionals,
|
||||
* use suffixes if OPT_POSIX is set in opt */
|
||||
/* print df_disp_hr; show no fractionals;
|
||||
* if -P, unit=1 (print it in full, no KMG suffixes) */
|
||||
make_human_readable_str(df_disp_hr, 0, !!(opt & OPT_POSIX))
|
||||
);
|
||||
#else
|
||||
|
||||
+1
-1
@@ -136,7 +136,7 @@ static void factorize(wide_t N);
|
||||
|
||||
static half_t isqrt_odd(wide_t N)
|
||||
{
|
||||
half_t s = isqrt(N);
|
||||
half_t s = isqrt_ull(N);
|
||||
/* s^2 is <= N, (s+1)^2 > N */
|
||||
|
||||
/* If s^2 in fact is EQUAL to N, it's very lucky.
|
||||
|
||||
+11
-11
@@ -50,11 +50,11 @@ if test $FLAG_USER_EXISTS = "yes"; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
adduser -s /bin/true -g "" -H -D "$TEST_USER" || exit 1
|
||||
$BUSYBOX adduser -s /bin/true -g "" -H -D "$TEST_USER" || exit 1
|
||||
|
||||
chown $TEST_USER.$TEST_USER $BUSYBOX
|
||||
chown $TEST_USER:$TEST_USER $BUSYBOX
|
||||
chmod u+s $BUSYBOX 2>&1 /dev/null
|
||||
chown $TEST_USER.$TEST_USER $ID
|
||||
chown $TEST_USER:$TEST_USER $ID
|
||||
chmod u+s $ID 2>&1 /dev/null
|
||||
|
||||
echo "test 3 setuid, existing user: id [options] no username"
|
||||
@@ -90,9 +90,9 @@ do
|
||||
done
|
||||
done
|
||||
|
||||
chown $TEST_USER.$TEST_USER $BUSYBOX
|
||||
chown $TEST_USER:$TEST_USER $BUSYBOX
|
||||
chmod g+s $BUSYBOX 2>&1 /dev/null
|
||||
chown $TEST_USER.$TEST_USER $ID
|
||||
chown $TEST_USER:$TEST_USER $ID
|
||||
chmod g+s $ID 2>&1 /dev/null
|
||||
|
||||
echo "test 5 setgid, existing user: id [options] no username"
|
||||
@@ -128,9 +128,9 @@ do
|
||||
done
|
||||
done
|
||||
|
||||
chown $TEST_USER.$TEST_USER $BUSYBOX
|
||||
chown $TEST_USER:$TEST_USER $BUSYBOX
|
||||
chmod u+s,g+s $BUSYBOX 2>&1 /dev/null
|
||||
chown $TEST_USER.$TEST_USER $ID
|
||||
chown $TEST_USER:$TEST_USER $ID
|
||||
chmod u+s,g+s $ID 2>&1 /dev/null
|
||||
|
||||
echo "test 7 setuid, setgid, existing user: id [options] no username"
|
||||
@@ -200,8 +200,8 @@ do
|
||||
done
|
||||
done
|
||||
|
||||
chown .root $BUSYBOX 2>&1 /dev/null
|
||||
chown .root $ID 2>&1 /dev/null
|
||||
chown :root $BUSYBOX 2>&1 /dev/null
|
||||
chown :root $ID 2>&1 /dev/null
|
||||
chmod g+s $BUSYBOX 2>&1 /dev/null
|
||||
chmod g+s $ID 2>&1 /dev/null
|
||||
|
||||
@@ -238,7 +238,7 @@ do
|
||||
done
|
||||
done
|
||||
|
||||
chown root.root $BUSYBOX 2>&1 /dev/null
|
||||
chown root.root $ID 2>&1 /dev/null
|
||||
chown root:root $BUSYBOX 2>&1 /dev/null
|
||||
chown root:root $ID 2>&1 /dev/null
|
||||
rm -f $ID
|
||||
rm -f foo bar
|
||||
+178
-90
@@ -126,6 +126,8 @@
|
||||
//usage: "\n -F Append indicator (one of */=@|) to names"
|
||||
//usage: )
|
||||
//usage: "\n -l Long format"
|
||||
////usage: "\n -g Long format without group column"
|
||||
////TODO: support -G too ("suppress owner column", GNUism)
|
||||
//usage: "\n -i List inode numbers"
|
||||
//usage: "\n -n List numeric UIDs and GIDs instead of names"
|
||||
//usage: "\n -s List allocated blocks"
|
||||
@@ -159,6 +161,8 @@
|
||||
//usage: IF_FEATURE_LS_WIDTH(
|
||||
//usage: "\n -w N Format N columns wide"
|
||||
//usage: )
|
||||
////usage: "\n -Q Double-quote names"
|
||||
////usage: "\n -q Replace unprintable chars with '?'"
|
||||
//usage: IF_FEATURE_LS_COLOR(
|
||||
//usage: "\n --color[={always,never,auto}]"
|
||||
//usage: )
|
||||
@@ -196,27 +200,47 @@ SPLIT_SUBDIR = 2,
|
||||
|
||||
/* -Cadi1l Std options, busybox always supports */
|
||||
/* -gnsxA Std options, busybox always supports */
|
||||
/* -Q GNU option, busybox always supports */
|
||||
/* -k Std option, busybox always supports (by ignoring) */
|
||||
/* It means "for -s, show sizes in kbytes" */
|
||||
/* Seems to only affect "POSIXLY_CORRECT=1 ls -sk" */
|
||||
/* since otherwise -s shows kbytes anyway */
|
||||
/* -Q GNU option, busybox always supports: */
|
||||
/* -Q, --quote-name */
|
||||
/* enclose entry names in double quotes */
|
||||
/* -LHRctur Std options, busybox optionally supports */
|
||||
/* -Fp Std options, busybox optionally supports */
|
||||
/* -SXvhTw GNU options, busybox optionally supports */
|
||||
/* -T WIDTH Ignored (we don't use tabs on output) */
|
||||
/* -Z SELinux mandated option, busybox optionally supports */
|
||||
/* -q Std option, busybox always supports: */
|
||||
/* https://pubs.opengroup.org/onlinepubs/9699919799/utilities/ls.html: */
|
||||
/* Force each instance of non-printable filename characters and */
|
||||
/* <tab> characters to be written as the <question-mark> ('?') */
|
||||
/* character. Implementations may provide this option by default */
|
||||
/* if the output is to a terminal device. */
|
||||
/* -k Std option, busybox always supports (by ignoring) */
|
||||
/* It means "for -s, show sizes in kbytes" */
|
||||
/* Seems to only affect "POSIXLY_CORRECT=1 ls -sk" */
|
||||
/* since otherwise -s shows kbytes anyway */
|
||||
#define ls_options \
|
||||
"Cadi1lgnsxAk" /* 12 opts, total 12 */ \
|
||||
IF_FEATURE_LS_FILETYPES("Fp") /* 2, 14 */ \
|
||||
IF_FEATURE_LS_RECURSIVE("R") /* 1, 15 */ \
|
||||
IF_SELINUX("Z") /* 1, 16 */ \
|
||||
"Q" /* 1, 17 */ \
|
||||
IF_FEATURE_LS_TIMESTAMPS("ctu") /* 3, 20 */ \
|
||||
IF_FEATURE_LS_SORTFILES("SXrv") /* 4, 24 */ \
|
||||
IF_FEATURE_LS_FOLLOWLINKS("LH") /* 2, 26 */ \
|
||||
IF_FEATURE_HUMAN_READABLE("h") /* 1, 27 */ \
|
||||
IF_FEATURE_LS_WIDTH("T:w:") /* 2, 29 */
|
||||
"Cadi1lgnsxA" /* 11 opts, total 11 */ \
|
||||
IF_FEATURE_LS_FILETYPES("Fp") /* 2, 13 */ \
|
||||
IF_FEATURE_LS_RECURSIVE("R") /* 1, 14 */ \
|
||||
IF_SELINUX("Z") /* 1, 15 */ \
|
||||
"Q" /* 1, 16 */ \
|
||||
IF_FEATURE_LS_TIMESTAMPS("ctu") /* 3, 19 */ \
|
||||
IF_FEATURE_LS_SORTFILES("SXrv") /* 4, 23 */ \
|
||||
IF_FEATURE_LS_FOLLOWLINKS("LH") /* 2, 25 */ \
|
||||
IF_FEATURE_HUMAN_READABLE("h") /* 1, 26 */ \
|
||||
IF_FEATURE_LS_WIDTH("T:w:") /* 2, 28 */ \
|
||||
IF_LONG_OPTS("\xff") /* 1, 29 */ \
|
||||
IF_LONG_OPTS("\xfe") /* 1, 30 */ \
|
||||
IF_LONG_OPTS("\xfd::") /* 1, 31 */ \
|
||||
"qk" /* 2, 33 */
|
||||
|
||||
#if ENABLE_LONG_OPTS
|
||||
static const char ls_longopts[] ALIGN1 =
|
||||
"full-time\0" No_argument "\xff"
|
||||
"group-directories-first\0" No_argument "\xfe"
|
||||
IF_FEATURE_LS_COLOR("color\0" Optional_argument "\xfd")
|
||||
;
|
||||
#endif
|
||||
|
||||
enum {
|
||||
OPT_C = (1 << 0),
|
||||
@@ -230,29 +254,31 @@ enum {
|
||||
OPT_s = (1 << 8),
|
||||
OPT_x = (1 << 9),
|
||||
OPT_A = (1 << 10),
|
||||
//OPT_k = (1 << 11),
|
||||
|
||||
OPTBIT_F = 12,
|
||||
OPTBIT_p, /* 13 */
|
||||
OPTBIT_F = 11,
|
||||
OPTBIT_p, /* 12 */
|
||||
OPTBIT_R = OPTBIT_F + 2 * ENABLE_FEATURE_LS_FILETYPES,
|
||||
OPTBIT_Z = OPTBIT_R + 1 * ENABLE_FEATURE_LS_RECURSIVE,
|
||||
OPTBIT_Q = OPTBIT_Z + 1 * ENABLE_SELINUX,
|
||||
OPTBIT_c, /* 17 */
|
||||
OPTBIT_t, /* 18 */
|
||||
OPTBIT_u, /* 19 */
|
||||
OPTBIT_c, /* 16 */
|
||||
OPTBIT_t, /* 17 */
|
||||
OPTBIT_u, /* 18 */
|
||||
OPTBIT_S = OPTBIT_c + 3 * ENABLE_FEATURE_LS_TIMESTAMPS,
|
||||
OPTBIT_X, /* 21 */
|
||||
OPTBIT_r, /* 22 */
|
||||
OPTBIT_v, /* 23 */
|
||||
OPTBIT_X, /* 20 */
|
||||
OPTBIT_r, /* 21 */
|
||||
OPTBIT_v, /* 22 */
|
||||
OPTBIT_L = OPTBIT_S + 4 * ENABLE_FEATURE_LS_SORTFILES,
|
||||
OPTBIT_H, /* 25 */
|
||||
OPTBIT_H, /* 24 */
|
||||
OPTBIT_h = OPTBIT_L + 2 * ENABLE_FEATURE_LS_FOLLOWLINKS,
|
||||
OPTBIT_T = OPTBIT_h + 1 * ENABLE_FEATURE_HUMAN_READABLE,
|
||||
OPTBIT_w, /* 28 */
|
||||
OPTBIT_w, /* 27 */
|
||||
OPTBIT_full_time = OPTBIT_T + 2 * ENABLE_FEATURE_LS_WIDTH,
|
||||
OPTBIT_dirs_first,
|
||||
OPTBIT_color, /* 31 */
|
||||
/* with long opts, we use all 32 bits */
|
||||
OPTBIT_color, /* 30 */
|
||||
OPTBIT_q = OPTBIT_color + 1, /* 31 */
|
||||
OPTBIT_k = OPTBIT_q + 1, /* 32 */
|
||||
/* with all options enabled, we use all 32 bits and even one extra bit! */
|
||||
/* this works because -k is ignored, and getopt32 allows such "ignore" options past 31th bit */
|
||||
|
||||
OPT_F = (1 << OPTBIT_F) * ENABLE_FEATURE_LS_FILETYPES,
|
||||
OPT_p = (1 << OPTBIT_p) * ENABLE_FEATURE_LS_FILETYPES,
|
||||
@@ -274,6 +300,8 @@ enum {
|
||||
OPT_full_time = (1 << OPTBIT_full_time ) * ENABLE_LONG_OPTS,
|
||||
OPT_dirs_first = (1 << OPTBIT_dirs_first) * ENABLE_LONG_OPTS,
|
||||
OPT_color = (1 << OPTBIT_color ) * ENABLE_FEATURE_LS_COLOR,
|
||||
OPT_q = (1 << OPTBIT_q),
|
||||
//-k is ignored: OPT_k = (1 << OPTBIT_k),
|
||||
};
|
||||
|
||||
/*
|
||||
@@ -327,6 +355,7 @@ struct globals {
|
||||
#endif
|
||||
smallint exit_code;
|
||||
smallint show_dirname;
|
||||
smallint tty_out;
|
||||
#if ENABLE_FEATURE_LS_WIDTH
|
||||
unsigned terminal_width;
|
||||
# define G_terminal_width (G.terminal_width)
|
||||
@@ -343,16 +372,21 @@ struct globals {
|
||||
setup_common_bufsiz(); \
|
||||
/* we have to zero it out because of NOEXEC */ \
|
||||
memset(&G, 0, sizeof(G)); \
|
||||
IF_FEATURE_LS_WIDTH(G_terminal_width = TERMINAL_WIDTH;) \
|
||||
IF_FEATURE_LS_WIDTH(G_terminal_width = ~0U;) \
|
||||
IF_FEATURE_LS_TIMESTAMPS(time(&G.current_time_t);) \
|
||||
} while (0)
|
||||
|
||||
#define ESC "\033"
|
||||
|
||||
static int G_isatty(void)
|
||||
{
|
||||
if (!G.tty_out) /* not known yet? */
|
||||
G.tty_out = isatty(STDOUT_FILENO) + 1;
|
||||
return (G.tty_out == 2);
|
||||
}
|
||||
|
||||
/*** Output code ***/
|
||||
|
||||
|
||||
/* FYI type values: 1:fifo 2:char 4:dir 6:blk 8:file 10:link 12:socket
|
||||
* (various wacky OSes: 13:Sun door 14:BSD whiteout 5:XENIX named file
|
||||
* 3/7:multiplexed char/block device)
|
||||
@@ -415,28 +449,6 @@ static char append_char(mode_t mode)
|
||||
}
|
||||
#endif
|
||||
|
||||
static unsigned calc_name_len(const char *name)
|
||||
{
|
||||
unsigned len;
|
||||
uni_stat_t uni_stat;
|
||||
|
||||
// TODO: quote tab as \t, etc, if -Q
|
||||
name = printable_string2(&uni_stat, name);
|
||||
|
||||
if (!(option_mask32 & OPT_Q)) {
|
||||
return uni_stat.unicode_width;
|
||||
}
|
||||
|
||||
len = 2 + uni_stat.unicode_width;
|
||||
while (*name) {
|
||||
if (*name == '"' || *name == '\\') {
|
||||
len++;
|
||||
}
|
||||
name++;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
/* Return the number of used columns.
|
||||
* Note that only columnar output uses return value.
|
||||
* -l and -1 modes don't care.
|
||||
@@ -444,27 +456,86 @@ static unsigned calc_name_len(const char *name)
|
||||
* ls -b (--escape) = octal escapes (although it doesn't look like working)
|
||||
* ls -N (--literal) = not escape at all
|
||||
*/
|
||||
static unsigned calc_name_len(const char *name)
|
||||
{
|
||||
unsigned len;
|
||||
uni_stat_t uni_stat;
|
||||
|
||||
if (!(option_mask32 & (OPT_q|OPT_Q)))
|
||||
return strlen(name);
|
||||
|
||||
if (!(option_mask32 & OPT_Q)) {
|
||||
/* the most likely branch: "ls" to tty (it auto-enables -q behavior) */
|
||||
printable_string2(&uni_stat, name);
|
||||
return uni_stat.unicode_width;
|
||||
}
|
||||
|
||||
len = 2 + strlen(name);
|
||||
while (*name) {
|
||||
unsigned char ch = (unsigned char)*name;
|
||||
if (ch < ' ' || ch > 0x7e) {
|
||||
ch -= 7;
|
||||
if (ch <= 6) {
|
||||
/* quote chars 7..13 as \a,b,t,n,v,f,r */
|
||||
goto two;
|
||||
}
|
||||
/* other chars <32 or >126 as \ooo octal */
|
||||
len += 3;
|
||||
goto next;
|
||||
}
|
||||
if (*name == '"' || *name == '\\') {
|
||||
two:
|
||||
len++;
|
||||
}
|
||||
next:
|
||||
name++;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
static unsigned print_name(const char *name)
|
||||
{
|
||||
unsigned len;
|
||||
uni_stat_t uni_stat;
|
||||
|
||||
// TODO: quote tab as \t, etc, if -Q
|
||||
name = printable_string2(&uni_stat, name);
|
||||
if (!(option_mask32 & (OPT_q|OPT_Q))) {
|
||||
fputs_stdout(name);
|
||||
return strlen(name);
|
||||
}
|
||||
|
||||
if (!(option_mask32 & OPT_Q)) {
|
||||
/* the most likely branch: "ls" to tty (it auto-enables -q behavior) */
|
||||
name = printable_string2(&uni_stat, name);
|
||||
fputs_stdout(name);
|
||||
return uni_stat.unicode_width;
|
||||
}
|
||||
|
||||
len = 2 + uni_stat.unicode_width;
|
||||
len = 2 + strlen(name);
|
||||
putchar('"');
|
||||
while (*name) {
|
||||
if (*name == '"' || *name == '\\') {
|
||||
unsigned char ch = (unsigned char)*name;
|
||||
if (ch < ' ' || ch > 0x7e) {
|
||||
putchar('\\');
|
||||
ch -= 7;
|
||||
if (ch <= 6) {
|
||||
/* quote chars 7..13 as \a,b,t,n,v,f,r */
|
||||
ch = c_escape_conv_str07[1 + 3 * ch];
|
||||
goto two;
|
||||
}
|
||||
/* other chars <32 or >126 as \ooo octal */
|
||||
ch = (unsigned char)*name;
|
||||
putchar('0' + (ch>>6));
|
||||
putchar('0' + ((ch>>3) & 7));
|
||||
ch = '0' + (ch & 7);
|
||||
len += 3;
|
||||
goto put_ch;
|
||||
}
|
||||
if (ch == '"' || ch == '\\') {
|
||||
putchar('\\');
|
||||
two:
|
||||
len++;
|
||||
}
|
||||
putchar(*name);
|
||||
put_ch:
|
||||
putchar(ch);
|
||||
name++;
|
||||
}
|
||||
putchar('"');
|
||||
@@ -646,7 +717,7 @@ static void display_files(struct dnode **dn, unsigned nfiles)
|
||||
unsigned i, ncols, nrows, row, nc;
|
||||
unsigned column;
|
||||
unsigned nexttab;
|
||||
unsigned column_width = 0; /* used only by coulmnal output */
|
||||
unsigned column_width = 0; /* used only by columnar output */
|
||||
|
||||
if (option_mask32 & (OPT_l|OPT_1)) {
|
||||
ncols = 1;
|
||||
@@ -691,6 +762,11 @@ static void display_files(struct dnode **dn, unsigned nfiles)
|
||||
}
|
||||
nexttab = column + column_width;
|
||||
column += display_single(dn[i]);
|
||||
} else {
|
||||
/* if -w999999999, ncols can be very large */
|
||||
//bb_error_msg(" col:%u ncol:%u i:%i", nc, ncols, i); sleep1();
|
||||
/* without "break", we loop millions of times here */
|
||||
break;
|
||||
}
|
||||
}
|
||||
putchar('\n');
|
||||
@@ -1090,25 +1166,11 @@ int ls_main(int argc UNUSED_PARAM, char **argv)
|
||||
/* need to initialize since --color has _an optional_ argument */
|
||||
const char *color_opt = color_str; /* "always" */
|
||||
#endif
|
||||
#if ENABLE_LONG_OPTS
|
||||
static const char ls_longopts[] ALIGN1 =
|
||||
"full-time\0" No_argument "\xff"
|
||||
"group-directories-first\0" No_argument "\xfe"
|
||||
IF_FEATURE_LS_COLOR("color\0" Optional_argument "\xfd")
|
||||
;
|
||||
#endif
|
||||
|
||||
INIT_G();
|
||||
|
||||
init_unicode();
|
||||
|
||||
#if ENABLE_FEATURE_LS_WIDTH
|
||||
/* obtain the terminal width */
|
||||
G_terminal_width = get_terminal_width(STDIN_FILENO);
|
||||
/* go one less... */
|
||||
G_terminal_width--;
|
||||
#endif
|
||||
|
||||
/* process options */
|
||||
opt = getopt32long(argv, "^"
|
||||
ls_options
|
||||
@@ -1144,6 +1206,29 @@ int ls_main(int argc UNUSED_PARAM, char **argv)
|
||||
exit(0);
|
||||
#endif
|
||||
|
||||
/* ftpd secret backdoor? */
|
||||
if (ENABLE_FTPD && applet_name[0] == 'f') {
|
||||
/* dirs first are much nicer */
|
||||
opt = option_mask32 |= OPT_dirs_first;
|
||||
/* don't show SEcontext */
|
||||
IF_SELINUX(opt = option_mask32 &= ~OPT_Z;)
|
||||
/* do not query stdout about size and tty-ness */
|
||||
IF_FEATURE_LS_WIDTH(G_terminal_width = INT_MAX;)
|
||||
G.tty_out = 1; /* not a tty */
|
||||
goto skip_if_ftpd;
|
||||
}
|
||||
|
||||
#if ENABLE_FEATURE_LS_WIDTH
|
||||
if ((int)G_terminal_width < 0) {
|
||||
/* obtain the terminal width */
|
||||
G_terminal_width = get_terminal_width(STDIN_FILENO);
|
||||
/* go one less... */
|
||||
G_terminal_width--;
|
||||
}
|
||||
if (G_terminal_width == 0) /* -w0 */
|
||||
G_terminal_width = INT_MAX; /* "infinite" */
|
||||
#endif
|
||||
|
||||
#if ENABLE_SELINUX
|
||||
if (opt & OPT_Z) {
|
||||
if (!is_selinux_enabled())
|
||||
@@ -1157,7 +1242,7 @@ int ls_main(int argc UNUSED_PARAM, char **argv)
|
||||
char *p = getenv("LS_COLORS");
|
||||
/* LS_COLORS is unset, or (not empty && not "none") ? */
|
||||
if (!p || (p[0] && strcmp(p, "none") != 0)) {
|
||||
if (isatty(STDOUT_FILENO)) {
|
||||
if (G_isatty()) {
|
||||
/* check isatty() last because it's expensive (syscall) */
|
||||
G_show_color = 1;
|
||||
}
|
||||
@@ -1166,23 +1251,28 @@ int ls_main(int argc UNUSED_PARAM, char **argv)
|
||||
if (opt & OPT_color) {
|
||||
if (color_opt[0] == 'n')
|
||||
G_show_color = 0;
|
||||
else switch (index_in_substrings(color_str, color_opt)) {
|
||||
case 3:
|
||||
case 4:
|
||||
case 5:
|
||||
if (!is_TERM_dumb() && isatty(STDOUT_FILENO)) {
|
||||
case 0:
|
||||
case 1:
|
||||
case 2:
|
||||
G_show_color = 1;
|
||||
else if (!G_show_color) {
|
||||
/* if() is not needed, but avoids extra isatty() if G_show_color is already set */
|
||||
/* Check --color=COLOR_OPT and maybe set show_color=1 */
|
||||
switch (index_in_substrings(color_str, color_opt)) {
|
||||
case 3: // auto
|
||||
case 4: // tty
|
||||
case 5: // if-tty
|
||||
if (!is_TERM_dumb() && G_isatty()) {
|
||||
case 0: // always
|
||||
case 1: // yes
|
||||
case 2: // force
|
||||
G_show_color = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
skip_if_ftpd:
|
||||
|
||||
/* sort out which command line options take precedence */
|
||||
if (ENABLE_FEATURE_LS_RECURSIVE && (opt & OPT_d))
|
||||
option_mask32 &= ~OPT_R; /* no recurse if listing only dir */
|
||||
opt = option_mask32 &= ~OPT_R; /* no recurse if listing only dir */
|
||||
if (!(opt & OPT_l)) { /* not -l? */
|
||||
if (ENABLE_FEATURE_LS_TIMESTAMPS && ENABLE_FEATURE_LS_SORTFILES) {
|
||||
/* when to sort by time? -t[cu] sorts by time even with -l */
|
||||
@@ -1190,19 +1280,17 @@ int ls_main(int argc UNUSED_PARAM, char **argv)
|
||||
/* without -l, bare -c or -u enable sort too */
|
||||
/* (with -l, bare -c or -u just select which time to show) */
|
||||
if (opt & (OPT_c|OPT_u)) {
|
||||
option_mask32 |= OPT_t;
|
||||
opt = option_mask32 |= OPT_t;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* choose a display format if one was not already specified by an option */
|
||||
if (!(option_mask32 & (OPT_l|OPT_1|OPT_x|OPT_C)))
|
||||
option_mask32 |= (isatty(STDOUT_FILENO) ? OPT_C : OPT_1);
|
||||
if (!(opt & (OPT_l|OPT_1|OPT_x|OPT_C)))
|
||||
opt = option_mask32 |= (G_isatty() ? OPT_C : OPT_1);
|
||||
|
||||
if (ENABLE_FTPD && applet_name[0] == 'f') {
|
||||
/* ftpd secret backdoor. dirs first are much nicer */
|
||||
option_mask32 |= OPT_dirs_first;
|
||||
}
|
||||
if (!(opt & OPT_q) && G_isatty())
|
||||
opt = option_mask32 |= OPT_q;
|
||||
|
||||
argv += optind;
|
||||
if (!argv[0])
|
||||
|
||||
+51
-21
@@ -23,6 +23,12 @@
|
||||
//config: help
|
||||
//config: Compute and check SHA256 message digest
|
||||
//config:
|
||||
//config:config SHA384SUM
|
||||
//config: bool "sha384sum (7.3 kb)"
|
||||
//config: default y
|
||||
//config: help
|
||||
//config: Compute and check SHA384 message digest
|
||||
//config:
|
||||
//config:config SHA512SUM
|
||||
//config: bool "sha512sum (7.3 kb)"
|
||||
//config: default y
|
||||
@@ -35,13 +41,13 @@
|
||||
//config: help
|
||||
//config: Compute and check SHA3 message digest
|
||||
//config:
|
||||
//config:comment "Common options for md5sum, sha1sum, sha256sum, sha512sum, sha3sum"
|
||||
//config: depends on MD5SUM || SHA1SUM || SHA256SUM || SHA512SUM || SHA3SUM
|
||||
//config:comment "Common options for md5sum, sha1sum, sha256sum, ..., sha3sum"
|
||||
//config: depends on MD5SUM || SHA1SUM || SHA256SUM || SHA384SUM || SHA512SUM || SHA3SUM
|
||||
//config:
|
||||
//config:config FEATURE_MD5_SHA1_SUM_CHECK
|
||||
//config: bool "Enable -c, -s and -w options"
|
||||
//config: default y
|
||||
//config: depends on MD5SUM || SHA1SUM || SHA256SUM || SHA512SUM || SHA3SUM
|
||||
//config: depends on MD5SUM || SHA1SUM || SHA256SUM || SHA384SUM || SHA512SUM || SHA3SUM
|
||||
//config: help
|
||||
//config: Enabling the -c options allows files to be checked
|
||||
//config: against pre-calculated hash values.
|
||||
@@ -51,11 +57,13 @@
|
||||
//applet:IF_SHA1SUM(APPLET_NOEXEC(sha1sum, md5_sha1_sum, BB_DIR_USR_BIN, BB_SUID_DROP, sha1sum))
|
||||
//applet:IF_SHA3SUM(APPLET_NOEXEC(sha3sum, md5_sha1_sum, BB_DIR_USR_BIN, BB_SUID_DROP, sha3sum))
|
||||
//applet:IF_SHA256SUM(APPLET_NOEXEC(sha256sum, md5_sha1_sum, BB_DIR_USR_BIN, BB_SUID_DROP, sha256sum))
|
||||
//applet:IF_SHA384SUM(APPLET_NOEXEC(sha384sum, md5_sha1_sum, BB_DIR_USR_BIN, BB_SUID_DROP, sha384sum))
|
||||
//applet:IF_SHA512SUM(APPLET_NOEXEC(sha512sum, md5_sha1_sum, BB_DIR_USR_BIN, BB_SUID_DROP, sha512sum))
|
||||
|
||||
//kbuild:lib-$(CONFIG_MD5SUM) += md5_sha1_sum.o
|
||||
//kbuild:lib-$(CONFIG_SHA1SUM) += md5_sha1_sum.o
|
||||
//kbuild:lib-$(CONFIG_SHA256SUM) += md5_sha1_sum.o
|
||||
//kbuild:lib-$(CONFIG_SHA384SUM) += md5_sha1_sum.o
|
||||
//kbuild:lib-$(CONFIG_SHA512SUM) += md5_sha1_sum.o
|
||||
//kbuild:lib-$(CONFIG_SHA3SUM) += md5_sha1_sum.o
|
||||
|
||||
@@ -99,6 +107,16 @@
|
||||
//usage: "\n -w Warn about improperly formatted checksum lines"
|
||||
//usage: )
|
||||
//usage:
|
||||
//usage:#define sha384sum_trivial_usage
|
||||
//usage: IF_FEATURE_MD5_SHA1_SUM_CHECK("[-c[sw]] ")"[FILE]..."
|
||||
//usage:#define sha384sum_full_usage "\n\n"
|
||||
//usage: "Print" IF_FEATURE_MD5_SHA1_SUM_CHECK(" or check") " SHA384 checksums"
|
||||
//usage: IF_FEATURE_MD5_SHA1_SUM_CHECK( "\n"
|
||||
//usage: "\n -c Check sums against list in FILEs"
|
||||
//usage: "\n -s Don't output anything, status code shows success"
|
||||
//usage: "\n -w Warn about improperly formatted checksum lines"
|
||||
//usage: )
|
||||
//usage:
|
||||
//usage:#define sha512sum_trivial_usage
|
||||
//usage: IF_FEATURE_MD5_SHA1_SUM_CHECK("[-c[sw]] ")"[FILE]..."
|
||||
//usage:#define sha512sum_full_usage "\n\n"
|
||||
@@ -130,16 +148,18 @@
|
||||
|
||||
enum {
|
||||
/* 4th letter of applet_name is... */
|
||||
HASH_MD5 = 's', /* "md5>s<um" */
|
||||
HASH_SHA1 = '1',
|
||||
HASH_MD5 = 's', /* "md5>s<um" */
|
||||
HASH_SHA1 = '1',
|
||||
HASH_SHA256 = '2',
|
||||
HASH_SHA3 = '3',
|
||||
HASH_SHA3 = '3',
|
||||
HASH_SHA512 = '5',
|
||||
/* unfortunately, sha384sum has the same '3' as sha3 */
|
||||
};
|
||||
|
||||
#define FLAG_SILENT 1
|
||||
#define FLAG_CHECK 2
|
||||
#define FLAG_WARN 4
|
||||
#define FLAG_BINARY 8
|
||||
|
||||
/* This might be useful elsewhere */
|
||||
static unsigned char *hash_bin_to_hex(unsigned char *hash_value,
|
||||
@@ -158,10 +178,11 @@ static unsigned char *hash_bin_to_hex(unsigned char *hash_value,
|
||||
#endif
|
||||
static uint8_t *hash_file(unsigned char *in_buf, const char *filename, unsigned sha3_width)
|
||||
{
|
||||
int src_fd, hash_len, count;
|
||||
int src_fd, count;
|
||||
union _ctx_ {
|
||||
sha3_ctx_t sha3;
|
||||
sha512_ctx_t sha512;
|
||||
sha384_ctx_t sha384;
|
||||
sha256_ctx_t sha256;
|
||||
sha1_ctx_t sha1;
|
||||
md5_ctx_t md5;
|
||||
@@ -183,25 +204,31 @@ static uint8_t *hash_file(unsigned char *in_buf, const char *filename, unsigned
|
||||
md5_begin(&context.md5);
|
||||
update = (void*)md5_hash;
|
||||
final = (void*)md5_end;
|
||||
hash_len = 16;
|
||||
}
|
||||
else if (ENABLE_SHA1SUM && hash_algo == HASH_SHA1) {
|
||||
sha1_begin(&context.sha1);
|
||||
update = (void*)sha1_hash;
|
||||
final = (void*)sha1_end;
|
||||
hash_len = 20;
|
||||
}
|
||||
else if (ENABLE_SHA256SUM && hash_algo == HASH_SHA256) {
|
||||
sha256_begin(&context.sha256);
|
||||
update = (void*)sha256_hash;
|
||||
final = (void*)sha256_end;
|
||||
hash_len = 32;
|
||||
}
|
||||
else if (ENABLE_SHA384SUM
|
||||
&& (ENABLE_SHA3SUM
|
||||
? (applet_name[4] == '8') /* check for "sha384", but do not match "sha3" */
|
||||
: (hash_algo == '3') /* applet_name = "sha3sum" is not possible */
|
||||
)
|
||||
) {
|
||||
sha384_begin(&context.sha384);
|
||||
update = (void*)sha384_hash;
|
||||
final = (void*)sha384_end;
|
||||
}
|
||||
else if (ENABLE_SHA512SUM && hash_algo == HASH_SHA512) {
|
||||
sha512_begin(&context.sha512);
|
||||
update = (void*)sha512_hash;
|
||||
final = (void*)sha512_end;
|
||||
hash_len = 64;
|
||||
}
|
||||
#if ENABLE_SHA3SUM
|
||||
else if (ENABLE_SHA3SUM && hash_algo == HASH_SHA3) {
|
||||
@@ -219,9 +246,7 @@ static uint8_t *hash_file(unsigned char *in_buf, const char *filename, unsigned
|
||||
) {
|
||||
bb_error_msg_and_die("bad -a%u", sha3_width);
|
||||
}
|
||||
sha3_width /= 4;
|
||||
context.sha3.input_block_bytes = 1600/8 - sha3_width;
|
||||
hash_len = sha3_width/2;
|
||||
context.sha3.input_block_bytes = 1600/8 - sha3_width/4;
|
||||
}
|
||||
#endif
|
||||
else {
|
||||
@@ -236,7 +261,7 @@ static uint8_t *hash_file(unsigned char *in_buf, const char *filename, unsigned
|
||||
if (count < 0)
|
||||
bb_perror_msg("can't read '%s'", filename);
|
||||
else /* count == 0 */ {
|
||||
final(&context, in_buf);
|
||||
unsigned hash_len = final(&context, in_buf);
|
||||
hash_value = hash_bin_to_hex(in_buf, hash_len);
|
||||
}
|
||||
}
|
||||
@@ -257,19 +282,24 @@ int md5_sha1_sum_main(int argc UNUSED_PARAM, char **argv)
|
||||
#if ENABLE_SHA3SUM
|
||||
unsigned sha3_width = 224;
|
||||
#endif
|
||||
const char *fmt = "%s %s\n";
|
||||
|
||||
if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK) {
|
||||
/* -b "binary", -t "text" are ignored (shaNNNsum compat) */
|
||||
/* -t per se is a no-op (it means "text mode").
|
||||
* -b sets the '*' binary mode indicator in the output.
|
||||
* -t unsets -b (-bt is the same as -t, which is the same as no option) */
|
||||
/* -s and -w require -c */
|
||||
#if ENABLE_SHA3SUM
|
||||
if (applet_name[3] == HASH_SHA3)
|
||||
flags = getopt32(argv, "^" "scwbta:+" "\0" "s?c:w?c", &sha3_width);
|
||||
if (applet_name[3] == HASH_SHA3 && (!ENABLE_SHA384SUM || applet_name[4] != '8'))
|
||||
flags = getopt32(argv, "^" "scwbta:+" "\0" "t-b:s?c:w?c", &sha3_width);
|
||||
else
|
||||
#endif
|
||||
flags = getopt32(argv, "^" "scwbt" "\0" "s?c:w?c");
|
||||
flags = getopt32(argv, "^" "scwbt" "\0" "t-b:s?c:w?c");
|
||||
if (flags & FLAG_BINARY)
|
||||
fmt = "%s *%s\n";
|
||||
} else {
|
||||
#if ENABLE_SHA3SUM
|
||||
if (applet_name[3] == HASH_SHA3)
|
||||
if (applet_name[3] == HASH_SHA3 && (!ENABLE_SHA384SUM || applet_name[4] != '8'))
|
||||
getopt32(argv, "a:+", &sha3_width);
|
||||
else
|
||||
#endif
|
||||
@@ -351,7 +381,7 @@ int md5_sha1_sum_main(int argc UNUSED_PARAM, char **argv)
|
||||
if (hash_value == NULL) {
|
||||
return_value = EXIT_FAILURE;
|
||||
} else {
|
||||
printf("%s %s\n", hash_value, *argv);
|
||||
printf(fmt, hash_value, *argv);
|
||||
free(hash_value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1155,7 +1155,7 @@ dump_strings(off_t address, off_t end_offset)
|
||||
leading '+' return nonzero and set *OFFSET to the offset it denotes. */
|
||||
|
||||
static int
|
||||
parse_old_offset(const char *s, off_t *offset)
|
||||
parse_old_offset(char *s, off_t *offset)
|
||||
{
|
||||
static const struct suffix_mult Bb[] ALIGN_SUFFIX = {
|
||||
{ "B", 1024 },
|
||||
@@ -1181,7 +1181,8 @@ parse_old_offset(const char *s, off_t *offset)
|
||||
radix = 16;
|
||||
|
||||
*offset = xstrtooff_sfx(s, radix, Bb);
|
||||
if (p) p[0] = '.';
|
||||
if (p)
|
||||
p[0] = '.'; /* undo cheating */
|
||||
|
||||
return (*offset >= 0);
|
||||
}
|
||||
@@ -1234,7 +1235,7 @@ int od_main(int argc UNUSED_PARAM, char **argv)
|
||||
static const uint8_t doxn_address_pad_len_char[] ALIGN1 = {
|
||||
'7', '7', '6', /* '?' */
|
||||
};
|
||||
char *p;
|
||||
const char *p;
|
||||
int pos;
|
||||
p = strchr(doxn, str_A[0]);
|
||||
if (!p)
|
||||
|
||||
+23
-11
@@ -34,27 +34,38 @@
|
||||
|
||||
static void paste_files(FILE** files, int file_cnt, char* delims, int del_cnt)
|
||||
{
|
||||
char *line;
|
||||
char **line = xmalloc(file_cnt * sizeof(char *));
|
||||
char delim;
|
||||
int active_files = file_cnt;
|
||||
int i;
|
||||
|
||||
while (active_files > 0) {
|
||||
int del_idx = 0;
|
||||
int got_line = FALSE;
|
||||
|
||||
for (i = 0; i < file_cnt; ++i) {
|
||||
if (files[i] == NULL)
|
||||
continue;
|
||||
if (files[i]) {
|
||||
line[i] = xmalloc_fgetline(files[i]);
|
||||
if (!line[i]) {
|
||||
fclose_if_not_stdin(files[i]);
|
||||
files[i] = NULL;
|
||||
--active_files;
|
||||
} else {
|
||||
got_line = TRUE;
|
||||
}
|
||||
} else {
|
||||
line[i] = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
line = xmalloc_fgetline(files[i]);
|
||||
if (!line) {
|
||||
fclose_if_not_stdin(files[i]);
|
||||
files[i] = NULL;
|
||||
--active_files;
|
||||
continue;
|
||||
if (!got_line)
|
||||
break;
|
||||
|
||||
for (i = 0; i < file_cnt; ++i) {
|
||||
if (line[i]) {
|
||||
fputs_stdout(line[i]);
|
||||
free(line[i]);
|
||||
}
|
||||
fputs_stdout(line);
|
||||
free(line);
|
||||
delim = '\n';
|
||||
if (i != file_cnt - 1) {
|
||||
delim = delims[del_idx++];
|
||||
@@ -65,6 +76,7 @@ static void paste_files(FILE** files, int file_cnt, char* delims, int del_cnt)
|
||||
fputc(delim, stdout);
|
||||
}
|
||||
}
|
||||
free(line);
|
||||
}
|
||||
|
||||
static void paste_files_separate(FILE** files, char* delims, int del_cnt)
|
||||
|
||||
+1
-1
@@ -354,7 +354,7 @@ static char **print_formatted(char *f, char **argv, int *conv_err)
|
||||
/* Add "ll" if integer modifier, then print */
|
||||
{
|
||||
static const char format_chars[] ALIGN1 = "diouxXfeEgGcs";
|
||||
char *p = strchr(format_chars, *f);
|
||||
char *p = (char*)strchr(format_chars, *f);
|
||||
/* needed - try "printf %" without it */
|
||||
if (p == NULL || *f == '\0') {
|
||||
bb_error_msg("%s: invalid format", direc_start);
|
||||
|
||||
+47
-44
@@ -426,8 +426,7 @@ struct test_statics {
|
||||
/* set only by check_operator(), either to bogus struct
|
||||
* or points to matching operator_t struct. Never NULL. */
|
||||
const struct operator_t *last_operator;
|
||||
gid_t *group_array;
|
||||
int ngroups;
|
||||
struct cached_groupinfo *groupinfo;
|
||||
#if BASH_TEST2
|
||||
bool bash_test2;
|
||||
#endif
|
||||
@@ -440,8 +439,7 @@ extern struct test_statics *BB_GLOBAL_CONST test_ptr_to_statics;
|
||||
#define S (*test_ptr_to_statics)
|
||||
#define args (S.args )
|
||||
#define last_operator (S.last_operator)
|
||||
#define group_array (S.group_array )
|
||||
#define ngroups (S.ngroups )
|
||||
#define groupinfo (S.groupinfo )
|
||||
#define bash_test2 (S.bash_test2 )
|
||||
#define leaving (S.leaving )
|
||||
|
||||
@@ -449,7 +447,6 @@ extern struct test_statics *BB_GLOBAL_CONST test_ptr_to_statics;
|
||||
XZALLOC_CONST_PTR(&test_ptr_to_statics, sizeof(S)); \
|
||||
} while (0)
|
||||
#define DEINIT_S() do { \
|
||||
free(group_array); \
|
||||
free(test_ptr_to_statics); \
|
||||
} while (0)
|
||||
|
||||
@@ -637,62 +634,52 @@ static int binop(void)
|
||||
/*return 1; - NOTREACHED */
|
||||
}
|
||||
|
||||
static void initialize_group_array(void)
|
||||
{
|
||||
group_array = bb_getgroups(&ngroups, NULL);
|
||||
}
|
||||
|
||||
/* Return non-zero if GID is one that we have in our groups list. */
|
||||
//XXX: FIXME: duplicate of existing libbb function?
|
||||
// see toplevel TODO file:
|
||||
// possible code duplication ingroup() and is_a_group_member()
|
||||
static int is_a_group_member(gid_t gid)
|
||||
{
|
||||
int i;
|
||||
|
||||
/* Short-circuit if possible, maybe saving a call to getgroups(). */
|
||||
if (gid == getgid() || gid == getegid())
|
||||
if (gid == get_cached_egid(&groupinfo->egid))
|
||||
return 1;
|
||||
|
||||
if (ngroups == 0)
|
||||
initialize_group_array();
|
||||
|
||||
/* Search through the list looking for GID. */
|
||||
for (i = 0; i < ngroups; i++)
|
||||
if (gid == group_array[i])
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
return is_in_supplementary_groups(groupinfo, gid);
|
||||
}
|
||||
|
||||
|
||||
/* Do the same thing access(2) does, but use the effective uid and gid,
|
||||
and don't make the mistake of telling root that any file is
|
||||
executable. */
|
||||
static int test_eaccess(struct stat *st, int mode)
|
||||
/*
|
||||
* Similar to what access(2) does, but uses the effective uid and gid.
|
||||
* Doesn't make the mistake of telling root that any file is executable.
|
||||
* Returns non-zero if the file is accessible.
|
||||
*/
|
||||
static int test_st_mode(struct stat *st, int mode)
|
||||
{
|
||||
unsigned int euid = geteuid();
|
||||
enum { ANY_IX = S_IXUSR | S_IXGRP | S_IXOTH };
|
||||
unsigned euid;
|
||||
|
||||
if (mode == X_OK) {
|
||||
/* Do we already know with no extra syscalls? */
|
||||
//if (!S_ISREG(st->st_mode))
|
||||
// return 0; /* not a regular file */
|
||||
// ^^^ bash 5.2.15 "test -x" does not check this!
|
||||
if ((st->st_mode & ANY_IX) == 0)
|
||||
return 0; /* no one can execute */
|
||||
if ((st->st_mode & ANY_IX) == ANY_IX)
|
||||
return 1; /* anyone can execute */
|
||||
}
|
||||
|
||||
euid = get_cached_euid(&groupinfo->euid);
|
||||
if (euid == 0) {
|
||||
/* Root can read or write any file. */
|
||||
if (mode != X_OK)
|
||||
return 0;
|
||||
return 1;
|
||||
|
||||
/* Root can execute any file that has any one of the execute
|
||||
* bits set. */
|
||||
if (st->st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (st->st_uid == euid) /* owner */
|
||||
mode = S_IXUSR | S_IXGRP | S_IXOTH;
|
||||
} else if (st->st_uid == euid) /* owner */
|
||||
mode <<= 6;
|
||||
else if (is_a_group_member(st->st_gid))
|
||||
mode <<= 3;
|
||||
|
||||
if (st->st_mode & mode)
|
||||
return 0;
|
||||
|
||||
return -1;
|
||||
return st->st_mode & mode;
|
||||
}
|
||||
|
||||
|
||||
@@ -722,7 +709,7 @@ static int filstat(char *nm, enum token mode)
|
||||
i = W_OK;
|
||||
if (mode == FILEX)
|
||||
i = X_OK;
|
||||
return test_eaccess(&s, i) == 0;
|
||||
return test_st_mode(&s, i);
|
||||
}
|
||||
if (is_file_type(mode)) {
|
||||
if (mode == FILREG)
|
||||
@@ -760,7 +747,7 @@ static int filstat(char *nm, enum token mode)
|
||||
return ((s.st_mode & i) != 0);
|
||||
}
|
||||
if (mode == FILGZ)
|
||||
return s.st_size > 0L;
|
||||
return s.st_size != 0L; /* shorter than "> 0" test */
|
||||
if (mode == FILUID)
|
||||
return s.st_uid == geteuid();
|
||||
if (mode == FILGID)
|
||||
@@ -891,7 +878,7 @@ static number_t primary(enum token n)
|
||||
}
|
||||
|
||||
|
||||
int test_main(int argc, char **argv)
|
||||
int FAST_FUNC test_main2(struct cached_groupinfo *pgroupinfo, int argc, char **argv)
|
||||
{
|
||||
int res;
|
||||
const char *arg0;
|
||||
@@ -924,6 +911,7 @@ int test_main(int argc, char **argv)
|
||||
|
||||
/* We must do DEINIT_S() prior to returning */
|
||||
INIT_S();
|
||||
groupinfo = pgroupinfo;
|
||||
|
||||
#if BASH_TEST2
|
||||
bash_test2 = bt2;
|
||||
@@ -1026,3 +1014,18 @@ int test_main(int argc, char **argv)
|
||||
DEINIT_S();
|
||||
return res;
|
||||
}
|
||||
|
||||
int test_main(int argc, char **argv)
|
||||
{
|
||||
struct cached_groupinfo info;
|
||||
int r;
|
||||
|
||||
info.euid = -1;
|
||||
info.egid = -1;
|
||||
info.ngroups = 0;
|
||||
info.supplementary_array = NULL;
|
||||
r = test_main2(&info, argc, argv);
|
||||
free(info.supplementary_array);
|
||||
|
||||
return r;
|
||||
}
|
||||
+3
-6
@@ -31,15 +31,12 @@
|
||||
int which_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
||||
int which_main(int argc UNUSED_PARAM, char **argv)
|
||||
{
|
||||
char *env_path;
|
||||
const char *env_path;
|
||||
int status = 0;
|
||||
/* This sizeof(): bb_default_root_path is shorter than BB_PATH_ROOT_PATH */
|
||||
char buf[sizeof(BB_PATH_ROOT_PATH)];
|
||||
|
||||
env_path = getenv("PATH");
|
||||
if (!env_path)
|
||||
/* env_path must be writable, and must not alloc, so... */
|
||||
env_path = strcpy(buf, bb_default_root_path);
|
||||
env_path = bb_default_root_path;
|
||||
|
||||
getopt32(argv, "^" "a" "\0" "-1"/*at least one arg*/);
|
||||
argv += optind;
|
||||
@@ -54,7 +51,7 @@ int which_main(int argc UNUSED_PARAM, char **argv)
|
||||
puts(*argv);
|
||||
}
|
||||
} else {
|
||||
char *path;
|
||||
const char *path;
|
||||
char *p;
|
||||
|
||||
path = env_path;
|
||||
|
||||
@@ -56,6 +56,11 @@ roughly are:
|
||||
(although usually xfunc_error_retval's state is not a problem).
|
||||
* do not expect that stdio wasn't used before. Calling set[v]buf()
|
||||
can be disastrous.
|
||||
* the parent program's signal handlers will not be reset to SIG_DFL
|
||||
(on exec, they are, but we avoid exactly that).
|
||||
This means that signals will cause a part of _parent's code_
|
||||
to execute, and may cause EINTR returns from syscalls
|
||||
in the NOEXEC applet code.
|
||||
* ...
|
||||
|
||||
NOEXEC applets save only one half of fork+exec overhead.
|
||||
@@ -76,7 +81,7 @@ This poses much more serious limitations on what applet can do:
|
||||
hush shell only handles signals (like ^C) after you return
|
||||
from APPLET_main().
|
||||
* do not ever exit() or exec().
|
||||
- xfuncs are okay. They are using special trick to return
|
||||
- xfuncs are okay. They use special trick to return
|
||||
to the caller applet instead of dying when they detect "x" condition.
|
||||
- you may "exit" to caller applet by calling xfunc_die(). Return value
|
||||
is taken from xfunc_error_retval.
|
||||
|
||||
+1
-3
@@ -423,13 +423,11 @@ static int wait_one(int flags)
|
||||
/* if (G.noexecute) { already returned -1; } */
|
||||
|
||||
while (1) {
|
||||
pid = waitpid(-1, &status, flags);
|
||||
pid = safe_waitpid(-1, &status, flags);
|
||||
kill_all_if_got_signal();
|
||||
if (pid == 0) /* flags == WNOHANG and no children exited */
|
||||
return -1;
|
||||
if (pid < 0) {
|
||||
if (errno == EINTR)
|
||||
continue;
|
||||
if (errno == ECHILD) { /* paranoia */
|
||||
bb_simple_error_msg("wait: no more children");
|
||||
return -1;
|
||||
|
||||
+34
-40
@@ -583,14 +583,10 @@ static const char vValues[] ALIGN1 =
|
||||
static const uint16_t PRIMES[] ALIGN2 = { 251, 1021, 4093, 16381, 65521 };
|
||||
|
||||
|
||||
/* Globals. Split in two parts so that first one is addressed
|
||||
* with (mostly short) negative offsets.
|
||||
* NB: it's unsafe to put members of type "double"
|
||||
* into globals2 (gcc may fail to align them).
|
||||
*/
|
||||
struct globals {
|
||||
double t_double;
|
||||
chain beginseq, mainseq, endseq;
|
||||
|
||||
double t_double;
|
||||
chain *seq;
|
||||
node *break_ptr, *continue_ptr;
|
||||
xhash *ahash; /* argument names, used only while parsing function bodies */
|
||||
@@ -618,9 +614,8 @@ struct globals {
|
||||
smallint next_token__concat_inserted;
|
||||
uint32_t next_token__save_tclass;
|
||||
uint32_t next_token__save_info;
|
||||
};
|
||||
struct globals2 {
|
||||
uint32_t t_info; /* often used */
|
||||
|
||||
uint32_t t_info;
|
||||
uint32_t t_tclass;
|
||||
char *t_string;
|
||||
int t_lineno;
|
||||
@@ -654,41 +649,40 @@ struct globals2 {
|
||||
|
||||
char g_buf[MAXVARFMT + 1];
|
||||
};
|
||||
#define G1 (ptr_to_globals[-1])
|
||||
#define G (*(struct globals2 *)ptr_to_globals)
|
||||
#define G (*OFFSET_PTR_TO_GLOBALS)
|
||||
/* For debug. nm --size-sort awk.o | grep -vi ' [tr] ' */
|
||||
//char G1size[sizeof(G1)]; // 0x70
|
||||
//char Gsize[sizeof(G)]; // 0x2f8
|
||||
/* Trying to keep most of members accessible with short offsets: */
|
||||
//char Gofs_seed[offsetof(struct globals2, evaluate__seed)]; // 0x7c
|
||||
#define t_double (G1.t_double )
|
||||
#define beginseq (G1.beginseq )
|
||||
#define mainseq (G1.mainseq )
|
||||
#define endseq (G1.endseq )
|
||||
#define seq (G1.seq )
|
||||
#define break_ptr (G1.break_ptr )
|
||||
#define continue_ptr (G1.continue_ptr)
|
||||
#define ahash (G1.ahash )
|
||||
#define fnhash (G1.fnhash )
|
||||
#define vhash (G1.vhash )
|
||||
#define t_double (G.t_double )
|
||||
#define beginseq (G.beginseq )
|
||||
#define mainseq (G.mainseq )
|
||||
#define endseq (G.endseq )
|
||||
#define seq (G.seq )
|
||||
#define break_ptr (G.break_ptr )
|
||||
#define continue_ptr (G.continue_ptr)
|
||||
#define ahash (G.ahash )
|
||||
#define fnhash (G.fnhash )
|
||||
#define vhash (G.vhash )
|
||||
#define fdhash ahash
|
||||
//^^^^^^^^^^^^^^^^^^ ahash is cleared after every function parsing,
|
||||
// and ends up empty after parsing phase. Thus, we can simply reuse it
|
||||
// for fdhash in execution stage.
|
||||
#define g_progname (G1.g_progname )
|
||||
#define g_lineno (G1.g_lineno )
|
||||
#define num_fields (G1.num_fields )
|
||||
#define num_alloc_fields (G1.num_alloc_fields)
|
||||
#define Fields (G1.Fields )
|
||||
#define g_pos (G1.g_pos )
|
||||
#define g_saved_ch (G1.g_saved_ch )
|
||||
#define got_program (G1.got_program )
|
||||
#define icase (G1.icase )
|
||||
#define exiting (G1.exiting )
|
||||
#define nextrec (G1.nextrec )
|
||||
#define nextfile (G1.nextfile )
|
||||
#define is_f0_split (G1.is_f0_split )
|
||||
#define t_rollback (G1.t_rollback )
|
||||
#define g_progname (G.g_progname )
|
||||
#define g_lineno (G.g_lineno )
|
||||
#define num_fields (G.num_fields )
|
||||
#define num_alloc_fields (G.num_alloc_fields)
|
||||
#define Fields (G.Fields )
|
||||
#define g_pos (G.g_pos )
|
||||
#define g_saved_ch (G.g_saved_ch )
|
||||
#define got_program (G.got_program )
|
||||
#define icase (G.icase )
|
||||
#define exiting (G.exiting )
|
||||
#define nextrec (G.nextrec )
|
||||
#define nextfile (G.nextfile )
|
||||
#define is_f0_split (G.is_f0_split )
|
||||
#define t_rollback (G.t_rollback )
|
||||
#define t_info (G.t_info )
|
||||
#define t_tclass (G.t_tclass )
|
||||
#define t_string (G.t_string )
|
||||
@@ -699,7 +693,7 @@ struct globals2 {
|
||||
#define rsplitter (G.rsplitter )
|
||||
#define g_buf (G.g_buf )
|
||||
#define INIT_G() do { \
|
||||
SET_PTR_TO_GLOBALS((char*)xzalloc(sizeof(G1)+sizeof(G)) + sizeof(G1)); \
|
||||
SET_OFFSET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
|
||||
t_tclass = TC_NEWLINE; \
|
||||
G.evaluate__seed = 1; \
|
||||
} while (0)
|
||||
@@ -1168,9 +1162,9 @@ static int istrue(var *v)
|
||||
*/
|
||||
static uint32_t next_token(uint32_t expected)
|
||||
{
|
||||
#define concat_inserted (G1.next_token__concat_inserted)
|
||||
#define save_tclass (G1.next_token__save_tclass)
|
||||
#define save_info (G1.next_token__save_info)
|
||||
#define concat_inserted (G.next_token__concat_inserted)
|
||||
#define save_tclass (G.next_token__save_tclass)
|
||||
#define save_info (G.next_token__save_info)
|
||||
|
||||
char *p;
|
||||
const char *tl;
|
||||
@@ -2847,7 +2841,7 @@ static NOINLINE var *exec_builtin(node *op, var *res)
|
||||
l = strlen(as[0]) - ll;
|
||||
if (ll > 0 && l >= 0) {
|
||||
if (!icase) {
|
||||
char *s = strstr(as[0], as[1]);
|
||||
const char *s = strstr(as[0], as[1]);
|
||||
if (s)
|
||||
n = (s - as[0]) + 1;
|
||||
} else {
|
||||
|
||||
+23
-13
@@ -18,13 +18,21 @@
|
||||
//kbuild:lib-$(CONFIG_CMP) += cmp.o
|
||||
|
||||
//usage:#define cmp_trivial_usage
|
||||
//usage: "[-ls] [-n NUM] FILE1 [FILE2" IF_DESKTOP(" [SKIP1 [SKIP2]]") "]"
|
||||
//usage: "[-l|s] [-n NUM] FILE1 [FILE2" IF_DESKTOP(" [SKIP1 [SKIP2]]") "]"
|
||||
//usage:#define cmp_full_usage "\n\n"
|
||||
//usage: "Compare FILE1 with FILE2 (or stdin)\n"
|
||||
//usage: "\n -l Write the byte numbers (decimal) and values (octal)"
|
||||
//usage: "\n for all differing bytes"
|
||||
//usage: "\n -l Show decimal offset and octal byte value for differing bytes,"
|
||||
//usage: "\n don't stop on first mismatch"
|
||||
//usage: "\n -s Quiet"
|
||||
//usage: "\n -n NUM Compare at most NUM bytes"
|
||||
//TODO?
|
||||
// -b, --print-bytes print differing bytes
|
||||
//Prints the first difference:
|
||||
//FILE1 FILE2 differ: byte 5, line 2 is <OCTAL1> <char1> <OCTAL2> <char2>
|
||||
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -b adds this
|
||||
//and exits with exitcode 1.
|
||||
// -i, --ignore-initial=SKIP1[:SKIP2]
|
||||
//Same as SKIP1 [SKIP2] in argv, but here SKIP2 defaults to SKIP1, not to zero.
|
||||
|
||||
/* BB_AUDIT SUSv3 (virtually) compliant -- uses nicer GNU format for -l. */
|
||||
/* http://www.opengroup.org/onlinepubs/007904975/utilities/cmp.html */
|
||||
@@ -32,9 +40,9 @@
|
||||
#include "libbb.h"
|
||||
|
||||
static const char fmt_eof[] ALIGN1 = "cmp: EOF on %s\n";
|
||||
static const char fmt_differ[] ALIGN1 = "%s %s differ: char %"OFF_FMT"u, line %u\n";
|
||||
// This fmt_l_opt uses gnu-isms. SUSv3 would be "%.0s%.0s%"OFF_FMT"u %o %o\n"
|
||||
static const char fmt_l_opt[] ALIGN1 = "%.0s%.0s%"OFF_FMT"u %3o %3o\n";
|
||||
static const char fmt_differ[] ALIGN1 = "%s %s differ: byte %llu, line %u\n";
|
||||
// This fmt_l_opt is gnu-ism. SUSv3 is "%.0s%.0s%llu %o %o\n"
|
||||
static const char fmt_l_opt[] ALIGN1 = "%.0s%.0s%llu %3o %3o\n";
|
||||
|
||||
#define OPT_STR "sln:+"
|
||||
#define CMP_OPT_s (1<<0)
|
||||
@@ -46,7 +54,7 @@ int cmp_main(int argc UNUSED_PARAM, char **argv)
|
||||
{
|
||||
FILE *fp1, *fp2, *outfile = stdout;
|
||||
const char *filename1, *filename2 = "-";
|
||||
off_t skip1 = 0, skip2 = 0, char_pos = 0;
|
||||
unsigned long long skip1 = 0, skip2 = 0, char_pos = 0;
|
||||
int line_pos = 1; /* Hopefully won't overflow... */
|
||||
const char *fmt;
|
||||
int c1, c2;
|
||||
@@ -61,6 +69,7 @@ int cmp_main(int argc UNUSED_PARAM, char **argv)
|
||||
IF_DESKTOP(":?4")
|
||||
IF_NOT_DESKTOP(":?2")
|
||||
":l--s:s--l",
|
||||
//TODO: -n MAXCOUNT should allow KMG suffixes
|
||||
&max_count
|
||||
);
|
||||
#else
|
||||
@@ -86,9 +95,9 @@ int cmp_main(int argc UNUSED_PARAM, char **argv)
|
||||
if (*++argv) {
|
||||
filename2 = *argv;
|
||||
if (ENABLE_DESKTOP && *++argv) {
|
||||
skip1 = XATOOFF(*argv);
|
||||
skip1 = xatoull_sfx(*argv, kmg_i_suffixes);
|
||||
if (*++argv) {
|
||||
skip2 = XATOOFF(*argv);
|
||||
skip2 = xatoull_sfx(*argv, kmg_i_suffixes);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -113,8 +122,8 @@ int cmp_main(int argc UNUSED_PARAM, char **argv)
|
||||
fmt = fmt_differ;
|
||||
|
||||
if (ENABLE_DESKTOP) {
|
||||
while (skip1) { getc(fp1); skip1--; }
|
||||
while (skip2) { getc(fp2); skip2--; }
|
||||
while (skip1) { if (getc(fp1) == EOF) break; skip1--; }
|
||||
while (skip2) { if (getc(fp2) == EOF) break; skip2--; }
|
||||
}
|
||||
do {
|
||||
if (max_count >= 0 && --max_count < 0)
|
||||
@@ -146,8 +155,9 @@ int cmp_main(int argc UNUSED_PARAM, char **argv)
|
||||
line_pos = c1; /* line_pos is unused in the -l case. */
|
||||
}
|
||||
fprintf(outfile, fmt, filename1, filename2, char_pos, line_pos, c2);
|
||||
if (opt) { /* This must be -l since not -s. */
|
||||
/* If we encountered an EOF,
|
||||
if (opt & CMP_OPT_l) {
|
||||
/* -l: do not stop on first mismatch.
|
||||
* If we encountered an EOF,
|
||||
* the while check will catch it. */
|
||||
continue;
|
||||
}
|
||||
|
||||
+1
-1
@@ -741,7 +741,7 @@ static int diffreg(char *file[2])
|
||||
fd = fd_tmp;
|
||||
xlseek(fd, 0, SEEK_SET);
|
||||
}
|
||||
fp[i] = fdopen(fd, "r");
|
||||
fp[i] = xfdopen_for_read(fd);
|
||||
}
|
||||
|
||||
setup_common_bufsiz();
|
||||
|
||||
@@ -345,6 +345,8 @@ static int insertLine(int num, const char *data, int len)
|
||||
lp->prev->next = newLp;
|
||||
lp->prev = newLp;
|
||||
|
||||
if (num <= curNum)
|
||||
curLine = curLine->prev;
|
||||
lastNum++;
|
||||
dirty = TRUE;
|
||||
return setCurNum(num);
|
||||
|
||||
+2
-3
@@ -657,9 +657,8 @@ static void add_cmd(const char *cmdstr)
|
||||
|
||||
/* Append this line to any unfinished line from last time. */
|
||||
if (G.add_cmd_line) {
|
||||
char *tp = xasprintf("%s\n%s", G.add_cmd_line, cmdstr);
|
||||
free(G.add_cmd_line);
|
||||
cmdstr = G.add_cmd_line = tp;
|
||||
cmdstr = xasprintf_inplace(G.add_cmd_line,
|
||||
"%s\n%s", G.add_cmd_line, cmdstr);
|
||||
}
|
||||
|
||||
/* If this line ends with unescaped backslash, request next line. */
|
||||
|
||||
+127
-129
@@ -555,7 +555,6 @@ static int crashme = 0;
|
||||
#endif
|
||||
|
||||
static void show_status_line(void); // put a message on the bottom line
|
||||
static void status_line_bold(const char *, ...);
|
||||
|
||||
static void show_help(void)
|
||||
{
|
||||
@@ -1097,11 +1096,15 @@ static void redraw(int full_screen)
|
||||
//----- Flash the screen --------------------------------------
|
||||
static void flash(int h)
|
||||
{
|
||||
standout_start();
|
||||
redraw(TRUE);
|
||||
//standout_start();
|
||||
//redraw(TRUE);
|
||||
write1(ESC"[?5h"); // "reverse screen on"
|
||||
|
||||
mysleep(h);
|
||||
standout_end();
|
||||
redraw(TRUE);
|
||||
|
||||
//standout_end();
|
||||
//redraw(TRUE);
|
||||
write1(ESC"[?5l"); // "reverse screen off"
|
||||
}
|
||||
|
||||
static void indicate_error(void)
|
||||
@@ -1336,11 +1339,10 @@ static void show_status_line(void)
|
||||
go_bottom_and_clear_to_eol();
|
||||
write1(status_buffer);
|
||||
if (have_status_msg) {
|
||||
if (((int)strlen(status_buffer) - (have_status_msg - 1)) >
|
||||
(columns - 1) ) {
|
||||
have_status_msg = 0;
|
||||
int n = (int)strlen(status_buffer) - (have_status_msg - 1);
|
||||
// careful with int->unsigned promotion in comparison!
|
||||
if (n >= 0 && n >= columns)
|
||||
Hit_Return();
|
||||
}
|
||||
have_status_msg = 0;
|
||||
}
|
||||
place_cursor(crow, ccol); // put cursor back in correct place
|
||||
@@ -2601,7 +2603,7 @@ static char *get_one_address(char *p, int *result, int *valid)
|
||||
|
||||
// Read line addresses for a colon command. The user can enter as
|
||||
// many as they like but only the last two will be used.
|
||||
static char *get_address(char *p, int *b, int *e, unsigned int *got)
|
||||
static char *get_address(char *p, int *b, int *e, unsigned *got)
|
||||
{
|
||||
int state = GET_ADDRESS;
|
||||
int valid;
|
||||
@@ -2682,19 +2684,23 @@ static void setops(char *args, int flg_no)
|
||||
# if ENABLE_FEATURE_VI_COLON_EXPAND
|
||||
static char *expand_args(char *args)
|
||||
{
|
||||
char *s, *t;
|
||||
char *s;
|
||||
const char *replace;
|
||||
|
||||
args = xstrdup(args);
|
||||
for (s = args; *s; s++) {
|
||||
unsigned n;
|
||||
|
||||
if (*s == '%') {
|
||||
replace = current_filename;
|
||||
} else if (*s == '#') {
|
||||
replace = alt_filename;
|
||||
} else {
|
||||
if (*s == '\\' && s[1] != '\0') {
|
||||
for (t = s++; *t; t++)
|
||||
char *t;
|
||||
for (t = s; *t; t++)
|
||||
*t = t[1];
|
||||
s++;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
@@ -2705,11 +2711,9 @@ static char *expand_args(char *args)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
*s = '\0';
|
||||
t = xasprintf("%s%s%s", args, replace, s+1);
|
||||
s = t + (s - args) + strlen(replace);
|
||||
free(args);
|
||||
args = t;
|
||||
n = (s - args);
|
||||
xasprintf_inplace(args, "%.*s%s%s", n, args, replace, s+1);
|
||||
s = args + n + strlen(replace);
|
||||
}
|
||||
return args;
|
||||
}
|
||||
@@ -2787,7 +2791,6 @@ static char *regex_search(char *q, regex_t *preg, const char *Rorig,
|
||||
# define strchr_backslash(s, c) strchr(s, c)
|
||||
#endif /* ENABLE_FEATURE_VI_REGEX_SEARCH */
|
||||
|
||||
// buf must be no longer than MAX_INPUT_LEN!
|
||||
static void colon(char *buf)
|
||||
{
|
||||
#if !ENABLE_FEATURE_VI_COLON
|
||||
@@ -2847,16 +2850,17 @@ static void colon(char *buf)
|
||||
}
|
||||
not_implemented(p);
|
||||
#else
|
||||
|
||||
char cmd[sizeof("features!")]; // longest known command + NUL
|
||||
char *args;
|
||||
int cmdlen;
|
||||
char *useforce;
|
||||
char *q, *r;
|
||||
int b, e;
|
||||
// check how many addresses we got
|
||||
# define GOT_ADDRESS (got & 1)
|
||||
# define GOT_RANGE ((got & 3) == 3)
|
||||
|
||||
char c, *buf1, *q, *r;
|
||||
char *fn, cmd[MAX_INPUT_LEN], *cmdend, *args, *exp = NULL;
|
||||
int i, l, li, b, e;
|
||||
unsigned int got;
|
||||
int useforce;
|
||||
# define GOT_RANGE ((got & 3) == 3)
|
||||
unsigned got;
|
||||
char *exp = NULL; // may hold expand_args() result: if VI_COLON_EXPAND, needs freeing!
|
||||
|
||||
// :3154 // if (-e line 3154) goto it else stay put
|
||||
// :4,33w! foo // write a portion of buffer to file "foo"
|
||||
@@ -2871,52 +2875,38 @@ static void colon(char *buf)
|
||||
// :/xyz/ // goto the "xyz" line
|
||||
// :s/find/replace/ // substitute pattern "find" with "replace"
|
||||
// :!<cmd> // run <cmd> then return
|
||||
//
|
||||
|
||||
while (*buf == ':')
|
||||
buf++; // move past leading colons
|
||||
while (isblank(*buf))
|
||||
buf++; // move past leading blanks
|
||||
if (!buf[0] || buf[0] == '"')
|
||||
buf = skip_whitespace(buf); // move past leading blanks
|
||||
if (!*buf || *buf == '"')
|
||||
goto ret; // ignore empty lines or those starting with '"'
|
||||
|
||||
li = i = 0;
|
||||
// look for optional address(es) ":." ":1" ":1,9" ":'q,'a" ":%"
|
||||
b = e = -1;
|
||||
got = 0;
|
||||
li = count_lines(text, end - 1);
|
||||
fn = current_filename;
|
||||
|
||||
// look for optional address(es) :. :1 :1,9 :'q,'a :%
|
||||
buf = get_address(buf, &b, &e, &got);
|
||||
if (buf == NULL) {
|
||||
if (buf == NULL)
|
||||
goto ret;
|
||||
}
|
||||
|
||||
// get the COMMAND into cmd[]
|
||||
strcpy(cmd, buf);
|
||||
buf1 = cmd;
|
||||
while (!isspace(*buf1) && *buf1 != '\0') {
|
||||
buf1++;
|
||||
}
|
||||
cmdend = buf1;
|
||||
// get any ARGuments
|
||||
while (isblank(*buf1))
|
||||
buf1++;
|
||||
args = buf1;
|
||||
*cmdend = '\0';
|
||||
useforce = FALSE;
|
||||
if (cmdend > cmd && cmdend[-1] == '!') {
|
||||
useforce = TRUE;
|
||||
cmdend[-1] = '\0'; // get rid of !
|
||||
}
|
||||
safe_strncpy(cmd, buf, sizeof(cmd));
|
||||
skip_non_whitespace(cmd)[0] = '\0';
|
||||
useforce = last_char_is(cmd, '!');
|
||||
if (useforce && useforce > cmd)
|
||||
*useforce = '\0'; // "CMD!" -> "CMD" (unless single "!")
|
||||
// find ARGuments
|
||||
args = skip_whitespace(skip_non_whitespace(buf));
|
||||
|
||||
// assume the command will want a range, certain commands
|
||||
// (read, substitute) need to adjust these assumptions
|
||||
if (!GOT_ADDRESS) {
|
||||
q = text; // no addr, use 1,$ for the range
|
||||
r = end - 1;
|
||||
} else {
|
||||
// at least one addr was given, get its details
|
||||
if (e < 0 || e > li) {
|
||||
q = text; // if no addr, use 1,$ for the range
|
||||
r = end - 1;
|
||||
if (GOT_ADDRESS) { // at least one addr was given, get its details
|
||||
int lines;
|
||||
if (e < 0
|
||||
|| e > (lines = count_lines(text, end - 1))
|
||||
) {
|
||||
status_line_bold("Invalid range");
|
||||
goto ret;
|
||||
}
|
||||
@@ -2926,31 +2916,32 @@ static void colon(char *buf)
|
||||
// number of the single line the user wants.
|
||||
// Reset the end pointer to the end of that line.
|
||||
r = end_line(q);
|
||||
li = 1;
|
||||
} else {
|
||||
// we were given two addrs. change the
|
||||
// start pointer to the addr given by user.
|
||||
if (b < 0 || b > li || b > e) {
|
||||
if (b < 0 || b > lines || b > e) {
|
||||
status_line_bold("Invalid range");
|
||||
goto ret;
|
||||
}
|
||||
q = find_line(b); // what line is #b
|
||||
r = end_line(r);
|
||||
li = e - b + 1;
|
||||
}
|
||||
}
|
||||
// ------------ now look for the command ------------
|
||||
i = strlen(cmd);
|
||||
if (i == 0) { // :123CR goto line #123
|
||||
cmdlen = strlen(cmd);
|
||||
if (cmdlen == 0) { // ":123<enter>" - goto line #123
|
||||
if (e >= 0) {
|
||||
dot = find_line(e); // what line is #e
|
||||
dot_skip_over_ws();
|
||||
}
|
||||
}
|
||||
# if ENABLE_FEATURE_ALLOW_EXEC
|
||||
else if (cmd[0] == '!') { // run a cmd
|
||||
else if (cmd[0] == '!') { // ":!CMD" - run shell CMD
|
||||
int retcode;
|
||||
// :!ls run the <cmd>
|
||||
if (GOT_ADDRESS) {
|
||||
status_line_bold("Range not allowed");
|
||||
goto ret;
|
||||
}
|
||||
exp = expand_args(buf + 1);
|
||||
if (exp == NULL)
|
||||
goto ret;
|
||||
@@ -2968,25 +2959,27 @@ static void colon(char *buf)
|
||||
e = count_lines(text, dot);
|
||||
}
|
||||
status_line("%d", e);
|
||||
} else if (strncmp(cmd, "delete", i) == 0) { // delete lines
|
||||
} else if (strncmp(cmd, "delete", cmdlen) == 0) { // delete lines
|
||||
if (!GOT_ADDRESS) { // no addr given- use defaults
|
||||
q = begin_line(dot); // assume .,. for the range
|
||||
r = end_line(dot);
|
||||
}
|
||||
dot = yank_delete(q, r, WHOLE, YANKDEL, ALLOW_UNDO); // save, then delete lines
|
||||
dot_skip_over_ws();
|
||||
} else if (strncmp(cmd, "edit", i) == 0) { // Edit a file
|
||||
} else if (strncmp(cmd, "edit", cmdlen) == 0) { // Edit a file
|
||||
int size;
|
||||
char *fn;
|
||||
|
||||
// don't edit, if the current file has been modified
|
||||
if (modified_count && !useforce) {
|
||||
status_line_bold("No write since last change (:%s! overrides)", cmd);
|
||||
goto ret;
|
||||
}
|
||||
fn = current_filename;
|
||||
if (args[0]) {
|
||||
// the user supplied a file name
|
||||
fn = exp = expand_args(args);
|
||||
if (exp == NULL)
|
||||
fn = expand_args(args);
|
||||
if (fn == NULL)
|
||||
goto ret;
|
||||
} else if (current_filename == NULL) {
|
||||
// no user file name, no current name- punt
|
||||
@@ -3006,8 +2999,6 @@ static void colon(char *buf)
|
||||
reg[YDreg] = NULL;
|
||||
}
|
||||
# endif
|
||||
// how many lines in text[]?
|
||||
li = count_lines(text, end - 1);
|
||||
status_line("'%s'%s"
|
||||
IF_FEATURE_VI_READONLY("%s")
|
||||
" %uL, %uC",
|
||||
@@ -3016,9 +3007,10 @@ static void colon(char *buf)
|
||||
IF_FEATURE_VI_READONLY(
|
||||
((readonly_mode) ? " [Readonly]" : ""),
|
||||
)
|
||||
li, (int)(end - text)
|
||||
count_lines(text, end - 1),
|
||||
(int)(end - text)
|
||||
);
|
||||
} else if (strncmp(cmd, "file", i) == 0) { // what File is this
|
||||
} else if (strncmp(cmd, "file", cmdlen) == 0) { // what File is this
|
||||
if (e >= 0) {
|
||||
status_line_bold("No address allowed on this command");
|
||||
goto ret;
|
||||
@@ -3033,46 +3025,52 @@ static void colon(char *buf)
|
||||
// user wants file status info
|
||||
last_status_cksum = 0; // force status update
|
||||
}
|
||||
} else if (strncmp(cmd, "features", i) == 0) { // what features are available
|
||||
} else if (strncmp(cmd, "features", cmdlen) == 0) { // what features are available
|
||||
// print out values of all features
|
||||
go_bottom_and_clear_to_eol();
|
||||
cookmode();
|
||||
show_help();
|
||||
rawmode();
|
||||
Hit_Return();
|
||||
} else if (strncmp(cmd, "list", i) == 0) { // literal print line
|
||||
} else if (strncmp(cmd, "list", cmdlen) == 0) { // literal print line
|
||||
char *dst;
|
||||
if (!GOT_ADDRESS) { // no addr given- use defaults
|
||||
q = begin_line(dot); // assume .,. for the range
|
||||
r = end_line(dot);
|
||||
}
|
||||
go_bottom_and_clear_to_eol();
|
||||
puts("\r");
|
||||
for (; q <= r; q++) {
|
||||
have_status_msg = 1;
|
||||
dst = status_buffer;
|
||||
#define MAXPRINT (sizeof(ESC_BOLD_TEXT "^?" ESC_NORM_TEXT) + 1)
|
||||
while (q <= r && dst < status_buffer + STATUS_BUFFER_LEN - MAXPRINT) {
|
||||
char c;
|
||||
int c_is_no_print;
|
||||
|
||||
c = *q;
|
||||
c = *q++;
|
||||
if (c == '\n') {
|
||||
*dst++ = '$';
|
||||
break;
|
||||
}
|
||||
c_is_no_print = (c & 0x80) && !Isprint(c);
|
||||
if (c_is_no_print) {
|
||||
c = '.';
|
||||
standout_start();
|
||||
//TODO: print fewer ESC if more than one ctrl char
|
||||
dst = stpcpy(dst, ESC_BOLD_TEXT);
|
||||
*dst++ = '.';
|
||||
dst = stpcpy(dst, ESC_NORM_TEXT);
|
||||
continue;
|
||||
}
|
||||
if (c == '\n') {
|
||||
write1("$\r");
|
||||
} else if (c < ' ' || c == 127) {
|
||||
bb_putchar('^');
|
||||
if (c < ' ' || c == 127) {
|
||||
*dst++ = '^';
|
||||
if (c == 127)
|
||||
c = '?';
|
||||
else
|
||||
c += '@';
|
||||
}
|
||||
bb_putchar(c);
|
||||
if (c_is_no_print)
|
||||
standout_end();
|
||||
*dst++ = c;
|
||||
}
|
||||
Hit_Return();
|
||||
} else if (strncmp(cmd, "quit", i) == 0 // quit
|
||||
|| strncmp(cmd, "next", i) == 0 // edit next file
|
||||
|| strncmp(cmd, "prev", i) == 0 // edit previous file
|
||||
*dst = '\0';
|
||||
} else if (strncmp(cmd, "quit", cmdlen) == 0 // quit
|
||||
|| strncmp(cmd, "next", cmdlen) == 0 // edit next file
|
||||
|| strncmp(cmd, "prev", cmdlen) == 0 // edit previous file
|
||||
) {
|
||||
int n;
|
||||
if (useforce) {
|
||||
@@ -3107,13 +3105,14 @@ static void colon(char *buf)
|
||||
optind -= 2;
|
||||
}
|
||||
editing = 0;
|
||||
} else if (strncmp(cmd, "read", i) == 0) { // read file into text[]
|
||||
} else if (strncmp(cmd, "read", cmdlen) == 0) { // read file into text[]
|
||||
int size, num;
|
||||
char *fn = current_filename;
|
||||
|
||||
if (args[0]) {
|
||||
// the user supplied a file name
|
||||
fn = exp = expand_args(args);
|
||||
if (exp == NULL)
|
||||
fn = expand_args(args);
|
||||
if (fn == NULL)
|
||||
goto ret;
|
||||
init_filename(fn);
|
||||
} else if (current_filename == NULL) {
|
||||
@@ -3139,17 +3138,16 @@ static void colon(char *buf)
|
||||
}
|
||||
if (size < 0)
|
||||
goto ret; // nothing was inserted
|
||||
// how many lines in text[]?
|
||||
li = count_lines(q, q + size - 1);
|
||||
status_line("'%s'"
|
||||
IF_FEATURE_VI_READONLY("%s")
|
||||
" %uL, %uC",
|
||||
fn,
|
||||
IF_FEATURE_VI_READONLY((readonly_mode ? " [Readonly]" : ""),)
|
||||
li, size
|
||||
count_lines(q, q + size - 1),
|
||||
size
|
||||
);
|
||||
dot = find_line(num);
|
||||
} else if (strncmp(cmd, "rewind", i) == 0) { // rewind cmd line args
|
||||
} else if (strncmp(cmd, "rewind", cmdlen) == 0) { // rewind cmd line args
|
||||
if (modified_count && !useforce) {
|
||||
status_line_bold("No write since last change (:%s! overrides)", cmd);
|
||||
} else {
|
||||
@@ -3158,11 +3156,12 @@ static void colon(char *buf)
|
||||
editing = 0;
|
||||
}
|
||||
# if ENABLE_FEATURE_VI_SET
|
||||
} else if (strncmp(cmd, "set", i) == 0) { // set or clear features
|
||||
} else if (strncmp(cmd, "set", cmdlen) == 0 // set or clear features
|
||||
IF_FEATURE_VI_SEARCH(&& cmdlen > 1) // (do not confuse with "s /find/repl/")
|
||||
) {
|
||||
# if ENABLE_FEATURE_VI_SETOPTS
|
||||
char *argp, *argn, oldch;
|
||||
# endif
|
||||
// only blank is regarded as args delimiter. What about tab '\t'?
|
||||
if (!args[0] || strcmp(args, "all") == 0) {
|
||||
// print out values of all options
|
||||
# if ENABLE_FEATURE_VI_SETOPTS
|
||||
@@ -3186,7 +3185,7 @@ static void colon(char *buf)
|
||||
# if ENABLE_FEATURE_VI_SETOPTS
|
||||
argp = args;
|
||||
while (*argp) {
|
||||
i = 0;
|
||||
int i = 0;
|
||||
if (argp[0] == 'n' && argp[1] == 'o') // "noXXX"
|
||||
i = 2;
|
||||
argn = skip_non_whitespace(argp);
|
||||
@@ -3201,8 +3200,10 @@ static void colon(char *buf)
|
||||
|
||||
# if ENABLE_FEATURE_VI_SEARCH
|
||||
} else if (cmd[0] == 's') { // substitute a pattern with a replacement pattern
|
||||
char c;
|
||||
char *F, *R, *flags;
|
||||
size_t len_F, len_R;
|
||||
int i;
|
||||
int gflag = 0; // global replace flag
|
||||
int subs = 0; // number of substitutions
|
||||
# if ENABLE_FEATURE_VI_VERBOSE_STATUS
|
||||
@@ -3216,12 +3217,12 @@ static void colon(char *buf)
|
||||
int undo = 0;
|
||||
# endif
|
||||
# endif
|
||||
|
||||
buf = skip_whitespace(buf + 1); // spaces allowed: "s /find/repl/"
|
||||
// F points to the "find" pattern
|
||||
// R points to the "replace" pattern
|
||||
// replace the cmd line delimiters "/" with NULs
|
||||
c = buf[1]; // what is the delimiter
|
||||
F = buf + 2; // start of "find"
|
||||
c = buf[0]; // what is the delimiter
|
||||
F = buf + 1; // start of "find"
|
||||
R = strchr_backslash(F, c); // middle delimiter
|
||||
if (!R)
|
||||
goto colon_s_fail;
|
||||
@@ -3266,7 +3267,6 @@ static void colon(char *buf)
|
||||
# else
|
||||
len_R = strlen(R);
|
||||
# endif
|
||||
|
||||
for (i = b; i <= e; i++) { // so, :20,23 s \0 find \0 replace \0
|
||||
char *ls = q; // orig line start
|
||||
char *found;
|
||||
@@ -3338,15 +3338,16 @@ static void colon(char *buf)
|
||||
regfree(&preg);
|
||||
# endif
|
||||
# endif /* FEATURE_VI_SEARCH */
|
||||
} else if (strncmp(cmd, "version", i) == 0) { // show software version
|
||||
} else if (strncmp(cmd, "version", cmdlen) == 0) { // show software version
|
||||
status_line(BB_VER);
|
||||
} else if (strncmp(cmd, "write", i) == 0 // write text to file
|
||||
} else if (strncmp(cmd, "write", cmdlen) == 0 // write text to file
|
||||
|| strcmp(cmd, "wq") == 0
|
||||
|| strcmp(cmd, "wn") == 0
|
||||
|| (cmd[0] == 'x' && !cmd[1])
|
||||
) {
|
||||
int size;
|
||||
int size, l;
|
||||
//int forced = FALSE;
|
||||
char *fn = current_filename;
|
||||
|
||||
// is there a file name to write to?
|
||||
if (args[0]) {
|
||||
@@ -3355,8 +3356,10 @@ static void colon(char *buf)
|
||||
exp = expand_args(args);
|
||||
if (exp == NULL)
|
||||
goto ret;
|
||||
if (!useforce && (fn == NULL || strcmp(fn, exp) != 0) &&
|
||||
stat(exp, &statbuf) == 0) {
|
||||
if (!useforce
|
||||
&& (fn == NULL || strcmp(fn, exp) != 0)
|
||||
&& stat(exp, &statbuf) == 0
|
||||
) {
|
||||
status_line_bold("File exists (:w! overrides)");
|
||||
goto ret;
|
||||
}
|
||||
@@ -3375,12 +3378,10 @@ static void colon(char *buf)
|
||||
// system(syscmd);
|
||||
// forced = TRUE;
|
||||
//}
|
||||
size = l = 0;
|
||||
if (modified_count != 0 || cmd[0] != 'x') {
|
||||
size = r - q + 1;
|
||||
l = file_write(fn, q, r);
|
||||
} else {
|
||||
size = 0;
|
||||
l = 0;
|
||||
}
|
||||
//if (useforce && forced) {
|
||||
// chmod u-w
|
||||
@@ -3393,8 +3394,8 @@ static void colon(char *buf)
|
||||
status_line_bold_errno(fn);
|
||||
} else {
|
||||
// how many lines written
|
||||
li = count_lines(q, q + l - 1);
|
||||
status_line("'%s' %uL, %uC", fn, li, l);
|
||||
int lines = count_lines(q, q + l - 1);
|
||||
status_line("'%s' %uL, %uC", fn, lines, l);
|
||||
if (l == size) {
|
||||
if (q == text && q + l == end) {
|
||||
modified_count = 0;
|
||||
@@ -3406,37 +3407,35 @@ static void colon(char *buf)
|
||||
// are there other files to edit?
|
||||
int n = cmdline_filecnt - optind - 1;
|
||||
if (n > 0) {
|
||||
if (useforce) {
|
||||
// force end of argv list
|
||||
optind = cmdline_filecnt;
|
||||
} else {
|
||||
if (!useforce) {
|
||||
status_line_bold("%u more file(s) to edit", n);
|
||||
goto ret;
|
||||
}
|
||||
// force end of argv list
|
||||
optind = cmdline_filecnt;
|
||||
}
|
||||
editing = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
# if ENABLE_FEATURE_VI_YANKMARK
|
||||
} else if (strncmp(cmd, "yank", i) == 0) { // yank lines
|
||||
} else if (strncmp(cmd, "yank", cmdlen) == 0) { // yank lines
|
||||
int lines;
|
||||
if (!GOT_ADDRESS) { // no addr given- use defaults
|
||||
q = begin_line(dot); // assume .,. for the range
|
||||
r = end_line(dot);
|
||||
}
|
||||
text_yank(q, r, YDreg, WHOLE);
|
||||
li = count_lines(q, r);
|
||||
lines = count_lines(q, r);
|
||||
status_line("Yank %d lines (%d chars) into [%c]",
|
||||
li, strlen(reg[YDreg]), what_reg());
|
||||
lines, strlen(reg[YDreg]), what_reg());
|
||||
# endif
|
||||
} else {
|
||||
// cmd unknown
|
||||
not_implemented(cmd);
|
||||
}
|
||||
ret:
|
||||
# if ENABLE_FEATURE_VI_COLON_EXPAND
|
||||
free(exp);
|
||||
# endif
|
||||
IF_FEATURE_VI_COLON_EXPAND(free(exp);)
|
||||
dot = bound_dot(dot); // make sure "dot" is valid
|
||||
return;
|
||||
# if ENABLE_FEATURE_VI_SEARCH
|
||||
@@ -4793,8 +4792,7 @@ static void run_cmds(char *p)
|
||||
if (p)
|
||||
while (*p == '\n')
|
||||
*p++ = '\0';
|
||||
if (strlen(q) < MAX_INPUT_LEN)
|
||||
colon(q);
|
||||
colon(q);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -202,7 +202,8 @@ char get_header_tar_xz(archive_handle_t *archive_handle) FAST_FUNC;
|
||||
void seek_by_jump(int fd, off_t amount) FAST_FUNC;
|
||||
void seek_by_read(int fd, off_t amount) FAST_FUNC;
|
||||
|
||||
const char *strip_unsafe_prefix(const char *str) FAST_FUNC;
|
||||
const char *skip_unsafe_prefix(const char *str) FAST_FUNC;
|
||||
void strip_unsafe_prefix(char *str) FAST_FUNC;
|
||||
void create_or_remember_link(llist_t **link_placeholders,
|
||||
const char *target,
|
||||
const char *linkname,
|
||||
|
||||
+265
-95
@@ -290,6 +290,7 @@ PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN
|
||||
/* "long" is long enough on this system */
|
||||
typedef unsigned long uoff_t;
|
||||
# define XATOOFF(a) xatoul_range((a), 0, LONG_MAX)
|
||||
# define XATOOFF_SFX(a, s) xatoul_range_sfx((a), 0, LONG_MAX, s)
|
||||
/* usage: sz = BB_STRTOOFF(s, NULL, 10); if (errno || sz < 0) die(); */
|
||||
# define BB_STRTOOFF bb_strtoul
|
||||
# define STRTOOFF strtoul
|
||||
@@ -299,6 +300,7 @@ typedef unsigned long uoff_t;
|
||||
/* "long" is too short, need "long long" */
|
||||
typedef unsigned long long uoff_t;
|
||||
# define XATOOFF(a) xatoull_range((a), 0, LLONG_MAX)
|
||||
# define XATOOFF_SFX(a, s) xatoull_range_sfx((a), 0, LLONG_MAX, s)
|
||||
# define BB_STRTOOFF bb_strtoull
|
||||
# define STRTOOFF strtoull
|
||||
# define OFF_FMT "ll"
|
||||
@@ -314,12 +316,14 @@ typedef unsigned long long uoff_t;
|
||||
# if UINT_MAX == ULONG_MAX
|
||||
typedef unsigned long uoff_t;
|
||||
# define XATOOFF(a) xatoi_positive(a)
|
||||
# define XATOOFF_SFX(a, s) xatoul_range_sfx((a), 0, INT_MAX, s)
|
||||
# define BB_STRTOOFF bb_strtou
|
||||
# define STRTOOFF strtol
|
||||
# define OFF_FMT "l"
|
||||
# else
|
||||
typedef unsigned long uoff_t;
|
||||
# define XATOOFF(a) xatoul_range((a), 0, LONG_MAX)
|
||||
# define XATOOFF_SFX(a, s) xatoul_range_sfx((a), 0, LONG_MAX, s)
|
||||
# define BB_STRTOOFF bb_strtoul
|
||||
# define STRTOOFF strtol
|
||||
# define OFF_FMT "l"
|
||||
@@ -404,7 +408,12 @@ unsigned FAST_FUNC bb_popcnt_long(unsigned long m);
|
||||
#define bb_popcnt_long(m) bb_popcnt_32(m)
|
||||
#endif
|
||||
|
||||
unsigned long FAST_FUNC isqrt(unsigned long long N);
|
||||
unsigned FAST_FUNC isqrt(unsigned long N);
|
||||
#if LLONG_MAX > LONG_MAX
|
||||
unsigned long FAST_FUNC isqrt_ull(unsigned long long N);
|
||||
#else
|
||||
# define isqrt_ull(N) isqrt(N)
|
||||
#endif
|
||||
|
||||
unsigned long long monotonic_ns(void) FAST_FUNC;
|
||||
unsigned long long monotonic_us(void) FAST_FUNC;
|
||||
@@ -442,6 +451,11 @@ void *mmap_read(int fd, size_t size) FAST_FUNC;
|
||||
void *mmap_anon(size_t size) FAST_FUNC;
|
||||
void *xmmap_anon(size_t size) FAST_FUNC;
|
||||
|
||||
#if defined(__x86_64__) || defined(i386)
|
||||
/* 0x7f would be better, but it causes alignment problems */
|
||||
# define ARCH_GLOBAL_PTR_OFF 0x80
|
||||
#endif
|
||||
|
||||
#if defined(__x86_64__) || defined(i386)
|
||||
# define BB_ARCH_FIXED_PAGESIZE 4096
|
||||
#elif defined(__arm__) /* only 32bit, 64bit ARM has variable page size */
|
||||
@@ -643,6 +657,8 @@ int sigaction_set(int sig, const struct sigaction *act) FAST_FUNC;
|
||||
int sigprocmask_allsigs(int how) FAST_FUNC;
|
||||
/* Return old set in the same set: */
|
||||
int sigprocmask2(int how, sigset_t *set) FAST_FUNC;
|
||||
/* SIG_BLOCK all signals, return old set: */
|
||||
int sigblockall(sigset_t *set) FAST_FUNC;
|
||||
/* Standard handler which just records signo */
|
||||
extern smallint bb_got_signal;
|
||||
void record_signo(int signo); /* not FAST_FUNC! */
|
||||
@@ -687,6 +703,76 @@ struct fd_pair { int rd; int wr; };
|
||||
#define piped_pair(pair) pipe(&((pair).rd))
|
||||
#define xpiped_pair(pair) xpipe(&((pair).rd))
|
||||
|
||||
|
||||
int parse_datestr(const char *date_str, struct tm *ptm) FAST_FUNC;
|
||||
time_t validate_tm_time(const char *date_str, struct tm *ptm) FAST_FUNC;
|
||||
char *strftime_HHMMSS(char *buf, unsigned len, time_t *tp) FAST_FUNC;
|
||||
char *strftime_YYYYMMDDHHMMSS(char *buf, unsigned len, time_t *tp) FAST_FUNC;
|
||||
void xgettimeofday(struct timeval *tv) FAST_FUNC;
|
||||
void xsettimeofday(const struct timeval *tv) FAST_FUNC;
|
||||
|
||||
|
||||
/* Generic I/O loop */
|
||||
struct connection;
|
||||
typedef struct ioloop_state {
|
||||
struct connection *conns;
|
||||
unsigned flags;
|
||||
unsigned max_timeout;
|
||||
unsigned current_iteration_timeout;
|
||||
unsigned last_timeout;
|
||||
int (*pre_poll_callback)(struct ioloop_state*);
|
||||
} ioloop_state_t;
|
||||
|
||||
#define STRUCT_CONNECTION \
|
||||
struct connection *next; \
|
||||
ioloop_state_t *io; \
|
||||
int read_fd; \
|
||||
int write_fd; \
|
||||
int (*should_poll_read_fd)(void *this); \
|
||||
int (*should_poll_write_fd)(void *this); \
|
||||
int (*read)(void *this); \
|
||||
int (*write)(void *this); \
|
||||
|
||||
typedef struct connection {
|
||||
STRUCT_CONNECTION
|
||||
} connection_t;
|
||||
|
||||
static ALWAYS_INLINE void free_connection(connection_t *conn)
|
||||
{
|
||||
free(conn);
|
||||
}
|
||||
|
||||
void FAST_FUNC conn_close_fds(connection_t *conn);
|
||||
void FAST_FUNC conn_close_fds_remove_and_free(connection_t *conn);
|
||||
static ALWAYS_INLINE ioloop_state_t *new_ioloop_state(void)
|
||||
{
|
||||
ioloop_state_t *io = xzalloc(sizeof(*io));
|
||||
return io;
|
||||
}
|
||||
static ALWAYS_INLINE void free_ioloop_state(ioloop_state_t *io)
|
||||
{
|
||||
free(io);
|
||||
}
|
||||
static ALWAYS_INLINE void ioloop_decrease_current_timeout(ioloop_state_t *io, unsigned n)
|
||||
{
|
||||
if (io->current_iteration_timeout > n)
|
||||
io->current_iteration_timeout = n;
|
||||
}
|
||||
|
||||
void FAST_FUNC ioloop_insert_conn(ioloop_state_t *io, connection_t *conn);
|
||||
void FAST_FUNC ioloop_remove_conn(ioloop_state_t *io, connection_t *conn);
|
||||
enum {
|
||||
IOLOOP_FLAG_EXIT_IF_TIMEOUT = (1 << 0),
|
||||
IOLOOP_FLAG_EXIT_IF_EINTR = (1 << 1),
|
||||
//IOLOOP_FLAG_EXIT_IF_ALL_NOT_READY = (1 << 1),
|
||||
/* ioloop_run return values */
|
||||
IOLOOP_NO_CONNS = 0,
|
||||
IOLOOP_TIMEOUT = 1,
|
||||
IOLOOP_EINTR = 2,
|
||||
};
|
||||
int FAST_FUNC ioloop_run(ioloop_state_t *io);
|
||||
|
||||
|
||||
/* Useful for having small structure members/global variables */
|
||||
typedef int8_t socktype_t;
|
||||
typedef int8_t family_t;
|
||||
@@ -715,14 +801,6 @@ struct BUG_too_small {
|
||||
};
|
||||
|
||||
|
||||
int parse_datestr(const char *date_str, struct tm *ptm) FAST_FUNC;
|
||||
time_t validate_tm_time(const char *date_str, struct tm *ptm) FAST_FUNC;
|
||||
char *strftime_HHMMSS(char *buf, unsigned len, time_t *tp) FAST_FUNC;
|
||||
char *strftime_YYYYMMDDHHMMSS(char *buf, unsigned len, time_t *tp) FAST_FUNC;
|
||||
void xgettimeofday(struct timeval *tv) FAST_FUNC;
|
||||
void xsettimeofday(const struct timeval *tv) FAST_FUNC;
|
||||
|
||||
|
||||
int xsocket(int domain, int type, int protocol) FAST_FUNC;
|
||||
void xbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen) FAST_FUNC;
|
||||
void xlisten(int s, int backlog) FAST_FUNC;
|
||||
@@ -833,6 +911,11 @@ char* xmalloc_sockaddr2hostonly_noport(const struct sockaddr *sa) FAST_FUNC RETU
|
||||
/* inet_[ap]ton on steroids */
|
||||
char* xmalloc_sockaddr2dotted(const struct sockaddr *sa) FAST_FUNC RETURNS_MALLOC;
|
||||
char* xmalloc_sockaddr2dotted_noport(const struct sockaddr *sa) FAST_FUNC RETURNS_MALLOC;
|
||||
// NB: unlike getservbyport, port parameter/retval are in host, NOT network order!
|
||||
#define getservbyport dont_use_getservbyport_uses_global_buffer
|
||||
char* bb_get_servname_by_port(char **p_etc_services, int port, const char *type) FAST_FUNC RETURNS_MALLOC;
|
||||
#define getservbyname dont_use_getservbyname_uses_global_buffer
|
||||
int bb_get_servport_by_name(char **p_etc_services, const char *name, const char *type) FAST_FUNC;
|
||||
// "old" (ipv4 only) API
|
||||
// users: traceroute.c hostname.c - use _list_ of all IPs
|
||||
struct hostent *xgethostbyname(const char *name) FAST_FUNC;
|
||||
@@ -854,8 +937,11 @@ typedef struct tls_state {
|
||||
int ofd;
|
||||
int ifd;
|
||||
|
||||
unsigned min_encrypted_len_on_read;
|
||||
#if ENABLE_SSL_SERVER // || ENABLE_FEATURE_HTTPD_SSL
|
||||
smallint expecting_first_packet;
|
||||
#endif
|
||||
uint16_t cipher_id;
|
||||
unsigned min_encrypted_len_on_read;
|
||||
unsigned MAC_size;
|
||||
unsigned key_size;
|
||||
unsigned IV_size;
|
||||
@@ -880,21 +966,27 @@ typedef struct tls_state {
|
||||
/*uint64_t read_seq64_be;*/
|
||||
uint64_t write_seq64_be;
|
||||
|
||||
/*uint8_t *server_write_MAC_key;*/
|
||||
uint8_t *client_write_key;
|
||||
uint8_t *server_write_key;
|
||||
uint8_t *client_write_IV;
|
||||
uint8_t *server_write_IV;
|
||||
uint8_t client_write_MAC_key[TLS_MAX_MAC_SIZE];
|
||||
uint8_t server_write_MAC_k__[TLS_MAX_MAC_SIZE];
|
||||
uint8_t client_write_k__[TLS_MAX_KEY_SIZE];
|
||||
uint8_t server_write_k__[TLS_MAX_KEY_SIZE];
|
||||
uint8_t client_write_I_[TLS_MAX_IV_SIZE];
|
||||
uint8_t server_write_I_[TLS_MAX_IV_SIZE];
|
||||
uint8_t *our_write_MAC_key;
|
||||
uint8_t *peer_write_MAC_key;
|
||||
uint8_t *our_write_key;
|
||||
uint8_t *peer_write_key;
|
||||
uint8_t *our_write_IV;
|
||||
uint8_t *peer_write_IV;
|
||||
uint8_t key_block[TLS_MAX_MAC_SIZE];
|
||||
uint8_t key_block2[TLS_MAX_MAC_SIZE];
|
||||
uint8_t key_block3[TLS_MAX_KEY_SIZE];
|
||||
uint8_t key_block4[TLS_MAX_KEY_SIZE];
|
||||
uint8_t key_block5[TLS_MAX_IV_SIZE];
|
||||
uint8_t key_block6[TLS_MAX_IV_SIZE];
|
||||
|
||||
struct tls_aes aes_encrypt;
|
||||
struct tls_aes aes_decrypt;
|
||||
uint8_t H[16]; //used by AES_GCM
|
||||
|
||||
#if ENABLE_SSL_SERVER // || ENABLE_FEATURE_HTTPD_SSL
|
||||
/* For ECDHE: server's ephemeral EC private key */
|
||||
//uint8_t ecc_priv_key32[32];
|
||||
#endif
|
||||
} tls_state_t;
|
||||
|
||||
static inline tls_state_t *new_tls_state(void)
|
||||
@@ -902,7 +994,9 @@ static inline tls_state_t *new_tls_state(void)
|
||||
tls_state_t *tls = xzalloc(sizeof(*tls));
|
||||
return tls;
|
||||
}
|
||||
void tls_handshake(tls_state_t *tls, const char *sni) FAST_FUNC;
|
||||
void FAST_FUNC tls_handshake(tls_state_t *tls, const char *sni);
|
||||
void FAST_FUNC tls_handshake_as_server(tls_state_t *tls,
|
||||
const char *pem_filename);
|
||||
#define TLSLOOP_EXIT_ON_LOCAL_EOF (1 << 0)
|
||||
void tls_run_copy_loop(tls_state_t *tls, unsigned flags) FAST_FUNC;
|
||||
|
||||
@@ -923,6 +1017,8 @@ int parse_pasv_epsv(char *buf) FAST_FUNC;
|
||||
/* 0 if argv[0] is NULL: */
|
||||
unsigned string_array_len(char **argv) FAST_FUNC;
|
||||
void overlapping_strcpy(char *dst, const char *src) FAST_FUNC;
|
||||
/* Like strncpy but make sure the resulting string is always 0 terminated: */
|
||||
/* writes SIZE chars, the [SIZE-1] char is always NUL (unless SIZE==0). */
|
||||
char *safe_strncpy(char *dst, const char *src, size_t size) FAST_FUNC;
|
||||
char *strncpy_IFNAMSIZ(char *dst, const char *src) FAST_FUNC;
|
||||
unsigned count_strstr(const char *str, const char *sub) FAST_FUNC;
|
||||
@@ -934,6 +1030,8 @@ int bb_putchar(int ch) FAST_FUNC;
|
||||
int bb_putchar_stderr(char ch) FAST_FUNC;
|
||||
int fputs_stdout(const char *s) FAST_FUNC;
|
||||
char *xasprintf(const char *format, ...) __attribute__ ((format(printf, 1, 2))) FAST_FUNC RETURNS_MALLOC;
|
||||
char *xasprintf_and_free(char *allocated, const char *format, ...) __attribute__ ((format(printf, 2, 3))) FAST_FUNC RETURNS_MALLOC;
|
||||
#define xasprintf_inplace(allocated, ...) ((allocated) = xasprintf_and_free((allocated), __VA_ARGS__))
|
||||
char *auto_string(char *str) FAST_FUNC;
|
||||
// gcc-4.1.1 still isn't good enough at optimizing it
|
||||
// (+200 bytes compared to macro)
|
||||
@@ -1011,13 +1109,13 @@ unsigned bb_clk_tck(void) FAST_FUNC;
|
||||
|
||||
#if SEAMLESS_COMPRESSION
|
||||
/* Autodetects gzip/bzip2 formats. fd may be in the middle of the file! */
|
||||
int setup_unzip_on_fd(int fd, int fail_if_not_compressed) FAST_FUNC;
|
||||
int setup_unzip_on_fd(int fd, int die_if_not_compressed) FAST_FUNC;
|
||||
/* Autodetects .gz etc */
|
||||
extern int open_zipped(const char *fname, int fail_if_not_compressed) FAST_FUNC;
|
||||
extern int open_zipped(const char *fname, int die_if_not_compressed) FAST_FUNC;
|
||||
extern void *xmalloc_open_zipped_read_close(const char *fname, size_t *maxsz_p) FAST_FUNC RETURNS_MALLOC;
|
||||
#else
|
||||
# define setup_unzip_on_fd(...) (0)
|
||||
# define open_zipped(fname, fail_if_not_compressed) open((fname), O_RDONLY);
|
||||
# define open_zipped(fname, die_if_not_compressed) open((fname), O_RDONLY);
|
||||
# define xmalloc_open_zipped_read_close(fname, maxsz_p) xmalloc_open_read_close((fname), (maxsz_p))
|
||||
#endif
|
||||
/* lzma has no signature, need a little helper. NB: exist only for ENABLE_FEATURE_SEAMLESS_LZMA=y */
|
||||
@@ -1113,8 +1211,35 @@ char *bin2hex(char *dst, const char *src, int count) FAST_FUNC;
|
||||
/* Reverse */
|
||||
char* hex2bin(char *dst, const char *src, int count) FAST_FUNC;
|
||||
|
||||
/* Returns strlen as a bonus */
|
||||
//size_t replace_char(char *s, char what, char with) FAST_FUNC;
|
||||
static inline size_t replace_char(char *str, char from, char to)
|
||||
{
|
||||
char *p = str;
|
||||
while (*p) {
|
||||
if (*p == from)
|
||||
*p = to;
|
||||
p++;
|
||||
}
|
||||
return p - str;
|
||||
}
|
||||
|
||||
extern const char c_escape_conv_str00[];
|
||||
#define c_escape_conv_str07 (c_escape_conv_str00+3)
|
||||
|
||||
void FAST_FUNC xorbuf_3(void *dst, const void *src1, const void *src2, unsigned count);
|
||||
void FAST_FUNC xorbuf(void* buf, const void* mask, unsigned count);
|
||||
void FAST_FUNC xorbuf16_aligned_long(void* buf, const void* mask);
|
||||
void FAST_FUNC xorbuf64_3_aligned64(void *dst, const void *src1, const void *src2);
|
||||
#if BB_UNALIGNED_MEMACCESS_OK
|
||||
# define xorbuf16(buf,mask) xorbuf16_aligned_long(buf,mask)
|
||||
#else
|
||||
void FAST_FUNC xorbuf16(void* buf, const void* mask);
|
||||
#endif
|
||||
|
||||
/* Generate a UUID */
|
||||
void generate_uuid(uint8_t *buf) FAST_FUNC;
|
||||
void FAST_FUNC format_uuid_DCE_37_chars(char *dst37, const uint8_t *buf16);
|
||||
|
||||
/* Last element is marked by mult == 0 */
|
||||
struct suffix_mult {
|
||||
@@ -1201,6 +1326,19 @@ void die_if_bad_username(const char* name) FAST_FUNC;
|
||||
* Dies on errors (on Linux, only xrealloc can cause this, not internal getgroups call).
|
||||
*/
|
||||
gid_t *bb_getgroups(int *ngroups, gid_t *group_array) FAST_FUNC;
|
||||
/*
|
||||
* True if GID is in our getgroups() result.
|
||||
* getgroups() is cached in supplementary_array[], to make successive calls faster.
|
||||
*/
|
||||
struct cached_groupinfo {
|
||||
uid_t euid;
|
||||
gid_t egid;
|
||||
int ngroups;
|
||||
gid_t *supplementary_array;
|
||||
};
|
||||
uid_t FAST_FUNC get_cached_euid(uid_t *euid);
|
||||
gid_t FAST_FUNC get_cached_egid(gid_t *egid);
|
||||
int FAST_FUNC is_in_supplementary_groups(struct cached_groupinfo *groupinfo, gid_t gid);
|
||||
|
||||
#if ENABLE_FEATURE_UTMP
|
||||
void FAST_FUNC write_new_utmp(pid_t pid, int new_type, const char *tty_name, const char *username, const char *hostname);
|
||||
@@ -1214,7 +1352,7 @@ void FAST_FUNC update_utmp_DEAD_PROCESS(pid_t pid);
|
||||
|
||||
|
||||
int file_is_executable(const char *name) FAST_FUNC;
|
||||
char *find_executable(const char *filename, char **PATHp) FAST_FUNC;
|
||||
char *find_executable(const char *filename, const char **PATHp) FAST_FUNC;
|
||||
int executable_exists(const char *filename) FAST_FUNC;
|
||||
|
||||
/* BB_EXECxx always execs (it's not doing NOFORK/NOEXEC stuff),
|
||||
@@ -1436,7 +1574,11 @@ extern smallint logmode;
|
||||
extern uint8_t xfunc_error_retval;
|
||||
extern void (*die_func)(void);
|
||||
void xfunc_die(void) NORETURN FAST_FUNC;
|
||||
#if !ENABLE_SHOW_USAGE
|
||||
#define bb_show_usage() xfunc_die()
|
||||
#else
|
||||
void bb_show_usage(void) NORETURN FAST_FUNC;
|
||||
#endif
|
||||
void bb_error_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2))) FAST_FUNC;
|
||||
void bb_simple_error_msg(const char *s) FAST_FUNC;
|
||||
void bb_error_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2))) FAST_FUNC;
|
||||
@@ -1539,6 +1681,7 @@ int test_main(int argc, char **argv)
|
||||
MAIN_EXTERNALLY_VISIBLE
|
||||
#endif
|
||||
;
|
||||
int FAST_FUNC test_main2(struct cached_groupinfo *pgroupinfo, int argc, char **argv);
|
||||
int kill_main(int argc, char **argv)
|
||||
#if ENABLE_KILL || ENABLE_KILLALL || ENABLE_KILLALL5
|
||||
MAIN_EXTERNALLY_VISIBLE
|
||||
@@ -1792,18 +1935,25 @@ extern char *pw_encrypt(const char *clear, const char *salt, int cleanup) FAST_F
|
||||
extern int obscure(const char *old, const char *newval, const struct passwd *pwdp) FAST_FUNC;
|
||||
/*
|
||||
* rnd is additional random input. New one is returned.
|
||||
* Useful if you call crypt_make_salt many times in a row:
|
||||
* rnd = crypt_make_salt(buf1, 4, 0);
|
||||
* rnd = crypt_make_salt(buf2, 4, rnd);
|
||||
* rnd = crypt_make_salt(buf3, 4, rnd);
|
||||
* Useful if you call crypt_make_rand64encoded many times in a row:
|
||||
* rnd = crypt_make_rand64encoded(buf1, 4, 0);
|
||||
* rnd = crypt_make_rand64encoded(buf2, 4, rnd);
|
||||
* rnd = crypt_make_rand64encoded(buf3, 4, rnd);
|
||||
* (otherwise we risk having same salt generated)
|
||||
*/
|
||||
extern int crypt_make_salt(char *p, int cnt /*, int rnd*/) FAST_FUNC;
|
||||
/* "$N$" + sha_salt_16_bytes + NUL */
|
||||
#define MAX_PW_SALT_LEN (3 + 16 + 1)
|
||||
extern int crypt_make_rand64encoded(char *p, int cnt /*, int rnd*/) FAST_FUNC;
|
||||
/* Size of char salt[] to hold randomly-generated salt string
|
||||
* sha256/512:
|
||||
* "$5$" ["rounds=999999999$"] "<sha_salt_16_chars><NUL>"
|
||||
* "$6$" ["rounds=999999999$"] "<sha_salt_16_chars><NUL>"
|
||||
* #define MAX_PW_SALT_LEN (3 + sizeof("rounds=999999999$")-1 + 16 + 1)
|
||||
* yescrypt:
|
||||
* "$y$" <up to 8 params of up to 6 chars each> "$" <up to 86 chars salt><NUL>
|
||||
* (86 chars are ascii64-encoded 64 binary bytes)
|
||||
*/
|
||||
#define MAX_PW_SALT_LEN (3 + 8*6 + 1 + 86 + 1)
|
||||
extern char* crypt_make_pw_salt(char p[MAX_PW_SALT_LEN], const char *algo) FAST_FUNC;
|
||||
|
||||
|
||||
/* Returns number of lines changed, or -1 on error */
|
||||
#if !(ENABLE_FEATURE_ADDUSER_TO_GROUP || ENABLE_FEATURE_DEL_USER_FROM_GROUP)
|
||||
#define update_passwd(filename, username, data, member) \
|
||||
@@ -1937,6 +2087,7 @@ int64_t read_key(int fd, char *buffer, int timeout) FAST_FUNC;
|
||||
int64_t safe_read_key(int fd, char *buffer, int timeout) FAST_FUNC;
|
||||
void read_key_ungets(char *buffer, const char *str, unsigned len) FAST_FUNC;
|
||||
|
||||
int check_got_signal_and_poll(struct pollfd pfd[1], int timeout) FAST_FUNC;
|
||||
|
||||
#if ENABLE_FEATURE_EDITING
|
||||
/* It's NOT just ENABLEd or disabled. It's a number: */
|
||||
@@ -1974,7 +2125,7 @@ typedef struct line_input_t {
|
||||
# if MAX_HISTORY
|
||||
int cnt_history;
|
||||
int cur_history;
|
||||
int max_history; /* must never be <= 0 */
|
||||
int max_history; /* must never be < 0 */
|
||||
# if ENABLE_FEATURE_EDITING_SAVEHISTORY
|
||||
/* meaning of this field depends on FEATURE_EDITING_SAVE_ON_EXIT:
|
||||
* if !FEATURE_EDITING_SAVE_ON_EXIT: "how many lines are
|
||||
@@ -1998,11 +2149,11 @@ enum {
|
||||
FOR_SHELL = DO_HISTORY | TAB_COMPLETION | USERNAME_COMPLETION | LI_INTERRUPTIBLE,
|
||||
};
|
||||
line_input_t *new_line_input_t(int flags) FAST_FUNC;
|
||||
#if ENABLE_FEATURE_EDITING_SAVEHISTORY
|
||||
# if ENABLE_FEATURE_EDITING_SAVEHISTORY
|
||||
void free_line_input_t(line_input_t *n) FAST_FUNC;
|
||||
#else
|
||||
# define free_line_input_t(n) free(n)
|
||||
#endif
|
||||
# else
|
||||
# define free_line_input_t(n) free(n)
|
||||
# endif
|
||||
/*
|
||||
* maxsize must be >= 2.
|
||||
* Returns:
|
||||
@@ -2013,7 +2164,7 @@ void free_line_input_t(line_input_t *n) FAST_FUNC;
|
||||
int read_line_input(line_input_t *st, const char *prompt, char *command, int maxsize) FAST_FUNC;
|
||||
void show_history(const line_input_t *st) FAST_FUNC;
|
||||
# if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT
|
||||
void save_history(line_input_t *st);
|
||||
void save_history(line_input_t *st) FAST_FUNC;
|
||||
# endif
|
||||
#else
|
||||
#define MAX_HISTORY 0
|
||||
@@ -2033,33 +2184,6 @@ enum { COMM_LEN = 16 };
|
||||
# endif
|
||||
#endif
|
||||
|
||||
struct smaprec {
|
||||
unsigned long mapped_rw;
|
||||
unsigned long mapped_ro;
|
||||
unsigned long shared_clean;
|
||||
unsigned long shared_dirty;
|
||||
unsigned long private_clean;
|
||||
unsigned long private_dirty;
|
||||
unsigned long stack;
|
||||
unsigned long smap_pss, smap_swap;
|
||||
unsigned long smap_size;
|
||||
// For mixed 32/64 userspace, 32-bit pmap still needs
|
||||
// 64-bit field here to correctly show 64-bit processes:
|
||||
unsigned long long smap_start;
|
||||
// (strictly speaking, other fields need to be wider too,
|
||||
// but they are in kbytes, not bytes, and they hold sizes,
|
||||
// not start addresses, sizes tend to be less than 4 terabytes)
|
||||
char smap_mode[5];
|
||||
char *smap_name;
|
||||
};
|
||||
|
||||
#if !ENABLE_PMAP
|
||||
#define procps_read_smaps(pid, total, cb, data) \
|
||||
procps_read_smaps(pid, total)
|
||||
#endif
|
||||
int FAST_FUNC procps_read_smaps(pid_t pid, struct smaprec *total,
|
||||
void (*cb)(struct smaprec *, void *), void *data);
|
||||
|
||||
typedef struct procps_status_t {
|
||||
DIR *dir;
|
||||
IF_FEATURE_SHOW_THREADS(DIR *task_dir;)
|
||||
@@ -2089,7 +2213,13 @@ typedef struct procps_status_t {
|
||||
#endif
|
||||
unsigned tty_major,tty_minor;
|
||||
#if ENABLE_FEATURE_TOPMEM
|
||||
struct smaprec smaps;
|
||||
unsigned long mapped_rw;
|
||||
unsigned long mapped_ro;
|
||||
unsigned long shared_clean;
|
||||
unsigned long shared_dirty;
|
||||
unsigned long private_clean;
|
||||
unsigned long private_dirty;
|
||||
unsigned long stack;
|
||||
#endif
|
||||
char state[4];
|
||||
/* basename of executable in exec(2), read from /proc/N/stat
|
||||
@@ -2138,11 +2268,15 @@ void free_procps_scan(procps_status_t* sp) FAST_FUNC;
|
||||
procps_status_t* procps_scan(procps_status_t* sp, int flags) FAST_FUNC;
|
||||
/* Format cmdline (up to col chars) into char buf[size] */
|
||||
/* Puts [comm] if cmdline is empty (-> process is a kernel thread) */
|
||||
void read_cmdline(char *buf, int size, unsigned pid, const char *comm) FAST_FUNC;
|
||||
int read_cmdline(char *buf, int size, unsigned pid, const char *comm) FAST_FUNC;
|
||||
pid_t *find_pid_by_name(const char* procName) FAST_FUNC;
|
||||
pid_t *pidlist_reverse(pid_t *pidList) FAST_FUNC;
|
||||
int starts_with_cpu(const char *str) FAST_FUNC;
|
||||
unsigned get_cpu_count(void) FAST_FUNC;
|
||||
/* Some internals reused by pmap: */
|
||||
unsigned long FAST_FUNC fast_strtoul_10(char **endptr);
|
||||
unsigned long long FAST_FUNC fast_strtoull_16(char **endptr);
|
||||
char* FAST_FUNC skip_fields(char *str, int count);
|
||||
|
||||
|
||||
/* Use strict=1 if you process input from untrusted source:
|
||||
@@ -2168,6 +2302,21 @@ char *decode_base64(char *dst, const char **pp_src) FAST_FUNC;
|
||||
char *decode_base32(char *dst, const char **pp_src) FAST_FUNC;
|
||||
void read_base64(FILE *src_stream, FILE *dst_stream, int flags) FAST_FUNC;
|
||||
|
||||
int FAST_FUNC i2a64(int i);
|
||||
int FAST_FUNC a2i64(char c);
|
||||
char* FAST_FUNC num2str64_lsb_first(char *s, unsigned v, int n);
|
||||
|
||||
enum {
|
||||
/* how many bytes XYZ_end() fills */
|
||||
MD5_OUTSIZE = 16,
|
||||
SHA1_OUTSIZE = 20,
|
||||
SHA256_OUTSIZE = 32,
|
||||
SHA384_OUTSIZE = 48,
|
||||
SHA512_OUTSIZE = 64,
|
||||
//SHA3-224_OUTSIZE = 28,
|
||||
/* size of input block */
|
||||
SHA2_INSIZE = 64,
|
||||
};
|
||||
typedef struct md5_ctx_t {
|
||||
uint8_t wbuffer[64]; /* always correctly aligned for uint64_t */
|
||||
void (*process_block)(struct md5_ctx_t*) FAST_FUNC;
|
||||
@@ -2181,6 +2330,7 @@ typedef struct sha512_ctx_t {
|
||||
uint64_t hash[8];
|
||||
uint8_t wbuffer[128]; /* always correctly aligned for uint64_t */
|
||||
} sha512_ctx_t;
|
||||
typedef struct sha512_ctx_t sha384_ctx_t;
|
||||
typedef struct sha3_ctx_t {
|
||||
uint64_t state[25];
|
||||
unsigned bytes_queued;
|
||||
@@ -2198,20 +2348,46 @@ void sha256_begin(sha256_ctx_t *ctx) FAST_FUNC;
|
||||
void sha512_begin(sha512_ctx_t *ctx) FAST_FUNC;
|
||||
void sha512_hash(sha512_ctx_t *ctx, const void *buffer, size_t len) FAST_FUNC;
|
||||
unsigned sha512_end(sha512_ctx_t *ctx, void *resbuf) FAST_FUNC;
|
||||
void sha384_begin(sha384_ctx_t *ctx) FAST_FUNC;
|
||||
#define sha384_hash sha512_hash
|
||||
unsigned sha384_end(sha384_ctx_t *ctx, void *resbuf) FAST_FUNC;
|
||||
void sha3_begin(sha3_ctx_t *ctx) FAST_FUNC;
|
||||
void sha3_hash(sha3_ctx_t *ctx, const void *buffer, size_t len) FAST_FUNC;
|
||||
unsigned sha3_end(sha3_ctx_t *ctx, void *resbuf) FAST_FUNC;
|
||||
void FAST_FUNC sha256_block(const void *in, size_t len, uint8_t hash[32]);
|
||||
/* TLS benefits from knowing that sha1 and sha256 share these. Give them "agnostic" names too */
|
||||
typedef struct md5_ctx_t md5sha_ctx_t;
|
||||
#define md5sha_hash md5_hash
|
||||
#define sha_end sha1_end
|
||||
enum {
|
||||
MD5_OUTSIZE = 16,
|
||||
SHA1_OUTSIZE = 20,
|
||||
SHA256_OUTSIZE = 32,
|
||||
SHA512_OUTSIZE = 64,
|
||||
SHA3_OUTSIZE = 28,
|
||||
};
|
||||
|
||||
/* RFC 2104 HMAC (hash-based message authentication code) */
|
||||
typedef struct hmac_ctx {
|
||||
md5sha_ctx_t hashed_key_xor_ipad;
|
||||
md5sha_ctx_t hashed_key_xor_opad;
|
||||
} hmac_ctx_t;
|
||||
#define HMAC_ONLY_SHA256 (!ENABLE_FEATURE_TLS_SHA1)
|
||||
typedef void md5sha_begin_func(md5sha_ctx_t *ctx) FAST_FUNC;
|
||||
#if HMAC_ONLY_SHA256
|
||||
#define hmac_begin(ctx,key,key_size,begin) \
|
||||
hmac_begin(ctx,key,key_size)
|
||||
#endif
|
||||
void FAST_FUNC hmac_begin(hmac_ctx_t *ctx, const uint8_t *key, unsigned key_size, md5sha_begin_func *begin);
|
||||
static ALWAYS_INLINE void hmac_hash(hmac_ctx_t *ctx, const void *in, size_t len)
|
||||
{
|
||||
md5sha_hash(&ctx->hashed_key_xor_ipad, in, len);
|
||||
}
|
||||
unsigned FAST_FUNC hmac_end(hmac_ctx_t *ctx, uint8_t *out);
|
||||
#if HMAC_ONLY_SHA256
|
||||
#define hmac_block(key,key_size,begin,in,sz,out) \
|
||||
hmac_block(key,key_size,in,sz,out)
|
||||
#endif
|
||||
unsigned FAST_FUNC hmac_block(const uint8_t *key, unsigned key_size,
|
||||
md5sha_begin_func *begin,
|
||||
const void *in, unsigned sz,
|
||||
uint8_t *out);
|
||||
/* HMAC helpers for TLS: */
|
||||
void FAST_FUNC hmac_hash_v(hmac_ctx_t *ctx, va_list va);
|
||||
unsigned FAST_FUNC hmac_peek_hash(hmac_ctx_t *ctx, uint8_t *out, ...);
|
||||
|
||||
extern uint32_t *global_crc32_table;
|
||||
uint32_t *crc32_filltable(uint32_t *tbl256, int endian) FAST_FUNC;
|
||||
@@ -2323,26 +2499,10 @@ extern struct globals *BB_GLOBAL_CONST ptr_to_globals;
|
||||
#define barrier() asm volatile ("":::"memory")
|
||||
|
||||
#if defined(__clang_major__) && __clang_major__ >= 9
|
||||
/* Clang/llvm drops assignment to "constant" storage. Silently.
|
||||
* Needs serious convincing to not eliminate the store.
|
||||
*/
|
||||
static ALWAYS_INLINE void* not_const_pp(const void *p)
|
||||
{
|
||||
void *pp;
|
||||
asm volatile (
|
||||
"# forget that p points to const"
|
||||
: /*outputs*/ "=r" (pp)
|
||||
: /*inputs*/ "0" (p)
|
||||
);
|
||||
return pp;
|
||||
}
|
||||
# define ASSIGN_CONST_PTR(pptr, v) do { \
|
||||
*(void**)not_const_pp(pptr) = (void*)(v); \
|
||||
barrier(); \
|
||||
} while (0)
|
||||
/* XZALLOC_CONST_PTR() is an out-of-line function to prevent
|
||||
* clang from reading pointer before it is assigned.
|
||||
/* {ASSIGN,XZALLOC}_CONST_PTR() are out-of-line functions
|
||||
* to prevent clang from reading pointer before it is assigned.
|
||||
*/
|
||||
void ASSIGN_CONST_PTR(const void *pptr, void *v) FAST_FUNC;
|
||||
void XZALLOC_CONST_PTR(const void *pptr, size_t size) FAST_FUNC;
|
||||
#else
|
||||
# define ASSIGN_CONST_PTR(pptr, v) do { \
|
||||
@@ -2360,6 +2520,16 @@ void XZALLOC_CONST_PTR(const void *pptr, size_t size) FAST_FUNC;
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#if defined(ARCH_GLOBAL_PTR_OFF)
|
||||
# define SET_OFFSET_PTR_TO_GLOBALS(x) \
|
||||
ASSIGN_CONST_PTR(&ptr_to_globals, (char*)(x) + ARCH_GLOBAL_PTR_OFF)
|
||||
# define OFFSET_PTR_TO_GLOBALS \
|
||||
((struct globals*)((char*)ptr_to_globals - ARCH_GLOBAL_PTR_OFF))
|
||||
#else
|
||||
# define SET_OFFSET_PTR_TO_GLOBALS(x) SET_PTR_TO_GLOBALS(x)
|
||||
# define OFFSET_PTR_TO_GLOBALS ptr_to_globals
|
||||
#endif
|
||||
|
||||
|
||||
/* You can change LIBBB_DEFAULT_LOGIN_SHELL, but don't use it,
|
||||
* use bb_default_login_shell and following defines.
|
||||
|
||||
+6
-1
@@ -187,7 +187,7 @@
|
||||
#elif defined(BYTE_ORDER) && BYTE_ORDER == LITTLE_ENDIAN
|
||||
# define BB_BIG_ENDIAN 0
|
||||
# define BB_LITTLE_ENDIAN 1
|
||||
#elif defined(__386__)
|
||||
#elif defined(__i386__)
|
||||
# define BB_BIG_ENDIAN 0
|
||||
# define BB_LITTLE_ENDIAN 1
|
||||
#else
|
||||
@@ -209,6 +209,9 @@
|
||||
# define SWAP_LE64(x) bb_bswap_64(x)
|
||||
# define IF_BIG_ENDIAN(...) __VA_ARGS__
|
||||
# define IF_LITTLE_ENDIAN(...)
|
||||
/* How do bytes a,b,c,d (sequential in memory) look if fetched into uint32_t? */
|
||||
# define PACK32_BYTES(a,b,c,d) (uint32_t)((d)+((c)<<8)+((b)<<16)+((a)<<24))
|
||||
# define PACK64_LITERAL_STR(s) (((uint64_t)PACK32_BYTES((s)[0],(s)[1],(s)[2],(s)[3])<<32) + PACK32_BYTES((s)[4],(s)[5],(s)[6],(s)[7]))
|
||||
#else
|
||||
# define SWAP_BE16(x) bswap_16(x)
|
||||
# define SWAP_BE32(x) bswap_32(x)
|
||||
@@ -218,6 +221,8 @@
|
||||
# define SWAP_LE64(x) (x)
|
||||
# define IF_BIG_ENDIAN(...)
|
||||
# define IF_LITTLE_ENDIAN(...) __VA_ARGS__
|
||||
# define PACK32_BYTES(a,b,c,d) (uint32_t)((a)+((b)<<8)+((c)<<16)+((d)<<24))
|
||||
# define PACK64_LITERAL_STR(s) (((uint64_t)PACK32_BYTES((s)[4],(s)[5],(s)[6],(s)[7])<<32) + PACK32_BYTES((s)[0],(s)[1],(s)[2],(s)[3]))
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
+3
-3
@@ -17,11 +17,11 @@
|
||||
#define scripted_trivial_usage NOUSAGE_STR
|
||||
#define scripted_full_usage ""
|
||||
|
||||
#if !ENABLE_USE_BB_CRYPT || ENABLE_USE_BB_CRYPT_SHA
|
||||
# define CRYPT_METHODS_HELP_STR "des,md5,sha256/512" \
|
||||
#if !ENABLE_USE_BB_CRYPT
|
||||
# define CRYPT_METHODS_HELP_STR "des,md5,sha256/512,yescrypt" \
|
||||
" (default "CONFIG_FEATURE_DEFAULT_PASSWD_ALGO")"
|
||||
#else
|
||||
# define CRYPT_METHODS_HELP_STR "des,md5" \
|
||||
# define CRYPT_METHODS_HELP_STR "des,md5"IF_USE_BB_CRYPT_SHA(",sha256/512")IF_USE_BB_CRYPT_YES(",yescrypt") \
|
||||
" (default "CONFIG_FEATURE_DEFAULT_PASSWD_ALGO")"
|
||||
#endif
|
||||
|
||||
|
||||
+1
-1
@@ -133,7 +133,7 @@ static void dump_file(FILE *fp, const char *filename)
|
||||
static int dump_procs(FILE *fp, int look_for_login_process)
|
||||
{
|
||||
struct dirent *entry;
|
||||
DIR *dir = opendir("/proc");
|
||||
DIR *dir = xopendir("/proc");
|
||||
int found_login_process = 0;
|
||||
|
||||
fputs(G.jiffy_line, fp);
|
||||
|
||||
+15
-3
@@ -1198,17 +1198,29 @@ int init_main(int argc UNUSED_PARAM, char **argv)
|
||||
/* Wait for any child process(es) to exit */
|
||||
while (1) {
|
||||
pid_t wpid;
|
||||
int status;
|
||||
struct init_action *a;
|
||||
|
||||
wpid = waitpid(-1, NULL, WNOHANG);
|
||||
wpid = wait_any_nohang(&status);
|
||||
if (wpid <= 0)
|
||||
break;
|
||||
|
||||
a = mark_terminated(wpid);
|
||||
if (a) {
|
||||
message(L_LOG, "process '%s' (pid %u) exited. "
|
||||
const char *s = "killed, signal";
|
||||
int ex = WTERMSIG(status);
|
||||
/* "if (!WIFSIGNALED(status))" generates more code:
|
||||
* on linux, WIFEXITED(status) is "WTERMSIG(status) == 0"
|
||||
* and WTERMSIG(status) is known, so compiler optimizes.
|
||||
*/
|
||||
if (WIFEXITED(status)) {
|
||||
s = "exited, exitcode";
|
||||
ex = WEXITSTATUS(status);
|
||||
}
|
||||
message(L_LOG, "process '%s' (pid %u) %s:%d. "
|
||||
"Scheduling for restart.",
|
||||
a->command, (unsigned)wpid);
|
||||
a->command, (unsigned)wpid,
|
||||
s, ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@
|
||||
* - /dev/ram (alias to /dev/ram0)
|
||||
* - /dev/mtd
|
||||
*/
|
||||
static dev_t name_to_dev_t(const char *devname)
|
||||
static dev_t name_to_dev_t(char *devname)
|
||||
{
|
||||
char devfile[sizeof(int)*3 * 2 + 4];
|
||||
char *sysname;
|
||||
@@ -39,7 +39,7 @@ static dev_t name_to_dev_t(const char *devname)
|
||||
struct stat st;
|
||||
int r;
|
||||
|
||||
if (strncmp(devname, "/dev/", 5) != 0) {
|
||||
if (!is_prefixed_with(devname, "/dev/")) {
|
||||
char *cptr;
|
||||
|
||||
cptr = strchr(devname, ':');
|
||||
@@ -59,7 +59,7 @@ static dev_t name_to_dev_t(const char *devname)
|
||||
return res;
|
||||
}
|
||||
|
||||
devname = xasprintf("/dev/%s", devname);
|
||||
devname = concat_path_file("/dev", devname);
|
||||
}
|
||||
/* Now devname is always "/dev/FOO" */
|
||||
|
||||
|
||||
+2
-2
@@ -182,8 +182,8 @@ config FEATURE_EDITING_VI
|
||||
config FEATURE_EDITING_HISTORY
|
||||
int "History size"
|
||||
# Don't allow way too big values here, code uses fixed "char *history[N]" struct member
|
||||
range 0 9999
|
||||
default 255
|
||||
range 0 2000
|
||||
default 200
|
||||
depends on FEATURE_EDITING
|
||||
help
|
||||
Specify command history size (0 - disable).
|
||||
|
||||
+72
-59
@@ -124,55 +124,57 @@ static const char packed_usage[] ALIGN1 = { PACKED_USAGE };
|
||||
#endif /* FEATURE_COMPRESS_USAGE */
|
||||
|
||||
|
||||
#if ENABLE_SHOW_USAGE
|
||||
void FAST_FUNC bb_show_usage(void)
|
||||
{
|
||||
if (ENABLE_SHOW_USAGE) {
|
||||
#ifdef SINGLE_APPLET_STR
|
||||
/* Imagine that this applet is "true". Dont link in printf! */
|
||||
const char *usage_string = unpack_usage_messages();
|
||||
ssize_t FAST_FUNC (*full_write_fn)(const char *) =
|
||||
xfunc_error_retval ? full_write2_str : full_write1_str;
|
||||
# ifdef SINGLE_APPLET_STR
|
||||
/* Imagine that this applet is "true". Dont link in printf! */
|
||||
const char *usage_string = unpack_usage_messages();
|
||||
|
||||
if (usage_string) {
|
||||
if (*usage_string == '\b') {
|
||||
full_write2_str("No help available\n");
|
||||
} else {
|
||||
full_write2_str("Usage: "SINGLE_APPLET_STR" ");
|
||||
full_write2_str(usage_string);
|
||||
full_write2_str("\n");
|
||||
}
|
||||
if (ENABLE_FEATURE_CLEAN_UP)
|
||||
dealloc_usage_messages((char*)usage_string);
|
||||
}
|
||||
#else
|
||||
const char *p;
|
||||
const char *usage_string = p = unpack_usage_messages();
|
||||
int ap = find_applet_by_name(applet_name);
|
||||
|
||||
if (ap < 0 || usage_string == NULL)
|
||||
xfunc_die();
|
||||
while (ap) {
|
||||
while (*p++) continue;
|
||||
ap--;
|
||||
}
|
||||
full_write2_str(bb_banner);
|
||||
full_write2_str(" multi-call binary.\n"); /* common string */
|
||||
if (*p == '\b')
|
||||
full_write2_str("\nNo help available\n");
|
||||
else {
|
||||
full_write2_str("\nUsage: ");
|
||||
full_write2_str(applet_name);
|
||||
if (p[0]) {
|
||||
if (p[0] != '\n')
|
||||
full_write2_str(" ");
|
||||
full_write2_str(p);
|
||||
}
|
||||
full_write2_str("\n");
|
||||
if (usage_string) {
|
||||
if (*usage_string == '\b') {
|
||||
full_write_fn("No help available\n");
|
||||
} else {
|
||||
full_write_fn("Usage: "SINGLE_APPLET_STR" ");
|
||||
full_write_fn(usage_string);
|
||||
full_write_fn("\n");
|
||||
}
|
||||
if (ENABLE_FEATURE_CLEAN_UP)
|
||||
dealloc_usage_messages((char*)usage_string);
|
||||
#endif
|
||||
}
|
||||
# else
|
||||
const char *p;
|
||||
const char *usage_string = p = unpack_usage_messages();
|
||||
int ap = find_applet_by_name(applet_name);
|
||||
|
||||
if (ap < 0 || usage_string == NULL)
|
||||
xfunc_die();
|
||||
while (ap) {
|
||||
while (*p++) continue;
|
||||
ap--;
|
||||
}
|
||||
full_write_fn(bb_banner);
|
||||
full_write_fn(" multi-call binary.\n"); /* common string */
|
||||
if (*p == '\b')
|
||||
full_write_fn("\nNo help available\n");
|
||||
else {
|
||||
full_write_fn("\nUsage: ");
|
||||
full_write_fn(applet_name);
|
||||
if (p[0]) {
|
||||
if (p[0] != '\n')
|
||||
full_write_fn(" ");
|
||||
full_write_fn(p);
|
||||
}
|
||||
full_write_fn("\n");
|
||||
}
|
||||
if (ENABLE_FEATURE_CLEAN_UP)
|
||||
dealloc_usage_messages((char*)usage_string);
|
||||
# endif
|
||||
xfunc_die();
|
||||
}
|
||||
#endif
|
||||
|
||||
int FAST_FUNC find_applet_by_name(const char *name)
|
||||
{
|
||||
@@ -268,8 +270,10 @@ void lbb_prepare(const char *applet
|
||||
&& !(ENABLE_TRUE && strcmp(applet_name, "true") == 0)
|
||||
&& !(ENABLE_FALSE && strcmp(applet_name, "false") == 0)
|
||||
&& !(ENABLE_ECHO && strcmp(applet_name, "echo") == 0)
|
||||
)
|
||||
) {
|
||||
xfunc_error_retval = 0;
|
||||
bb_show_usage();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -776,10 +780,9 @@ int busybox_main(int argc UNUSED_PARAM, char **argv)
|
||||
help:
|
||||
output_width = get_terminal_width(2);
|
||||
|
||||
dup2(1, 2);
|
||||
full_write2_str(bb_banner); /* reuse const string */
|
||||
full_write2_str(" multi-call binary.\n"); /* reuse */
|
||||
full_write2_str(
|
||||
full_write1_str(bb_banner); /* reuse const string */
|
||||
full_write1_str(" multi-call binary.\n"); /* reuse */
|
||||
full_write1_str(
|
||||
"BusyBox is copyrighted by many authors between 1998-2015.\n"
|
||||
"Licensed under GPLv2. See source distribution for detailed\n"
|
||||
"copyright notices.\n"
|
||||
@@ -817,20 +820,20 @@ int busybox_main(int argc UNUSED_PARAM, char **argv)
|
||||
while (*a) {
|
||||
int len2 = strlen(a) + 2;
|
||||
if (col >= (int)output_width - len2) {
|
||||
full_write2_str(",\n");
|
||||
full_write1_str(",\n");
|
||||
col = 0;
|
||||
}
|
||||
if (col == 0) {
|
||||
col = 6;
|
||||
full_write2_str("\t");
|
||||
full_write1_str("\t");
|
||||
} else {
|
||||
full_write2_str(", ");
|
||||
full_write1_str(", ");
|
||||
}
|
||||
full_write2_str(a);
|
||||
full_write1_str(a);
|
||||
col += len2;
|
||||
a += len2 - 1;
|
||||
}
|
||||
full_write2_str("\n");
|
||||
full_write1_str("\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -850,14 +853,13 @@ int busybox_main(int argc UNUSED_PARAM, char **argv)
|
||||
if (is_prefixed_with(argv[1], "--list")) {
|
||||
unsigned i = 0;
|
||||
const char *a = applet_names;
|
||||
dup2(1, 2);
|
||||
while (*a) {
|
||||
# if ENABLE_FEATURE_INSTALLER
|
||||
if (argv[1][6]) /* --list-full? */
|
||||
full_write2_str(install_dir[APPLET_INSTALL_LOC(i)] + 1);
|
||||
full_write1_str(install_dir[APPLET_INSTALL_LOC(i)] + 1);
|
||||
# endif
|
||||
full_write2_str(a);
|
||||
full_write2_str("\n");
|
||||
full_write1_str(a);
|
||||
full_write1_str("\n");
|
||||
i++;
|
||||
while (*a++ != '\0')
|
||||
continue;
|
||||
@@ -890,14 +892,14 @@ int busybox_main(int argc UNUSED_PARAM, char **argv)
|
||||
}
|
||||
|
||||
if (strcmp(argv[1], "--help") == 0) {
|
||||
/* "busybox --help [<applet>]" */
|
||||
/* "busybox --help [APPLET]" */
|
||||
if (!argv[2]
|
||||
# if ENABLE_FEATURE_SH_STANDALONE && ENABLE_FEATURE_TAB_COMPLETION
|
||||
|| strcmp(argv[2], "busybox") == 0 /* prevent getting "No help available" */
|
||||
# endif
|
||||
)
|
||||
goto help;
|
||||
/* convert to "<applet> --help" */
|
||||
/* convert to "APPLET --help" */
|
||||
applet_name = argv[0] = argv[2];
|
||||
argv[2] = NULL;
|
||||
if (find_applet_by_name(applet_name) >= 0) {
|
||||
@@ -905,8 +907,16 @@ int busybox_main(int argc UNUSED_PARAM, char **argv)
|
||||
xfunc_error_retval = 0;
|
||||
bb_show_usage();
|
||||
} /* else: unknown applet, fall through (causes "applet not found" later) */
|
||||
} else {
|
||||
/* "busybox <applet> arg1 arg2 ..." */
|
||||
}
|
||||
# if ENABLE_FEATURE_VERSION
|
||||
else if (!argv[2] && strcmp(argv[1], "--version") == 0) {
|
||||
full_write1_str(bb_banner); /* reuse const string */
|
||||
full_write1_str("\n");
|
||||
return 0;
|
||||
}
|
||||
# endif
|
||||
else {
|
||||
/* "busybox APPLET arg1 arg2 ..." */
|
||||
argv++;
|
||||
/* We support "busybox /a/path/to/applet args..." too. Allows for
|
||||
* "#!/bin/busybox"-style wrappers
|
||||
@@ -1101,8 +1111,11 @@ int main(int argc UNUSED_PARAM, char **argv)
|
||||
|
||||
lbb_prepare("busybox" IF_FEATURE_INDIVIDUAL(, argv));
|
||||
# if !ENABLE_BUSYBOX
|
||||
if (argv[1] && is_prefixed_with(bb_basename(argv[0]), "busybox"))
|
||||
if (argv[1] && argv[1][0] != '-' /* do not match "busybox --OPT" */
|
||||
&& is_prefixed_with(bb_basename(argv[0]), "busybox")
|
||||
) {
|
||||
argv++;
|
||||
}
|
||||
# endif
|
||||
applet_name = argv[0];
|
||||
if (applet_name[0] == '-')
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Utility routines.
|
||||
*
|
||||
* Copyright (C) 2026 Denys Vlasenko
|
||||
*
|
||||
* Licensed under GPLv2, see file LICENSE in this source tree.
|
||||
*/
|
||||
//kbuild:lib-$(CONFIG_NETSTAT) += bb_get_servname_by_port.o
|
||||
//kbuild:lib-$(CONFIG_PSCAN) += bb_get_servname_by_port.o
|
||||
#include "libbb.h"
|
||||
|
||||
//Rationale for existing:
|
||||
//#define getservbyport dont_use_getservbyport_uses_global_buffer
|
||||
//(for example: -280 bytes on musl, x86-32)
|
||||
|
||||
char* FAST_FUNC bb_get_servname_by_port(char **p_etc_services, int port, const char *type)
|
||||
{
|
||||
char *sp;
|
||||
|
||||
sp = *p_etc_services;
|
||||
if (!sp) {
|
||||
//TODO: we need mmap_entire_file() for this use case!
|
||||
sp = xmalloc_open_read_close("/etc/services", NULL);
|
||||
if (!sp)
|
||||
return NULL;
|
||||
*p_etc_services = sp;
|
||||
}
|
||||
while (*sp) {
|
||||
const char *pnstr, *sp_end;
|
||||
char *end;
|
||||
unsigned n;
|
||||
|
||||
sp = skip_whitespace(sp);
|
||||
if (*sp == '#')
|
||||
goto next;
|
||||
sp_end = skip_non_whitespace(sp);
|
||||
pnstr = skip_whitespace(sp_end);
|
||||
n = bb_strtou(pnstr, &end, 10);
|
||||
if (n != port || *end != '/')
|
||||
goto next;
|
||||
if (type) {
|
||||
end++;
|
||||
end = is_prefixed_with(end, type);
|
||||
if (!end
|
||||
|| !(isspace(*end) || *end == '\0'
|
||||
|| *end == '#') // glibc treats "http 80/tcp#COMMENT" (no space!) as valid
|
||||
) {
|
||||
goto next;
|
||||
}
|
||||
}
|
||||
return auto_string(xstrndup(sp, sp_end - sp));
|
||||
next:
|
||||
sp = strchrnul(sp, '\n');
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
/*
|
||||
* Utility routines.
|
||||
*
|
||||
* Copyright (C) 2026 Denys Vlasenko
|
||||
*
|
||||
* Licensed under GPLv2, see file LICENSE in this source tree.
|
||||
*/
|
||||
//kbuild:lib-y += bb_get_servport_by_name.o
|
||||
//kbuild:lib-y += bb_get_servport_by_name.o
|
||||
#include "libbb.h"
|
||||
|
||||
//Rationale for existing:
|
||||
//#define getservbyname dont_use_getservbyname_uses_global_buffer
|
||||
//(for example: -32 BSS bytes on musl, x86-32)
|
||||
|
||||
int FAST_FUNC bb_get_servport_by_name(char **p_etc_services, const char *name, const char *proto)
|
||||
{
|
||||
unsigned namelen;
|
||||
char *sp;
|
||||
|
||||
if (!name[0]) // This can hang the search (strstr advances by 0 bytes)
|
||||
return -1; // bad service name form: ""
|
||||
// Any other bogosity to reject?
|
||||
// Or just enforce isalnum_or_dash_or_slash(name[i])?
|
||||
// Yes, service name like "cl/1" is allowed and _exists_.
|
||||
//
|
||||
// Protect against finding service "www#c" in "http 10/tcp www#c" line
|
||||
//if (strchr(name, '#'))
|
||||
// return -1; // bad service name form
|
||||
//^^^^ the main code already catches this possibility.
|
||||
//
|
||||
// Current code allows e.g. service names like "http 80/tcp"
|
||||
// to map to port 80. Probably harmless?
|
||||
//if (skip_non_whitespace(name)[0])
|
||||
// return -1; // bad service name form (has whitespace)
|
||||
|
||||
sp = *p_etc_services;
|
||||
if (!sp) {
|
||||
//TODO: we need mmap_entire_file() for this use case!
|
||||
sp = xmalloc_open_read_close("/etc/services", NULL);
|
||||
if (!sp)
|
||||
return -1;
|
||||
*p_etc_services = sp;
|
||||
}
|
||||
namelen = strlen(name);
|
||||
while (*sp) {
|
||||
const char *pnstr;
|
||||
char *start, *end;
|
||||
unsigned n;
|
||||
|
||||
// First, find a possible service name without regard for line separators (!)
|
||||
start = strstr(sp, name);
|
||||
if (!start)
|
||||
return -1;
|
||||
sp = start + namelen;
|
||||
if (start != *p_etc_services && !isspace(start[-1])) {
|
||||
// There is a char before it, and it's not whitespace
|
||||
continue;
|
||||
}
|
||||
if (!(isspace(*sp) || *sp == '\0' || *sp == '#'))
|
||||
// After it: not whitespace/EOF/comment
|
||||
continue;
|
||||
// The found substring _is_ correctly delimited before and after
|
||||
|
||||
// Find beginning of the line we are on
|
||||
while (start != *p_etc_services && start[-1] != '\n')
|
||||
start--;
|
||||
start = skip_whitespace(start);
|
||||
// Is there a comment char between start of line and "service name"?
|
||||
if (memchr(start, '#', sp - start)) {
|
||||
// The "service name" we found is actually in comment / has comment in it.
|
||||
// Also rejects names with #:
|
||||
// service "www#c" won't be found even on "http 80/tcp www#c" line
|
||||
continue;
|
||||
}
|
||||
// Is the [start...sp) of the form "SERVNAME NUM/PROTO[ ALIAS[ ALIAS...][ ]]"?
|
||||
pnstr = skip_whitespace(skip_non_whitespace(start)); // go to NUM...
|
||||
|
||||
// I've seen this line in /etc/services of a real-world machine:
|
||||
//914c/g 211/tcp 914c-g
|
||||
// Now consider just a bit more pathological example:
|
||||
//914c/tcp 914/tcp 914/tcp
|
||||
// If we search for service name "914/tcp",
|
||||
// we'll find it at second word first, yet we must not reject this line
|
||||
// because the THIRD word also matches, and it's a valid match (tested on glibc!).
|
||||
// But in this case we must not match:
|
||||
//914c/tcp 914/tcp something-else
|
||||
if (sp - namelen == pnstr)
|
||||
continue; // the match is at NUM..., try matching further
|
||||
|
||||
n = bb_strtou(pnstr, &end, 10);
|
||||
if (n > 0xffff || *end != '/')
|
||||
continue; // NUM... part is not a valid port#, or has no slash
|
||||
|
||||
if (proto) {
|
||||
end++; // points to PROTO
|
||||
end = is_prefixed_with(end, proto);
|
||||
if (!end
|
||||
|| !(isspace(*end) || *end == '\0'
|
||||
|| *end == '#') // glibc treats "http 80/tcp#COMMENT" (no space!) as valid
|
||||
) {
|
||||
continue; // PROTO does not match
|
||||
}
|
||||
}
|
||||
// By now, either WORD or one of the ALIASes has to be the part
|
||||
// which was found by strstr()!
|
||||
return n;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Return port number for a service.
|
||||
// If "port" is a number use it as the port.
|
||||
// If "port" is a name it is looked up in /etc/services.
|
||||
// if NULL, return default_port
|
||||
unsigned FAST_FUNC bb_lookup_port(const char *port, const char *protocol, unsigned port_nr)
|
||||
{
|
||||
if (port) {
|
||||
port_nr = bb_strtou(port, NULL, 10);
|
||||
if (errno || port_nr > 65535) {
|
||||
char *p_etc_services = NULL;
|
||||
port_nr = bb_get_servport_by_name(&p_etc_services, port, protocol);
|
||||
if (port_nr > 0xffff) // -1?
|
||||
bb_error_msg_and_die("bad port '%s'", port);
|
||||
free(p_etc_services);
|
||||
}
|
||||
}
|
||||
return (uint16_t)port_nr;
|
||||
}
|
||||
@@ -45,3 +45,37 @@ gid_t* FAST_FUNC bb_getgroups(int *ngroups, gid_t *group_array)
|
||||
*ngroups = n;
|
||||
return group_array;
|
||||
}
|
||||
|
||||
uid_t FAST_FUNC get_cached_euid(uid_t *euid)
|
||||
{
|
||||
if (*euid == (uid_t)-1)
|
||||
*euid = geteuid();
|
||||
return *euid;
|
||||
}
|
||||
|
||||
gid_t FAST_FUNC get_cached_egid(gid_t *egid)
|
||||
{
|
||||
if (*egid == (gid_t)-1)
|
||||
*egid = getegid();
|
||||
return *egid;
|
||||
}
|
||||
|
||||
/* Return non-zero if GID is in our supplementary group list. */
|
||||
int FAST_FUNC is_in_supplementary_groups(struct cached_groupinfo *groupinfo, gid_t gid)
|
||||
{
|
||||
int i;
|
||||
int ngroups;
|
||||
gid_t *group_array;
|
||||
|
||||
if (groupinfo->ngroups == 0)
|
||||
groupinfo->supplementary_array = bb_getgroups(&groupinfo->ngroups, NULL);
|
||||
ngroups = groupinfo->ngroups;
|
||||
group_array = groupinfo->supplementary_array;
|
||||
|
||||
/* Search through the list looking for GID. */
|
||||
for (i = 0; i < ngroups; i++)
|
||||
if (gid == group_array[i])
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
+128
@@ -0,0 +1,128 @@
|
||||
/*
|
||||
* Utility routines.
|
||||
*
|
||||
* Copyright (C) 2025 by Denys Vlasenko <vda.linux@googlemail.com>
|
||||
*
|
||||
* Licensed under GPLv2, see file LICENSE in this source tree.
|
||||
*/
|
||||
//kbuild:lib-y += bitops.o
|
||||
|
||||
#include "libbb.h"
|
||||
|
||||
void FAST_FUNC xorbuf_3(void *dst, const void *src1, const void *src2, unsigned count)
|
||||
{
|
||||
uint8_t *d = dst;
|
||||
const uint8_t *s1 = src1;
|
||||
const uint8_t *s2 = src2;
|
||||
#if BB_UNALIGNED_MEMACCESS_OK
|
||||
while (count >= sizeof(long)) {
|
||||
*(long*)d = *(long*)s1 ^ *(long*)s2;
|
||||
count -= sizeof(long);
|
||||
d += sizeof(long);
|
||||
s1 += sizeof(long);
|
||||
s2 += sizeof(long);
|
||||
}
|
||||
#endif
|
||||
while (count--)
|
||||
*d++ = *s1++ ^ *s2++;
|
||||
}
|
||||
|
||||
void FAST_FUNC xorbuf(void *dst, const void *src, unsigned count)
|
||||
{
|
||||
xorbuf_3(dst, dst, src, count);
|
||||
}
|
||||
|
||||
void FAST_FUNC xorbuf16_aligned_long(void *dst, const void *src)
|
||||
{
|
||||
#if defined(__SSE__) /* any x86_64 has it */
|
||||
asm volatile(
|
||||
"\n movups (%0),%%xmm0"
|
||||
"\n movups (%1),%%xmm1" // can't just xorps(%1),%%xmm0:
|
||||
"\n xorps %%xmm1,%%xmm0" // SSE requires 16-byte alignment
|
||||
"\n movups %%xmm0,(%0)"
|
||||
"\n"
|
||||
: "=r" (dst), "=r" (src)
|
||||
: "0" (dst), "1" (src)
|
||||
: "xmm0", "xmm1", "memory"
|
||||
);
|
||||
#else
|
||||
unsigned long *d = dst;
|
||||
const unsigned long *s = src;
|
||||
d[0] ^= s[0];
|
||||
# if LONG_MAX <= 0x7fffffffffffffff
|
||||
d[1] ^= s[1];
|
||||
# if LONG_MAX == 0x7fffffff
|
||||
d[2] ^= s[2];
|
||||
d[3] ^= s[3];
|
||||
# endif
|
||||
# endif
|
||||
#endif
|
||||
}
|
||||
// The above can be inlined in libbb.h, in a way where compiler
|
||||
// is even free to use better addressing modes than (%reg), and
|
||||
// to keep the result in a register
|
||||
// (to not store it to memory after each XOR):
|
||||
//#if defined(__SSE__)
|
||||
//#include <xmmintrin.h>
|
||||
//^^^ or just: typedef float __m128_u attribute((__vector_size__(16),__may_alias__,__aligned__(1)));
|
||||
//static ALWAYS_INLINE void xorbuf16_aligned_long(void *dst, const void *src)
|
||||
//{
|
||||
// __m128_u xmm0, xmm1;
|
||||
// asm volatile(
|
||||
//"\n xorps %1,%0"
|
||||
// : "=x" (xmm0), "=x" (xmm1)
|
||||
// : "0" (*(__m128_u*)dst), "1" (*(__m128_u*)src)
|
||||
// );
|
||||
// *(__m128_u*)dst = xmm0; // this store may be optimized out!
|
||||
//}
|
||||
//#endif
|
||||
// but I don't trust gcc optimizer enough to not generate some monstrosity.
|
||||
// See GMULT() function in TLS code as an example.
|
||||
|
||||
void FAST_FUNC xorbuf64_3_aligned64(void *dst, const void *src1, const void *src2)
|
||||
{
|
||||
#if defined(__SSE__) /* any x86_64 has it */
|
||||
asm volatile(
|
||||
"\n movups 0*16(%1),%%xmm0"
|
||||
"\n movups 0*16(%2),%%xmm1" // can't just xorps(%2),%%xmm0:
|
||||
"\n xorps %%xmm1,%%xmm0" // SSE requires 16-byte alignment, we have only 8-byte
|
||||
"\n movups %%xmm0,0*16(%0)"
|
||||
"\n movups 1*16(%1),%%xmm0"
|
||||
"\n movups 1*16(%2),%%xmm1"
|
||||
"\n xorps %%xmm1,%%xmm0"
|
||||
"\n movups %%xmm0,1*16(%0)"
|
||||
"\n movups 2*16(%1),%%xmm0"
|
||||
"\n movups 2*16(%2),%%xmm1"
|
||||
"\n xorps %%xmm1,%%xmm0"
|
||||
"\n movups %%xmm0,2*16(%0)"
|
||||
"\n movups 3*16(%1),%%xmm0"
|
||||
"\n movups 3*16(%2),%%xmm1"
|
||||
"\n xorps %%xmm1,%%xmm0"
|
||||
"\n movups %%xmm0,3*16(%0)"
|
||||
"\n"
|
||||
: "=r" (dst), "=r" (src1), "=r" (src2)
|
||||
: "0" (dst), "1" (src1), "2" (src2)
|
||||
: "xmm0", "xmm1", "memory"
|
||||
);
|
||||
#else
|
||||
long *d = dst;
|
||||
const long *s1 = src1;
|
||||
const long *s2 = src2;
|
||||
unsigned count = 64 / sizeof(long);
|
||||
do {
|
||||
*d++ = *s1++ ^ *s2++;
|
||||
} while (--count != 0);
|
||||
#endif
|
||||
}
|
||||
|
||||
#if !BB_UNALIGNED_MEMACCESS_OK
|
||||
void FAST_FUNC xorbuf16(void *dst, const void *src)
|
||||
{
|
||||
#define p_aligned(a) (((uintptr_t)(a) & (sizeof(long)-1)) == 0)
|
||||
if (p_aligned(src) && p_aligned(dst)) {
|
||||
xorbuf16_aligned_long(dst, src);
|
||||
return;
|
||||
}
|
||||
xorbuf_3(dst, dst, src, 16);
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,20 @@
|
||||
/* vi: set sw=4 ts=4: */
|
||||
/*
|
||||
* Copyright (C) 2025 by Denys Vlasenko <vda.linux@googlemail.com>
|
||||
*
|
||||
* Licensed under GPLv2, see file LICENSE in this source tree.
|
||||
*/
|
||||
//kbuild:lib-y += c_escape.o
|
||||
|
||||
#include "libbb.h"
|
||||
|
||||
const char c_escape_conv_str00[] ALIGN1 =
|
||||
"\\""0""\0" // [0]:00
|
||||
"\\""a""\0" // [1]:07
|
||||
"\\""b""\0" // [2]:08
|
||||
"\\""t""\0" // [3]:09
|
||||
"\\""n""\0" // [4]:0a
|
||||
"\\""v""\0" // [5]:0b
|
||||
"\\""f""\0" // [6]:0c
|
||||
"\\""r" // [7]:0d
|
||||
;
|
||||
@@ -17,6 +17,7 @@
|
||||
|
||||
char* FAST_FUNC concat_path_file(const char *path, const char *filename)
|
||||
{
|
||||
#if 0
|
||||
char *lc;
|
||||
|
||||
if (!path)
|
||||
@@ -25,4 +26,78 @@ char* FAST_FUNC concat_path_file(const char *path, const char *filename)
|
||||
while (*filename == '/')
|
||||
filename++;
|
||||
return xasprintf("%s%s%s", path, (lc==NULL ? "/" : ""), filename);
|
||||
#else
|
||||
/* ^^^^^^^^^^^ timing of xasprintf-based code above:
|
||||
* real 7.074s
|
||||
* user 0.156s <<<
|
||||
* sys 6.394s
|
||||
* "rm -rf" of a Linux kernel tree from tmpfs (run time still dominated by in-kernel work, though)
|
||||
* real 6.989s
|
||||
* user 0.055s <<< 3 times less CPU used
|
||||
* sys 6.450s
|
||||
* vvvvvvvvvvv timing of open-coded malloc+memcpy code below (+59 bytes):
|
||||
*/
|
||||
char *buf, *p;
|
||||
size_t n1, n2, n3;
|
||||
|
||||
while (*filename == '/')
|
||||
filename++;
|
||||
|
||||
if (!path || !path[0])
|
||||
return xstrdup(filename);
|
||||
|
||||
n1 = strlen(path);
|
||||
n2 = (path[n1 - 1] != '/'); /* 1: "path has no trailing slash" */
|
||||
n3 = strlen(filename) + 1;
|
||||
|
||||
buf = xmalloc(n1 + n2 + n3);
|
||||
p = mempcpy(buf, path, n1);
|
||||
if (n2)
|
||||
*p++ = '/';
|
||||
memcpy(p, filename, n3);
|
||||
return buf;
|
||||
#endif
|
||||
}
|
||||
|
||||
/* If second component comes from struct dirent,
|
||||
* it's possible to eliminate one strlen() by using name length
|
||||
* provided by kernel in struct dirent. See below.
|
||||
* However, the win seems to be insignificant.
|
||||
*/
|
||||
|
||||
#if 0
|
||||
|
||||
/* Extract d_namlen from struct dirent */
|
||||
static size_t get_d_namlen(const struct dirent *de)
|
||||
{
|
||||
#if defined(_DIRENT_HAVE_D_NAMLEN)
|
||||
return de->d_namlen;
|
||||
#elif defined(_DIRENT_HAVE_D_RECLEN)
|
||||
const size_t prefix_sz = offsetof(struct dirent, d_name);
|
||||
return de->d_reclen - prefix_sz;
|
||||
#else
|
||||
return strlen(de->d_name);
|
||||
#endif
|
||||
}
|
||||
|
||||
char* FAST_FUNC concat_path_dirent(const char *path, const struct dirent *de)
|
||||
{
|
||||
char *buf, *p;
|
||||
size_t n1, n2, n3;
|
||||
|
||||
if (!path || !path[0])
|
||||
return xstrdup(de->d_name);
|
||||
|
||||
n1 = strlen(path);
|
||||
n2 = (path[n1 - 1] != '/');
|
||||
n3 = get_d_namlen(de) + 1;
|
||||
|
||||
buf = xmalloc(n1 + n2 + n3);
|
||||
p = mempcpy(buf, path, n1);
|
||||
if (n2)
|
||||
*p++ = '/';
|
||||
memcpy(p, de->d_name, n3);
|
||||
return buf;
|
||||
}
|
||||
|
||||
#endif
|
||||
+20
-1
@@ -9,8 +9,27 @@
|
||||
#include "libbb.h"
|
||||
|
||||
#if defined(__clang_major__) && __clang_major__ >= 9
|
||||
/* Clang/llvm drops assignment to "constant" storage. Silently.
|
||||
* Needs serious convincing to not eliminate the store.
|
||||
*/
|
||||
static ALWAYS_INLINE void* not_const_pp(const void *p)
|
||||
{
|
||||
void *pp;
|
||||
asm volatile (
|
||||
"# forget that p points to const"
|
||||
: /*outputs*/ "=r" (pp)
|
||||
: /*inputs*/ "0" (p)
|
||||
);
|
||||
return pp;
|
||||
}
|
||||
void FAST_FUNC ASSIGN_CONST_PTR(const void *pptr, void *v)
|
||||
{
|
||||
*(void**)not_const_pp(pptr) = v;
|
||||
barrier();
|
||||
}
|
||||
void FAST_FUNC XZALLOC_CONST_PTR(const void *pptr, size_t size)
|
||||
{
|
||||
ASSIGN_CONST_PTR(pptr, xzalloc(size));
|
||||
*(void**)not_const_pp(pptr) = xzalloc(size);
|
||||
barrier();
|
||||
}
|
||||
#endif
|
||||
+46
-23
@@ -198,9 +198,11 @@ static NOINLINE void rewrite(priv_dumper_t *dumper, FS *fs)
|
||||
if (!e)
|
||||
goto DO_BAD_CONV_CHAR;
|
||||
pr->flags = F_INT;
|
||||
if (e > int_convs + 1) /* not d or i? */
|
||||
pr->flags = F_UINT;
|
||||
byte_count_str = "\010\004\002\001";
|
||||
if (e > int_convs + 1) { /* not d or i? */
|
||||
pr->flags = F_UINT;
|
||||
byte_count_str++;
|
||||
}
|
||||
goto DO_BYTE_COUNT;
|
||||
} else
|
||||
if (strchr(int_convs, *p1)) { /* %d etc */
|
||||
@@ -476,37 +478,52 @@ static void bpad(PR *pr)
|
||||
continue;
|
||||
}
|
||||
|
||||
static const char conv_str[] ALIGN1 =
|
||||
"\0" "\\""0""\0"
|
||||
"\007""\\""a""\0"
|
||||
"\b" "\\""b""\0"
|
||||
"\f" "\\""f""\0"
|
||||
"\n" "\\""n""\0"
|
||||
"\r" "\\""r""\0"
|
||||
"\t" "\\""t""\0"
|
||||
"\v" "\\""v""\0"
|
||||
;
|
||||
|
||||
static void conv_c(PR *pr, unsigned char *p)
|
||||
{
|
||||
const char *str = conv_str;
|
||||
const char *str;
|
||||
unsigned char ch;
|
||||
|
||||
do {
|
||||
if (*p == *str) {
|
||||
++str;
|
||||
goto strpr; /* map e.g. '\n' to "\\n" */
|
||||
}
|
||||
str += 4;
|
||||
} while (*str);
|
||||
ch = *p;
|
||||
if (ch == 0 || (ch -= 6, (signed char)ch > 0 && ch <= 7)) {
|
||||
/* map chars 0,7..13 to "\0","\{a,b,t,n,v,f,r}" */
|
||||
str = c_escape_conv_str00 + 3 * ch;
|
||||
goto strpr;
|
||||
}
|
||||
|
||||
if (isprint_asciionly(*p)) {
|
||||
*pr->cchar = 'c';
|
||||
printf(pr->fmt, *p);
|
||||
} else {
|
||||
#if defined(__i386__) || defined(__x86_64__)
|
||||
/* Abuse partial register operations */
|
||||
uint32_t buf;
|
||||
unsigned n = *p;
|
||||
asm ( //00000000 00000000 00000000 aabbbccc
|
||||
"\n shll $10,%%eax" //00000000 000000aa bbbccc00 00000000
|
||||
"\n shrw $5,%%ax" //00000000 000000aa 00000bbb ccc00000
|
||||
"\n shrb $5,%%al" //00000000 000000aa 00000bbb 00000ccc
|
||||
"\n shll $8,%%eax" //000000aa 00000bbb 00000ccc 00000000
|
||||
"\n bswapl %%eax" //00000000 00000ccc 00000bbb 000000aa
|
||||
"\n addl $0x303030,%%eax"
|
||||
"\n" : "=a" (n)
|
||||
: "0" (n)
|
||||
);
|
||||
buf = n;
|
||||
str = (void*)&buf;
|
||||
#elif 1
|
||||
char buf[4];
|
||||
/* gcc-8.0.1 needs lots of casts to shut up */
|
||||
sprintf(buf, "%03o", (unsigned)(uint8_t)*p);
|
||||
str = buf;
|
||||
#else // use faster version? +20 bytes of code relative to sprintf() method
|
||||
char buf[4];
|
||||
buf[3] = '\0';
|
||||
ch = *p;
|
||||
buf[2] = '0' + (ch & 7); ch >>= 3;
|
||||
buf[1] = '0' + (ch & 7); ch >>= 3;
|
||||
buf[0] = '0' + ch;
|
||||
str = buf;
|
||||
#endif
|
||||
strpr:
|
||||
*pr->cchar = 's';
|
||||
printf(pr->fmt, str);
|
||||
@@ -665,15 +682,21 @@ static NOINLINE void display(priv_dumper_t* dumper)
|
||||
conv_u(pr, bp);
|
||||
break;
|
||||
case F_UINT: {
|
||||
union {
|
||||
uint16_t uval16;
|
||||
uint32_t uval32;
|
||||
} u;
|
||||
unsigned value = (unsigned char)*bp;
|
||||
switch (pr->bcnt) {
|
||||
case 1:
|
||||
break;
|
||||
case 2:
|
||||
move_from_unaligned16(value, bp);
|
||||
move_from_unaligned16(u.uval16, bp);
|
||||
value = u.uval16;
|
||||
break;
|
||||
case 4:
|
||||
move_from_unaligned32(value, bp);
|
||||
move_from_unaligned32(u.uval32, bp);
|
||||
value = u.uval32;
|
||||
break;
|
||||
/* case 8: no users yet */
|
||||
}
|
||||
|
||||
+30
-26
@@ -21,14 +21,11 @@ int FAST_FUNC file_is_executable(const char *name)
|
||||
/* search (*PATHp) for an executable file;
|
||||
* return allocated string containing full path if found;
|
||||
* PATHp points to the component after the one where it was found
|
||||
* (or NULL),
|
||||
* (or NULL if found in last component),
|
||||
* you may call find_executable again with this PATHp to continue
|
||||
* (if it's not NULL).
|
||||
* return NULL otherwise; (PATHp is undefined)
|
||||
* in all cases (*PATHp) contents are temporarily modified
|
||||
* but are restored on return (s/:/NUL/ and back).
|
||||
* return NULL otherwise (PATHp is undefined)
|
||||
*/
|
||||
char* FAST_FUNC find_executable(const char *filename, char **PATHp)
|
||||
char* FAST_FUNC find_executable(const char *name, const char **PATHp)
|
||||
{
|
||||
/* About empty components in $PATH:
|
||||
* http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap08.html
|
||||
@@ -38,38 +35,45 @@ char* FAST_FUNC find_executable(const char *filename, char **PATHp)
|
||||
* initial colon preceding the rest of the list, or as a trailing colon
|
||||
* following the rest of the list.
|
||||
*/
|
||||
char *p, *n;
|
||||
char *p = (char*) *PATHp;
|
||||
|
||||
p = *PATHp;
|
||||
while (p) {
|
||||
int ex;
|
||||
if (!p)
|
||||
return NULL;
|
||||
while (1) {
|
||||
const char *end = strchrnul(p, ':');
|
||||
int sz = end - p;
|
||||
|
||||
n = strchr(p, ':');
|
||||
if (n) *n = '\0';
|
||||
p = concat_path_file(
|
||||
p[0] ? p : ".", /* handle "::" case */
|
||||
filename
|
||||
);
|
||||
ex = file_is_executable(p);
|
||||
if (n) *n++ = ':';
|
||||
if (ex) {
|
||||
*PATHp = n;
|
||||
if (sz != 0) {
|
||||
p = xasprintf("%.*s/%s", sz, p, name);
|
||||
} else {
|
||||
/* We have xxx::yyy in $PATH,
|
||||
* it means "use current dir" */
|
||||
p = xstrdup(name);
|
||||
// A bit of discrepancy wrt the path used if file is found here.
|
||||
// bash 5.2.15 "type" returns "./NAME".
|
||||
// GNU which v2.21 returns "/CUR/DIR/NAME".
|
||||
// With -a, both skip over all colons: xxx::::yyy is the same as xxx::yyy,
|
||||
// current dir is not tried the second time.
|
||||
}
|
||||
if (file_is_executable(p)) {
|
||||
*PATHp = (*end ? end+1 : NULL);
|
||||
return p;
|
||||
}
|
||||
free(p);
|
||||
p = n;
|
||||
} /* on loop exit p == NULL */
|
||||
return p;
|
||||
if (*end == '\0')
|
||||
return NULL;
|
||||
p = (char *) end + 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* search $PATH for an executable file;
|
||||
* return 1 if found;
|
||||
* return 0 otherwise;
|
||||
*/
|
||||
int FAST_FUNC executable_exists(const char *filename)
|
||||
int FAST_FUNC executable_exists(const char *name)
|
||||
{
|
||||
char *path = getenv("PATH");
|
||||
char *ret = find_executable(filename, &path);
|
||||
const char *path = getenv("PATH");
|
||||
char *ret = find_executable(name, &path);
|
||||
free(ret);
|
||||
return ret != NULL;
|
||||
}
|
||||
|
||||
@@ -24,12 +24,12 @@ const char* FAST_FUNC bb_basename(const char *name)
|
||||
*/
|
||||
char* FAST_FUNC bb_get_last_path_component_nostrip(const char *path)
|
||||
{
|
||||
char *slash = strrchr(path, '/');
|
||||
const char *slash = strrchr(path, '/');
|
||||
|
||||
if (!slash || (slash == path && !slash[1]))
|
||||
return (char*)path;
|
||||
|
||||
return slash + 1;
|
||||
return (char*)slash + 1;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
+18
-12
@@ -93,7 +93,7 @@ getopt32(char **argv, const char *applet_opts, ...)
|
||||
|
||||
"!" If the first character in the applet_opts string is a '!',
|
||||
report bad options, missing required options,
|
||||
inconsistent options with all-ones return value (instead of abort.
|
||||
inconsistent options with all-ones return value instead of aborting.
|
||||
|
||||
"+" If the first character in the applet_opts string is a plus,
|
||||
then option processing will stop as soon as a non-option is
|
||||
@@ -265,7 +265,7 @@ Special characters:
|
||||
for "long options only" cases, such as tar --exclude=PATTERN,
|
||||
wget --header=HDR cases.
|
||||
|
||||
"a?b" A "?" between an option and a group of options means that
|
||||
"a?bc" A "?" between an option and a group of options means that
|
||||
at least one of them is required to occur if the first option
|
||||
occurs in preceding command line arguments.
|
||||
|
||||
@@ -348,9 +348,6 @@ vgetopt32(char **argv, const char *applet_opts, const char *applet_long_options,
|
||||
unsigned trigger;
|
||||
int min_arg = 0;
|
||||
int max_arg = -1;
|
||||
int spec_flgs = 0;
|
||||
|
||||
#define SHOW_USAGE_IF_ERROR 1
|
||||
|
||||
on_off = complementary;
|
||||
memset(on_off, 0, sizeof(complementary));
|
||||
@@ -449,9 +446,7 @@ vgetopt32(char **argv, const char *applet_opts, const char *applet_long_options,
|
||||
continue;
|
||||
c = s[1];
|
||||
if (*s == '?') {
|
||||
if (c < '0' || c > '9') {
|
||||
spec_flgs |= SHOW_USAGE_IF_ERROR;
|
||||
} else {
|
||||
if (c >= '0' && c <= '9') {
|
||||
max_arg = c - '0';
|
||||
s++;
|
||||
}
|
||||
@@ -465,8 +460,10 @@ vgetopt32(char **argv, const char *applet_opts, const char *applet_long_options,
|
||||
continue;
|
||||
}
|
||||
if (*s == '=') {
|
||||
min_arg = max_arg = c - '0';
|
||||
s++;
|
||||
if (c >= '0' && c <= '9') {
|
||||
min_arg = max_arg = c - '0';
|
||||
s++;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
for (on_off = complementary; on_off->opt_char; on_off++)
|
||||
@@ -533,6 +530,7 @@ vgetopt32(char **argv, const char *applet_opts, const char *applet_long_options,
|
||||
* "fake" short options, like this one:
|
||||
* wget $'-\203' "Test: test" http://kernel.org/
|
||||
* (supposed to act as --header, but doesn't) */
|
||||
next_opt:
|
||||
#if ENABLE_LONG_OPTS
|
||||
while ((c = getopt_long(argc, argv, applet_opts,
|
||||
long_options, NULL)) != -1) {
|
||||
@@ -547,8 +545,16 @@ vgetopt32(char **argv, const char *applet_opts, const char *applet_long_options,
|
||||
* but we construct long opts so that flag
|
||||
* is always NULL (see above) */
|
||||
if (on_off->opt_char == '\0' /* && c != '\0' */) {
|
||||
/* c is probably '?' - "bad option" */
|
||||
goto error;
|
||||
/* We reached the end of complementary[] and did not find -c */
|
||||
if (c == '?') /* getopt says: "bad option, or option has no required argument" */
|
||||
goto error;
|
||||
/* if there were options beyond 32 bits (example: ls),
|
||||
* they got no complementary[] slot, and no result bit.
|
||||
* IOW: they must be "accept but ignore" options.
|
||||
* For them, we end up here.
|
||||
*/
|
||||
//bb_error_msg("ignored option '%c', skipping", c);
|
||||
goto next_opt;
|
||||
}
|
||||
}
|
||||
if (flags & on_off->incongruously)
|
||||
|
||||
+58
-24
@@ -9,35 +9,69 @@
|
||||
|
||||
#define DEBUG 0
|
||||
|
||||
// On modern systems (old ways were more kludgy with setuid "pt_chown" binary):
|
||||
// ptyfd = open("/dev/ptmx"):
|
||||
// Kernel creates slave /dev/pts/N with owner = real UID of opener,
|
||||
// group and mode as configured by /dev/pts mount options.
|
||||
// The mode is safe with standard mount options (gid=5,mode=620).
|
||||
//
|
||||
// grantpt(ptyfd) is now only a verification step. In glibc:
|
||||
// Calls ioctl(fd, TIOCGPTN, &minor) to confirm ptyfd is valid master pty.
|
||||
// If ioctl fails with ENOTTY, remaps to EINVAL (POSIX).
|
||||
// Otherwise returns 0. (No chown/chmod occurs.)
|
||||
//
|
||||
// unlockpt(ptyfd) is ioctl(TIOCSPTLCK, 0): clears optional protection flag
|
||||
// on slave. If flag was set, open(slave) fails with EIO for everyone (even root)
|
||||
// until unlocked. [Do modern kernels still set the lock in open("/dev/ptmx")?]
|
||||
// The lock historically prevented opening slave before grantpt() fixes perms
|
||||
// in misconfigured/old setups where kernel didn't set 0620 atomically.
|
||||
// Today it's mostly belt-and-suspenders + POSIX compatibility.
|
||||
//
|
||||
// The attack thwarted by the lock (assuming unprivileged adversary):
|
||||
// you open /dev/ptmx, kernel allocates /dev/pts/N with overly permissive mode.
|
||||
// Adversary manages to open /dev/pts/N before your grantpt() call
|
||||
// locks down permissions.
|
||||
// Typically, /dev/pts mount options are gid=5,mode=620, 5 = "tty" group,
|
||||
// system is set up with only trusted binaries and no users in "tty" group.
|
||||
// In this case, the locking mechanism is overkill (would be safe without it).
|
||||
// To fortify more, you can mount with gid=0,mode=600. Tools like "wall",
|
||||
// "mesg" would stop working.
|
||||
// Processes with my own UID can still race against me, but I probably
|
||||
// trust myself...
|
||||
//
|
||||
// ptsname(ptyfd), internally ioctl(TIOCGPTN): get index, build
|
||||
// "/dev/pts/NN" string which refers to the slave pty.
|
||||
int FAST_FUNC xgetpty(char *line)
|
||||
{
|
||||
int p;
|
||||
|
||||
#if ENABLE_FEATURE_DEVPTS
|
||||
p = open("/dev/ptmx", O_RDWR);
|
||||
if (p >= 0) {
|
||||
grantpt(p); /* chmod+chown corresponding slave pty */
|
||||
unlockpt(p); /* (what does this do?) */
|
||||
int ptyfd;
|
||||
|
||||
ptyfd = open("/dev/ptmx", O_RDWR);
|
||||
if (ptyfd < 0)
|
||||
bb_simple_perror_msg_and_die("can't find free pty");
|
||||
grantpt(ptyfd); /* chmod+chown corresponding slave pty */
|
||||
unlockpt(ptyfd); /* allow open() on slave /dev node */
|
||||
# ifndef HAVE_PTSNAME_R
|
||||
{
|
||||
const char *name;
|
||||
name = ptsname(p); /* find out the name of slave pty */
|
||||
if (!name) {
|
||||
bb_simple_perror_msg_and_die("ptsname error (is /dev/pts mounted?)");
|
||||
}
|
||||
safe_strncpy(line, name, GETPTY_BUFSIZE);
|
||||
}
|
||||
# else
|
||||
/* find out the name of slave pty */
|
||||
if (ptsname_r(p, line, GETPTY_BUFSIZE-1) != 0) {
|
||||
{
|
||||
const char *name;
|
||||
name = ptsname(ptyfd); /* find out the name of slave pty */
|
||||
if (!name) {
|
||||
bb_simple_perror_msg_and_die("ptsname error (is /dev/pts mounted?)");
|
||||
}
|
||||
line[GETPTY_BUFSIZE-1] = '\0';
|
||||
# endif
|
||||
return p;
|
||||
safe_strncpy(line, name, GETPTY_BUFSIZE);
|
||||
}
|
||||
# else
|
||||
/* find out the name of slave pty */
|
||||
if (ptsname_r(ptyfd, line, GETPTY_BUFSIZE-1) != 0) {
|
||||
bb_simple_perror_msg_and_die("ptsname error (is /dev/pts mounted?)");
|
||||
}
|
||||
line[GETPTY_BUFSIZE-1] = '\0';
|
||||
# endif
|
||||
return ptyfd;
|
||||
|
||||
#else
|
||||
struct stat stb;
|
||||
int ptyfd;
|
||||
int i;
|
||||
int j;
|
||||
|
||||
@@ -53,13 +87,13 @@ int FAST_FUNC xgetpty(char *line)
|
||||
line[9] = j < 10 ? j + '0' : j - 10 + 'a';
|
||||
if (DEBUG)
|
||||
fprintf(stderr, "Trying to open device: %s\n", line);
|
||||
p = open(line, O_RDWR | O_NOCTTY);
|
||||
if (p >= 0) {
|
||||
ptyfd = open(line, O_RDWR | O_NOCTTY);
|
||||
if (ptyfd >= 0) {
|
||||
line[5] = 't';
|
||||
return p;
|
||||
return ptyfd;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif /* FEATURE_DEVPTS */
|
||||
bb_simple_error_msg_and_die("can't find free pty");
|
||||
#endif /* FEATURE_DEVPTS */
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
* Copyright (C) 2025 Denys Vlasenko
|
||||
*
|
||||
* Licensed under GPLv2, see file LICENSE in this source tree.
|
||||
*/
|
||||
//kbuild:lib-$(CONFIG_TLS) += hash_hmac.o
|
||||
//kbuild:lib-$(CONFIG_USE_BB_CRYPT_YES) += hash_hmac.o
|
||||
|
||||
#include "libbb.h"
|
||||
|
||||
// RFC 2104:
|
||||
// HMAC(key, text) based on a hash H (say, sha256) is:
|
||||
// ipad = [0x36 x INSIZE]
|
||||
// opad = [0x5c x INSIZE]
|
||||
// HMAC(key, text) = H((key XOR opad) + H((key XOR ipad) + text))
|
||||
//
|
||||
// H(key XOR opad) and H(key XOR ipad) can be precomputed
|
||||
// if we often need HMAC hmac with the same key.
|
||||
//
|
||||
// text is often given in disjoint pieces.
|
||||
void FAST_FUNC hmac_begin(hmac_ctx_t *ctx, const uint8_t *key, unsigned key_size, md5sha_begin_func *begin)
|
||||
{
|
||||
#if HMAC_ONLY_SHA256
|
||||
#define begin sha256_begin
|
||||
#endif
|
||||
uint8_t key_xor_ipad[SHA2_INSIZE];
|
||||
uint8_t key_xor_opad[SHA2_INSIZE];
|
||||
unsigned i;
|
||||
|
||||
// "The authentication key can be of any length up to INSIZE, the
|
||||
// block length of the hash function. Applications that use keys longer
|
||||
// than INSIZE bytes will first hash the key using H and then use the
|
||||
// resultant OUTSIZE byte string as the actual key to HMAC."
|
||||
if (key_size > SHA2_INSIZE) {
|
||||
uint8_t tempkey[SHA1_OUTSIZE < SHA256_OUTSIZE ? SHA256_OUTSIZE : SHA1_OUTSIZE];
|
||||
/* use ctx->hashed_key_xor_ipad as scratch ctx */
|
||||
begin(&ctx->hashed_key_xor_ipad);
|
||||
md5sha_hash(&ctx->hashed_key_xor_ipad, key, key_size);
|
||||
key_size = sha_end(&ctx->hashed_key_xor_ipad, tempkey);
|
||||
key = tempkey;
|
||||
}
|
||||
|
||||
for (i = 0; i < key_size; i++) {
|
||||
key_xor_ipad[i] = key[i] ^ 0x36;
|
||||
key_xor_opad[i] = key[i] ^ 0x5c;
|
||||
}
|
||||
for (; i < SHA2_INSIZE; i++) {
|
||||
key_xor_ipad[i] = 0x36;
|
||||
key_xor_opad[i] = 0x5c;
|
||||
}
|
||||
|
||||
begin(&ctx->hashed_key_xor_ipad);
|
||||
begin(&ctx->hashed_key_xor_opad);
|
||||
md5sha_hash(&ctx->hashed_key_xor_ipad, key_xor_ipad, SHA2_INSIZE);
|
||||
md5sha_hash(&ctx->hashed_key_xor_opad, key_xor_opad, SHA2_INSIZE);
|
||||
}
|
||||
#undef begin
|
||||
|
||||
unsigned FAST_FUNC hmac_end(hmac_ctx_t *ctx, uint8_t *out)
|
||||
{
|
||||
unsigned len = sha_end(&ctx->hashed_key_xor_ipad, out);
|
||||
/* out = H((key XOR opad) + out) */
|
||||
md5sha_hash(&ctx->hashed_key_xor_opad, out, len);
|
||||
return sha_end(&ctx->hashed_key_xor_opad, out);
|
||||
}
|
||||
|
||||
unsigned FAST_FUNC hmac_block(const uint8_t *key, unsigned key_size, md5sha_begin_func *begin, const void *in, unsigned sz, uint8_t *out)
|
||||
{
|
||||
hmac_ctx_t ctx;
|
||||
hmac_begin(&ctx, key, key_size, begin);
|
||||
hmac_hash(&ctx, in, sz);
|
||||
return hmac_end(&ctx, out);
|
||||
}
|
||||
|
||||
/* TLS helpers */
|
||||
|
||||
void FAST_FUNC hmac_hash_v(
|
||||
hmac_ctx_t *ctx,
|
||||
va_list va)
|
||||
{
|
||||
uint8_t *in;
|
||||
|
||||
/* ctx->hashed_key_xor_ipad contains unclosed "H((key XOR ipad) +" state */
|
||||
/* ctx->hashed_key_xor_opad contains unclosed "H((key XOR opad) +" state */
|
||||
|
||||
/* calculate out = H((key XOR ipad) + text) */
|
||||
while ((in = va_arg(va, uint8_t*)) != NULL) {
|
||||
unsigned size = va_arg(va, unsigned);
|
||||
md5sha_hash(&ctx->hashed_key_xor_ipad, in, size);
|
||||
}
|
||||
}
|
||||
|
||||
/* Using HMAC state, make a copy of it (IOW: without affecting this state!)
|
||||
* hash in the list of (ptr,size) blocks, and finish the HMAC to out[] buffer.
|
||||
* This function is useful for TLS PRF.
|
||||
*/
|
||||
unsigned FAST_FUNC hmac_peek_hash(hmac_ctx_t *ctx, uint8_t *out, ...)
|
||||
{
|
||||
hmac_ctx_t tmpctx = *ctx; /* struct copy */
|
||||
va_list va;
|
||||
|
||||
va_start(va, out);
|
||||
hmac_hash_v(&tmpctx, va);
|
||||
va_end(va);
|
||||
|
||||
return hmac_end(&tmpctx, out);
|
||||
}
|
||||
+47
-13
@@ -11,7 +11,7 @@
|
||||
#define STR1(s) #s
|
||||
#define STR(s) STR1(s)
|
||||
|
||||
#define NEED_SHA512 (ENABLE_SHA512SUM || ENABLE_USE_BB_CRYPT_SHA)
|
||||
#define NEED_SHA512 (ENABLE_SHA512SUM || ENABLE_SHA384SUM || ENABLE_USE_BB_CRYPT_SHA)
|
||||
|
||||
#if ENABLE_SHA1_HWACCEL || ENABLE_SHA256_HWACCEL
|
||||
# if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
|
||||
@@ -1032,7 +1032,7 @@ static const sha_K_int sha_K[] ALIGN8 = {
|
||||
K(0x84c87814a1f0ab72ULL), K(0x8cc702081a6439ecULL),
|
||||
K(0x90befffa23631e28ULL), K(0xa4506cebde82bde9ULL),
|
||||
K(0xbef9a3f7b2c67915ULL), K(0xc67178f2e372532bULL),
|
||||
#if NEED_SHA512 /* [64]+ are used for sha512 only */
|
||||
#if NEED_SHA512 /* [64]+ are used for sha384 and sha512 only */
|
||||
K(0xca273eceea26619cULL), K(0xd186b8c721c0c207ULL),
|
||||
K(0xeada7dd6cde0eb1eULL), K(0xf57d4f7fee6ed178ULL),
|
||||
K(0x06f067aa72176fbaULL), K(0x0a637dc5a2c898a6ULL),
|
||||
@@ -1229,11 +1229,20 @@ static const uint32_t init512_lo[] ALIGN4 = {
|
||||
0x137e2179,
|
||||
};
|
||||
#endif /* NEED_SHA512 */
|
||||
|
||||
// Note: SHA-384 is identical to SHA-512, except that initial hash values are
|
||||
// 0xcbbb9d5dc1059ed8, 0x629a292a367cd507, 0x9159015a3070dd17, 0x152fecd8f70e5939,
|
||||
// 0x67332667ffc00b31, 0x8eb44a8768581511, 0xdb0c2e0d64f98fa7, 0x47b5481dbefa4fa4,
|
||||
// and the output is constructed by omitting last two 64-bit words of it.
|
||||
#if ENABLE_SHA384SUM
|
||||
static const uint64_t init384[] ALIGN8 = {
|
||||
0,
|
||||
0,
|
||||
0xcbbb9d5dc1059ed8,
|
||||
0x629a292a367cd507,
|
||||
0x9159015a3070dd17,
|
||||
0x152fecd8f70e5939,
|
||||
0x67332667ffc00b31,
|
||||
0x8eb44a8768581511,
|
||||
0xdb0c2e0d64f98fa7,
|
||||
0x47b5481dbefa4fa4,
|
||||
};
|
||||
#endif
|
||||
|
||||
/* Initialize structure containing state of computation.
|
||||
(FIPS 180-2:5.3.2) */
|
||||
@@ -1255,9 +1264,19 @@ void FAST_FUNC sha256_begin(sha256_ctx_t *ctx)
|
||||
#endif
|
||||
}
|
||||
|
||||
#if NEED_SHA512
|
||||
#if ENABLE_SHA384SUM
|
||||
/* Initialize structure containing state of computation.
|
||||
(FIPS 180-2:5.3.3) */
|
||||
void FAST_FUNC sha384_begin(sha512_ctx_t *ctx)
|
||||
{
|
||||
memcpy(&ctx->total64, init384, sizeof(init384));
|
||||
/*ctx->total64[0] = ctx->total64[1] = 0; - already done */
|
||||
}
|
||||
#endif
|
||||
|
||||
#if NEED_SHA512
|
||||
/* Initialize structure containing state of computation.
|
||||
(FIPS 180-2:5.3.4) */
|
||||
void FAST_FUNC sha512_begin(sha512_ctx_t *ctx)
|
||||
{
|
||||
int i;
|
||||
@@ -1313,7 +1332,9 @@ unsigned FAST_FUNC sha1_end(sha1_ctx_t *ctx, void *resbuf)
|
||||
hash_size = 8;
|
||||
if (ctx->process_block == sha1_process_block64
|
||||
#if ENABLE_SHA1_HWACCEL
|
||||
# if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
|
||||
|| ctx->process_block == sha1_process_block64_shaNI
|
||||
# endif
|
||||
#endif
|
||||
) {
|
||||
hash_size = 5;
|
||||
@@ -1330,7 +1351,7 @@ unsigned FAST_FUNC sha1_end(sha1_ctx_t *ctx, void *resbuf)
|
||||
}
|
||||
|
||||
#if NEED_SHA512
|
||||
unsigned FAST_FUNC sha512_end(sha512_ctx_t *ctx, void *resbuf)
|
||||
static unsigned FAST_FUNC sha512384_end(sha512_ctx_t *ctx, void *resbuf, unsigned outsize)
|
||||
{
|
||||
unsigned bufpos = ctx->total64[0] & 127;
|
||||
|
||||
@@ -1361,11 +1382,21 @@ unsigned FAST_FUNC sha512_end(sha512_ctx_t *ctx, void *resbuf)
|
||||
for (i = 0; i < ARRAY_SIZE(ctx->hash); ++i)
|
||||
ctx->hash[i] = SWAP_BE64(ctx->hash[i]);
|
||||
}
|
||||
memcpy(resbuf, ctx->hash, sizeof(ctx->hash));
|
||||
return sizeof(ctx->hash);
|
||||
memcpy(resbuf, ctx->hash, outsize);
|
||||
return outsize;
|
||||
}
|
||||
unsigned FAST_FUNC sha512_end(sha384_ctx_t *ctx, void *resbuf)
|
||||
{
|
||||
return sha512384_end(ctx, resbuf, SHA512_OUTSIZE);
|
||||
}
|
||||
#endif /* NEED_SHA512 */
|
||||
|
||||
#if ENABLE_SHA384SUM
|
||||
unsigned FAST_FUNC sha384_end(sha384_ctx_t *ctx, void *resbuf)
|
||||
{
|
||||
return sha512384_end(ctx, resbuf, SHA384_OUTSIZE);
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* The Keccak sponge function, designed by Guido Bertoni, Joan Daemen,
|
||||
@@ -1902,6 +1933,8 @@ void FAST_FUNC sha3_hash(sha3_ctx_t *ctx, const void *buffer, size_t len)
|
||||
|
||||
unsigned FAST_FUNC sha3_end(sha3_ctx_t *ctx, void *resbuf)
|
||||
{
|
||||
unsigned hash_len;
|
||||
|
||||
/* Padding */
|
||||
uint8_t *buf = (uint8_t*)ctx->state;
|
||||
/*
|
||||
@@ -1924,6 +1957,7 @@ unsigned FAST_FUNC sha3_end(sha3_ctx_t *ctx, void *resbuf)
|
||||
sha3_process_block72(ctx->state);
|
||||
|
||||
/* Output */
|
||||
memcpy(resbuf, ctx->state, 64);
|
||||
return 64;
|
||||
hash_len = (1600/8 - ctx->input_block_bytes) / 2;
|
||||
memcpy(resbuf, ctx->state, hash_len);
|
||||
return hash_len;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
/* vi: set sw=4 ts=4: */
|
||||
/*
|
||||
* Utility routines.
|
||||
*
|
||||
* Copyright (C) 2025 Denys Vlasenko
|
||||
*
|
||||
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
||||
*/
|
||||
//kbuild:lib-y += hash_sha256_block.o
|
||||
#include "libbb.h"
|
||||
|
||||
void FAST_FUNC
|
||||
sha256_block(const void *in, size_t len, uint8_t hash[32])
|
||||
{
|
||||
sha256_ctx_t ctx;
|
||||
sha256_begin(&ctx);
|
||||
sha256_hash(&ctx, in, len);
|
||||
sha256_end(&ctx, hash);
|
||||
}
|
||||
+118
-100
@@ -34,21 +34,21 @@
|
||||
#define MSG %xmm0
|
||||
#define STATE0 %xmm1
|
||||
#define STATE1 %xmm2
|
||||
#define MSGTMP0 %xmm3
|
||||
#define MSGTMP1 %xmm4
|
||||
#define MSGTMP2 %xmm5
|
||||
#define MSGTMP3 %xmm6
|
||||
#define MSG0 %xmm3
|
||||
#define MSG1 %xmm4
|
||||
#define MSG2 %xmm5
|
||||
#define MSG3 %xmm6
|
||||
|
||||
#define XMMTMP %xmm7
|
||||
|
||||
#define SHUF(a,b,c,d) $(a+(b<<2)+(c<<4)+(d<<6))
|
||||
#define SHUF(a,b,c,d) $((a)+((b)<<2)+((c)<<4)+((d)<<6))
|
||||
|
||||
.balign 8 # allow decoders to fetch at least 2 first insns
|
||||
sha256_process_block64_shaNI:
|
||||
|
||||
movu128 76+0*16(%eax), XMMTMP /* ABCD (little-endian dword order) */
|
||||
movu128 76+0*16(%eax), XMMTMP /* ABCD (shown least-significant-dword-first) */
|
||||
movu128 76+1*16(%eax), STATE1 /* EFGH */
|
||||
/* shufps takes dwords 0,1 from *2nd* operand, and dwords 2,3 from 1st one */
|
||||
/* shufps: dwords 0,1 of the result are selected from *2nd* operand, and dwords 2,3 from 1st operand */
|
||||
mova128 STATE1, STATE0
|
||||
/* --- -------------- ABCD -- EFGH */
|
||||
shufps SHUF(1,0,1,0), XMMTMP, STATE0 /* FEBA */
|
||||
@@ -58,190 +58,208 @@ sha256_process_block64_shaNI:
|
||||
mova128 PSHUFFLE_BSWAP32_FLIP_MASK, XMMTMP
|
||||
movl $K256+8*16, SHA256CONSTANTS
|
||||
|
||||
// sha256rnds2 instruction uses only lower 64 bits of MSG.
|
||||
// The code below needs to move upper 64 bits to lower 64 bits
|
||||
// for the second sha256rnds2 invocation
|
||||
// (what remains in upper bits does not matter).
|
||||
// There are several ways to do it:
|
||||
// movhlps MSG, MSG // abcd -> cdcd (3 bytes of code)
|
||||
// shuf128_32 SHUF(2,3,n,n), MSG, MSG // abcd -> cdXX (4 bytes)
|
||||
// punpckhqdq MSG, MSG // abcd -> cdcd (4 bytes)
|
||||
// unpckhpd MSG, MSG // abcd -> cdcd (4 bytes)
|
||||
// psrldq $8, MSG // abcd -> cd00 (5 bytes)
|
||||
// palignr $8, MSG, MSG // abcd -> cdab (6 bytes, SSSE3 insn)
|
||||
#define MOVE_UPPER64_DOWN(reg) movhlps reg, reg
|
||||
//#define MOVE_UPPER64_DOWN(reg) shuf128_32 SHUF(2,3,0,0), reg, reg
|
||||
//#define MOVE_UPPER64_DOWN(reg) punpckhqdq reg, reg
|
||||
//#define MOVE_UPPER64_DOWN(reg) unpckhpd reg, reg
|
||||
//#define MOVE_UPPER64_DOWN(reg) psrldq $8, reg
|
||||
//#define MOVE_UPPER64_DOWN(reg) palignr $8, reg, reg
|
||||
|
||||
/* Rounds 0-3 */
|
||||
movu128 0*16(DATA_PTR), MSG
|
||||
pshufb XMMTMP, MSG
|
||||
mova128 MSG, MSGTMP0
|
||||
mova128 MSG, MSG0
|
||||
paddd 0*16-8*16(SHA256CONSTANTS), MSG
|
||||
sha256rnds2 MSG, STATE0, STATE1
|
||||
shuf128_32 $0x0E, MSG, MSG
|
||||
MOVE_UPPER64_DOWN(MSG)
|
||||
sha256rnds2 MSG, STATE1, STATE0
|
||||
|
||||
/* Rounds 4-7 */
|
||||
movu128 1*16(DATA_PTR), MSG
|
||||
pshufb XMMTMP, MSG
|
||||
mova128 MSG, MSGTMP1
|
||||
mova128 MSG, MSG1
|
||||
paddd 1*16-8*16(SHA256CONSTANTS), MSG
|
||||
sha256rnds2 MSG, STATE0, STATE1
|
||||
shuf128_32 $0x0E, MSG, MSG
|
||||
MOVE_UPPER64_DOWN(MSG)
|
||||
sha256rnds2 MSG, STATE1, STATE0
|
||||
sha256msg1 MSGTMP1, MSGTMP0
|
||||
sha256msg1 MSG1, MSG0
|
||||
|
||||
/* Rounds 8-11 */
|
||||
movu128 2*16(DATA_PTR), MSG
|
||||
pshufb XMMTMP, MSG
|
||||
mova128 MSG, MSGTMP2
|
||||
mova128 MSG, MSG2
|
||||
paddd 2*16-8*16(SHA256CONSTANTS), MSG
|
||||
sha256rnds2 MSG, STATE0, STATE1
|
||||
shuf128_32 $0x0E, MSG, MSG
|
||||
MOVE_UPPER64_DOWN(MSG)
|
||||
sha256rnds2 MSG, STATE1, STATE0
|
||||
sha256msg1 MSGTMP2, MSGTMP1
|
||||
sha256msg1 MSG2, MSG1
|
||||
|
||||
/* Rounds 12-15 */
|
||||
movu128 3*16(DATA_PTR), MSG
|
||||
pshufb XMMTMP, MSG
|
||||
/* ...to here */
|
||||
mova128 MSG, MSGTMP3
|
||||
mova128 MSG, MSG3
|
||||
paddd 3*16-8*16(SHA256CONSTANTS), MSG
|
||||
sha256rnds2 MSG, STATE0, STATE1
|
||||
mova128 MSGTMP3, XMMTMP
|
||||
palignr $4, MSGTMP2, XMMTMP
|
||||
paddd XMMTMP, MSGTMP0
|
||||
sha256msg2 MSGTMP3, MSGTMP0
|
||||
shuf128_32 $0x0E, MSG, MSG
|
||||
mova128 MSG3, XMMTMP
|
||||
palignr $4, MSG2, XMMTMP
|
||||
paddd XMMTMP, MSG0
|
||||
sha256msg2 MSG3, MSG0
|
||||
MOVE_UPPER64_DOWN(MSG)
|
||||
sha256rnds2 MSG, STATE1, STATE0
|
||||
sha256msg1 MSGTMP3, MSGTMP2
|
||||
sha256msg1 MSG3, MSG2
|
||||
|
||||
/* Rounds 16-19 */
|
||||
mova128 MSGTMP0, MSG
|
||||
mova128 MSG0, MSG
|
||||
paddd 4*16-8*16(SHA256CONSTANTS), MSG
|
||||
sha256rnds2 MSG, STATE0, STATE1
|
||||
mova128 MSGTMP0, XMMTMP
|
||||
palignr $4, MSGTMP3, XMMTMP
|
||||
paddd XMMTMP, MSGTMP1
|
||||
sha256msg2 MSGTMP0, MSGTMP1
|
||||
shuf128_32 $0x0E, MSG, MSG
|
||||
mova128 MSG0, XMMTMP
|
||||
palignr $4, MSG3, XMMTMP
|
||||
paddd XMMTMP, MSG1
|
||||
sha256msg2 MSG0, MSG1
|
||||
MOVE_UPPER64_DOWN(MSG)
|
||||
sha256rnds2 MSG, STATE1, STATE0
|
||||
sha256msg1 MSGTMP0, MSGTMP3
|
||||
sha256msg1 MSG0, MSG3
|
||||
|
||||
/* Rounds 20-23 */
|
||||
mova128 MSGTMP1, MSG
|
||||
mova128 MSG1, MSG
|
||||
paddd 5*16-8*16(SHA256CONSTANTS), MSG
|
||||
sha256rnds2 MSG, STATE0, STATE1
|
||||
mova128 MSGTMP1, XMMTMP
|
||||
palignr $4, MSGTMP0, XMMTMP
|
||||
paddd XMMTMP, MSGTMP2
|
||||
sha256msg2 MSGTMP1, MSGTMP2
|
||||
shuf128_32 $0x0E, MSG, MSG
|
||||
mova128 MSG1, XMMTMP
|
||||
palignr $4, MSG0, XMMTMP
|
||||
paddd XMMTMP, MSG2
|
||||
sha256msg2 MSG1, MSG2
|
||||
MOVE_UPPER64_DOWN(MSG)
|
||||
sha256rnds2 MSG, STATE1, STATE0
|
||||
sha256msg1 MSGTMP1, MSGTMP0
|
||||
sha256msg1 MSG1, MSG0
|
||||
|
||||
/* Rounds 24-27 */
|
||||
mova128 MSGTMP2, MSG
|
||||
mova128 MSG2, MSG
|
||||
paddd 6*16-8*16(SHA256CONSTANTS), MSG
|
||||
sha256rnds2 MSG, STATE0, STATE1
|
||||
mova128 MSGTMP2, XMMTMP
|
||||
palignr $4, MSGTMP1, XMMTMP
|
||||
paddd XMMTMP, MSGTMP3
|
||||
sha256msg2 MSGTMP2, MSGTMP3
|
||||
shuf128_32 $0x0E, MSG, MSG
|
||||
mova128 MSG2, XMMTMP
|
||||
palignr $4, MSG1, XMMTMP
|
||||
paddd XMMTMP, MSG3
|
||||
sha256msg2 MSG2, MSG3
|
||||
MOVE_UPPER64_DOWN(MSG)
|
||||
sha256rnds2 MSG, STATE1, STATE0
|
||||
sha256msg1 MSGTMP2, MSGTMP1
|
||||
sha256msg1 MSG2, MSG1
|
||||
|
||||
/* Rounds 28-31 */
|
||||
mova128 MSGTMP3, MSG
|
||||
mova128 MSG3, MSG
|
||||
paddd 7*16-8*16(SHA256CONSTANTS), MSG
|
||||
sha256rnds2 MSG, STATE0, STATE1
|
||||
mova128 MSGTMP3, XMMTMP
|
||||
palignr $4, MSGTMP2, XMMTMP
|
||||
paddd XMMTMP, MSGTMP0
|
||||
sha256msg2 MSGTMP3, MSGTMP0
|
||||
shuf128_32 $0x0E, MSG, MSG
|
||||
mova128 MSG3, XMMTMP
|
||||
palignr $4, MSG2, XMMTMP
|
||||
paddd XMMTMP, MSG0
|
||||
sha256msg2 MSG3, MSG0
|
||||
MOVE_UPPER64_DOWN(MSG)
|
||||
sha256rnds2 MSG, STATE1, STATE0
|
||||
sha256msg1 MSGTMP3, MSGTMP2
|
||||
sha256msg1 MSG3, MSG2
|
||||
|
||||
/* Rounds 32-35 */
|
||||
mova128 MSGTMP0, MSG
|
||||
mova128 MSG0, MSG
|
||||
paddd 8*16-8*16(SHA256CONSTANTS), MSG
|
||||
sha256rnds2 MSG, STATE0, STATE1
|
||||
mova128 MSGTMP0, XMMTMP
|
||||
palignr $4, MSGTMP3, XMMTMP
|
||||
paddd XMMTMP, MSGTMP1
|
||||
sha256msg2 MSGTMP0, MSGTMP1
|
||||
shuf128_32 $0x0E, MSG, MSG
|
||||
mova128 MSG0, XMMTMP
|
||||
palignr $4, MSG3, XMMTMP
|
||||
paddd XMMTMP, MSG1
|
||||
sha256msg2 MSG0, MSG1
|
||||
MOVE_UPPER64_DOWN(MSG)
|
||||
sha256rnds2 MSG, STATE1, STATE0
|
||||
sha256msg1 MSGTMP0, MSGTMP3
|
||||
sha256msg1 MSG0, MSG3
|
||||
|
||||
/* Rounds 36-39 */
|
||||
mova128 MSGTMP1, MSG
|
||||
mova128 MSG1, MSG
|
||||
paddd 9*16-8*16(SHA256CONSTANTS), MSG
|
||||
sha256rnds2 MSG, STATE0, STATE1
|
||||
mova128 MSGTMP1, XMMTMP
|
||||
palignr $4, MSGTMP0, XMMTMP
|
||||
paddd XMMTMP, MSGTMP2
|
||||
sha256msg2 MSGTMP1, MSGTMP2
|
||||
shuf128_32 $0x0E, MSG, MSG
|
||||
mova128 MSG1, XMMTMP
|
||||
palignr $4, MSG0, XMMTMP
|
||||
paddd XMMTMP, MSG2
|
||||
sha256msg2 MSG1, MSG2
|
||||
MOVE_UPPER64_DOWN(MSG)
|
||||
sha256rnds2 MSG, STATE1, STATE0
|
||||
sha256msg1 MSGTMP1, MSGTMP0
|
||||
sha256msg1 MSG1, MSG0
|
||||
|
||||
/* Rounds 40-43 */
|
||||
mova128 MSGTMP2, MSG
|
||||
mova128 MSG2, MSG
|
||||
paddd 10*16-8*16(SHA256CONSTANTS), MSG
|
||||
sha256rnds2 MSG, STATE0, STATE1
|
||||
mova128 MSGTMP2, XMMTMP
|
||||
palignr $4, MSGTMP1, XMMTMP
|
||||
paddd XMMTMP, MSGTMP3
|
||||
sha256msg2 MSGTMP2, MSGTMP3
|
||||
shuf128_32 $0x0E, MSG, MSG
|
||||
mova128 MSG2, XMMTMP
|
||||
palignr $4, MSG1, XMMTMP
|
||||
paddd XMMTMP, MSG3
|
||||
sha256msg2 MSG2, MSG3
|
||||
MOVE_UPPER64_DOWN(MSG)
|
||||
sha256rnds2 MSG, STATE1, STATE0
|
||||
sha256msg1 MSGTMP2, MSGTMP1
|
||||
sha256msg1 MSG2, MSG1
|
||||
|
||||
/* Rounds 44-47 */
|
||||
mova128 MSGTMP3, MSG
|
||||
mova128 MSG3, MSG
|
||||
paddd 11*16-8*16(SHA256CONSTANTS), MSG
|
||||
sha256rnds2 MSG, STATE0, STATE1
|
||||
mova128 MSGTMP3, XMMTMP
|
||||
palignr $4, MSGTMP2, XMMTMP
|
||||
paddd XMMTMP, MSGTMP0
|
||||
sha256msg2 MSGTMP3, MSGTMP0
|
||||
shuf128_32 $0x0E, MSG, MSG
|
||||
mova128 MSG3, XMMTMP
|
||||
palignr $4, MSG2, XMMTMP
|
||||
paddd XMMTMP, MSG0
|
||||
sha256msg2 MSG3, MSG0
|
||||
MOVE_UPPER64_DOWN(MSG)
|
||||
sha256rnds2 MSG, STATE1, STATE0
|
||||
sha256msg1 MSGTMP3, MSGTMP2
|
||||
sha256msg1 MSG3, MSG2
|
||||
|
||||
/* Rounds 48-51 */
|
||||
mova128 MSGTMP0, MSG
|
||||
mova128 MSG0, MSG
|
||||
paddd 12*16-8*16(SHA256CONSTANTS), MSG
|
||||
sha256rnds2 MSG, STATE0, STATE1
|
||||
mova128 MSGTMP0, XMMTMP
|
||||
palignr $4, MSGTMP3, XMMTMP
|
||||
paddd XMMTMP, MSGTMP1
|
||||
sha256msg2 MSGTMP0, MSGTMP1
|
||||
shuf128_32 $0x0E, MSG, MSG
|
||||
mova128 MSG0, XMMTMP
|
||||
palignr $4, MSG3, XMMTMP
|
||||
paddd XMMTMP, MSG1
|
||||
sha256msg2 MSG0, MSG1
|
||||
MOVE_UPPER64_DOWN(MSG)
|
||||
sha256rnds2 MSG, STATE1, STATE0
|
||||
sha256msg1 MSGTMP0, MSGTMP3
|
||||
sha256msg1 MSG0, MSG3
|
||||
|
||||
/* Rounds 52-55 */
|
||||
mova128 MSGTMP1, MSG
|
||||
mova128 MSG1, MSG
|
||||
paddd 13*16-8*16(SHA256CONSTANTS), MSG
|
||||
sha256rnds2 MSG, STATE0, STATE1
|
||||
mova128 MSGTMP1, XMMTMP
|
||||
palignr $4, MSGTMP0, XMMTMP
|
||||
paddd XMMTMP, MSGTMP2
|
||||
sha256msg2 MSGTMP1, MSGTMP2
|
||||
shuf128_32 $0x0E, MSG, MSG
|
||||
mova128 MSG1, XMMTMP
|
||||
palignr $4, MSG0, XMMTMP
|
||||
paddd XMMTMP, MSG2
|
||||
sha256msg2 MSG1, MSG2
|
||||
MOVE_UPPER64_DOWN(MSG)
|
||||
sha256rnds2 MSG, STATE1, STATE0
|
||||
|
||||
/* Rounds 56-59 */
|
||||
mova128 MSGTMP2, MSG
|
||||
mova128 MSG2, MSG
|
||||
paddd 14*16-8*16(SHA256CONSTANTS), MSG
|
||||
sha256rnds2 MSG, STATE0, STATE1
|
||||
mova128 MSGTMP2, XMMTMP
|
||||
palignr $4, MSGTMP1, XMMTMP
|
||||
paddd XMMTMP, MSGTMP3
|
||||
sha256msg2 MSGTMP2, MSGTMP3
|
||||
shuf128_32 $0x0E, MSG, MSG
|
||||
mova128 MSG2, XMMTMP
|
||||
palignr $4, MSG1, XMMTMP
|
||||
paddd XMMTMP, MSG3
|
||||
sha256msg2 MSG2, MSG3
|
||||
MOVE_UPPER64_DOWN(MSG)
|
||||
sha256rnds2 MSG, STATE1, STATE0
|
||||
|
||||
/* Rounds 60-63 */
|
||||
mova128 MSGTMP3, MSG
|
||||
mova128 MSG3, MSG
|
||||
paddd 15*16-8*16(SHA256CONSTANTS), MSG
|
||||
sha256rnds2 MSG, STATE0, STATE1
|
||||
shuf128_32 $0x0E, MSG, MSG
|
||||
MOVE_UPPER64_DOWN(MSG)
|
||||
sha256rnds2 MSG, STATE1, STATE0
|
||||
|
||||
/* Write hash values back in the correct order */
|
||||
mova128 STATE0, XMMTMP
|
||||
/* shufps takes dwords 0,1 from *2nd* operand, and dwords 2,3 from 1st one */
|
||||
/* shufps: dwords 0,1 of the result are selected from *2nd* operand, and dwords 2,3 from 1st operand */
|
||||
/* --- -------------- HGDC -- FEBA */
|
||||
shufps SHUF(3,2,3,2), STATE1, STATE0 /* ABCD */
|
||||
shufps SHUF(1,0,1,0), STATE1, XMMTMP /* EFGH */
|
||||
|
||||
+118
-100
@@ -34,24 +34,24 @@
|
||||
#define MSG %xmm0
|
||||
#define STATE0 %xmm1
|
||||
#define STATE1 %xmm2
|
||||
#define MSGTMP0 %xmm3
|
||||
#define MSGTMP1 %xmm4
|
||||
#define MSGTMP2 %xmm5
|
||||
#define MSGTMP3 %xmm6
|
||||
#define MSG0 %xmm3
|
||||
#define MSG1 %xmm4
|
||||
#define MSG2 %xmm5
|
||||
#define MSG3 %xmm6
|
||||
|
||||
#define XMMTMP %xmm7
|
||||
|
||||
#define SAVE0 %xmm8
|
||||
#define SAVE1 %xmm9
|
||||
|
||||
#define SHUF(a,b,c,d) $(a+(b<<2)+(c<<4)+(d<<6))
|
||||
#define SHUF(a,b,c,d) $((a)+((b)<<2)+((c)<<4)+((d)<<6))
|
||||
|
||||
.balign 8 # allow decoders to fetch at least 2 first insns
|
||||
sha256_process_block64_shaNI:
|
||||
|
||||
movu128 80+0*16(%rdi), XMMTMP /* ABCD (little-endian dword order) */
|
||||
movu128 80+0*16(%rdi), XMMTMP /* ABCD (shown least-significant-dword-first) */
|
||||
movu128 80+1*16(%rdi), STATE1 /* EFGH */
|
||||
/* shufps takes dwords 0,1 from *2nd* operand, and dwords 2,3 from 1st one */
|
||||
/* shufps: dwords 0,1 of the result are selected from *2nd* operand, and dwords 2,3 from 1st operand */
|
||||
mova128 STATE1, STATE0
|
||||
/* --- -------------- ABCD -- EFGH */
|
||||
shufps SHUF(1,0,1,0), XMMTMP, STATE0 /* FEBA */
|
||||
@@ -65,185 +65,203 @@ sha256_process_block64_shaNI:
|
||||
mova128 STATE0, SAVE0
|
||||
mova128 STATE1, SAVE1
|
||||
|
||||
// sha256rnds2 instruction uses only lower 64 bits of MSG.
|
||||
// The code below needs to move upper 64 bits to lower 64 bits
|
||||
// for the second sha256rnds2 invocation
|
||||
// (what remains in upper bits does not matter).
|
||||
// There are several ways to do it:
|
||||
// movhlps MSG, MSG // abcd -> cdcd (3 bytes of code)
|
||||
// shuf128_32 SHUF(2,3,n,n), MSG, MSG // abcd -> cdXX (4 bytes)
|
||||
// punpckhqdq MSG, MSG // abcd -> cdcd (4 bytes)
|
||||
// unpckhpd MSG, MSG // abcd -> cdcd (4 bytes)
|
||||
// psrldq $8, MSG // abcd -> cd00 (5 bytes)
|
||||
// palignr $8, MSG, MSG // abcd -> cdab (6 bytes, SSSE3 insn)
|
||||
#define MOVE_UPPER64_DOWN(reg) movhlps reg, reg
|
||||
//#define MOVE_UPPER64_DOWN(reg) shuf128_32 SHUF(2,3,0,0), reg, reg
|
||||
//#define MOVE_UPPER64_DOWN(reg) punpckhqdq reg, reg
|
||||
//#define MOVE_UPPER64_DOWN(reg) unpckhpd reg, reg
|
||||
//#define MOVE_UPPER64_DOWN(reg) psrldq $8, reg
|
||||
//#define MOVE_UPPER64_DOWN(reg) palignr $8, reg, reg
|
||||
|
||||
/* Rounds 0-3 */
|
||||
movu128 0*16(DATA_PTR), MSG
|
||||
pshufb XMMTMP, MSG
|
||||
mova128 MSG, MSGTMP0
|
||||
mova128 MSG, MSG0
|
||||
paddd 0*16-8*16(SHA256CONSTANTS), MSG
|
||||
sha256rnds2 MSG, STATE0, STATE1
|
||||
shuf128_32 $0x0E, MSG, MSG
|
||||
MOVE_UPPER64_DOWN(MSG)
|
||||
sha256rnds2 MSG, STATE1, STATE0
|
||||
|
||||
/* Rounds 4-7 */
|
||||
movu128 1*16(DATA_PTR), MSG
|
||||
pshufb XMMTMP, MSG
|
||||
mova128 MSG, MSGTMP1
|
||||
mova128 MSG, MSG1
|
||||
paddd 1*16-8*16(SHA256CONSTANTS), MSG
|
||||
sha256rnds2 MSG, STATE0, STATE1
|
||||
shuf128_32 $0x0E, MSG, MSG
|
||||
MOVE_UPPER64_DOWN(MSG)
|
||||
sha256rnds2 MSG, STATE1, STATE0
|
||||
sha256msg1 MSGTMP1, MSGTMP0
|
||||
sha256msg1 MSG1, MSG0
|
||||
|
||||
/* Rounds 8-11 */
|
||||
movu128 2*16(DATA_PTR), MSG
|
||||
pshufb XMMTMP, MSG
|
||||
mova128 MSG, MSGTMP2
|
||||
mova128 MSG, MSG2
|
||||
paddd 2*16-8*16(SHA256CONSTANTS), MSG
|
||||
sha256rnds2 MSG, STATE0, STATE1
|
||||
shuf128_32 $0x0E, MSG, MSG
|
||||
MOVE_UPPER64_DOWN(MSG)
|
||||
sha256rnds2 MSG, STATE1, STATE0
|
||||
sha256msg1 MSGTMP2, MSGTMP1
|
||||
sha256msg1 MSG2, MSG1
|
||||
|
||||
/* Rounds 12-15 */
|
||||
movu128 3*16(DATA_PTR), MSG
|
||||
pshufb XMMTMP, MSG
|
||||
/* ...to here */
|
||||
mova128 MSG, MSGTMP3
|
||||
mova128 MSG, MSG3
|
||||
paddd 3*16-8*16(SHA256CONSTANTS), MSG
|
||||
sha256rnds2 MSG, STATE0, STATE1
|
||||
mova128 MSGTMP3, XMMTMP
|
||||
palignr $4, MSGTMP2, XMMTMP
|
||||
paddd XMMTMP, MSGTMP0
|
||||
sha256msg2 MSGTMP3, MSGTMP0
|
||||
shuf128_32 $0x0E, MSG, MSG
|
||||
mova128 MSG3, XMMTMP
|
||||
palignr $4, MSG2, XMMTMP
|
||||
paddd XMMTMP, MSG0
|
||||
sha256msg2 MSG3, MSG0
|
||||
MOVE_UPPER64_DOWN(MSG)
|
||||
sha256rnds2 MSG, STATE1, STATE0
|
||||
sha256msg1 MSGTMP3, MSGTMP2
|
||||
sha256msg1 MSG3, MSG2
|
||||
|
||||
/* Rounds 16-19 */
|
||||
mova128 MSGTMP0, MSG
|
||||
mova128 MSG0, MSG
|
||||
paddd 4*16-8*16(SHA256CONSTANTS), MSG
|
||||
sha256rnds2 MSG, STATE0, STATE1
|
||||
mova128 MSGTMP0, XMMTMP
|
||||
palignr $4, MSGTMP3, XMMTMP
|
||||
paddd XMMTMP, MSGTMP1
|
||||
sha256msg2 MSGTMP0, MSGTMP1
|
||||
shuf128_32 $0x0E, MSG, MSG
|
||||
mova128 MSG0, XMMTMP
|
||||
palignr $4, MSG3, XMMTMP
|
||||
paddd XMMTMP, MSG1
|
||||
sha256msg2 MSG0, MSG1
|
||||
MOVE_UPPER64_DOWN(MSG)
|
||||
sha256rnds2 MSG, STATE1, STATE0
|
||||
sha256msg1 MSGTMP0, MSGTMP3
|
||||
sha256msg1 MSG0, MSG3
|
||||
|
||||
/* Rounds 20-23 */
|
||||
mova128 MSGTMP1, MSG
|
||||
mova128 MSG1, MSG
|
||||
paddd 5*16-8*16(SHA256CONSTANTS), MSG
|
||||
sha256rnds2 MSG, STATE0, STATE1
|
||||
mova128 MSGTMP1, XMMTMP
|
||||
palignr $4, MSGTMP0, XMMTMP
|
||||
paddd XMMTMP, MSGTMP2
|
||||
sha256msg2 MSGTMP1, MSGTMP2
|
||||
shuf128_32 $0x0E, MSG, MSG
|
||||
mova128 MSG1, XMMTMP
|
||||
palignr $4, MSG0, XMMTMP
|
||||
paddd XMMTMP, MSG2
|
||||
sha256msg2 MSG1, MSG2
|
||||
MOVE_UPPER64_DOWN(MSG)
|
||||
sha256rnds2 MSG, STATE1, STATE0
|
||||
sha256msg1 MSGTMP1, MSGTMP0
|
||||
sha256msg1 MSG1, MSG0
|
||||
|
||||
/* Rounds 24-27 */
|
||||
mova128 MSGTMP2, MSG
|
||||
mova128 MSG2, MSG
|
||||
paddd 6*16-8*16(SHA256CONSTANTS), MSG
|
||||
sha256rnds2 MSG, STATE0, STATE1
|
||||
mova128 MSGTMP2, XMMTMP
|
||||
palignr $4, MSGTMP1, XMMTMP
|
||||
paddd XMMTMP, MSGTMP3
|
||||
sha256msg2 MSGTMP2, MSGTMP3
|
||||
shuf128_32 $0x0E, MSG, MSG
|
||||
mova128 MSG2, XMMTMP
|
||||
palignr $4, MSG1, XMMTMP
|
||||
paddd XMMTMP, MSG3
|
||||
sha256msg2 MSG2, MSG3
|
||||
MOVE_UPPER64_DOWN(MSG)
|
||||
sha256rnds2 MSG, STATE1, STATE0
|
||||
sha256msg1 MSGTMP2, MSGTMP1
|
||||
sha256msg1 MSG2, MSG1
|
||||
|
||||
/* Rounds 28-31 */
|
||||
mova128 MSGTMP3, MSG
|
||||
mova128 MSG3, MSG
|
||||
paddd 7*16-8*16(SHA256CONSTANTS), MSG
|
||||
sha256rnds2 MSG, STATE0, STATE1
|
||||
mova128 MSGTMP3, XMMTMP
|
||||
palignr $4, MSGTMP2, XMMTMP
|
||||
paddd XMMTMP, MSGTMP0
|
||||
sha256msg2 MSGTMP3, MSGTMP0
|
||||
shuf128_32 $0x0E, MSG, MSG
|
||||
mova128 MSG3, XMMTMP
|
||||
palignr $4, MSG2, XMMTMP
|
||||
paddd XMMTMP, MSG0
|
||||
sha256msg2 MSG3, MSG0
|
||||
MOVE_UPPER64_DOWN(MSG)
|
||||
sha256rnds2 MSG, STATE1, STATE0
|
||||
sha256msg1 MSGTMP3, MSGTMP2
|
||||
sha256msg1 MSG3, MSG2
|
||||
|
||||
/* Rounds 32-35 */
|
||||
mova128 MSGTMP0, MSG
|
||||
mova128 MSG0, MSG
|
||||
paddd 8*16-8*16(SHA256CONSTANTS), MSG
|
||||
sha256rnds2 MSG, STATE0, STATE1
|
||||
mova128 MSGTMP0, XMMTMP
|
||||
palignr $4, MSGTMP3, XMMTMP
|
||||
paddd XMMTMP, MSGTMP1
|
||||
sha256msg2 MSGTMP0, MSGTMP1
|
||||
shuf128_32 $0x0E, MSG, MSG
|
||||
mova128 MSG0, XMMTMP
|
||||
palignr $4, MSG3, XMMTMP
|
||||
paddd XMMTMP, MSG1
|
||||
sha256msg2 MSG0, MSG1
|
||||
MOVE_UPPER64_DOWN(MSG)
|
||||
sha256rnds2 MSG, STATE1, STATE0
|
||||
sha256msg1 MSGTMP0, MSGTMP3
|
||||
sha256msg1 MSG0, MSG3
|
||||
|
||||
/* Rounds 36-39 */
|
||||
mova128 MSGTMP1, MSG
|
||||
mova128 MSG1, MSG
|
||||
paddd 9*16-8*16(SHA256CONSTANTS), MSG
|
||||
sha256rnds2 MSG, STATE0, STATE1
|
||||
mova128 MSGTMP1, XMMTMP
|
||||
palignr $4, MSGTMP0, XMMTMP
|
||||
paddd XMMTMP, MSGTMP2
|
||||
sha256msg2 MSGTMP1, MSGTMP2
|
||||
shuf128_32 $0x0E, MSG, MSG
|
||||
mova128 MSG1, XMMTMP
|
||||
palignr $4, MSG0, XMMTMP
|
||||
paddd XMMTMP, MSG2
|
||||
sha256msg2 MSG1, MSG2
|
||||
MOVE_UPPER64_DOWN(MSG)
|
||||
sha256rnds2 MSG, STATE1, STATE0
|
||||
sha256msg1 MSGTMP1, MSGTMP0
|
||||
sha256msg1 MSG1, MSG0
|
||||
|
||||
/* Rounds 40-43 */
|
||||
mova128 MSGTMP2, MSG
|
||||
mova128 MSG2, MSG
|
||||
paddd 10*16-8*16(SHA256CONSTANTS), MSG
|
||||
sha256rnds2 MSG, STATE0, STATE1
|
||||
mova128 MSGTMP2, XMMTMP
|
||||
palignr $4, MSGTMP1, XMMTMP
|
||||
paddd XMMTMP, MSGTMP3
|
||||
sha256msg2 MSGTMP2, MSGTMP3
|
||||
shuf128_32 $0x0E, MSG, MSG
|
||||
mova128 MSG2, XMMTMP
|
||||
palignr $4, MSG1, XMMTMP
|
||||
paddd XMMTMP, MSG3
|
||||
sha256msg2 MSG2, MSG3
|
||||
MOVE_UPPER64_DOWN(MSG)
|
||||
sha256rnds2 MSG, STATE1, STATE0
|
||||
sha256msg1 MSGTMP2, MSGTMP1
|
||||
sha256msg1 MSG2, MSG1
|
||||
|
||||
/* Rounds 44-47 */
|
||||
mova128 MSGTMP3, MSG
|
||||
mova128 MSG3, MSG
|
||||
paddd 11*16-8*16(SHA256CONSTANTS), MSG
|
||||
sha256rnds2 MSG, STATE0, STATE1
|
||||
mova128 MSGTMP3, XMMTMP
|
||||
palignr $4, MSGTMP2, XMMTMP
|
||||
paddd XMMTMP, MSGTMP0
|
||||
sha256msg2 MSGTMP3, MSGTMP0
|
||||
shuf128_32 $0x0E, MSG, MSG
|
||||
mova128 MSG3, XMMTMP
|
||||
palignr $4, MSG2, XMMTMP
|
||||
paddd XMMTMP, MSG0
|
||||
sha256msg2 MSG3, MSG0
|
||||
MOVE_UPPER64_DOWN(MSG)
|
||||
sha256rnds2 MSG, STATE1, STATE0
|
||||
sha256msg1 MSGTMP3, MSGTMP2
|
||||
sha256msg1 MSG3, MSG2
|
||||
|
||||
/* Rounds 48-51 */
|
||||
mova128 MSGTMP0, MSG
|
||||
mova128 MSG0, MSG
|
||||
paddd 12*16-8*16(SHA256CONSTANTS), MSG
|
||||
sha256rnds2 MSG, STATE0, STATE1
|
||||
mova128 MSGTMP0, XMMTMP
|
||||
palignr $4, MSGTMP3, XMMTMP
|
||||
paddd XMMTMP, MSGTMP1
|
||||
sha256msg2 MSGTMP0, MSGTMP1
|
||||
shuf128_32 $0x0E, MSG, MSG
|
||||
mova128 MSG0, XMMTMP
|
||||
palignr $4, MSG3, XMMTMP
|
||||
paddd XMMTMP, MSG1
|
||||
sha256msg2 MSG0, MSG1
|
||||
MOVE_UPPER64_DOWN(MSG)
|
||||
sha256rnds2 MSG, STATE1, STATE0
|
||||
sha256msg1 MSGTMP0, MSGTMP3
|
||||
sha256msg1 MSG0, MSG3
|
||||
|
||||
/* Rounds 52-55 */
|
||||
mova128 MSGTMP1, MSG
|
||||
mova128 MSG1, MSG
|
||||
paddd 13*16-8*16(SHA256CONSTANTS), MSG
|
||||
sha256rnds2 MSG, STATE0, STATE1
|
||||
mova128 MSGTMP1, XMMTMP
|
||||
palignr $4, MSGTMP0, XMMTMP
|
||||
paddd XMMTMP, MSGTMP2
|
||||
sha256msg2 MSGTMP1, MSGTMP2
|
||||
shuf128_32 $0x0E, MSG, MSG
|
||||
mova128 MSG1, XMMTMP
|
||||
palignr $4, MSG0, XMMTMP
|
||||
paddd XMMTMP, MSG2
|
||||
sha256msg2 MSG1, MSG2
|
||||
MOVE_UPPER64_DOWN(MSG)
|
||||
sha256rnds2 MSG, STATE1, STATE0
|
||||
|
||||
/* Rounds 56-59 */
|
||||
mova128 MSGTMP2, MSG
|
||||
mova128 MSG2, MSG
|
||||
paddd 14*16-8*16(SHA256CONSTANTS), MSG
|
||||
sha256rnds2 MSG, STATE0, STATE1
|
||||
mova128 MSGTMP2, XMMTMP
|
||||
palignr $4, MSGTMP1, XMMTMP
|
||||
paddd XMMTMP, MSGTMP3
|
||||
sha256msg2 MSGTMP2, MSGTMP3
|
||||
shuf128_32 $0x0E, MSG, MSG
|
||||
mova128 MSG2, XMMTMP
|
||||
palignr $4, MSG1, XMMTMP
|
||||
paddd XMMTMP, MSG3
|
||||
sha256msg2 MSG2, MSG3
|
||||
MOVE_UPPER64_DOWN(MSG)
|
||||
sha256rnds2 MSG, STATE1, STATE0
|
||||
|
||||
/* Rounds 60-63 */
|
||||
mova128 MSGTMP3, MSG
|
||||
mova128 MSG3, MSG
|
||||
paddd 15*16-8*16(SHA256CONSTANTS), MSG
|
||||
sha256rnds2 MSG, STATE0, STATE1
|
||||
shuf128_32 $0x0E, MSG, MSG
|
||||
MOVE_UPPER64_DOWN(MSG)
|
||||
sha256rnds2 MSG, STATE1, STATE0
|
||||
|
||||
/* Add current hash values with previously saved */
|
||||
@@ -252,7 +270,7 @@ sha256_process_block64_shaNI:
|
||||
|
||||
/* Write hash values back in the correct order */
|
||||
mova128 STATE0, XMMTMP
|
||||
/* shufps takes dwords 0,1 from *2nd* operand, and dwords 2,3 from 1st one */
|
||||
/* shufps: dwords 0,1 of the result are selected from *2nd* operand, and dwords 2,3 from 1st operand */
|
||||
/* --- -------------- HGDC -- FEBA */
|
||||
shufps SHUF(3,2,3,2), STATE1, STATE0 /* ABCD */
|
||||
shufps SHUF(1,0,1,0), STATE1, XMMTMP /* EFGH */
|
||||
|
||||
+265
@@ -0,0 +1,265 @@
|
||||
/* vi: set sw=4 ts=4: */
|
||||
/*
|
||||
* Copyright (C) 2026 Denys Vlasenko <vda.linux@googlemail.com>
|
||||
*
|
||||
* Licensed under GPLv2, see file LICENSE in this source tree.
|
||||
*/
|
||||
//kbuild:lib-$(CONFIG_TELNETD) += ioloop.o
|
||||
//kbuild:lib-$(CONFIG_TELNET) += ioloop.o
|
||||
|
||||
#include "libbb.h"
|
||||
|
||||
#define DEBUG 0
|
||||
|
||||
#if DEBUG
|
||||
# define dbg(...) bb_error_msg(__VA_ARGS__)
|
||||
static char *bin_to_hex(const void *hash_value, unsigned hash_length)
|
||||
{
|
||||
/* xzalloc zero-terminates */
|
||||
char *hex_value = xzalloc((hash_length * 2) + 1);
|
||||
bin2hex(hex_value, (char*)hash_value, hash_length);
|
||||
return auto_string(hex_value);
|
||||
}
|
||||
#else
|
||||
# define dbg(...) ((void)0)
|
||||
#endif
|
||||
|
||||
void FAST_FUNC ioloop_insert_conn(ioloop_state_t *io, connection_t *conn)
|
||||
{
|
||||
conn->io = io;
|
||||
conn->next = io->conns;
|
||||
io->conns = conn;
|
||||
}
|
||||
|
||||
void FAST_FUNC ioloop_remove_conn(ioloop_state_t *io, connection_t *conn)
|
||||
{
|
||||
connection_t **pp = &io->conns;
|
||||
while (*pp) {
|
||||
if (*pp == conn) {
|
||||
*pp = conn->next;
|
||||
return;
|
||||
}
|
||||
pp = &(*pp)->next;
|
||||
}
|
||||
}
|
||||
|
||||
void FAST_FUNC conn_close_fds(connection_t *conn)
|
||||
{
|
||||
if (conn->read_fd >= 0)
|
||||
close(conn->read_fd);
|
||||
if (conn->write_fd >= 0 && conn->write_fd != conn->read_fd)
|
||||
close(conn->write_fd);
|
||||
conn->write_fd = -1;
|
||||
conn->read_fd = -1;
|
||||
}
|
||||
|
||||
void FAST_FUNC conn_close_fds_remove_and_free(connection_t *conn)
|
||||
{
|
||||
conn_close_fds(conn);
|
||||
ioloop_remove_conn(conn->io, conn);
|
||||
free_connection(conn);
|
||||
}
|
||||
|
||||
#ifdef UNUSED
|
||||
void FAST_FUNC ioloop_close_fd_in_all_conns(ioloop_state_t *io, int fd)
|
||||
{
|
||||
connection_t *conn;
|
||||
|
||||
close(fd);
|
||||
conn = io->conns;
|
||||
while (conn) {
|
||||
if (conn->read_fd == fd)
|
||||
conn->read_fd = -1;
|
||||
if (conn->write_fd == fd)
|
||||
conn->write_fd = -1;
|
||||
conn = conn->next;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
// should_poll_write_fd() - Should ioloop poll write_fd for writability?
|
||||
// May return error if knows that write side is closed, even if it has free buffer space.
|
||||
// > 0: Yes, poll write_fd (has data to write, or can generate such data)
|
||||
// In this case, write_fd must be valid! (it's a bug if it is < 0, can crash)
|
||||
// 0: No, don't poll write_fd currently
|
||||
// < 0: error, I probably freed myself (do not use my structure in this iteration,
|
||||
// on next iteration, if I indeed freed myself, you won't find me in the list).
|
||||
//
|
||||
// should_poll_read_fd() - Should ioloop poll read_fd for readability?
|
||||
// May return error if knows that write side is closed, even if it has free buffer space.
|
||||
// > 0: Yes, poll read_fd (has buffer space, healthy)
|
||||
// In this case, read_fd must be valid! (it's a bug if it is < 0)
|
||||
// 0: No, don't poll read_fd. One of:
|
||||
// Buffer is full (hopefully write() will free some)
|
||||
// Got error/EOF, want to drain write buffer first
|
||||
// < 0: Error/EOF, I probably freed myself (do not use my structure)
|
||||
//
|
||||
// Essentially: add your "this connection is dead, close+ioloop_remove_conn(this)+free(this)" code
|
||||
// to either of the above two functions.
|
||||
// Either would work equally well from ioloop POV.
|
||||
//
|
||||
// write() - Perform write
|
||||
// Even if poll shows that write_fd become ready, the method will not be called if conn->write_fd become < 0.
|
||||
// However, changing it to another fd >= 0 is a bug.
|
||||
// >= 0: No error (ioloop() does not handle 0 specially)
|
||||
// < 0: Error occurred, I probably freed myself (do not use my structure)
|
||||
//
|
||||
// Error returns from read() and write() will skip the opposite op.
|
||||
// (Which one happens first depends on ioloop_run() code, as I write this, write()
|
||||
// is done first - therefore read() can't skip write()).
|
||||
// IOW: if you "remove(this)" in either of them you MUST return < 0.
|
||||
//
|
||||
// Write function, if it performs some processing, needs to account for the possibility
|
||||
// that any "incomplete data, can't write, wait for more" logic should also check whether
|
||||
// "more" is even possible: your other code may know that the read side is at EOF,
|
||||
// and arrange for it to not be polled. Thus a hang: read not reading, write waits for more date.
|
||||
//
|
||||
// read() - Perform read
|
||||
// Will not be called if read_fd become < 0.
|
||||
// >= 0: No error (0 is EOF, ioloop() does not handle 0 specially)
|
||||
// < 0: Error occurred, I probably freed myself (do not use my structure)
|
||||
//
|
||||
// Putting "this connection is dead, close+remove+free" code into read function
|
||||
// is often inconvenient: if you got EOF/error on read, you still want to poll write side
|
||||
// and try to write out the buffered data to it. Which means read() can't "remove+free".
|
||||
// Instead, you can remember EOF/error and make future should_poll_read_fd() respond 0.
|
||||
// One way is to close (if possible) read_fd, set it to -1 and use as a flag.
|
||||
//
|
||||
// If need to support one-sided close, such as when HTTP/1.x client sends us
|
||||
// "GET / HTTP/1.1\r\n\r\n" and closes its writing side with shutdown(SHUT_WR),
|
||||
// the idiom is that when read() sees EOF, it sets conn->read_fd to -1
|
||||
// and subsequently should_poll_read_fd() always return 0 (no more attempts to read);
|
||||
// write() flushes all remaining data to conn->write_fd and then signals EOF
|
||||
// to write_fd: shutdown(SHUT_WR) for sockets, close() for pipes
|
||||
// (how to do this for ptys!?).
|
||||
// After this, should_poll_write_fd() can return 0 if fd has to stay open (socket)
|
||||
// or can return -1 and free itself if fd is closed.
|
||||
|
||||
static ALWAYS_INLINE int should_poll_write_fd(connection_t *conn)
|
||||
{
|
||||
return conn->should_poll_write_fd(conn);
|
||||
}
|
||||
static ALWAYS_INLINE int should_poll_read_fd(connection_t *conn)
|
||||
{
|
||||
return conn->should_poll_read_fd(conn);
|
||||
}
|
||||
static ALWAYS_INLINE int write_from_buf(connection_t *conn)
|
||||
{
|
||||
return conn->write(conn);
|
||||
}
|
||||
static ALWAYS_INLINE int read_to_buf(connection_t *conn)
|
||||
{
|
||||
return conn->read(conn);
|
||||
}
|
||||
|
||||
int FAST_FUNC ioloop_run(ioloop_state_t *io)
|
||||
{
|
||||
connection_t *conn;
|
||||
unsigned poll_timeout_us;
|
||||
int maxfd;
|
||||
int count;
|
||||
struct timeval tv;
|
||||
struct timeval *tv_ptr;
|
||||
fd_set rdfdset, wrfdset;
|
||||
|
||||
io->current_iteration_timeout = io->max_timeout ? io->max_timeout : UINT_MAX;
|
||||
//bb_error_msg("io->current_iteration_timeout:%d", io->current_iteration_timeout);
|
||||
again:
|
||||
FD_ZERO(&rdfdset);
|
||||
FD_ZERO(&wrfdset);
|
||||
|
||||
conn = io->conns;
|
||||
if (!conn)
|
||||
return IOLOOP_NO_CONNS;
|
||||
|
||||
maxfd = -1;
|
||||
while (conn) {
|
||||
connection_t *next;
|
||||
int rcw, rcr;
|
||||
|
||||
next = conn->next; /* in case conn is freed */
|
||||
rcw = should_poll_write_fd(conn);
|
||||
if (rcw < 0) {
|
||||
/* often indicates that conn is gone (freed), do not use it anymore */
|
||||
goto next;
|
||||
}
|
||||
/* Do not add conn->write_fd to waiting set yet,
|
||||
* the *reader* may decide to abort (return rcr < 0)!
|
||||
* Check it first:
|
||||
*/
|
||||
rcr = should_poll_read_fd(conn);
|
||||
if (rcr < 0)
|
||||
goto next;
|
||||
if (rcw > 0) {
|
||||
FD_SET(conn->write_fd, &wrfdset);
|
||||
//FIXME: what if fd > 1023
|
||||
if (conn->write_fd > maxfd)
|
||||
maxfd = conn->write_fd;
|
||||
}
|
||||
if (rcr > 0) {
|
||||
FD_SET(conn->read_fd, &rdfdset);
|
||||
if (conn->read_fd > maxfd)
|
||||
maxfd = conn->read_fd;
|
||||
}
|
||||
next:
|
||||
conn = next;
|
||||
}
|
||||
if (!io->conns)
|
||||
return IOLOOP_NO_CONNS; /* all conns removed themselves before we reached polling */
|
||||
|
||||
poll_timeout_us = io->current_iteration_timeout;
|
||||
/* May be useful to know after loop exit: */
|
||||
io->last_timeout = poll_timeout_us;
|
||||
dbg("poll_timeout_us:%d", poll_timeout_us);
|
||||
|
||||
tv_ptr = NULL; /* infinite timeout */
|
||||
if (poll_timeout_us != UINT_MAX) {
|
||||
tv.tv_sec = poll_timeout_us / 1000000;
|
||||
tv.tv_usec = poll_timeout_us % 1000000;
|
||||
tv_ptr = &tv;
|
||||
}
|
||||
maxfd++;
|
||||
count = select(maxfd, maxfd ? &rdfdset : NULL, maxfd ? &wrfdset : NULL, NULL, tv_ptr);
|
||||
dbg("select:%d", count);
|
||||
|
||||
/* Any function in the loop can change it, modifying next select() timeout */
|
||||
io->current_iteration_timeout = io->max_timeout ? io->max_timeout : UINT_MAX;
|
||||
dbg("io->current_iteration_timeout:%d", io->current_iteration_timeout);
|
||||
|
||||
if (count <= 0) {
|
||||
/* 0: timeout */
|
||||
if (count == 0 && (io->flags & IOLOOP_FLAG_EXIT_IF_TIMEOUT))
|
||||
return IOLOOP_TIMEOUT;
|
||||
/* < 0: EINTR or ENOMEM */
|
||||
if (count < 0 && (io->flags & IOLOOP_FLAG_EXIT_IF_EINTR) && errno == EINTR)
|
||||
return IOLOOP_EINTR;
|
||||
goto again;
|
||||
}
|
||||
|
||||
conn = io->conns;
|
||||
while (conn) {
|
||||
connection_t *next = conn->next; /* in case conn freed */
|
||||
int rcw;
|
||||
|
||||
/* We do check for valid fd >=0: this means that conns
|
||||
* may prevent I/O of other conns by setting fds to -1.
|
||||
*/
|
||||
dbg("conn->write_fd:%d ?", conn->write_fd);
|
||||
if (conn->write_fd >= 0 && FD_ISSET(conn->write_fd, &wrfdset)) {
|
||||
dbg("conn->write_fd:%d ready", conn->write_fd);
|
||||
rcw = write_from_buf(conn);
|
||||
if (rcw < 0) {
|
||||
/* often indicates that conn is gone (freed), do not use it anymore */
|
||||
goto next1;
|
||||
}
|
||||
}
|
||||
dbg("conn->read_fd:%d ?", conn->read_fd);
|
||||
if (conn->read_fd >= 0 && FD_ISSET(conn->read_fd, &rdfdset)) {
|
||||
dbg("conn->read_fd:%d ready", conn->read_fd);
|
||||
read_to_buf(conn);
|
||||
}
|
||||
next1:
|
||||
conn = next;
|
||||
}
|
||||
goto again;
|
||||
}
|
||||
+55
-3
@@ -9,15 +9,42 @@
|
||||
#ifndef ISQRT_TEST
|
||||
# include "libbb.h"
|
||||
#else
|
||||
/* gcc -DISQRT_TEST -Wall -O2 isqrt.c -oisqrt && ./isqrt $((RANDOM*RANDOM)) */
|
||||
// gcc -DISQRT_TEST -Wall -Os -ffunction-sections isqrt.c -S -fverbose-asm
|
||||
// gcc -DISQRT_TEST -Wall -Os -ffunction-sections isqrt.c -c
|
||||
// gcc -DISQRT_TEST -Wall -Os -ffunction-sections isqrt.c -oisqrt && ./isqrt $((RANDOM*RANDOM))
|
||||
# include <stdlib.h>
|
||||
# include <stdio.h>
|
||||
# include <time.h>
|
||||
# include <limits.h>
|
||||
# define FAST_FUNC /* nothing */
|
||||
#endif
|
||||
|
||||
/* Returns such x that x+1 > sqrt(N) */
|
||||
unsigned long FAST_FUNC isqrt(unsigned long long N)
|
||||
|
||||
/* Stolen from kernel code */
|
||||
unsigned FAST_FUNC isqrt(unsigned long x)
|
||||
{
|
||||
unsigned long y = 0;
|
||||
unsigned long m = 1UL << (8*sizeof(x) - 2);
|
||||
// ^^^^^ should be m = 1UL << ((fls(x) - 1) & ~1UL);
|
||||
// but calculating fls() would take time and code
|
||||
|
||||
do {
|
||||
unsigned long b = y + m;
|
||||
y >>= 1;
|
||||
if (x >= b) {
|
||||
x -= b;
|
||||
y += m;
|
||||
}
|
||||
m >>= 2;
|
||||
} while (m);
|
||||
return y;
|
||||
}
|
||||
|
||||
#if LLONG_MAX > LONG_MAX
|
||||
# if defined(i386)
|
||||
/* Smaller code on this register-starved arch */
|
||||
unsigned long FAST_FUNC isqrt_ull(unsigned long long N)
|
||||
{
|
||||
unsigned long x;
|
||||
unsigned shift;
|
||||
@@ -33,15 +60,40 @@ unsigned long FAST_FUNC isqrt(unsigned long long N)
|
||||
} while ((int)shift >= 0);
|
||||
return x;
|
||||
}
|
||||
# else
|
||||
/* Stolen from kernel code */
|
||||
unsigned long FAST_FUNC isqrt_ull(unsigned long long x)
|
||||
{
|
||||
unsigned long long y = 0;
|
||||
unsigned long long m = 1ULL << (8*sizeof(x) - 2);
|
||||
// ^^^^^ should be m = 1ULL << ((fls(x) - 1) & ~1ULL);
|
||||
// but calculating fls() would take time and code
|
||||
|
||||
do {
|
||||
unsigned long long b = y + m;
|
||||
y >>= 1;
|
||||
if (x >= b) {
|
||||
x -= b;
|
||||
y += m;
|
||||
}
|
||||
m >>= 2;
|
||||
} while (m);
|
||||
return y;
|
||||
}
|
||||
# endif
|
||||
#endif /* wide version needed */
|
||||
|
||||
#ifdef ISQRT_TEST
|
||||
# if LLONG_MAX == LONG_MAX
|
||||
# define isqrt_ull(N) isqrt(N)
|
||||
# endif
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
unsigned long long n = argv[1] ? strtoull(argv[1], NULL, 0) : time(NULL);
|
||||
for (;;) {
|
||||
unsigned long h;
|
||||
n--;
|
||||
h = isqrt(n);
|
||||
h = isqrt_ull(n);
|
||||
if (!(n & 0xffff))
|
||||
printf("isqrt(%llx)=%lx\n", n, h);
|
||||
if ((unsigned long long)h * h > n) {
|
||||
|
||||
+65
-53
@@ -451,7 +451,7 @@ static void put_cur_glyph_and_inc_cursor(void)
|
||||
* have automargin (IOW: it is moving cursor to next line
|
||||
* by itself (which is wrong for VT-10x terminals)),
|
||||
* this will break things: there will be one extra empty line */
|
||||
puts("\r"); /* + implicit '\n' */
|
||||
fputs("\r\n", stderr);
|
||||
#else
|
||||
/* VT-10x terminals don't wrap cursor to next line when last char
|
||||
* on the line is printed - cursor stays "over" this char.
|
||||
@@ -1170,9 +1170,10 @@ static void showfiles(void)
|
||||
);
|
||||
}
|
||||
if (ENABLE_UNICODE_SUPPORT)
|
||||
puts(printable_string(matches[n]));
|
||||
fputs(printable_string(matches[n]), stderr);
|
||||
else
|
||||
puts(matches[n]);
|
||||
fputs(matches[n], stderr);
|
||||
bb_putchar_stderr('\n');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1405,8 +1406,8 @@ unsigned FAST_FUNC size_from_HISTFILESIZE(const char *hp)
|
||||
int size = MAX_HISTORY;
|
||||
if (hp) {
|
||||
size = atoi(hp);
|
||||
if (size <= 0)
|
||||
return 1;
|
||||
if (size < 0)
|
||||
return 0;
|
||||
if (size > MAX_HISTORY)
|
||||
return MAX_HISTORY;
|
||||
}
|
||||
@@ -1463,7 +1464,7 @@ void FAST_FUNC show_history(const line_input_t *st)
|
||||
if (!st)
|
||||
return;
|
||||
for (i = 0; i < st->cnt_history; i++)
|
||||
fprintf(stderr, "%4d %s\n", i, st->history[i]);
|
||||
printf("%4d %s\n", i, st->history[i]);
|
||||
}
|
||||
|
||||
# if ENABLE_FEATURE_EDITING_SAVEHISTORY
|
||||
@@ -1500,18 +1501,21 @@ static void load_history(line_input_t *st_parm)
|
||||
/* NB: do not trash old history if file can't be opened */
|
||||
|
||||
fp = fopen_for_read(st_parm->hist_file);
|
||||
if (fp) {
|
||||
/* clean up old history */
|
||||
for (idx = st_parm->cnt_history; idx > 0;) {
|
||||
idx--;
|
||||
free(st_parm->history[idx]);
|
||||
st_parm->history[idx] = NULL;
|
||||
}
|
||||
if (!fp)
|
||||
return;
|
||||
|
||||
/* fill temp_h[], retaining only last MAX_HISTORY lines */
|
||||
memset(temp_h, 0, sizeof(temp_h));
|
||||
idx = 0;
|
||||
st_parm->cnt_history_in_file = 0;
|
||||
/* clean up old history */
|
||||
for (idx = st_parm->cnt_history; idx > 0;) {
|
||||
idx--;
|
||||
free(st_parm->history[idx]);
|
||||
st_parm->history[idx] = NULL;
|
||||
}
|
||||
|
||||
/* fill temp_h[], retaining only last max_history lines */
|
||||
memset(temp_h, 0, sizeof(temp_h));
|
||||
idx = 0;
|
||||
st_parm->cnt_history_in_file = 0;
|
||||
if (st_parm->max_history != 0) {
|
||||
while ((line = xmalloc_fgetline(fp)) != NULL) {
|
||||
if (line[0] == '\0') {
|
||||
free(line);
|
||||
@@ -1524,52 +1528,62 @@ static void load_history(line_input_t *st_parm)
|
||||
if (idx == st_parm->max_history)
|
||||
idx = 0;
|
||||
}
|
||||
fclose(fp);
|
||||
}
|
||||
fclose(fp);
|
||||
|
||||
/* find first non-NULL temp_h[], if any */
|
||||
if (st_parm->cnt_history_in_file) {
|
||||
while (temp_h[idx] == NULL) {
|
||||
idx++;
|
||||
if (idx == st_parm->max_history)
|
||||
idx = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* copy temp_h[] to st_parm->history[] */
|
||||
for (i = 0; i < st_parm->max_history;) {
|
||||
line = temp_h[idx];
|
||||
if (!line)
|
||||
break;
|
||||
/* find first non-NULL temp_h[], if any */
|
||||
if (st_parm->cnt_history_in_file != 0) {
|
||||
while (temp_h[idx] == NULL) {
|
||||
idx++;
|
||||
if (idx == st_parm->max_history)
|
||||
idx = 0;
|
||||
line_len = strlen(line);
|
||||
if (line_len >= MAX_LINELEN)
|
||||
line[MAX_LINELEN-1] = '\0';
|
||||
st_parm->history[i++] = line;
|
||||
}
|
||||
st_parm->cnt_history = i;
|
||||
if (ENABLE_FEATURE_EDITING_SAVE_ON_EXIT)
|
||||
st_parm->cnt_history_in_file = i;
|
||||
}
|
||||
|
||||
/* copy temp_h[] to st_parm->history[] */
|
||||
for (i = 0; i < st_parm->max_history;) {
|
||||
line = temp_h[idx];
|
||||
if (!line)
|
||||
break;
|
||||
idx++;
|
||||
if (idx == st_parm->max_history)
|
||||
idx = 0;
|
||||
line_len = strlen(line);
|
||||
if (line_len >= MAX_LINELEN)
|
||||
line[MAX_LINELEN-1] = '\0';
|
||||
st_parm->history[i++] = line;
|
||||
}
|
||||
st_parm->cnt_history = i;
|
||||
if (ENABLE_FEATURE_EDITING_SAVE_ON_EXIT)
|
||||
st_parm->cnt_history_in_file = i;
|
||||
}
|
||||
|
||||
# if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT
|
||||
void save_history(line_input_t *st)
|
||||
void FAST_FUNC save_history(line_input_t *st)
|
||||
{
|
||||
FILE *fp;
|
||||
|
||||
if (!st || !st->hist_file)
|
||||
/* bash compat: HISTFILE="" disables history saving */
|
||||
if (!st || !st->hist_file || !st->hist_file[0])
|
||||
return;
|
||||
if (st->cnt_history <= st->cnt_history_in_file)
|
||||
return;
|
||||
return; /* no new entries were added */
|
||||
/* note: if st->max_history is 0, we do not abort: we truncate the history to 0 lines */
|
||||
|
||||
fp = fopen(st->hist_file, "a");
|
||||
fp = fopen(st->hist_file, (st->max_history == 0 ? "w" : "a"));
|
||||
if (fp) {
|
||||
int i, fd;
|
||||
char *new_name;
|
||||
line_input_t *st_temp;
|
||||
|
||||
/* max_history==0 needs special-casing in general code,
|
||||
* just handle it in a simpler way: */
|
||||
if (st->max_history == 0) {
|
||||
/* fopen("w") already truncated it */
|
||||
fclose(fp);
|
||||
return;
|
||||
}
|
||||
|
||||
for (i = st->cnt_history_in_file; i < st->cnt_history; i++)
|
||||
fprintf(fp, "%s\n", st->history[i]);
|
||||
fclose(fp);
|
||||
@@ -1579,6 +1593,8 @@ void save_history(line_input_t *st)
|
||||
st_temp = new_line_input_t(st->flags);
|
||||
st_temp->hist_file = st->hist_file;
|
||||
st_temp->max_history = st->max_history;
|
||||
/* load no more than max_history last lines */
|
||||
/* (in unlikely case that file disappeared, st_temp gets empty history) */
|
||||
load_history(st_temp);
|
||||
|
||||
/* write out temp file and replace hist_file atomically */
|
||||
@@ -1602,13 +1618,13 @@ static void save_history(char *str)
|
||||
int fd;
|
||||
int len, len2;
|
||||
|
||||
if (!state->hist_file)
|
||||
/* bash compat: HISTFILE="" disables history saving */
|
||||
if (!state->hist_file || !state->hist_file[0])
|
||||
return;
|
||||
|
||||
fd = open(state->hist_file, O_WRONLY | O_CREAT | O_APPEND, 0600);
|
||||
if (fd < 0)
|
||||
return;
|
||||
xlseek(fd, 0, SEEK_END); /* paranoia */
|
||||
len = strlen(str);
|
||||
str[len] = '\n'; /* we (try to) do atomic write */
|
||||
len2 = full_write(fd, str, len + 1);
|
||||
@@ -1663,13 +1679,10 @@ static void remember_in_history(char *str)
|
||||
if (str[0] == '\0')
|
||||
return;
|
||||
i = state->cnt_history;
|
||||
/* Don't save dupes */
|
||||
if (i && strcmp(state->history[i-1], str) == 0)
|
||||
/* Don't save dups */
|
||||
if (i != 0 && strcmp(state->history[i-1], str) == 0)
|
||||
return;
|
||||
|
||||
free(state->history[state->max_history]); /* redundant, paranoia */
|
||||
state->history[state->max_history] = NULL; /* redundant, paranoia */
|
||||
|
||||
/* If history[] is full, remove the oldest command */
|
||||
/* we need to keep history[state->max_history] empty, hence >=, not > */
|
||||
if (i >= state->max_history) {
|
||||
@@ -1682,7 +1695,7 @@ static void remember_in_history(char *str)
|
||||
state->cnt_history_in_file--;
|
||||
# endif
|
||||
}
|
||||
/* i <= state->max_history-1 */
|
||||
/* i < state->max_history */
|
||||
state->history[i++] = xstrdup(str);
|
||||
/* i <= state->max_history */
|
||||
state->cur_history = i;
|
||||
@@ -2066,7 +2079,7 @@ static void parse_and_put_prompt(const char *prmt_ptr)
|
||||
if (c == 'w')
|
||||
break;
|
||||
cp = strrchr(pbuf, '/');
|
||||
if (cp)
|
||||
if (cp && cp[1])
|
||||
pbuf = (char*)cp + 1;
|
||||
break;
|
||||
// bb_process_escape_sequence does this now:
|
||||
@@ -2194,7 +2207,6 @@ static int lineedit_read_key(char *read_key_buffer, int timeout)
|
||||
errno = EINTR;
|
||||
return -1;
|
||||
}
|
||||
//FIXME: still races here with signals, but small window to poll() inside read_key
|
||||
IF_FEATURE_EDITING_WINCH(S.ok_to_redraw = 1;)
|
||||
/* errno = 0; - read_key does this itself */
|
||||
ic = read_key(STDIN_FILENO, read_key_buffer, timeout);
|
||||
|
||||
@@ -28,8 +28,6 @@ void FAST_FUNC print_login_issue(const char *issue_file, const char *tty)
|
||||
time(&t);
|
||||
uname(&uts);
|
||||
|
||||
puts("\r"); /* start a new line */
|
||||
|
||||
fp = fopen_for_read(issue_file);
|
||||
if (!fp)
|
||||
return;
|
||||
|
||||
+1
-1
@@ -158,7 +158,7 @@ static int set_loopdev_params(int lfd,
|
||||
if (rc == 0)
|
||||
return rc; /* SUCCESS! */
|
||||
# if ENABLE_TRY_LOOP_CONFIGURE
|
||||
if (errno != EINVAL)
|
||||
if (errno != EINVAL && errno != ENOTTY)
|
||||
return rc; /* error other than old kernel */
|
||||
/* Old kernel, fall through into old way to do it: */
|
||||
# endif
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
/* vi: set sw=4 ts=4: */
|
||||
/*
|
||||
* Utility routines.
|
||||
*
|
||||
* Copyright (C) 2025 Denys Vlasenko <vda.linux@googlemail.com>
|
||||
*
|
||||
* Licensed under GPLv2, see file LICENSE in this source tree.
|
||||
*/
|
||||
//kbuild:lib-y += poll_with_signals.o
|
||||
|
||||
#include "libbb.h"
|
||||
|
||||
/* Shells, for example, need their line input and "read" builtin
|
||||
* to be interruptible, and the naive handling of it a-la:
|
||||
* if (bb_got_signal) {
|
||||
* errno = EINTR;
|
||||
* return -1;
|
||||
* }
|
||||
* poll(pfd, 1, -1); // signal here would set EINTR
|
||||
* is racy.
|
||||
* This is a bit heavy-handed, but safe wrt races:
|
||||
*/
|
||||
int FAST_FUNC check_got_signal_and_poll(struct pollfd pfd[1], int timeout)
|
||||
{
|
||||
int n;
|
||||
struct timespec tv;
|
||||
sigset_t orig_mask;
|
||||
|
||||
if (bb_got_signal) /* optimization */
|
||||
goto eintr;
|
||||
|
||||
if (timeout >= 0) {
|
||||
tv.tv_sec = timeout / 1000;
|
||||
tv.tv_nsec = (timeout % 1000) * 1000000;
|
||||
}
|
||||
/* test bb_got_signal, then poll(), atomically wrt signals */
|
||||
sigfillset(&orig_mask);
|
||||
sigprocmask2(SIG_BLOCK, &orig_mask);
|
||||
if (bb_got_signal) {
|
||||
sigprocmask2(SIG_SETMASK, &orig_mask);
|
||||
eintr:
|
||||
errno = EINTR; /* inform the caller that we got a signal */
|
||||
return -1;
|
||||
}
|
||||
n = ppoll(pfd, 1, timeout >= 0 ? &tv : NULL, &orig_mask);
|
||||
sigprocmask2(SIG_SETMASK, &orig_mask);
|
||||
return n;
|
||||
}
|
||||
+91
-92
@@ -109,7 +109,7 @@ void FAST_FUNC free_procps_scan(procps_status_t* sp)
|
||||
}
|
||||
|
||||
#if ENABLE_FEATURE_TOPMEM || ENABLE_PMAP
|
||||
static unsigned long long fast_strtoull_16(char **endptr)
|
||||
unsigned long long FAST_FUNC fast_strtoull_16(char **endptr)
|
||||
{
|
||||
unsigned char c;
|
||||
char *str = *endptr;
|
||||
@@ -130,7 +130,7 @@ static unsigned long long fast_strtoull_16(char **endptr)
|
||||
|
||||
#if ENABLE_FEATURE_FAST_TOP || ENABLE_FEATURE_TOPMEM || ENABLE_PMAP
|
||||
/* We cut a lot of corners here for speed */
|
||||
static unsigned long fast_strtoul_10(char **endptr)
|
||||
unsigned long FAST_FUNC fast_strtoul_10(char **endptr)
|
||||
{
|
||||
unsigned char c;
|
||||
char *str = *endptr;
|
||||
@@ -143,6 +143,24 @@ static unsigned long fast_strtoul_10(char **endptr)
|
||||
*endptr = str + 1; /* We skip trailing space! */
|
||||
return n;
|
||||
}
|
||||
# if LONG_MAX < LLONG_MAX
|
||||
/* For VSZ, which can be very large */
|
||||
static unsigned long long fast_strtoull_10(char **endptr)
|
||||
{
|
||||
unsigned char c;
|
||||
char *str = *endptr;
|
||||
unsigned long long n = *str - '0';
|
||||
|
||||
/* Need to stop on both ' ' and '\n' */
|
||||
while ((c = *++str) > ' ')
|
||||
n = n*10 + (c - '0');
|
||||
|
||||
*endptr = str + 1; /* We skip trailing space! */
|
||||
return n;
|
||||
}
|
||||
# else
|
||||
# define fast_strtoull_10(endptr) fast_strtoul_10(endptr)
|
||||
# endif
|
||||
|
||||
# if ENABLE_FEATURE_FAST_TOP
|
||||
static long fast_strtol_10(char **endptr)
|
||||
@@ -155,7 +173,7 @@ static long fast_strtol_10(char **endptr)
|
||||
}
|
||||
# endif
|
||||
|
||||
static char *skip_fields(char *str, int count)
|
||||
char* FAST_FUNC skip_fields(char *str, int count)
|
||||
{
|
||||
do {
|
||||
while (*str++ != ' ')
|
||||
@@ -166,35 +184,25 @@ static char *skip_fields(char *str, int count)
|
||||
}
|
||||
#endif
|
||||
|
||||
#if ENABLE_FEATURE_TOPMEM || ENABLE_PMAP
|
||||
static char* skip_whitespace_if_prefixed_with(char *buf, const char *prefix)
|
||||
#if ENABLE_FEATURE_TOPMEM
|
||||
static NOINLINE void procps_read_smaps(pid_t pid, procps_status_t *sp)
|
||||
{
|
||||
char *tp = is_prefixed_with(buf, prefix);
|
||||
if (tp) {
|
||||
tp = skip_whitespace(tp);
|
||||
}
|
||||
return tp;
|
||||
}
|
||||
// There is A LOT of /proc/PID/smaps data on a big system.
|
||||
// Optimize this for speed, makes "top -m" faster.
|
||||
//TODO large speedup:
|
||||
//read /proc/PID/smaps_rollup (cumulative stats of all mappings, much faster)
|
||||
//and /proc/PID/maps to get mapped_ro and mapped_rw (IOW: VSZ,VSZRW)
|
||||
|
||||
int FAST_FUNC procps_read_smaps(pid_t pid, struct smaprec *total,
|
||||
void (*cb)(struct smaprec *, void *), void *data)
|
||||
{
|
||||
FILE *file;
|
||||
struct smaprec currec;
|
||||
char filename[sizeof("/proc/%u/smaps") + sizeof(int)*3];
|
||||
char buf[PROCPS_BUFSIZE];
|
||||
#if !ENABLE_PMAP
|
||||
void (*cb)(struct smaprec *, void *) = NULL;
|
||||
void *data = NULL;
|
||||
#endif
|
||||
char buf[PROCPS_BUFSIZE] ALIGN4;
|
||||
|
||||
sprintf(filename, "/proc/%u/smaps", (int)pid);
|
||||
|
||||
file = fopen_for_read(filename);
|
||||
if (!file)
|
||||
return 1;
|
||||
return;
|
||||
|
||||
memset(&currec, 0, sizeof(currec));
|
||||
while (fgets(buf, PROCPS_BUFSIZE, file)) {
|
||||
// Each mapping datum has this form:
|
||||
// f7d29000-f7d39000 rw-s FILEOFS M:m INODE FILENAME
|
||||
@@ -202,80 +210,60 @@ int FAST_FUNC procps_read_smaps(pid_t pid, struct smaprec *total,
|
||||
// Rss: nnn kB
|
||||
// .....
|
||||
|
||||
char *tp, *p;
|
||||
|
||||
char *tp;
|
||||
#define bytes4 *(uint32_t*)buf
|
||||
#define Priv PACK32_BYTES('P','r','i','v')
|
||||
#define Shar PACK32_BYTES('S','h','a','r')
|
||||
#define SCAN(S, X) \
|
||||
if ((tp = skip_whitespace_if_prefixed_with(buf, S)) != NULL) { \
|
||||
total->X += currec.X = fast_strtoul_10(&tp); \
|
||||
continue; \
|
||||
if (memcmp(buf+4, S, sizeof(S)-1) == 0) { \
|
||||
tp = skip_whitespace(buf+4 + sizeof(S)-1); \
|
||||
sp->X += fast_strtoul_10(&tp); \
|
||||
continue; \
|
||||
}
|
||||
if (bytes4 == Priv) {
|
||||
SCAN("ate_Dirty:", private_dirty)
|
||||
SCAN("ate_Clean:", private_clean)
|
||||
}
|
||||
if (cb) {
|
||||
SCAN("Pss:" , smap_pss );
|
||||
SCAN("Swap:" , smap_swap );
|
||||
if (bytes4 == Shar) {
|
||||
SCAN("ed_Dirty:" , shared_dirty )
|
||||
SCAN("ed_Clean:" , shared_clean )
|
||||
}
|
||||
SCAN("Private_Dirty:", private_dirty);
|
||||
SCAN("Private_Clean:", private_clean);
|
||||
SCAN("Shared_Dirty:" , shared_dirty );
|
||||
SCAN("Shared_Clean:" , shared_clean );
|
||||
#undef bytes4
|
||||
#undef Priv
|
||||
#undef Shar
|
||||
#undef SCAN
|
||||
tp = strchr(buf, '-');
|
||||
if (tp) {
|
||||
// We reached next mapping - the line of this form:
|
||||
// f7d29000-f7d39000 rw-s FILEOFS M:m INODE FILENAME
|
||||
|
||||
if (cb) {
|
||||
/* If we have a previous record, there's nothing more
|
||||
* for it, call the callback and clear currec
|
||||
*/
|
||||
if (currec.smap_size)
|
||||
cb(&currec, data);
|
||||
free(currec.smap_name);
|
||||
}
|
||||
memset(&currec, 0, sizeof(currec));
|
||||
char *rwx;
|
||||
unsigned long long sz;
|
||||
|
||||
*tp = ' ';
|
||||
tp = buf;
|
||||
currec.smap_start = fast_strtoull_16(&tp);
|
||||
currec.smap_size = (fast_strtoull_16(&tp) - currec.smap_start) >> 10;
|
||||
|
||||
strncpy(currec.smap_mode, tp, sizeof(currec.smap_mode)-1);
|
||||
|
||||
sz = fast_strtoull_16(&tp); // start
|
||||
sz = (fast_strtoull_16(&tp) - sz) >> 10; // end - start
|
||||
// tp -> "rw-s" string
|
||||
rwx = tp;
|
||||
// skipping "rw-s FILEOFS M:m INODE "
|
||||
tp = skip_whitespace(skip_fields(tp, 4));
|
||||
// filter out /dev/something (something != zero)
|
||||
if (!is_prefixed_with(tp, "/dev/") || strcmp(tp, "/dev/zero\n") == 0) {
|
||||
if (currec.smap_mode[1] == 'w') {
|
||||
currec.mapped_rw = currec.smap_size;
|
||||
total->mapped_rw += currec.smap_size;
|
||||
} else if (currec.smap_mode[1] == '-') {
|
||||
currec.mapped_ro = currec.smap_size;
|
||||
total->mapped_ro += currec.smap_size;
|
||||
}
|
||||
// if not a device memory mapped...
|
||||
if (memcmp(tp, "/dev/", 5) != 0 // not "/dev/something"
|
||||
|| strcmp(tp + 5, "zero\n") == 0 // or is "/dev/zero" (which isn't a device)
|
||||
) {
|
||||
if (rwx[1] == 'w')
|
||||
sp->mapped_rw += sz;
|
||||
else if (rwx[0] == 'r' || rwx[2] == 'x')
|
||||
sp->mapped_ro += sz;
|
||||
// else: seen "---p" mappings (mmap guard gaps?),
|
||||
// do NOT account these as VSZ, they aren't really
|
||||
}
|
||||
|
||||
if (strcmp(tp, "[stack]\n") == 0)
|
||||
total->stack += currec.smap_size;
|
||||
if (cb) {
|
||||
p = skip_non_whitespace(tp);
|
||||
if (p == tp) {
|
||||
currec.smap_name = xstrdup(" [ anon ]");
|
||||
} else {
|
||||
*p = '\0';
|
||||
currec.smap_name = xstrdup(tp);
|
||||
}
|
||||
}
|
||||
total->smap_size += currec.smap_size;
|
||||
sp->stack += sz;
|
||||
}
|
||||
}
|
||||
fclose(file);
|
||||
|
||||
if (cb) {
|
||||
if (currec.smap_size)
|
||||
cb(&currec, data);
|
||||
free(currec.smap_name);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -370,7 +358,8 @@ procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags)
|
||||
char *cp, *comm1;
|
||||
int tty;
|
||||
#if !ENABLE_FEATURE_FAST_TOP
|
||||
unsigned long vsz, rss;
|
||||
unsigned long long vsz;
|
||||
unsigned long rss;
|
||||
#endif
|
||||
/* see proc(5) for some details on this */
|
||||
strcpy(filename_tail, "stat");
|
||||
@@ -396,7 +385,7 @@ procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags)
|
||||
"%ld " /* nice */
|
||||
"%*s %*s " /* timeout, it_real_value */
|
||||
"%lu " /* start_time */
|
||||
"%lu " /* vsize */
|
||||
"%llu " /* vsize - can be very large */
|
||||
"%lu " /* rss */
|
||||
# if ENABLE_FEATURE_TOP_SMP_PROCESS
|
||||
"%*s %*s %*s %*s %*s %*s " /*rss_rlim, start_code, end_code, start_stack, kstk_esp, kstk_eip */
|
||||
@@ -449,7 +438,7 @@ procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags)
|
||||
cp = skip_fields(cp, 2); /* timeout, it_real_value */
|
||||
sp->start_time = fast_strtoul_10(&cp);
|
||||
/* vsz is in bytes and we want kb */
|
||||
sp->vsz = fast_strtoul_10(&cp) >> 10;
|
||||
sp->vsz = fast_strtoull_10(&cp) >> 10;
|
||||
/* vsz is in bytes but rss is in *PAGES*! Can you believe that? */
|
||||
sp->rss = fast_strtoul_10(&cp) << sp->shift_pages_to_kb;
|
||||
# if ENABLE_FEATURE_TOP_SMP_PROCESS
|
||||
@@ -483,7 +472,7 @@ procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags)
|
||||
|
||||
#if ENABLE_FEATURE_TOPMEM
|
||||
if (flags & PSSCAN_SMAPS)
|
||||
procps_read_smaps(pid, &sp->smaps, NULL, NULL);
|
||||
procps_read_smaps(pid, sp);
|
||||
#endif /* TOPMEM */
|
||||
#if ENABLE_FEATURE_PS_ADDITIONAL_COLUMNS
|
||||
if (flags & PSSCAN_RUIDGID) {
|
||||
@@ -566,36 +555,45 @@ procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags)
|
||||
return sp;
|
||||
}
|
||||
|
||||
void FAST_FUNC read_cmdline(char *buf, int col, unsigned pid, const char *comm)
|
||||
int FAST_FUNC read_cmdline(char *buf, int col, unsigned pid, const char *comm)
|
||||
{
|
||||
int sz;
|
||||
char filename[sizeof("/proc/%u/cmdline") + sizeof(int)*3];
|
||||
|
||||
sprintf(filename, "/proc/%u/cmdline", pid);
|
||||
sz = open_read_close(filename, buf, col - 1);
|
||||
if (sz < 0)
|
||||
return sz;
|
||||
if (sz > 0) {
|
||||
const char *base;
|
||||
const char *program_basename;
|
||||
int comm_len;
|
||||
|
||||
buf[sz] = '\0';
|
||||
while (--sz >= 0 && buf[sz] == '\0')
|
||||
continue;
|
||||
/* Prevent basename("process foo/bar") = "bar" */
|
||||
strchrnul(buf, ' ')[0] = '\0';
|
||||
base = bb_basename(buf); /* before we replace argv0's NUL with space */
|
||||
|
||||
/* Find "program" in "[-][/PATH/TO/]program" */
|
||||
strchrnul(buf, ' ')[0] = '\0'; /* prevent basename("program foo/bar") = "bar" */
|
||||
program_basename = bb_basename(buf[0] == '-' ? buf + 1 : buf);
|
||||
/* ^^^ note: must do it *before* replacing argv0's NUL with space */
|
||||
|
||||
/* Prevent stuff like this:
|
||||
* echo 'sleep 999; exit' >`printf '\ec'`; sh ?c
|
||||
* messing up top and ps output (or worse).
|
||||
* This also replaces NULs with spaces, converting
|
||||
* list of NUL-strings into one string.
|
||||
*/
|
||||
while (sz >= 0) {
|
||||
if ((unsigned char)(buf[sz]) < ' ')
|
||||
buf[sz] = ' ';
|
||||
buf[sz] = (buf[sz] ? /*ctrl*/'?' : /*NUL*/' ');
|
||||
sz--;
|
||||
}
|
||||
if (base[0] == '-') /* "-sh" (login shell)? */
|
||||
base++;
|
||||
|
||||
/* If comm differs from argv0, prepend "{comm} ".
|
||||
* It allows to see thread names set by prctl(PR_SET_NAME).
|
||||
*/
|
||||
if (!comm)
|
||||
return;
|
||||
return 0;
|
||||
comm_len = strlen(comm);
|
||||
/* Why compare up to comm_len, not COMM_LEN-1?
|
||||
* Well, some processes rewrite argv, and use _spaces_ there
|
||||
@@ -603,19 +601,20 @@ void FAST_FUNC read_cmdline(char *buf, int col, unsigned pid, const char *comm)
|
||||
* I prefer to still treat argv0 "process foo bar"
|
||||
* as 'equal' to comm "process".
|
||||
*/
|
||||
if (strncmp(base, comm, comm_len) != 0) {
|
||||
if (strncmp(program_basename, comm, comm_len) != 0) {
|
||||
comm_len += 3;
|
||||
if (col > comm_len)
|
||||
memmove(buf + comm_len, buf, col - comm_len);
|
||||
snprintf(buf, col, "{%s}", comm);
|
||||
if (col <= comm_len)
|
||||
return;
|
||||
return 0;
|
||||
buf[comm_len - 1] = ' ';
|
||||
buf[col - 1] = '\0';
|
||||
}
|
||||
} else {
|
||||
snprintf(buf, col, "[%s]", comm ? comm : "?");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* from kernel:
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
/* vi: set sw=4 ts=4: */
|
||||
/*
|
||||
* Utility routines.
|
||||
*
|
||||
* Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
|
||||
*
|
||||
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
||||
*/
|
||||
|
||||
/* Returns >=64 for invalid chars */
|
||||
int FAST_FUNC a2i64(char c)
|
||||
{
|
||||
unsigned char ch = c;
|
||||
if (ch >= 'a')
|
||||
/* "a..z" to 38..63 */
|
||||
/* anything after "z": positive int >= 64 */
|
||||
return (ch - 'a' + 38);
|
||||
|
||||
if (ch > 'Z')
|
||||
/* after "Z" but before "a": positive byte >= 64 */
|
||||
return ch;
|
||||
|
||||
if (ch >= 'A')
|
||||
/* "A..Z" to 12..37 */
|
||||
return (ch - 'A' + 12);
|
||||
|
||||
if (ch > '9')
|
||||
return 64;
|
||||
|
||||
/* "./0123456789" to 0,1,2..11 */
|
||||
/* anything before "." becomes positive byte >= 64 */
|
||||
return (unsigned char)(ch - '.');
|
||||
}
|
||||
|
||||
/* 0..63 ->
|
||||
* "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
||||
*/
|
||||
int FAST_FUNC i2a64(int i)
|
||||
{
|
||||
i &= 0x3f;
|
||||
|
||||
i += '.';
|
||||
/* the above maps 0..11 to "./0123456789":
|
||||
* ACSII codes of "./" are ('0'-2) and ('0'-1) */
|
||||
|
||||
if (i > '9')
|
||||
i += ('A' - '9' - 1);
|
||||
if (i > 'Z')
|
||||
i += ('a' - 'Z' - 1);
|
||||
return i;
|
||||
}
|
||||
|
||||
char* FAST_FUNC
|
||||
num2str64_lsb_first(char *s, unsigned v, int n)
|
||||
{
|
||||
while (--n >= 0) {
|
||||
*s++ = i2a64(v);
|
||||
v >>= 6;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
static void
|
||||
num2str64_4chars_msb_first(char *s, unsigned v)
|
||||
{
|
||||
*s++ = i2a64(v >> 18); /* bits 23..18 */
|
||||
*s++ = i2a64(v >> 12); /* bits 17..12 */
|
||||
*s++ = i2a64(v >> 6); /* bits 11..6 */
|
||||
*s = i2a64(v); /* bits 5..0 */
|
||||
}
|
||||
|
||||
int FAST_FUNC crypt_make_rand64encoded(char *p, int cnt /*, int x */)
|
||||
{
|
||||
/* was: x += ... */
|
||||
unsigned x = getpid() + monotonic_us();
|
||||
do {
|
||||
/* x = (x*1664525 + 1013904223) % 2^32 generator is lame
|
||||
* (low-order bit is not "random", etc...),
|
||||
* but for our purposes it is good enough */
|
||||
x = x*1664525 + 1013904223;
|
||||
/* BTW, Park and Miller's "minimal standard generator" is
|
||||
* x = x*16807 % ((2^31)-1)
|
||||
* It has no problem with visibly alternating lowest bit
|
||||
* but is also weak in cryptographic sense + needs div,
|
||||
* which needs more code (and slower) on many CPUs */
|
||||
*p++ = i2a64(x >> 16);
|
||||
*p++ = i2a64(x >> 22);
|
||||
} while (--cnt);
|
||||
*p = '\0';
|
||||
return x;
|
||||
}
|
||||
+58
-55
@@ -13,48 +13,11 @@
|
||||
#endif
|
||||
#include "libbb.h"
|
||||
|
||||
/* static const uint8_t ascii64[] ALIGN1 =
|
||||
* "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
||||
*/
|
||||
|
||||
static int i64c(int i)
|
||||
{
|
||||
i &= 0x3f;
|
||||
if (i == 0)
|
||||
return '.';
|
||||
if (i == 1)
|
||||
return '/';
|
||||
if (i < 12)
|
||||
return ('0' - 2 + i);
|
||||
if (i < 38)
|
||||
return ('A' - 12 + i);
|
||||
return ('a' - 38 + i);
|
||||
}
|
||||
|
||||
int FAST_FUNC crypt_make_salt(char *p, int cnt /*, int x */)
|
||||
{
|
||||
/* was: x += ... */
|
||||
unsigned x = getpid() + monotonic_us();
|
||||
do {
|
||||
/* x = (x*1664525 + 1013904223) % 2^32 generator is lame
|
||||
* (low-order bit is not "random", etc...),
|
||||
* but for our purposes it is good enough */
|
||||
x = x*1664525 + 1013904223;
|
||||
/* BTW, Park and Miller's "minimal standard generator" is
|
||||
* x = x*16807 % ((2^31)-1)
|
||||
* It has no problem with visibly alternating lowest bit
|
||||
* but is also weak in cryptographic sense + needs div,
|
||||
* which needs more code (and slower) on many CPUs */
|
||||
*p++ = i64c(x >> 16);
|
||||
*p++ = i64c(x >> 22);
|
||||
} while (--cnt);
|
||||
*p = '\0';
|
||||
return x;
|
||||
}
|
||||
#include "pw_ascii64.c"
|
||||
|
||||
char* FAST_FUNC crypt_make_pw_salt(char salt[MAX_PW_SALT_LEN], const char *algo)
|
||||
{
|
||||
int len = 2/2;
|
||||
int len = 2 / 2;
|
||||
char *salt_ptr = salt;
|
||||
|
||||
/* Standard chpasswd uses uppercase algos ("MD5", not "md5").
|
||||
@@ -67,28 +30,61 @@ char* FAST_FUNC crypt_make_pw_salt(char salt[MAX_PW_SALT_LEN], const char *algo)
|
||||
*salt_ptr++ = '$';
|
||||
#if !ENABLE_USE_BB_CRYPT || ENABLE_USE_BB_CRYPT_SHA
|
||||
if ((algo[0]|0x20) == 's') { /* sha */
|
||||
salt[1] = '5' + (strcasecmp(algo, "sha512") == 0);
|
||||
len = 16/2;
|
||||
salt[1] = '5' + (strncasecmp(algo, "sha512", 6) == 0);
|
||||
len = 16 / 2;
|
||||
}
|
||||
#endif
|
||||
#if !ENABLE_USE_BB_CRYPT || ENABLE_USE_BB_CRYPT_YES
|
||||
if ((algo[0]|0x20) == 'y') { /* yescrypt */
|
||||
int rnd;
|
||||
salt[1] = 'y';
|
||||
// The "j9T$" below is the default "yescrypt parameters" encoded by yescrypt_encode_params_r():
|
||||
//shadow-4.17.4/src/passwd.c
|
||||
// salt = crypt_make_salt(NULL, NULL);
|
||||
//shadow-4.17.4/lib/salt.c
|
||||
//const char *crypt_make_salt(const char *meth, void *arg)
|
||||
// if (streq(method, "YESCRYPT")) {
|
||||
// MAGNUM(result, 'y');
|
||||
// salt_len = YESCRYPT_SALT_SIZE; // 24
|
||||
// rounds = YESCRYPT_get_salt_cost(arg); // always Y_COST_DEFAULT == 5 for NULL arg
|
||||
// YESCRYPT_salt_cost_to_buf(result, rounds); // always "j9T$"
|
||||
// char *retval = crypt_gensalt(result, rounds, NULL, 0);
|
||||
//libxcrypt-4.4.38/lib/crypt-yescrypt.c
|
||||
//void gensalt_yescrypt_rn (unsigned long count,
|
||||
// const uint8_t *rbytes, size_t nrbytes,
|
||||
// uint8_t *output, size_t o_size)
|
||||
// yescrypt_params_t params = {
|
||||
// .flags = YESCRYPT_DEFAULTS,
|
||||
// .p = 1,
|
||||
// };
|
||||
// if (count < 3) ... else
|
||||
// params.r = 32; // N in 4KiB
|
||||
// params.N = 1ULL << (count + 7); // 3 -> 1024, 4 -> 2048, ... 11 -> 262144
|
||||
// yescrypt_encode_params_r(¶ms, rbytes, nrbytes, outbuf, o_size) // always "$y$j9T$<random>"
|
||||
len = 22 / 2;
|
||||
salt_ptr = stpcpy(salt_ptr, "j9T$");
|
||||
/* append 2*len random chars */
|
||||
rnd = crypt_make_rand64encoded(salt_ptr, len);
|
||||
/* fix up last char: it must be in 0..3 range (encoded as one of "./01").
|
||||
* IOW: salt_ptr[20..21] encode 16th random byte, must not be > 0xff.
|
||||
* Without this, we can generate salts which are rejected
|
||||
* by implementations with more strict salt length check.
|
||||
*/
|
||||
salt_ptr[21] = i2a64(rnd & 3);
|
||||
/* For "mkpasswd -m yescrypt PASS j9T$<salt>" use case,
|
||||
* "j9T$" is considered part of salt,
|
||||
* need to return pointer to 'j'. Without -4,
|
||||
* we'd end up using "j9T$j9T$<salt>" as salt.
|
||||
*/
|
||||
return salt_ptr - 4;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
crypt_make_salt(salt_ptr, len);
|
||||
crypt_make_rand64encoded(salt_ptr, len); /* appends 2*len random chars */
|
||||
return salt_ptr;
|
||||
}
|
||||
|
||||
#if ENABLE_USE_BB_CRYPT
|
||||
|
||||
static char*
|
||||
to64(char *s, unsigned v, int n)
|
||||
{
|
||||
while (--n >= 0) {
|
||||
/* *s++ = ascii64[v & 0x3f]; */
|
||||
*s++ = i64c(v);
|
||||
v >>= 6;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
/*
|
||||
* DES and MD5 crypt implementations are taken from uclibc.
|
||||
* They were modified to not use static buffers.
|
||||
@@ -99,6 +95,9 @@ to64(char *s, unsigned v, int n)
|
||||
#if ENABLE_USE_BB_CRYPT_SHA
|
||||
#include "pw_encrypt_sha.c"
|
||||
#endif
|
||||
#if ENABLE_USE_BB_CRYPT_YES
|
||||
#include "pw_encrypt_yes.c"
|
||||
#endif
|
||||
|
||||
/* Other advanced crypt ids (TODO?): */
|
||||
/* $2$ or $2a$: Blowfish */
|
||||
@@ -109,13 +108,17 @@ static struct des_ctx *des_ctx;
|
||||
/* my_crypt returns malloc'ed data */
|
||||
static char *my_crypt(const char *key, const char *salt)
|
||||
{
|
||||
/* MD5 or SHA? */
|
||||
/* "$x$...." string? */
|
||||
if (salt[0] == '$' && salt[1] && salt[2] == '$') {
|
||||
if (salt[1] == '1')
|
||||
return md5_crypt(xzalloc(MD5_OUT_BUFSIZE), (unsigned char*)key, (unsigned char*)salt);
|
||||
#if ENABLE_USE_BB_CRYPT_SHA
|
||||
if (salt[1] == '5' || salt[1] == '6')
|
||||
return sha_crypt((char*)key, (char*)salt);
|
||||
#endif
|
||||
#if ENABLE_USE_BB_CRYPT_YES
|
||||
if (salt[1] == 'y')
|
||||
return yes_crypt(key, salt);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
+17
-71
@@ -186,39 +186,9 @@ static const uint8_t pbox[32] ALIGN1 = {
|
||||
2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25
|
||||
};
|
||||
|
||||
static const uint32_t bits32[32] ALIGN4 = {
|
||||
0x80000000, 0x40000000, 0x20000000, 0x10000000,
|
||||
0x08000000, 0x04000000, 0x02000000, 0x01000000,
|
||||
0x00800000, 0x00400000, 0x00200000, 0x00100000,
|
||||
0x00080000, 0x00040000, 0x00020000, 0x00010000,
|
||||
0x00008000, 0x00004000, 0x00002000, 0x00001000,
|
||||
0x00000800, 0x00000400, 0x00000200, 0x00000100,
|
||||
0x00000080, 0x00000040, 0x00000020, 0x00000010,
|
||||
0x00000008, 0x00000004, 0x00000002, 0x00000001
|
||||
};
|
||||
|
||||
static const uint8_t bits8[8] ALIGN1 = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
|
||||
|
||||
|
||||
static int
|
||||
ascii_to_bin(char ch)
|
||||
{
|
||||
if (ch > 'z')
|
||||
return 0;
|
||||
if (ch >= 'a')
|
||||
return (ch - 'a' + 38);
|
||||
if (ch > 'Z')
|
||||
return 0;
|
||||
if (ch >= 'A')
|
||||
return (ch - 'A' + 12);
|
||||
if (ch > '9')
|
||||
return 0;
|
||||
if (ch >= '.')
|
||||
return (ch - '.');
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/* Static stuff that stays resident and doesn't change after
|
||||
* being initialized, and therefore doesn't need to be made
|
||||
* reentrant. */
|
||||
@@ -354,11 +324,18 @@ des_init(struct des_ctx *ctx, const struct const_des_ctx *cctx)
|
||||
int i, j, b, k, inbit, obit;
|
||||
uint32_t p;
|
||||
const uint32_t *bits28, *bits24;
|
||||
uint32_t bits32[32];
|
||||
|
||||
if (!ctx)
|
||||
ctx = xmalloc(sizeof(*ctx));
|
||||
const_ctx = cctx;
|
||||
|
||||
p = 0x80000000U;
|
||||
for (i = 0; p; i++) {
|
||||
bits32[i] = p;
|
||||
p >>= 1;
|
||||
}
|
||||
|
||||
#if USE_REPETITIVE_SPEEDUP
|
||||
old_rawkey0 = old_rawkey1 = 0;
|
||||
old_salt = 0;
|
||||
@@ -694,21 +671,6 @@ do_des(struct des_ctx *ctx, /*uint32_t l_in, uint32_t r_in,*/ uint32_t *l_out, u
|
||||
|
||||
#define DES_OUT_BUFSIZE 21
|
||||
|
||||
static void
|
||||
to64_msb_first(char *s, unsigned v)
|
||||
{
|
||||
#if 0
|
||||
*s++ = ascii64[(v >> 18) & 0x3f]; /* bits 23..18 */
|
||||
*s++ = ascii64[(v >> 12) & 0x3f]; /* bits 17..12 */
|
||||
*s++ = ascii64[(v >> 6) & 0x3f]; /* bits 11..6 */
|
||||
*s = ascii64[v & 0x3f]; /* bits 5..0 */
|
||||
#endif
|
||||
*s++ = i64c(v >> 18); /* bits 23..18 */
|
||||
*s++ = i64c(v >> 12); /* bits 17..12 */
|
||||
*s++ = i64c(v >> 6); /* bits 11..6 */
|
||||
*s = i64c(v); /* bits 5..0 */
|
||||
}
|
||||
|
||||
static char *
|
||||
NOINLINE
|
||||
des_crypt(struct des_ctx *ctx, char output[DES_OUT_BUFSIZE],
|
||||
@@ -740,44 +702,28 @@ des_crypt(struct des_ctx *ctx, char output[DES_OUT_BUFSIZE],
|
||||
*/
|
||||
output[0] = salt_str[0];
|
||||
output[1] = salt_str[1];
|
||||
salt = (ascii_to_bin(salt_str[1]) << 6)
|
||||
| ascii_to_bin(salt_str[0]);
|
||||
|
||||
salt = a2i64(salt_str[0]);
|
||||
if (salt >= 64)
|
||||
return NULL; /* bad salt char */
|
||||
salt |= (a2i64(salt_str[1]) << 6);
|
||||
if (salt >= (64 << 6))
|
||||
return NULL; /* bad salt char */
|
||||
setup_salt(ctx, salt); /* set ctx->saltbits for do_des() */
|
||||
|
||||
/* Do it. */
|
||||
do_des(ctx, /*0, 0,*/ &r0, &r1, 25 /* count */);
|
||||
|
||||
/* Now encode the result. */
|
||||
#if 0
|
||||
{
|
||||
uint32_t l = (r0 >> 8);
|
||||
q = (uint8_t *)output + 2;
|
||||
*q++ = ascii64[(l >> 18) & 0x3f]; /* bits 31..26 of r0 */
|
||||
*q++ = ascii64[(l >> 12) & 0x3f]; /* bits 25..20 of r0 */
|
||||
*q++ = ascii64[(l >> 6) & 0x3f]; /* bits 19..14 of r0 */
|
||||
*q++ = ascii64[l & 0x3f]; /* bits 13..8 of r0 */
|
||||
l = ((r0 << 16) | (r1 >> 16));
|
||||
*q++ = ascii64[(l >> 18) & 0x3f]; /* bits 7..2 of r0 */
|
||||
*q++ = ascii64[(l >> 12) & 0x3f]; /* bits 1..2 of r0 and 31..28 of r1 */
|
||||
*q++ = ascii64[(l >> 6) & 0x3f]; /* bits 27..22 of r1 */
|
||||
*q++ = ascii64[l & 0x3f]; /* bits 21..16 of r1 */
|
||||
l = r1 << 2;
|
||||
*q++ = ascii64[(l >> 12) & 0x3f]; /* bits 15..10 of r1 */
|
||||
*q++ = ascii64[(l >> 6) & 0x3f]; /* bits 9..4 of r1 */
|
||||
*q++ = ascii64[l & 0x3f]; /* bits 3..0 of r1 + 00 */
|
||||
*q = 0;
|
||||
}
|
||||
#else
|
||||
/* Each call takes low-order 24 bits and stores 4 chars */
|
||||
/* bits 31..8 of r0 */
|
||||
to64_msb_first(output + 2, (r0 >> 8));
|
||||
num2str64_4chars_msb_first(output + 2, (r0 >> 8));
|
||||
/* bits 7..0 of r0 and 31..16 of r1 */
|
||||
to64_msb_first(output + 6, (r0 << 16) | (r1 >> 16));
|
||||
num2str64_4chars_msb_first(output + 6, (r0 << 16) | (r1 >> 16));
|
||||
/* bits 15..0 of r1 and two zero bits (plus extra zero byte) */
|
||||
to64_msb_first(output + 10, (r1 << 8));
|
||||
num2str64_4chars_msb_first(output + 10, (r1 << 8));
|
||||
/* extra zero byte is encoded as '.', fixing it */
|
||||
output[13] = '\0';
|
||||
#endif
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
@@ -149,9 +149,9 @@ md5_crypt(char result[MD5_OUT_BUFSIZE], const unsigned char *pw, const unsigned
|
||||
final[16] = final[5];
|
||||
for (i = 0; i < 5; i++) {
|
||||
unsigned l = (final[i] << 16) | (final[i+6] << 8) | final[i+12];
|
||||
p = to64(p, l, 4);
|
||||
p = num2str64_lsb_first(p, l, 4);
|
||||
}
|
||||
p = to64(p, final[11], 2);
|
||||
p = num2str64_lsb_first(p, final[11], 2);
|
||||
*p = '\0';
|
||||
|
||||
/* Don't leave anything around in vm they could use. */
|
||||
|
||||
@@ -84,8 +84,7 @@ sha_crypt(/*const*/ char *key_data, /*const*/ char *salt_data)
|
||||
as a scratch space later. */
|
||||
salt_data = xstrndup(salt_data, salt_len);
|
||||
/* add "salt$" to result */
|
||||
strcpy(resptr, salt_data);
|
||||
resptr += salt_len;
|
||||
resptr = stpcpy(resptr, salt_data);
|
||||
*resptr++ = '$';
|
||||
/* key data doesn't need much processing */
|
||||
key_len = strlen(key_data);
|
||||
@@ -198,7 +197,7 @@ sha_crypt(/*const*/ char *key_data, /*const*/ char *salt_data)
|
||||
#define b64_from_24bit(B2, B1, B0, N) \
|
||||
do { \
|
||||
unsigned w = ((B2) << 16) | ((B1) << 8) | (B0); \
|
||||
resptr = to64(resptr, w, N); \
|
||||
resptr = num2str64_lsb_first(resptr, w, N); \
|
||||
} while (0)
|
||||
if (_32or64 == 32) { /* sha256 */
|
||||
unsigned i = 0;
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Utility routines.
|
||||
*
|
||||
* Copyright (C) 2025 by Denys Vlasenko <vda.linux@googlemail.com>
|
||||
*
|
||||
* Licensed under GPLv2, see file LICENSE in this source tree.
|
||||
*/
|
||||
#include "yescrypt/alg-yescrypt.h"
|
||||
|
||||
static char *
|
||||
yes_crypt(const char *passwd, const char *salt_data)
|
||||
{
|
||||
/* prefix, '$', hash, NUL */
|
||||
char buf[YESCRYPT_PREFIX_LEN + 1 + YESCRYPT_HASH_LEN + 1];
|
||||
char *retval;
|
||||
|
||||
retval = yescrypt_r(
|
||||
(const uint8_t *)passwd, strlen(passwd),
|
||||
(const uint8_t *)salt_data,
|
||||
buf, sizeof(buf));
|
||||
/* The returned value is either buf[], or NULL on error */
|
||||
|
||||
return xstrdup(retval);
|
||||
}
|
||||
+53
-47
@@ -11,7 +11,7 @@
|
||||
|
||||
int64_t FAST_FUNC read_key(int fd, char *buffer, int timeout)
|
||||
{
|
||||
struct pollfd pfd;
|
||||
struct pollfd pfd[1];
|
||||
const char *seq;
|
||||
int n;
|
||||
|
||||
@@ -26,7 +26,7 @@ int64_t FAST_FUNC read_key(int fd, char *buffer, int timeout)
|
||||
'd' |0x80,KEYCODE_ALT_D ,
|
||||
/* lineedit mimics bash: Alt-f and Alt-b are forward/backward
|
||||
* word jumps. We cheat here and make them return ALT_LEFT/RIGHT
|
||||
* keycodes. This way, lineedit need no special code to handle them.
|
||||
* keycodes. This way, lineedit needs no special code to handle them.
|
||||
* If we'll need to distinguish them, introduce new ALT_F/B keycodes,
|
||||
* and update lineedit to react to them.
|
||||
*/
|
||||
@@ -40,12 +40,12 @@ int64_t FAST_FUNC read_key(int fd, char *buffer, int timeout)
|
||||
'O','F' |0x80,KEYCODE_END ,
|
||||
#if 0
|
||||
'O','P' |0x80,KEYCODE_FUN1 ,
|
||||
/* [ESC] ESC O [2] P - [Alt-][Shift-]F1 */
|
||||
/* ESC [ O 1 ; 2 P - Shift-F1 */
|
||||
/* ESC [ O 1 ; 3 P - Alt-F1 */
|
||||
/* ESC [ O 1 ; 4 P - Alt-Shift-F1 */
|
||||
/* ESC [ O 1 ; 5 P - Ctrl-F1 */
|
||||
/* ESC [ O 1 ; 6 P - Ctrl-Shift-F1 */
|
||||
// [ESC] ESC O [2] P - [Alt-][Shift-]F1
|
||||
// ESC [ O 1 ; 2 P - Shift-F1
|
||||
// ESC [ O 1 ; 3 P - Alt-F1
|
||||
// ESC [ O 1 ; 4 P - Alt-Shift-F1
|
||||
// ESC [ O 1 ; 5 P - Ctrl-F1
|
||||
// ESC [ O 1 ; 6 P - Ctrl-Shift-F1
|
||||
'O','Q' |0x80,KEYCODE_FUN2 ,
|
||||
'O','R' |0x80,KEYCODE_FUN3 ,
|
||||
'O','S' |0x80,KEYCODE_FUN4 ,
|
||||
@@ -54,29 +54,29 @@ int64_t FAST_FUNC read_key(int fd, char *buffer, int timeout)
|
||||
'[','B' |0x80,KEYCODE_DOWN ,
|
||||
'[','C' |0x80,KEYCODE_RIGHT ,
|
||||
'[','D' |0x80,KEYCODE_LEFT ,
|
||||
/* ESC [ 1 ; 2 x, where x = A/B/C/D: Shift-<arrow> */
|
||||
/* ESC [ 1 ; 3 x, where x = A/B/C/D: Alt-<arrow> - implemented below */
|
||||
/* ESC [ 1 ; 4 x, where x = A/B/C/D: Alt-Shift-<arrow> */
|
||||
/* ESC [ 1 ; 5 x, where x = A/B/C/D: Ctrl-<arrow> - implemented below */
|
||||
/* ESC [ 1 ; 6 x, where x = A/B/C/D: Ctrl-Shift-<arrow> */
|
||||
/* ESC [ 1 ; 7 x, where x = A/B/C/D: Ctrl-Alt-<arrow> */
|
||||
/* ESC [ 1 ; 8 x, where x = A/B/C/D: Ctrl-Alt-Shift-<arrow> */
|
||||
// ESC [ 1 ; 2 x, where x = A/B/C/D: Shift-<arrow>
|
||||
// ESC [ 1 ; 3 x, where x = A/B/C/D: Alt-<arrow> - implemented below
|
||||
// ESC [ 1 ; 4 x, where x = A/B/C/D: Alt-Shift-<arrow>
|
||||
// ESC [ 1 ; 5 x, where x = A/B/C/D: Ctrl-<arrow> - implemented below
|
||||
// ESC [ 1 ; 6 x, where x = A/B/C/D: Ctrl-Shift-<arrow>
|
||||
// ESC [ 1 ; 7 x, where x = A/B/C/D: Ctrl-Alt-<arrow>
|
||||
// ESC [ 1 ; 8 x, where x = A/B/C/D: Ctrl-Alt-Shift-<arrow>
|
||||
'[','H' |0x80,KEYCODE_HOME , /* xterm */
|
||||
'[','F' |0x80,KEYCODE_END , /* xterm */
|
||||
/* [ESC] ESC [ [2] H - [Alt-][Shift-]Home (End similarly?) */
|
||||
/* '[','Z' |0x80,KEYCODE_SHIFT_TAB, */
|
||||
// [ESC] ESC [ [2] H - [Alt-][Shift-]Home (End similarly?)
|
||||
// '[','Z' |0x80,KEYCODE_SHIFT_TAB,
|
||||
'[','1','~' |0x80,KEYCODE_HOME , /* vt100? linux vt? or what? */
|
||||
'[','2','~' |0x80,KEYCODE_INSERT ,
|
||||
/* ESC [ 2 ; 3 ~ - Alt-Insert */
|
||||
// ESC [ 2 ; 3 ~ - Alt-Insert
|
||||
'[','3','~' |0x80,KEYCODE_DELETE ,
|
||||
/* [ESC] ESC [ 3 [;2] ~ - [Alt-][Shift-]Delete */
|
||||
/* ESC [ 3 ; 3 ~ - Alt-Delete */
|
||||
/* ESC [ 3 ; 5 ~ - Ctrl-Delete */
|
||||
// [ESC] ESC [ 3 [;2] ~ - [Alt-][Shift-]Delete
|
||||
// ESC [ 3 ; 3 ~ - Alt-Delete
|
||||
// ESC [ 3 ; 5 ~ - Ctrl-Delete
|
||||
'[','4','~' |0x80,KEYCODE_END , /* vt100? linux vt? or what? */
|
||||
'[','5','~' |0x80,KEYCODE_PAGEUP ,
|
||||
/* ESC [ 5 ; 3 ~ - Alt-PgUp */
|
||||
/* ESC [ 5 ; 5 ~ - Ctrl-PgUp */
|
||||
/* ESC [ 5 ; 7 ~ - Ctrl-Alt-PgUp */
|
||||
// ESC [ 5 ; 3 ~ - Alt-PgUp
|
||||
// ESC [ 5 ; 5 ~ - Ctrl-PgUp
|
||||
// ESC [ 5 ; 7 ~ - Ctrl-Alt-PgUp
|
||||
'[','6','~' |0x80,KEYCODE_PAGEDOWN,
|
||||
'[','7','~' |0x80,KEYCODE_HOME , /* vt100? linux vt? or what? */
|
||||
'[','8','~' |0x80,KEYCODE_END , /* vt100? linux vt? or what? */
|
||||
@@ -86,7 +86,7 @@ int64_t FAST_FUNC read_key(int fd, char *buffer, int timeout)
|
||||
'[','1','3','~'|0x80,KEYCODE_FUN3 , /* old xterm... */
|
||||
'[','1','4','~'|0x80,KEYCODE_FUN4 , /* old xterm... */
|
||||
'[','1','5','~'|0x80,KEYCODE_FUN5 ,
|
||||
/* [ESC] ESC [ 1 5 [;2] ~ - [Alt-][Shift-]F5 */
|
||||
// [ESC] ESC [ 1 5 [;2] ~ - [Alt-][Shift-]F5
|
||||
'[','1','7','~'|0x80,KEYCODE_FUN6 ,
|
||||
'[','1','8','~'|0x80,KEYCODE_FUN7 ,
|
||||
'[','1','9','~'|0x80,KEYCODE_FUN8 ,
|
||||
@@ -94,26 +94,26 @@ int64_t FAST_FUNC read_key(int fd, char *buffer, int timeout)
|
||||
'[','2','1','~'|0x80,KEYCODE_FUN10 ,
|
||||
'[','2','3','~'|0x80,KEYCODE_FUN11 ,
|
||||
'[','2','4','~'|0x80,KEYCODE_FUN12 ,
|
||||
/* ESC [ 2 4 ; 2 ~ - Shift-F12 */
|
||||
/* ESC [ 2 4 ; 3 ~ - Alt-F12 */
|
||||
/* ESC [ 2 4 ; 4 ~ - Alt-Shift-F12 */
|
||||
/* ESC [ 2 4 ; 5 ~ - Ctrl-F12 */
|
||||
/* ESC [ 2 4 ; 6 ~ - Ctrl-Shift-F12 */
|
||||
// ESC [ 2 4 ; 2 ~ - Shift-F12
|
||||
// ESC [ 2 4 ; 3 ~ - Alt-F12
|
||||
// ESC [ 2 4 ; 4 ~ - Alt-Shift-F12
|
||||
// ESC [ 2 4 ; 5 ~ - Ctrl-F12
|
||||
// ESC [ 2 4 ; 6 ~ - Ctrl-Shift-F12
|
||||
#endif
|
||||
/* '[','1',';','5','A' |0x80,KEYCODE_CTRL_UP , - unused */
|
||||
/* '[','1',';','5','B' |0x80,KEYCODE_CTRL_DOWN , - unused */
|
||||
'[','1',';','5','C' |0x80,KEYCODE_CTRL_RIGHT,
|
||||
'[','1',';','5','D' |0x80,KEYCODE_CTRL_LEFT ,
|
||||
/* '[','1',';','3','A' |0x80,KEYCODE_ALT_UP , - unused */
|
||||
/* '[','1',';','3','B' |0x80,KEYCODE_ALT_DOWN , - unused */
|
||||
// '[','1',';','3','A' |0x80,KEYCODE_ALT_UP , - unused
|
||||
// '[','1',';','3','B' |0x80,KEYCODE_ALT_DOWN , - unused
|
||||
'[','1',';','3','C' |0x80,KEYCODE_ALT_RIGHT,
|
||||
'[','1',';','3','D' |0x80,KEYCODE_ALT_LEFT ,
|
||||
/* '[','3',';','3','~' |0x80,KEYCODE_ALT_DELETE, - unused */
|
||||
// '[','1',';','5','A' |0x80,KEYCODE_CTRL_UP , - unused
|
||||
// '[','1',';','5','B' |0x80,KEYCODE_CTRL_DOWN , - unused
|
||||
'[','1',';','5','C' |0x80,KEYCODE_CTRL_RIGHT,
|
||||
'[','1',';','5','D' |0x80,KEYCODE_CTRL_LEFT ,
|
||||
// '[','3',';','3','~' |0x80,KEYCODE_ALT_DELETE, - unused
|
||||
0
|
||||
};
|
||||
|
||||
pfd.fd = fd;
|
||||
pfd.events = POLLIN;
|
||||
pfd->fd = fd;
|
||||
pfd->events = POLLIN;
|
||||
|
||||
buffer++; /* saved chars counter is in buffer[-1] now */
|
||||
|
||||
@@ -121,12 +121,16 @@ int64_t FAST_FUNC read_key(int fd, char *buffer, int timeout)
|
||||
errno = 0;
|
||||
n = (unsigned char)buffer[-1];
|
||||
if (n == 0) {
|
||||
/* If no data, wait for input.
|
||||
* If requested, wait TIMEOUT ms. TIMEOUT = -1 is useful
|
||||
* if fd can be in non-blocking mode.
|
||||
*/
|
||||
/* No data. Wait for input. */
|
||||
|
||||
/* timeout == -2 means "do not poll". Else: */
|
||||
if (timeout >= -1) {
|
||||
n = poll(&pfd, 1, timeout);
|
||||
/* We must poll even if timeout == -1:
|
||||
* we want to be interrupted if signal arrives,
|
||||
* regardless of SA_RESTART-ness of that signal!
|
||||
*/
|
||||
/* test bb_got_signal, then poll(), atomically wrt signals */
|
||||
n = check_got_signal_and_poll(pfd, timeout);
|
||||
if (n < 0 && errno == EINTR)
|
||||
return n;
|
||||
if (n == 0) {
|
||||
@@ -135,6 +139,7 @@ int64_t FAST_FUNC read_key(int fd, char *buffer, int timeout)
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/* It is tempting to read more than one byte here,
|
||||
* but it breaks pasting. Example: at shell prompt,
|
||||
* user presses "c","a","t" and then pastes "\nline\n".
|
||||
@@ -173,7 +178,7 @@ int64_t FAST_FUNC read_key(int fd, char *buffer, int timeout)
|
||||
* so if we block for long it's not really an escape sequence.
|
||||
* Timeout is needed to reconnect escape sequences
|
||||
* split up by transmission over a serial console. */
|
||||
if (safe_poll(&pfd, 1, 50) == 0) {
|
||||
if (safe_poll(pfd, 1, 50) == 0) {
|
||||
/* No more data!
|
||||
* Array is sorted from shortest to longest,
|
||||
* we can't match anything later in array -
|
||||
@@ -200,7 +205,7 @@ int64_t FAST_FUNC read_key(int fd, char *buffer, int timeout)
|
||||
/* Forward to last char */
|
||||
while (!(*seq & 0x80))
|
||||
seq++;
|
||||
/* Skip it and the keycode which follows */
|
||||
/* Skip seq terminating byte, and KEYCODE_xyz byte that follows */
|
||||
seq += 2;
|
||||
break;
|
||||
}
|
||||
@@ -212,6 +217,7 @@ int64_t FAST_FUNC read_key(int fd, char *buffer, int timeout)
|
||||
* but we never read ahead that much,
|
||||
* and n == i here. */
|
||||
buffer[-1] = 0;
|
||||
/* Return KEYCODE_xyz (always negative) */
|
||||
return (signed char)seq[i+1];
|
||||
}
|
||||
i++;
|
||||
@@ -222,7 +228,7 @@ int64_t FAST_FUNC read_key(int fd, char *buffer, int timeout)
|
||||
* n = bytes read. Try to read more until we time out.
|
||||
*/
|
||||
while (n < KEYCODE_BUFFER_SIZE-1) { /* 1 for count byte at buffer[-1] */
|
||||
if (safe_poll(&pfd, 1, 50) == 0) {
|
||||
if (safe_poll(pfd, 1, 50) == 0) {
|
||||
/* No more data! */
|
||||
break;
|
||||
}
|
||||
|
||||
+16
-1
@@ -28,7 +28,8 @@ unsigned FAST_FUNC count_strstr(const char *str, const char *sub)
|
||||
|
||||
char* FAST_FUNC xmalloc_substitute_string(const char *src, int count, const char *sub, const char *repl)
|
||||
{
|
||||
char *buf, *dst, *end;
|
||||
char *buf, *dst;
|
||||
const char *end;
|
||||
size_t sub_len = strlen(sub);
|
||||
size_t repl_len = strlen(repl);
|
||||
|
||||
@@ -46,3 +47,17 @@ char* FAST_FUNC xmalloc_substitute_string(const char *src, int count, const char
|
||||
//dbg_msg("subst9:'%s'", buf);
|
||||
return buf;
|
||||
}
|
||||
|
||||
#if 0 /* inlined in libbb.h */
|
||||
/* Returns strlen as a bonus */
|
||||
size_t FAST_FUNC replace_char(char *str, char from, char to)
|
||||
{
|
||||
char *p = str;
|
||||
while (*p) {
|
||||
if (*p == from)
|
||||
*p = to;
|
||||
p++;
|
||||
}
|
||||
return p - str;
|
||||
}
|
||||
#endif
|
||||
+21
-9
@@ -24,13 +24,6 @@ int FAST_FUNC sigaction_set(int signum, const struct sigaction *act)
|
||||
return sigaction(signum, act, NULL);
|
||||
}
|
||||
|
||||
int FAST_FUNC sigprocmask_allsigs(int how)
|
||||
{
|
||||
sigset_t set;
|
||||
sigfillset(&set);
|
||||
return sigprocmask(how, &set, NULL);
|
||||
}
|
||||
|
||||
int FAST_FUNC sigprocmask2(int how, sigset_t *set)
|
||||
{
|
||||
// Grr... gcc 8.1.1:
|
||||
@@ -41,6 +34,25 @@ int FAST_FUNC sigprocmask2(int how, sigset_t *set)
|
||||
return sigprocmask(how, set, oset);
|
||||
}
|
||||
|
||||
int FAST_FUNC sigprocmask_allsigs(int how)
|
||||
{
|
||||
sigset_t set;
|
||||
sigfillset(&set);
|
||||
return sigprocmask2(how, &set);
|
||||
}
|
||||
|
||||
int FAST_FUNC sigblockall(sigset_t *set)
|
||||
{
|
||||
#if 0 /* nope. set can be NULL */
|
||||
sigfillset(set);
|
||||
return sigprocmask2(SIG_SETMASK, set);
|
||||
#else
|
||||
sigset_t mask;
|
||||
sigfillset(&mask);
|
||||
return sigprocmask(SIG_SETMASK, &mask, set);
|
||||
#endif
|
||||
}
|
||||
|
||||
void FAST_FUNC bb_signals(int sigs, void (*f)(int))
|
||||
{
|
||||
int sig_no = 0;
|
||||
@@ -82,7 +94,7 @@ void FAST_FUNC sig_block(int sig)
|
||||
sigset_t ss;
|
||||
sigemptyset(&ss);
|
||||
sigaddset(&ss, sig);
|
||||
sigprocmask(SIG_BLOCK, &ss, NULL);
|
||||
sigprocmask2(SIG_BLOCK, &ss);
|
||||
}
|
||||
|
||||
void FAST_FUNC sig_unblock(int sig)
|
||||
@@ -90,7 +102,7 @@ void FAST_FUNC sig_unblock(int sig)
|
||||
sigset_t ss;
|
||||
sigemptyset(&ss);
|
||||
sigaddset(&ss, sig);
|
||||
sigprocmask(SIG_UNBLOCK, &ss, NULL);
|
||||
sigprocmask2(SIG_UNBLOCK, &ss);
|
||||
}
|
||||
|
||||
void FAST_FUNC wait_for_any_sig(void)
|
||||
|
||||
+2
-2
@@ -19,10 +19,10 @@ char* FAST_FUNC strrstr(const char *haystack, const char *needle)
|
||||
if (!needle[0])
|
||||
return (char*)haystack + strlen(haystack);
|
||||
while (1) {
|
||||
char *p = strstr(haystack, needle);
|
||||
const char *p = strstr(haystack, needle);
|
||||
if (!p)
|
||||
return r;
|
||||
r = p;
|
||||
r = (char *)p;
|
||||
haystack = p + 1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,29 +113,8 @@ void FAST_FUNC xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen)
|
||||
}
|
||||
}
|
||||
|
||||
/* Return port number for a service.
|
||||
* If "port" is a number use it as the port.
|
||||
* If "port" is a name it is looked up in /etc/services.
|
||||
* if NULL, return default_port
|
||||
*/
|
||||
unsigned FAST_FUNC bb_lookup_port(const char *port, const char *protocol, unsigned port_nr)
|
||||
{
|
||||
if (port) {
|
||||
port_nr = bb_strtou(port, NULL, 10);
|
||||
if (errno || port_nr > 65535) {
|
||||
struct servent *tserv = getservbyname(port, protocol);
|
||||
if (!tserv)
|
||||
bb_error_msg_and_die("bad port '%s'", port);
|
||||
port_nr = ntohs(tserv->s_port);
|
||||
}
|
||||
}
|
||||
return (uint16_t)port_nr;
|
||||
}
|
||||
|
||||
|
||||
/* "New" networking API */
|
||||
|
||||
|
||||
int FAST_FUNC get_nport(const struct sockaddr *sa)
|
||||
{
|
||||
#if ENABLE_FEATURE_IPV6
|
||||
|
||||
@@ -176,47 +176,6 @@ char* FAST_FUNC hex2bin(char *dst, const char *str, int count)
|
||||
return dst;
|
||||
}
|
||||
|
||||
/* Return how long the file at fd is, if there's any way to determine it. */
|
||||
#ifdef UNUSED
|
||||
off_t FAST_FUNC fdlength(int fd)
|
||||
{
|
||||
off_t bottom = 0, top = 0, pos;
|
||||
long size;
|
||||
|
||||
// If the ioctl works for this, return it.
|
||||
|
||||
if (ioctl(fd, BLKGETSIZE, &size) >= 0) return size*512;
|
||||
|
||||
// FIXME: explain why lseek(SEEK_END) is not used here!
|
||||
|
||||
// If not, do a binary search for the last location we can read. (Some
|
||||
// block devices don't do BLKGETSIZE right.)
|
||||
|
||||
do {
|
||||
char temp;
|
||||
|
||||
pos = bottom + (top - bottom) / 2;
|
||||
|
||||
// If we can read from the current location, it's bigger.
|
||||
|
||||
if (lseek(fd, pos, SEEK_SET)>=0 && safe_read(fd, &temp, 1)==1) {
|
||||
if (bottom == top) bottom = top = (top+1) * 2;
|
||||
else bottom = pos;
|
||||
|
||||
// If we can't, it's smaller.
|
||||
} else {
|
||||
if (bottom == top) {
|
||||
if (!top) return 0;
|
||||
bottom = top/2;
|
||||
}
|
||||
else top = pos;
|
||||
}
|
||||
} while (bottom + 1 != top);
|
||||
|
||||
return pos + 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
int FAST_FUNC bb_putchar_stderr(char ch)
|
||||
{
|
||||
return write(STDERR_FILENO, &ch, 1);
|
||||
|
||||
+42
-4
@@ -349,6 +349,22 @@ char* FAST_FUNC xasprintf(const char *format, ...)
|
||||
return string_ptr;
|
||||
}
|
||||
|
||||
char* FAST_FUNC xasprintf_and_free(char *allocated, const char *format, ...)
|
||||
{
|
||||
va_list p;
|
||||
int r;
|
||||
char *string_ptr;
|
||||
|
||||
va_start(p, format);
|
||||
r = vasprintf(&string_ptr, format, p);
|
||||
va_end(p);
|
||||
|
||||
if (r < 0)
|
||||
bb_die_memory_exhausted();
|
||||
free(allocated);
|
||||
return string_ptr;
|
||||
}
|
||||
|
||||
void FAST_FUNC xsetenv(const char *key, const char *value)
|
||||
{
|
||||
if (setenv(key, value, 1))
|
||||
@@ -363,7 +379,7 @@ void FAST_FUNC bb_unsetenv(const char *var)
|
||||
char onstack[128 - 16]; /* smaller stack setup code on x86 */
|
||||
char *tp;
|
||||
|
||||
tp = strchr(var, '=');
|
||||
tp = (char*)strchr(var, '=');
|
||||
if (tp) {
|
||||
/* In case var was putenv'ed, we can't replace '='
|
||||
* with NUL and unsetenv(var) - it won't work,
|
||||
@@ -642,14 +658,14 @@ void FAST_FUNC generate_uuid(uint8_t *buf)
|
||||
* uint32_t time_low (big endian)
|
||||
* uint16_t time_mid (big endian)
|
||||
* uint16_t time_hi_and_version (big endian)
|
||||
* version is a 4-bit field:
|
||||
* version is a 4-bit field (most significant 4 bits):
|
||||
* 1 Time-based
|
||||
* 2 DCE Security, with embedded POSIX UIDs
|
||||
* 3 Name-based (MD5)
|
||||
* 4 Randomly generated
|
||||
* 5 Name-based (SHA-1)
|
||||
* uint16_t clk_seq_and_variant (big endian)
|
||||
* variant is a 3-bit field:
|
||||
* variant is a 3-bit field (most significant 3 bits):
|
||||
* 0xx Reserved, NCS backward compatibility
|
||||
* 10x The variant specified in rfc4122
|
||||
* 110 Reserved, Microsoft backward compatibility
|
||||
@@ -681,10 +697,23 @@ void FAST_FUNC generate_uuid(uint8_t *buf)
|
||||
|
||||
/* version = 4 */
|
||||
buf[4 + 2 ] = (buf[4 + 2 ] & 0x0f) | 0x40;
|
||||
/* variant = 10x */
|
||||
/* variant = 10x binary (uppermost 3 bits) */
|
||||
buf[4 + 2 + 2] = (buf[4 + 2 + 2] & 0x3f) | 0x80;
|
||||
}
|
||||
|
||||
void FAST_FUNC format_uuid_DCE_37_chars(char *dst37, const uint8_t *buf)
|
||||
{
|
||||
/* 37 = 16*2 hexdigits + 4 dashes + 1 NUL */
|
||||
sprintf(dst37,
|
||||
"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
|
||||
buf[0], buf[1], buf[2], buf[3],
|
||||
buf[4], buf[5],
|
||||
buf[6], buf[7],
|
||||
buf[8], buf[9],
|
||||
buf[10], buf[11], buf[12], buf[13], buf[14], buf[15]
|
||||
);
|
||||
}
|
||||
|
||||
#if BB_MMU
|
||||
pid_t FAST_FUNC xfork(void)
|
||||
{
|
||||
@@ -696,6 +725,14 @@ pid_t FAST_FUNC xfork(void)
|
||||
}
|
||||
#endif
|
||||
|
||||
#if 0
|
||||
/* DO NOT DO THIS. This can't be a function.
|
||||
* It works on some arches (x86) but fails on others (ppc64le: SEGV).
|
||||
* The reason is: the child returns from this function
|
||||
* and likely pops up the stack in an arch-dependent way.
|
||||
* When child eventually exits or execs, parent "reappear"
|
||||
* in the now-unwound stack (!) and the behavior is undefined.
|
||||
*/
|
||||
void FAST_FUNC xvfork_parent_waits_and_exits(void)
|
||||
{
|
||||
pid_t pid;
|
||||
@@ -711,6 +748,7 @@ void FAST_FUNC xvfork_parent_waits_and_exits(void)
|
||||
}
|
||||
/* Child continues */
|
||||
}
|
||||
#endif
|
||||
|
||||
// Useful when we do know that pid is valid, and we just want to wait
|
||||
// for it to exit. Not existing pid is fatal. waitpid() status is not returned.
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
# Makefile for busybox
|
||||
#
|
||||
# Copyright (C) 2025 by Denys Vlasenko <vda.linux@googlemail.com>
|
||||
#
|
||||
# Licensed under GPLv2, see file LICENSE in this source tree.
|
||||
|
||||
lib-y:=
|
||||
|
||||
INSERT
|
||||
@@ -0,0 +1,196 @@
|
||||
Optimal yescrypt configuration.
|
||||
|
||||
yescrypt is very flexible, but configuring it optimally is complicated.
|
||||
Here are some guidelines to simplify near-optimal configuration. We
|
||||
start by listing the parameters and their typical values, and then give
|
||||
currently recommended parameter sets by use case.
|
||||
|
||||
|
||||
Parameters and their typical values.
|
||||
|
||||
Set flags (yescrypt flavor) to YESCRYPT_DEFAULTS to use the currently
|
||||
recommended flavor. (Other flags values exist for compatibility and for
|
||||
specialized cases where you think you know what you're doing.)
|
||||
|
||||
Set N (block count) based on target memory usage and running time, as
|
||||
well as on the value of r (block size in 128 byte units). N must be a
|
||||
power of two.
|
||||
|
||||
Set r (block size) to 8 (so that N is in KiB, which is convenient) or to
|
||||
another small value (if more optimal or for fine-tuning of the total
|
||||
size and/or running time). Reasonable values for r are from 8 to 96.
|
||||
|
||||
Set p (parallelism) to 1 meaning no thread-level parallelism within one
|
||||
computation of yescrypt. (Use of thread-level parallelism within
|
||||
yescrypt makes sense for ROM initialization and for key derivation at
|
||||
high memory usage, but usually not for password hashing where
|
||||
parallelism is available through concurrent authentication attempts.
|
||||
Don't use p > 1 unnecessarily.)
|
||||
|
||||
Set t (time) to 0 to use the optimal running time for a given memory
|
||||
usage. This will allow you to maximize the memory usage (the value of
|
||||
N*r) while staying within your running time constraints. (Non-zero t
|
||||
makes sense in specialized cases where you can't afford higher memory
|
||||
usage but can afford more time.)
|
||||
|
||||
Set g (upgrades) to 0 because there have been no hash upgrades yet.
|
||||
|
||||
Set NROM (block count of ROM) to 0 unless you use a ROM (see below).
|
||||
NROM must be a power of two.
|
||||
|
||||
|
||||
Password hashing for user authentication, no ROM.
|
||||
|
||||
Small and fast (memory usage 2 MiB, performance like bcrypt cost 2^5 -
|
||||
latency 2-3 ms and throughput 10,000+ per second on a 16-core server):
|
||||
|
||||
flags = YESCRYPT_DEFAULTS, N = 2048, r = 8, p = 1, t = 0, g = 0, NROM = 0
|
||||
|
||||
Large and slow (memory usage 16 MiB, performance like bcrypt cost 2^8 -
|
||||
latency 10-30 ms and throughput 1000+ per second on a 16-core server):
|
||||
|
||||
flags = YESCRYPT_DEFAULTS, N = 4096, r = 32, p = 1, t = 0, g = 0, NROM = 0
|
||||
|
||||
Of course, even heavier and slower settings are possible, if affordable.
|
||||
Simply double the value of N as many times as needed. Since N must be a
|
||||
power of two, you may use r (in the range of 8 to 32) or/and t (in the
|
||||
range of 0 to 2) for fine-tuning the running time, but first bring N to
|
||||
the maximum you can afford. If this feels too complicated, just use one
|
||||
of the two parameter sets given above (preferably the second) as-is.
|
||||
|
||||
|
||||
Password hashing for user authentication, with ROM.
|
||||
|
||||
It's similar to the above, except that you need to adjust r, set NROM,
|
||||
and initialize the ROM.
|
||||
|
||||
First decide on a ROM size, such as making it a large portion of your
|
||||
dedicated authentication servers' RAM sizes. Since NROM (block count)
|
||||
must be a power of two, you might need to choose r (block size) based on
|
||||
how your desired ROM size corresponds to a power of two. Also tuning
|
||||
for performance on current hardware, you'll likely end up with r in the
|
||||
range from slightly below 16 to 32. For example, to use 15/16 of a
|
||||
server's 256 GiB RAM as ROM (thus, making it 240 GiB), you could use
|
||||
r=15 or r=30. To use 23/24 of a server's 384 GiB RAM as ROM (thus,
|
||||
making it 368 GiB), you'd use r=23. Then set NROM to your desired ROM
|
||||
size in KiB divided by 128*r. Note that these examples might (or might
|
||||
not) be too extreme, leaving little memory for the rest of the system.
|
||||
You could as well opt for 7/8 with r=14 or 11/12 with r=11 or r=22.
|
||||
|
||||
Note that higher r may make placing of ROM in e.g. NVMe flash memory
|
||||
instead of in RAM more reasonable (or less unreasonable) than it would
|
||||
have been with a lower r. If this is a concern as it relates to
|
||||
possible attacks and you do not intend to ever do it defensively, you
|
||||
might want to keep r lower (e.g., prefer r=15 over r=30 in the example
|
||||
above, even if 30 performs slightly faster).
|
||||
|
||||
Your adjustments to r, if you deviate from powers of two, will also
|
||||
result in weirder memory usage per hash. Like 1.75 MiB at r=14 instead
|
||||
of 2 MiB at r=8 that you would have used without a ROM. That's OK.
|
||||
|
||||
For ROM initialization, which you do with yescrypt_init_shared(), use
|
||||
the same r and NROM that you'd later use for password hashing, choose p
|
||||
based on your servers' physical and/or logical CPU count (maybe
|
||||
considering eventual upgrades as you won't be able to change this later,
|
||||
but without going unnecessarily high - e.g., p=28, p=56, or p=112 make
|
||||
sense on servers that currently have 28 physical / 56 logical CPUs), and
|
||||
set the rest of the parameters to:
|
||||
|
||||
flags = YESCRYPT_DEFAULTS, N = 0, t = 0, g = 0
|
||||
|
||||
N is set to 0 because it isn't relevant during ROM initialization (you
|
||||
can use different values of N for hashing passwords with the same ROM).
|
||||
|
||||
To keep the ROM in e.g. SysV shared memory and reuse it across your
|
||||
authentication service restarts, you'd need to allocate the memory and
|
||||
set the flags to "YESCRYPT_DEFAULTS | YESCRYPT_SHARED_PREALLOCATED".
|
||||
|
||||
For actual password hashing, you'd use your chosen values for N, r,
|
||||
NROM, and set the rest of the parameters to:
|
||||
|
||||
flags = YESCRYPT_DEFAULTS, p = 1, t = 0, g = 0
|
||||
|
||||
Note that although you'd use a large p for ROM initialization, you
|
||||
should use p=1 for actual password hashing like you would without a ROM.
|
||||
|
||||
Do not forget to pass the ROM into the actual password hashing (and keep
|
||||
r and NROM set accordingly).
|
||||
|
||||
Since N must be a power of two and r is dependent on ROM size, you may
|
||||
use t (in the range of 0 to 2) for fine-tuning the running time, but
|
||||
first bring N to the maximum you can afford.
|
||||
|
||||
If this feels too complicated, or even if it doesn't, please consider
|
||||
engaging Openwall for your yescrypt deployment. We'd be happy to help.
|
||||
|
||||
|
||||
Password-based key derivation.
|
||||
|
||||
(Or rather passphrase-based.)
|
||||
|
||||
Use settings similar to those for password hashing without a ROM, but
|
||||
adjusted for higher memory usage and running time, and optionally with
|
||||
thread-level parallelism.
|
||||
|
||||
Small and fast (memory usage 128 MiB, running time under 100 ms on a
|
||||
fast desktop):
|
||||
|
||||
flags = YESCRYPT_DEFAULTS, N = 32768, r = 32, p = 1, t = 0, g = 0, NROM = 0
|
||||
|
||||
Large and fast (memory usage 1 GiB, running time under 200 ms on a fast
|
||||
quad-core desktop not including memory allocation overhead, under 250 ms
|
||||
with the overhead included), but requires build with OpenMP support (or
|
||||
otherwise will run as slow as yet be weaker than its p=1 alternative):
|
||||
|
||||
flags = YESCRYPT_DEFAULTS, N = 262144, r = 32, p = 4, t = 0, g = 0, NROM = 0
|
||||
|
||||
Large and slower (memory usage 1 GiB, running time under 300 ms on a
|
||||
fast quad-core desktop not including memory allocation overhead, under
|
||||
350 ms with the overhead included), also requires build with OpenMP
|
||||
support (or otherwise will run slower than the p=1 alternative below):
|
||||
|
||||
flags = YESCRYPT_DEFAULTS, N = 262144, r = 32, p = 4, t = 2, g = 0, NROM = 0
|
||||
|
||||
Large and slow (memory usage 1 GiB, running time under 600 ms on a fast
|
||||
desktop not including memory allocation overhead, under 650 ms with the
|
||||
overhead included):
|
||||
|
||||
flags = YESCRYPT_DEFAULTS, N = 262144, r = 32, p = 1, t = 0, g = 0, NROM = 0
|
||||
|
||||
Just like with password hashing, even heavier and slower settings are
|
||||
possible, if affordable, and you achieve them by adjusting N, r, t in
|
||||
the same way and in the same preferred ranges (please see the section on
|
||||
password hashing without a ROM, above). Unlike with password hashing,
|
||||
it makes some sense to go above t=2 if you expect that your users might
|
||||
not be able to afford more memory but can afford more time. However,
|
||||
increasing the memory usage provides better protection, and we don't
|
||||
recommend forcing your users to wait for more than 1 second as they
|
||||
could as well type more characters in that time. If this feels too
|
||||
complicated, just use one of the above parameter sets as-is.
|
||||
|
||||
|
||||
Amortization of memory allocation overhead.
|
||||
|
||||
It takes a significant fraction of yescrypt's total running time to
|
||||
allocate memory from the operating system, especially considering that
|
||||
the kernel zeroizes the memory before handing it over to your program.
|
||||
|
||||
Unless you naturally need to compute yescrypt just once per process, you
|
||||
may achieve greater efficiency by fully using advanced yescrypt APIs
|
||||
that let you preserve and reuse the memory allocation across yescrypt
|
||||
invocations. This is done by reusing the structure pointed to by the
|
||||
"yescrypt_local_t *local" argument of yescrypt_r() or yescrypt_kdf()
|
||||
without calling yescrypt_free_local() inbetween the repeated invocations
|
||||
of yescrypt.
|
||||
|
||||
|
||||
YESCRYPT_DEFAULTS macro.
|
||||
|
||||
Please note that the value of the YESCRYPT_DEFAULTS macro might change
|
||||
later, so if you use the macro like it's recommended here then for
|
||||
results reproducible across versions you might need to store its value
|
||||
somewhere along with the hashes or the encrypted data.
|
||||
|
||||
If you use yescrypt's standard hash string encoding, then yescrypt
|
||||
already encodes and decodes this value for you, so you don't need to
|
||||
worry about this.
|
||||
@@ -0,0 +1,4 @@
|
||||
The yescrypt code in this directory is adapted from libxcrypt-4.4.38
|
||||
with minimal edits, hopefully making it easier to track
|
||||
backports by resetting the tree to the commit which created this file,
|
||||
then comparing changes in upstream libxcrypt to the tree.
|
||||
@@ -0,0 +1,91 @@
|
||||
/*-
|
||||
* Copyright 2005-2016 Colin Percival
|
||||
* Copyright 2016-2018,2021 Alexander Peslyak
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, c, buf, dkLen):
|
||||
* Compute PBKDF2(passwd, salt, c, dkLen) using HMAC-SHA256 as the PRF, and
|
||||
* write the output to buf. The value dkLen must be at most 32 * (2^32 - 1).
|
||||
*/
|
||||
static void
|
||||
PBKDF2_SHA256(const uint8_t *passwd, size_t passwdlen,
|
||||
const uint8_t *salt, size_t saltlen,
|
||||
uint64_t c, uint8_t *buf, size_t dkLen)
|
||||
{
|
||||
hmac_ctx_t Phctx, PShctx;
|
||||
uint32_t i;
|
||||
|
||||
/* Compute HMAC state after processing P. */
|
||||
hmac_begin(&Phctx, passwd, passwdlen, sha256_begin);
|
||||
|
||||
/* Compute HMAC state after processing P and S. */
|
||||
PShctx = Phctx;
|
||||
hmac_hash(&PShctx, salt, saltlen);
|
||||
|
||||
/* Iterate through the blocks. */
|
||||
for (i = 0; dkLen != 0; ) {
|
||||
long U[32 / sizeof(long)];
|
||||
long T[32 / sizeof(long)];
|
||||
// Do not make these ^^ uint64_t[]. Keep them long[].
|
||||
// Even though the XORing loop below is optimized out,
|
||||
// gcc is not smart enough to realize that 64-bit alignment of the stack
|
||||
// is no longer useful, and generates ~50 more bytes of code on i386...
|
||||
uint32_t ivec;
|
||||
size_t clen;
|
||||
int k;
|
||||
|
||||
/* Generate INT(i). */
|
||||
i++;
|
||||
ivec = SWAP_BE32(i);
|
||||
|
||||
/* Compute U_1 = PRF(P, S || INT(i)). */
|
||||
hmac_peek_hash(&PShctx, (void*)T, &ivec, 4, NULL);
|
||||
//TODO: the above is a vararg function, might incur some ABI pain
|
||||
//does libbb need a non-vararg version with just one (buf,len)?
|
||||
|
||||
if (c > 1) {
|
||||
//in yescrypt, c is always 1, so this if() branch is optimized out
|
||||
uint64_t j;
|
||||
/* T_i = U_1 ... */
|
||||
memcpy(U, T, 32);
|
||||
for (j = 2; j <= c; j++) {
|
||||
/* Compute U_j. */
|
||||
hmac_peek_hash(&Phctx, (void*)U, U, 32, NULL);
|
||||
/* ... xor U_j ... */
|
||||
for (k = 0; k < 32 / sizeof(long); k++)
|
||||
T[k] ^= U[k];
|
||||
//TODO: xorbuf32_aligned_long(T, U);
|
||||
}
|
||||
}
|
||||
|
||||
/* Copy as many bytes as necessary into buf. */
|
||||
clen = dkLen;
|
||||
if (clen > 32)
|
||||
clen = 32;
|
||||
buf = mempcpy(buf, T, clen);
|
||||
dkLen -= clen;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,408 @@
|
||||
/*-
|
||||
* Copyright 2013-2018 Alexander Peslyak
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#if RESTRICTED_PARAMS
|
||||
|
||||
#define decode64_uint32(dst, src, min) \
|
||||
({ \
|
||||
uint32_t d32 = a2i64(*(src)); \
|
||||
if (d32 > 47) \
|
||||
goto fail; \
|
||||
*(dst) = d32 + (min); \
|
||||
++src; \
|
||||
})
|
||||
#define test_decode64_uint32() ((void)0)
|
||||
#define FULL_PARAMS(...)
|
||||
|
||||
#else
|
||||
|
||||
#define FULL_PARAMS(...) __VA_ARGS__
|
||||
|
||||
/* Not inlining:
|
||||
* de/encode64 functions are only used to read
|
||||
* yescrypt_params_t field, and convert salt to binary -
|
||||
* both of these are negligible compared to main hashing operation
|
||||
*/
|
||||
static NOINLINE const uint8_t *decode64_uint32(
|
||||
uint32_t *dst,
|
||||
const uint8_t *src, uint32_t val)
|
||||
{
|
||||
uint32_t start = 0, end = 47, bits = 0;
|
||||
uint32_t c;
|
||||
|
||||
if (!src) /* previous decode failed already? */
|
||||
goto fail;
|
||||
|
||||
c = a2i64(*src++);
|
||||
if (c > 63)
|
||||
goto fail;
|
||||
|
||||
// The encoding of number N:
|
||||
// start = 0 end = 47
|
||||
// If N < 48, it is encoded verbatim, else
|
||||
// N -= 48
|
||||
// start = end+1 = 48
|
||||
// end += (64-end)/2 = 55
|
||||
// If N < (end+1-start)<<6 = 8<<6, it is encoded as 48+(N>>6)|low6bits (that is, 48...55|<6bit>), else
|
||||
// N -= 8<<6
|
||||
// start = end+1 = 56
|
||||
// end += (64-end)/2 = 59
|
||||
// If N < (end+1-start)<<2*6 = 4<<12, it is encoded as 56+(N>>2*6)|low12bits (that is, 56...59|<6bit>|<6bit>), else
|
||||
// ...same for 60..61|<6bit>|<6bit>|<6bit>
|
||||
// .......same for 62|<6bit>|<6bit>|<6bit>|<6bit>
|
||||
// .......same for 63|<6bit>|<6bit>|<6bit>|<6bit>|<6bit>
|
||||
dbg_dec64("c:%d val:0x%08x", (int)c, (unsigned)val);
|
||||
while (c > end) {
|
||||
dbg_dec64("c:%d > end:%d", (int)c, (int)end);
|
||||
val += (end + 1 - start) << bits;
|
||||
dbg_dec64("val+=0x%08x", (int)((end + 1 - start) << bits));
|
||||
dbg_dec64(" val:0x%08x", (unsigned)val);
|
||||
start = end + 1;
|
||||
end += (64 - end) / 2;
|
||||
bits += 6;
|
||||
dbg_dec64("start=%d", (int)start);
|
||||
dbg_dec64("end=%d", (int)end);
|
||||
dbg_dec64("bits=%d", (int)bits);
|
||||
}
|
||||
|
||||
val += (c - start) << bits;
|
||||
dbg_dec64("final val+=0x%08x", (int)((c - start) << bits));
|
||||
dbg_dec64(" val:0x%08x", (unsigned)val);
|
||||
|
||||
while (bits != 0) {
|
||||
c = a2i64(*src++);
|
||||
if (c > 63)
|
||||
goto fail;
|
||||
bits -= 6;
|
||||
val += c << bits;
|
||||
dbg_dec64("low bits val+=0x%08x", (int)(c << bits));
|
||||
dbg_dec64(" val:0x%08x", (unsigned)val);
|
||||
}
|
||||
ret:
|
||||
*dst = val;
|
||||
return src;
|
||||
fail:
|
||||
val = 0;
|
||||
src = NULL;
|
||||
goto ret;
|
||||
}
|
||||
|
||||
#if TEST_DECODE64
|
||||
static void test_decode64_uint32(void)
|
||||
{
|
||||
const uint8_t *src, *end;
|
||||
uint32_t u32;
|
||||
int a = 48;
|
||||
int b = 8<<6; // 0x0200
|
||||
int c = 4<<12; // 0x04000
|
||||
int d = 2<<18; // 0x080000
|
||||
int e = 1<<24; // 0x1000000
|
||||
|
||||
src = (void*)"wzzz";
|
||||
end = decode64_uint32(&u32, src, 0);
|
||||
if (u32 != 0x0003ffff+c+b+a) bb_error_msg_and_die("Incorrect decode '%s':0x%08x", src, (unsigned)u32);
|
||||
if (end != src + 4) bb_error_msg_and_die("Incorrect decode '%s': %p end:%p", src, src, end);
|
||||
src = (void*)"xzzz";
|
||||
end = decode64_uint32(&u32, src, 0);
|
||||
if (u32 != 0x0007ffff+c+b+a) bb_error_msg_and_die("Incorrect decode '%s':0x%08x", src, (unsigned)u32);
|
||||
if (end != src + 4) bb_error_msg_and_die("Incorrect decode '%s': %p end:%p", src, src, end);
|
||||
// Note how the last representable "x---" encoding, 0x7ffff, is exactly d-1!
|
||||
// And if we now increment it, we get:
|
||||
src = (void*)"y....";
|
||||
end = decode64_uint32(&u32, src, 0);
|
||||
if (u32 != 0x00000000+d+c+b+a) bb_error_msg_and_die("Incorrect decode '%s':0x%08x", src, (unsigned)u32);
|
||||
if (end != src + 5) bb_error_msg_and_die("Incorrect decode '%s': %p end:%p", src, src, end);
|
||||
src = (void*)"yzzzz";
|
||||
end = decode64_uint32(&u32, src, 0);
|
||||
if (u32 != 0x00ffffff+d+c+b+a) bb_error_msg_and_die("Incorrect decode '%s':0x%08x", src, (unsigned)u32);
|
||||
if (end != src + 5) bb_error_msg_and_die("Incorrect decode '%s': %p end:%p", src, src, end);
|
||||
|
||||
src = (void*)"zzzzzz";
|
||||
end = decode64_uint32(&u32, src, 0);
|
||||
if (u32 != 0x3fffffff+e+d+c+b+a) bb_error_msg_and_die("Incorrect decode '%s':0x%08x", src, (unsigned)u32);
|
||||
if (end != src + 6) bb_error_msg_and_die("Incorrect decode '%s': %p end:%p", src, src, end);
|
||||
|
||||
bb_error_msg("test_decode64_uint32() OK");
|
||||
}
|
||||
#else
|
||||
# define test_decode64_uint32() ((void)0)
|
||||
#endif
|
||||
|
||||
#endif /* !RESTRICTED_PARAMS */
|
||||
|
||||
#if 1
|
||||
static const uint8_t *decode64(
|
||||
uint8_t *dst, size_t *dstlen,
|
||||
const uint8_t *src)
|
||||
{
|
||||
unsigned dstpos = 0;
|
||||
|
||||
dbg_dec64("src:'%s'", src);
|
||||
for (;;) {
|
||||
uint32_t c, value = 0;
|
||||
int bits = 0;
|
||||
while (*src != '\0' && *src != '$') {
|
||||
c = a2i64(*src);
|
||||
if (c > 63) { /* bad ascii64 char, stop decoding at it */
|
||||
break;
|
||||
}
|
||||
src++;
|
||||
value |= c << bits;
|
||||
bits += 6;
|
||||
if (bits == 24) /* got 4 chars */
|
||||
goto store;
|
||||
}
|
||||
/* we read entire src, or met a non-ascii64 char (such as "$") */
|
||||
if (bits == 0)
|
||||
break;
|
||||
/* else: we got last, partial bit block - store it */
|
||||
store:
|
||||
dbg_dec64(" storing bits:%d dstpos:%u v:%08x", bits, dstpos, (int)SWAP_BE32(value)); //BE to see lsb first
|
||||
for (;;) {
|
||||
if ((*src == '\0' || *src == '$')
|
||||
&& value == 0 && bits < 8
|
||||
) {
|
||||
/* Example: mkpasswd PWD '$y$j9T$123':
|
||||
* the "123" is bits:18 value:03,51,00
|
||||
* is considered to be 2 bytes, not 3!
|
||||
*
|
||||
* '$y$j9T$zzz' in upstream fails outright (3rd byte isn't zero).
|
||||
* IOW: for upstream, validity of salt depends on VALUE,
|
||||
* not just size of salt. Which is a bug.
|
||||
* The '$y$j9T$zzz.' salt is the same
|
||||
* (it adds 6 zero msbits) but upstream works with it,
|
||||
* thus '$y$j9T$zzz' should work too and give the same result.
|
||||
*/
|
||||
goto end;
|
||||
}
|
||||
if (dstpos >= *dstlen) {
|
||||
dbg_dec64(" ERR: bits:%d dstpos:%u dst[] is too small", bits, dstpos);
|
||||
goto fail;
|
||||
}
|
||||
*dst++ = value;
|
||||
dstpos++;
|
||||
value >>= 8;
|
||||
bits -= 8;
|
||||
if (bits <= 0) /* can get negative, if we e.g. had 6 bits */
|
||||
break;
|
||||
}
|
||||
if (*src == '\0' || *src == '$')
|
||||
break;
|
||||
}
|
||||
end:
|
||||
*dstlen = dstpos;
|
||||
dbg_dec64("dec64: OK, dst[%d]", (int)dstpos);
|
||||
return src;
|
||||
fail:
|
||||
/* *dstlen = 0; - not needed, caller detects error by seeing NULL */
|
||||
return NULL;
|
||||
}
|
||||
#else
|
||||
/* Buggy (and larger) original code */
|
||||
static const uint8_t *decode64(
|
||||
uint8_t *dst, size_t *dstlen,
|
||||
const uint8_t *src, size_t srclen)
|
||||
{
|
||||
size_t dstpos = 0;
|
||||
|
||||
while (dstpos <= *dstlen && srclen) {
|
||||
uint32_t value = 0, bits = 0;
|
||||
while (srclen--) {
|
||||
uint32_t c = a2i64(*src);
|
||||
if (c > 63) {
|
||||
srclen = 0;
|
||||
break;
|
||||
}
|
||||
src++;
|
||||
value |= c << bits;
|
||||
bits += 6;
|
||||
if (bits >= 24)
|
||||
break;
|
||||
}
|
||||
if (!bits)
|
||||
break;
|
||||
if (bits < 12) /* must have at least one full byte */
|
||||
goto fail;
|
||||
dbg_dec64(" storing bits:%d v:%08x", (int)bits, (int)SWAP_BE32(value)); //BE to see lsb first
|
||||
while (dstpos++ < *dstlen) {
|
||||
*dst++ = value;
|
||||
value >>= 8;
|
||||
bits -= 8;
|
||||
if (bits < 8) { /* 2 or 4 */
|
||||
if (value) /* must be 0 */
|
||||
goto fail;
|
||||
bits = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (bits)
|
||||
goto fail;
|
||||
}
|
||||
|
||||
if (!srclen && dstpos <= *dstlen) {
|
||||
*dstlen = dstpos;
|
||||
dbg_dec64("dec64: OK, dst[%d]", (int)dstpos);
|
||||
return src;
|
||||
}
|
||||
fail:
|
||||
/* *dstlen = 0; - not needed, caller detects error by seeing NULL */
|
||||
return NULL;
|
||||
}
|
||||
#endif
|
||||
|
||||
static char *encode64(
|
||||
char *dst, size_t dstlen,
|
||||
const uint8_t *src, size_t srclen)
|
||||
{
|
||||
while (srclen) {
|
||||
uint32_t value = 0, b = 0;
|
||||
do {
|
||||
value |= (uint32_t)(*src++ << b);
|
||||
b += 8;
|
||||
srclen--;
|
||||
} while (srclen && b < 24);
|
||||
|
||||
b >>= 3; /* number of bits to number of bytes */
|
||||
b++; /* 1, 2 or 3 bytes will become 2, 3 or 4 ascii64 chars */
|
||||
dstlen -= b;
|
||||
if ((ssize_t)dstlen <= 0)
|
||||
return NULL;
|
||||
dst = num2str64_lsb_first(dst, value, b);
|
||||
}
|
||||
*dst = '\0';
|
||||
return dst;
|
||||
}
|
||||
|
||||
char *yescrypt_r(
|
||||
const uint8_t *passwd, size_t passwdlen,
|
||||
const uint8_t *setting,
|
||||
char *buf, size_t buflen)
|
||||
{
|
||||
struct {
|
||||
yescrypt_ctx_t yctx[1];
|
||||
unsigned char hashbin32[32];
|
||||
} u;
|
||||
#define yctx u.yctx
|
||||
#define hashbin32 u.hashbin32
|
||||
char *dst;
|
||||
const uint8_t *src, *saltend;
|
||||
size_t need, prefixlen;
|
||||
uint32_t u32;
|
||||
|
||||
test_decode64_uint32();
|
||||
|
||||
memset(yctx, 0, sizeof(yctx));
|
||||
FULL_PARAMS(yctx->param.p = 1;)
|
||||
|
||||
/* we assume setting starts with "$y$" (caller must ensure this) */
|
||||
src = setting + 3;
|
||||
|
||||
src = decode64_uint32(&yctx->param.flags, src, 0);
|
||||
/* "j9T" returns: 0x2f */
|
||||
//if (!src)
|
||||
// goto fail;
|
||||
|
||||
if (yctx->param.flags < YESCRYPT_RW) {
|
||||
dbg("yctx->param.flags=0x%x", (unsigned)yctx->param.flags);
|
||||
goto fail; // bbox: we don't support scrypt - only yescrypt
|
||||
} else if (yctx->param.flags <= YESCRYPT_RW + (YESCRYPT_RW_FLAVOR_MASK >> 2)) {
|
||||
/* "j9T" sets flags to 0xb6 */
|
||||
yctx->param.flags = YESCRYPT_RW + ((yctx->param.flags - YESCRYPT_RW) << 2);
|
||||
dbg("yctx->param.flags=0x%x", (unsigned)yctx->param.flags);
|
||||
dbg(" YESCRYPT_RW:%u", !!(yctx->param.flags & YESCRYPT_RW));
|
||||
dbg((yctx->param.flags & YESCRYPT_RW_FLAVOR_MASK) ==
|
||||
(YESCRYPT_ROUNDS_6 | YESCRYPT_GATHER_4 | YESCRYPT_SIMPLE_2 | YESCRYPT_SBOX_12K)
|
||||
? " YESCRYPT_ROUNDS_6 | YESCRYPT_GATHER_4 | YESCRYPT_SIMPLE_2 | YESCRYPT_SBOX_12K"
|
||||
: " flags are not standard"
|
||||
);
|
||||
} else {
|
||||
goto fail;
|
||||
}
|
||||
|
||||
src = decode64_uint32(&u32, src, 1);
|
||||
if (/*!src ||*/ u32 > 63)
|
||||
goto fail;
|
||||
yctx->param.N = (uint64_t)1 << u32;
|
||||
/* "j9T" sets to 4096 (1<<12) */
|
||||
dbg("yctx->param.N=%llu (1<<%u)", (unsigned long long)yctx->param.N, (unsigned)u32);
|
||||
|
||||
src = decode64_uint32(&yctx->param.r, src, 1);
|
||||
/* "j9T" sets to 32 */
|
||||
dbg("yctx->param.r=%u", yctx->param.r);
|
||||
|
||||
if (!src)
|
||||
goto fail;
|
||||
if (*src != '$') {
|
||||
#if RESTRICTED_PARAMS
|
||||
goto fail;
|
||||
#else
|
||||
src = decode64_uint32(&u32, src, 1);
|
||||
dbg("yescrypt has extended params:0x%x", (unsigned)u32);
|
||||
if (u32 & 1)
|
||||
src = decode64_uint32(&yctx->param.p, src, 2);
|
||||
if (u32 & 2)
|
||||
src = decode64_uint32(&yctx->param.t, src, 1);
|
||||
if (u32 & 4)
|
||||
src = decode64_uint32(&yctx->param.g, src, 1);
|
||||
if (u32 & 8) {
|
||||
src = decode64_uint32(&u32, src, 1);
|
||||
if (/*!src ||*/ u32 > 63)
|
||||
goto fail;
|
||||
yctx->param.NROM = (uint64_t)1 << u32;
|
||||
}
|
||||
if (!src)
|
||||
goto fail;
|
||||
if (*src != '$')
|
||||
goto fail;
|
||||
#endif
|
||||
}
|
||||
|
||||
yctx->saltlen = sizeof(yctx->salt);
|
||||
src++; /* now points to salt */
|
||||
saltend = decode64(yctx->salt, &yctx->saltlen, src);
|
||||
if (!saltend || (*saltend != '\0' && *saltend != '$'))
|
||||
goto fail; /* salt[] is too small, or bad char during decode */
|
||||
dbg_dec64("salt is %d ascii64 chars -> %d bytes (in binary)", (int)(saltend - src), (int)yctx->saltlen);
|
||||
|
||||
prefixlen = saltend - setting;
|
||||
need = prefixlen + 1 + YESCRYPT_HASH_LEN + 1;
|
||||
if (need > buflen /*overflow is quite unlikely: || need < prefixlen*/)
|
||||
goto fail;
|
||||
|
||||
if (yescrypt_kdf32(yctx, passwd, passwdlen, hashbin32)) {
|
||||
dbg("error in yescrypt_kdf32");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
dst = mempcpy(buf, setting, prefixlen);
|
||||
*dst++ = '$';
|
||||
dst = encode64(dst, buflen - (dst - buf), hashbin32, sizeof(hashbin32));
|
||||
if (!dst)
|
||||
goto fail;
|
||||
ret:
|
||||
free_region(yctx->local);
|
||||
explicit_bzero(&u, sizeof(u));
|
||||
return buf;
|
||||
fail:
|
||||
buf = NULL;
|
||||
goto ret;
|
||||
#undef yctx
|
||||
#undef hashbin32
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,247 @@
|
||||
/*-
|
||||
* Copyright 2009 Colin Percival
|
||||
* Copyright 2013-2018 Alexander Peslyak
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* This file was originally written by Colin Percival as part of the Tarsnap
|
||||
* online backup system.
|
||||
*/
|
||||
|
||||
// busybox debug and size-reduction configuration
|
||||
|
||||
#ifdef YESCRYPT_INTERNAL
|
||||
# if 1
|
||||
# define dbg(...) ((void)0)
|
||||
# else
|
||||
# define dbg(...) bb_error_msg(__VA_ARGS__)
|
||||
# endif
|
||||
# if 1
|
||||
# define dbg_dec64(...) ((void)0)
|
||||
# else
|
||||
# define dbg_dec64(...) bb_error_msg(__VA_ARGS__)
|
||||
# endif
|
||||
# define TEST_DECODE64 0
|
||||
#endif
|
||||
|
||||
// Only accept one-char parameters in salt, and only first three?
|
||||
// Almost any reasonable yescrypt hashes in /etc/shadow should
|
||||
// only ever use "jXY" parameters which set N and r.
|
||||
// Fancy multi-byte-encoded wide integers are not needed for that.
|
||||
#define RESTRICTED_PARAMS 1
|
||||
// Note: if you enable the above, please also enable
|
||||
// YCTX_param_p, YCTX_param_t, YCTX_param_g, YCTX_param_NROM
|
||||
// optimizations, and DISABLE_NROM_CODE.
|
||||
|
||||
#define DISABLE_NROM_CODE 1
|
||||
|
||||
// How much we save by forcing "standard" value by commenting the next line:
|
||||
// 160 bytes
|
||||
//#define YCTX_param_flags yctx->param.flags
|
||||
// 260 bytes
|
||||
//#define flags___YESCRYPT_RW (flags & YESCRYPT_RW)
|
||||
// 140 bytes
|
||||
//#define flags___YESCRYPT_MODE_MASK (flags & YESCRYPT_MODE_MASK)
|
||||
// ^^^^ forcing the above since the code already requires (checks for) this
|
||||
// 50 bytes
|
||||
#define YCTX_param_N yctx->param.N
|
||||
// -100 bytes (negative!!!)
|
||||
#define YCTX_param_r yctx->param.r
|
||||
// 400 bytes
|
||||
//#define YCTX_param_p yctx->param.p
|
||||
// 130 bytes
|
||||
//#define YCTX_param_t yctx->param.t
|
||||
// 2 bytes
|
||||
//#define YCTX_param_g yctx->param.g
|
||||
// 1 bytes
|
||||
// ^^^^ this looks wrong, compiler should be able to constant-propagate the fact that NROM code is dead
|
||||
//#define YCTX_param_NROM yctx->param.NROM
|
||||
|
||||
#ifndef YCTX_param_flags
|
||||
#define YCTX_param_flags (YESCRYPT_RW | YESCRYPT_ROUNDS_6 | YESCRYPT_GATHER_4 | YESCRYPT_SIMPLE_2 | YESCRYPT_SBOX_12K)
|
||||
#endif
|
||||
#ifndef flags___YESCRYPT_RW
|
||||
#define flags___YESCRYPT_RW ((void)flags, YESCRYPT_RW)
|
||||
#endif
|
||||
#ifndef flags___YESCRYPT_MODE_MASK
|
||||
#define flags___YESCRYPT_MODE_MASK ((void)flags, YESCRYPT_RW)
|
||||
#endif
|
||||
// standard ("j9T") values:
|
||||
#ifndef YCTX_param_N
|
||||
#define YCTX_param_N 4096
|
||||
#endif
|
||||
#ifndef YCTX_param_r
|
||||
#define YCTX_param_r 32
|
||||
#endif
|
||||
#ifndef YCTX_param_p
|
||||
#define YCTX_param_p 1
|
||||
#endif
|
||||
#ifndef YCTX_param_t
|
||||
#define YCTX_param_t 0
|
||||
#endif
|
||||
#ifndef YCTX_param_g
|
||||
#define YCTX_param_g 0
|
||||
#endif
|
||||
#ifndef YCTX_param_NROM
|
||||
#define YCTX_param_NROM 0
|
||||
#endif
|
||||
|
||||
// "Faster/smaller code" knobs:
|
||||
// -941 bytes:
|
||||
#define KDF_UNROLL_COPY 0
|
||||
// -5324 bytes if 0:
|
||||
#define KDF_UNROLL_PWXFORM_ROUND 0
|
||||
// -4864 bytes if 0:
|
||||
#define KDF_UNROLL_PWXFORM 0
|
||||
// if both this ^^^^^^^^^^ and PWXFORM_ROUND set to 0: -7666 bytes
|
||||
// -464 bytes:
|
||||
#define KDF_UNROLL_SALSA20 0
|
||||
|
||||
/**
|
||||
* Type and possible values for the flags argument of yescrypt_kdf(),
|
||||
* yescrypt_encode_params_r(), yescrypt_encode_params(). Most of these may be
|
||||
* OR'ed together, except that YESCRYPT_WORM stands on its own.
|
||||
* Please refer to the description of yescrypt_kdf() below for the meaning of
|
||||
* these flags.
|
||||
*/
|
||||
/* yescrypt flags:
|
||||
* bits pos: 7654321076543210
|
||||
* ss r w
|
||||
* sbox gg y
|
||||
*/
|
||||
/* Public */
|
||||
#define YESCRYPT_WORM 1
|
||||
#define YESCRYPT_RW 0x002
|
||||
#define YESCRYPT_ROUNDS_3 0x000 //r=0
|
||||
#define YESCRYPT_ROUNDS_6 0x004 //r=1
|
||||
#define YESCRYPT_GATHER_1 0x000 //gg=00
|
||||
#define YESCRYPT_GATHER_2 0x008 //gg=01
|
||||
#define YESCRYPT_GATHER_4 0x010 //gg=10
|
||||
#define YESCRYPT_GATHER_8 0x018 //gg=11
|
||||
#define YESCRYPT_SIMPLE_1 0x000 //ss=00
|
||||
#define YESCRYPT_SIMPLE_2 0x020 //ss=01
|
||||
#define YESCRYPT_SIMPLE_4 0x040 //ss=10
|
||||
#define YESCRYPT_SIMPLE_8 0x060 //ss=11
|
||||
#define YESCRYPT_SBOX_6K 0x000 //sbox=0000
|
||||
#define YESCRYPT_SBOX_12K 0x080 //sbox=0001
|
||||
#define YESCRYPT_SBOX_24K 0x100 //sbox=0010
|
||||
#define YESCRYPT_SBOX_48K 0x180 //sbox=0011
|
||||
#define YESCRYPT_SBOX_96K 0x200 //sbox=0100
|
||||
#define YESCRYPT_SBOX_192K 0x280 //sbox=0101
|
||||
#define YESCRYPT_SBOX_384K 0x300 //sbox=0110
|
||||
#define YESCRYPT_SBOX_768K 0x380 //sbox=0111
|
||||
|
||||
#ifdef YESCRYPT_INTERNAL
|
||||
/* Private */
|
||||
#define YESCRYPT_MODE_MASK 0x003
|
||||
#define YESCRYPT_RW_FLAVOR_MASK 0x3fc
|
||||
#define YESCRYPT_ALLOC_ONLY 0x08000000
|
||||
#define YESCRYPT_PREHASH 0x10000000
|
||||
#endif
|
||||
|
||||
#define YESCRYPT_RW_DEFAULTS \
|
||||
(YESCRYPT_RW | \
|
||||
YESCRYPT_ROUNDS_6 | YESCRYPT_GATHER_4 | YESCRYPT_SIMPLE_2 | \
|
||||
YESCRYPT_SBOX_12K)
|
||||
|
||||
#define YESCRYPT_DEFAULTS YESCRYPT_RW_DEFAULTS
|
||||
|
||||
#ifdef YESCRYPT_INTERNAL
|
||||
#define YESCRYPT_KNOWN_FLAGS \
|
||||
(YESCRYPT_MODE_MASK | YESCRYPT_RW_FLAVOR_MASK | \
|
||||
YESCRYPT_ALLOC_ONLY | YESCRYPT_PREHASH)
|
||||
#endif
|
||||
|
||||
/* How many chars base-64 encoded bytes require? */
|
||||
#define YESCRYPT_BYTES2CHARS(bytes) ((((bytes) * 8) + 5) / 6)
|
||||
/* The /etc/passwd-style hash is "<prefix>$<hash><NUL>" */
|
||||
/*
|
||||
* "$y$", up to 8 params of up to 6 chars each, '$', salt
|
||||
* Alternatively, but that's smaller:
|
||||
* "$7$", 3 params encoded as 1+5+5 chars, salt
|
||||
*/
|
||||
#define YESCRYPT_PREFIX_LEN (3 + 8 * 6 + 1 + YESCRYPT_BYTES2CHARS(32))
|
||||
|
||||
#define YESCRYPT_HASH_SIZE 32
|
||||
#define YESCRYPT_HASH_LEN YESCRYPT_BYTES2CHARS(YESCRYPT_HASH_SIZE)
|
||||
|
||||
/**
|
||||
* Internal type used by the memory allocator. Please do not use it directly.
|
||||
* Use yescrypt_shared_t and yescrypt_local_t as appropriate instead, since
|
||||
* they might differ from each other in a future version.
|
||||
*/
|
||||
typedef struct {
|
||||
// void *base;
|
||||
void *aligned;
|
||||
// size_t base_size;
|
||||
size_t aligned_size;
|
||||
} yescrypt_region_t;
|
||||
|
||||
/**
|
||||
* yescrypt parameters combined into one struct. N, r, p are the same as in
|
||||
* classic scrypt, except that the meaning of p changes when YESCRYPT_RW is
|
||||
* set. flags, t, g, NROM are special to yescrypt.
|
||||
*/
|
||||
typedef struct {
|
||||
uint32_t flags;
|
||||
uint32_t r;
|
||||
uint64_t N;
|
||||
#if !RESTRICTED_PARAMS
|
||||
uint32_t p, t, g;
|
||||
uint64_t NROM;
|
||||
#endif
|
||||
} yescrypt_params_t;
|
||||
|
||||
typedef struct {
|
||||
yescrypt_params_t param;
|
||||
|
||||
/* salt in binary form */
|
||||
/* stored here to cut down on the amount of function paramaters */
|
||||
unsigned char salt[64];
|
||||
size_t saltlen;
|
||||
|
||||
/* used by the memory allocator */
|
||||
//yescrypt_region_t shared[1];
|
||||
yescrypt_region_t local[1];
|
||||
} yescrypt_ctx_t;
|
||||
|
||||
/**
|
||||
* yescrypt_r(shared, local, passwd, passwdlen, setting, key, buf, buflen):
|
||||
* Compute and encode an scrypt or enhanced scrypt hash of passwd given the
|
||||
* parameters and salt value encoded in setting. If shared is not NULL, a ROM
|
||||
* is used and YESCRYPT_RW is required. Otherwise, whether to compute classic
|
||||
* scrypt, YESCRYPT_WORM (a slight deviation from classic scrypt), or
|
||||
* YESCRYPT_RW (time-memory tradeoff discouraging modification) is determined
|
||||
* by the setting string. shared (if not NULL) and local must be initialized
|
||||
* as described above for yescrypt_kdf(). buf must be large enough (as
|
||||
* indicated by buflen) to hold the encoded hash string.
|
||||
*
|
||||
* Return the encoded hash string on success; or NULL on error.
|
||||
*
|
||||
* MT-safe as long as local and buf are local to the thread.
|
||||
*/
|
||||
extern char *yescrypt_r(
|
||||
const uint8_t *passwd, size_t passwdlen,
|
||||
const uint8_t *setting,
|
||||
char *buf, size_t buflen
|
||||
);
|
||||
@@ -0,0 +1,16 @@
|
||||
/*
|
||||
* The compilation unit for yescrypt-related code.
|
||||
*
|
||||
* Copyright (C) 2025 by Denys Vlasenko <vda.linux@googlemail.com>
|
||||
*
|
||||
* Licensed under GPLv2, see file LICENSE in this source tree.
|
||||
*/
|
||||
//kbuild:lib-$(CONFIG_USE_BB_CRYPT_YES) += y.o
|
||||
|
||||
#include "libbb.h"
|
||||
|
||||
#define YESCRYPT_INTERNAL
|
||||
#include "alg-yescrypt.h"
|
||||
#include "alg-sha256.c"
|
||||
#include "alg-yescrypt-kdf.c"
|
||||
#include "alg-yescrypt-common.c"
|
||||
Loaded 100 of 324 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user