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