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)
79 /* this was solv_realloc2(old, len, size), but we now overshoot
80 * for huge len sizes */
82 solv_extend_realloc(void *old, size_t len, size_t size, size_t block)
84 size_t xblock = (block + 1) << 5;
85 len = (len + block) & ~block;
86 if (len >= xblock && xblock)
89 while (len >= xblock && xblock)
94 xblock = (xblock >> 5) - 1;
95 nlen = (len + xblock) & ~xblock;
100 return solv_realloc2(old, len, size);
112 solv_strdup(const char *s)
119 solv_oom(0, strlen(s));
124 solv_timems(unsigned int subtract)
129 if (gettimeofday(&tv, 0))
131 r = (((unsigned int)tv.tv_sec >> 16) * 1000) << 16;
132 r += ((unsigned int)tv.tv_sec & 0xffff) * 1000;
133 r += (unsigned int)tv.tv_usec / 1000;
137 /* bsd's qsort_r has different arguments, so we define our
138 own version in case we need to do some clever mapping
140 see also: http://sources.redhat.com/ml/libc-alpha/2008-12/msg00003.html
142 #if defined(__GLIBC__) && (defined(HAVE_QSORT_R) || defined(HAVE___QSORT_R))
145 solv_sort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *, void *), void *compard)
147 # if defined(HAVE_QSORT_R)
148 qsort_r(base, nmemb, size, compar, compard);
150 /* backported for SLE10-SP2 */
151 __qsort_r(base, nmemb, size, compar, compard);
156 #elif defined(HAVE_QSORT_R) /* not glibc, but has qsort_r() */
158 struct solv_sort_data {
159 int (*compar)(const void *, const void *, void *);
164 solv_sort_helper(void *compard, const void *a, const void *b)
166 struct solv_sort_data *d = compard;
167 return (*d->compar)(a, b, d->compard);
171 solv_sort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *, void *), void *compard)
173 struct solv_sort_data d;
176 qsort_r(base, nmemb, size, &d, solv_sort_helper);
179 #else /* not glibc and no qsort_r() */
180 /* use own version of qsort if none available */
185 solv_dupjoin(const char *str1, const char *str2, const char *str3)
189 l1 = str1 ? strlen(str1) : 0;
190 l2 = str2 ? strlen(str2) : 0;
191 l3 = str3 ? strlen(str3) : 0;
192 s = str = solv_malloc(l1 + l2 + l3 + 1);
213 solv_dupappend(const char *str1, const char *str2, const char *str3)
215 char *str = solv_dupjoin(str1, str2, str3);
216 solv_free((void *)str1);
221 solv_hex2bin(const char **strp, unsigned char *buf, int bufl)
223 const char *str = *strp;
226 for (i = 0; i < bufl; i++)
230 if (c >= '0' && c <= '9')
232 else if (c >= 'a' && c <= 'f')
234 else if (c >= 'A' && c <= 'F')
240 if (c >= '0' && c <= '9')
242 else if (c >= 'a' && c <= 'f')
244 else if (c >= 'A' && c <= 'F')
256 solv_bin2hex(const unsigned char *buf, int l, char *str)
259 for (i = 0; i < l; i++, buf++)
262 *str++ = c < 10 ? c + '0' : c + ('a' - 10);
264 *str++ = c < 10 ? c + '0' : c + ('a' - 10);
271 solv_validutf8(const char *buf)
273 const unsigned char *p;
276 for (p = (const unsigned char *)buf; (x = *p) != 0; p++)
284 /* one byte to follow */
285 if ((p[1] & 0xc0) != 0x80)
288 break; /* not minimal */
294 /* two bytes to follow */
295 if ((p[1] & 0xc0) != 0x80 || (p[2] & 0xc0) != 0x80)
297 if ((x & 0x0f) == 0 && (p[1] & 0x20) == 0)
298 break; /* not minimal */
299 if (x == 0xed && (p[1] & 0x20) != 0)
300 break; /* d800-dfff surrogate */
301 if (x == 0xef && p[1] == 0xbf && (p[2] == 0xbe || p[2] == 0xbf))
302 break; /* fffe or ffff */
308 /* three bytes to follow */
309 if ((p[1] & 0xc0) != 0x80 || (p[2] & 0xc0) != 0x80 || (p[3] & 0xc0) != 0x80)
311 if ((x & 0x07) == 0 && (p[1] & 0x30) == 0)
312 break; /* not minimal */
313 if ((x & 0x07) > 4 || ((x & 0x07) == 4 && (p[1] & 0x30) != 0))
314 break; /* above 0x10ffff */
318 break; /* maybe valid utf8, but above 0x10ffff */
320 return (const char *)p - buf;
324 solv_latin1toutf8(const char *buf)
330 for (p = buf; *p; p++)
331 if ((*(const unsigned char *)p & 128) != 0)
333 r = rp = solv_malloc(p - buf + l);
334 for (p = buf; *p; p++)
336 if ((*(const unsigned char *)p & 128) != 0)
338 *rp++ = *(const unsigned char *)p & 64 ? 0xc3 : 0xc2;
349 solv_replacebadutf8(const char *buf, int replchar)
353 char *r = 0, *rp = 0;
356 if (replchar < 0 || replchar > 0x10ffff)
359 repllen = replin = 0;
360 else if (replchar < 0x80)
363 replin = (replchar & 0x40) | 0x80;
365 else if (replchar < 0x800)
370 else if (replchar < 0x10000)
382 for (p = buf, nl = 0; *p; )
384 l = solv_validutf8(p);
394 /* found a bad char, replace with replchar */
400 *rp++ = (replchar >> 18 & 0x3f) | 0x80;
402 *rp++ = (replchar >> 12 & 0x3f) | 0x80;
404 *rp++ = (replchar >> 6 & 0x3f) | 0x80;
406 *rp++ = (replchar & 0x3f) | 0x80;
408 rp[-repllen] ^= replin;
412 while ((*(const unsigned char *)p & 0xc0) == 0x80)
417 r = rp = solv_malloc(nl + 1);