Browse code

Moving strcommon.[ch] from kamailio core to libkcore

Jan Janak authored on 03/03/2009 16:10:47
Showing 2 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,369 @@
1
+/*
2
+ * $Id$
3
+ *
4
+ * Copyright (C) 2007 voice-system.ro
5
+ *
6
+ * This file is part of Kamailio, a free SIP server.
7
+ *
8
+ * Kamailio is free software; you can redistribute it and/or modify
9
+ * it under the terms of the GNU General Public License as published by
10
+ * the Free Software Foundation; either version 2 of the License, or
11
+ * (at your option) any later version
12
+ *
13
+ * Kamailio is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
+ * GNU General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU General Public License 
19
+ * along with this program; if not, write to the Free Software 
20
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21
+ *
22
+
23
+ */
24
+/*!
25
+ * \file
26
+ * \brief Generic string handling functions
27
+ */
28
+
29
+#include "../../ut.h"
30
+#include "strcommon.h"
31
+
32
+/*! \brief
33
+ * add backslashes to special characters
34
+ */
35
+int escape_common(char *dst, char *src, int src_len)
36
+{
37
+	int i, j;
38
+
39
+	if(dst==0 || src==0 || src_len<=0)
40
+		return 0;
41
+	j = 0;
42
+	for(i=0; i<src_len; i++)
43
+	{
44
+		switch(src[i])
45
+		{
46
+			case '\'':
47
+				dst[j++] = '\\';
48
+				dst[j++] = src[i];
49
+				break;
50
+			case '"':
51
+				dst[j++] = '\\';
52
+				dst[j++] = src[i];
53
+				break;
54
+			case '\\':
55
+				dst[j++] = '\\';
56
+				dst[j++] = src[i];
57
+				break;
58
+			case '\0':
59
+				dst[j++] = '\\';
60
+				dst[j++] = '0';
61
+				break;
62
+			default:
63
+				dst[j++] = src[i];
64
+		}
65
+	}
66
+	return j;
67
+}
68
+
69
+/*! \brief
70
+ * remove backslashes to special characters
71
+ */
72
+int unescape_common(char *dst, char *src, int src_len)
73
+{
74
+	int i, j;
75
+
76
+	if(dst==0 || src==0 || src_len<=0)
77
+		return 0;
78
+	j = 0;
79
+	i = 0;
80
+	while(i<src_len)
81
+	{
82
+		if(src[i]=='\\' && i+1<src_len)
83
+		{
84
+			switch(src[i+1])
85
+			{
86
+				case '\'':
87
+					dst[j++] = '\'';
88
+					i++;
89
+					break;
90
+				case '"':
91
+					dst[j++] = '"';
92
+					i++;
93
+					break;
94
+				case '\\':
95
+					dst[j++] = '\\';
96
+					i++;
97
+					break;
98
+				case '0':
99
+					dst[j++] = '\0';
100
+					i++;
101
+					break;
102
+				default:
103
+					dst[j++] = src[i];
104
+			}
105
+		} else {
106
+			dst[j++] = src[i];
107
+		}
108
+		i++;
109
+	}
110
+	return j;
111
+}
112
+
113
+/*! \brief Compute MD5 checksum */
114
+void compute_md5(char *dst, char *src, int src_len)
115
+{
116
+	MD5_CTX context;
117
+	unsigned char digest[16];
118
+	MD5Init (&context);
119
+  	MD5Update (&context, src, src_len);
120
+	MD5Final (digest, &context);
121
+	string2hex(digest, 16, dst);
122
+}
123
+
124
+/*! \brief Unscape all printable ASCII characters */
125
+int unescape_user(str *sin, str *sout)
126
+{
127
+	char *at, *p, c;
128
+
129
+	if(sin==NULL || sout==NULL || sin->s==NULL || sout->s==NULL
130
+			|| sin->len<0 || sout->len < sin->len+1)
131
+		return -1;
132
+
133
+	at = sout->s;
134
+	p  = sin->s;
135
+	while(p < sin->s+sin->len)
136
+	{
137
+	    if (*p == '%')
138
+		{
139
+			p++;
140
+			switch (*p)
141
+			{
142
+				case '0':
143
+				case '1':
144
+				case '2':
145
+				case '3':
146
+				case '4':
147
+				case '5':
148
+				case '6':
149
+				case '7':
150
+				case '8':
151
+				case '9':
152
+				    c = (*p - '0') << 4;
153
+			    break;
154
+				case 'a':
155
+				case 'b':
156
+				case 'c':
157
+				case 'd':
158
+				case 'e':
159
+				case 'f':
160
+				    c = (*p - 'a' + 10) << 4;
161
+			    break;
162
+				case 'A':
163
+				case 'B':
164
+				case 'C':
165
+				case 'D':
166
+				case 'E':
167
+				case 'F':
168
+				    c = (*p - 'A' + 10) << 4;
169
+			    break;
170
+				default:
171
+				    LM_ERR("invalid hex digit <%u>\n", (unsigned int)*p);
172
+				    return -1;
173
+			}
174
+			p++;
175
+			switch (*p)
176
+			{
177
+				case '0':
178
+				case '1':
179
+				case '2':
180
+				case '3':
181
+				case '4':
182
+				case '5':
183
+				case '6':
184
+				case '7':
185
+				case '8':
186
+				case '9':
187
+				    c =  c + (*p - '0');
188
+			    break;
189
+				case 'a':
190
+				case 'b':
191
+				case 'c':
192
+				case 'd':
193
+				case 'e':
194
+				case 'f':
195
+				    c = c + (*p - 'a' + 10);
196
+			    break;
197
+				case 'A':
198
+				case 'B':
199
+				case 'C':
200
+				case 'D':
201
+				case 'E':
202
+				case 'F':
203
+				    c = c + (*p - 'A' + 10);
204
+			    break;
205
+				default:
206
+				    LM_ERR("invalid hex digit <%u>\n", (unsigned int)*p);
207
+				    return -1;
208
+			}
209
+			if ((c < 32) || (c > 126))
210
+			{
211
+			    LM_ERR("invalid escaped character <%u>\n", (unsigned int)c);
212
+			    return -1;
213
+			}
214
+			*at++ = c;
215
+	    } else {
216
+			*at++ = *p;
217
+	    }
218
+		p++;
219
+	}
220
+
221
+	*at = 0;
222
+	sout->len = at - sout->s;
223
+	
224
+	LM_DBG("unescaped string is <%s>\n", sout->s);
225
+	return 0;
226
+}
227
+
228
+/*! \brief
229
+ * Escape all printable characters that are not valid in user
230
+ * part of request uri
231
+ * no_need_to_escape = unreserved | user-unreserved
232
+ * unreserved = aplhanum | mark
233
+ * mark = - | _ | . | ! | ~ | * | ' | ( | )
234
+ * user-unreserved = & | = | + | $ | , | ; | ? | /
235
+ */
236
+int escape_user(str *sin, str *sout)
237
+{
238
+
239
+	char *at, *p;
240
+	unsigned char x;
241
+
242
+	if(sin==NULL || sout==NULL || sin->s==NULL || sout->s==NULL
243
+			|| sin->len<0 || sout->len < 3*sin->len+1)
244
+		return -1;
245
+
246
+
247
+	at = sout->s;
248
+	p  = sin->s;
249
+	while (p < sin->s+sin->len)
250
+	{
251
+	    if (*p < 32 || *p > 126)
252
+		{
253
+			LM_ERR("invalid escaped character <%u>\n", (unsigned int)*p);
254
+			return -1;
255
+	    }
256
+	    if (isdigit((int)*p) || ((*p >= 'A') && (*p <= 'Z')) ||
257
+				((*p >= 'a') && (*p <= 'z')))
258
+		{
259
+			*at = *p;
260
+	    } else {
261
+			switch (*p) {
262
+				case '-':
263
+				case '_':
264
+				case '.':
265
+				case '!':
266
+				case '~':
267
+				case '*':
268
+				case '\'':
269
+				case '(':
270
+				case ')':
271
+				case '&':
272
+				case '=':
273
+				case '+':
274
+				case '$':
275
+				case ',':
276
+				case ';':
277
+				case '?':
278
+				    *at = *p;
279
+				break;
280
+				default:
281
+				    *at++ = '%';
282
+				    x = (*p) >> 4;
283
+				    if (x < 10)
284
+					{
285
+						*at++ = x + '0';
286
+				    } else {
287
+						*at++ = x - 10 + 'a';
288
+				    }
289
+				    x = (*p) & 0x0f;
290
+				    if (x < 10) {
291
+						*at = x + '0';
292
+				    } else {
293
+						*at = x - 10 + 'a';
294
+				    }
295
+			}
296
+	    }
297
+	    at++;
298
+	    p++;
299
+	}
300
+	*at = 0;
301
+	sout->len = at - sout->s;
302
+	LM_DBG("escaped string is <%s>\n", sout->s);
303
+	return 0;
304
+}
305
+
306
+
307
+int unescape_param(str *sin, str *sout)
308
+{
309
+    return unescape_user(sin, sout);
310
+}
311
+
312
+
313
+/*! \brief
314
+ * Escape all printable characters that are not valid in
315
+ * a param part of request uri: = | ; | , |   | " | ? | &
316
+ */
317
+int escape_param(str *sin, str *sout)
318
+{
319
+    char *at, *p;
320
+    unsigned char x;
321
+
322
+    if (sin==NULL || sout==NULL || sin->s==NULL || sout->s==NULL ||
323
+        sin->len<0 || sout->len < 3*sin->len+1)
324
+        return -1;
325
+
326
+    at = sout->s;
327
+    p  = sin->s;
328
+    while (p < sin->s+sin->len) {
329
+        if (*p < 32 || *p > 126) {
330
+            LM_ERR("invalid escaped character <%u>\n", (unsigned int)*p);
331
+            return -1;
332
+        }
333
+        switch (*p) {
334
+        case ' ':
335
+        case '?':
336
+        case '&':
337
+        case '=':
338
+        case ',':
339
+        case ';':
340
+        case '"':
341
+        case '+':
342
+            *at++ = '%';
343
+            x = (*p) >> 4;
344
+            if (x < 10)
345
+            {
346
+                *at++ = x + '0';
347
+            } else {
348
+                *at++ = x - 10 + 'a';
349
+            }
350
+            x = (*p) & 0x0f;
351
+            if (x < 10) {
352
+                *at = x + '0';
353
+            } else {
354
+                *at = x - 10 + 'a';
355
+            }
356
+            break;
357
+        default:
358
+            *at = *p;
359
+        }
360
+        at++;
361
+        p++;
362
+    }
363
+    *at = 0;
364
+    sout->len = at - sout->s;
365
+    LM_DBG("escaped string is <%s>\n", sout->s);
366
+
367
+    return 0;
368
+}
369
+
0 370
new file mode 100644
... ...
@@ -0,0 +1,54 @@
1
+/*
2
+ * $Id$
3
+ *
4
+ * Copyright (C) 2007 voice-system.ro
5
+ *
6
+ * This file is part of Kamailio, a free SIP server.
7
+ *
8
+ * Kamailio is free software; you can redistribute it and/or modify
9
+ * it under the terms of the GNU General Public License as published by
10
+ * the Free Software Foundation; either version 2 of the License, or
11
+ * (at your option) any later version
12
+ *
13
+ * Kamailio is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
+ * GNU General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU General Public License 
19
+ * along with this program; if not, write to the Free Software 
20
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21
+ *
22
+ */
23
+/*!
24
+ * \file
25
+ * \brief Common string handling functions
26
+ */
27
+
28
+#ifndef _STRCOMMON_H_
29
+#define _STRCOMMON_H_
30
+
31
+#include "../../str.h"
32
+#include "../../md5.h"
33
+
34
+/*
35
+ * add backslashes to special characters
36
+ */
37
+int escape_common(char *dst, char *src, int src_len);
38
+/*
39
+ * remove backslashes to special characters
40
+ */
41
+int unescape_common(char *dst, char *src, int src_len);
42
+
43
+void compute_md5(char *dst, char *src, int src_len);
44
+
45
+int escape_user(str *sin, str *sout);
46
+
47
+int unescape_user(str *sin, str *sout);
48
+
49
+int escape_param(str *sin, str *sout);
50
+
51
+int unescape_param(str *sin, str *sout);
52
+
53
+#endif
54
+