Codebase list libhx / e829867
Merge branch 'feature/upstream' into develop Jörg Frings-Fürst 1 year, 2 months ago
49 changed file(s) with 647 addition(s) and 317 deletion(s). Raw diff Collapse all Expand all
0 Permission is hereby granted, free of charge, to any person obtaining
1 a copy of this software and associated documentation files (the
2 "Software"), to deal in the Software without restriction, including
3 without limitation the rights to use, copy, modify, merge, publish,
4 distribute, sublicense, and/or sell copies of the Software, and to
5 permit persons to whom the Software is furnished to do so, subject to
6 the following conditions:
7
8 The above copyright notice and this permission notice (including the
9 next paragraph) shall be included in all copies or substantial
10 portions of the Software.
11
12 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
16 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
17 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 SOFTWARE.
+0
-14
LICENSE.WTFPL less more
0 DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
1 Version 2, December 2004
2
3 Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
4
5 Everyone is permitted to copy and distribute verbatim or modified
6 copies of this license document, and changing it is allowed as long
7 as the name is changed.
8
9 DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
10 TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
11
12 0. You just DO WHAT THE FUCK YOU WANT TO.
13
9292 /* Define to 1 if you have the <sys/types.h> header file. */
9393 #undef HAVE_SYS_TYPES_H
9494
95 /* Define to 1 if you have the <sys/un.h> header file. */
96 #undef HAVE_SYS_UN_H
97
9598 /* Define to 1 if you have the <unistd.h> header file. */
9699 #undef HAVE_UNISTD_H
97100
00 #! /bin/sh
11 # Guess values for system-dependent variables and create Makefiles.
2 # Generated by GNU Autoconf 2.71 for libHX 4.8.
2 # Generated by GNU Autoconf 2.71 for libHX 4.10.
33 #
44 #
55 # Copyright (C) 1992-1996, 1998-2017, 2020-2021 Free Software Foundation,
617617 # Identity of this package.
618618 PACKAGE_NAME='libHX'
619619 PACKAGE_TARNAME='libhx'
620 PACKAGE_VERSION='4.8'
621 PACKAGE_STRING='libHX 4.8'
620 PACKAGE_VERSION='4.10'
621 PACKAGE_STRING='libHX 4.10'
622622 PACKAGE_BUGREPORT=''
623623 PACKAGE_URL=''
624624
13741374 # Omit some internal or obsolete options to make the list less imposing.
13751375 # This message is too long to be a string in the A/UX 3.1 sh.
13761376 cat <<_ACEOF
1377 \`configure' configures libHX 4.8 to adapt to many kinds of systems.
1377 \`configure' configures libHX 4.10 to adapt to many kinds of systems.
13781378
13791379 Usage: $0 [OPTION]... [VAR=VALUE]...
13801380
14451445
14461446 if test -n "$ac_init_help"; then
14471447 case $ac_init_help in
1448 short | recursive ) echo "Configuration of libHX 4.8:";;
1448 short | recursive ) echo "Configuration of libHX 4.10:";;
14491449 esac
14501450 cat <<\_ACEOF
14511451
15601560 test -n "$ac_init_help" && exit $ac_status
15611561 if $ac_init_version; then
15621562 cat <<\_ACEOF
1563 libHX configure 4.8
1563 libHX configure 4.10
15641564 generated by GNU Autoconf 2.71
15651565
15661566 Copyright (C) 2021 Free Software Foundation, Inc.
21942194 This file contains any messages produced by compilers while
21952195 running configure, to aid debugging if configure makes a mistake.
21962196
2197 It was created by libHX $as_me 4.8, which was
2197 It was created by libHX $as_me 4.10, which was
21982198 generated by GNU Autoconf 2.71. Invocation command line was
21992199
22002200 $ $0$ac_configure_args_raw
36833683
36843684 # Define the identity of the package.
36853685 PACKAGE='libhx'
3686 VERSION='4.8'
3686 VERSION='4.10'
36873687
36883688
36893689 printf "%s\n" "#define PACKAGE \"$PACKAGE\"" >>confdefs.h
1759417594 printf "%s\n" "#define HAVE_SYS_RESOURCE_H 1" >>confdefs.h
1759517595
1759617596 fi
17597 ac_fn_c_check_header_compile "$LINENO" "sys/un.h" "ac_cv_header_sys_un_h" "$ac_includes_default"
17598 if test "x$ac_cv_header_sys_un_h" = xyes
17599 then :
17600 printf "%s\n" "#define HAVE_SYS_UN_H 1" >>confdefs.h
17601
17602 fi
1759717603
1759817604 if test "$ac_cv_header_dlfcn_h" = "yes"; then
1759917605 HAVE_DLFCN_H_TRUE=
1876418770 # report actual input values of CONFIG_FILES etc. instead of their
1876518771 # values after options handling.
1876618772 ac_log="
18767 This file was extended by libHX $as_me 4.8, which was
18773 This file was extended by libHX $as_me 4.10, which was
1876818774 generated by GNU Autoconf 2.71. Invocation command line was
1876918775
1877018776 CONFIG_FILES = $CONFIG_FILES
1883218838 cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
1883318839 ac_cs_config='$ac_cs_config_escaped'
1883418840 ac_cs_version="\\
18835 libHX config.status 4.8
18841 libHX config.status 4.10
1883618842 configured by $0, generated by GNU Autoconf 2.71,
1883718843 with options \\"\$ac_cs_config\\"
1883818844
0 AC_INIT([libHX], [4.8])
0 AC_INIT([libHX], [4.10])
11 AC_CONFIG_AUX_DIR([build-aux])
22 AC_CONFIG_HEADERS([config.h])
33 AC_CONFIG_MACRO_DIR([m4])
6464 AM_CONDITIONAL([WITH_GNU_LD], [test "$with_gnu_ld" = yes])
6565
6666 LIBS_system="$LIBS"
67 AC_CHECK_HEADERS([dlfcn.h sys/resource.h])
67 AC_CHECK_HEADERS([dlfcn.h sys/resource.h sys/un.h])
6868 AM_CONDITIONAL([HAVE_DLFCN_H], [test "$ac_cv_header_dlfcn_h" = "yes"])
6969 AC_SEARCH_LIBS([dlopen], [dl], [libdl_LIBS="$LIBS"; LIBS="$LIBS_system"])
7070 AC_SUBST([libdl_LIBS])
88 ====== ====== ====== ========================================
99 RMV MinVer FirstA Name
1010 ====== ====== ====== ========================================
11 4.9 4.9 4.9 HX_sockaddr_is_local
12 4.9 4.9 4.9 HX_ipaddr_is_local
1113 4.7 4.7 4.7 HXQUOTE_BASE64IMAP
1214 4.7 4.7 4.7 HXQUOTE_BASE64URL
1315 4.3 4.3 4.3 HX_unit_seconds
0 v4.10 (2023-01-29)
1 ==================
2
3 Fixes:
4
5 * format: plug a memory leak relating to func_entry_clone
6 * Resolve mingw build failure
7
8
9 v4.9 (2023-01-23)
10 =================
11
12 Enhancements:
13
14 * socket: add sockaddr_is_local, ipaddr_is_local functions
15
16 Fixes:
17
18 * format: avoid return value truncation from HXformat_aprintf, HXformat_sprintf
19 * format: avoid calling HXmc_length on a non-hxmc object
20 * format: add new variations of printf functions returning ssize_t
21 * Resolve Coverity-SCAN reports
22
23
024 v4.8 (2022-12-03)
125 =================
226
77
88 int HX_ffs(unsigned long z);
99 int HX_fls(unsigned long z);
10 void HX_hexdump(FILE *fp, const void *ptr, unsigned int len);
1110
1211 void HX_zvecfree(char **);
1312 unsigned int HX_zveclen(const char *const *);
2019 Finds the last (most-significant) bit in a value and returns its
2120 position, or ``-1`` to indicate failure.
2221
23 ``HX_hexdump``
24 Outputs a nice pretty-printed hex and ASCII dump to the filedescriptor
25 ``fp``. ``ptr`` is the memory area, of which ``len`` bytes will be
26 dumped.
27
2822 ``HX_zvecfree``
2923 Frees the supplied Z-vector array. (Frees all array elements from the
3024 first element to (excluding) the first ``NULL`` element.)
3226 ``HX_zveclen``
3327 Counts the number of array elements until the first ``NULL`` array
3428 element is seen, and returns this number.
29
30 .. code-block:: c
31
32 #include <libHX/io.h>
33 void HX_hexdump(FILE *fp, const void *ptr, unsigned int len);
34
35 ``HX_hexdump``
36 Outputs a nice pretty-printed hex and ASCII dump to the filedescriptor
37 ``fp``. ``ptr`` is the memory area, of which ``len`` bytes will be
38 dumped.
66 #include <libHX/socket.h>
77
88 int HX_socket_from_env(const struct addrinfo *ai, const char *intf);
9 int HX_sockaddr_is_local(const struct sockaddr *, socklen_t, unsigned int flags);
10 int HX_ipaddr_is_local(const char *, unsigned int flags);
911
1012 ``HX_socket_from_env``
1113 The function looks up the current process's file descriptors for a
1315 (optionally) intf if the latter is not NULL``. Upon success, the fd
1416 number is returned, or -1 if no file descriptor matched. No errors are
1517 signalled.
18
19 ``HX_sockaddr_is_local``
20 Attempts to determine if the given socket address refers to a local
21 address. This function may be helpful in determining whether a process
22 should spend any time (or not) on compressing data before sending to a
23 peer. The definition of "local" is generally dependent upon the network
24 APIs. The ``flags`` parameter can contain ``AI_V4MAPPED`` if
25 IPv4-mapped IPv6 addresses should be recognized. The function returns
26 >0 if the address is considered local, 0 if not, and any other
27 negative value for a system error that makes the result
28 indeterminate.
29
30 ``HX_ipaddr_is_local``
31 Takes a text representation of an IPv6/IPv4 address and, after
32 transformation, calls ``HX_sockaddr_is_local``. ``flags`` and
33 return value behave the same as that.
115115 Does substituion and directly outputs the expansion to the given stdio
116116 stream.
117117
118 On success, the length of the expanded string is returned, excluding the
119 trailing ``\0``. While ``HXformat_sprintf`` will not write more than ``size``
120 bytes (including the ``\0``), the length it would have taken is returned,
121 similar to what sprintf does. On error, ``-errno`` is returned.
118 On success, the length of the expanded string is returned (only up to a maximum
119 of SSIZE_MAX), excluding the trailing ``\0``. While ``HXformat_sprintf`` will
120 not write more than ``size`` bytes (including the ``\0``), the length it would
121 have taken is returned, similar to what sprintf does. On error, ``-errno`` is
122 returned.
122123
123124 The HXformat function family recognizes make-style like functions and recursive
124125 expansion, described below.
212212 # define container_of(v, s, m) containerof((v), s, m)
213213 #endif
214214
215 #ifdef _WIN32
215 #if !defined(_WIN32) || (defined(__USE_MINGW_ANSI_STDIO) && __USE_MINGW_ANSI_STDIO + 0 > 0)
216216 /*
217217 * Sufficiently old versions of the VC runtime do not even support %ll.
218218 */
219 # define HX_LONGLONG_FMT "ll"
220 # define HX_SIZET_FMT "z"
221 #else
219222 # define HX_LONGLONG_FMT "I64"
220223 # define HX_SIZET_FMT "I"
221 #else
222 # define HX_LONGLONG_FMT "ll"
223 # define HX_SIZET_FMT "z"
224224 #endif
225225
226226 #endif /* _LIBHX_DEFS_H */
00 #ifndef _LIBHX_IO_H
11 #define _LIBHX_IO_H 1
22
3 #include <stdio.h>
34 #include <sys/types.h>
45
56 #ifdef __cplusplus
4041
4142 extern ssize_t HXio_fullread(int, void *, size_t);
4243 extern ssize_t HXio_fullwrite(int, const void *, size_t);
44 #ifndef HX_HEXDUMP_DECLARATION
45 #define HX_HEXDUMP_DECLARATION 1
46 extern void HX_hexdump(FILE *, const void *, unsigned int);
47 #endif
4348
4449 #ifdef __cplusplus
4550 } /* extern "C" */
5858 */
5959 extern int HX_ffs(unsigned long);
6060 extern int HX_fls(unsigned long);
61 #ifndef HX_HEXDUMP_DECLARATION
62 #define HX_HEXDUMP_DECLARATION 1
6163 extern void HX_hexdump(FILE *, const void *, unsigned int);
64 #endif
6265 extern bool HX_timespec_isneg(const struct timespec *);
6366 extern struct timespec *HX_timespec_neg(struct timespec *,
6467 const struct timespec *);
2626 extern void HXformat_free(struct HXformat_map *);
2727 extern int HXformat_add(struct HXformat_map *, const char *, const void *,
2828 unsigned int);
29 extern int HXformat_aprintf(const struct HXformat_map *,
30 hxmc_t **, const char *);
31 extern int HXformat_sprintf(const struct HXformat_map *,
32 char *, size_t, const char *);
33 extern int HXformat_fprintf(const struct HXformat_map *,
34 FILE *, const char *);
29 #define HXformat_aprintf(a, b, c) HXformat3_aprintf((a), (b), (c))
30 #define HXformat_fprintf(a, b, c) HXformat3_fprintf((a), (b), (c))
31 #define HXformat_sprintf(a, b, c, d) HXformat3_sprintf((a), (b), (c), (d))
32 extern ssize_t HXformat3_aprintf(const struct HXformat_map *, hxmc_t **, const char *);
33 extern ssize_t HXformat3_fprintf(const struct HXformat_map *, FILE *, const char *);
34 extern ssize_t HXformat3_sprintf(const struct HXformat_map *, char *, size_t, const char *);
3535
3636 /*
3737 * OPT.C
44 # include <ws2tcpip.h>
55 #else
66 # include <netdb.h>
7 # include <sys/socket.h>
78 #endif
89
910 #ifdef __cplusplus
1112 #endif
1213
1314 extern int HX_socket_from_env(const struct addrinfo *, const char *intf);
15 extern int HX_sockaddr_is_local(const struct sockaddr *, socklen_t, unsigned int flags);
16 extern int HX_ipaddr_is_local(const char *, unsigned int flags);
1417
1518 #ifdef __cplusplus
1619 } /* extern "C" */
1818 /t?-rand
1919 /t?-realpath
2020 /t?-shconfig
21 /t?-socket
2122 /t?-strchr2
2223 /t?-string
2324 /t?-strquote
1212 mc.c misc.c opt.c proc.c \
1313 rand.c socket.c string.c time.c
1414 libHX_la_LIBADD = ${libdl_LIBS} -lm ${libpthread_LIBS} ${librt_LIBS}
15 libHX_la_LDFLAGS = -no-undefined -version-info 35:0:3
15 libHX_la_LDFLAGS = -no-undefined -version-info 36:0:4
1616 if WITH_GNU_LD
1717 libHX_la_LDFLAGS += -Wl,--version-script=${srcdir}/libHX.map
1818 endif
3535 check_PROGRAMS = tc-compile tc-cast tc-deque tc-dir tc-format tc-io \
3636 tc-list tc-list2 tc-map tc-memmem tc-misc tc-netio \
3737 tc-option tc-proc tc-rand tc-realpath \
38 tc-shconfig tc-strchr2 tc-string tc-strquote \
38 tc-shconfig tc-socket tc-strchr2 tc-string tc-strquote \
3939 tc-switchuser tc-time
40 TESTS = tc-strchr2 tc-strquote
40 TESTS = tc-format tc-strchr2 tc-strquote
4141 tc_cast_CFLAGS = ${AM_CFLAGS} -std=gnu99
4242 tc_cast_LDADD = libHX.la -lm
4343 tc_compile_LDADD = libHX.la
5656 tc_rand_LDADD = libHX.la
5757 tc_realpath_LDADD = libHX.la
5858 tc_shconfig_LDADD = libHX.la
59 tc_socket_LDADD = libHX.la
5960 tc_strchr2_LDADD = libHX.la
6061 tc_string_LDADD = libHX.la
6162 tc_strquote_LDADD = libHX.la
100100 tc-map$(EXEEXT) tc-memmem$(EXEEXT) tc-misc$(EXEEXT) \
101101 tc-netio$(EXEEXT) tc-option$(EXEEXT) tc-proc$(EXEEXT) \
102102 tc-rand$(EXEEXT) tc-realpath$(EXEEXT) tc-shconfig$(EXEEXT) \
103 tc-strchr2$(EXEEXT) tc-string$(EXEEXT) tc-strquote$(EXEEXT) \
104 tc-switchuser$(EXEEXT) tc-time$(EXEEXT) $(am__EXEEXT_1)
105 TESTS = tc-strchr2$(EXEEXT) tc-strquote$(EXEEXT) $(am__EXEEXT_2)
103 tc-socket$(EXEEXT) tc-strchr2$(EXEEXT) tc-string$(EXEEXT) \
104 tc-strquote$(EXEEXT) tc-switchuser$(EXEEXT) tc-time$(EXEEXT) \
105 $(am__EXEEXT_1)
106 TESTS = tc-format$(EXEEXT) tc-strchr2$(EXEEXT) tc-strquote$(EXEEXT) \
107 $(am__EXEEXT_2)
106108 @HAVE_CXX_TRUE@am__append_6 = tx-compile tx-cast tx-deque tx-dir \
107109 @HAVE_CXX_TRUE@ tx-intdiff tx-list tx-list2 \
108110 @HAVE_CXX_TRUE@ tx-misc tx-netio \
243245 tc_shconfig_SOURCES = tc-shconfig.c
244246 tc_shconfig_OBJECTS = tc-shconfig.$(OBJEXT)
245247 tc_shconfig_DEPENDENCIES = libHX.la
248 tc_socket_SOURCES = tc-socket.c
249 tc_socket_OBJECTS = tc-socket.$(OBJEXT)
250 tc_socket_DEPENDENCIES = libHX.la
246251 tc_strchr2_SOURCES = tc-strchr2.c
247252 tc_strchr2_OBJECTS = tc-strchr2.$(OBJEXT)
248253 tc_strchr2_DEPENDENCIES = libHX.la
353358 ./$(DEPDIR)/tc-netio.Po ./$(DEPDIR)/tc-option.Po \
354359 ./$(DEPDIR)/tc-proc.Po ./$(DEPDIR)/tc-rand.Po \
355360 ./$(DEPDIR)/tc-realpath.Po ./$(DEPDIR)/tc-shconfig.Po \
356 ./$(DEPDIR)/tc-strchr2.Po ./$(DEPDIR)/tc-string.Po \
357 ./$(DEPDIR)/tc-strquote.Po ./$(DEPDIR)/tc-switchuser.Po \
358 ./$(DEPDIR)/tc-time.Po ./$(DEPDIR)/tc_cast-tc-cast.Po \
361 ./$(DEPDIR)/tc-socket.Po ./$(DEPDIR)/tc-strchr2.Po \
362 ./$(DEPDIR)/tc-string.Po ./$(DEPDIR)/tc-strquote.Po \
363 ./$(DEPDIR)/tc-switchuser.Po ./$(DEPDIR)/tc-time.Po \
364 ./$(DEPDIR)/tc_cast-tc-cast.Po \
359365 ./$(DEPDIR)/tc_list2-tc-list2.Po ./$(DEPDIR)/time.Plo \
360366 ./$(DEPDIR)/tx-cast.Po ./$(DEPDIR)/tx-compile.Po \
361367 ./$(DEPDIR)/tx-deque.Po ./$(DEPDIR)/tx-dir.Po \
407413 tc-compile.c tc-deque.c tc-dir.c tc-format.c tc-io.c tc-list.c \
408414 tc-list2.c tc-map.c tc-memmem.c tc-misc.c tc-netio.c \
409415 tc-option.c tc-proc.c tc-rand.c tc-realpath.c tc-shconfig.c \
410 tc-strchr2.c tc-string.c tc-strquote.c tc-switchuser.c \
411 tc-time.c $(tx_cast_SOURCES) $(tx_compile_SOURCES) \
412 $(tx_deque_SOURCES) $(tx_dir_SOURCES) $(tx_intdiff_SOURCES) \
413 $(tx_list_SOURCES) $(tx_list2_SOURCES) $(tx_misc_SOURCES) \
414 $(tx_netio_SOURCES) $(tx_option_SOURCES) $(tx_proc_SOURCES) \
415 $(tx_rand_SOURCES) $(tx_strchr2_SOURCES) $(tx_string_SOURCES) \
416 $(tx_strquote_SOURCES) $(tx_time_SOURCES)
416 tc-socket.c tc-strchr2.c tc-string.c tc-strquote.c \
417 tc-switchuser.c tc-time.c $(tx_cast_SOURCES) \
418 $(tx_compile_SOURCES) $(tx_deque_SOURCES) $(tx_dir_SOURCES) \
419 $(tx_intdiff_SOURCES) $(tx_list_SOURCES) $(tx_list2_SOURCES) \
420 $(tx_misc_SOURCES) $(tx_netio_SOURCES) $(tx_option_SOURCES) \
421 $(tx_proc_SOURCES) $(tx_rand_SOURCES) $(tx_strchr2_SOURCES) \
422 $(tx_string_SOURCES) $(tx_strquote_SOURCES) $(tx_time_SOURCES)
417423 DIST_SOURCES = $(am__libHX_la_SOURCES_DIST) \
418424 $(libHX_rtcheck_la_SOURCES) tc-cast.c tc-compile.c tc-deque.c \
419425 tc-dir.c tc-format.c tc-io.c tc-list.c tc-list2.c tc-map.c \
420426 tc-memmem.c tc-misc.c tc-netio.c tc-option.c tc-proc.c \
421 tc-rand.c tc-realpath.c tc-shconfig.c tc-strchr2.c tc-string.c \
422 tc-strquote.c tc-switchuser.c tc-time.c \
427 tc-rand.c tc-realpath.c tc-shconfig.c tc-socket.c tc-strchr2.c \
428 tc-string.c tc-strquote.c tc-switchuser.c tc-time.c \
423429 $(am__tx_cast_SOURCES_DIST) $(am__tx_compile_SOURCES_DIST) \
424430 $(am__tx_deque_SOURCES_DIST) $(am__tx_dir_SOURCES_DIST) \
425431 $(am__tx_intdiff_SOURCES_DIST) $(am__tx_list_SOURCES_DIST) \
773779 proc.c rand.c socket.c string.c time.c $(am__append_3)
774780 libHX_la_LIBADD = ${libdl_LIBS} -lm ${libpthread_LIBS} ${librt_LIBS} \
775781 $(am__append_4)
776 libHX_la_LDFLAGS = -no-undefined -version-info 35:0:3 $(am__append_2)
782 libHX_la_LDFLAGS = -no-undefined -version-info 36:0:4 $(am__append_2)
777783 EXTRA_libHX_la_DEPENDENCIES = libHX.map
778784 libHX_rtcheck_la_SOURCES = rtcheck.c
779785 libHX_rtcheck_la_LIBADD = ${libdl_LIBS}
798804 tc_rand_LDADD = libHX.la
799805 tc_realpath_LDADD = libHX.la
800806 tc_shconfig_LDADD = libHX.la
807 tc_socket_LDADD = libHX.la
801808 tc_strchr2_LDADD = libHX.la
802809 tc_string_LDADD = libHX.la
803810 tc_strquote_LDADD = libHX.la
986993 @rm -f tc-shconfig$(EXEEXT)
987994 $(AM_V_CCLD)$(LINK) $(tc_shconfig_OBJECTS) $(tc_shconfig_LDADD) $(LIBS)
988995
996 tc-socket$(EXEEXT): $(tc_socket_OBJECTS) $(tc_socket_DEPENDENCIES) $(EXTRA_tc_socket_DEPENDENCIES)
997 @rm -f tc-socket$(EXEEXT)
998 $(AM_V_CCLD)$(LINK) $(tc_socket_OBJECTS) $(tc_socket_LDADD) $(LIBS)
999
9891000 tc-strchr2$(EXEEXT): $(tc_strchr2_OBJECTS) $(tc_strchr2_DEPENDENCIES) $(EXTRA_tc_strchr2_DEPENDENCIES)
9901001 @rm -f tc-strchr2$(EXEEXT)
9911002 $(AM_V_CCLD)$(LINK) $(tc_strchr2_OBJECTS) $(tc_strchr2_LDADD) $(LIBS)
11041115 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/tc-rand.Po@am__quote@ # am--include-marker
11051116 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/tc-realpath.Po@am__quote@ # am--include-marker
11061117 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/tc-shconfig.Po@am__quote@ # am--include-marker
1118 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/tc-socket.Po@am__quote@ # am--include-marker
11071119 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/tc-strchr2.Po@am__quote@ # am--include-marker
11081120 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/tc-string.Po@am__quote@ # am--include-marker
11091121 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/tc-strquote.Po@am__quote@ # am--include-marker
14261438 am__force_recheck=am--force-recheck \
14271439 TEST_LOGS="$$log_list"; \
14281440 exit $$?
1441 tc-format.log: tc-format$(EXEEXT)
1442 @p='tc-format$(EXEEXT)'; \
1443 b='tc-format'; \
1444 $(am__check_pre) $(LOG_DRIVER) --test-name "$$f" \
1445 --log-file $$b.log --trs-file $$b.trs \
1446 $(am__common_driver_flags) $(AM_LOG_DRIVER_FLAGS) $(LOG_DRIVER_FLAGS) -- $(LOG_COMPILE) \
1447 "$$tst" $(AM_TESTS_FD_REDIRECT)
14291448 tc-strchr2.log: tc-strchr2$(EXEEXT)
14301449 @p='tc-strchr2$(EXEEXT)'; \
14311450 b='tc-strchr2'; \
15791598 -rm -f ./$(DEPDIR)/tc-rand.Po
15801599 -rm -f ./$(DEPDIR)/tc-realpath.Po
15811600 -rm -f ./$(DEPDIR)/tc-shconfig.Po
1601 -rm -f ./$(DEPDIR)/tc-socket.Po
15821602 -rm -f ./$(DEPDIR)/tc-strchr2.Po
15831603 -rm -f ./$(DEPDIR)/tc-string.Po
15841604 -rm -f ./$(DEPDIR)/tc-strquote.Po
16781698 -rm -f ./$(DEPDIR)/tc-rand.Po
16791699 -rm -f ./$(DEPDIR)/tc-realpath.Po
16801700 -rm -f ./$(DEPDIR)/tc-shconfig.Po
1701 -rm -f ./$(DEPDIR)/tc-socket.Po
16811702 -rm -f ./$(DEPDIR)/tc-strchr2.Po
16821703 -rm -f ./$(DEPDIR)/tc-string.Po
16831704 -rm -f ./$(DEPDIR)/tc-strquote.Po
1515 #include <unistd.h>
1616 #include <libHX.h>
1717 #include "internal.h"
18 #undef HXformat_aprintf
19 #undef HXformat_fprintf
20 #undef HXformat_sprintf
21 extern int HXformat_aprintf(const struct HXformat_map *, hxmc_t **, const char *);
22 extern int HXformat_sprintf(const struct HXformat_map *, char *, size_t, const char *);
23 extern int HXformat_fprintf(const struct HXformat_map *, FILE *, const char *);
1824
1925 /* To make it easier on the highlighter */
2026 #define C_OPEN '('
96102
97103 static const struct HXmap_ops func_entry_ops = {
98104 .d_clone = func_entry_clone,
105 .d_free = free,
99106 };
100107
101108 EXPORT_SYMBOL void HXformat_free(struct HXformat_map *blk)
296303 static hxmc_t *HXformat2_substr(int argc, const hxmc_t *const *argv,
297304 const struct HXformat_map *blk)
298305 {
299 ssize_t offset, length, z;
300306 hxmc_t *ret;
301307 char *end;
302308
305311 return &HXformat2_nexp;
306312 }
307313
308 offset = strtoll(argv[1], &end, 0);
314 long w = LONG_MAX, v = strtol(argv[1], &end, 0);
309315 if (*end != '\0') {
310316 fprintf(stderr, "HXformat2-substr: found garbage in "
311317 "offset specification\n");
312318 return &HXformat2_nexp;
313319 }
314
315 z = strlen(argv[0]);
316 if (offset < 0)
317 offset = z + offset;
318 if (offset >= z)
319 return &HXformat2_nexp;
320
321 if (argc == 2) {
322 if (offset < 0)
323 offset = 0;
324 length = z - offset;
325 } else {
326 length = strtoll(argv[2], &end, 0);
320 if (argc >= 3) {
321 w = strtol(argv[2], &end, 0);
327322 if (*end != '\0') {
328323 fprintf(stderr, "HXformat2-substr; found garbage in "
329324 "length specification\n");
330325 return &HXformat2_nexp;
331326 }
332 if (length < 0)
333 length/*end*/ = z + length;
334 else
335 length/*end*/ = offset + length;
336 if (offset < 0)
337 offset = 0;
338 }
339 if (length <= 0)
340 return &HXformat2_nexp;
341
342 ret = HXmc_meminit(NULL, length);
327 }
328 size_t start = 0, tocopy = HX_substr_helper(strlen(argv[0]), v, w, &start);
329 if (tocopy == 0)
330 return &HXformat2_nexp;
331 ret = HXmc_meminit(NULL, tocopy);
343332 if (ret == NULL)
344333 return &HXformat2_nexp;
345 if (HXmc_memcpy(&ret, &argv[0][offset], length) == NULL) {
334 if (HXmc_memcpy(&ret, &argv[0][start], tocopy) == NULL) {
346335 HXmc_free(ret);
347336 return &HXformat2_nexp;
348337 }
642631 EXPORT_SYMBOL int HXformat_aprintf(const struct HXformat_map *blk,
643632 hxmc_t **resultp, const char *fmt)
644633 {
634 ssize_t ret = HXformat3_aprintf(blk, resultp, fmt);
635 return ret > INT_MAX ? INT_MAX : ret;
636 }
637
638 EXPORT_SYMBOL ssize_t HXformat3_aprintf(const struct HXformat_map *blk,
639 hxmc_t **resultp, const char *fmt)
640 {
645641 hxmc_t *ex, *ts, *out;
646642 const char *current;
647643 int ret = 0;
678674 }
679675
680676 *resultp = out;
681 return HXmc_length(out);
677 size_t xl = HXmc_length(out);
678 return xl > SSIZE_MAX ? SSIZE_MAX : xl;
682679
683680 out:
684681 ret = -errno;
689686 EXPORT_SYMBOL int HXformat_fprintf(const struct HXformat_map *ftable,
690687 FILE *filp, const char *fmt)
691688 {
689 ssize_t ret = HXformat3_fprintf(ftable, filp, fmt);
690 return ret > INT_MAX ? INT_MAX : ret;
691 }
692
693 EXPORT_SYMBOL ssize_t HXformat3_fprintf(const struct HXformat_map *ftable,
694 FILE *filp, const char *fmt)
695 {
692696 hxmc_t *str;
693 int ret;
694
695 if ((ret = HXformat_aprintf(ftable, &str, fmt)) <= 0)
697 ssize_t ret;
698
699 if ((ret = HXformat3_aprintf(ftable, &str, fmt)) <= 0)
696700 return ret;
697701 errno = 0;
698702 if (fputs(str, filp) < 0)
704708 EXPORT_SYMBOL int HXformat_sprintf(const struct HXformat_map *ftable,
705709 char *dest, size_t size, const char *fmt)
706710 {
711 ssize_t ret = HXformat3_sprintf(ftable, dest, size, fmt);
712 return ret > INT_MAX ? INT_MAX : ret;
713 }
714
715 EXPORT_SYMBOL ssize_t HXformat3_sprintf(const struct HXformat_map *ftable,
716 char *dest, size_t size, const char *fmt)
717 {
707718 hxmc_t *str;
708 int ret;
719 ssize_t ret;
709720
710721 if ((ret = HXformat_aprintf(ftable, &str, fmt)) < 0)
711722 return ret;
714725 return 0;
715726 }
716727 strncpy(dest, str, size);
717 ret = HXmc_length(dest);
728 size_t xl = strlen(dest);
718729 HXmc_free(str);
719 return ret;
720 }
730 return xl > SSIZE_MAX ? SSIZE_MAX : xl;
731 }
99 #define LIBHX_INTERNAL_H 1
1010
1111 #include "config.h"
12 #include <stdint.h>
1213 #include <libHX/defs.h>
1314 #include <libHX/string.h>
1415
5556 struct timeval;
5657
5758 extern hxmc_t *HXparse_dequote_fmt(const char *, const char *, const char **);
59 extern size_t HX_substr_helper(size_t, long, long, size_t *);
5860
5961 #endif /* LIBHX_INTERNAL_H */
5656 static int mkdir_gen(const char *d, unsigned int mode)
5757 {
5858 struct stat sb;
59 if (lstat(d, &sb) < 0) {
6059 #if defined(_WIN32)
61 if (mkdir(d) < 0)
62 #else
63 if (mkdir(d, mode) < 0) /* use umask() for permissions */
64 #endif
65 return -errno;
66 } else {
60 if (mkdir(d) == 0)
61 #else
62 if (mkdir(d, mode) == 0) /* use umask() for permissions */
63 #endif
64 return 1;
65 if (errno != EEXIST)
66 return -errno;
67 if (lstat(d, &sb) == 0) {
6768 #if defined(_WIN32)
68 if ((sb.st_mode & S_IFDIR) != S_IFDIR)
69 #else
70 if (!S_ISDIR(sb.st_mode))
71 #endif
72 return -errno;
73 }
74 return 1;
69 if (sb.st_mode & S_IFDIR)
70 #else
71 if (S_ISDIR(sb.st_mode))
72 #endif
73 return 0;
74 }
75 return -EEXIST;
7576 }
7677
7778 EXPORT_SYMBOL struct HXdir *HXdir_open(const char *s)
203204 unlink(dest);
204205 close(dstfd);
205206 close(srcfd);
207 free(buf);
208 va_end(argp);
206209 return -(errno = saved_errno);
207210 }
208211 uid = sb.st_uid;
215218 unlink(dest);
216219 close(dstfd);
217220 close(srcfd);
221 free(buf);
222 va_end(argp);
218223 return -(errno = saved_errno);
219224 }
220225 va_end(argp);
228233 int saved_errno = errno;
229234 close(srcfd);
230235 close(dstfd);
236 free(buf);
231237 return -(errno = saved_errno);
232238 }
233239 }
263269 continue;
264270 snprintf(fsrc, MAXFNLEN, "%s/%s", src, fn);
265271 snprintf(fdest, MAXFNLEN, "%s/%s", dest, fn);
266
267 lstat(fsrc, &sb);
272 if (lstat(fsrc, &sb) < 0)
273 continue;
268274 sb.st_mode &= 0777; /* clear SUID/GUID/Sticky bits */
269275
270276 if (S_ISREG(sb.st_mode)) {
277283 memset(pt, '\0', MAXFNLEN);
278284 if (readlink(fsrc, pt, MAXFNLEN - 1) < MAXFNLEN - 1)
279285 if (symlink(pt, fdest) < 0)
280 {};
286 /* ignore */;
281287 } else if (S_ISBLK(sb.st_mode) || S_ISCHR(sb.st_mode)) {
282 mknod(fdest, sb.st_mode, sb.st_dev);
288 if (mknod(fdest, sb.st_mode, sb.st_dev) < 0)
289 /* ignore */;
283290 } else if (S_ISFIFO(sb.st_mode)) {
284 mkfifo(fdest, sb.st_mode);
291 if (mkfifo(fdest, sb.st_mode) < 0)
292 /* ignore */;
285293 }
286294
287295 if (lchown(fdest, uid, gid) < 0)
288 {};
296 /* ignore */;
289297 if (!S_ISLNK(sb.st_mode))
290 chmod(fdest, sb.st_mode);
298 if (chmod(fdest, sb.st_mode) < 0)
299 /* ignore */;
291300 }
292301
293302 HXdir_close(dt);
320329 if (dir[i] == '/') {
321330 strncpy(buf, dir, i);
322331 buf[i] = '\0';
323 if ((v = mkdir_gen(buf, mode)) <= 0)
332 if ((v = mkdir_gen(buf, mode)) < 0)
324333 return v;
325334 } else if (i == len - 1) {
326335 strncpy(buf, dir, len);
327336 buf[len] = '\0';
328 if ((v = mkdir_gen(buf, mode)) <= 0)
337 if ((v = mkdir_gen(buf, mode)) < 0)
329338 return v;
330339 }
331340 }
357366 return -(errno = saved_errno);
358367 }
359368 if (static_cast(size_t, ret) < linkbuf_size) {
360 HXmc_setlen(target, ret);
369 (*target)[ret] = '\0'; // please cov-scan
370 HXmc_setlen(target, ret); // \0 set here anyway
361371 return ret;
362372 }
363373 linkbuf_size *= 2;
499509 ret = HX_realpath_symres(&state, path);
500510 if (ret == -EINVAL)
501511 continue;
502 else if (ret < 0)
512 else if (ret <= 0)
503513 goto out;
504514 path = state.path;
505515 }
667677 xferd += ret;
668678 count -= ret;
669679 }
680 free(buf);
670681 if (xferd > 0)
671682 return xferd;
672683 if (ret < 0)
149149 HX_unit_seconds;
150150 HX_strtoull_sec;
151151 } LIBHX_4.2;
152
153 LIBHX_4.9 {
154 global:
155 HXformat3_aprintf;
156 HXformat3_fprintf;
157 HXformat3_sprintf;
158 HX_ipaddr_is_local;
159 HX_sockaddr_is_local;
160 } LIBHX_4.3;
13031303 trav->current = trav->path[--trav->depth];
13041304 if (trav->current == NULL)
13051305 fprintf(stderr, "btrav_rewalk: problem: current==NULL\n");
1306 HXrbtrav_checkpoint(trav, trav->current);
1306 else
1307 HXrbtrav_checkpoint(trav, trav->current);
13071308 }
13081309
13091310 trav->tid = btree->tid;
1310 if (go_next)
1311 if (trav->current != nullptr && go_next)
13111312 return HXrbtrav_next(trav);
13121313 else
13131314 return trav->current;
77 * either version 2.1 or (at your option) any later version.
88 */
99 #include <stddef.h>
10 #include <stdint.h>
1011 #include <stdio.h>
1112 #include <stdlib.h>
1213 #include <string.h>
1920 return sizeof(struct memcont) + len + 1;
2021 }
2122
22 static __inline__ void HXmc_check(const struct memcont *c)
23 {
24 if (c->id != HXMC_IDENT)
23 static __inline__ int HXmc_check(const struct memcont *c)
24 {
25 int err = c->id != HXMC_IDENT;
26 if (err != 0)
2527 fprintf(stderr, "libHX-mc error: not a hxmc object!\n");
28 return err;
2629 }
2730
2831 static __inline__ struct memcont *HXmc_base(const hxmc_t *p)
6063 struct memcont *ctx;
6164 if (*vp != NULL) {
6265 ctx = HXmc_base(*vp);
63 HXmc_check(ctx);
66 if (HXmc_check(ctx) != 0)
67 return nullptr;
6468 if (ctx->alloc < len) {
6569 ctx = realloc(ctx, __HXmc_request(len));
6670 if (ctx == NULL)
9599 if (vp == NULL)
96100 return 0;
97101 ctx = HXmc_base(vp);
98 HXmc_check(ctx);
102 if (HXmc_check(ctx) != 0)
103 return SIZE_MAX;
99104 return ctx->length;
100105 }
101106
114119 {
115120 struct memcont *ctx = HXmc_base(*vp);
116121
117 HXmc_check(ctx);
122 if (HXmc_check(ctx) != 0)
123 return nullptr;
118124 if (len > ctx->alloc) {
119125 ctx = realloc(ctx, __HXmc_request(len));
120126 if (ctx == NULL)
140146 struct memcont *ctx = HXmc_base(*vp);
141147 size_t nl = ctx->length + len;
142148
143 HXmc_check(ctx);
149 if (HXmc_check(ctx) != 0)
150 return nullptr;
144151 if (nl > ctx->alloc) {
145152 ctx = realloc(ctx, __HXmc_request(nl));
146153 if (ctx == NULL)
190197 struct memcont *ctx = HXmc_base(*vp);
191198 size_t nl = ctx->length + len;
192199
193 HXmc_check(ctx);
200 if (HXmc_check(ctx) != 0)
201 return nullptr;
194202 if (ctx->alloc < nl) {
195203 ctx = realloc(ctx, __HXmc_request(nl));
196204 if (ctx == NULL)
211219 EXPORT_SYMBOL hxmc_t *HXmc_memdel(hxmc_t *vp, size_t pos, size_t len)
212220 {
213221 struct memcont *ctx = HXmc_base(vp);
214 HXmc_check(ctx);
215
222 if (HXmc_check(ctx) != 0)
223 return nullptr;
216224 if (pos + len > ctx->length)
217225 len = ctx->length - pos;
218226
229237 if (vp == NULL)
230238 return;
231239 ctx = HXmc_base(vp);
232 HXmc_check(ctx);
240 if (HXmc_check(ctx) != 0)
241 return;
233242 free(ctx);
234243 }
235244
546546 key = HX_strdup(cur);
547547 if (key == NULL)
548548 return -errno;
549
550549 value = strchr(key, '=');
550 if (value == nullptr) {
551 /* Cannot happen because state is always !S_TWOLONG */
552 free(key);
553 return -EINVAL;
554 }
551555 *value++ = '\0';
552556 par->cbi.current = lookup_long_pfx(par->cbi.table, key + 2);
553557 if (par->cbi.current == &HXopt_ambig_prefix) {
698702 struct HX_getopt_vars ps;
699703 const char **opt = *argv;
700704 int state = HXOPT_S_NORMAL;
701 int ret = HXOPT_ERR_SUCCESS;
705 int ret = -ENOMEM;
702706 unsigned int argk;
703707 const char *cur;
704708
705709 memset(&ps, 0, sizeof(ps));
706710 ps.remaining = HXdeque_init();
707 if (ps.remaining == NULL)
711 if (ps.remaining == NULL) {
712 ret = -errno;
708713 goto out;
714 }
709715 ps.flags = flags;
710716 ps.arg0 = **argv;
711717 ps.cbi.table = table;
713719 if (*opt != NULL) {
714720 /* put argv[0] back */
715721 char *arg = HX_strdup(*opt++);
716 if (arg == NULL)
717 goto out_errno;
722 if (arg == NULL) {
723 ret = -errno;
724 goto out;
725 }
718726 if (HXdeque_push(ps.remaining, arg) == NULL) {
719727 free(arg);
720 goto out_errno;
728 ret = -errno;
729 goto out;
721730 }
722731 }
723732
724733 if (posix_me_harder())
725734 ps.flags |= HXOPT_RQ_ORDER;
726
727735 for (cur = *opt; cur != NULL; ) {
728736 if (state == HXOPT_S_TWOLONG)
729737 state = HX_getopt_twolong(opt, &ps);
738746
739747 if (state < 0) {
740748 ret = state;
741 break;
749 goto out;
742750 }
743751 if (state & HXOPT_I_ERROR) {
744752 ret = state & ~HXOPT_I_ERROR;
745 break;
753 goto out;
746754 }
747755 if (state & HXOPT_I_ASSIGN)
748756 do_assign(&ps.cbi, ps.arg0);
755763 state &= ~HXOPT_I_MASK;
756764 }
757765
758 out:
759 if (ret == HXOPT_ERR_SUCCESS) {
760 if (!(ps.flags & HXOPT_KEEP_ARGV)) {
766 if (!(ps.flags & HXOPT_KEEP_ARGV)) {
761767 const char **nvec = reinterpret_cast(const char **,
762768 HXdeque_to_vec(ps.remaining, &argk));
763 if (nvec == NULL)
764 goto out_errno;
769 if (nvec == NULL) {
770 ret = -errno;
771 goto out;
772 }
765773 if (ps.flags & HXOPT_DESTROY_OLD)
766774 /*
767775 * Only the "true, original" argv is stored on the
779787 /* pointers are owned by nvec/argv now */
780788 HXdeque_free(ps.remaining);
781789 ps.remaining = nullptr;
782 }
790 }
791 ret = HXOPT_ERR_SUCCESS;
792
793 out:
794 if (ret == HXOPT_ERR_SUCCESS) {
783795 } else if (ret < 0) {
784796 if (!(ps.flags & HXOPT_QUIET))
785797 fprintf(stderr, "%s: %s\n", __func__, strerror(errno));
793805 if (ps.remaining != nullptr)
794806 HXdeque_genocide2(ps.remaining, free);
795807 return ret;
796
797 out_errno:
798 ret = -errno;
799 goto out;
800808 }
801809
802810 EXPORT_SYMBOL void HX_getopt_help(const struct HXoptcb *cbi, FILE *nfp)
1616 #ifdef _WIN32
1717 # include <ws2tcpip.h>
1818 #else
19 # include <netdb.h>
20 # include <unistd.h>
21 # include <netinet/in.h>
1922 # include <sys/socket.h>
20 # include <netdb.h>
23 #endif
24 #ifdef HAVE_SYS_UN_H
25 # include <sys/un.h>
26 #endif
27 #ifdef __linux__
28 # include <linux/rtnetlink.h>
29 #endif
30 #ifdef __OpenBSD__
31 # include <net/route.h>
2132 #endif
2233 #include <libHX/proc.h>
2334 #include <libHX/socket.h>
2940 #endif
3041 #if defined(__sunos__) && !defined(SO_PROTOCOL)
3142 # define SO_PROTOCOL SO_PROTOTYPE
43 #endif
44 #ifndef AI_V4MAPPED
45 # define AI_V4MAPPED 0
3246 #endif
3347
3448 static int try_sk_from_env(int fd, const struct addrinfo *ai, const char *intf)
121135 errno = ENOENT;
122136 return -1;
123137 }
138
139 #ifdef __linux__
140 static int linux_sockaddr_local3(int sk, const void *buf, size_t bufsize)
141 {
142 if (send(sk, buf, bufsize, 0) < 0)
143 return -errno;
144 char rsp[4096];
145 ssize_t ret = recv(sk, rsp, sizeof(rsp), 0);
146 if (ret < 0)
147 return -errno;
148 else if (static_cast(size_t, ret) < sizeof(struct nlmsghdr))
149 return -EIO;
150 struct nlmsghdr nlh;
151 memcpy(&nlh, rsp, sizeof(nlh));
152 if (!NLMSG_OK(&nlh, ret))
153 return -EIO;
154 const struct rtmsg *rtm = static_cast(void *, rsp + NLMSG_HDRLEN);
155 return rtm->rtm_type == RTN_LOCAL;
156 }
157
158 static int linux_sockaddr_local2(const struct sockaddr *sa, socklen_t sl)
159 {
160 int sk = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
161 if (sk < 0)
162 return -errno;
163 struct {
164 struct nlmsghdr nh;
165 struct rtmsg rth;
166 char attrbuf[4096];
167 } req;
168 memset(&req, 0, sizeof(req));
169 req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(req.rth));
170 req.nh.nlmsg_flags = NLM_F_REQUEST;
171 req.nh.nlmsg_type = RTM_GETROUTE;
172 req.rth.rtm_family = sa->sa_family;
173 req.rth.rtm_protocol = RTPROT_UNSPEC;
174 req.rth.rtm_type = RTN_UNSPEC;
175 req.rth.rtm_scope = RT_SCOPE_UNIVERSE;
176 req.rth.rtm_table = RT_TABLE_UNSPEC;
177 struct rtattr *rta = reinterpret_cast(struct rtattr *,
178 reinterpret_cast(char *, &req) + NLMSG_ALIGN(req.nh.nlmsg_len));
179 rta->rta_type = RTA_DST;
180
181 int ret = -ENODATA;
182 if (sa->sa_family == AF_INET6) {
183 const struct in6_addr *ad = &reinterpret_cast(const struct sockaddr_in6 *, sa)->sin6_addr;
184 req.rth.rtm_dst_len = 16;
185 rta->rta_len = RTA_LENGTH(16);
186 req.nh.nlmsg_len = NLMSG_ALIGN(req.nh.nlmsg_len) + rta->rta_len;
187 memcpy(RTA_DATA(rta), ad, 16);
188 } else if (sa->sa_family == AF_INET) {
189 struct in_addr ad = reinterpret_cast(const struct sockaddr_in *, sa)->sin_addr;
190 req.rth.rtm_dst_len = 4;
191 rta->rta_len = RTA_LENGTH(4);
192 req.nh.nlmsg_len = NLMSG_ALIGN(req.nh.nlmsg_len) + rta->rta_len;
193 memcpy(RTA_DATA(rta), &ad, 4);
194 }
195 ret = linux_sockaddr_local3(sk, &req, req.nh.nlmsg_len);
196 close(sk);
197 return ret;
198 }
199 #endif
200
201 #ifdef __OpenBSD__
202 static int openbsd_sockaddr_local3(int rsk, const void *buf, size_t bufsize)
203 {
204 ssize_t ret = send(rsk, buf, bufsize, 0);
205 if (ret < 0)
206 return -errno;
207 else if (ret != bufsize)
208 return -EIO;
209 struct rt_msghdr rsp;
210 do {
211 ret = recv(rsk, &rsp, sizeof(rsp), 0);
212 } while (ret > 0 && (rsp.rtm_version != RTM_VERSION ||
213 rsp.rtm_seq != 1 || rsp.rtm_pid != getpid()));
214 return rsp.rtm_flags & RTF_LOCAL;
215 }
216
217 static int openbsd_sockaddr_local2(const struct sockaddr *sa, socklen_t sl)
218 {
219 int sk = socket(AF_ROUTE, SOCK_RAW, AF_UNSPEC);
220 if (sk < 0)
221 return -errno;
222 struct {
223 struct rt_msghdr rtm;
224 char ab[512];
225 } req;
226 memset(&req, 0, sizeof(req));
227 req.rtm.rtm_type = RTM_GET;
228 req.rtm.rtm_version = RTM_VERSION;
229 req.rtm.rtm_flags = RTF_STATIC | RTF_UP | RTF_HOST | RTF_GATEWAY;
230 req.rtm.rtm_seq = 1;
231 req.rtm.rtm_addrs = /*RTA_IFP |*/ RTA_DST;
232 req.rtm.rtm_tableid = getrtable();
233 req.rtm.rtm_hdrlen = sizeof(req.rtm);
234 memcpy(req.ab, sa, sl);
235 req.rtm.rtm_msglen = sizeof(req.rtm) + sl;
236 int ret = openbsd_sockaddr_local3(sk, &req, req.rtm.rtm_msglen);
237 close(sk);
238 return ret;
239 }
240 #endif
241
242 EXPORT_SYMBOL int HX_sockaddr_is_local(const struct sockaddr *sa, socklen_t sl,
243 unsigned int flags)
244 {
245 struct sockaddr_in xl = {};
246
247 if (sa->sa_family == AF_INET6) {
248 if (sl < sizeof(struct sockaddr_in6))
249 return -EINVAL;
250 } else if (sa->sa_family == AF_INET) {
251 if (sl < sizeof(struct sockaddr_in))
252 return -EINVAL;
253 }
254 #ifdef HAVE_SYS_UN_H
255 else if (sa->sa_family == AF_UNIX) {
256 if (sl < sizeof(struct sockaddr_un))
257 return 1;
258 }
259 #endif
260 else {
261 return -EPROTONOSUPPORT;
262 }
263 if (flags & AI_V4MAPPED && sa->sa_family == AF_INET6) {
264 /*
265 * Preprocess mapped addresses, becuase kernel interfaces do
266 * not support them.
267 */
268 const struct in6_addr *ad = &reinterpret_cast(const struct sockaddr_in6 *, sa)->sin6_addr;
269 static const uint8_t mappedv4[] = {0,0,0,0, 0,0,0,0, 0,0,0xff,0xff};
270 if (memcmp(ad, mappedv4, 12) == 0) {
271 xl.sin_family = AF_INET;
272 memcpy(&xl.sin_addr, &ad->s6_addr[12], 4);
273 sa = reinterpret_cast(struct sockaddr *, &xl);
274 sl = sizeof(xl);
275 }
276 }
277 #if defined(__linux__)
278 return linux_sockaddr_local2(sa, sl);
279 #elif defined(__OpenBSD__)
280 return openbsd_sockaddr_local2(sa, sl);
281 #else
282 if (sa->sa_family == AF_INET) {
283 struct in_addr a = reinterpret_cast(const struct sockaddr_in *, sa)->sin_addr;
284 return ntohl(a.s_addr) >> 24 == 127;
285 } else if (sa->sa_family == AF_INET6) {
286 return IN6_IS_ADDR_LOOPBACK(&reinterpret_cast(const struct sockaddr_in6 *, sa)->sin6_addr);
287 }
288 #endif
289 return -EPROTONOSUPPORT;
290 }
291
292 EXPORT_SYMBOL int HX_ipaddr_is_local(const char *addr, unsigned int flags)
293 {
294 struct addrinfo hints = {.ai_flags = flags & AI_V4MAPPED};
295 struct addrinfo *r = nullptr;
296 int err = getaddrinfo(addr, nullptr, &hints, &r);
297 if (err != 0) {
298 freeaddrinfo(r);
299 return 0;
300 }
301 int lcl = HX_sockaddr_is_local(r->ai_addr, r->ai_addrlen, hints.ai_flags);
302 freeaddrinfo(r);
303 return lcl;
304 }
2626 * all others pass through
2727 */
2828 enum HX_quote_selector {
29 HXQUOTE_ALWAYS,
2930 HXQUOTE_ACCEPT,
3031 HXQUOTE_REJECT,
3132 };
210211 *cp = max;
211212
212213 ret = malloc(sizeof(char *) * (*cp + 1));
214 if (ret == nullptr)
215 return nullptr;
213216 ret[*cp] = NULL;
214217
215218 {
377380 return const_cast1(char *, p);
378381 }
379382
383 /**
384 * Helper for substr() function for dealing with negative off/len values
385 * @z: total length of string
386 * @offset: n>=0 specifies offset from the start,
387 * n<0 specifies offset from the end
388 * @len: "length"; n>=0 specifies length to copy (from @offset),
389 * n<0 specifies the byte relative to the end at which to stop
390 *
391 * @abstart: (result) absolute start
392 * @retval: (result) absolute length to copy (from *@abstart)
393 */
394 size_t HX_substr_helper(size_t z, long offset, long len, size_t *start)
395 {
396 if (offset >= 0)
397 *start = offset;
398 else if (offset == LONG_MIN)
399 *start = z + -static_cast(size_t, LONG_MIN);
400 else
401 *start = z >= static_cast(unsigned long, -offset) ? z + offset : z;
402
403 size_t end;
404 if (len >= 0)
405 end = *start < SIZE_MAX - len ? *start + len : SIZE_MAX;
406 else if (len == LONG_MIN)
407 end = z + -static_cast(unsigned long, LONG_MIN);
408 else
409 end = z >= static_cast(unsigned long, -len) ? z + len : 0;
410 if (end > z)
411 end = z;
412 return end > *start ? end - *start : 0;
413 }
414
380415 /* supports negative offsets like scripting languages */
381416 EXPORT_SYMBOL char *HX_strmid(const char *expr, long offset, long length)
382417 {
383 char *buffer;
384
385 if (offset < 0)
386 offset = strlen(expr) + offset;
387 if (length < 0)
388 length = strlen(expr) - offset + length;
389 if ((buffer = malloc(length + 1)) == NULL)
390 return NULL;
391
392 expr += offset;
393 return HX_strlcpy(buffer, expr, length + 1);
418 size_t start = 0, tocopy = HX_substr_helper(strlen(expr), offset, length, &start);
419 char *buffer = malloc(tocopy + 1);
420 if (buffer == nullptr)
421 return NULL;
422 memcpy(buffer, &expr[start], tocopy);
423 buffer[tocopy] = '\0';
424 return buffer;
394425 }
395426
396427 EXPORT_SYMBOL char *HX_strndup(const char *src, size_t size)
772803 EXPORT_SYMBOL char *HX_strquote(const char *src, unsigned int type,
773804 char **free_me)
774805 {
775 const struct HX_quote_rule *rule;
806 const struct HX_quote_rule *rule = nullptr;
776807 bool do_quote;
777808 char *tmp;
778809
781812 return NULL;
782813 }
783814 /* If quote_chars is NULL, it is clear all chars are to be encoded. */
784 rule = &HX_quote_rules[type];
785 if (type >= ARRAY_SIZE(HX_quote_rules) || rule->chars == NULL)
815 if (type >= ARRAY_SIZE(HX_quote_rules)) {
786816 do_quote = true;
787 else if (rule->selector == HXQUOTE_REJECT)
788 do_quote = strpbrk(src, rule->chars) != NULL;
789 else if (rule->selector == HXQUOTE_ACCEPT)
790 do_quote = HX_strchr2(src, rule->chars) != NULL;
791 else
792 do_quote = false;
817 } else {
818 rule = &HX_quote_rules[type];
819 if (rule->selector == HXQUOTE_ALWAYS)
820 do_quote = true;
821 else if (rule->selector == HXQUOTE_REJECT)
822 do_quote = strpbrk(src, rule->chars) != NULL;
823 else if (rule->selector == HXQUOTE_ACCEPT)
824 do_quote = HX_strchr2(src, rule->chars) != NULL;
825 else
826 do_quote = false;
827 }
793828 /*
794829 * free_me == NULL implies that we always allocate, even if
795830 * there is nothing to quote.
0 /*
1 * Testing for compile error in the cast helpers
2 * written by Jan Engelhardt
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the WTF Public License version 2 or
6 * (at your option) any later version.
7 */
0 // SPDX-License-Identifier: MIT
81 #include <math.h>
92 #include <stdint.h>
103 #include <stdio.h>
0 /*
1 * Copyright Jan Engelhardt
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the WTF Public License version 2 or
5 * (at your option) any later version.
6 */
0 // SPDX-License-Identifier: MIT
71 #ifndef __cplusplus
82 # include <stdio.h>
93 # include <stdlib.h>
0 /*
1 * formatter test program
2 * Copyright by Jan Engelhardt
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the WTF Public License version 2 or
6 * (at your option) any later version.
7 */
0 // SPDX-License-Identifier: MIT
81 #include <stdint.h>
92 #include <stdio.h>
103 #include <stdlib.h>
5447 NULL,
5548 };
5649
57 static void t_format(int argc)
50 static int t_format(int argc)
5851 {
5952 struct HXformat_map *fmt = HXformat_init();
6053 const char *const *s;
6962 HXformat_add(fmt, "TWOARG", "a, b", HXTYPE_STRING | HXFORMAT_IMMED);
7063 ++argc;
7164 printf("# HXformat2\n");
72 for (s = fmt2_strings; *s != NULL; ++s)
73 HXformat_fprintf(fmt, stdout, *s);
65 for (s = fmt2_strings; *s != NULL; ++s) {
66 char buf[80];
67 if (HXformat_sprintf(fmt, buf, ARRAY_SIZE(buf), *s) < 0 ||
68 HXformat_fprintf(fmt, stdout, *s) < 0)
69 return EXIT_FAILURE;
70 }
7471 HXformat_free(fmt);
72 return EXIT_SUCCESS;
7573 }
7674
7775 int main(int argc, const char **argv)
8381 fprintf(stderr, "HX_init: %s\n", strerror(-ret));
8482 return EXIT_FAILURE;
8583 }
86 t_format(argc);
84 ret = t_format(argc);
8785 HX_exit();
88 return EXIT_SUCCESS;
86 return ret;
8987 }
0 // SPDX-License-Identifier: MIT
01 #include <errno.h>
12 #include <fcntl.h>
23 #include <stdint.h>
0 /*
1 * Copyright Jan Engelhardt
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the WTF Public License version 2 or
5 * (at your option) any later version.
6 */
0 // SPDX-License-Identifier: MIT
71 #ifdef __cplusplus
82 # include <cstdio>
93 # include <cstdlib>
0 /*
1 * Copyright Jan Engelhardt
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the WTF Public License version 2 or
5 * (at your option) any later version.
6 */
0 // SPDX-License-Identifier: MIT
71 #include <stdio.h>
82 #include <stdlib.h>
93 #include <libHX/list.h>
0 /*
1 * Test for libHX's maps
2 * Copyright Jan Engelhardt
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the WTF Public License version 2 or
6 * (at your option) any later version.
7 */
0 // SPDX-License-Identifier: MIT
81 #include "config.h"
92 #include <errno.h>
103 #include <math.h>
0 // SPDX-License-Identifier: MIT
01 /*
12 * speed test HX_memmem
2 * Copyright Jan Engelhardt
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the WTF Public License version 2 or
6 * (at your option) any later version.
73 */
84 #include <stdio.h>
95 #include <stdlib.h>
0 /*
1 * Copyright Jan Engelhardt
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the WTF Public License version 2 or
5 * (at your option) any later version.
6 */
0 // SPDX-License-Identifier: MIT
71 #ifndef __cplusplus
82 # include <stdlib.h>
93 #else
3731 if (stat(argv[1], &sa) < 0 ||
3832 stat(argv[2], &sb) < 0)
3933 perror("stat");
40 printf("Difference: %ld\n", HX_time_compare(&sa, &sb, 'm'));
34 else
35 printf("Difference: %ld\n", HX_time_compare(&sa, &sb, 'm'));
4136 }
4237
4338 HX_exit();
0 /*
1 * Copyright Jan Engelhardt
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the WTF Public License version 2 or
5 * (at your option) any later version.
6 */
0 // SPDX-License-Identifier: MIT
71 #define WIN32_LEAN_AND_MEAN 1
82 #ifdef __cplusplus
93 # include <cstdlib>
0 // SPDX-License-Identifier: MIT
1 // SPDX-FileCopyrightText: 2022 Jan Engelhardt
02 /*
13 * option parser test program
2 * Copyright Jan Engelhardt
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the WTF Public License version 2 or
6 * (at your option) any later version.
74 */
85 #include <errno.h>
96 #include <stdio.h>
0 /*
1 * Copyright Jan Engelhardt
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the WTF Public License version 2 or
5 * (at your option) any later version.
6 */
0 // SPDX-License-Identifier: MIT
71 #ifdef __cplusplus
82 # include <cerrno>
93 # include <cstdio>
0 /*
1 * Copyright Jan Engelhardt
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the WTF Public License version 2 or
5 * (at your option) any later version.
6 */
0 // SPDX-License-Identifier: MIT
71 #include <stdio.h>
82 #include <stdlib.h>
93 #include <time.h>
0 // SPDX-License-Identifier: MIT
01 /*
12 * Test utility for libHX's realpath
2 * Copyright Jan Engelhardt
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the WTF Public License version 2 or
6 * (at your option) any later version.
73 */
84 #include <stdbool.h>
95 #include <stdio.h>
00 /*
11 A=b;C="d" ; E="F;" ; F= G=Z
22 */
3 // SPDX-License-Identifier: MIT
34 /*
45 * shconfig test program
5 * Copyright Jan Engelhardt
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the WTF Public License version 2 or
9 * (at your option) any later version.
106 */
117 #include <errno.h>
128 #include <stdio.h>
0 // SPDX-License-Identifier: MIT
1 #include <errno.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <libHX/defs.h>
6 #include <libHX/socket.h>
7 #ifndef _WIN32
8 # include <netdb.h>
9 #endif
10 #ifndef AI_V4MAPPED
11 # define AI_V4MAPPED 0
12 #endif
13
14 int main(void)
15 {
16 static const char *addrs[] = {
17 "::1", "::2", "::ffff:127.0.0.1", "::",
18 "127.0.0.1", "127.0.0.2", "1.1.1.1", "255.255.255.255",
19 };
20 for (size_t i = 0; i < ARRAY_SIZE(addrs); ++i) {
21 printf("%-16s\t", addrs[i]);
22 int lcl = HX_ipaddr_is_local(addrs[i], AI_V4MAPPED);
23 if (lcl < 0) {
24 printf("%s\n", strerror(-lcl));
25 return EXIT_FAILURE;
26 }
27 printf("%d\n", lcl);
28 }
29 return EXIT_SUCCESS;
30 }
0 // SPDX-License-Identifier: MIT
01 /*
12 * Behavior Correctness Test for HX_strchr2
2 * Copyright Jan Engelhardt, 2013
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the WTF Public License version 2 or
6 * (at your option) any later version.
73 */
84 #include <libHX/string.h>
95
00 /* long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing long line for testing */
1 /*
2 * Copyright Jan Engelhardt
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the WTF Public License version 2 or
6 * (at your option) any later version.
7 */
1 // SPDX-License-Identifier: MIT
82 #ifndef __cplusplus
93 # include <assert.h>
104 # include <errno.h>
479473 }
480474 }
481475
476 static int t_strmid(void)
477 {
478 #define T(spar,opar,lpar,xpar) do { \
479 char *s = HX_strmid((spar), (opar), (lpar)); \
480 if (s == nullptr) \
481 return EXIT_FAILURE; \
482 int ret = strcmp(s, (xpar)); \
483 if (ret != 0) { \
484 fprintf(stderr, "Faillure: substr %s,%d,%d = %s\n", \
485 (spar), static_cast(int, (opar)), static_cast(int, (lpar)), s); \
486 free(s); \
487 return ret; \
488 } \
489 free(s); \
490 } while (false)
491
492 T("Hello World", -12, 5, "");
493 T("bark", -3, -1, "ar");
494 T("cake", -3, -3, "");
495 T("cake", -3, -4, "");
496 T("fun", 0, 0, "");
497 T("bark", 0, 1, "b");
498 T("bark", 0, 5, "bark");
499 T("bark", -4, 1, "b");
500 T("bark", -4, 5, "bark");
501 return EXIT_SUCCESS;
502 #undef T
503 }
504
482505 int main(int argc, const char **argv)
483506 {
484507 hxmc_t *tx = NULL;
487510
488511 if (HX_init() <= 0)
489512 abort();
513 int ret = t_strmid();
514 if (ret != EXIT_SUCCESS)
515 return EXIT_FAILURE;
490516
491517 fp = fopen(file, "r");
492518 if (fp == NULL) {
0 // SPDX-License-Identifier: MIT
01 /*
12 * Behavior Correctness Test for HX_strquote
2 * Copyright Jan Engelhardt, 2013
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the WTF Public License version 2 or
6 * (at your option) any later version.
73 */
84 #include <stdbool.h>
95 #include <stdio.h>
0 /*
1 * Copyright Jan Engelhardt
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the WTF Public License version 2 or
5 * (at your option) any later version.
6 */
0 // SPDX-License-Identifier: MIT
71 #ifdef HAVE_CONFIG_H
82 # include "config.h"
93 #endif
10 #if defined(HAVE_INITGROUPS)
114 #include <errno.h>
125 #include <stdio.h>
136 #include <stdlib.h>
169 #include <libHX/defs.h>
1710 #include <libHX/option.h>
1811 #include <libHX/proc.h>
12 #if defined(HAVE_INITGROUPS)
1913
2014 static char *user_name, *group_name;
2115 static const struct HXoption options_table[] = {
6963 }
7064 return EXIT_SUCCESS;
7165 }
66 #else
67 int main(void)
68 {
69 return EXIT_SUCCESS;
70 }
7271 #endif
0 /*
1 * Copyright Jan Engelhardt, 2012
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the WTF Public License version 2 or
5 * (at your option) any later version.
6 */
0 // SPDX-License-Identifier: MIT
71 #include <assert.h>
82 #include <math.h>
93 #include <stdio.h>
2620 static const long long NANOSECOND_LL = 1000000000;
2721 static const unsigned int clock_id = CLOCK_THREAD_CPUTIME_ID;
2822 static const unsigned int step = 1000;
29 static const unsigned int step_mul = 10000000;
23 static const long step_mul = 10000000;
3024
3125 static const struct timespec pairs[] = {
3226 {-1, 700000000}, {-1, 400000000}, {-1, 0},
0 /*
1 * Copyright Jan Engelhardt
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the WTF Public License version 2 or
5 * (at your option) any later version.
6 */
0 // SPDX-License-Identifier: MIT
71 #include <stdbool.h>
82 #include <stdio.h>
93 #include <libxml/parser.h>