aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/util/Makefile.am5
-rw-r--r--src/util/common_gettext.c42
-rw-r--r--src/util/common_logging.c14
-rw-r--r--src/util/connection.c10
-rw-r--r--src/util/crypto_crc.c9
-rw-r--r--src/util/crypto_ksk.c93
-rw-r--r--src/util/crypto_random.c92
-rw-r--r--src/util/os_load.c3
-rw-r--r--src/util/plugin.c31
-rw-r--r--src/util/program.c35
-rw-r--r--src/util/program_lib_argz.c403
-rw-r--r--src/util/program_lib_mempcpy.c27
-rw-r--r--src/util/program_lib_strndup.c35
-rw-r--r--src/util/program_lib_strnlen.c29
-rw-r--r--src/util/test_crypto_ksk.c2
-rw-r--r--src/util/test_crypto_rsa.c2
16 files changed, 161 insertions, 671 deletions
diff --git a/src/util/Makefile.am b/src/util/Makefile.am
index 66259b3eb..9cc402fc5 100644
--- a/src/util/Makefile.am
+++ b/src/util/Makefile.am
@@ -29,7 +29,6 @@ libgnunetutil_la_SOURCES = \
29 client.c \ 29 client.c \
30 common_allocation.c \ 30 common_allocation.c \
31 common_endian.c \ 31 common_endian.c \
32 common_gettext.c \
33 common_logging.c \ 32 common_logging.c \
34 configuration.c \ 33 configuration.c \
35 connection.c \ 34 connection.c \
@@ -370,10 +369,6 @@ perf_crypto_hash_LDADD = \
370 369
371 370
372EXTRA_DIST = \ 371EXTRA_DIST = \
373 program_lib_argz.c \
374 program_lib_strndup.c \
375 program_lib_strnlen.c \
376 program_lib_mempcpy.c \
377 test_configuration_data.conf \ 372 test_configuration_data.conf \
378 test_program_data.conf \ 373 test_program_data.conf \
379 test_pseudonym_data.conf \ 374 test_pseudonym_data.conf \
diff --git a/src/util/common_gettext.c b/src/util/common_gettext.c
deleted file mode 100644
index ad347e160..000000000
--- a/src/util/common_gettext.c
+++ /dev/null
@@ -1,42 +0,0 @@
1/*
2 This file is part of GNUnet.
3 (C) 2008 Christian Grothoff (and other contributing authors)
4
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19*/
20
21/**
22 * @file util/common_gettext.c
23 * @brief gettext init routine
24 * @author Heikki Lindholm
25 */
26#include "platform.h"
27#include "gnunet_common.h"
28#include "gnunet_os_lib.h"
29
30void __attribute__ ((constructor)) GNUNET_util_generic_ltdl_init ()
31{
32#if ENABLE_NLS
33 char *path;
34
35 path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LOCALEDIR);
36 if (path != NULL)
37 {
38 BINDTEXTDOMAIN ("GNUnet", path);
39 GNUNET_free (path);
40 }
41#endif
42}
diff --git a/src/util/common_logging.c b/src/util/common_logging.c
index 139e9ee3f..7bd39c7ca 100644
--- a/src/util/common_logging.c
+++ b/src/util/common_logging.c
@@ -571,6 +571,20 @@ GNUNET_a2s (const struct sockaddr *addr, socklen_t addrlen)
571void __attribute__ ((constructor)) GNUNET_util_cl_init () 571void __attribute__ ((constructor)) GNUNET_util_cl_init ()
572{ 572{
573 GNUNET_stderr = stderr; 573 GNUNET_stderr = stderr;
574#ifdef MINGW
575 InitWinEnv (NULL);
576#endif
577}
578
579
580/**
581 * Destructor
582 */
583void __attribute__ ((destructor)) GNUNET_util_cl_fini ()
584{
585#ifdef MINGW
586 ShutdownWinEnv ();
587#endif
574} 588}
575 589
576/* end of common_logging.c */ 590/* end of common_logging.c */
diff --git a/src/util/connection.c b/src/util/connection.c
index b67da814a..1631718e7 100644
--- a/src/util/connection.c
+++ b/src/util/connection.c
@@ -726,11 +726,11 @@ connect_probe_continuation (void *cls,
726 len = sizeof (error); 726 len = sizeof (error);
727 errno = 0; 727 errno = 0;
728 error = 0; 728 error = 0;
729 if ((0 == (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) || 729 if ( (0 == (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) ||
730 (GNUNET_OK != 730 (GNUNET_OK !=
731 GNUNET_NETWORK_socket_getsockopt (ap->sock, SOL_SOCKET, SO_ERROR, 731 GNUNET_NETWORK_socket_getsockopt (ap->sock, SOL_SOCKET, SO_ERROR,
732 &error, &len)) || (error != 0) 732 &error, &len)) ||
733 || (errno != 0)) 733 (error != 0) )
734 { 734 {
735 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock)); 735 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (ap->sock));
736 GNUNET_free (ap); 736 GNUNET_free (ap);
diff --git a/src/util/crypto_crc.c b/src/util/crypto_crc.c
index a89b059c5..8f5cfc800 100644
--- a/src/util/crypto_crc.c
+++ b/src/util/crypto_crc.c
@@ -52,10 +52,16 @@ static uLong crc_table[256];
52 * with the ccorrect final value. Thus, it is safe to call 52 * with the ccorrect final value. Thus, it is safe to call
53 * even on a table that someone else is using concurrently. 53 * even on a table that someone else is using concurrently.
54 */ 54 */
55void __attribute__ ((constructor)) GNUNET_CRYPTO_crc_init () 55static void
56crc_init ()
56{ 57{
58 static int once;
57 unsigned int i, j; 59 unsigned int i, j;
58 uLong h = 1; 60 uLong h = 1;
61
62 if (once)
63 return;
64 once = 1;
59 crc_table[0] = 0; 65 crc_table[0] = 0;
60 for (i = 128; i; i >>= 1) 66 for (i = 128; i; i >>= 1)
61 { 67 {
@@ -79,6 +85,7 @@ void __attribute__ ((constructor)) GNUNET_CRYPTO_crc_init ()
79static uLong 85static uLong
80crc32 (uLong crc, const char *buf, size_t len) 86crc32 (uLong crc, const char *buf, size_t len)
81{ 87{
88 crc_init ();
82 GNUNET_assert (crc_table[255] != 0); 89 GNUNET_assert (crc_table[255] != 0);
83 crc ^= 0xffffffff; 90 crc ^= 0xffffffff;
84 while (len--) 91 while (len--)
diff --git a/src/util/crypto_ksk.c b/src/util/crypto_ksk.c
index 888c08384..1aa493ca8 100644
--- a/src/util/crypto_ksk.c
+++ b/src/util/crypto_ksk.c
@@ -684,8 +684,6 @@ ksk_decode_key (const struct KskRsaPrivateKeyBinaryEncoded *encoding)
684} 684}
685 685
686 686
687
688
689typedef struct 687typedef struct
690{ 688{
691 GNUNET_HashCode hc; 689 GNUNET_HashCode hc;
@@ -693,6 +691,7 @@ typedef struct
693} KBlockKeyCacheLine; 691} KBlockKeyCacheLine;
694 692
695static KBlockKeyCacheLine **cache; 693static KBlockKeyCacheLine **cache;
694
696static unsigned int cacheSize; 695static unsigned int cacheSize;
697 696
698/** 697/**
@@ -724,92 +723,6 @@ GNUNET_CRYPTO_rsa_key_create_from_hash (const GNUNET_HashCode * hc)
724} 723}
725 724
726 725
727/**
728 * Process ID of the "find" process that we use for
729 * entropy gathering.
730 */
731static pid_t genproc;
732
733/**
734 * Function called by libgcrypt whenever we are
735 * blocked gathering entropy.
736 */
737static void
738entropy_generator (void *cls,
739 const char *what, int printchar, int current, int total)
740{
741 unsigned long code;
742 enum GNUNET_OS_ProcessStatusType type;
743 int ret;
744
745 if (0 != strcmp (what, "need_entropy"))
746 return;
747 if (current == total)
748 {
749 if (genproc != 0)
750 {
751 if (0 != PLIBC_KILL (genproc, SIGTERM))
752 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "kill");
753 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (genproc));
754 genproc = 0;
755 }
756 return;
757 }
758 if (genproc != 0)
759 {
760 ret = GNUNET_OS_process_status (genproc, &type, &code);
761 if (ret == GNUNET_NO)
762 return; /* still running */
763 if (ret == GNUNET_SYSERR)
764 {
765 GNUNET_break (0);
766 return;
767 }
768 if (0 != PLIBC_KILL (genproc, SIGTERM))
769 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "kill");
770 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (genproc));
771 genproc = 0;
772 }
773 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
774 _("Starting `%s' process to generate entropy\n"), "find");
775 genproc = GNUNET_OS_start_process (NULL, NULL, "sh",
776 "sh",
777 "-c",
778 "exec find / -mount -type f -exec cp {} /dev/null \\; 2>/dev/null",
779 NULL);
780}
781
782
783static void
784killfind ()
785{
786 if (genproc != 0)
787 {
788 PLIBC_KILL (genproc, SIGKILL);
789 genproc = 0;
790 }
791}
792
793
794void __attribute__ ((constructor)) GNUNET_CRYPTO_ksk_init ()
795{
796 gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
797 if (!gcry_check_version (GCRYPT_VERSION))
798 {
799 fprintf (stderr,
800 _
801 ("libgcrypt has not the expected version (version %s is required).\n"),
802 GCRYPT_VERSION);
803 abort ();
804 }
805#ifdef gcry_fast_random_poll
806 gcry_fast_random_poll ();
807#endif
808 gcry_set_progress_handler (&entropy_generator, NULL);
809 atexit (&killfind);
810}
811
812
813void __attribute__ ((destructor)) GNUNET_CRYPTO_ksk_fini () 726void __attribute__ ((destructor)) GNUNET_CRYPTO_ksk_fini ()
814{ 727{
815 int i; 728 int i;
@@ -820,7 +733,7 @@ void __attribute__ ((destructor)) GNUNET_CRYPTO_ksk_fini ()
820 GNUNET_free (cache[i]); 733 GNUNET_free (cache[i]);
821 } 734 }
822 GNUNET_array_grow (cache, cacheSize, 0); 735 GNUNET_array_grow (cache, cacheSize, 0);
823 gcry_set_progress_handler (NULL, NULL);
824} 736}
825 737
826/* end of kblockkey.c */ 738
739/* end of crypto_ksk.c */
diff --git a/src/util/crypto_random.c b/src/util/crypto_random.c
index 25ba4db2a..0a092cdcc 100644
--- a/src/util/crypto_random.c
+++ b/src/util/crypto_random.c
@@ -27,6 +27,7 @@
27#include "platform.h" 27#include "platform.h"
28#include "gnunet_common.h" 28#include "gnunet_common.h"
29#include "gnunet_crypto_lib.h" 29#include "gnunet_crypto_lib.h"
30#include "gnunet_os_lib.h"
30#include <gcrypt.h> 31#include <gcrypt.h>
31 32
32/** 33/**
@@ -161,12 +162,99 @@ GNUNET_CRYPTO_random_disable_entropy_gathering ()
161 gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0); 162 gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0);
162} 163}
163 164
165
166/**
167 * Process ID of the "find" process that we use for
168 * entropy gathering.
169 */
170static pid_t genproc;
171
164/** 172/**
165 * Initializer 173 * Function called by libgcrypt whenever we are
174 * blocked gathering entropy.
166 */ 175 */
167void __attribute__ ((constructor)) GNUNET_util_random_init () 176static void
177entropy_generator (void *cls,
178 const char *what, int printchar, int current, int total)
179{
180 unsigned long code;
181 enum GNUNET_OS_ProcessStatusType type;
182 int ret;
183
184 if (0 != strcmp (what, "need_entropy"))
185 return;
186 if (current == total)
187 {
188 if (genproc != 0)
189 {
190 if (0 != PLIBC_KILL (genproc, SIGTERM))
191 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "kill");
192 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (genproc));
193 genproc = 0;
194 }
195 return;
196 }
197 if (genproc != 0)
198 {
199 ret = GNUNET_OS_process_status (genproc, &type, &code);
200 if (ret == GNUNET_NO)
201 return; /* still running */
202 if (ret == GNUNET_SYSERR)
203 {
204 GNUNET_break (0);
205 return;
206 }
207 if (0 != PLIBC_KILL (genproc, SIGTERM))
208 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "kill");
209 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (genproc));
210 genproc = 0;
211 }
212 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
213 _("Starting `%s' process to generate entropy\n"), "find");
214 genproc = GNUNET_OS_start_process (NULL, NULL, "sh",
215 "sh",
216 "-c",
217 "exec find / -mount -type f -exec cp {} /dev/null \\; 2>/dev/null",
218 NULL);
219}
220
221
222static void
223killfind ()
224{
225 if (genproc != 0)
226 {
227 PLIBC_KILL (genproc, SIGKILL);
228 genproc = 0;
229 }
230}
231
232
233void __attribute__ ((constructor)) GNUNET_CRYPTO_random_init ()
168{ 234{
169 SRANDOM (time (NULL)); 235 SRANDOM (time (NULL));
236 gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
237 if (!gcry_check_version (GCRYPT_VERSION))
238 {
239 fprintf (stderr,
240 _
241 ("libgcrypt has not the expected version (version %s is required).\n"),
242 GCRYPT_VERSION);
243 abort ();
244 }
245#ifdef gcry_fast_random_poll
246 gcry_fast_random_poll ();
247#endif
248 gcry_set_progress_handler (&entropy_generator, NULL);
249 atexit (&killfind);
170} 250}
171 251
252
253void __attribute__ ((destructor)) GNUNET_CRYPTO_random_fini ()
254{
255 gcry_set_progress_handler (NULL, NULL);
256}
257
258
259
172/* end of crypto_random.c */ 260/* end of crypto_random.c */
diff --git a/src/util/os_load.c b/src/util/os_load.c
index f7b5dff96..e54147ff4 100644
--- a/src/util/os_load.c
+++ b/src/util/os_load.c
@@ -156,9 +156,10 @@ updateUsage ()
156 unsigned long long usage_time = 0, total_time = 1; 156 unsigned long long usage_time = 0, total_time = 1;
157 157
158 /* Get the first line with the data */ 158 /* Get the first line with the data */
159 memset (line, 0, sizeof (line));
159 rewind (proc_stat); 160 rewind (proc_stat);
160 fflush (proc_stat); 161 fflush (proc_stat);
161 if (NULL == fgets (line, 256, proc_stat)) 162 if (NULL == fgets (line, sizeof (line), proc_stat))
162 { 163 {
163 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR | 164 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR |
164 GNUNET_ERROR_TYPE_BULK, 165 GNUNET_ERROR_TYPE_BULK,
diff --git a/src/util/plugin.c b/src/util/plugin.c
index 36380f339..ca93e9ca0 100644
--- a/src/util/plugin.c
+++ b/src/util/plugin.c
@@ -53,6 +53,12 @@ struct PluginList
53 53
54 54
55/** 55/**
56 * Have we been initialized?
57 */
58static int initialized;
59
60
61/**
56 * Libtool search path before we started. 62 * Libtool search path before we started.
57 */ 63 */
58static char *old_dlsearchpath; 64static char *old_dlsearchpath;
@@ -67,17 +73,14 @@ static struct PluginList *plugins;
67/** 73/**
68 * Setup libtool paths. 74 * Setup libtool paths.
69 */ 75 */
70void __attribute__ ((constructor)) GNUNET_PLUGIN_init () 76static void
77plugin_init ()
71{ 78{
72 int err; 79 int err;
73 const char *opath; 80 const char *opath;
74 char *path; 81 char *path;
75 char *cpath; 82 char *cpath;
76 83
77#ifdef MINGW
78 InitWinEnv (NULL);
79#endif
80
81 err = lt_dlinit (); 84 err = lt_dlinit ();
82 if (err > 0) 85 if (err > 0)
83 { 86 {
@@ -114,7 +117,8 @@ void __attribute__ ((constructor)) GNUNET_PLUGIN_init ()
114/** 117/**
115 * Shutdown libtool. 118 * Shutdown libtool.
116 */ 119 */
117void __attribute__ ((destructor)) GNUNET_PLUGIN_fini () 120static void
121plugin_fini ()
118{ 122{
119 lt_dlsetsearchpath (old_dlsearchpath); 123 lt_dlsetsearchpath (old_dlsearchpath);
120 if (old_dlsearchpath != NULL) 124 if (old_dlsearchpath != NULL)
@@ -122,11 +126,6 @@ void __attribute__ ((destructor)) GNUNET_PLUGIN_fini ()
122 GNUNET_free (old_dlsearchpath); 126 GNUNET_free (old_dlsearchpath);
123 old_dlsearchpath = NULL; 127 old_dlsearchpath = NULL;
124 } 128 }
125
126#ifdef MINGW
127 ShutdownWinEnv ();
128#endif
129
130 lt_dlexit (); 129 lt_dlexit ();
131} 130}
132 131
@@ -173,6 +172,11 @@ GNUNET_PLUGIN_load (const char *library_name, void *arg)
173 GNUNET_PLUGIN_Callback init; 172 GNUNET_PLUGIN_Callback init;
174 void *ret; 173 void *ret;
175 174
175 if (! initialized)
176 {
177 initialized = GNUNET_YES;
178 plugin_init ();
179 }
176 libhandle = lt_dlopenext (library_name); 180 libhandle = lt_dlopenext (library_name);
177 if (libhandle == NULL) 181 if (libhandle == NULL)
178 { 182 {
@@ -235,6 +239,11 @@ GNUNET_PLUGIN_unload (const char *library_name, void *arg)
235 lt_dlclose (pos->handle); 239 lt_dlclose (pos->handle);
236 GNUNET_free (pos->name); 240 GNUNET_free (pos->name);
237 GNUNET_free (pos); 241 GNUNET_free (pos);
242 if (plugins == NULL)
243 {
244 plugin_fini();
245 initialized = GNUNET_NO;
246 }
238 return ret; 247 return ret;
239} 248}
240 249
diff --git a/src/util/program.c b/src/util/program.c
index bbedc05d0..88b67a21e 100644
--- a/src/util/program.c
+++ b/src/util/program.c
@@ -35,15 +35,6 @@
35#include "gnunet_scheduler_lib.h" 35#include "gnunet_scheduler_lib.h"
36#include <gcrypt.h> 36#include <gcrypt.h>
37 37
38#if HAVE_ARGZ_H
39#include <argz.h>
40#else
41#include "program_lib_strnlen.c"
42#include "program_lib_strndup.c"
43#include "program_lib_mempcpy.c"
44#include "program_lib_argz.c"
45#endif
46
47/** 38/**
48 * Context for the command. 39 * Context for the command.
49 */ 40 */
@@ -156,19 +147,27 @@ GNUNET_PROGRAM_run (int argc,
156 gargs = getenv ("GNUNET_ARGS"); 147 gargs = getenv ("GNUNET_ARGS");
157 if (gargs != NULL) 148 if (gargs != NULL)
158 { 149 {
159 char *gargz;
160 size_t gargl;
161 char **gargv; 150 char **gargv;
151 unsigned int gargc;
162 int i; 152 int i;
163 153 char *tok;
164 argz_create_sep (gargs, ' ', &gargz, &gargl); 154 char *cargs;
155
156 gargv = NULL;
157 gargc = 0;
165 for (i=0;i<argc;i++) 158 for (i=0;i<argc;i++)
166 argz_insert (&gargz, &gargl, gargz, argv[i]); 159 GNUNET_array_append (gargv, gargc, GNUNET_strdup (argv[i]));
167 gargv = GNUNET_malloc (sizeof (char*) * (gargl+1)); 160 cargs = GNUNET_strdup (gargs);
168 argz_extract (gargz, gargl, gargv); 161 tok = strtok (cargs, " ");
169 argc = argz_count (gargz, gargl); 162 while (NULL != tok)
170 free (gargz); 163 {
164 GNUNET_array_append (gargv, gargc, GNUNET_strdup (tok));
165 tok = strtok (NULL, " ");
166 }
167 GNUNET_free (cargs);
168 GNUNET_array_append (gargv, gargc, NULL);
171 argv = (char *const *) gargv; 169 argv = (char *const *) gargv;
170 argc = gargc - 1;
172 } 171 }
173 memset (&cc, 0, sizeof (cc)); 172 memset (&cc, 0, sizeof (cc));
174 loglev = NULL; 173 loglev = NULL;
diff --git a/src/util/program_lib_argz.c b/src/util/program_lib_argz.c
deleted file mode 100644
index 6a7776fce..000000000
--- a/src/util/program_lib_argz.c
+++ /dev/null
@@ -1,403 +0,0 @@
1/* Functions for dealing with '\0' separated arg vectors.
2 Copyright (C) 1995-1998, 2000-2002, 2006, 2008 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18
19#include <errno.h>
20#include <stdlib.h>
21#include <string.h>
22
23
24
25/* Add BUF, of length BUF_LEN to the argz vector in ARGZ & ARGZ_LEN. */
26error_t
27argz_append (char **argz, size_t *argz_len, const char *buf, size_t buf_len)
28{
29 size_t new_argz_len = *argz_len + buf_len;
30 char *new_argz = realloc (*argz, new_argz_len);
31 if (new_argz)
32 {
33 memcpy (new_argz + *argz_len, buf, buf_len);
34 *argz = new_argz;
35 *argz_len = new_argz_len;
36 return 0;
37 }
38 else
39 return ENOMEM;
40}
41
42/* Add STR to the argz vector in ARGZ & ARGZ_LEN. This should be moved into
43 argz.c in libshouldbelibc. */
44error_t
45argz_add (char **argz, size_t *argz_len, const char *str)
46{
47 return argz_append (argz, argz_len, str, strlen (str) + 1);
48}
49
50
51
52error_t
53argz_add_sep (char **argz, size_t *argz_len, const char *string, int delim)
54{
55 size_t nlen = strlen (string) + 1;
56
57 if (nlen > 1)
58 {
59 const char *rp;
60 char *wp;
61
62 *argz = (char *) realloc (*argz, *argz_len + nlen);
63 if (*argz == NULL)
64 return ENOMEM;
65
66 wp = *argz + *argz_len;
67 rp = string;
68 do
69 if (*rp == delim)
70 {
71 if (wp > *argz && wp[-1] != '\0')
72 *wp++ = '\0';
73 else
74 --nlen;
75 }
76 else
77 *wp++ = *rp;
78 while (*rp++ != '\0');
79
80 *argz_len += nlen;
81 }
82
83 return 0;
84}
85
86
87
88error_t
89argz_create_sep (const char *string, int delim, char **argz, size_t *len)
90{
91 size_t nlen = strlen (string) + 1;
92
93 if (nlen > 1)
94 {
95 const char *rp;
96 char *wp;
97
98 *argz = (char *) malloc (nlen);
99 if (*argz == NULL)
100 return ENOMEM;
101
102 rp = string;
103 wp = *argz;
104 do
105 if (*rp == delim)
106 {
107 if (wp > *argz && wp[-1] != '\0')
108 *wp++ = '\0';
109 else
110 --nlen;
111 }
112 else
113 *wp++ = *rp;
114 while (*rp++ != '\0');
115
116 if (nlen == 0)
117 {
118 free (*argz);
119 *argz = NULL;
120 *len = 0;
121 }
122
123 *len = nlen;
124 }
125 else
126 {
127 *argz = NULL;
128 *len = 0;
129 }
130
131 return 0;
132}
133
134
135/* Insert ENTRY into ARGZ & ARGZ_LEN before BEFORE, which should be an
136 existing entry in ARGZ; if BEFORE is NULL, ENTRY is appended to the end.
137 Since ARGZ's first entry is the same as ARGZ, argz_insert (ARGZ, ARGZ_LEN,
138 ARGZ, ENTRY) will insert ENTRY at the beginning of ARGZ. If BEFORE is not
139 in ARGZ, EINVAL is returned, else if memory can't be allocated for the new
140 ARGZ, ENOMEM is returned, else 0. */
141error_t
142argz_insert (char **argz, size_t *argz_len, char *before, const char *entry)
143{
144 if (! before)
145 return argz_add (argz, argz_len, entry);
146
147 if (before < *argz || before >= *argz + *argz_len)
148 return EINVAL;
149
150 if (before > *argz)
151 /* Make sure before is actually the beginning of an entry. */
152 while (before[-1])
153 before--;
154
155 {
156 size_t after_before = *argz_len - (before - *argz);
157 size_t entry_len = strlen (entry) + 1;
158 size_t new_argz_len = *argz_len + entry_len;
159 char *new_argz = realloc (*argz, new_argz_len);
160
161 if (new_argz)
162 {
163 before = new_argz + (before - *argz);
164 memmove (before + entry_len, before, after_before);
165 memmove (before, entry, entry_len);
166 *argz = new_argz;
167 *argz_len = new_argz_len;
168 return 0;
169 }
170 else
171 return ENOMEM;
172 }
173}
174
175
176char *
177argz_next (const char *argz, size_t argz_len, const char *entry)
178{
179 if (entry)
180 {
181 if (entry < argz + argz_len)
182 entry = strchr (entry, '\0') + 1;
183
184 return entry >= argz + argz_len ? NULL : (char *) entry;
185 }
186 else
187 if (argz_len > 0)
188 return (char *) argz;
189 else
190 return NULL;
191}
192
193
194/* Make '\0' separated arg vector ARGZ printable by converting all the '\0's
195 except the last into the character SEP. */
196void
197argz_stringify (char *argz, size_t len, int sep)
198{
199 if (len > 0)
200 while (1)
201 {
202 size_t part_len = strnlen (argz, len);
203 argz += part_len;
204 len -= part_len;
205 if (len-- <= 1) /* includes final '\0' we want to stop at */
206 break;
207 *argz++ = sep;
208 }
209}
210
211
212/* Returns the number of strings in ARGZ. */
213size_t
214argz_count (const char *argz, size_t len)
215{
216 size_t count = 0;
217 while (len > 0)
218 {
219 size_t part_len = strlen(argz);
220 argz += part_len + 1;
221 len -= part_len + 1;
222 count++;
223 }
224 return count;
225}
226
227
228/* Puts pointers to each string in ARGZ, plus a terminating 0 element, into
229 ARGV, which must be large enough to hold them all. */
230void
231argz_extract (const char *argz, size_t len, char **argv)
232{
233 while (len > 0)
234 {
235 size_t part_len = strlen (argz);
236 *argv++ = (char *) argz;
237 argz += part_len + 1;
238 len -= part_len + 1;
239 }
240 *argv = 0;
241}
242
243
244/* Make a '\0' separated arg vector from a unix argv vector, returning it in
245 ARGZ, and the total length in LEN. If a memory allocation error occurs,
246 ENOMEM is returned, otherwise 0. */
247error_t
248argz_create (char *const argv[], char **argz, size_t *len)
249{
250 int argc;
251 size_t tlen = 0;
252 char *const *ap;
253 char *p;
254
255 for (argc = 0; argv[argc] != NULL; ++argc)
256 tlen += strlen (argv[argc]) + 1;
257
258 if (tlen == 0)
259 *argz = NULL;
260 else
261 {
262 *argz = malloc (tlen);
263 if (*argz == NULL)
264 return ENOMEM;
265
266 for (p = *argz, ap = argv; *ap; ++ap, ++p)
267 p = stpcpy (p, *ap);
268 }
269 *len = tlen;
270
271 return 0;
272}
273
274
275/* Delete ENTRY from ARGZ & ARGZ_LEN, if any. */
276void
277argz_delete (char **argz, size_t *argz_len, char *entry)
278{
279 if (entry)
280 /* Get rid of the old value for NAME. */
281 {
282 size_t entry_len = strlen (entry) + 1;
283 *argz_len -= entry_len;
284 memmove (entry, entry + entry_len, *argz_len - (entry - *argz));
285 if (*argz_len == 0)
286 {
287 free (*argz);
288 *argz = 0;
289 }
290 }
291}
292
293
294/* Append BUF, of length BUF_LEN to *TO, of length *TO_LEN, reallocating and
295 updating *TO & *TO_LEN appropriately. If an allocation error occurs,
296 *TO's old value is freed, and *TO is set to 0. */
297static void
298str_append (char **to, size_t *to_len, const char *buf, const size_t buf_len)
299{
300 size_t new_len = *to_len + buf_len;
301 char *new_to = realloc (*to, new_len + 1);
302
303 if (new_to)
304 {
305 *((char *) mempcpy (new_to + *to_len, buf, buf_len)) = '\0';
306 *to = new_to;
307 *to_len = new_len;
308 }
309 else
310 {
311 free (*to);
312 *to = 0;
313 }
314}
315
316/* Replace any occurrences of the string STR in ARGZ with WITH, reallocating
317 ARGZ as necessary. If REPLACE_COUNT is non-zero, *REPLACE_COUNT will be
318 incremented by number of replacements performed. */
319error_t
320argz_replace (char **argz, size_t *argz_len, const char *str, const char *with,
321 unsigned *replace_count)
322{
323 error_t err = 0;
324
325 if (str && *str)
326 {
327 char *arg = 0;
328 char *src = *argz;
329 size_t src_len = *argz_len;
330 char *dst = 0;
331 size_t dst_len = 0;
332 int delayed_copy = 1; /* True while we've avoided copying anything. */
333 size_t str_len = strlen (str), with_len = strlen (with);
334
335 while (!err && (arg = argz_next (src, src_len, arg)))
336 {
337 char *match = strstr (arg, str);
338 if (match)
339 {
340 char *from = match + str_len;
341 size_t to_len = match - arg;
342 char *to = strndup (arg, to_len);
343
344 while (to && from)
345 {
346 str_append (&to, &to_len, with, with_len);
347 if (to)
348 {
349 match = strstr (from, str);
350 if (match)
351 {
352 str_append (&to, &to_len, from, match - from);
353 from = match + str_len;
354 }
355 else
356 {
357 str_append (&to, &to_len, from, strlen (from));
358 from = 0;
359 }
360 }
361 }
362
363 if (to)
364 {
365 if (delayed_copy)
366 /* We avoided copying SRC to DST until we found a match;
367 now that we've done so, copy everything from the start
368 of SRC. */
369 {
370 if (arg > src)
371 err = argz_append (&dst, &dst_len, src, (arg - src));
372 delayed_copy = 0;
373 }
374 if (! err)
375 err = argz_add (&dst, &dst_len, to);
376 free (to);
377 }
378 else
379 err = ENOMEM;
380
381 if (replace_count)
382 (*replace_count)++;
383 }
384 else if (! delayed_copy)
385 err = argz_add (&dst, &dst_len, arg);
386 }
387
388 if (! err)
389 {
390 if (! delayed_copy)
391 /* We never found any instances of str. */
392 {
393 free (src);
394 *argz = dst;
395 *argz_len = dst_len;
396 }
397 }
398 else if (dst_len > 0)
399 free (dst);
400 }
401
402 return err;
403}
diff --git a/src/util/program_lib_mempcpy.c b/src/util/program_lib_mempcpy.c
deleted file mode 100644
index af0ed452e..000000000
--- a/src/util/program_lib_mempcpy.c
+++ /dev/null
@@ -1,27 +0,0 @@
1/* Copy memory area and return pointer after last written byte.
2 Copyright (C) 2003, 2007 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
17
18/* Specification. */
19#include <string.h>
20
21/* Copy N bytes of SRC to DEST, return pointer to bytes after the
22 last written byte. */
23void *
24mempcpy (void *dest, const void *src, size_t n)
25{
26 return (char *) memcpy (dest, src, n) + n;
27}
diff --git a/src/util/program_lib_strndup.c b/src/util/program_lib_strndup.c
deleted file mode 100644
index 8d071e0e4..000000000
--- a/src/util/program_lib_strndup.c
+++ /dev/null
@@ -1,35 +0,0 @@
1/* A replacement function, for systems that lack strndup.
2
3 Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2005, 2006, 2007
4 Free Software Foundation, Inc.
5
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
9 later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19
20#include <string.h>
21
22#include <stdlib.h>
23
24char *
25strndup (char const *s, size_t n)
26{
27 size_t len = strnlen (s, n);
28 char *new = malloc (len + 1);
29
30 if (new == NULL)
31 return NULL;
32
33 new[len] = '\0';
34 return memcpy (new, s, len);
35}
diff --git a/src/util/program_lib_strnlen.c b/src/util/program_lib_strnlen.c
deleted file mode 100644
index 732c0f845..000000000
--- a/src/util/program_lib_strnlen.c
+++ /dev/null
@@ -1,29 +0,0 @@
1/* Find the length of STRING, but scan at most MAXLEN characters.
2 Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc.
3 Written by Simon Josefsson.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18
19#include <string.h>
20
21/* Find the length of STRING, but scan at most MAXLEN characters.
22 If no '\0' terminator is found in that many characters, return MAXLEN. */
23
24size_t
25strnlen (const char *string, size_t maxlen)
26{
27 const char *end = memchr (string, '\0', maxlen);
28 return end ? (size_t) (end - string) : maxlen;
29}
diff --git a/src/util/test_crypto_ksk.c b/src/util/test_crypto_ksk.c
index c9a8e57dc..f3878a0ec 100644
--- a/src/util/test_crypto_ksk.c
+++ b/src/util/test_crypto_ksk.c
@@ -170,7 +170,7 @@ testSignVerify (struct GNUNET_CRYPTO_RsaPrivateKey *hostkey)
170 } 170 }
171 if (GNUNET_SYSERR != 171 if (GNUNET_SYSERR !=
172 GNUNET_CRYPTO_rsa_verify 172 GNUNET_CRYPTO_rsa_verify
173 (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PING, &purp, &sig, &pkey)) 173 (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN, &purp, &sig, &pkey))
174 { 174 {
175 printf ("GNUNET_CRYPTO_rsa_verify failed to fail!\n"); 175 printf ("GNUNET_CRYPTO_rsa_verify failed to fail!\n");
176 ok = GNUNET_SYSERR; 176 ok = GNUNET_SYSERR;
diff --git a/src/util/test_crypto_rsa.c b/src/util/test_crypto_rsa.c
index 1d88a697b..e836ad5bb 100644
--- a/src/util/test_crypto_rsa.c
+++ b/src/util/test_crypto_rsa.c
@@ -228,7 +228,7 @@ testSignVerify ()
228 } 228 }
229 if (GNUNET_SYSERR != 229 if (GNUNET_SYSERR !=
230 GNUNET_CRYPTO_rsa_verify 230 GNUNET_CRYPTO_rsa_verify
231 (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PING, &purp, &sig, &pkey)) 231 (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_PONG_OWN, &purp, &sig, &pkey))
232 { 232 {
233 printf ("GNUNET_CRYPTO_rsa_verify failed to fail!\n"); 233 printf ("GNUNET_CRYPTO_rsa_verify failed to fail!\n");
234 ok = GNUNET_SYSERR; 234 ok = GNUNET_SYSERR;