libfreerdp-utils: initial commit

This commit is contained in:
Marc-André Moreau 2011-06-30 18:24:37 -04:00
parent 49566c38ec
commit 57d5e7555c
5 changed files with 401 additions and 0 deletions

View File

@ -0,0 +1,47 @@
/**
* FreeRDP: A Remote Desktop Protocol Client
* DATABLOB Utils
*
* Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
*
* 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 <freerdp/utils/memory.h>
#include <freerdp/utils/datablob.h>
/**
* 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;
}

View File

@ -0,0 +1,56 @@
/**
* FreeRDP: A Remote Desktop Protocol Client
* Hex Dump Utils
*
* Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
*
* 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 <stdio.h>
#include <string.h>
#include <freerdp/types/base.h>
#include <freerdp/utils/hexdump.h>
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;
}
}

86
libfreerdp-utils/memory.c Normal file
View File

@ -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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <freerdp/utils/memory.h>
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;
}

View File

@ -0,0 +1,57 @@
/**
* FreeRDP: A Remote Desktop Protocol Client
* Semaphore Utils
*
* Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
*
* 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 <pthread.h>
#include <semaphore.h>
#include <freerdp/utils/semaphore.h>
#ifdef __APPLE__
#include <mach/mach.h>
#include <mach/semaphore.h>
#include <mach/task.h>
#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
}

155
libfreerdp-utils/unicode.c Normal file
View File

@ -0,0 +1,155 @@
/**
* FreeRDP: A Remote Desktop Protocol Client
* Unicode Utils
*
* Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
*
* 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 <errno.h>
#include <freerdp/utils/memory.h>
#include <freerdp/utils/unicode.h>
/* 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);
}
}