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 | /* output operations */ |
---|
14 | |
---|
15 | USP_put_boolean(us, bo) |
---|
16 | |
---|
17 | USPStream *us; |
---|
18 | USPBoolean bo; |
---|
19 | { |
---|
20 | errno = 0; |
---|
21 | if(! us->us_out_sending_p) { |
---|
22 | errno = UENOTSENDING; |
---|
23 | return(ERROR); |
---|
24 | } |
---|
25 | switch (bo) { |
---|
26 | case TRUE: |
---|
27 | case FALSE: |
---|
28 | break; |
---|
29 | default: |
---|
30 | errno = UEBADATA; |
---|
31 | return(ERROR); |
---|
32 | } |
---|
33 | bo = (USPBoolean) htons(bo); |
---|
34 | if(put_into_sub_block(us, (char *) &bo, sizeof(USPBoolean)) == ERROR) { |
---|
35 | return(ERROR); |
---|
36 | } |
---|
37 | return(SUCCESS); |
---|
38 | } |
---|
39 | |
---|
40 | USP_put_integer(us, ui) |
---|
41 | |
---|
42 | USPStream *us; |
---|
43 | USPInteger ui; |
---|
44 | { |
---|
45 | errno = 0; |
---|
46 | if(! us->us_out_sending_p) { |
---|
47 | errno = UENOTSENDING; |
---|
48 | return(ERROR); |
---|
49 | } |
---|
50 | ui = (USPInteger) htons((u_short) ui); |
---|
51 | if(put_into_sub_block(us, (char *) &ui, sizeof(USPInteger)) == ERROR) { |
---|
52 | return(ERROR); |
---|
53 | } |
---|
54 | return(SUCCESS); |
---|
55 | } |
---|
56 | |
---|
57 | USP_put_cardinal(us, ca) |
---|
58 | |
---|
59 | USPStream *us; |
---|
60 | USPCardinal ca; |
---|
61 | { |
---|
62 | errno = 0; |
---|
63 | if(! us->us_out_sending_p) { |
---|
64 | errno = UENOTSENDING; |
---|
65 | return(ERROR); |
---|
66 | } |
---|
67 | ca = (USPCardinal) htons((u_short) ca); |
---|
68 | if(put_into_sub_block(us, (char *) &ca, sizeof(USPCardinal)) == ERROR) { |
---|
69 | return(ERROR); |
---|
70 | } |
---|
71 | return(SUCCESS); |
---|
72 | } |
---|
73 | |
---|
74 | USP_put_long_integer(us, li) |
---|
75 | |
---|
76 | USPStream *us; |
---|
77 | USPLong_integer li; |
---|
78 | { |
---|
79 | errno = 0; |
---|
80 | if(! us->us_out_sending_p) { |
---|
81 | errno = UENOTSENDING; |
---|
82 | return(ERROR); |
---|
83 | } |
---|
84 | li = (USPLong_integer) htonl((u_long) li); |
---|
85 | if(put_into_sub_block(us, (char *) &li, sizeof(USPLong_integer)) == ERROR){ |
---|
86 | return(ERROR); |
---|
87 | } |
---|
88 | return(SUCCESS); |
---|
89 | } |
---|
90 | |
---|
91 | USP_put_long_cardinal(us, lc) |
---|
92 | |
---|
93 | USPStream *us; |
---|
94 | USPLong_cardinal lc; |
---|
95 | { |
---|
96 | errno = 0; |
---|
97 | if(! us->us_out_sending_p) { |
---|
98 | errno = UENOTSENDING; |
---|
99 | return(ERROR); |
---|
100 | } |
---|
101 | lc = (USPLong_cardinal) htonl((u_long) lc); |
---|
102 | if(put_into_sub_block(us, (char *)&lc, sizeof(USPLong_cardinal)) == ERROR){ |
---|
103 | return(ERROR); |
---|
104 | } |
---|
105 | return(SUCCESS); |
---|
106 | } |
---|
107 | |
---|
108 | USP_put_string(us, str) |
---|
109 | |
---|
110 | USPStream *us; |
---|
111 | USPString str; |
---|
112 | { |
---|
113 | USPCardinal sl = 0, ssl; |
---|
114 | register char *sptr = str, *stptr; |
---|
115 | register int c; |
---|
116 | char zero = '\0'; |
---|
117 | static char crlf[] = "\r\n"; |
---|
118 | static char crnul[] = "\r\0"; |
---|
119 | |
---|
120 | errno = 0; |
---|
121 | if(! us->us_out_sending_p) { |
---|
122 | errno = UENOTSENDING; |
---|
123 | return(ERROR); |
---|
124 | } |
---|
125 | |
---|
126 | /* Process string one byte at a time in order to netasciify */ |
---|
127 | |
---|
128 | /* first find netasciified length by adding 1 extra for every CR or NL */ |
---|
129 | |
---|
130 | while(*sptr) { |
---|
131 | ++sl; |
---|
132 | if(*sptr == '\r' || *sptr == '\n') { |
---|
133 | ++sl; |
---|
134 | } |
---|
135 | ++sptr; |
---|
136 | } |
---|
137 | ssl = (USPCardinal) htons((u_short) sl); |
---|
138 | if(put_into_sub_block(us, (char *) &ssl, sizeof(USPCardinal)) == ERROR) { |
---|
139 | return(ERROR); |
---|
140 | } |
---|
141 | sptr = str; |
---|
142 | stptr = sptr; |
---|
143 | while((c = *sptr) != '\0') { |
---|
144 | if (c == '\n') { |
---|
145 | if (sptr > stptr) { |
---|
146 | if (put_into_sub_block (us, stptr, sptr-stptr) == ERROR) { |
---|
147 | return (ERROR); |
---|
148 | } |
---|
149 | } |
---|
150 | if(put_into_sub_block(us, crlf, 2) == ERROR) { |
---|
151 | return(ERROR); |
---|
152 | } |
---|
153 | stptr = sptr+1; |
---|
154 | } else if (c == '\r') { |
---|
155 | if (sptr > stptr) { |
---|
156 | if (put_into_sub_block (us, stptr, sptr-stptr) == ERROR) { |
---|
157 | return (ERROR); |
---|
158 | } |
---|
159 | } |
---|
160 | if(put_into_sub_block(us, crnul, 2) == ERROR) { |
---|
161 | return(ERROR); |
---|
162 | } |
---|
163 | stptr = sptr+1; |
---|
164 | } |
---|
165 | ++sptr; |
---|
166 | } |
---|
167 | if (stptr - sptr) { |
---|
168 | if (put_into_sub_block (us, stptr, sptr-stptr) == ERROR) { |
---|
169 | return (ERROR); |
---|
170 | } |
---|
171 | } |
---|
172 | /* pad odd length strings */ |
---|
173 | |
---|
174 | if(sl & 1) { |
---|
175 | if(put_into_sub_block(us, &zero, sizeof(char)) == ERROR) { |
---|
176 | return(ERROR); |
---|
177 | } |
---|
178 | } |
---|
179 | return(SUCCESS); |
---|
180 | } |
---|
181 | |
---|
182 | /* this puts a raw block of bytes into a USP block. For those of us who |
---|
183 | have no need of USP's data types... */ |
---|
184 | |
---|
185 | USP_put_byte_block(us, buf, len) |
---|
186 | |
---|
187 | USPStream *us; |
---|
188 | Byte *buf; |
---|
189 | unsigned len; |
---|
190 | { |
---|
191 | return(put_into_sub_block(us, buf, len)); |
---|
192 | } |
---|