1 /* This file is part of the Project Athena Zephyr Notification System.
2  * It contains source for the ZReadAscii function.
3  *
4  *	Created by:	Robert French
5  *
6  *	Copyright (c) 1987, 1990 by the Massachusetts Institute of Technology.
7  *	For copying and distribution information, see the file
8  *	"mit-copyright.h".
9  */
10 
11 #include "internal.h"
12 
13 #define Z_cnvt_xtoi(c)  ((temp=(c)-'0'),(temp<10)?(int)temp:((temp-='A'-'9'-1),(temp<16)?(int)temp:-1))
14 
ZReadAscii(ptr,len,field,num)15 Code_t ZReadAscii(ptr, len, field, num)
16     char *ptr;
17     int len;
18     unsigned char *field;
19     int num;
20 {
21     int i;
22     unsigned int hexbyte;
23     register int c1, c2;
24     register unsigned int temp;
25 
26     for (i=0;i<num;i++) {
27 	if (*ptr == ' ') {
28 	    ptr++;
29 	    if (--len < 0)
30 		return ZERR_BADFIELD;
31 	}
32 	if (ptr[0] == '0' && ptr[1] == 'x') {
33 	    ptr += 2;
34 	    len -= 2;
35 	    if (len < 0)
36 		return ZERR_BADFIELD;
37 	}
38 	c1 = Z_cnvt_xtoi(ptr[0]);
39 	if (c1 < 0)
40 		return ZERR_BADFIELD;
41 	c2 = Z_cnvt_xtoi(ptr[1]);
42 	if (c2 < 0)
43 		return ZERR_BADFIELD;
44 	hexbyte = (c1 << 4) | c2;
45 	field[i] = hexbyte;
46 	ptr += 2;
47 	len -= 2;
48 	if (len < 0)
49 	    return ZERR_BADFIELD;
50     }
51 
52     return *ptr ? ZERR_BADFIELD : ZERR_NONE;
53 }
54 
ZReadAscii32(ptr,len,value_ptr)55 Code_t ZReadAscii32(ptr, len, value_ptr)
56     char *ptr;
57     int len;
58     unsigned long *value_ptr;
59 {
60     unsigned char buf[4];
61     unsigned long value = 0;
62     Code_t retval;
63 
64     retval = ZReadAscii(ptr, len, buf, 4);
65     if (retval != ZERR_NONE)
66 	return retval;
67     value |= (unsigned long)buf[0] << 24;
68     value |= buf[1] << 16;
69     value |= buf[2] << 8;
70     value |= buf[3];
71     *value_ptr = value;
72     return ZERR_NONE;
73 }
74 
ZReadAscii16(ptr,len,value_ptr)75 Code_t ZReadAscii16(ptr, len, value_ptr)
76     char *ptr;
77     int len;
78     unsigned short *value_ptr;
79 {
80     unsigned char buf[2];
81     Code_t retval;
82 
83     retval = ZReadAscii(ptr, len, buf, 2);
84     if (retval != ZERR_NONE)
85 	return retval;
86     *value_ptr = (buf[0] << 8) | buf[1];
87     return ZERR_NONE;
88 }
89 
90