gdbutil.c
2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*---------------------------------------------------------------------*
* Copyright (C) 2004 BroadOn Communications Corp.
*
* $RCSfile: gdbutil.c,v $
* $Revision: 1.1 $
* $Date: 2004/02/06 02:12:22 $
*---------------------------------------------------------------------*/
#include "ultragdb.h"
/**************** GDB Utility Functions **************/
const char hexchars[]="0123456789abcdef";
/* Do we need this? Do we need our own copy of strlen, strncmp. strcmp? */
char *strcpy (char *s1, const char *s2)
{
char *dest=s1;
char c;
do {
c = *s2++;
*dest++ = c;
} while (c != 0);
return s1;
}
/* convert the memory pointed to by mem into hex, placing result in buf */
/* return a pointer to the last char put in buf (null) */
char* mem2hex(char *mem, char *buf, int count)
{
int i;
unsigned char ch;
if (mem == NULL) return NULL;
if (buf == NULL) return NULL;
for (i=0;i<count;i++) {
ch = *mem++;
*buf++ = hexchars[ch >> 4];
*buf++ = hexchars[ch % 16];
}
*buf = 0;
return(buf);
}
int hex(char ch)
{
if ((ch >= 'a') && (ch <= 'f')) return (ch-'a'+10);
if ((ch >= '0') && (ch <= '9')) return (ch-'0');
if ((ch >= 'A') && (ch <= 'F')) return (ch-'A'+10);
return (-1);
}
/**********************************************/
/* WHILE WE FIND NICE HEX CHARS, BUILD AN INT */
/* RETURN NUMBER OF CHARS PROCESSED */
/**********************************************/
int hexToInt(char **ptr, int *intValue)
{
int sign = 1;
int numChars = 0;
int hexValue;
if ((ptr == NULL) || (*ptr == NULL)) return 0;
if (intValue == NULL) return 0;
*intValue = 0;
if (**ptr == '-')
{
(*ptr)++;
sign = -1;
}
while (**ptr)
{
hexValue = hex(**ptr);
if (hexValue >=0)
{
*intValue = (*intValue <<4) | hexValue;
numChars ++;
}
else
break;
(*ptr)++;
}
(*intValue) *= sign;
return (numChars);
}
/* convert the hex array pointed to by buf into binary to be placed in mem */
/* return a pointer to the character AFTER the last byte written */
char* hex2mem(char *buf, char *mem, int count)
{
int i;
unsigned char ch;
if (buf == NULL) return NULL;
if (mem == NULL) return NULL;
for (i=0;i<count;i++) {
ch = hex(*buf++) << 4;
ch = ch + hex(*buf++);
*mem++ = ch;
}
return(mem);
}