Debian Codegraph @

assuan/assuan-buffer.c

raw symbols SCIP
1 /* assuan-buffer.c - read and send data
2 * Copyright (C) 2001 Free Software Foundation, Inc.
3 *
4 * This file is part of GnuPG.
5 *
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #include <config.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include <errno.h>
26 #include <unistd.h>
27 #include <assert.h>
28 #ifdef USE_GNU_PTH
29 # include <pth.h>
30 #endif
31 #include "assuan-defs.h"
32
33 #ifdef HAVE_JNLIB_LOGGING
34 #include "../jnlib/logging.h"
35 #endif
36
37
38 static const char *
39 my_log_prefix (void)
40 {
41 #ifdef HAVE_JNLIB_LOGGING
42 return log_get_prefix (NULL);
43 #else
44 return "";
45 #endif
46 }
47
48
49 static int
50 writen ( int fd, const char *buffer, size_t length )
51 {
52 while (length)
53 {
54 #ifdef USE_GNU_PTH
55 int nwritten = pth_write (fd, buffer, length);
56 #else
57 int nwritten = write (fd, buffer, length);
58 #endif
59
60 if (nwritten < 0)
61 {
62 if (errno == EINTR)
63 continue;
64 return -1; /* write error */
65 }
66 length -= nwritten;
67 buffer += nwritten;
68 }
69 return 0; /* okay */
70 }
71
72 /* read an entire line */
73 static int
74 readline (int fd, char *buf, size_t buflen, int *r_nread, int *iseof)
75 {
76 size_t nleft = buflen; char *p;
77
78 *iseof = 0;
79 *r_nread = 0;
80 while (nleft > 0)
81 {
82 #ifdef USE_GNU_PTH
83 int n = pth_read (fd, buf, nleft);
84 #else
85 int n = read (fd, buf, nleft);
86 #endif
87 if (n < 0)
88 {
89 if (errno == EINTR)
90 continue;
91 return -1; /* read error */
92 }
93 else if (!n)
94 {
95 *iseof = 1;
96 break; /* allow incomplete lines */
97 }
98 p = buf;
99 nleft -= n;
100 buf += n;
101 *r_nread += n;
102
103 for (; n && *p != '\n'; n--, p++)
104 ;
105 if (n)
106 break; /* at least one full line available - that's enough for now */
107 }
108
109 return 0;
110 }
111
112
113 int
114 _assuan_read_line (ASSUAN_CONTEXT ctx)
115 {
116 char *line = ctx->inbound.line;
117 int n, nread, atticlen;
118 int rc;
119
120 if (ctx->inbound.eof)
121 return -1;
122
123 atticlen = ctx->inbound.attic.linelen;
124 if (atticlen)
125 {
126 memcpy (line, ctx->inbound.attic.line, atticlen);
127 ctx->inbound.attic.linelen = 0;
128 for (n=0; n < atticlen && line[n] != '\n'; n++)
129 ;
130 if (n < atticlen)
131 {
132 rc = 0; /* found another line in the attic */
133 nread = atticlen;
134 atticlen = 0;
135 }
136 else
137 { /* read the rest */
138 assert (atticlen < LINELENGTH);
139 rc = readline (ctx->inbound.fd, line + atticlen,
140 LINELENGTH - atticlen, &nread, &ctx->inbound.eof);
141 }
142 }
143 else
144 rc = readline (ctx->inbound.fd, line, LINELENGTH,
145 &nread, &ctx->inbound.eof);
146 if (rc)
147 {
148 if (ctx->log_fp)
149 fprintf (ctx->log_fp, "%s[%p] <- [Error: %s]\n",
150 my_log_prefix (), ctx, strerror (errno));
151 return ASSUAN_Read_Error;
152 }
153 if (!nread)
154 {
155 assert (ctx->inbound.eof);
156 if (ctx->log_fp)
157 fprintf (ctx->log_fp, "%s[%p] <- [EOF]\n", my_log_prefix (),ctx);
158 return -1;
159 }
160
161 ctx->inbound.attic.pending = 0;
162 nread += atticlen;
163 for (n=0; n < nread; n++)
164 {
165 if (line[n] == '\n')
166 {
167 if (n+1 < nread)
168 {
169 char *s, *d;
170 int i;
171
172 n++;
173 /* we have to copy the rest because the handlers are
174 allowed to modify the passed buffer */
175 for (d=ctx->inbound.attic.line, s=line+n, i=nread-n; i; i--)
176 {
177 if (*s=='\n')
178 ctx->inbound.attic.pending = 1;
179 *d++ = *s++;
180 }
181 ctx->inbound.attic.linelen = nread-n;
182 n--;
183 }
184 if (n && line[n-1] == '\r')
185 n--;
186 line[n] = 0;
187 ctx->inbound.linelen = n;
188 if (ctx->log_fp)
189 {
190 fprintf (ctx->log_fp, "%s[%p] <- ", my_log_prefix (), ctx);
191 if (ctx->confidential)
192 fputs ("[Confidential data not shown]", ctx->log_fp);
193 else
194 _assuan_log_print_buffer (ctx->log_fp,
195 ctx->inbound.line,
196 ctx->inbound.linelen);
197 putc ('\n', ctx->log_fp);
198 }
199 return 0;
200 }
201 }
202
203 if (ctx->log_fp)
204 fprintf (ctx->log_fp, "%s[%p] <- [Invalid line]\n", my_log_prefix (), ctx);
205 *line = 0;
206 ctx->inbound.linelen = 0;
207 return ctx->inbound.eof? ASSUAN_Line_Not_Terminated : ASSUAN_Line_Too_Long;
208 }
209
210
211 /* Read the next line from the client or server and return a pointer
212 to a buffer with holding that line. linelen returns the length of
213 the line. This buffer is valid until another read operation is
214 done on this buffer. The caller is allowed to modify this buffer.
215 He should only use the buffer if the function returns without an
216 error.
217
218 Returns: 0 on success or an assuan error code
219 See also: assuan_pending_line().
220 */
221 AssuanError
222 assuan_read_line (ASSUAN_CONTEXT ctx, char **line, size_t *linelen)
223 {
224 AssuanError err;
225
226 if (!ctx)
227 return ASSUAN_Invalid_Value;
228
229 err = _assuan_read_line (ctx);
230 *line = ctx->inbound.line;
231 *linelen = ctx->inbound.linelen;
232 return err;
233 }
234
235
236 /* Return true when a full line is pending for a read, without the need
237 for actual IO */
238 int
239 assuan_pending_line (ASSUAN_CONTEXT ctx)
240 {
241 return ctx && ctx->inbound.attic.pending;
242 }
243
244
245 AssuanError
246 assuan_write_line (ASSUAN_CONTEXT ctx, const char *line )
247 {
248 int rc;
249
250 if (!ctx)
251 return ASSUAN_Invalid_Value;
252
253 /* fixme: we should do some kind of line buffering */
254 if (ctx->log_fp)
255 {
256 fprintf (ctx->log_fp, "%s[%p] -> ", my_log_prefix (), ctx);
257 if (ctx->confidential)
258 fputs ("[Confidential data not shown]", ctx->log_fp);
259 else
260 _assuan_log_print_buffer (ctx->log_fp,
261 line, strlen (line));
262 putc ('\n', ctx->log_fp);
263 }
264
265 rc = writen (ctx->outbound.fd, line, strlen(line));
266 if (rc)
267 rc = ASSUAN_Write_Error;
268 if (!rc)
269 {
270 rc = writen (ctx->outbound.fd, "\n", 1);
271 if (rc)
272 rc = ASSUAN_Write_Error;
273 }
274
275 return rc;
276 }
277
278
279
280 /* Write out the data in buffer as datalines with line wrapping and
281 percent escaping. This fucntion is used for GNU's custom streams */
282 int
283 _assuan_cookie_write_data (void *cookie, const char *buffer, size_t size)
284 {
285 ASSUAN_CONTEXT ctx = cookie;
286 char *line;
287 size_t linelen;
288
289 if (ctx->outbound.data.error)
290 return 0;
291
292 line = ctx->outbound.data.line;
293 linelen = ctx->outbound.data.linelen;
294 line += linelen;
295 while (size)
296 {
297 /* insert data line header */
298 if (!linelen)
299 {
300 *line++ = 'D';
301 *line++ = ' ';
302 linelen += 2;
303 }
304
305 /* copy data, keep some space for the CRLF and to escape one character */
306 while (size && linelen < LINELENGTH-2-2)
307 {
308 if (*buffer == '%' || *buffer == '\r' || *buffer == '\n')
309 {
310 sprintf (line, "%%%02X", *(unsigned char*)buffer);
311 line += 3;
312 linelen += 3;
313 buffer++;
314 }
315 else
316 {
317 *line++ = *buffer++;
318 linelen++;
319 }
320 size--;
321 }
322
323 if (linelen >= LINELENGTH-2-2)
324 {
325 if (ctx->log_fp)
326 {
327 fprintf (ctx->log_fp, "%s[%p] -> ", my_log_prefix (), ctx);
328 if (ctx->confidential)
329 fputs ("[Confidential data not shown]", ctx->log_fp);
330 else
331 _assuan_log_print_buffer (ctx->log_fp,
332 ctx->outbound.data.line,
333 linelen);
334 putc ('\n', ctx->log_fp);
335 }
336 *line++ = '\n';
337 linelen++;
338 if (writen (ctx->outbound.fd, ctx->outbound.data.line, linelen))
339 {
340 ctx->outbound.data.error = ASSUAN_Write_Error;
341 return 0;
342 }
343 line = ctx->outbound.data.line;
344 linelen = 0;
345 }
346 }
347
348 ctx->outbound.data.linelen = linelen;
349 return 0;
350 }
351
352
353 /* Write out any buffered data
354 This fucntion is used for GNU's custom streams */
355 int
356 _assuan_cookie_write_flush (void *cookie)
357 {
358 ASSUAN_CONTEXT ctx = cookie;
359 char *line;
360 size_t linelen;
361
362 if (ctx->outbound.data.error)
363 return 0;
364
365 line = ctx->outbound.data.line;
366 linelen = ctx->outbound.data.linelen;
367 line += linelen;
368 if (linelen)
369 {
370 if (ctx->log_fp)
371 {
372 fprintf (ctx->log_fp, "%s[%p] -> ", my_log_prefix (), ctx);
373 if (ctx->confidential)
374 fputs ("[Confidential data not shown]", ctx->log_fp);
375 else
376 _assuan_log_print_buffer (ctx->log_fp,
377 ctx->outbound.data.line,
378 linelen);
379 putc ('\n', ctx->log_fp);
380 }
381 *line++ = '\n';
382 linelen++;
383 if (writen (ctx->outbound.fd, ctx->outbound.data.line, linelen))
384 {
385 ctx->outbound.data.error = ASSUAN_Write_Error;
386 return 0;
387 }
388 ctx->outbound.data.linelen = 0;
389 }
390 return 0;
391 }
392
393
394 /**
395 * assuan_send_data:
396 * @ctx: An assuan context
397 * @buffer: Data to send or NULL to flush
398 * @length: length of the data to send/
399 *
400 * This function may be used by the server or the client to send data
401 * lines. The data will be escaped as required by the Assuan protocol
402 * and may get buffered until a line is full. To force sending the
403 * data out @buffer may be passed as NULL (in which case @length must
404 * also be 0); however when used by a client this flush operation does
405 * also send the terminating "END" command to terminate the reponse on
406 * a INQUIRE response. However, when assuan_transact() is used, this
407 * function takes care of sending END itself.
408 *
409 * Return value: 0 on success or an error code
410 **/
411
412 AssuanError
413 assuan_send_data (ASSUAN_CONTEXT ctx, const void *buffer, size_t length)
414 {
415 if (!ctx)
416 return ASSUAN_Invalid_Value;
417 if (!buffer && length)
418 return ASSUAN_Invalid_Value;
419
420 if (!buffer)
421 { /* flush what we have */
422 _assuan_cookie_write_flush (ctx);
423 if (ctx->outbound.data.error)
424 return ctx->outbound.data.error;
425 if (!ctx->is_server)
426 return assuan_write_line (ctx, "END");
427 }
428 else
429 {
430 _assuan_cookie_write_data (ctx, buffer, length);
431 if (ctx->outbound.data.error)
432 return ctx->outbound.data.error;
433 }
434
435 return 0;
436 }
437
438
439
440