Browse code

*** empty log message ***

Andrei Pelinescu-Onciul authored on 19/11/2001 12:44:51
Showing 2 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,17 @@
0
+/*
1
+ * $Id$
2
+ */
3
+
4
+#ifndef str_h
5
+#define str_h
6
+
7
+
8
+struct _str{
9
+	char* s; /* null terminated string*/
10
+	int len; /*string len*/
11
+};
12
+
13
+typedef struct _str str;
14
+
15
+
16
+#endif
0 17
new file mode 100644
... ...
@@ -0,0 +1,110 @@
0
+/*
1
+ *$Id$
2
+ *
3
+ * - various general purpose functions
4
+ */
5
+
6
+#ifndef ut_h
7
+#define ut_h
8
+
9
+#include "dprint.h"
10
+
11
+/* converts a str to an u. short, returns the u. short and sets *err on 
12
+ * error and if err!=null
13
+ * */
14
+static inline unsigned short str2s(unsigned char* str, unsigned int len,
15
+									int *err)
16
+{
17
+	unsigned short ret;
18
+	int i;
19
+	unsigned char *limit;
20
+	unsigned char *init;
21
+	
22
+	/*init*/
23
+	ret=i=0;
24
+	limit=str+len;
25
+	init=str;
26
+	
27
+	for(;str<limit ;str++){
28
+		if ( (*str <= '9' ) && (*str >= '0') ){
29
+				ret=ret*10+*str-'0';
30
+				i++;
31
+				if (i>5) goto error_digits;
32
+		}else{
33
+				//error unknown char
34
+				goto error_char;
35
+		}
36
+	}
37
+	if (err) *err=0;
38
+	return ret;
39
+	
40
+error_digits:
41
+	DBG("str2s: ERROR: too many letters in [%s]\n", init);
42
+	if (err) *err=1;
43
+	return 0;
44
+error_char:
45
+	DBG("str2s: ERROR: unexpected char %c in %s\n", *str, init);
46
+	if (err) *err=1;
47
+	return 0;
48
+}
49
+
50
+
51
+
52
+/* converts a str to an ipv4 address, returns the address and sets *err
53
+ * if error and err!=null
54
+ */
55
+static inline unsigned int str2ip(unsigned char* str, unsigned int len,
56
+		int * err)
57
+{
58
+	unsigned int ret;
59
+	int i;
60
+	unsigned char *limit;
61
+	unsigned char *init;
62
+
63
+	/*init*/
64
+	ret=i=0;
65
+	limit=str+len;
66
+	init=str;
67
+	
68
+	for(;str<limit ;str++){
69
+		if (*str=='.'){
70
+				i++;
71
+				if (i>3) goto error_dots;
72
+		}else if ( (*str <= '9' ) && (*str >= '0') ){
73
+				((unsigned char*)&ret)[i]=((unsigned char*)&ret)[i]*10+
74
+											*str-'0';
75
+		}else{
76
+				//error unknown char
77
+				goto error_char;
78
+		}
79
+	}
80
+	if (err) *err=0;
81
+	return ret;
82
+	
83
+error_dots:
84
+	DBG("str2ip: ERROR: too many dots in [%s]\n", init);
85
+	if (err) *err=1;
86
+	return 0;
87
+error_char:
88
+	DBG("str2ip: ERROR: unexpected char %c in %s\n", *str, init);
89
+	if (err) *err=1;
90
+	return 0;
91
+}
92
+
93
+
94
+
95
+/* faster memchr version */
96
+
97
+static inline char* q_memchr(char* p, int c, unsigned int size)
98
+{
99
+	char* end;
100
+
101
+	end=p+size;
102
+	for(;p<end;p++){
103
+		if (*p==(unsigned char)c) return p;
104
+	}
105
+	return 0;
106
+}
107
+	
108
+
109
+#endif