1 | /* UNIX Unified Stream Protocol |
---|
2 | |
---|
3 | Copyright 1986 by the Massachusetts Institute of Technology |
---|
4 | See permission and disclaimer notice in file "notice.h" |
---|
5 | */ |
---|
6 | |
---|
7 | #include <sys/types.h> |
---|
8 | #include <stdio.h> |
---|
9 | #include "gen.h" |
---|
10 | #include "usp.h" |
---|
11 | #include <netinet/in.h> |
---|
12 | |
---|
13 | /* input operations */ |
---|
14 | |
---|
15 | USP_get_boolean(us, bo) |
---|
16 | |
---|
17 | USPStream *us; |
---|
18 | USPBoolean *bo; |
---|
19 | { |
---|
20 | unsigned actual; |
---|
21 | |
---|
22 | errno = 0; |
---|
23 | if(! us->us_in_receiving_p) { |
---|
24 | errno = UENOTRCVING; |
---|
25 | return(ERROR); |
---|
26 | } |
---|
27 | if(get_from_sub_block(us, (char *) bo, sizeof(USPBoolean), |
---|
28 | &actual) == ERROR) { |
---|
29 | return(ERROR); |
---|
30 | } |
---|
31 | if(actual != sizeof(USPBoolean)) { |
---|
32 | errno = UEPREMEOB; |
---|
33 | return(ERROR); |
---|
34 | } |
---|
35 | *bo = (USPBoolean) ntohs((u_short) *bo); |
---|
36 | switch(*bo) { |
---|
37 | case TRUE: |
---|
38 | case FALSE: |
---|
39 | return(SUCCESS); |
---|
40 | default: |
---|
41 | errno = UEBADATA; |
---|
42 | return(ERROR); |
---|
43 | } |
---|
44 | } |
---|
45 | |
---|
46 | USP_get_integer(us, ui) |
---|
47 | |
---|
48 | USPStream *us; |
---|
49 | USPInteger *ui; |
---|
50 | { |
---|
51 | unsigned actual; |
---|
52 | |
---|
53 | errno = 0; |
---|
54 | if(! us->us_in_receiving_p) { |
---|
55 | errno = UENOTRCVING; |
---|
56 | return(ERROR); |
---|
57 | } |
---|
58 | if(get_from_sub_block(us, (char *) ui, sizeof(USPInteger), |
---|
59 | &actual) == ERROR) { |
---|
60 | return(ERROR); |
---|
61 | } |
---|
62 | if(actual != sizeof(USPInteger)) { |
---|
63 | errno = UEPREMEOB; |
---|
64 | return(ERROR); |
---|
65 | } |
---|
66 | *ui = (USPInteger) ntohs((u_short) *ui); |
---|
67 | return(SUCCESS); |
---|
68 | } |
---|
69 | |
---|
70 | USP_get_cardinal(us, ca) |
---|
71 | |
---|
72 | USPStream *us; |
---|
73 | USPCardinal *ca; |
---|
74 | { |
---|
75 | unsigned actual; |
---|
76 | |
---|
77 | errno = 0; |
---|
78 | if(! us->us_in_receiving_p) { |
---|
79 | errno = UENOTRCVING; |
---|
80 | return(ERROR); |
---|
81 | } |
---|
82 | if(get_from_sub_block(us, (char *) ca, sizeof(USPCardinal), |
---|
83 | &actual) == ERROR) { |
---|
84 | return(ERROR); |
---|
85 | } |
---|
86 | if(actual != sizeof(USPCardinal)) { |
---|
87 | errno = UEPREMEOB; |
---|
88 | return(ERROR); |
---|
89 | } |
---|
90 | *ca = (USPCardinal) ntohs((u_short) *ca); |
---|
91 | return(SUCCESS); |
---|
92 | } |
---|
93 | |
---|
94 | USP_get_long_integer(us, li) |
---|
95 | |
---|
96 | USPStream *us; |
---|
97 | USPLong_integer *li; |
---|
98 | { |
---|
99 | unsigned actual; |
---|
100 | |
---|
101 | errno = 0; |
---|
102 | if(! us->us_in_receiving_p) { |
---|
103 | errno = UENOTRCVING; |
---|
104 | return(ERROR); |
---|
105 | } |
---|
106 | if(get_from_sub_block(us, (char *) li, sizeof(USPLong_integer), |
---|
107 | &actual) == ERROR) { |
---|
108 | return(ERROR); |
---|
109 | } |
---|
110 | if(actual != sizeof(USPLong_integer)) { |
---|
111 | errno = UEPREMEOB; |
---|
112 | return(ERROR); |
---|
113 | } |
---|
114 | *li = (USPLong_integer) ntohl((u_long) *li); |
---|
115 | return(SUCCESS); |
---|
116 | } |
---|
117 | |
---|
118 | USP_get_long_cardinal(us, lc) |
---|
119 | |
---|
120 | USPStream *us; |
---|
121 | USPLong_cardinal *lc; |
---|
122 | { |
---|
123 | unsigned actual; |
---|
124 | |
---|
125 | errno = 0; |
---|
126 | if(! us->us_in_receiving_p) { |
---|
127 | errno = UENOTRCVING; |
---|
128 | return(ERROR); |
---|
129 | } |
---|
130 | if(get_from_sub_block(us, (char *) lc, sizeof(USPLong_cardinal), |
---|
131 | &actual) == ERROR) { |
---|
132 | return(ERROR); |
---|
133 | } |
---|
134 | if(actual != sizeof(USPLong_cardinal)) { |
---|
135 | errno = UEPREMEOB; |
---|
136 | return(ERROR); |
---|
137 | } |
---|
138 | *lc = (USPLong_cardinal) ntohl((u_long) *lc); |
---|
139 | return(SUCCESS); |
---|
140 | } |
---|
141 | |
---|
142 | USP_get_string(us, str) |
---|
143 | |
---|
144 | USPStream *us; |
---|
145 | USPString *str; |
---|
146 | { |
---|
147 | USPCardinal sl; /* string length */ |
---|
148 | register char *sptr, c; |
---|
149 | register char *dptr; |
---|
150 | Boolean oddp = FALSE; |
---|
151 | unsigned actual; |
---|
152 | |
---|
153 | errno = 0; |
---|
154 | if(! us->us_in_receiving_p) { |
---|
155 | errno = UENOTRCVING; |
---|
156 | return(ERROR); |
---|
157 | } |
---|
158 | if(get_from_sub_block(us, (char *)&sl, sizeof(USPCardinal), |
---|
159 | &actual) == ERROR) { |
---|
160 | return(ERROR); |
---|
161 | } |
---|
162 | if(actual != sizeof(USPCardinal)) { |
---|
163 | errno = UEPREMEOB; |
---|
164 | return(ERROR); |
---|
165 | } |
---|
166 | sl = ntohs(sl); |
---|
167 | if(sl & 1) oddp = TRUE; |
---|
168 | if(! (*str = (USPString) malloc(sl + 1))) { |
---|
169 | errno = UENOMEM; |
---|
170 | return(ERROR); |
---|
171 | } |
---|
172 | dptr = sptr = *str; |
---|
173 | |
---|
174 | if (get_from_sub_block (us, sptr, sl + oddp, &actual) == ERROR) { |
---|
175 | free (*str); |
---|
176 | *str = NULL; |
---|
177 | return (ERROR); |
---|
178 | } |
---|
179 | if (actual != sl + oddp) { |
---|
180 | errno = UEPREMEOB; |
---|
181 | return ERROR; |
---|
182 | } |
---|
183 | /* de-netasciify */ |
---|
184 | actual -= oddp; |
---|
185 | |
---|
186 | while (actual > 0) { |
---|
187 | --actual; |
---|
188 | c = *sptr++; |
---|
189 | if ((c == '\r') && (actual > 0)) { |
---|
190 | --actual; |
---|
191 | c = *sptr++; |
---|
192 | if(c == '\012') { |
---|
193 | *dptr++ = '\n'; |
---|
194 | } else { |
---|
195 | *dptr++ = '\r'; |
---|
196 | } |
---|
197 | } else { |
---|
198 | *dptr++ = c; |
---|
199 | } |
---|
200 | } |
---|
201 | *dptr = '\0'; |
---|
202 | |
---|
203 | /* if length is odd, discard the last byte */ |
---|
204 | return(SUCCESS); |
---|
205 | } |
---|
206 | |
---|
207 | /* this reads a raw block of bytes from a USP block. For those of us who |
---|
208 | have no need of USP's data types... */ |
---|
209 | |
---|
210 | USP_get_byte_block(us, buf, len, actual) |
---|
211 | |
---|
212 | USPStream *us; |
---|
213 | Byte *buf; |
---|
214 | unsigned len; |
---|
215 | unsigned *actual; |
---|
216 | { |
---|
217 | return(get_from_sub_block(us, buf, len, actual)); |
---|
218 | } |
---|