aboutsummaryrefslogtreecommitdiff
path: root/include/libbb.h
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--include/libbb.h422
1 files changed, 364 insertions, 58 deletions
diff --git a/include/libbb.h b/include/libbb.h
index e765e18eb..60037ed3d 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -10,6 +10,12 @@
10#ifndef LIBBB_H 10#ifndef LIBBB_H
11#define LIBBB_H 1 11#define LIBBB_H 1
12 12
13#if ENABLE_PLATFORM_MINGW32
14/* We have our own nanosleep(), clock_gettime() and clock_settime(). */
15/* Skip the Windows include file that declares them. */
16# define WIN_PTHREADS_TIME_H
17#endif
18
13#include "platform.h" 19#include "platform.h"
14 20
15#include <ctype.h> 21#include <ctype.h>
@@ -143,6 +149,9 @@
143# include <arpa/inet.h> 149# include <arpa/inet.h>
144#elif defined __APPLE__ 150#elif defined __APPLE__
145# include <netinet/in.h> 151# include <netinet/in.h>
152#elif ENABLE_PLATFORM_MINGW32
153# include <winsock2.h>
154# include <ws2tcpip.h>
146#else 155#else
147# include <arpa/inet.h> 156# include <arpa/inet.h>
148//This breaks on bionic: 157//This breaks on bionic:
@@ -182,7 +191,9 @@
182 191
183/* Some libc's forget to declare these, do it ourself */ 192/* Some libc's forget to declare these, do it ourself */
184 193
194#if !ENABLE_PLATFORM_MINGW32
185extern char **environ; 195extern char **environ;
196#endif
186/* klogctl is in libc's klog.h, but we cheat and not #include that */ 197/* klogctl is in libc's klog.h, but we cheat and not #include that */
187int klogctl(int type, char *b, int len); 198int klogctl(int type, char *b, int len);
188#ifndef PATH_MAX 199#ifndef PATH_MAX
@@ -192,6 +203,13 @@ int klogctl(int type, char *b, int len);
192# define BUFSIZ 4096 203# define BUFSIZ 4096
193#endif 204#endif
194 205
206#if ENABLE_PLATFORM_MINGW32
207# include "mingw.h"
208# define MINGW_SPECIAL(a) mingw_ ## a
209#else
210# define MINGW_SPECIAL(a) a
211#endif
212
195#if __GNUC_PREREQ(5,0) 213#if __GNUC_PREREQ(5,0)
196/* Since musl is apparently unable to get it right and would use 214/* Since musl is apparently unable to get it right and would use
197 * a function call to a single-instruction function of "bswap %eax", 215 * a function call to a single-instruction function of "bswap %eax",
@@ -263,6 +281,26 @@ PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN
263# endif 281# endif
264#endif 282#endif
265 283
284#if ENABLE_FEATURE_TLS_SCHANNEL || ENABLE_FEATURE_USE_CNG_API
285# define SECURITY_WIN32
286# include <windows.h>
287# include <security.h>
288#endif
289
290#if ENABLE_FEATURE_USE_CNG_API
291# include <bcrypt.h>
292
293// these work on Windows >= 10
294# define BCRYPT_HMAC_SHA1_ALG_HANDLE ((BCRYPT_ALG_HANDLE) 0x000000a1)
295# define BCRYPT_HMAC_SHA256_ALG_HANDLE ((BCRYPT_ALG_HANDLE) 0x000000b1)
296# define sha1_begin_hmac BCRYPT_HMAC_SHA1_ALG_HANDLE
297# define sha256_begin_hmac BCRYPT_HMAC_SHA256_ALG_HANDLE
298#else
299# define sha1_begin_hmac sha1_begin
300# define sha256_begin_hmac sha256_begin
301# define hmac_uninit(...) ((void)0)
302#endif
303
266/* Tested to work correctly with all int types (IIRC :]) */ 304/* Tested to work correctly with all int types (IIRC :]) */
267#define MAXINT(T) (T)( \ 305#define MAXINT(T) (T)( \
268 ((T)-1) > 0 \ 306 ((T)-1) > 0 \
@@ -276,6 +314,20 @@ PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN
276 : ((T)1 << (sizeof(T)*8-1)) \ 314 : ((T)1 << (sizeof(T)*8-1)) \
277 ) 315 )
278 316
317// UCRT supports both "ll" and "I64", but gcc warns on "I64" with UCRT mingw
318#if ENABLE_PLATFORM_MINGW32 && !defined(_UCRT) && \
319 (!defined(__USE_MINGW_ANSI_STDIO) || !__USE_MINGW_ANSI_STDIO)
320#define LL_FMT "I64"
321#else
322#define LL_FMT "ll"
323#endif
324
325#if ENABLE_PLATFORM_MINGW32 && defined(_WIN64)
326#define PID_FMT LL_FMT
327#else
328#define PID_FMT
329#endif
330
279/* Large file support */ 331/* Large file support */
280/* Note that CONFIG_LFS=y forces bbox to be built with all common ops 332/* Note that CONFIG_LFS=y forces bbox to be built with all common ops
281 * (stat, lseek etc) mapped to "largefile" variants by libc. 333 * (stat, lseek etc) mapped to "largefile" variants by libc.
@@ -301,7 +353,7 @@ typedef unsigned long long uoff_t;
301# define XATOOFF(a) xatoull_range((a), 0, LLONG_MAX) 353# define XATOOFF(a) xatoull_range((a), 0, LLONG_MAX)
302# define BB_STRTOOFF bb_strtoull 354# define BB_STRTOOFF bb_strtoull
303# define STRTOOFF strtoull 355# define STRTOOFF strtoull
304# define OFF_FMT "ll" 356# define OFF_FMT LL_FMT
305# endif 357# endif
306#else 358#else
307/* CONFIG_LFS is off */ 359/* CONFIG_LFS is off */
@@ -570,13 +622,20 @@ char *bb_get_last_path_component_nostrip(const char *path) FAST_FUNC;
570const char *bb_basename(const char *name) FAST_FUNC; 622const char *bb_basename(const char *name) FAST_FUNC;
571/* NB: can violate const-ness (similarly to strchr) */ 623/* NB: can violate const-ness (similarly to strchr) */
572char *last_char_is(const char *s, int c) FAST_FUNC; 624char *last_char_is(const char *s, int c) FAST_FUNC;
625char *last_char_is_dir_sep(const char *s) FAST_FUNC;
573const char* endofname(const char *name) FAST_FUNC; 626const char* endofname(const char *name) FAST_FUNC;
574char *is_prefixed_with(const char *string, const char *key) FAST_FUNC; 627char *is_prefixed_with(const char *string, const char *key) FAST_FUNC;
575char *is_suffixed_with(const char *string, const char *key) FAST_FUNC; 628char *is_suffixed_with(const char *string, const char *key) FAST_FUNC;
576 629
630#if !ENABLE_PLATFORM_MINGW32
577int ndelay_on(int fd) FAST_FUNC; 631int ndelay_on(int fd) FAST_FUNC;
578int ndelay_off(int fd) FAST_FUNC; 632int ndelay_off(int fd) FAST_FUNC;
579void close_on_exec_on(int fd) FAST_FUNC; 633void close_on_exec_on(int fd) FAST_FUNC;
634#else
635static inline int ndelay_on(int fd UNUSED_PARAM) { return 0; }
636static inline int ndelay_off(int fd UNUSED_PARAM) { return 0; }
637static inline void close_on_exec_on(int fd UNUSED_PARAM) { return; }
638#endif
580void xdup2(int, int) FAST_FUNC; 639void xdup2(int, int) FAST_FUNC;
581void xmove_fd(int, int) FAST_FUNC; 640void xmove_fd(int, int) FAST_FUNC;
582 641
@@ -610,20 +669,37 @@ enum {
610 * Dance around with long long to guard against that... 669 * Dance around with long long to guard against that...
611 */ 670 */
612 BB_FATAL_SIGS = (int)(0 671 BB_FATAL_SIGS = (int)(0
672#ifdef SIGHUP
613 + (1LL << SIGHUP) 673 + (1LL << SIGHUP)
674#endif
614 + (1LL << SIGINT) 675 + (1LL << SIGINT)
615 + (1LL << SIGTERM) 676 + (1LL << SIGTERM)
616 + (1LL << SIGPIPE) // Write to pipe with no readers 677 + (1LL << SIGPIPE) // Write to pipe with no readers
678#ifdef SIGQUIT
617 + (1LL << SIGQUIT) // Quit from keyboard 679 + (1LL << SIGQUIT) // Quit from keyboard
680#endif
618 + (1LL << SIGABRT) // Abort signal from abort(3) 681 + (1LL << SIGABRT) // Abort signal from abort(3)
682#ifdef SIGALRM
619 + (1LL << SIGALRM) // Timer signal from alarm(2) 683 + (1LL << SIGALRM) // Timer signal from alarm(2)
684#endif
685#ifdef SIGVTALRM
620 + (1LL << SIGVTALRM) // Virtual alarm clock 686 + (1LL << SIGVTALRM) // Virtual alarm clock
687#endif
688#ifdef SIGXCPU
621 + (1LL << SIGXCPU) // CPU time limit exceeded 689 + (1LL << SIGXCPU) // CPU time limit exceeded
690#endif
691#ifdef SIGXFSZ
622 + (1LL << SIGXFSZ) // File size limit exceeded 692 + (1LL << SIGXFSZ) // File size limit exceeded
693#endif
694#ifdef SIGUSR1
623 + (1LL << SIGUSR1) // Yes kids, these are also fatal! 695 + (1LL << SIGUSR1) // Yes kids, these are also fatal!
696#endif
697#ifdef SIGUSR1
624 + (1LL << SIGUSR2) 698 + (1LL << SIGUSR2)
699#endif
625 + 0), 700 + 0),
626}; 701};
702#if !ENABLE_PLATFORM_MINGW32
627void bb_signals(int sigs, void (*f)(int)) FAST_FUNC; 703void bb_signals(int sigs, void (*f)(int)) FAST_FUNC;
628/* Unlike signal() and bb_signals, sets handler with sigaction() 704/* Unlike signal() and bb_signals, sets handler with sigaction()
629 * and in a way that while signal handler is run, no other signals 705 * and in a way that while signal handler is run, no other signals
@@ -643,6 +719,10 @@ int sigaction_set(int sig, const struct sigaction *act) FAST_FUNC;
643int sigprocmask_allsigs(int how) FAST_FUNC; 719int sigprocmask_allsigs(int how) FAST_FUNC;
644/* Return old set in the same set: */ 720/* Return old set in the same set: */
645int sigprocmask2(int how, sigset_t *set) FAST_FUNC; 721int sigprocmask2(int how, sigset_t *set) FAST_FUNC;
722#else
723#define bb_signals(s, f)
724#define kill_myself_with_sig(s)
725#endif
646/* Standard handler which just records signo */ 726/* Standard handler which just records signo */
647extern smallint bb_got_signal; 727extern smallint bb_got_signal;
648void record_signo(int signo); /* not FAST_FUNC! */ 728void record_signo(int signo); /* not FAST_FUNC! */
@@ -726,7 +806,7 @@ void xsettimeofday(const struct timeval *tv) FAST_FUNC;
726int xsocket(int domain, int type, int protocol) FAST_FUNC; 806int xsocket(int domain, int type, int protocol) FAST_FUNC;
727void xbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen) FAST_FUNC; 807void xbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen) FAST_FUNC;
728void xlisten(int s, int backlog) FAST_FUNC; 808void xlisten(int s, int backlog) FAST_FUNC;
729void xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen) FAST_FUNC; 809void xconnect(int s, const struct sockaddr *saddr, socklen_t addrlen) FAST_FUNC;
730ssize_t xsendto(int s, const void *buf, size_t len, const struct sockaddr *to, 810ssize_t xsendto(int s, const void *buf, size_t len, const struct sockaddr *to,
731 socklen_t tolen) FAST_FUNC; 811 socklen_t tolen) FAST_FUNC;
732 812
@@ -839,7 +919,36 @@ struct hostent *xgethostbyname(const char *name) FAST_FUNC;
839// Also mount.c and inetd.c are using gethostbyname(), 919// Also mount.c and inetd.c are using gethostbyname(),
840// + inet_common.c has additional IPv4-only stuff 920// + inet_common.c has additional IPv4-only stuff
841 921
922#if defined CONFIG_FEATURE_TLS_SCHANNEL
923typedef struct tls_state {
924 int ofd;
925 int ifd;
926
927 // handles
928 CredHandle cred_handle;
929 CtxtHandle ctx_handle;
930
931 // buffers
932 char in_buffer[16384 + 256]; // input buffer (to read from server)
933 unsigned long in_buffer_size; // amount of data currently in input buffer
934
935 char *out_buffer; // output buffer (for decrypted data), this is essentially the same as input buffer as data is decrypted in place
936 unsigned long out_buffer_size; // amount of data currently in output buffer
937 unsigned long out_buffer_used; // amount of extra data currently in output buffer
842 938
939 // data
940 char *hostname;
941 SecPkgContext_StreamSizes stream_sizes;
942
943 // booleans
944
945 // context initialized
946 int initialized;
947
948 // closed by remote peer
949 int closed;
950} tls_state_t;
951#else
843struct tls_aes { 952struct tls_aes {
844 uint32_t key[60]; 953 uint32_t key[60];
845 unsigned rounds; 954 unsigned rounds;
@@ -896,12 +1005,14 @@ typedef struct tls_state {
896 struct tls_aes aes_decrypt; 1005 struct tls_aes aes_decrypt;
897 uint8_t H[16]; //used by AES_GCM 1006 uint8_t H[16]; //used by AES_GCM
898} tls_state_t; 1007} tls_state_t;
1008#endif
899 1009
900static inline tls_state_t *new_tls_state(void) 1010static inline tls_state_t *new_tls_state(void)
901{ 1011{
902 tls_state_t *tls = xzalloc(sizeof(*tls)); 1012 tls_state_t *tls = xzalloc(sizeof(*tls));
903 return tls; 1013 return tls;
904} 1014}
1015
905void tls_handshake(tls_state_t *tls, const char *sni) FAST_FUNC; 1016void tls_handshake(tls_state_t *tls, const char *sni) FAST_FUNC;
906#define TLSLOOP_EXIT_ON_LOCAL_EOF (1 << 0) 1017#define TLSLOOP_EXIT_ON_LOCAL_EOF (1 << 0)
907void tls_run_copy_loop(tls_state_t *tls, unsigned flags) FAST_FUNC; 1018void tls_run_copy_loop(tls_state_t *tls, unsigned flags) FAST_FUNC;
@@ -933,7 +1044,7 @@ int bb_putchar(int ch) FAST_FUNC;
933/* Note: does not use stdio, writes to fd 2 directly */ 1044/* Note: does not use stdio, writes to fd 2 directly */
934int bb_putchar_stderr(char ch) FAST_FUNC; 1045int bb_putchar_stderr(char ch) FAST_FUNC;
935int fputs_stdout(const char *s) FAST_FUNC; 1046int fputs_stdout(const char *s) FAST_FUNC;
936char *xasprintf(const char *format, ...) __attribute__ ((format(printf, 1, 2))) FAST_FUNC RETURNS_MALLOC; 1047char *xasprintf(const char *format, ...) __attribute__ ((format(printf, 1, 2))) RETURNS_MALLOC;
937char *auto_string(char *str) FAST_FUNC; 1048char *auto_string(char *str) FAST_FUNC;
938// gcc-4.1.1 still isn't good enough at optimizing it 1049// gcc-4.1.1 still isn't good enough at optimizing it
939// (+200 bytes compared to macro) 1050// (+200 bytes compared to macro)
@@ -1113,6 +1224,32 @@ char *bin2hex(char *dst, const char *src, int count) FAST_FUNC;
1113/* Reverse */ 1224/* Reverse */
1114char* hex2bin(char *dst, const char *src, int count) FAST_FUNC; 1225char* hex2bin(char *dst, const char *src, int count) FAST_FUNC;
1115 1226
1227/* Returns strlen as a bonus */
1228//size_t replace_char(char *s, char what, char with) FAST_FUNC;
1229static inline size_t replace_char(char *str, char from, char to)
1230{
1231 char *p = str;
1232 while (*p) {
1233 if (*p == from)
1234 *p = to;
1235 p++;
1236 }
1237 return p - str;
1238}
1239
1240extern const char c_escape_conv_str00[];
1241#define c_escape_conv_str07 (c_escape_conv_str00+3)
1242
1243void FAST_FUNC xorbuf_3(void *dst, const void *src1, const void *src2, unsigned count);
1244void FAST_FUNC xorbuf(void* buf, const void* mask, unsigned count);
1245void FAST_FUNC xorbuf16_aligned_long(void* buf, const void* mask);
1246void FAST_FUNC xorbuf64_3_aligned64(void *dst, const void *src1, const void *src2);
1247#if BB_UNALIGNED_MEMACCESS_OK
1248# define xorbuf16(buf,mask) xorbuf16_aligned_long(buf,mask)
1249#else
1250void FAST_FUNC xorbuf16(void* buf, const void* mask);
1251#endif
1252
1116/* Generate a UUID */ 1253/* Generate a UUID */
1117void generate_uuid(uint8_t *buf) FAST_FUNC; 1254void generate_uuid(uint8_t *buf) FAST_FUNC;
1118 1255
@@ -1208,12 +1345,20 @@ gid_t *bb_getgroups(int *ngroups, gid_t *group_array) FAST_FUNC;
1208struct cached_groupinfo { 1345struct cached_groupinfo {
1209 uid_t euid; 1346 uid_t euid;
1210 gid_t egid; 1347 gid_t egid;
1348#if !ENABLE_PLATFORM_MINGW32
1349 // If these are ever restored on Windows it will be necessary to alter
1350 // globals_misc_size()/globals_misc_copy() in ash.
1211 int ngroups; 1351 int ngroups;
1212 gid_t *supplementary_array; 1352 gid_t *supplementary_array;
1353#endif
1213}; 1354};
1214uid_t FAST_FUNC get_cached_euid(uid_t *euid); 1355uid_t FAST_FUNC get_cached_euid(uid_t *euid);
1215gid_t FAST_FUNC get_cached_egid(gid_t *egid); 1356gid_t FAST_FUNC get_cached_egid(gid_t *egid);
1357#if !ENABLE_PLATFORM_MINGW32
1216int FAST_FUNC is_in_supplementary_groups(struct cached_groupinfo *groupinfo, gid_t gid); 1358int FAST_FUNC is_in_supplementary_groups(struct cached_groupinfo *groupinfo, gid_t gid);
1359#else
1360# define is_in_supplementary_groups(g, i) (FALSE)
1361#endif
1217 1362
1218#if ENABLE_FEATURE_UTMP 1363#if ENABLE_FEATURE_UTMP
1219void FAST_FUNC write_new_utmp(pid_t pid, int new_type, const char *tty_name, const char *username, const char *hostname); 1364void FAST_FUNC write_new_utmp(pid_t pid, int new_type, const char *tty_name, const char *username, const char *hostname);
@@ -1249,6 +1394,7 @@ void BB_EXECVP_or_die(char **argv) NORETURN FAST_FUNC;
1249 1394
1250/* xvfork() can't be a _function_, return after vfork in child mangles stack 1395/* xvfork() can't be a _function_, return after vfork in child mangles stack
1251 * in the parent. It must be a macro. */ 1396 * in the parent. It must be a macro. */
1397#if !ENABLE_PLATFORM_MINGW32
1252#define xvfork() \ 1398#define xvfork() \
1253({ \ 1399({ \
1254 pid_t bb__xvfork_pid = vfork(); \ 1400 pid_t bb__xvfork_pid = vfork(); \
@@ -1256,6 +1402,9 @@ void BB_EXECVP_or_die(char **argv) NORETURN FAST_FUNC;
1256 bb_simple_perror_msg_and_die("vfork"); \ 1402 bb_simple_perror_msg_and_die("vfork"); \
1257 bb__xvfork_pid; \ 1403 bb__xvfork_pid; \
1258}) 1404})
1405#else
1406#define xvfork() vfork()
1407#endif
1259#if BB_MMU 1408#if BB_MMU
1260pid_t xfork(void) FAST_FUNC; 1409pid_t xfork(void) FAST_FUNC;
1261#endif 1410#endif
@@ -1290,6 +1439,15 @@ void run_noexec_applet_and_exit(int a, const char *name, char **argv) NORETURN F
1290#ifndef BUILD_INDIVIDUAL 1439#ifndef BUILD_INDIVIDUAL
1291int find_applet_by_name(const char *name) FAST_FUNC; 1440int find_applet_by_name(const char *name) FAST_FUNC;
1292void run_applet_no_and_exit(int a, const char *name, char **argv) NORETURN FAST_FUNC; 1441void run_applet_no_and_exit(int a, const char *name, char **argv) NORETURN FAST_FUNC;
1442# if ENABLE_PLATFORM_MINGW32
1443# if ENABLE_FEATURE_PREFER_APPLETS || ENABLE_FEATURE_SH_STANDALONE
1444int prefer_applet(const char *name, const char *path) FAST_FUNC;
1445int find_applet_by_name_for_sh(const char *name, const char *path) FAST_FUNC;
1446# endif
1447# else
1448# define prefer_applet(n, p) (1)
1449# define find_applet_by_name_for_sh(n, p) find_applet_by_name(n)
1450# endif
1293#endif 1451#endif
1294void show_usage_if_dash_dash_help(int applet_no, char **argv) FAST_FUNC; 1452void show_usage_if_dash_dash_help(int applet_no, char **argv) FAST_FUNC;
1295#if defined(__linux__) 1453#if defined(__linux__)
@@ -1370,12 +1528,12 @@ char* single_argv(char **argv) FAST_FUNC;
1370char **skip_dash_dash(char **argv) FAST_FUNC; 1528char **skip_dash_dash(char **argv) FAST_FUNC;
1371extern const char *const bb_argv_dash[]; /* { "-", NULL } */ 1529extern const char *const bb_argv_dash[]; /* { "-", NULL } */
1372extern uint32_t option_mask32; 1530extern uint32_t option_mask32;
1373uint32_t getopt32(char **argv, const char *applet_opts, ...) FAST_FUNC; 1531uint32_t getopt32(char **argv, const char *applet_opts, ...);
1374# define No_argument "\0" 1532# define No_argument "\0"
1375# define Required_argument "\001" 1533# define Required_argument "\001"
1376# define Optional_argument "\002" 1534# define Optional_argument "\002"
1377#if ENABLE_LONG_OPTS 1535#if ENABLE_LONG_OPTS
1378uint32_t getopt32long(char **argv, const char *optstring, const char *longopts, ...) FAST_FUNC; 1536uint32_t getopt32long(char **argv, const char *optstring, const char *longopts, ...);
1379#else 1537#else
1380#define getopt32long(argv,optstring,longopts,...) \ 1538#define getopt32long(argv,optstring,longopts,...) \
1381 getopt32(argv,optstring,##__VA_ARGS__) 1539 getopt32(argv,optstring,##__VA_ARGS__)
@@ -1450,17 +1608,17 @@ extern uint8_t xfunc_error_retval;
1450extern void (*die_func)(void); 1608extern void (*die_func)(void);
1451void xfunc_die(void) NORETURN FAST_FUNC; 1609void xfunc_die(void) NORETURN FAST_FUNC;
1452void bb_show_usage(void) NORETURN FAST_FUNC; 1610void bb_show_usage(void) NORETURN FAST_FUNC;
1453void bb_error_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2))) FAST_FUNC; 1611void bb_error_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2)));
1454void bb_simple_error_msg(const char *s) FAST_FUNC; 1612void bb_simple_error_msg(const char *s) FAST_FUNC;
1455void bb_error_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2))) FAST_FUNC; 1613void bb_error_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2)));
1456void bb_simple_error_msg_and_die(const char *s) NORETURN FAST_FUNC; 1614void bb_simple_error_msg_and_die(const char *s) NORETURN FAST_FUNC;
1457void bb_perror_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2))) FAST_FUNC; 1615void bb_perror_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2)));
1458void bb_simple_perror_msg(const char *s) FAST_FUNC; 1616void bb_simple_perror_msg(const char *s) FAST_FUNC;
1459void bb_perror_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2))) FAST_FUNC; 1617void bb_perror_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2)));
1460void bb_simple_perror_msg_and_die(const char *s) NORETURN FAST_FUNC; 1618void bb_simple_perror_msg_and_die(const char *s) NORETURN FAST_FUNC;
1461void bb_herror_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2))) FAST_FUNC; 1619void bb_herror_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2)));
1462void bb_simple_herror_msg(const char *s) FAST_FUNC; 1620void bb_simple_herror_msg(const char *s) FAST_FUNC;
1463void bb_herror_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2))) FAST_FUNC; 1621void bb_herror_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2)));
1464void bb_simple_herror_msg_and_die(const char *s) NORETURN FAST_FUNC; 1622void bb_simple_herror_msg_and_die(const char *s) NORETURN FAST_FUNC;
1465void bb_perror_nomsg_and_die(void) NORETURN FAST_FUNC; 1623void bb_perror_nomsg_and_die(void) NORETURN FAST_FUNC;
1466void bb_perror_nomsg(void) FAST_FUNC; 1624void bb_perror_nomsg(void) FAST_FUNC;
@@ -1476,7 +1634,7 @@ void bb_logenv_override(void) FAST_FUNC;
1476typedef smalluint exitcode_t; 1634typedef smalluint exitcode_t;
1477 1635
1478#if ENABLE_FEATURE_SYSLOG_INFO 1636#if ENABLE_FEATURE_SYSLOG_INFO
1479void bb_info_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2))) FAST_FUNC; 1637void bb_info_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2)));
1480void bb_simple_info_msg(const char *s) FAST_FUNC; 1638void bb_simple_info_msg(const char *s) FAST_FUNC;
1481void bb_vinfo_msg(const char *s, va_list p) FAST_FUNC; 1639void bb_vinfo_msg(const char *s, va_list p) FAST_FUNC;
1482#else 1640#else
@@ -1806,18 +1964,25 @@ extern char *pw_encrypt(const char *clear, const char *salt, int cleanup) FAST_F
1806extern int obscure(const char *old, const char *newval, const struct passwd *pwdp) FAST_FUNC; 1964extern int obscure(const char *old, const char *newval, const struct passwd *pwdp) FAST_FUNC;
1807/* 1965/*
1808 * rnd is additional random input. New one is returned. 1966 * rnd is additional random input. New one is returned.
1809 * Useful if you call crypt_make_salt many times in a row: 1967 * Useful if you call crypt_make_rand64encoded many times in a row:
1810 * rnd = crypt_make_salt(buf1, 4, 0); 1968 * rnd = crypt_make_rand64encoded(buf1, 4, 0);
1811 * rnd = crypt_make_salt(buf2, 4, rnd); 1969 * rnd = crypt_make_rand64encoded(buf2, 4, rnd);
1812 * rnd = crypt_make_salt(buf3, 4, rnd); 1970 * rnd = crypt_make_rand64encoded(buf3, 4, rnd);
1813 * (otherwise we risk having same salt generated) 1971 * (otherwise we risk having same salt generated)
1814 */ 1972 */
1815extern int crypt_make_salt(char *p, int cnt /*, int rnd*/) FAST_FUNC; 1973extern int crypt_make_rand64encoded(char *p, int cnt /*, int rnd*/) FAST_FUNC;
1816/* "$N$" + sha_salt_16_bytes + NUL */ 1974/* Size of char salt[] to hold randomly-generated salt string
1817#define MAX_PW_SALT_LEN (3 + 16 + 1) 1975 * sha256/512:
1976 * "$5$" ["rounds=999999999$"] "<sha_salt_16_chars><NUL>"
1977 * "$6$" ["rounds=999999999$"] "<sha_salt_16_chars><NUL>"
1978 * #define MAX_PW_SALT_LEN (3 + sizeof("rounds=999999999$")-1 + 16 + 1)
1979 * yescrypt:
1980 * "$y$" <up to 8 params of up to 6 chars each> "$" <up to 86 chars salt><NUL>
1981 * (86 chars are ascii64-encoded 64 binary bytes)
1982 */
1983#define MAX_PW_SALT_LEN (3 + 8*6 + 1 + 86 + 1)
1818extern char* crypt_make_pw_salt(char p[MAX_PW_SALT_LEN], const char *algo) FAST_FUNC; 1984extern char* crypt_make_pw_salt(char p[MAX_PW_SALT_LEN], const char *algo) FAST_FUNC;
1819 1985
1820
1821/* Returns number of lines changed, or -1 on error */ 1986/* Returns number of lines changed, or -1 on error */
1822#if !(ENABLE_FEATURE_ADDUSER_TO_GROUP || ENABLE_FEATURE_DEL_USER_FROM_GROUP) 1987#if !(ENABLE_FEATURE_ADDUSER_TO_GROUP || ENABLE_FEATURE_DEL_USER_FROM_GROUP)
1823#define update_passwd(filename, username, data, member) \ 1988#define update_passwd(filename, username, data, member) \
@@ -1853,8 +2018,8 @@ int get_termios_and_make_raw(int fd, struct termios *newterm, struct termios *ol
1853int set_termios_to_raw(int fd, struct termios *oldterm, int flags) FAST_FUNC; 2018int set_termios_to_raw(int fd, struct termios *oldterm, int flags) FAST_FUNC;
1854 2019
1855/* NB: "unsigned request" is crucial! "int request" will break some arches! */ 2020/* NB: "unsigned request" is crucial! "int request" will break some arches! */
1856int ioctl_or_perror(int fd, unsigned request, void *argp, const char *fmt,...) __attribute__ ((format (printf, 4, 5))) FAST_FUNC; 2021int ioctl_or_perror(int fd, unsigned request, void *argp, const char *fmt,...) __attribute__ ((format (printf, 4, 5)));
1857int ioctl_or_perror_and_die(int fd, unsigned request, void *argp, const char *fmt,...) __attribute__ ((format (printf, 4, 5))) FAST_FUNC; 2022int ioctl_or_perror_and_die(int fd, unsigned request, void *argp, const char *fmt,...) __attribute__ ((format (printf, 4, 5)));
1858#if ENABLE_IOCTL_HEX2STR_ERROR 2023#if ENABLE_IOCTL_HEX2STR_ERROR
1859int bb_ioctl_or_warn(int fd, unsigned request, void *argp, const char *ioctl_name) FAST_FUNC; 2024int bb_ioctl_or_warn(int fd, unsigned request, void *argp, const char *ioctl_name) FAST_FUNC;
1860int bb_xioctl(int fd, unsigned request, void *argp, const char *ioctl_name) FAST_FUNC; 2025int bb_xioctl(int fd, unsigned request, void *argp, const char *ioctl_name) FAST_FUNC;
@@ -1867,9 +2032,15 @@ int bb_xioctl(int fd, unsigned request, void *argp) FAST_FUNC;
1867#define xioctl(fd,request,argp) bb_xioctl(fd,request,argp) 2032#define xioctl(fd,request,argp) bb_xioctl(fd,request,argp)
1868#endif 2033#endif
1869 2034
2035#if !ENABLE_PLATFORM_MINGW32 || ENABLE_FEATURE_EXTRA_FILE_DATA
1870char *is_in_ino_dev_hashtable(const struct stat *statbuf) FAST_FUNC; 2036char *is_in_ino_dev_hashtable(const struct stat *statbuf) FAST_FUNC;
1871void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name) FAST_FUNC; 2037void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name) FAST_FUNC;
1872void reset_ino_dev_hashtable(void) FAST_FUNC; 2038void reset_ino_dev_hashtable(void) FAST_FUNC;
2039#else
2040#define add_to_ino_dev_hashtable(s, n) (void)0
2041#define is_in_ino_dev_hashtable(s) NULL
2042#define reset_ino_dev_hashtable()
2043#endif
1873#ifdef __GLIBC__ 2044#ifdef __GLIBC__
1874/* At least glibc has horrendously large inline for this, so wrap it */ 2045/* At least glibc has horrendously large inline for this, so wrap it */
1875unsigned long long bb_makedev(unsigned major, unsigned minor) FAST_FUNC; 2046unsigned long long bb_makedev(unsigned major, unsigned minor) FAST_FUNC;
@@ -1947,11 +2118,17 @@ enum {
1947 * >=0: poll() for TIMEOUT milliseconds, return -1/EAGAIN on timeout 2118 * >=0: poll() for TIMEOUT milliseconds, return -1/EAGAIN on timeout
1948 */ 2119 */
1949int64_t read_key(int fd, char *buffer, int timeout) FAST_FUNC; 2120int64_t read_key(int fd, char *buffer, int timeout) FAST_FUNC;
2121#if ENABLE_PLATFORM_MINGW32
2122int64_t windows_read_key(int fd, char *buffer, int timeout) FAST_FUNC;
2123#endif
1950/* This version loops on EINTR: */ 2124/* This version loops on EINTR: */
1951int64_t safe_read_key(int fd, char *buffer, int timeout) FAST_FUNC; 2125int64_t safe_read_key(int fd, char *buffer, int timeout) FAST_FUNC;
1952void read_key_ungets(char *buffer, const char *str, unsigned len) FAST_FUNC; 2126void read_key_ungets(char *buffer, const char *str, unsigned len) FAST_FUNC;
1953 2127
1954int check_got_signal_and_poll(struct pollfd pfd[1], int timeout) FAST_FUNC; 2128int check_got_signal_and_poll(struct pollfd pfd[1], int timeout) FAST_FUNC;
2129#if ENABLE_PLATFORM_MINGW32
2130# define check_got_signal_and_poll(p, t) poll(p, 1, t)
2131#endif
1955 2132
1956#if ENABLE_FEATURE_EDITING 2133#if ENABLE_FEATURE_EDITING
1957/* It's NOT just ENABLEd or disabled. It's a number: */ 2134/* It's NOT just ENABLEd or disabled. It's a number: */
@@ -1961,8 +2138,14 @@ unsigned size_from_HISTFILESIZE(const char *hp) FAST_FUNC;
1961# else 2138# else
1962# define MAX_HISTORY 0 2139# define MAX_HISTORY 0
1963# endif 2140# endif
2141# if defined CONFIG_FEATURE_EDITING_HISTORY_DEFAULT && CONFIG_FEATURE_EDITING_HISTORY_DEFAULT > 0
2142# define DEFAULT_HISTORY (CONFIG_FEATURE_EDITING_HISTORY_DEFAULT + 0)
2143# else
2144# define DEFAULT_HISTORY 0
2145# endif
1964typedef const char *get_exe_name_t(int i) FAST_FUNC; 2146typedef const char *get_exe_name_t(int i) FAST_FUNC;
1965typedef const char *sh_get_var_t(const char *name) FAST_FUNC; 2147typedef const char *sh_get_var_t(const char *name) FAST_FUNC;
2148typedef int sh_accept_glob_t(const char *name) FAST_FUNC;
1966typedef struct line_input_t { 2149typedef struct line_input_t {
1967 int flags; 2150 int flags;
1968 int timeout; 2151 int timeout;
@@ -1976,6 +2159,9 @@ typedef struct line_input_t {
1976# if ENABLE_SHELL_ASH || ENABLE_SHELL_HUSH 2159# if ENABLE_SHELL_ASH || ENABLE_SHELL_HUSH
1977 /* function to fetch additional application-specific names to match */ 2160 /* function to fetch additional application-specific names to match */
1978 get_exe_name_t *get_exe_name; 2161 get_exe_name_t *get_exe_name;
2162# if ENABLE_ASH_GLOB_OPTIONS
2163 sh_accept_glob_t *sh_accept_glob;
2164# endif
1979# endif 2165# endif
1980# endif 2166# endif
1981# if (ENABLE_FEATURE_USERNAME_COMPLETION || ENABLE_FEATURE_EDITING_FANCY_PROMPT) \ 2167# if (ENABLE_FEATURE_USERNAME_COMPLETION || ENABLE_FEATURE_EDITING_FANCY_PROMPT) \
@@ -2010,6 +2196,9 @@ enum {
2010 VI_MODE = 8 * ENABLE_FEATURE_EDITING_VI, 2196 VI_MODE = 8 * ENABLE_FEATURE_EDITING_VI,
2011 WITH_PATH_LOOKUP = 0x10, 2197 WITH_PATH_LOOKUP = 0x10,
2012 LI_INTERRUPTIBLE = 0x20, 2198 LI_INTERRUPTIBLE = 0x20,
2199#if ENABLE_PLATFORM_MINGW32
2200 IGNORE_CTRL_C = 0x40,
2201#endif
2013 FOR_SHELL = DO_HISTORY | TAB_COMPLETION | USERNAME_COMPLETION | LI_INTERRUPTIBLE, 2202 FOR_SHELL = DO_HISTORY | TAB_COMPLETION | USERNAME_COMPLETION | LI_INTERRUPTIBLE,
2014}; 2203};
2015line_input_t *new_line_input_t(int flags) FAST_FUNC; 2204line_input_t *new_line_input_t(int flags) FAST_FUNC;
@@ -2039,6 +2228,10 @@ int read_line_input(const char* prompt, char* command, int maxsize) FAST_FUNC;
2039 2228
2040unsigned long* FAST_FUNC get_malloc_cpu_affinity(int pid, unsigned *sz); 2229unsigned long* FAST_FUNC get_malloc_cpu_affinity(int pid, unsigned *sz);
2041 2230
2231#if ENABLE_PLATFORM_MINGW32
2232# undef COMM_LEN
2233# define COMM_LEN 32
2234#endif
2042#ifndef COMM_LEN 2235#ifndef COMM_LEN
2043# ifdef TASK_COMM_LEN 2236# ifdef TASK_COMM_LEN
2044enum { COMM_LEN = TASK_COMM_LEN }; 2237enum { COMM_LEN = TASK_COMM_LEN };
@@ -2048,35 +2241,14 @@ enum { COMM_LEN = 16 };
2048# endif 2241# endif
2049#endif 2242#endif
2050 2243
2051struct smaprec {
2052 unsigned long mapped_rw;
2053 unsigned long mapped_ro;
2054 unsigned long shared_clean;
2055 unsigned long shared_dirty;
2056 unsigned long private_clean;
2057 unsigned long private_dirty;
2058 unsigned long stack;
2059 unsigned long smap_pss, smap_swap;
2060 unsigned long smap_size;
2061 // For mixed 32/64 userspace, 32-bit pmap still needs
2062 // 64-bit field here to correctly show 64-bit processes:
2063 unsigned long long smap_start;
2064 // (strictly speaking, other fields need to be wider too,
2065 // but they are in kbytes, not bytes, and they hold sizes,
2066 // not start addresses, sizes tend to be less than 4 terabytes)
2067 char smap_mode[5];
2068 char *smap_name;
2069};
2070
2071#if !ENABLE_PMAP
2072#define procps_read_smaps(pid, total, cb, data) \
2073 procps_read_smaps(pid, total)
2074#endif
2075int FAST_FUNC procps_read_smaps(pid_t pid, struct smaprec *total,
2076 void (*cb)(struct smaprec *, void *), void *data);
2077
2078typedef struct procps_status_t { 2244typedef struct procps_status_t {
2245#if !ENABLE_PLATFORM_MINGW32
2079 DIR *dir; 2246 DIR *dir;
2247#else
2248 HANDLE snapshot;
2249 DWORD *pids;
2250 int npids;
2251#endif
2080 IF_FEATURE_SHOW_THREADS(DIR *task_dir;) 2252 IF_FEATURE_SHOW_THREADS(DIR *task_dir;)
2081 uint8_t shift_pages_to_bytes; 2253 uint8_t shift_pages_to_bytes;
2082 uint8_t shift_pages_to_kb; 2254 uint8_t shift_pages_to_kb;
@@ -2104,7 +2276,13 @@ typedef struct procps_status_t {
2104#endif 2276#endif
2105 unsigned tty_major,tty_minor; 2277 unsigned tty_major,tty_minor;
2106#if ENABLE_FEATURE_TOPMEM 2278#if ENABLE_FEATURE_TOPMEM
2107 struct smaprec smaps; 2279 unsigned long mapped_rw;
2280 unsigned long mapped_ro;
2281 unsigned long shared_clean;
2282 unsigned long shared_dirty;
2283 unsigned long private_clean;
2284 unsigned long private_dirty;
2285 unsigned long stack;
2108#endif 2286#endif
2109 char state[4]; 2287 char state[4];
2110 /* basename of executable in exec(2), read from /proc/N/stat 2288 /* basename of executable in exec(2), read from /proc/N/stat
@@ -2153,11 +2331,15 @@ void free_procps_scan(procps_status_t* sp) FAST_FUNC;
2153procps_status_t* procps_scan(procps_status_t* sp, int flags) FAST_FUNC; 2331procps_status_t* procps_scan(procps_status_t* sp, int flags) FAST_FUNC;
2154/* Format cmdline (up to col chars) into char buf[size] */ 2332/* Format cmdline (up to col chars) into char buf[size] */
2155/* Puts [comm] if cmdline is empty (-> process is a kernel thread) */ 2333/* Puts [comm] if cmdline is empty (-> process is a kernel thread) */
2156void read_cmdline(char *buf, int size, unsigned pid, const char *comm) FAST_FUNC; 2334int read_cmdline(char *buf, int size, unsigned pid, const char *comm) FAST_FUNC;
2157pid_t *find_pid_by_name(const char* procName) FAST_FUNC; 2335pid_t *find_pid_by_name(const char* procName) FAST_FUNC;
2158pid_t *pidlist_reverse(pid_t *pidList) FAST_FUNC; 2336pid_t *pidlist_reverse(pid_t *pidList) FAST_FUNC;
2159int starts_with_cpu(const char *str) FAST_FUNC; 2337int starts_with_cpu(const char *str) FAST_FUNC;
2160unsigned get_cpu_count(void) FAST_FUNC; 2338unsigned get_cpu_count(void) FAST_FUNC;
2339/* Some internals reused by pmap: */
2340unsigned long FAST_FUNC fast_strtoul_10(char **endptr);
2341unsigned long long FAST_FUNC fast_strtoull_16(char **endptr);
2342char* FAST_FUNC skip_fields(char *str, int count);
2161 2343
2162 2344
2163/* Use strict=1 if you process input from untrusted source: 2345/* Use strict=1 if you process input from untrusted source:
@@ -2183,6 +2365,56 @@ char *decode_base64(char *dst, const char **pp_src) FAST_FUNC;
2183char *decode_base32(char *dst, const char **pp_src) FAST_FUNC; 2365char *decode_base32(char *dst, const char **pp_src) FAST_FUNC;
2184void read_base64(FILE *src_stream, FILE *dst_stream, int flags) FAST_FUNC; 2366void read_base64(FILE *src_stream, FILE *dst_stream, int flags) FAST_FUNC;
2185 2367
2368int FAST_FUNC i2a64(int i);
2369int FAST_FUNC a2i64(char c);
2370char* FAST_FUNC num2str64_lsb_first(char *s, unsigned v, int n);
2371
2372enum {
2373 /* how many bytes XYZ_end() fills */
2374 MD5_OUTSIZE = 16,
2375 SHA1_OUTSIZE = 20,
2376 SHA256_OUTSIZE = 32,
2377 SHA384_OUTSIZE = 48,
2378 SHA512_OUTSIZE = 64,
2379 //SHA3-224_OUTSIZE = 28,
2380 /* size of input block */
2381 SHA2_INSIZE = 64,
2382};
2383
2384#if defined CONFIG_FEATURE_USE_CNG_API
2385struct bcrypt_hash_ctx_t {
2386 void *handle;
2387 void *hash_obj;
2388 unsigned int output_size;
2389};
2390typedef struct bcrypt_hash_ctx_t md5_ctx_t;
2391typedef struct bcrypt_hash_ctx_t sha1_ctx_t;
2392typedef struct bcrypt_hash_ctx_t sha256_ctx_t;
2393typedef struct bcrypt_hash_ctx_t sha384_ctx_t;
2394typedef struct bcrypt_hash_ctx_t sha512_ctx_t;
2395typedef struct sha3_ctx_t {
2396 uint64_t state[25];
2397 unsigned bytes_queued;
2398 unsigned input_block_bytes;
2399} sha3_ctx_t;
2400void md5_begin(struct bcrypt_hash_ctx_t *ctx) FAST_FUNC;
2401void sha1_begin(struct bcrypt_hash_ctx_t *ctx) FAST_FUNC;
2402void sha256_begin(struct bcrypt_hash_ctx_t *ctx) FAST_FUNC;
2403void sha384_begin(struct bcrypt_hash_ctx_t *ctx) FAST_FUNC;
2404void sha512_begin(struct bcrypt_hash_ctx_t *ctx) FAST_FUNC;
2405void generic_hash(struct bcrypt_hash_ctx_t *ctx, const void *buffer, size_t len) FAST_FUNC;
2406unsigned generic_end(struct bcrypt_hash_ctx_t *ctx, void *resbuf) FAST_FUNC;
2407# define md5_hash generic_hash
2408# define sha1_hash generic_hash
2409# define sha256_hash generic_hash
2410# define sha384_hash generic_hash
2411# define sha512_hash generic_hash
2412# define md5_end generic_end
2413# define sha1_end generic_end
2414# define sha256_end generic_end
2415# define sha384_end generic_end
2416# define sha512_end generic_end
2417#else
2186typedef struct md5_ctx_t { 2418typedef struct md5_ctx_t {
2187 uint8_t wbuffer[64]; /* always correctly aligned for uint64_t */ 2419 uint8_t wbuffer[64]; /* always correctly aligned for uint64_t */
2188 void (*process_block)(struct md5_ctx_t*) FAST_FUNC; 2420 void (*process_block)(struct md5_ctx_t*) FAST_FUNC;
@@ -2196,6 +2428,7 @@ typedef struct sha512_ctx_t {
2196 uint64_t hash[8]; 2428 uint64_t hash[8];
2197 uint8_t wbuffer[128]; /* always correctly aligned for uint64_t */ 2429 uint8_t wbuffer[128]; /* always correctly aligned for uint64_t */
2198} sha512_ctx_t; 2430} sha512_ctx_t;
2431typedef struct sha512_ctx_t sha384_ctx_t;
2199typedef struct sha3_ctx_t { 2432typedef struct sha3_ctx_t {
2200 uint64_t state[25]; 2433 uint64_t state[25];
2201 unsigned bytes_queued; 2434 unsigned bytes_queued;
@@ -2213,20 +2446,69 @@ void sha256_begin(sha256_ctx_t *ctx) FAST_FUNC;
2213void sha512_begin(sha512_ctx_t *ctx) FAST_FUNC; 2446void sha512_begin(sha512_ctx_t *ctx) FAST_FUNC;
2214void sha512_hash(sha512_ctx_t *ctx, const void *buffer, size_t len) FAST_FUNC; 2447void sha512_hash(sha512_ctx_t *ctx, const void *buffer, size_t len) FAST_FUNC;
2215unsigned sha512_end(sha512_ctx_t *ctx, void *resbuf) FAST_FUNC; 2448unsigned sha512_end(sha512_ctx_t *ctx, void *resbuf) FAST_FUNC;
2449void sha384_begin(sha384_ctx_t *ctx) FAST_FUNC;
2450#define sha384_hash sha512_hash
2451unsigned sha384_end(sha384_ctx_t *ctx, void *resbuf) FAST_FUNC;
2452#endif
2216void sha3_begin(sha3_ctx_t *ctx) FAST_FUNC; 2453void sha3_begin(sha3_ctx_t *ctx) FAST_FUNC;
2217void sha3_hash(sha3_ctx_t *ctx, const void *buffer, size_t len) FAST_FUNC; 2454void sha3_hash(sha3_ctx_t *ctx, const void *buffer, size_t len) FAST_FUNC;
2218unsigned sha3_end(sha3_ctx_t *ctx, void *resbuf) FAST_FUNC; 2455unsigned sha3_end(sha3_ctx_t *ctx, void *resbuf) FAST_FUNC;
2456void FAST_FUNC sha256_block(const void *in, size_t len, uint8_t hash[32]);
2219/* TLS benefits from knowing that sha1 and sha256 share these. Give them "agnostic" names too */ 2457/* TLS benefits from knowing that sha1 and sha256 share these. Give them "agnostic" names too */
2458#if defined CONFIG_FEATURE_USE_CNG_API
2459typedef struct bcrypt_hash_ctx_t md5sha_ctx_t;
2460#define md5sha_hash generic_hash
2461#define sha_end generic_end
2462#else
2220typedef struct md5_ctx_t md5sha_ctx_t; 2463typedef struct md5_ctx_t md5sha_ctx_t;
2221#define md5sha_hash md5_hash 2464#define md5sha_hash md5_hash
2222#define sha_end sha1_end 2465#define sha_end sha1_end
2223enum { 2466#endif
2224 MD5_OUTSIZE = 16, 2467
2225 SHA1_OUTSIZE = 20, 2468/* RFC 2104 HMAC (hash-based message authentication code) */
2226 SHA256_OUTSIZE = 32, 2469#if !ENABLE_FEATURE_USE_CNG_API
2227 SHA512_OUTSIZE = 64, 2470typedef struct hmac_ctx {
2228 SHA3_OUTSIZE = 28, 2471 md5sha_ctx_t hashed_key_xor_ipad;
2229}; 2472 md5sha_ctx_t hashed_key_xor_opad;
2473} hmac_ctx_t;
2474#else
2475typedef struct bcrypt_hash_ctx_t hmac_ctx_t;
2476#endif
2477#define HMAC_ONLY_SHA256 (!ENABLE_FEATURE_TLS_SHA1)
2478typedef void md5sha_begin_func(md5sha_ctx_t *ctx) FAST_FUNC;
2479#if !ENABLE_FEATURE_USE_CNG_API
2480#if HMAC_ONLY_SHA256
2481#define hmac_begin(ctx,key,key_size,begin) \
2482 hmac_begin(ctx,key,key_size)
2483#endif
2484void FAST_FUNC hmac_begin(hmac_ctx_t *ctx, const uint8_t *key, unsigned key_size, md5sha_begin_func *begin);
2485static ALWAYS_INLINE void hmac_hash(hmac_ctx_t *ctx, const void *in, size_t len)
2486{
2487 md5sha_hash(&ctx->hashed_key_xor_ipad, in, len);
2488}
2489#else
2490# if HMAC_ONLY_SHA256
2491# define hmac_begin(pre,key,key_size,begin) \
2492 _hmac_begin(pre, key, key_size, sha256_begin_hmac)
2493# else
2494# define hmac_begin _hmac_begin
2495# endif
2496void _hmac_begin(hmac_ctx_t *pre, uint8_t *key, unsigned key_size,
2497 BCRYPT_ALG_HANDLE alg_handle);
2498void hmac_uninit(hmac_ctx_t *pre);
2499#endif
2500unsigned FAST_FUNC hmac_end(hmac_ctx_t *ctx, uint8_t *out);
2501#if HMAC_ONLY_SHA256
2502#define hmac_block(key,key_size,begin,in,sz,out) \
2503 hmac_block(key,key_size,in,sz,out)
2504#endif
2505unsigned FAST_FUNC hmac_block(const uint8_t *key, unsigned key_size,
2506 md5sha_begin_func *begin,
2507 const void *in, unsigned sz,
2508 uint8_t *out);
2509/* HMAC helpers for TLS: */
2510void FAST_FUNC hmac_hash_v(hmac_ctx_t *ctx, va_list va);
2511unsigned hmac_peek_hash(hmac_ctx_t *ctx, uint8_t *out, ...);
2230 2512
2231extern uint32_t *global_crc32_table; 2513extern uint32_t *global_crc32_table;
2232uint32_t *crc32_filltable(uint32_t *tbl256, int endian) FAST_FUNC; 2514uint32_t *crc32_filltable(uint32_t *tbl256, int endian) FAST_FUNC;
@@ -2310,11 +2592,18 @@ extern const char bb_path_wtmp_file[] ALIGN1;
2310#define bb_path_motd_file "/etc/motd" 2592#define bb_path_motd_file "/etc/motd"
2311 2593
2312#define bb_dev_null "/dev/null" 2594#define bb_dev_null "/dev/null"
2595#if ENABLE_PLATFORM_MINGW32
2596#define bb_busybox_exec_path get_busybox_exec_path()
2597extern char bb_comm[];
2598extern char bb_command_line[];
2599#else
2313extern const char bb_busybox_exec_path[] ALIGN1; 2600extern const char bb_busybox_exec_path[] ALIGN1;
2601#endif
2314/* allow default system PATH to be extended via CFLAGS */ 2602/* allow default system PATH to be extended via CFLAGS */
2315#ifndef BB_ADDITIONAL_PATH 2603#ifndef BB_ADDITIONAL_PATH
2316#define BB_ADDITIONAL_PATH "" 2604#define BB_ADDITIONAL_PATH ""
2317#endif 2605#endif
2606#if !ENABLE_PLATFORM_MINGW32
2318#define BB_PATH_ROOT_PATH "PATH=/sbin:/usr/sbin:/bin:/usr/bin" BB_ADDITIONAL_PATH 2607#define BB_PATH_ROOT_PATH "PATH=/sbin:/usr/sbin:/bin:/usr/bin" BB_ADDITIONAL_PATH
2319extern const char bb_PATH_root_path[] ALIGN1; /* BB_PATH_ROOT_PATH */ 2608extern const char bb_PATH_root_path[] ALIGN1; /* BB_PATH_ROOT_PATH */
2320#define bb_default_root_path (bb_PATH_root_path + sizeof("PATH")) 2609#define bb_default_root_path (bb_PATH_root_path + sizeof("PATH"))
@@ -2322,6 +2611,23 @@ extern const char bb_PATH_root_path[] ALIGN1; /* BB_PATH_ROOT_PATH */
2322 * but I want to save a few bytes here: 2611 * but I want to save a few bytes here:
2323 */ 2612 */
2324#define bb_default_path (bb_PATH_root_path + sizeof("PATH=/sbin:/usr/sbin")) 2613#define bb_default_path (bb_PATH_root_path + sizeof("PATH=/sbin:/usr/sbin"))
2614#define PATH_SEP ':'
2615#define PATH_SEP_STR ":"
2616#else
2617#define BB_PATH_ROOT_PATH "PATH=C:/Windows/System32;C:/Windows" BB_ADDITIONAL_PATH
2618extern const char bb_PATH_root_path[] ALIGN1; /* BB_PATH_ROOT_PATH */
2619#define bb_default_root_path (bb_PATH_root_path + sizeof("PATH"))
2620#define bb_default_path (bb_PATH_root_path + sizeof("PATH"))
2621#define PATH_SEP ';'
2622#define PATH_SEP_STR ";"
2623extern const char bbvar[] ALIGN1;
2624#define bbafter(p) (p + sizeof(#p))
2625#define BB_OVERRIDE_APPLETS bbvar
2626#define BB_SKIP_ANSI_EMULATION bbafter(BB_OVERRIDE_APPLETS)
2627#define BB_TERMINAL_MODE bbafter(BB_SKIP_ANSI_EMULATION)
2628#define BB_SYSTEMROOT bbafter(BB_TERMINAL_MODE)
2629#define BB_CRITICAL_ERROR_DIALOGS bbafter(BB_SYSTEMROOT)
2630#endif
2325 2631
2326extern const int const_int_0; 2632extern const int const_int_0;
2327//extern const int const_int_1; 2633//extern const int const_int_1;