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