annotate common/strhelp.c @ 400:d481b1d0956f

Resized the administrator main window.
author Raimund Renkert <rrenkert@intevation.de>
date Wed, 16 Apr 2014 10:08:32 +0200
parents 4077eff1dd39
children 17e1c8f37d72
rev   line source
319
4077eff1dd39 Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents: 288
diff changeset
1 /* Needed to get asprintf */
4077eff1dd39 Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents: 288
diff changeset
2 #define _GNU_SOURCE 1
4077eff1dd39 Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents: 288
diff changeset
3
134
5f830969e4a1 Added missing include.
Sascha Wilde <wilde@intevation.de>
parents: 133
diff changeset
4 #include <ctype.h>
319
4077eff1dd39 Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents: 288
diff changeset
5 #include <stdarg.h>
131
9104b1b2e4da Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents: 116
diff changeset
6 #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
7 #include <stdio.h>
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
8 #include <stdlib.h>
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
9 #include <string.h>
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
10 #include <assert.h>
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
11
160
bf4bfd8843bd Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents: 143
diff changeset
12 #include <polarssl/base64.h>
bf4bfd8843bd Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents: 143
diff changeset
13
251
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
14 #ifdef WIN32
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
15 #include <windows.h>
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
16 #endif
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
17
143
b026e6d2a161 Make flawfinder (a bit more) happy.
Sascha Wilde <wilde@intevation.de>
parents: 136
diff changeset
18 /* 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
19 *
b026e6d2a161 Make flawfinder (a bit more) happy.
Sascha Wilde <wilde@intevation.de>
parents: 136
diff changeset
20 * - strlen:
b026e6d2a161 Make flawfinder (a bit more) happy.
Sascha Wilde <wilde@intevation.de>
parents: 136
diff changeset
21 *
b026e6d2a161 Make flawfinder (a bit more) happy.
Sascha Wilde <wilde@intevation.de>
parents: 136
diff changeset
22 * 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
23 * 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
24 * 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
25 * 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
26 * the caller.
b026e6d2a161 Make flawfinder (a bit more) happy.
Sascha Wilde <wilde@intevation.de>
parents: 136
diff changeset
27 */
b026e6d2a161 Make flawfinder (a bit more) happy.
Sascha Wilde <wilde@intevation.de>
parents: 136
diff changeset
28
59
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
29 static void
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
30 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
31 {
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
32 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
33 exit(2);
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
34 }
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
35 void *
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
36 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
37 {
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
38 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
39 if( !p )
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
40 out_of_core();
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
41 return p;
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
42 }
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
43
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
44 void *
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
45 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
46 {
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
47 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
48 if( !p )
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
49 out_of_core();
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
50 return p;
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
51 }
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
52
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
53 char *
60
6acb1dae6185 Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents: 59
diff changeset
54 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
55 {
60
6acb1dae6185 Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents: 59
diff changeset
56 char *p = xmalloc( len + 1 );
6acb1dae6185 Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents: 59
diff changeset
57 memcpy( p, string, len );
6acb1dae6185 Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents: 59
diff changeset
58 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
59 return p;
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
60 }
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
61
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
62 unsigned int
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
63 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
64 {
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
65 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
66
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
67 if (!str_array)
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
68 return 0;
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
69
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
70 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
71 ++i;
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
72
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
73 return i;
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
74 }
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
75
116
c602d8cfa619 Refactoring: unified naming of string vector functions.
Sascha Wilde <wilde@intevation.de>
parents: 91
diff changeset
76 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
77 {
60
6acb1dae6185 Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents: 59
diff changeset
78 unsigned int old_len = 0;
6acb1dae6185 Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents: 59
diff changeset
79
6acb1dae6185 Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents: 59
diff changeset
80 if (!*pArray) {
6acb1dae6185 Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents: 59
diff changeset
81 *pArray = xmalloc(2 * sizeof(char*));
6acb1dae6185 Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents: 59
diff changeset
82 (*pArray)[0] = xstrndup(string, len);
6acb1dae6185 Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents: 59
diff changeset
83 (*pArray)[1] = NULL;
6acb1dae6185 Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents: 59
diff changeset
84 return;
6acb1dae6185 Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents: 59
diff changeset
85 }
6acb1dae6185 Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents: 59
diff changeset
86 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
87 *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
88
60
6acb1dae6185 Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents: 59
diff changeset
89 (*pArray)[old_len] = xstrndup(string, len);
6acb1dae6185 Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents: 59
diff changeset
90 (*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
91 }
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
92
91
80ab2168760f Also add output size handling to str_append_str
Andre Heinecke <aheinecke@intevation.de>
parents: 63
diff changeset
93 void
80ab2168760f Also add output size handling to str_append_str
Andre Heinecke <aheinecke@intevation.de>
parents: 63
diff changeset
94 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
95 {
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
96 if (!appendage)
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
97 return;
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
98
91
80ab2168760f Also add output size handling to str_append_str
Andre Heinecke <aheinecke@intevation.de>
parents: 63
diff changeset
99 if (!(*pDst)) {
60
6acb1dae6185 Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents: 59
diff changeset
100 *pDst = xstrndup(appendage, len);
91
80ab2168760f Also add output size handling to str_append_str
Andre Heinecke <aheinecke@intevation.de>
parents: 63
diff changeset
101 *dst_len = len;
60
6acb1dae6185 Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents: 59
diff changeset
102 } else {
91
80ab2168760f Also add output size handling to str_append_str
Andre Heinecke <aheinecke@intevation.de>
parents: 63
diff changeset
103 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
104 char *p_old = *pDst;
80ab2168760f Also add output size handling to str_append_str
Andre Heinecke <aheinecke@intevation.de>
parents: 63
diff changeset
105 *pDst = xmalloc(new_size);
80ab2168760f Also add output size handling to str_append_str
Andre Heinecke <aheinecke@intevation.de>
parents: 63
diff changeset
106 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
107 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
108 *dst_len = new_size - 1;
80ab2168760f Also add output size handling to str_append_str
Andre Heinecke <aheinecke@intevation.de>
parents: 63
diff changeset
109 (*pDst)[*dst_len] = '\0';
80ab2168760f Also add output size handling to str_append_str
Andre Heinecke <aheinecke@intevation.de>
parents: 63
diff changeset
110 free (p_old);
60
6acb1dae6185 Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents: 59
diff changeset
111 }
59
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
112 }
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
113
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
114 void
116
c602d8cfa619 Refactoring: unified naming of string vector functions.
Sascha Wilde <wilde@intevation.de>
parents: 91
diff changeset
115 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
116 {
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
117 if (str_array)
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
118 {
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
119 int i;
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 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
122 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
123
3f6378647371 Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
124 free (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 }
131
9104b1b2e4da Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents: 116
diff changeset
127
9104b1b2e4da Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents: 116
diff changeset
128 bool
9104b1b2e4da Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents: 116
diff changeset
129 str_equal (char *s1, char *s2)
9104b1b2e4da Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents: 116
diff changeset
130 {
143
b026e6d2a161 Make flawfinder (a bit more) happy.
Sascha Wilde <wilde@intevation.de>
parents: 136
diff changeset
131 size_t l1 = strlen(s1); /* Flawfinder: ignore */
b026e6d2a161 Make flawfinder (a bit more) happy.
Sascha Wilde <wilde@intevation.de>
parents: 136
diff changeset
132 size_t l2 = strlen(s2); /* Flawfinder: ignore */
131
9104b1b2e4da Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents: 116
diff changeset
133 if ((l1 == l2) &&
136
5fa4791d6d0e Replaced unnecessary strncmp with strcmp.
Sascha Wilde <wilde@intevation.de>
parents: 135
diff changeset
134 (strcmp(s1, s2) == 0))
131
9104b1b2e4da Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents: 116
diff changeset
135 return true;
9104b1b2e4da Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents: 116
diff changeset
136 else
9104b1b2e4da Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents: 116
diff changeset
137 return false;
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
9104b1b2e4da Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents: 116
diff changeset
140 bool
9104b1b2e4da Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents: 116
diff changeset
141 str_starts_with (char *s1, char *s2)
9104b1b2e4da Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents: 116
diff changeset
142 {
143
b026e6d2a161 Make flawfinder (a bit more) happy.
Sascha Wilde <wilde@intevation.de>
parents: 136
diff changeset
143 size_t l2 = strlen(s2); /* Flawfinder: ignore */
b026e6d2a161 Make flawfinder (a bit more) happy.
Sascha Wilde <wilde@intevation.de>
parents: 136
diff changeset
144 if (strncmp(s1, s2, l2) == 0)
131
9104b1b2e4da Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents: 116
diff changeset
145 return true;
9104b1b2e4da Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents: 116
diff changeset
146 else
9104b1b2e4da Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents: 116
diff changeset
147 return false;
9104b1b2e4da Added string comparison functions.
Sascha Wilde <wilde@intevation.de>
parents: 116
diff changeset
148 }
133
c719d3fdbc15 Added functrion to trim white space from string.
Sascha Wilde <wilde@intevation.de>
parents: 131
diff changeset
149
c719d3fdbc15 Added functrion to trim white space from string.
Sascha Wilde <wilde@intevation.de>
parents: 131
diff changeset
150 void
c719d3fdbc15 Added functrion to trim white space from string.
Sascha Wilde <wilde@intevation.de>
parents: 131
diff changeset
151 str_trim (char **s)
c719d3fdbc15 Added functrion to trim white space from string.
Sascha Wilde <wilde@intevation.de>
parents: 131
diff changeset
152 {
c719d3fdbc15 Added functrion to trim white space from string.
Sascha Wilde <wilde@intevation.de>
parents: 131
diff changeset
153 size_t i;
135
3fb6ddd6b70d str_trim: do nothing if *s is NULL.
Sascha Wilde <wilde@intevation.de>
parents: 134
diff changeset
154 if (*s != NULL)
3fb6ddd6b70d str_trim: do nothing if *s is NULL.
Sascha Wilde <wilde@intevation.de>
parents: 134
diff changeset
155 {
3fb6ddd6b70d str_trim: do nothing if *s is NULL.
Sascha Wilde <wilde@intevation.de>
parents: 134
diff changeset
156 while (isspace(**s))
3fb6ddd6b70d str_trim: do nothing if *s is NULL.
Sascha Wilde <wilde@intevation.de>
parents: 134
diff changeset
157 (*s)++;
143
b026e6d2a161 Make flawfinder (a bit more) happy.
Sascha Wilde <wilde@intevation.de>
parents: 136
diff changeset
158 i = strlen(*s); /* Flawfinder: ignore */
135
3fb6ddd6b70d str_trim: do nothing if *s is NULL.
Sascha Wilde <wilde@intevation.de>
parents: 134
diff changeset
159 while (isspace((*s)[--i]))
3fb6ddd6b70d str_trim: do nothing if *s is NULL.
Sascha Wilde <wilde@intevation.de>
parents: 134
diff changeset
160 (*s)[i] = '\0';
3fb6ddd6b70d str_trim: do nothing if *s is NULL.
Sascha Wilde <wilde@intevation.de>
parents: 134
diff changeset
161 }
133
c719d3fdbc15 Added functrion to trim white space from string.
Sascha Wilde <wilde@intevation.de>
parents: 131
diff changeset
162 }
160
bf4bfd8843bd Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents: 143
diff changeset
163
bf4bfd8843bd Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents: 143
diff changeset
164 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
165 size_t src_size)
bf4bfd8843bd Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents: 143
diff changeset
166 {
bf4bfd8843bd Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents: 143
diff changeset
167 int ret = -1;
bf4bfd8843bd Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents: 143
diff changeset
168
bf4bfd8843bd Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents: 143
diff changeset
169 if (!dst || *dst) {
bf4bfd8843bd Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents: 143
diff changeset
170 return -1;
bf4bfd8843bd Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents: 143
diff changeset
171 }
bf4bfd8843bd Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents: 143
diff changeset
172
bf4bfd8843bd Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents: 143
diff changeset
173 /* Check the needed size for the buffer */
bf4bfd8843bd Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents: 143
diff changeset
174 ret = base64_decode(NULL, dst_size,
bf4bfd8843bd Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents: 143
diff changeset
175 (unsigned char *)src, src_size);
bf4bfd8843bd Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents: 143
diff changeset
176
bf4bfd8843bd Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents: 143
diff changeset
177 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
178 return ret;
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
bf4bfd8843bd Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents: 143
diff changeset
181 *dst = xmalloc(*dst_size);
bf4bfd8843bd Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents: 143
diff changeset
182 memset (*dst, 0, *dst_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 ret = base64_decode((unsigned char *)*dst, dst_size,
bf4bfd8843bd Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents: 143
diff changeset
185 (unsigned char *)src, src_size);
183
4def8b263dd3 Fix str_base64_decode
Andre Heinecke <aheinecke@intevation.de>
parents: 160
diff changeset
186 if (ret) {
160
bf4bfd8843bd Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents: 143
diff changeset
187 free (*dst);
bf4bfd8843bd Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents: 143
diff changeset
188 *dst = NULL;
183
4def8b263dd3 Fix str_base64_decode
Andre Heinecke <aheinecke@intevation.de>
parents: 160
diff changeset
189 *dst_size = 0;
160
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 return ret;
bf4bfd8843bd Add memory allocating base64 decode function
Andre Heinecke <aheinecke@intevation.de>
parents: 143
diff changeset
192 }
251
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
193
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
194 void
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
195 xfree (void *p)
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
196 {
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
197 if (p)
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
198 free (p);
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
199 }
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
200
319
4077eff1dd39 Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents: 288
diff changeset
201 int
4077eff1dd39 Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents: 288
diff changeset
202 xasprintf (char **strp, const char *fmt, ...)
4077eff1dd39 Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents: 288
diff changeset
203 {
4077eff1dd39 Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents: 288
diff changeset
204 int ret;
4077eff1dd39 Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents: 288
diff changeset
205 va_list ap;
4077eff1dd39 Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents: 288
diff changeset
206 va_start(ap, fmt);
4077eff1dd39 Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents: 288
diff changeset
207 ret = vasprintf(strp, fmt, ap);
4077eff1dd39 Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents: 288
diff changeset
208 va_end(ap);
4077eff1dd39 Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents: 288
diff changeset
209
4077eff1dd39 Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents: 288
diff changeset
210 if (ret == -1)
4077eff1dd39 Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents: 288
diff changeset
211 out_of_core();
4077eff1dd39 Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents: 288
diff changeset
212
4077eff1dd39 Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents: 288
diff changeset
213 return ret;
4077eff1dd39 Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents: 288
diff changeset
214 }
4077eff1dd39 Added terminateing version of asprintf.
Sascha Wilde <wilde@intevation.de>
parents: 288
diff changeset
215
251
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
216 #ifdef WIN32
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
217 /* Adapted from GPGOL rev. e512053 */
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
218 char *
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
219 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
220 {
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
221 int n, ilen;
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
222 char *result;
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
223
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
224 ilen = (int) len;
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
225 if (ilen < 0)
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
226 return NULL;
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 /* 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
229 than NT.*/
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
230 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
231 if (n < 0)
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
232 return NULL;
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
233
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
234 result = xmalloc ((size_t)n+1);
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
235 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
236 if (n < 0)
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 xfree (result);
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 }
288
c4a989a0d6cf Ensure that the utf8 string is always 0 terminated.
Andre Heinecke <aheinecke@intevation.de>
parents: 251
diff changeset
241 result[n] = 0;
251
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
242 return result;
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
243 }
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 /* Adapted from GPGOL rev. e512053 */
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
246 wchar_t *
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
247 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
248 {
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
249 int n, ilen;
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
250 wchar_t *result;
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 ilen = (int) len;
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
253 if (ilen < 0)
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
254 return NULL;
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 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
257 if (n < 0 || n + 1 < 0)
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
258 return NULL;
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
259
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
260 result = xmalloc ((size_t)(n+1) * sizeof *result);
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
261 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
262 if (n < 0)
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
263 {
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
264 xfree (result);
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[n] = 0;
c596568fa45b Add utf16 conversion functions for Windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 183
diff changeset
268 return result;
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 #endif

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