2 * Copyright (c) 2007, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
19 solv_oom(size_t num, size_t len)
22 fprintf(stderr, "Out of memory allocating %zu*%zu bytes!\n", num, len);
24 fprintf(stderr, "Out of memory allocating %zu bytes!\n", len);
30 solv_malloc(size_t len)
32 void *r = malloc(len ? len : 1);
39 solv_malloc2(size_t num, size_t len)
41 if (len && (num * len) / len != num)
43 return solv_malloc(num * len);
47 solv_realloc(void *old, size_t len)
50 old = malloc(len ? len : 1);
52 old = realloc(old, len ? len : 1);
59 solv_realloc2(void *old, size_t num, size_t len)
61 if (len && (num * len) / len != num)
63 return solv_realloc(old, num * len);
67 solv_calloc(size_t num, size_t len)
70 if (num == 0 || len == 0)
88 solv_strdup(const char *s)
95 solv_oom(0, strlen(s));
100 solv_timems(unsigned int subtract)
105 if (gettimeofday(&tv, 0))
107 r = (((unsigned int)tv.tv_sec >> 16) * 1000) << 16;
108 r += ((unsigned int)tv.tv_sec & 0xffff) * 1000;
109 r += (unsigned int)tv.tv_usec / 1000;
113 /* bsd's qsort_r has different arguments, so we define our
114 own version in case we need to do some clever mapping
116 see also: http://sources.redhat.com/ml/libc-alpha/2008-12/msg00003.html
118 #if defined(__GLIBC__) && (defined(HAVE_QSORT_R) || defined(HAVE___QSORT_R))
121 solv_sort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *, void *), void *compard)
123 # if defined(HAVE_QSORT_R)
124 qsort_r(base, nmemb, size, compar, compard);
126 /* backported for SLE10-SP2 */
127 __qsort_r(base, nmemb, size, compar, compard);
132 #elif defined(HAVE_QSORT_R) /* not glibc, but has qsort_r() */
134 struct solv_sort_data {
135 int (*compar)(const void *, const void *, void *);
140 solv_sort_helper(void *compard, const void *a, const void *b)
142 struct solv_sort_data *d = compard;
143 return (*d->compar)(a, b, d->compard);
147 solv_sort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *, void *), void *compard)
149 struct solv_sort_data d;
152 qsort_r(base, nmemb, size, &d, solv_sort_helper);
155 #else /* not glibc and no qsort_r() */
156 /* use own version of qsort if none available */
161 solv_dupjoin(const char *str1, const char *str2, const char *str3)
165 l1 = str1 ? strlen(str1) : 0;
166 l2 = str2 ? strlen(str2) : 0;
167 l3 = str3 ? strlen(str3) : 0;
168 s = str = solv_malloc(l1 + l2 + l3 + 1);
189 solv_dupappend(const char *str1, const char *str2, const char *str3)
191 char *str = solv_dupjoin(str1, str2, str3);
192 solv_free((void *)str1);
197 solv_hex2bin(const char **strp, unsigned char *buf, int bufl)
199 const char *str = *strp;
202 for (i = 0; i < bufl; i++)
206 if (c >= '0' && c <= '9')
208 else if (c >= 'a' && c <= 'f')
210 else if (c >= 'A' && c <= 'F')
216 if (c >= '0' && c <= '9')
218 else if (c >= 'a' && c <= 'f')
220 else if (c >= 'A' && c <= 'F')
232 solv_bin2hex(const unsigned char *buf, int l, char *str)
235 for (i = 0; i < l; i++, buf++)
238 *str++ = c < 10 ? c + '0' : c + ('a' - 10);
240 *str++ = c < 10 ? c + '0' : c + ('a' - 10);
247 solv_validutf8(const char *buf)
249 const unsigned char *p;
252 for (p = (const unsigned char *)buf; (x = *p) != 0; p++)
260 /* one byte to follow */
261 if ((p[1] & 0xc0) != 0x80)
264 break; /* not minimal */
270 /* two bytes to follow */
271 if ((p[1] & 0xc0) != 0x80 || (p[2] & 0xc0) != 0x80)
273 if ((x & 0x0f) == 0 && (p[1] & 0x20) == 0)
274 break; /* not minimal */
275 if (x == 0xed && (p[1] & 0x20) != 0)
276 break; /* d800-dfff surrogate */
277 if (x == 0xef && p[1] == 0xbf && (p[2] == 0xbe || p[2] == 0xbf))
278 break; /* fffe or ffff */
284 /* three bytes to follow */
285 if ((p[1] & 0xc0) != 0x80 || (p[2] & 0xc0) != 0x80 || (p[3] & 0xc0) != 0x80)
287 if ((x & 0x07) == 0 && (p[1] & 0x30) == 0)
288 break; /* not minimal */
289 if ((x & 0x07) > 4 || ((x & 0x07) == 4 && (p[1] & 0x30) != 0))
290 break; /* above 0x10ffff */
294 break; /* maybe valid utf8, but above 0x10ffff */
296 return (const char *)p - buf;
300 solv_latin1toutf8(const char *buf)
306 for (p = buf; *p; p++)
307 if ((*(const unsigned char *)p & 128) != 0)
309 r = rp = solv_malloc(p - buf + l);
310 for (p = buf; *p; p++)
312 if ((*(const unsigned char *)p & 128) != 0)
314 *rp++ = *(const unsigned char *)p & 64 ? 0xc3 : 0xc2;
325 solv_replacebadutf8(const char *buf, int replchar)
329 char *r = 0, *rp = 0;
332 if (replchar < 0 || replchar > 0x10ffff)
335 repllen = replin = 0;
336 else if (replchar < 0x80)
339 replin = (replchar & 0x40) | 0x80;
341 else if (replchar < 0x800)
346 else if (replchar < 0x10000)
358 for (p = buf, nl = 0; *p; )
360 l = solv_validutf8(p);
370 /* found a bad char, replace with replchar */
376 *rp++ = (replchar >> 18 & 0x3f) | 0x80;
378 *rp++ = (replchar >> 12 & 0x3f) | 0x80;
380 *rp++ = (replchar >> 6 & 0x3f) | 0x80;
382 *rp++ = (replchar & 0x3f) | 0x80;
384 rp[-repllen] ^= replin;
388 while ((*(const unsigned char *)p & 0xc0) == 0x80)
393 r = rp = solv_malloc(nl + 1);