Mercurial > trustbridge
annotate common/strhelp.c @ 708:bd48fae793b6
(Issue25) Log update check success, change header labels.
author | Andre Heinecke <andre.heinecke@intevation.de> |
---|---|
date | Wed, 02 Jul 2014 12:49:11 +0200 |
parents | 17e1c8f37d72 |
children | 698b6a9bd75e |
rev | line source |
---|---|
404 | 1 /* Copyright (C) 2014 by Bundesamt für Sicherheit in der Informationstechnik |
2 * Software engineering by Intevation GmbH | |
3 * | |
4 * This file is Free Software under the GNU GPL (v>=2) | |
5 * and comes with ABSOLUTELY NO WARRANTY! | |
6 * See LICENSE.txt for details. | |
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 | 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 { |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
39 fputs("\nfatal: out of memory\n", stderr); |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
40 exit(2); |
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 { |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
45 void *p = malloc( n ); |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
46 if( !p ) |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
47 out_of_core(); |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
48 return p; |
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 { |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
54 void *p = realloc( a, n ); |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
55 if( !p ) |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
56 out_of_core(); |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
57 return p; |
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 { |
60
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
63 char *p = xmalloc( len + 1 ); |
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
64 memcpy( p, string, len ); |
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
65 p[len] = '\0'; |
59
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
66 return p; |
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 { |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
72 unsigned int i = 0; |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
73 |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
74 if (!str_array) |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
75 return 0; |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
76 |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
77 while (str_array[i]) |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
78 ++i; |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
79 |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
80 return i; |
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 { |
60
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
85 unsigned int old_len = 0; |
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
86 |
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
87 if (!*pArray) { |
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
88 *pArray = xmalloc(2 * sizeof(char*)); |
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
89 (*pArray)[0] = xstrndup(string, len); |
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
90 (*pArray)[1] = NULL; |
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
91 return; |
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
92 } |
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
93 old_len = strv_length(*pArray); |
59
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
94 *pArray = xrealloc(*pArray, sizeof(char**) * (old_len + 2)); |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
95 |
60
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
96 (*pArray)[old_len] = xstrndup(string, len); |
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
97 (*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
|
98 } |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
99 |
91
80ab2168760f
Also add output size handling to str_append_str
Andre Heinecke <aheinecke@intevation.de>
parents:
63
diff
changeset
|
100 void |
80ab2168760f
Also add output size handling to str_append_str
Andre Heinecke <aheinecke@intevation.de>
parents:
63
diff
changeset
|
101 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
|
102 { |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
103 if (!appendage) |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
104 return; |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
105 |
91
80ab2168760f
Also add output size handling to str_append_str
Andre Heinecke <aheinecke@intevation.de>
parents:
63
diff
changeset
|
106 if (!(*pDst)) { |
60
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
107 *pDst = xstrndup(appendage, len); |
91
80ab2168760f
Also add output size handling to str_append_str
Andre Heinecke <aheinecke@intevation.de>
parents:
63
diff
changeset
|
108 *dst_len = len; |
60
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
109 } else { |
91
80ab2168760f
Also add output size handling to str_append_str
Andre Heinecke <aheinecke@intevation.de>
parents:
63
diff
changeset
|
110 size_t new_size = (*dst_len) + len + 1; |
80ab2168760f
Also add output size handling to str_append_str
Andre Heinecke <aheinecke@intevation.de>
parents:
63
diff
changeset
|
111 char *p_old = *pDst; |
80ab2168760f
Also add output size handling to str_append_str
Andre Heinecke <aheinecke@intevation.de>
parents:
63
diff
changeset
|
112 *pDst = xmalloc(new_size); |
80ab2168760f
Also add output size handling to str_append_str
Andre Heinecke <aheinecke@intevation.de>
parents:
63
diff
changeset
|
113 strncpy(*pDst, p_old, *dst_len); |
80ab2168760f
Also add output size handling to str_append_str
Andre Heinecke <aheinecke@intevation.de>
parents:
63
diff
changeset
|
114 strncpy(*pDst + *dst_len, appendage, len); |
80ab2168760f
Also add output size handling to str_append_str
Andre Heinecke <aheinecke@intevation.de>
parents:
63
diff
changeset
|
115 *dst_len = new_size - 1; |
80ab2168760f
Also add output size handling to str_append_str
Andre Heinecke <aheinecke@intevation.de>
parents:
63
diff
changeset
|
116 (*pDst)[*dst_len] = '\0'; |
80ab2168760f
Also add output size handling to str_append_str
Andre Heinecke <aheinecke@intevation.de>
parents:
63
diff
changeset
|
117 free (p_old); |
60
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
118 } |
59
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
119 } |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
120 |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
121 void |
116
c602d8cfa619
Refactoring: unified naming of string vector functions.
Sascha Wilde <wilde@intevation.de>
parents:
91
diff
changeset
|
122 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
|
123 { |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
124 if (str_array) |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
125 { |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
126 int i; |
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 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
|
129 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
|
130 |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
131 free (str_array); |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
132 } |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
133 } |
131
9104b1b2e4da
Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents:
116
diff
changeset
|
134 |
9104b1b2e4da
Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents:
116
diff
changeset
|
135 bool |
9104b1b2e4da
Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents:
116
diff
changeset
|
136 str_equal (char *s1, char *s2) |
9104b1b2e4da
Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents:
116
diff
changeset
|
137 { |
143
b026e6d2a161
Make flawfinder (a bit more) happy.
Sascha Wilde <wilde@intevation.de>
parents:
136
diff
changeset
|
138 size_t l1 = strlen(s1); /* Flawfinder: ignore */ |
b026e6d2a161
Make flawfinder (a bit more) happy.
Sascha Wilde <wilde@intevation.de>
parents:
136
diff
changeset
|
139 size_t l2 = strlen(s2); /* Flawfinder: ignore */ |
131
9104b1b2e4da
Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents:
116
diff
changeset
|
140 if ((l1 == l2) && |
136
5fa4791d6d0e
Replaced unnecessary strncmp with strcmp.
Sascha Wilde <wilde@intevation.de>
parents:
135
diff
changeset
|
141 (strcmp(s1, s2) == 0)) |
131
9104b1b2e4da
Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents:
116
diff
changeset
|
142 return true; |
9104b1b2e4da
Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents:
116
diff
changeset
|
143 else |
9104b1b2e4da
Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents:
116
diff
changeset
|
144 return false; |
9104b1b2e4da
Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents:
116
diff
changeset
|
145 } |
9104b1b2e4da
Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents:
116
diff
changeset
|
146 |
9104b1b2e4da
Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents:
116
diff
changeset
|
147 bool |
9104b1b2e4da
Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents:
116
diff
changeset
|
148 str_starts_with (char *s1, char *s2) |
9104b1b2e4da
Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents:
116
diff
changeset
|
149 { |
143
b026e6d2a161
Make flawfinder (a bit more) happy.
Sascha Wilde <wilde@intevation.de>
parents:
136
diff
changeset
|
150 size_t l2 = strlen(s2); /* Flawfinder: ignore */ |
b026e6d2a161
Make flawfinder (a bit more) happy.
Sascha Wilde <wilde@intevation.de>
parents:
136
diff
changeset
|
151 if (strncmp(s1, s2, l2) == 0) |
131
9104b1b2e4da
Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents:
116
diff
changeset
|
152 return true; |
9104b1b2e4da
Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents:
116
diff
changeset
|
153 else |
9104b1b2e4da
Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents:
116
diff
changeset
|
154 return false; |
9104b1b2e4da
Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents:
116
diff
changeset
|
155 } |
133
c719d3fdbc15
Added functrion to trim white space from string.
Sascha Wilde <wilde@intevation.de>
parents:
131
diff
changeset
|
156 |
c719d3fdbc15
Added functrion to trim white space from string.
Sascha Wilde <wilde@intevation.de>
parents:
131
diff
changeset
|
157 void |
c719d3fdbc15
Added functrion to trim white space from string.
Sascha Wilde <wilde@intevation.de>
parents:
131
diff
changeset
|
158 str_trim (char **s) |
c719d3fdbc15
Added functrion to trim white space from string.
Sascha Wilde <wilde@intevation.de>
parents:
131
diff
changeset
|
159 { |
c719d3fdbc15
Added functrion to trim white space from string.
Sascha Wilde <wilde@intevation.de>
parents:
131
diff
changeset
|
160 size_t i; |
135
3fb6ddd6b70d
str_trim: do nothing if *s is NULL.
Sascha Wilde <wilde@intevation.de>
parents:
134
diff
changeset
|
161 if (*s != NULL) |
3fb6ddd6b70d
str_trim: do nothing if *s is NULL.
Sascha Wilde <wilde@intevation.de>
parents:
134
diff
changeset
|
162 { |
3fb6ddd6b70d
str_trim: do nothing if *s is NULL.
Sascha Wilde <wilde@intevation.de>
parents:
134
diff
changeset
|
163 while (isspace(**s)) |
3fb6ddd6b70d
str_trim: do nothing if *s is NULL.
Sascha Wilde <wilde@intevation.de>
parents:
134
diff
changeset
|
164 (*s)++; |
143
b026e6d2a161
Make flawfinder (a bit more) happy.
Sascha Wilde <wilde@intevation.de>
parents:
136
diff
changeset
|
165 i = strlen(*s); /* Flawfinder: ignore */ |
135
3fb6ddd6b70d
str_trim: do nothing if *s is NULL.
Sascha Wilde <wilde@intevation.de>
parents:
134
diff
changeset
|
166 while (isspace((*s)[--i])) |
3fb6ddd6b70d
str_trim: do nothing if *s is NULL.
Sascha Wilde <wilde@intevation.de>
parents:
134
diff
changeset
|
167 (*s)[i] = '\0'; |
3fb6ddd6b70d
str_trim: do nothing if *s is NULL.
Sascha Wilde <wilde@intevation.de>
parents:
134
diff
changeset
|
168 } |
133
c719d3fdbc15
Added functrion to trim white space from string.
Sascha Wilde <wilde@intevation.de>
parents:
131
diff
changeset
|
169 } |
160
bf4bfd8843bd
Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents:
143
diff
changeset
|
170 |
bf4bfd8843bd
Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents:
143
diff
changeset
|
171 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
|
172 size_t src_size) |
bf4bfd8843bd
Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents:
143
diff
changeset
|
173 { |
bf4bfd8843bd
Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents:
143
diff
changeset
|
174 int ret = -1; |
bf4bfd8843bd
Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents:
143
diff
changeset
|
175 |
bf4bfd8843bd
Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents:
143
diff
changeset
|
176 if (!dst || *dst) { |
bf4bfd8843bd
Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents:
143
diff
changeset
|
177 return -1; |
bf4bfd8843bd
Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents:
143
diff
changeset
|
178 } |
bf4bfd8843bd
Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents:
143
diff
changeset
|
179 |
bf4bfd8843bd
Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents:
143
diff
changeset
|
180 /* Check the needed size for the buffer */ |
bf4bfd8843bd
Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents:
143
diff
changeset
|
181 ret = base64_decode(NULL, dst_size, |
bf4bfd8843bd
Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents:
143
diff
changeset
|
182 (unsigned char *)src, src_size); |
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 if (ret != 0 && ret != POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL) { |
bf4bfd8843bd
Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents:
143
diff
changeset
|
185 return ret; |
bf4bfd8843bd
Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents:
143
diff
changeset
|
186 } |
bf4bfd8843bd
Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents:
143
diff
changeset
|
187 |
bf4bfd8843bd
Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents:
143
diff
changeset
|
188 *dst = xmalloc(*dst_size); |
bf4bfd8843bd
Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents:
143
diff
changeset
|
189 memset (*dst, 0, *dst_size); |
bf4bfd8843bd
Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents:
143
diff
changeset
|
190 |
bf4bfd8843bd
Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents:
143
diff
changeset
|
191 ret = base64_decode((unsigned char *)*dst, dst_size, |
bf4bfd8843bd
Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents:
143
diff
changeset
|
192 (unsigned char *)src, src_size); |
183
4def8b263dd3
Fix str_base64_decode
Andre Heinecke <aheinecke@intevation.de>
parents:
160
diff
changeset
|
193 if (ret) { |
160
bf4bfd8843bd
Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents:
143
diff
changeset
|
194 free (*dst); |
bf4bfd8843bd
Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents:
143
diff
changeset
|
195 *dst = NULL; |
183
4def8b263dd3
Fix str_base64_decode
Andre Heinecke <aheinecke@intevation.de>
parents:
160
diff
changeset
|
196 *dst_size = 0; |
160
bf4bfd8843bd
Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents:
143
diff
changeset
|
197 } |
bf4bfd8843bd
Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents:
143
diff
changeset
|
198 return ret; |
bf4bfd8843bd
Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents:
143
diff
changeset
|
199 } |
251
c596568fa45b
Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents:
183
diff
changeset
|
200 |
c596568fa45b
Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents:
183
diff
changeset
|
201 void |
c596568fa45b
Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents:
183
diff
changeset
|
202 xfree (void *p) |
c596568fa45b
Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents:
183
diff
changeset
|
203 { |
c596568fa45b
Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents:
183
diff
changeset
|
204 if (p) |
c596568fa45b
Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents:
183
diff
changeset
|
205 free (p); |
c596568fa45b
Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents:
183
diff
changeset
|
206 } |
c596568fa45b
Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents:
183
diff
changeset
|
207 |
319
4077eff1dd39
Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents:
288
diff
changeset
|
208 int |
4077eff1dd39
Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents:
288
diff
changeset
|
209 xasprintf (char **strp, const char *fmt, ...) |
4077eff1dd39
Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents:
288
diff
changeset
|
210 { |
4077eff1dd39
Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents:
288
diff
changeset
|
211 int ret; |
4077eff1dd39
Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents:
288
diff
changeset
|
212 va_list ap; |
4077eff1dd39
Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents:
288
diff
changeset
|
213 va_start(ap, fmt); |
4077eff1dd39
Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents:
288
diff
changeset
|
214 ret = vasprintf(strp, fmt, ap); |
4077eff1dd39
Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents:
288
diff
changeset
|
215 va_end(ap); |
4077eff1dd39
Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents:
288
diff
changeset
|
216 |
4077eff1dd39
Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents:
288
diff
changeset
|
217 if (ret == -1) |
4077eff1dd39
Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents:
288
diff
changeset
|
218 out_of_core(); |
4077eff1dd39
Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents:
288
diff
changeset
|
219 |
4077eff1dd39
Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents:
288
diff
changeset
|
220 return ret; |
4077eff1dd39
Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents:
288
diff
changeset
|
221 } |
4077eff1dd39
Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents:
288
diff
changeset
|
222 |
251
c596568fa45b
Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents:
183
diff
changeset
|
223 #ifdef WIN32 |
c596568fa45b
Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents:
183
diff
changeset
|
224 /* Adapted from GPGOL rev. e512053 */ |
c596568fa45b
Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents:
183
diff
changeset
|
225 char * |
c596568fa45b
Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents:
183
diff
changeset
|
226 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
|
227 { |
c596568fa45b
Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents:
183
diff
changeset
|
228 int n, ilen; |
c596568fa45b
Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents:
183
diff
changeset
|
229 char *result; |
c596568fa45b
Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents:
183
diff
changeset
|
230 |
c596568fa45b
Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents:
183
diff
changeset
|
231 ilen = (int) len; |
c596568fa45b
Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents:
183
diff
changeset
|
232 if (ilen < 0) |
c596568fa45b
Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents:
183
diff
changeset
|
233 return NULL; |
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 /* 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
|
236 than NT.*/ |
c596568fa45b
Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents:
183
diff
changeset
|
237 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
|
238 if (n < 0) |
c596568fa45b
Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents:
183
diff
changeset
|
239 return NULL; |
c596568fa45b
Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents:
183
diff
changeset
|
240 |
c596568fa45b
Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents:
183
diff
changeset
|
241 result = xmalloc ((size_t)n+1); |
c596568fa45b
Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents:
183
diff
changeset
|
242 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
|
243 if (n < 0) |
c596568fa45b
Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents:
183
diff
changeset
|
244 { |
c596568fa45b
Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents:
183
diff
changeset
|
245 xfree (result); |
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 } |
288
c4a989a0d6cf
Ensure that the utf8 string is always 0 terminated.
Andre Heinecke <aheinecke@intevation.de>
parents:
251
diff
changeset
|
248 result[n] = 0; |
251
c596568fa45b
Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents:
183
diff
changeset
|
249 return result; |
c596568fa45b
Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents:
183
diff
changeset
|
250 } |
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 /* Adapted from GPGOL rev. e512053 */ |
c596568fa45b
Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents:
183
diff
changeset
|
253 wchar_t * |
c596568fa45b
Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents:
183
diff
changeset
|
254 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
|
255 { |
c596568fa45b
Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents:
183
diff
changeset
|
256 int n, ilen; |
c596568fa45b
Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents:
183
diff
changeset
|
257 wchar_t *result; |
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 ilen = (int) len; |
c596568fa45b
Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents:
183
diff
changeset
|
260 if (ilen < 0) |
c596568fa45b
Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents:
183
diff
changeset
|
261 return NULL; |
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 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
|
264 if (n < 0 || n + 1 < 0) |
c596568fa45b
Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents:
183
diff
changeset
|
265 return NULL; |
c596568fa45b
Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents:
183
diff
changeset
|
266 |
c596568fa45b
Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents:
183
diff
changeset
|
267 result = xmalloc ((size_t)(n+1) * sizeof *result); |
c596568fa45b
Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents:
183
diff
changeset
|
268 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
|
269 if (n < 0) |
c596568fa45b
Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents:
183
diff
changeset
|
270 { |
c596568fa45b
Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents:
183
diff
changeset
|
271 xfree (result); |
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[n] = 0; |
c596568fa45b
Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents:
183
diff
changeset
|
275 return result; |
c596568fa45b
Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents:
183
diff
changeset
|
276 } |
c596568fa45b
Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents:
183
diff
changeset
|
277 #endif |