From 57d5e7555c384eda16841deccfefc6696fb2b742 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Moreau?= Date: Thu, 30 Jun 2011 18:24:37 -0400 Subject: [PATCH] libfreerdp-utils: initial commit --- libfreerdp-utils/datablob.c | 47 +++++++++++ libfreerdp-utils/hexdump.c | 56 +++++++++++++ libfreerdp-utils/memory.c | 86 +++++++++++++++++++ libfreerdp-utils/semaphore.c | 57 +++++++++++++ libfreerdp-utils/unicode.c | 155 +++++++++++++++++++++++++++++++++++ 5 files changed, 401 insertions(+) create mode 100644 libfreerdp-utils/datablob.c create mode 100644 libfreerdp-utils/hexdump.c create mode 100644 libfreerdp-utils/memory.c create mode 100644 libfreerdp-utils/semaphore.c create mode 100644 libfreerdp-utils/unicode.c diff --git a/libfreerdp-utils/datablob.c b/libfreerdp-utils/datablob.c new file mode 100644 index 000000000..60651326f --- /dev/null +++ b/libfreerdp-utils/datablob.c @@ -0,0 +1,47 @@ +/** + * FreeRDP: A Remote Desktop Protocol Client + * DATABLOB Utils + * + * Copyright 2011 Marc-Andre Moreau + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include + +/** + * Allocate memory for data blob. + * @param datablob datablob structure + * @param length memory length + */ + +void datablob_alloc(DATABLOB *datablob, int length) +{ + datablob->data = xmalloc(length); + datablob->length = length; +} + +/** + * Free memory allocated for data blob. + * @param datablob + */ + +void datablob_free(DATABLOB *datablob) +{ + if (datablob->data) + xfree(datablob->data); + + datablob->length = 0; +} diff --git a/libfreerdp-utils/hexdump.c b/libfreerdp-utils/hexdump.c new file mode 100644 index 000000000..158879895 --- /dev/null +++ b/libfreerdp-utils/hexdump.c @@ -0,0 +1,56 @@ +/** + * FreeRDP: A Remote Desktop Protocol Client + * Hex Dump Utils + * + * Copyright 2011 Marc-Andre Moreau + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include + +#include + +void freerdp_hexdump(uint8* data, int length) +{ + uint8* p = data; + int i, line, offset = 0; + + while (offset < length) + { + printf("%04x ", offset); + + line = length - offset; + + if (line > FREERDP_HEXDUMP_LINE_LENGTH) + line = FREERDP_HEXDUMP_LINE_LENGTH; + + for (i = 0; i < line; i++) + printf("%02x ", p[i]); + + for (; i < FREERDP_HEXDUMP_LINE_LENGTH; i++) + printf(" "); + + for (i = 0; i < line; i++) + printf("%c", (p[i] >= 0x20 && p[i] < 0x7F) ? p[i] : '.'); + + printf("\n"); + + offset += line; + p += line; + } +} + diff --git a/libfreerdp-utils/memory.c b/libfreerdp-utils/memory.c new file mode 100644 index 000000000..c8fd2bda0 --- /dev/null +++ b/libfreerdp-utils/memory.c @@ -0,0 +1,86 @@ +/** + * FreeRDP: A Remote Desktop Protocol Client + * Memory Utils + * + * Copyright 2009-2011 Jay Sorg + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include + +#include + +void * +xmalloc(size_t size) +{ + void * mem; + + if (size < 1) + { + size = 1; + } + mem = malloc(size); + if (mem == NULL) + { + perror("xmalloc"); + } + return mem; +} + +void * +xrealloc(void * oldmem, size_t size) +{ + void * mem; + + if (size < 1) + { + size = 1; + } + mem = realloc(oldmem, size); + if (mem == NULL) + { + perror("xrealloc"); + } + return mem; +} + +void +xfree(void * mem) +{ + if (mem != NULL) + { + free(mem); + } +} + +char * +xstrdup(const char * s) +{ + char * mem; + +#ifdef _WIN32 + mem = _strdup(s); +#else + mem = strdup(s); +#endif + + if (mem == NULL) + { + perror("strdup"); + } + + return mem; +} diff --git a/libfreerdp-utils/semaphore.c b/libfreerdp-utils/semaphore.c new file mode 100644 index 000000000..6effcc1c4 --- /dev/null +++ b/libfreerdp-utils/semaphore.c @@ -0,0 +1,57 @@ +/** + * FreeRDP: A Remote Desktop Protocol Client + * Semaphore Utils + * + * Copyright 2011 Marc-Andre Moreau + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include + +#ifdef __APPLE__ +#include +#include +#include +#endif + +void freerdp_sem_create(void * sem_struct, int iv) +{ +#ifdef __APPLE__ + semaphore_create(mach_task_self(), (semaphore_t *)sem_struct, SYNC_POLICY_FIFO, iv); +#else + int pshared = 0; + sem_init((sem_t *)sem_struct, pshared, iv); +#endif +} + +void freerdp_sem_signal(void * sem_struct) +{ +#ifdef __APPLE__ + semaphore_signal(*((semaphore_t *)sem_struct)); +#else + sem_post((sem_t *)sem_struct); +#endif +} + +void freerdp_sem_wait(void * sem_struct) +{ +#ifdef __APPLE__ + semaphore_wait(*((semaphore_t *)sem_struct)); +#else + sem_wait((sem_t *)sem_struct); +#endif +} + diff --git a/libfreerdp-utils/unicode.c b/libfreerdp-utils/unicode.c new file mode 100644 index 000000000..5be3ed599 --- /dev/null +++ b/libfreerdp-utils/unicode.c @@ -0,0 +1,155 @@ +/** + * FreeRDP: A Remote Desktop Protocol Client + * Unicode Utils + * + * Copyright 2011 Marc-Andre Moreau + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "config.h" +#include +#include + +#include + +/* Convert pin/in_len from WINDOWS_CODEPAGE - return like xstrdup, 0-terminated */ + +char* freerdp_uniconv_in(UNICONV *uniconv, unsigned char* pin, size_t in_len) +{ + unsigned char *conv_pin = pin; + size_t conv_in_len = in_len; + char *pout = xmalloc(in_len + 1), *conv_pout = pout; + size_t conv_out_len = in_len; + +#ifdef HAVE_ICONV + if (iconv(uniconv->in_iconv_h, (ICONV_CONST char **) &conv_pin, &conv_in_len, &conv_pout, &conv_out_len) == (size_t) - 1) + { + /* TODO: xrealloc if conv_out_len == 0 - it shouldn't be needed, but would allow a smaller initial alloc ... */ + printf("freerdp_uniconv_in: iconv failure\n"); + return 0; + } +#else + while (conv_in_len >= 2) + { + if ((signed char)(*conv_pin) < 0) + { + printf("freerdp_uniconv_in: wrong input conversion of char %d\n", *conv_pin); + } + *conv_pout++ = *conv_pin++; + if ((*conv_pin) != 0) + { + printf("freerdp_uniconv_in: wrong input conversion skipping non-zero char %d\n", *conv_pin); + } + conv_pin++; + conv_in_len -= 2; + conv_out_len--; + } +#endif + + if (conv_in_len > 0) + { + printf("freerdp_uniconv_in: conversion failure - %d chars left\n", (int) conv_in_len); + } + + *conv_pout = 0; + return pout; +} + +/* Convert str from DEFAULT_CODEPAGE to WINDOWS_CODEPAGE and return buffer like xstrdup. + * Buffer is 0-terminated but that is not included in the returned length. */ + +char* freerdp_uniconv_out(UNICONV *uniconv, char *str, size_t *pout_len) +{ + size_t ibl = strlen(str), obl = 2 * ibl; /* FIXME: worst case */ + char *pin = str, *pout0 = xmalloc(obl + 2), *pout = pout0; + +#ifdef HAVE_ICONV + if (iconv(uniconv->out_iconv_h, (ICONV_CONST char **) &pin, &ibl, &pout, &obl) == (size_t) - 1) + { + printf("freerdp_uniconv_out: iconv() error\n"); + return NULL; + } +#else + while ((ibl > 0) && (obl > 0)) + { + if ((signed char)(*pin) < 0) + { + return NULL; + } + *pout++ = *pin++; + *pout++ = 0; + ibl--; + obl -= 2; + } +#endif + + if (ibl > 0) + { + printf("freerdp_uniconv_out: string not fully converted - %d chars left\n", (int) ibl); + } + + *pout_len = pout - pout0; + *pout++ = 0; /* Add extra double zero termination */ + *pout = 0; + return pout0; +} + +/* Uppercase a unicode string */ + +void freerdp_uniconv_uppercase(UNICONV *uniconv, char *wstr, int length) +{ + int i; + char* p; + + p = wstr; + for (i = 0; i < length; i++) + { + if (p[i * 2] >= 'a' && p[i * 2] <= 'z') + p[i * 2] = p[i * 2] - 32; + } +} + +UNICONV* freerdp_uniconv_new() +{ + UNICONV *uniconv = xmalloc(sizeof(UNICONV)); + memset(uniconv, '\0', sizeof(UNICONV)); + +#ifdef HAVE_ICONV + uniconv->iconv = 1; + uniconv->in_iconv_h = iconv_open(DEFAULT_CODEPAGE, WINDOWS_CODEPAGE); + if (errno == EINVAL) + { + printf("Error opening iconv converter to %s from %s\n", DEFAULT_CODEPAGE, WINDOWS_CODEPAGE); + } + uniconv->out_iconv_h = iconv_open(WINDOWS_CODEPAGE, DEFAULT_CODEPAGE); + if (errno == EINVAL) + { + printf("Error opening iconv converter to %s from %s\n", WINDOWS_CODEPAGE, DEFAULT_CODEPAGE); + } +#endif + + return uniconv; +} + +void freerdp_uniconv_free(UNICONV *uniconv) +{ + if (uniconv != NULL) + { +#ifdef HAVE_ICONV + iconv_close(uniconv->in_iconv_h); + iconv_close(uniconv->out_iconv_h); +#endif + xfree(uniconv); + } +}