2 * Copyright (C) 2008 Kristian Høgsberg <krh@redhat.com>
3 * Copyright (C) 2008 Red Hat, Inc
4 * Copyright (C) 2009, 2011 J. Ali Harlow <ali@juiblex.co.uk>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include <sys/types.h>
37 #include <sys/utsname.h>
45 #include "razor-internal.h"
51 /* Required by gnulib on non-libc platforms */
52 char *program_name = "librazor";
55 razor_file_get_contents(const char *filename, size_t *length)
65 fd = open(filename, O_RDONLY | O_BINARY);
69 if (fstat(fd, &st) < 0) {
76 addr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
78 addr = malloc(st.st_size);
81 while(nb < st.st_size) {
82 res = read(fd, addr + nb, st.st_size - nb);
95 if (addr == MAP_FAILED)
102 razor_file_free_contents(void *addr, size_t length)
105 return munmap(addr, length);
112 struct qsort_context {
114 razor_compare_with_data_func_t compare;
119 qsort_swap(void *p1, void *p2, size_t size)
124 memcpy(buffer, p1, size);
125 memcpy(p1, p2, size);
126 memcpy(p2, buffer, size);
131 __qsort_with_data(void *base, size_t nelem, uint32_t *map,
132 struct qsort_context *ctx)
134 void *p, *start, *end, *pivot;
135 uint32_t *mp, *mstart, *mend, tmp;
136 int left, right, result;
137 size_t size = ctx->size;
141 end = base + nelem * size;
145 pivot = base + (rand() % nelem) * size;
148 result = ctx->compare(p, pivot, ctx->data);
150 qsort_swap(p, start, size);
160 } else if (result == 0) {
166 qsort_swap(p, end, size);
175 left = (start - base) / size;
176 right = (base + nelem * size - end) / size;
178 __qsort_with_data(base, left, map, ctx);
180 __qsort_with_data(end, right, mend, ctx);
184 razor_qsort_with_data(void *base, size_t nelem, size_t size,
185 razor_compare_with_data_func_t compare, void *data)
187 struct qsort_context ctx;
195 ctx.compare = compare;
198 map = malloc(nelem * sizeof (uint32_t));
199 for (i = 0; i < nelem; i++)
202 __qsort_with_data(base, nelem, map, &ctx);
207 void environment_init(struct environment *env)
210 array_init(&env->string_pool);
211 array_init(&env->vars);
214 void environment_add_variable(struct environment *env,
215 const char *variable, const char *value)
219 assert(!env->is_set);
221 s = array_add(&env->string_pool,
222 strlen(variable) + strlen(value) + 2);
223 sprintf(s, "%s=%s", variable, value);
224 r = array_add(&env->vars, sizeof *r);
225 *r = s - (char *)env->string_pool.data;
228 void environment_set(struct environment *env)
235 count = env->vars.size / sizeof(uint32_t);
236 r = (uint32_t *)env->vars.data;
237 for (i = 0; i < count; i++) {
238 s = env->string_pool.data + *r++;
253 void environment_unset(struct environment *env)
260 count = env->vars.size / sizeof(uint32_t);
261 r = (uint32_t *)env->vars.data;
262 for (i = 0; i < count; i++) {
263 s = env->string_pool.data + *r++;
283 void environment_release(struct environment *env)
285 environment_unset(env);
286 array_release(&env->string_pool);
287 array_release(&env->vars);
290 RAZOR_EXPORT char *razor_concat(const char *s, ...)
300 while((string = va_arg(args, const char *)))
301 len += strlen(string);
305 concat = malloc(len + 1);
313 memcpy(concat, s, len);
315 while((string = va_arg(args, const char *))) {
316 len = strlen(string);
317 memcpy(concat + n, string, len);
328 RAZOR_EXPORT const char *razor_system_arch(void)
333 GetNativeSystemInfo(&si);
334 switch(si.wProcessorArchitecture)
336 case PROCESSOR_ARCHITECTURE_INTEL:
338 case PROCESSOR_ARCHITECTURE_AMD64:
344 static struct utsname un;
355 char *razor_utf16_to_utf8(const wchar_t *utf16, int len)
360 n = WideCharToMultiByte(CP_UTF8, 0, utf16, len, NULL, 0, NULL, NULL);
361 if (len >= 0 && utf16[len])
364 (void)WideCharToMultiByte(CP_UTF8, 0, utf16, len, utf8, n, NULL, NULL);
365 if (len >= 0 && utf16[len])
371 wchar_t *razor_utf8_to_utf16(const char *utf8, int len)
376 n = MultiByteToWideChar(CP_UTF8, 0, utf8, len, NULL, 0);
377 if (len >= 0 && utf8[len])
379 utf16 = malloc(n * sizeof(wchar_t));
380 (void)MultiByteToWideChar(CP_UTF8, 0, utf8, len, utf16, n);
381 if (len >= 0 && utf8[len])
387 #endif /* MSWIN_API */