librazor/util.c
author J. Ali Harlow <ali@juiblex.co.uk>
Thu Jul 07 15:18:02 2016 +0100 (2016-07-07)
changeset 480 18c1225cfa1b
parent 475 008c75a5e08d
child 491 b18e0bf48a91
permissions -rw-r--r--
Release 0.6.3.100
     1 /*
     2  * Copyright (C) 2008  Kristian Høgsberg <krh@redhat.com>
     3  * Copyright (C) 2008  Red Hat, Inc
     4  * Copyright (C) 2009, 2011, 2012, 2014, 2016  J. Ali Harlow <ali@juiblex.co.uk>
     5  *
     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.
    10  *
    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.
    15  *
    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.
    19  */
    20 
    21 #include "config.h"
    22 
    23 #include <limits.h>
    24 #include <string.h>
    25 #include <stdlib.h>
    26 #include <stdio.h>
    27 #include <stdint.h>
    28 #include <errno.h>
    29 #include <unistd.h>
    30 #ifndef MSWIN_API
    31 #include <sys/utsname.h>
    32 #endif
    33 #include <assert.h>
    34 
    35 #include "razor.h"
    36 #include "types/types.h"
    37 #include "razor-internal.h"
    38 
    39 /* Required by gnulib on non-libc platforms */
    40 char *program_name = "librazor";
    41 
    42 void *
    43 zalloc(size_t size)
    44 {
    45 	void *p;
    46 
    47 	p = malloc(size);
    48 	memset(p, 0, size);
    49 
    50 	return p;
    51 }
    52 
    53 struct qsort_context {
    54 	size_t size;
    55 	razor_compare_with_data_func_t compare;
    56 	void *data;
    57 };
    58 
    59 static void
    60 qsort_swap(void *p1, void *p2, size_t size)
    61 {
    62 	char buffer[size];
    63 
    64 	if (p1 != p2) {
    65 		memcpy(buffer, p1, size);
    66 		memcpy(p1, p2, size);
    67 		memcpy(p2, buffer, size);
    68 	}
    69 }
    70 
    71 static void
    72 __qsort_with_data(void *base, size_t nelem, uint32_t *map,
    73 		  struct qsort_context *ctx)
    74 {
    75 	void *p, *start, *end, *pivot;
    76 	uint32_t *mp, *mstart, *mend, tmp;
    77 	int left, right, result;
    78 	size_t size = ctx->size;
    79 
    80 	p = base;
    81 	start = base;
    82 	end = base + nelem * size;
    83 	mp = map;
    84 	mstart = map;
    85 	mend = map + nelem;
    86 	pivot = base + (rand() % nelem) * size;
    87 
    88 	while (p < end) {
    89 		result = ctx->compare(p, pivot, ctx->data);
    90 		if (result < 0) {
    91 			qsort_swap(p, start, size);
    92 			tmp = *mp;
    93 			*mp = *mstart;
    94 			*mstart = tmp;
    95 			if (start == pivot)
    96 				pivot = p;
    97 			start += size;
    98 			mstart++;
    99 			p += size;
   100 			mp++;
   101 		} else if (result == 0) {
   102 			p += size;
   103 			mp++;
   104 		} else {
   105  			end -= size;
   106 			mend--;
   107 			qsort_swap(p, end, size);
   108 			tmp = *mp;
   109 			*mp = *mend;
   110 			*mend = tmp;
   111 			if (end == pivot)
   112 				pivot = p;
   113 		}
   114 	}
   115 
   116 	left = (start - base) / size;
   117 	right = (base + nelem * size - end) / size;
   118 	if (left > 1)
   119 		__qsort_with_data(base, left, map, ctx);
   120 	if (right > 1)
   121 		__qsort_with_data(end, right, mend, ctx);
   122 }
   123 
   124 uint32_t *
   125 razor_qsort_with_data(void *base, size_t nelem, size_t size,
   126 		      razor_compare_with_data_func_t compare, void *data)
   127 {
   128 	struct qsort_context ctx;
   129 	uint32_t *map;
   130 	int i;
   131 
   132 	if (nelem == 0)
   133 		return NULL;
   134 
   135 	ctx.size = size;
   136 	ctx.compare = compare;
   137 	ctx.data = data;
   138 
   139 	map = malloc(nelem * sizeof (uint32_t));
   140 	for (i = 0; i < nelem; i++)
   141 		map[i] = i;
   142 
   143 	__qsort_with_data(base, nelem, map, &ctx);
   144 
   145 	return map;
   146 }
   147 
   148 void environment_init(struct environment *env)
   149 {
   150 	env->is_set = 0;
   151 	array_init(&env->string_pool);
   152 	array_init(&env->vars);
   153 }
   154 
   155 void environment_add_variable(struct environment *env,
   156 			      const char *variable, const char *value)
   157 {
   158 	char *s;
   159 	uint32_t *r;
   160 	assert(!env->is_set);
   161 
   162 	s = array_add(&env->string_pool,
   163 		      strlen(variable) + strlen(value) + 2);
   164 	sprintf(s, "%s=%s", variable, value);
   165 	r = array_add(&env->vars, sizeof *r);
   166 	*r = s - (char *)env->string_pool.data;
   167 }
   168 
   169 void environment_set(struct environment *env)
   170 {
   171 	int i, count;
   172 	char *s;
   173 #ifndef WIN32
   174 	char *t;
   175 #endif
   176         uint32_t *r;
   177 
   178 	if (!env->is_set) {
   179 		count = env->vars.size / sizeof(uint32_t);
   180 		r = (uint32_t *)env->vars.data;
   181 		for (i = 0; i < count; i++) {
   182 			s = env->string_pool.data + *r++;
   183 #ifdef WIN32
   184 			putenv(s);
   185 #else
   186 			t = strchr(s, '=');
   187 			*t = '\0';
   188 			setenv(s, t + 1, 1);
   189 			*t = '=';
   190 #endif
   191 		}
   192 
   193 		env->is_set = 1;
   194 	}
   195 }
   196 
   197 void environment_unset(struct environment *env)
   198 {
   199 	int i, count;
   200 	char c, *s, *t;
   201         uint32_t *r;
   202 
   203 	if (env->is_set) {
   204 		count = env->vars.size / sizeof(uint32_t);
   205 		r = (uint32_t *)env->vars.data;
   206 		for (i = 0; i < count; i++) {
   207 			s = env->string_pool.data + *r++;
   208 			t = strchr(s, '=');
   209 #ifdef WIN32
   210 			t++;
   211 			c = *t;
   212 			*t = '\0';
   213 			putenv(s);
   214 			*t = c;
   215 #else
   216 			c = *t;
   217 			*t = '\0';
   218 			unsetenv(s);
   219 			*t = c;
   220 #endif
   221 		}
   222 
   223 		env->is_set = 0;
   224 	}
   225 }
   226 
   227 void environment_release(struct environment *env)
   228 {
   229 	environment_unset(env);
   230 	array_release(&env->string_pool);
   231 	array_release(&env->vars);
   232 }
   233 
   234 RAZOR_EXPORT char *razor_concat(const char *s, ...)
   235 {
   236 	va_list args;
   237 	const char *string;
   238 	char *concat;
   239 	size_t n, len;
   240 
   241 	va_start(args, s);
   242 
   243 	len = strlen(s);
   244 	while((string = va_arg(args, const char *)))
   245 		len += strlen(string);
   246 
   247 	va_end(args);
   248 
   249 	concat = malloc(len + 1);
   250 
   251 	if (!concat)
   252 		return NULL;
   253 
   254 	va_start(args, s);
   255 
   256 	len = strlen(s);
   257 	memcpy(concat, s, len);
   258 	n = len;
   259 	while((string = va_arg(args, const char *))) {
   260 		len = strlen(string);
   261 		memcpy(concat + n, string, len);
   262 		n += len;
   263 	}
   264 
   265 	va_end(args);
   266 
   267 	concat[n] = '\0';
   268 
   269 	return concat;
   270 }
   271 
   272 RAZOR_EXPORT char *
   273 razor_resolve_relative_uri(const char *base_uri, const char *relative_uri,
   274 			   struct razor_error **error)
   275 {
   276 	char *result;
   277 	struct razor_uri ru, base, ref;
   278 
   279 	if (razor_uri_parse_uri(&base, base_uri, 1, error))
   280 		return NULL;
   281 
   282 	if (razor_uri_parse_relative_ref(&ref, relative_uri, error)) {
   283 		razor_uri_destroy(&base);
   284 		return NULL;
   285 	}
   286 
   287 	razor_uri_resolve(&ru, &base, &ref);
   288 
   289 	razor_uri_destroy(&base);
   290 	razor_uri_destroy(&ref);
   291 
   292 	result = razor_uri_recompose(&ru);
   293 
   294 	razor_uri_destroy(&ru);
   295 
   296 	return result;
   297 }
   298 
   299 RAZOR_EXPORT const char *razor_system_arch(void)
   300 {
   301 #ifdef MSWIN_API
   302 	SYSTEM_INFO si;
   303 
   304 	GetNativeSystemInfo(&si);
   305 	switch(si.wProcessorArchitecture)
   306 	{
   307 		case PROCESSOR_ARCHITECTURE_INTEL:
   308 			return "i686";
   309 		case PROCESSOR_ARCHITECTURE_AMD64:
   310 			return "x86_64";
   311 		default:
   312 			return NULL;
   313 	}
   314 #else
   315 	static struct utsname un;
   316 
   317 	if (uname(&un))
   318 		return NULL;
   319 	else
   320 		return un.machine;
   321 #endif
   322 }
   323 
   324 #ifdef MSWIN_API
   325 
   326 char *razor_utf16_to_utf8(const wchar_t *utf16, int len)
   327 {
   328 	int n;
   329 	char *utf8;
   330 
   331 	if (len == 0)
   332 		return strdup("");
   333 
   334 	n = WideCharToMultiByte(CP_UTF8, 0, utf16, len, NULL, 0, NULL, NULL);
   335 	if (len > 0)
   336 		n++;
   337 	utf8 = malloc(n);
   338 	(void)WideCharToMultiByte(CP_UTF8, 0, utf16, len, utf8, n, NULL, NULL);
   339 	if (len > 0)
   340 		utf8[n - 1] = 0;
   341 
   342 	return utf8;
   343 }
   344 
   345 wchar_t *razor_utf8_to_utf16(const char *utf8, int len)
   346 {
   347 	int n;
   348 	wchar_t *utf16;
   349 
   350 	if (len == 0) {
   351 		utf16 = calloc(1, sizeof(wchar_t));
   352 		return utf16;
   353 	}
   354 
   355 	n = MultiByteToWideChar(CP_UTF8, 0, utf8, len, NULL, 0);
   356 	if (len > 0)
   357 		n++;
   358 	utf16 = malloc(n * sizeof(wchar_t));
   359 	(void)MultiByteToWideChar(CP_UTF8, 0, utf8, len, utf16, n);
   360 	if (len > 0)
   361 		utf16[n - 1] = 0;
   362 
   363 	return utf16;
   364 }
   365 
   366 #endif	/* MSWIN_API */