nexmon – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /* source: utils.c */ |
2 | /* Copyright Gerhard Rieger */ |
||
3 | /* Published under the GNU General Public License V.2, see file COPYING */ |
||
4 | |||
5 | /* useful additions to C library */ |
||
6 | |||
7 | #include "config.h" |
||
8 | |||
9 | #include "sysincludes.h" |
||
10 | |||
11 | #include "compat.h" /* socklen_t */ |
||
12 | #include "mytypes.h" |
||
13 | #include "sycls.h" |
||
14 | #include "utils.h" |
||
15 | |||
16 | |||
17 | #if !HAVE_PROTOTYPE_LIB_memrchr |
||
18 | /* GNU extension, available since glibc 2.1.91 */ |
||
19 | void *memrchr(const void *s, int c, size_t n) { |
||
20 | const unsigned char *t = ((unsigned char *)s)+n; |
||
21 | while (--t >= (unsigned char *)s) { |
||
22 | if (*t == c) break; |
||
23 | } |
||
24 | if (t < (unsigned char *)s) |
||
25 | return NULL; |
||
26 | return (void *)t; |
||
27 | } |
||
28 | #endif /* !HAVE_PROTOTYPE_LIB_memrchr */ |
||
29 | |||
30 | void *memdup(const void *src, size_t n) { |
||
31 | void *dest; |
||
32 | |||
33 | if ((dest = Malloc(n)) == NULL) { |
||
34 | return NULL; |
||
35 | } |
||
36 | |||
37 | memcpy(dest, src, n); |
||
38 | return dest; |
||
39 | } |
||
40 | |||
41 | /* search the keyword-table for a match of the leading part of name. */ |
||
42 | /* returns the pointer to the matching field of the keyword or NULL if no |
||
43 | keyword was found. */ |
||
44 | const struct wordent *keyw(const struct wordent *keywds, const char *name, unsigned int nkeys) { |
||
45 | unsigned int lower, upper, mid; |
||
46 | int r; |
||
47 | |||
48 | lower = 0; |
||
49 | upper = nkeys; |
||
50 | |||
51 | while (upper - lower > 1) |
||
52 | { |
||
53 | mid = (upper + lower) >> 1; |
||
54 | if (!(r = strcasecmp(keywds[mid].name, name))) |
||
55 | { |
||
56 | return &keywds[mid]; |
||
57 | } |
||
58 | if (r < 0) |
||
59 | lower = mid; |
||
60 | else |
||
61 | upper = mid; |
||
62 | } |
||
63 | if (nkeys > 0 && !(strcasecmp(keywds[lower].name, name))) |
||
64 | { |
||
65 | return &keywds[lower]; |
||
66 | } |
||
67 | return NULL; |
||
68 | } |
||
69 | |||
70 | /* Linux: setenv(), AIX (4.3?): putenv() */ |
||
71 | #if !HAVE_SETENV |
||
72 | int setenv(const char *name, const char *value, int overwrite) { |
||
73 | int result; |
||
74 | char *env; |
||
75 | if (!overwrite) { |
||
76 | if (getenv(name)) return 0; /* already exists */ |
||
77 | } |
||
78 | if ((env = Malloc(strlen(name)+strlen(value)+2)) == NULL) { |
||
79 | return -1; |
||
80 | } |
||
81 | sprintf(env, "%s=%s", name, value); |
||
82 | if ((result = putenv(env)) != 0) { /* AIX docu says "... nonzero ..." */ |
||
83 | free(env); |
||
84 | result = -1; |
||
85 | } |
||
86 | /* linux "man putenv" says: ...this string becomes part of the environment*/ |
||
87 | return result; |
||
88 | } |
||
89 | #endif /* !HAVE_SETENV */ |
||
90 | |||
91 | |||
92 | |||
93 | /* sanitizes an "untrusted" character. output buffer must provide at least 4 |
||
94 | characters space. |
||
95 | Does not append \0. returns length out output (currently: max 4) */ |
||
96 | static size_t sanitize_char(char c, char *o, int style) { |
||
97 | int hn; /* high nibble */ |
||
98 | int ln; /* low nibble */ |
||
99 | int n; /* written chars */ |
||
100 | if (isprint(c)) { |
||
101 | *o = c; |
||
102 | return 1; |
||
103 | } |
||
104 | *o++ = '\\'; |
||
105 | n = 2; |
||
106 | switch (c) { |
||
107 | case '\0': *o++ = '0'; break; |
||
108 | case '\a': *o++ = 'a'; break; |
||
109 | case '\b': *o++ = 'b'; break; |
||
110 | case '\t': *o++ = 't'; break; |
||
111 | case '\n': *o++ = 'n'; break; |
||
112 | case '\v': *o++ = 'v'; break; |
||
113 | case '\f': *o++ = 'f'; break; |
||
114 | case '\r': *o++ = 'r'; break; |
||
115 | case '\'': *o++ = '\''; break; |
||
116 | case '\"': *o++ = '"'; break; |
||
117 | case '\\': *o++ = '\\'; break; |
||
118 | default: |
||
119 | *o++ = 'x'; |
||
120 | hn = (c>>4)&0x0f; |
||
121 | ln = c&0x0f; |
||
122 | *o++ = (hn>=10 ? (('A'-1)+(hn-10)) : ('0'+hn)); |
||
123 | *o++ = (ln>=10 ? (('A'-1)+(ln-10)) : ('0'+ln)); |
||
124 | n = 4; |
||
125 | } |
||
126 | return n; |
||
127 | } |
||
128 | |||
129 | /* sanitizes "untrusted" text, replacing special control characters with the C |
||
130 | string version (eg."\n"), and replacing unprintable chars with hex |
||
131 | representation ("\xAB"). |
||
132 | text can grow to four times of input, so keep output buffer long enough! |
||
133 | returns a pointer to the first untouched byte of the output buffer. |
||
134 | Output is not \0 terminated. |
||
135 | */ |
||
136 | char *sanitize_string(const char *data, /* input data */ |
||
137 | size_t bytes, /* length of input data, >=0 */ |
||
138 | char *coded, /* output buffer, must be long enough */ |
||
139 | int style |
||
140 | ) { |
||
141 | int c; |
||
142 | |||
143 | while (bytes > 0) { |
||
144 | c = *(unsigned char *)data++; |
||
145 | coded += sanitize_char(c, coded, style); |
||
146 | --bytes; |
||
147 | } |
||
148 | return coded; |
||
149 | } |
||
150 | |||
151 | /* copies a substring out of a given buff |
||
152 | returns scratch, \0 terminated; scratch must provide len+1 bytes |
||
153 | */ |
||
154 | char *xiosubstr(char *scratch, const char *str, size_t from, size_t len) { |
||
155 | char *scratch0 = scratch; |
||
156 | str += from; |
||
157 | while (len--) { |
||
158 | *scratch++ = *str++; |
||
159 | } |
||
160 | *scratch = '\0'; |
||
161 | return scratch0; |
||
162 | } |
||
163 | |||
164 | |||
165 | /* since version 1.7.2.4 socat supports C-99 behaviour of snprintf but still |
||
166 | can handle the old glibc case with -1 return on truncation. |
||
167 | Do not rely on exact return value in case of truncation |
||
168 | */ |
||
169 | int xio_snprintf(char *str, size_t size, const char *format, ...) { |
||
170 | va_list ap; |
||
171 | int result; |
||
172 | |||
173 | va_start(ap, format); |
||
174 | result = vsnprintf(str, size, format, ap); |
||
175 | #if ! HAVE_C99_SNPRINTF |
||
176 | if (result < 0) { |
||
177 | result = size+63; /* indicate truncation with just some guess */ |
||
178 | } |
||
179 | #endif /* !HAVE_C99_SNPRINTF */ |
||
180 | va_end(ap); |
||
181 | return result; |
||
182 | } |