/* * Copyright (C) 2008 Kristian Høgsberg * Copyright (C) 2008 Red Hat, Inc * Copyright (C) 2009, 2011, 2012, 2014, 2016 J. Ali Harlow * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include "config.h" #include #include #include #include #include #include #include #ifndef MSWIN_API #include #endif #include #include "razor.h" #include "types/types.h" #include "razor-internal.h" /* Required by gnulib on non-libc platforms */ char *program_name = "librazor"; void * zalloc(size_t size) { void *p; p = malloc(size); memset(p, 0, size); return p; } struct qsort_context { size_t size; razor_compare_with_data_func_t compare; void *data; }; static void qsort_swap(void *p1, void *p2, size_t size) { char buffer[size]; if (p1 != p2) { memcpy(buffer, p1, size); memcpy(p1, p2, size); memcpy(p2, buffer, size); } } static void __qsort_with_data(void *base, size_t nelem, uint32_t *map, struct qsort_context *ctx) { void *p, *start, *end, *pivot; uint32_t *mp, *mstart, *mend, tmp; int left, right, result; size_t size = ctx->size; p = base; start = base; end = base + nelem * size; mp = map; mstart = map; mend = map + nelem; pivot = base + (rand() % nelem) * size; while (p < end) { result = ctx->compare(p, pivot, ctx->data); if (result < 0) { qsort_swap(p, start, size); tmp = *mp; *mp = *mstart; *mstart = tmp; if (start == pivot) pivot = p; start += size; mstart++; p += size; mp++; } else if (result == 0) { p += size; mp++; } else { end -= size; mend--; qsort_swap(p, end, size); tmp = *mp; *mp = *mend; *mend = tmp; if (end == pivot) pivot = p; } } left = (start - base) / size; right = (base + nelem * size - end) / size; if (left > 1) __qsort_with_data(base, left, map, ctx); if (right > 1) __qsort_with_data(end, right, mend, ctx); } uint32_t * razor_qsort_with_data(void *base, size_t nelem, size_t size, razor_compare_with_data_func_t compare, void *data) { struct qsort_context ctx; uint32_t *map; int i; if (nelem == 0) return NULL; ctx.size = size; ctx.compare = compare; ctx.data = data; map = malloc(nelem * sizeof (uint32_t)); for (i = 0; i < nelem; i++) map[i] = i; __qsort_with_data(base, nelem, map, &ctx); return map; } void environment_init(struct environment *env) { env->is_set = 0; array_init(&env->string_pool); array_init(&env->vars); } void environment_add_variable(struct environment *env, const char *variable, const char *value) { char *s; uint32_t *r; assert(!env->is_set); s = array_add(&env->string_pool, strlen(variable) + strlen(value) + 2); sprintf(s, "%s=%s", variable, value); r = array_add(&env->vars, sizeof *r); *r = s - (char *)env->string_pool.data; } void environment_set(struct environment *env) { int i, count; char *s; #ifndef WIN32 char *t; #endif uint32_t *r; if (!env->is_set) { count = env->vars.size / sizeof(uint32_t); r = (uint32_t *)env->vars.data; for (i = 0; i < count; i++) { s = env->string_pool.data + *r++; #ifdef WIN32 putenv(s); #else t = strchr(s, '='); *t = '\0'; setenv(s, t + 1, 1); *t = '='; #endif } env->is_set = 1; } } void environment_unset(struct environment *env) { int i, count; char c, *s, *t; uint32_t *r; if (env->is_set) { count = env->vars.size / sizeof(uint32_t); r = (uint32_t *)env->vars.data; for (i = 0; i < count; i++) { s = env->string_pool.data + *r++; t = strchr(s, '='); #ifdef WIN32 t++; c = *t; *t = '\0'; putenv(s); *t = c; #else c = *t; *t = '\0'; unsetenv(s); *t = c; #endif } env->is_set = 0; } } void environment_release(struct environment *env) { environment_unset(env); array_release(&env->string_pool); array_release(&env->vars); } RAZOR_EXPORT char *razor_concat(const char *s, ...) { va_list args; const char *string; char *concat; size_t n, len; va_start(args, s); len = strlen(s); while((string = va_arg(args, const char *))) len += strlen(string); va_end(args); concat = malloc(len + 1); if (!concat) return NULL; va_start(args, s); len = strlen(s); memcpy(concat, s, len); n = len; while((string = va_arg(args, const char *))) { len = strlen(string); memcpy(concat + n, string, len); n += len; } va_end(args); concat[n] = '\0'; return concat; } RAZOR_EXPORT char * razor_resolve_relative_uri(const char *base_uri, const char *relative_uri, struct razor_error **error) { char *result; struct razor_uri ru, base, ref; if (razor_uri_parse_uri(&base, base_uri, 1, error)) return NULL; if (razor_uri_parse_relative_ref(&ref, relative_uri, error)) { razor_uri_destroy(&base); return NULL; } razor_uri_resolve(&ru, &base, &ref); razor_uri_destroy(&base); razor_uri_destroy(&ref); result = razor_uri_recompose(&ru); razor_uri_destroy(&ru); return result; } RAZOR_EXPORT const char *razor_system_arch(void) { #ifdef MSWIN_API SYSTEM_INFO si; GetNativeSystemInfo(&si); switch(si.wProcessorArchitecture) { case PROCESSOR_ARCHITECTURE_INTEL: return "i686"; case PROCESSOR_ARCHITECTURE_AMD64: return "x86_64"; default: return NULL; } #else static struct utsname un; if (uname(&un)) return NULL; else return un.machine; #endif } #ifdef MSWIN_API char *razor_utf16_to_utf8(const wchar_t *utf16, int len) { int n; char *utf8; if (len == 0) return strdup(""); n = WideCharToMultiByte(CP_UTF8, 0, utf16, len, NULL, 0, NULL, NULL); if (len > 0) n++; utf8 = malloc(n); (void)WideCharToMultiByte(CP_UTF8, 0, utf16, len, utf8, n, NULL, NULL); if (len > 0) utf8[n - 1] = 0; return utf8; } wchar_t *razor_utf8_to_utf16(const char *utf8, int len) { int n; wchar_t *utf16; if (len == 0) { utf16 = calloc(1, sizeof(wchar_t)); return utf16; } n = MultiByteToWideChar(CP_UTF8, 0, utf8, len, NULL, 0); if (len > 0) n++; utf16 = malloc(n * sizeof(wchar_t)); (void)MultiByteToWideChar(CP_UTF8, 0, utf8, len, utf16, n); if (len > 0) utf16[n - 1] = 0; return utf16; } #endif /* MSWIN_API */ /* * Returns an absolute path (ie., not relative to the current directory on the * default, or any other, disk). Note that the absolute path may still have * "." or ".." path segments present (ie., this is not equivalent to realpath). */ char *razor_abspath(const char *path) { #ifdef MSWIN_API int n; wchar_t *wpath, *wabspath; char *abspath; wpath = razor_utf8_to_utf16(path, -1); n = GetFullPathNameW(wpath, 0, NULL, NULL); if (!n) { free(wpath); return NULL; } wabspath = malloc(n * sizeof(sizeof(wchar_t))); if (!GetFullPathNameW(wpath, n, wabspath, NULL)) { free(wabspath); free(wpath); return NULL; } free(wpath); abspath = razor_utf16_to_utf8(wabspath, n - 1); free(wabspath); return abspath; #else /* MSWIN_API */ int pathlen, len; char *cwd, *abspath; if (*path == '/') abspath = strdup(path); else { pathlen = strlen(path); for (len = 32;; len *= 2) { abspath = malloc(pathlen + 1 + len); if (getcwd(abspath, len)) break; if (errno != ERANGE) { free(abspath); return NULL; } free(abspath); } len = strlen(abspath); if (abspath[len - 1] != '/') abspath[len++] = '/'; memcpy(abspath + len, path, pathlen + 1); abspath = realloc(abspath, len + pathlen + 1); } return abspath; #endif }