comparison packages/cygmon/current/misc/bsp/common/hex-utils.c @ 76:435cced73e2f ecos-v1_3_1-release

eCos v1.3.1 merged from eCos master repository on 2000-03-27-23:22:51-BST
author jlarmour
date Tue, 28 Mar 2000 14:10:45 +0000
parents
children e0c0827131d1
comparison
equal deleted inserted replaced
75:41bf073c0c32 76:435cced73e2f
1 //==========================================================================
2 //
3 // hex-utils.c
4 //
5 // Utilities for dealing with hexadecimal strings.
6 //
7 //==========================================================================
8 //####COPYRIGHTBEGIN####
9 //
10 // -------------------------------------------
11 // The contents of this file are subject to the Red Hat eCos Public License
12 // Version 1.1 (the "License"); you may not use this file except in
13 // compliance with the License. You may obtain a copy of the License at
14 // http://www.redhat.com/
15 //
16 // Software distributed under the License is distributed on an "AS IS"
17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
18 // License for the specific language governing rights and limitations under
19 // the License.
20 //
21 // The Original Code is eCos - Embedded Configurable Operating System,
22 // released September 30, 1998.
23 //
24 // The Initial Developer of the Original Code is Red Hat.
25 // Portions created by Red Hat are
26 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
27 // All Rights Reserved.
28 // -------------------------------------------
29 //
30 //####COPYRIGHTEND####
31 //==========================================================================
32 //#####DESCRIPTIONBEGIN####
33 //
34 // Author(s):
35 // Contributors: gthomas
36 // Date: 1999-10-20
37 // Purpose:
38 // Description:
39 //
40 //
41 //####DESCRIPTIONEND####
42 //
43 //=========================================================================
44
45
46 #include <bsp/hex-utils.h>
47
48 int
49 __hex(char ch)
50 {
51 if ((ch >= 'a') && (ch <= 'f')) return (ch-'a'+10);
52 if ((ch >= '0') && (ch <= '9')) return (ch-'0');
53 if ((ch >= 'A') && (ch <= 'F')) return (ch-'A'+10);
54 return (-1);
55 }
56
57
58 /*
59 * Convert the hex data in 'buf' into 'count' bytes to be placed in 'mem'.
60 * Returns a pointer to the character in mem AFTER the last byte written.
61 */
62 char *
63 __unpack_bytes_to_mem(char *buf, char *mem, int count)
64 {
65 int i;
66 char ch;
67
68 for (i = 0; i < count; i++) {
69 ch = __hex(*buf++) << 4;
70 ch = ch + __hex(*buf++);
71 *mem++ = ch;
72 }
73 return(mem);
74 }
75
76 /*
77 * While finding valid hex chars, build an unsigned long int.
78 * Return number of hex chars processed.
79 */
80 int
81 __unpack_ulong(char **ptr, unsigned long *val)
82 {
83 int numChars = 0;
84 int hexValue;
85
86 *val = 0;
87
88 while (**ptr) {
89 hexValue = __hex(**ptr);
90 if (hexValue >= 0) {
91 *val = (*val << 4) | hexValue;
92 numChars ++;
93 } else
94 break;
95 (*ptr)++;
96 }
97 return (numChars);
98 }
99
100
101 /*
102 * Unpack 'count' hex characters, forming them into a binary value.
103 * Return that value as an int. Adjust the source pointer accordingly.
104 */
105 int
106 __unpack_nibbles(char **ptr, int count)
107 {
108 int value = 0;
109
110 while (--count >= 0) {
111 value = (value << 4) | __hex(**ptr);
112 (*ptr)++;
113 }
114 return value;
115 }
116
117