/*
* memory.c - Memory allocation and private string handling
*
* Copyright (C) 1998-2003 Gero Kuhlmann <gero@gkminix.han.de>
*
* 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
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* $Id: memory.c,v 1.6 2003/01/25 23:29:44 gkminix Exp $
*/
#include <common.h>
#include <nblib.h>
#include "privlib.h"
/*
* Allocate memory - with error handling
*/
voidstar nbmalloc(amount)
size_t amount;
{
voidstar ptr;
assert(amount > 0);
if ((ptr = malloc(amount)) == NULL) {
if (!quiet) perror(progname);
exit(EXIT_MEMORY);
}
memset(ptr, 0, amount);
return(ptr);
}
/*
* Copy a string into a newly allocated memory block
*/
void copystr(dest, src)
char **dest;
char *src;
{
if (*dest != NULL)
free(*dest);
if (src == NULL) {
*dest = NULL;
return;
}
*dest = (char *)nbmalloc(strlen(src) + 1);
strcpy(*dest, src);
}
/*
* Table to convert latin1 characters into PC characters
*/
#define LATIN1_MAX 255
static __u8 const latin1[LATIN1_MAX + 1] = {
0, 1, 2, 3, 4, 5, 6, 7, /* 0 - 7 */
8, 9, 10, 11, 12, 13, 14, 15, /* 8 - 15 */
16, 17, 18, 19, 182, 186, 22, 23, /* 16 - 23 */
24, 25, 26, 27, 28, 29, 30, 31, /* 24 - 31 */
32, 33, 34, 35, 36, 37, 38, 39, /* 32 - 39 */
40, 41, 42, 43, 44, 45, 46, 47, /* 40 - 47 */
48, 49, 50, 51, 52, 53, 54, 55, /* 48 - 55 */
56, 57, 58, 59, 60, 61, 62, 63, /* 56 - 63 */
64, 65, 66, 67, 68, 69, 70, 71, /* 64 - 71 */
72, 73, 74, 75, 76, 77, 78, 79, /* 72 - 79 */
80, 81, 82, 83, 84, 85, 86, 87, /* 80 - 87 */
88, 89, 90, 91, 92, 93, 94, 95, /* 88 - 95 */
96, 97, 98, 99, 100, 101, 102, 103, /* 96 - 103 */
104, 105, 106, 107, 108, 109, 110, 111, /* 104 - 111 */
112, 113, 114, 115, 116, 117, 118, 119, /* 112 - 119 */
120, 121, 122, 123, 124, 125, 126, 127, /* 120 - 127 */
199, 252, 233, 226, 228, 224, 229, 231, /* 128 - 135 */
234, 235, 232, 239, 238, 236, 196, 197, /* 136 - 143 */
201, 181, 198, 244, 247, 242, 251, 249, /* 144 - 151 */
223, 214, 220, 243, 183, 209, 158, 159, /* 152 - 159 */
255, 173, 155, 156, 177, 157, 188, 21, /* 160 - 167 */
191, 169, 166, 174, 170, 237, 189, 187, /* 168 - 175 */
248, 241, 253, 179, 180, 230, 20, 250, /* 176 - 183 */
184, 185, 167, 175, 172, 171, 190, 168, /* 184 - 191 */
192, 193, 194, 195, 142, 143, 146, 128, /* 192 - 199 */
200, 144, 202, 203, 204, 205, 206, 207, /* 200 - 207 */
208, 165, 210, 211, 212, 213, 153, 215, /* 208 - 215 */
216, 217, 218, 219, 154, 221, 222, 225, /* 216 - 223 */
133, 160, 131, 227, 132, 134, 145, 135, /* 224 - 231 */
138, 130, 136, 137, 141, 161, 140, 139, /* 232 - 239 */
240, 164, 149, 162, 147, 245, 148, 246, /* 240 - 247 */
176, 151, 163, 150, 129, 178, 254, 152, /* 248 - 255 */
};
/*
* Convert a character from latin1 encoding into PC encoding
*/
__u8 totarget(c)
int c;
{
if (c > LATIN1_MAX)
return(0);
return(latin1[c] & 0xff);
}
/*
* Compare a string in a binary buffer with a string. We can't use
* strcmp() here, because characters might be represented with a different
* size on the host system than on the x86 target system. It returns
* TRUE, if the byte buffer contents and the string are identical.
*/
int bytecmp(str, buf, len)
char *str;
__u8 *buf;
size_t len;
{
while (*str && len > 0) {
if (totarget(*(str++)) != *(buf++))
break;
len--;
}
return(len == 0);
}
/*
* Copy a string into a byte array. We can't use strcpy() here because
* characters might be represented differently on the system than on the
* target x86 system. This does NOT copy the zero at the end of the
* source string.
*/
void bytecpy(str, buf, len)
char *str;
__u8 *buf;
size_t len;
{
while (*str && len > 0) {
*(buf++) = totarget(*(str++));
len--;
}
}
syntax highlighted by Code2HTML, v. 0.9.1