annotate common/strhelp.c @ 1071:fc4e1fe4e4d4

(issue116) Sign binaries with test certificate if RELESE_BUILD option is not used
author Andre Heinecke <andre.heinecke@intevation.de>
date Wed, 10 Sep 2014 17:52:11 +0200
parents 698b6a9bd75e
children ffdc8cba139a
rev   line source
404
17e1c8f37d72 Add License
Andre Heinecke <aheinecke@intevation.de>
parents: 319
diff changeset
1 /* Copyright (C) 2014 by Bundesamt für Sicherheit in der Informationstechnik
17e1c8f37d72 Add License
Andre Heinecke <aheinecke@intevation.de>
parents: 319
diff changeset
2 * Software engineering by Intevation GmbH
17e1c8f37d72 Add License
Andre Heinecke <aheinecke@intevation.de>
parents: 319
diff changeset
3 *
17e1c8f37d72 Add License
Andre Heinecke <aheinecke@intevation.de>
parents: 319
diff changeset
4 * This file is Free Software under the GNU GPL (v>=2)
17e1c8f37d72 Add License
Andre Heinecke <aheinecke@intevation.de>
parents: 319
diff changeset
5 * and comes with ABSOLUTELY NO WARRANTY!
17e1c8f37d72 Add License
Andre Heinecke <aheinecke@intevation.de>
parents: 319
diff changeset
6 * See LICENSE.txt for details.
17e1c8f37d72 Add License
Andre Heinecke <aheinecke@intevation.de>
parents: 319
diff changeset
7 */
319
4077eff1dd39 Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents: 288
diff changeset
8 /* Needed to get asprintf */
4077eff1dd39 Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents: 288
diff changeset
9 #define _GNU_SOURCE 1
4077eff1dd39 Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents: 288
diff changeset
10
134
5f830969e4a1 Added missing include.
Sascha Wilde <wilde@intevation.de>
parents: 133
diff changeset
11 #include <ctype.h>
319
4077eff1dd39 Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents: 288
diff changeset
12 #include <stdarg.h>
131
9104b1b2e4da Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents: 116
diff changeset
13 #include <stdbool.h>
59
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
14 #include <stdio.h>
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
15 #include <stdlib.h>
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
16 #include <string.h>
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
17 #include <assert.h>
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
18
160
bf4bfd8843bd Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents: 143
diff changeset
19 #include <polarssl/base64.h>
bf4bfd8843bd Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents: 143
diff changeset
20
251
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
21 #ifdef WIN32
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
22 #include <windows.h>
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
23 #endif
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
24
143
b026e6d2a161 Make flawfinder (a bit more) happy.
Sascha Wilde <wilde@intevation.de>
parents: 136
diff changeset
25 /* Remarks regarding the "Flawfinder: ignore" comments in this file:
b026e6d2a161 Make flawfinder (a bit more) happy.
Sascha Wilde <wilde@intevation.de>
parents: 136
diff changeset
26 *
b026e6d2a161 Make flawfinder (a bit more) happy.
Sascha Wilde <wilde@intevation.de>
parents: 136
diff changeset
27 * - strlen:
b026e6d2a161 Make flawfinder (a bit more) happy.
Sascha Wilde <wilde@intevation.de>
parents: 136
diff changeset
28 *
b026e6d2a161 Make flawfinder (a bit more) happy.
Sascha Wilde <wilde@intevation.de>
parents: 136
diff changeset
29 * It's true that strlen might crash if input is not null
b026e6d2a161 Make flawfinder (a bit more) happy.
Sascha Wilde <wilde@intevation.de>
parents: 136
diff changeset
30 * terminated. But by design there is not safe way to get the
b026e6d2a161 Make flawfinder (a bit more) happy.
Sascha Wilde <wilde@intevation.de>
parents: 136
diff changeset
31 * length of an string in C, and defining an additional length
b026e6d2a161 Make flawfinder (a bit more) happy.
Sascha Wilde <wilde@intevation.de>
parents: 136
diff changeset
32 * parameter for string parameter will only transfere the problem to
b026e6d2a161 Make flawfinder (a bit more) happy.
Sascha Wilde <wilde@intevation.de>
parents: 136
diff changeset
33 * the caller.
b026e6d2a161 Make flawfinder (a bit more) happy.
Sascha Wilde <wilde@intevation.de>
parents: 136
diff changeset
34 */
b026e6d2a161 Make flawfinder (a bit more) happy.
Sascha Wilde <wilde@intevation.de>
parents: 136
diff changeset
35
59
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
36 static void
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
37 out_of_core(void)
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
38 {
905
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
39 fputs("\nfatal: out of memory\n", stderr);
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
40 exit(2);
59
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
41 }
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
42 void *
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
43 xmalloc( size_t n )
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
44 {
905
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
45 void *p = malloc( n );
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
46 if( !p )
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
47 out_of_core();
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
48 return p;
59
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
49 }
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
50
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
51 void *
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
52 xrealloc( void *a, size_t n )
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
53 {
905
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
54 void *p = realloc( a, n );
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
55 if( !p )
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
56 out_of_core();
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
57 return p;
59
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
58 }
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
59
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
60 char *
60
6acb1dae6185 Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents: 59
diff changeset
61 xstrndup( const char *string, const size_t len )
59
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
62 {
905
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
63 char *p = xmalloc( len + 1 );
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
64 memcpy( p, string, len );
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
65 p[len] = '\0';
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
66 return p;
59
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
67 }
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
68
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
69 unsigned int
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
70 strv_length (char **str_array)
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
71 {
905
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
72 unsigned int i = 0;
59
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
73
905
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
74 if (!str_array)
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
75 return 0;
59
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
76
905
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
77 while (str_array[i])
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
78 ++i;
59
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
79
905
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
80 return i;
59
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
81 }
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
82
116
c602d8cfa619 Refactoring: unified naming of string vector functions.
Sascha Wilde <wilde@intevation.de>
parents: 91
diff changeset
83 void strv_append (char ***pArray, const char *string, const size_t len)
59
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
84 {
905
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
85 unsigned int old_len = 0;
60
6acb1dae6185 Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents: 59
diff changeset
86
905
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
87 if (!*pArray)
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
88 {
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
89 *pArray = xmalloc(2 * sizeof(char*));
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
90 (*pArray)[0] = xstrndup(string, len);
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
91 (*pArray)[1] = NULL;
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
92 return;
60
6acb1dae6185 Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents: 59
diff changeset
93 }
905
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
94 old_len = strv_length(*pArray);
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
95 *pArray = xrealloc(*pArray, sizeof(char**) * (old_len + 2));
59
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
96
905
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
97 (*pArray)[old_len] = xstrndup(string, len);
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
98 (*pArray)[old_len + 1] = NULL;
59
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
99 }
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
100
91
80ab2168760f Also add output size handling to str_append_str
Andre Heinecke <aheinecke@intevation.de>
parents: 63
diff changeset
101 void
80ab2168760f Also add output size handling to str_append_str
Andre Heinecke <aheinecke@intevation.de>
parents: 63
diff changeset
102 str_append_str(char **pDst, size_t *dst_len, const char *appendage, const size_t len)
59
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
103 {
905
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
104 if (!appendage)
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
105 return;
59
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
106
905
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
107 if (!(*pDst))
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
108 {
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
109 *pDst = xstrndup(appendage, len);
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
110 *dst_len = len;
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
111 }
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
112 else
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
113 {
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
114 size_t new_size = (*dst_len) + len + 1;
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
115 char *p_old = *pDst;
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
116 *pDst = xmalloc(new_size);
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
117 strncpy(*pDst, p_old, *dst_len);
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
118 strncpy(*pDst + *dst_len, appendage, len);
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
119 *dst_len = new_size - 1;
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
120 (*pDst)[*dst_len] = '\0';
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
121 free (p_old);
60
6acb1dae6185 Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents: 59
diff changeset
122 }
59
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
123 }
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
124
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
125 void
116
c602d8cfa619 Refactoring: unified naming of string vector functions.
Sascha Wilde <wilde@intevation.de>
parents: 91
diff changeset
126 strv_free (char **str_array)
59
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
127 {
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
128 if (str_array)
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
129 {
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
130 int i;
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
131
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
132 for (i = 0; str_array[i] != NULL; i++)
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
133 free (str_array[i]);
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
134
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
135 free (str_array);
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
136 }
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
137 }
131
9104b1b2e4da Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents: 116
diff changeset
138
9104b1b2e4da Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents: 116
diff changeset
139 bool
9104b1b2e4da Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents: 116
diff changeset
140 str_equal (char *s1, char *s2)
9104b1b2e4da Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents: 116
diff changeset
141 {
143
b026e6d2a161 Make flawfinder (a bit more) happy.
Sascha Wilde <wilde@intevation.de>
parents: 136
diff changeset
142 size_t l1 = strlen(s1); /* Flawfinder: ignore */
b026e6d2a161 Make flawfinder (a bit more) happy.
Sascha Wilde <wilde@intevation.de>
parents: 136
diff changeset
143 size_t l2 = strlen(s2); /* Flawfinder: ignore */
131
9104b1b2e4da Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents: 116
diff changeset
144 if ((l1 == l2) &&
136
5fa4791d6d0e Replaced unnecessary strncmp with strcmp.
Sascha Wilde <wilde@intevation.de>
parents: 135
diff changeset
145 (strcmp(s1, s2) == 0))
131
9104b1b2e4da Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents: 116
diff changeset
146 return true;
9104b1b2e4da Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents: 116
diff changeset
147 else
9104b1b2e4da Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents: 116
diff changeset
148 return false;
9104b1b2e4da Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents: 116
diff changeset
149 }
9104b1b2e4da Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents: 116
diff changeset
150
9104b1b2e4da Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents: 116
diff changeset
151 bool
9104b1b2e4da Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents: 116
diff changeset
152 str_starts_with (char *s1, char *s2)
9104b1b2e4da Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents: 116
diff changeset
153 {
143
b026e6d2a161 Make flawfinder (a bit more) happy.
Sascha Wilde <wilde@intevation.de>
parents: 136
diff changeset
154 size_t l2 = strlen(s2); /* Flawfinder: ignore */
b026e6d2a161 Make flawfinder (a bit more) happy.
Sascha Wilde <wilde@intevation.de>
parents: 136
diff changeset
155 if (strncmp(s1, s2, l2) == 0)
131
9104b1b2e4da Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents: 116
diff changeset
156 return true;
9104b1b2e4da Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents: 116
diff changeset
157 else
9104b1b2e4da Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents: 116
diff changeset
158 return false;
9104b1b2e4da Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents: 116
diff changeset
159 }
133
c719d3fdbc15 Added functrion to trim white space from string.
Sascha Wilde <wilde@intevation.de>
parents: 131
diff changeset
160
c719d3fdbc15 Added functrion to trim white space from string.
Sascha Wilde <wilde@intevation.de>
parents: 131
diff changeset
161 void
c719d3fdbc15 Added functrion to trim white space from string.
Sascha Wilde <wilde@intevation.de>
parents: 131
diff changeset
162 str_trim (char **s)
c719d3fdbc15 Added functrion to trim white space from string.
Sascha Wilde <wilde@intevation.de>
parents: 131
diff changeset
163 {
c719d3fdbc15 Added functrion to trim white space from string.
Sascha Wilde <wilde@intevation.de>
parents: 131
diff changeset
164 size_t i;
135
3fb6ddd6b70d str_trim: do nothing if *s is NULL.
Sascha Wilde <wilde@intevation.de>
parents: 134
diff changeset
165 if (*s != NULL)
3fb6ddd6b70d str_trim: do nothing if *s is NULL.
Sascha Wilde <wilde@intevation.de>
parents: 134
diff changeset
166 {
3fb6ddd6b70d str_trim: do nothing if *s is NULL.
Sascha Wilde <wilde@intevation.de>
parents: 134
diff changeset
167 while (isspace(**s))
3fb6ddd6b70d str_trim: do nothing if *s is NULL.
Sascha Wilde <wilde@intevation.de>
parents: 134
diff changeset
168 (*s)++;
143
b026e6d2a161 Make flawfinder (a bit more) happy.
Sascha Wilde <wilde@intevation.de>
parents: 136
diff changeset
169 i = strlen(*s); /* Flawfinder: ignore */
135
3fb6ddd6b70d str_trim: do nothing if *s is NULL.
Sascha Wilde <wilde@intevation.de>
parents: 134
diff changeset
170 while (isspace((*s)[--i]))
3fb6ddd6b70d str_trim: do nothing if *s is NULL.
Sascha Wilde <wilde@intevation.de>
parents: 134
diff changeset
171 (*s)[i] = '\0';
3fb6ddd6b70d str_trim: do nothing if *s is NULL.
Sascha Wilde <wilde@intevation.de>
parents: 134
diff changeset
172 }
133
c719d3fdbc15 Added functrion to trim white space from string.
Sascha Wilde <wilde@intevation.de>
parents: 131
diff changeset
173 }
160
bf4bfd8843bd Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents: 143
diff changeset
174
bf4bfd8843bd Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents: 143
diff changeset
175 int str_base64_decode(char **dst, size_t *dst_size, char *src,
bf4bfd8843bd Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents: 143
diff changeset
176 size_t src_size)
bf4bfd8843bd Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents: 143
diff changeset
177 {
905
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
178 int ret = -1;
160
bf4bfd8843bd Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents: 143
diff changeset
179
905
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
180 if (!dst || *dst)
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
181 {
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
182 return -1;
160
bf4bfd8843bd Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents: 143
diff changeset
183 }
bf4bfd8843bd Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents: 143
diff changeset
184
905
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
185 /* Check the needed size for the buffer */
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
186 ret = base64_decode(NULL, dst_size,
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
187 (unsigned char *)src, src_size);
160
bf4bfd8843bd Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents: 143
diff changeset
188
905
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
189 if (ret != 0 && ret != POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL)
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
190 {
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
191 return ret;
160
bf4bfd8843bd Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents: 143
diff changeset
192 }
bf4bfd8843bd Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents: 143
diff changeset
193
905
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
194 *dst = xmalloc(*dst_size);
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
195 memset (*dst, 0, *dst_size);
160
bf4bfd8843bd Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents: 143
diff changeset
196
905
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
197 ret = base64_decode((unsigned char *)*dst, dst_size,
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
198 (unsigned char *)src, src_size);
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
199 if (ret)
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
200 {
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
201 free (*dst);
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
202 *dst = NULL;
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
203 *dst_size = 0;
160
bf4bfd8843bd Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents: 143
diff changeset
204 }
905
698b6a9bd75e Fix coding style for C code
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
205 return ret;
160
bf4bfd8843bd Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents: 143
diff changeset
206 }
251
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
207
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
208 void
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
209 xfree (void *p)
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
210 {
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
211 if (p)
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
212 free (p);
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
213 }
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
214
319
4077eff1dd39 Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents: 288
diff changeset
215 int
4077eff1dd39 Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents: 288
diff changeset
216 xasprintf (char **strp, const char *fmt, ...)
4077eff1dd39 Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents: 288
diff changeset
217 {
4077eff1dd39 Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents: 288
diff changeset
218 int ret;
4077eff1dd39 Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents: 288
diff changeset
219 va_list ap;
4077eff1dd39 Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents: 288
diff changeset
220 va_start(ap, fmt);
4077eff1dd39 Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents: 288
diff changeset
221 ret = vasprintf(strp, fmt, ap);
4077eff1dd39 Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents: 288
diff changeset
222 va_end(ap);
4077eff1dd39 Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents: 288
diff changeset
223
4077eff1dd39 Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents: 288
diff changeset
224 if (ret == -1)
4077eff1dd39 Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents: 288
diff changeset
225 out_of_core();
4077eff1dd39 Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents: 288
diff changeset
226
4077eff1dd39 Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents: 288
diff changeset
227 return ret;
4077eff1dd39 Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents: 288
diff changeset
228 }
4077eff1dd39 Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents: 288
diff changeset
229
251
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
230 #ifdef WIN32
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
231 /* Adapted from GPGOL rev. e512053 */
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
232 char *
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
233 wchar_to_utf8 (const wchar_t *string, size_t len)
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
234 {
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
235 int n, ilen;
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
236 char *result;
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
237
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
238 ilen = (int) len;
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
239 if (ilen < 0)
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
240 return NULL;
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
241
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
242 /* Note, that CP_UTF8 is not defined in Windows versions earlier
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
243 than NT.*/
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
244 n = WideCharToMultiByte (CP_UTF8, 0, string, ilen, NULL, 0, NULL, NULL);
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
245 if (n < 0)
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
246 return NULL;
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
247
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
248 result = xmalloc ((size_t)n+1);
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
249 n = WideCharToMultiByte (CP_UTF8, 0, string, ilen, result, n, NULL, NULL);
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
250 if (n < 0)
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
251 {
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
252 xfree (result);
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
253 return NULL;
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
254 }
288
c4a989a0d6cf Ensure that the utf8 string is always 0 terminated.
Andre Heinecke <aheinecke@intevation.de>
parents: 251
diff changeset
255 result[n] = 0;
251
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
256 return result;
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
257 }
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
258
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
259 /* Adapted from GPGOL rev. e512053 */
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
260 wchar_t *
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
261 utf8_to_wchar (const char *string, size_t len)
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
262 {
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
263 int n, ilen;
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
264 wchar_t *result;
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
265
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
266 ilen = (int) len;
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
267 if (ilen < 0)
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
268 return NULL;
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
269
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
270 n = MultiByteToWideChar (CP_UTF8, 0, string, ilen, NULL, 0);
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
271 if (n < 0 || n + 1 < 0)
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
272 return NULL;
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
273
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
274 result = xmalloc ((size_t)(n+1) * sizeof *result);
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
275 n = MultiByteToWideChar (CP_UTF8, 0, string, ilen, result, n);
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
276 if (n < 0)
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
277 {
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
278 xfree (result);
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
279 return NULL;
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
280 }
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
281 result[n] = 0;
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
282 return result;
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
283 }
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
284 #endif

http://wald.intevation.org/projects/trustbridge/