~ubuntu-branches/ubuntu/lucid/gpgme1.0/lucid-security

« back to all changes in this revision

Viewing changes to assuan/assuan-buffer.c

  • Committer: Bazaar Package Importer
  • Author(s): Jose Carlos Garcia Sogo
  • Date: 2005-10-08 14:26:01 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051008142601-jajb789561w8nwhu
Tags: 1.1.0-1
New upstream version. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* assuan-buffer.c - read and send data
2
 
 *      Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
 
2
 *      Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
3
3
 *
4
4
 * This file is part of Assuan.
5
5
 *
25
25
#include <errno.h>
26
26
#include <unistd.h>
27
27
#include <assert.h>
 
28
#ifdef HAVE_W32_SYSTEM
 
29
#include <process.h>
 
30
#endif
28
31
#include "assuan-defs.h"
29
32
 
30
33
static int
32
35
{
33
36
  while (length)
34
37
    {
35
 
      ssize_t nwritten = ctx->io->write (ctx, buffer, length);
 
38
      ssize_t nwritten = ctx->io->writefnc (ctx, buffer, length);
36
39
      
37
40
      if (nwritten < 0)
38
41
        {
49
52
/* Read an entire line.  */
50
53
static int
51
54
readline (ASSUAN_CONTEXT ctx, char *buf, size_t buflen,
52
 
          int *r_nread, int *eof)
 
55
          int *r_nread, int *r_eof)
53
56
{
54
57
  size_t nleft = buflen;
55
58
  char *p;
56
59
 
57
 
  *eof = 0;
 
60
  *r_eof = 0;
58
61
  *r_nread = 0;
59
62
  while (nleft > 0)
60
63
    {
61
 
      ssize_t n = ctx->io->read (ctx, buf, nleft);
 
64
      ssize_t n = ctx->io->readfnc (ctx, buf, nleft);
62
65
 
63
66
      if (n < 0)
64
67
        {
68
71
        }
69
72
      else if (!n)
70
73
        {
71
 
          *eof = 1;
 
74
          *r_eof = 1;
72
75
          break; /* allow incomplete lines */
73
76
        }
74
77
      p = buf;
125
128
  if (rc)
126
129
    {
127
130
      if (ctx->log_fp)
128
 
        fprintf (ctx->log_fp, "%s[%p] <- [Error: %s]\n",
129
 
                 assuan_get_assuan_log_prefix (), ctx, strerror (errno));
 
131
        fprintf (ctx->log_fp, "%s[%u.%p] DBG: <- [Error: %s]\n",
 
132
                 assuan_get_assuan_log_prefix (),
 
133
                 (unsigned int)getpid (), ctx, strerror (errno));
130
134
      return ASSUAN_Read_Error;
131
135
    }
132
136
  if (!nread)
133
137
    {
134
138
      assert (ctx->inbound.eof);
135
139
      if (ctx->log_fp)
136
 
        fprintf (ctx->log_fp, "%s[%p] <- [EOF]\n",
137
 
                 assuan_get_assuan_log_prefix (), ctx);
 
140
        fprintf (ctx->log_fp, "%s[%u.%p] DBG: <- [EOF]\n",
 
141
                 assuan_get_assuan_log_prefix (),
 
142
                 (unsigned int)getpid (), ctx);
138
143
      return -1;
139
144
    }
140
145
 
165
170
      ctx->inbound.linelen = endp - line;
166
171
      if (ctx->log_fp)
167
172
        {
168
 
          fprintf (ctx->log_fp, "%s[%p] <- ",
169
 
                   assuan_get_assuan_log_prefix (), ctx);
 
173
          fprintf (ctx->log_fp, "%s[%u.%p] DBG: <- ",
 
174
                   assuan_get_assuan_log_prefix (),
 
175
                   (unsigned int)getpid (), ctx);
170
176
          if (ctx->confidential)
171
177
            fputs ("[Confidential data not shown]", ctx->log_fp);
172
178
          else
180
186
  else
181
187
    {
182
188
      if (ctx->log_fp)
183
 
        fprintf (ctx->log_fp, "%s[%p] <- [Invalid line]\n",
184
 
                 assuan_get_assuan_log_prefix (), ctx);
 
189
        fprintf (ctx->log_fp, "%s[%u.%p] DBG: <- [Invalid line]\n",
 
190
                 assuan_get_assuan_log_prefix (),
 
191
                 (unsigned int)getpid (), ctx);
185
192
      *line = 0;
186
193
      ctx->inbound.linelen = 0;
187
194
      return ctx->inbound.eof ? ASSUAN_Line_Not_Terminated
199
206
   Returns 0 on success or an assuan error code.
200
207
   See also: assuan_pending_line().
201
208
*/
202
 
AssuanError
 
209
assuan_error_t
203
210
assuan_read_line (ASSUAN_CONTEXT ctx, char **line, size_t *linelen)
204
211
{
205
 
  AssuanError err;
 
212
  assuan_error_t err;
206
213
 
207
214
  if (!ctx)
208
215
    return ASSUAN_Invalid_Value;
223
230
}
224
231
 
225
232
 
226
 
AssuanError 
 
233
assuan_error_t 
 
234
_assuan_write_line (assuan_context_t ctx, const char *prefix,
 
235
                    const char *line, size_t len)
 
236
{
 
237
  int rc = 0;
 
238
  size_t prefixlen = prefix? strlen (prefix):0;
 
239
 
 
240
  /* Make sure that the line is short enough. */
 
241
  if (len + prefixlen + 2 > ASSUAN_LINELENGTH)
 
242
    {
 
243
      if (ctx->log_fp)
 
244
        fprintf (ctx->log_fp, "%s[%u.%p] DBG: -> "
 
245
                 "[supplied line too long -truncated]\n",
 
246
                 assuan_get_assuan_log_prefix (),
 
247
                 (unsigned int)getpid (), ctx);
 
248
      if (prefixlen > 5)
 
249
        prefixlen = 5;
 
250
      if (len > ASSUAN_LINELENGTH - prefixlen - 2)
 
251
        len = ASSUAN_LINELENGTH - prefixlen - 2 - 1;
 
252
    }
 
253
 
 
254
  /* Fixme: we should do some kind of line buffering.  */
 
255
  if (ctx->log_fp)
 
256
    {
 
257
      fprintf (ctx->log_fp, "%s[%u.%p] DBG: -> ",
 
258
               assuan_get_assuan_log_prefix (),
 
259
               (unsigned int)getpid (), ctx);
 
260
      if (ctx->confidential)
 
261
        fputs ("[Confidential data not shown]", ctx->log_fp);
 
262
      else
 
263
        _assuan_log_print_buffer (ctx->log_fp, line, len);
 
264
      putc ('\n', ctx->log_fp);
 
265
    }
 
266
 
 
267
  if (prefixlen)
 
268
    {
 
269
      rc = writen (ctx, prefix, prefixlen);
 
270
      if (rc)
 
271
        rc = ASSUAN_Write_Error;
 
272
    }
 
273
  if (!rc)
 
274
    {
 
275
      rc = writen (ctx, line, len);
 
276
      if (rc)
 
277
        rc = ASSUAN_Write_Error;
 
278
      if (!rc)
 
279
        {
 
280
          rc = writen (ctx, "\n", 1);
 
281
          if (rc)
 
282
            rc = ASSUAN_Write_Error;
 
283
        }
 
284
    }
 
285
  return rc;
 
286
}
 
287
 
 
288
 
 
289
assuan_error_t 
227
290
assuan_write_line (ASSUAN_CONTEXT ctx, const char *line)
228
291
{
229
 
  int rc;
230
292
  size_t len;
231
293
  const char *s;
232
294
 
238
300
  s = strchr (line, '\n');
239
301
  len = s? (s-line) : strlen (line);
240
302
 
241
 
  if (len > LINELENGTH - 2)
242
 
    return ASSUAN_Line_Too_Long;
243
 
 
244
 
  /* fixme: we should do some kind of line buffering.  */
245
 
  if (ctx->log_fp)
246
 
    {
247
 
      fprintf (ctx->log_fp, "%s[%p] -> ",
248
 
               assuan_get_assuan_log_prefix (), ctx);
249
 
      if (s)
250
 
        fputs ("[supplied line contained a LF]", ctx->log_fp);
251
 
      if (ctx->confidential)
252
 
        fputs ("[Confidential data not shown]", ctx->log_fp);
253
 
      else
254
 
        _assuan_log_print_buffer (ctx->log_fp, line, len);
255
 
      putc ('\n', ctx->log_fp);
256
 
    }
257
 
 
258
 
  rc = writen (ctx, line, len);
259
 
  if (rc)
260
 
    rc = ASSUAN_Write_Error;
261
 
  if (!rc)
262
 
    {
263
 
      rc = writen (ctx, "\n", 1);
264
 
      if (rc)
265
 
        rc = ASSUAN_Write_Error;
266
 
    }
267
 
 
268
 
  return rc;
 
303
  if (ctx->log_fp && s)
 
304
    fprintf (ctx->log_fp, "%s[%u.%p] DBG: -> "
 
305
             "[supplied line contained a LF -truncated]\n",
 
306
             assuan_get_assuan_log_prefix (),
 
307
             (unsigned int)getpid (), ctx);
 
308
 
 
309
  return _assuan_write_line (ctx, NULL, line, len);
269
310
}
270
311
 
271
312
 
272
313
 
273
314
/* Write out the data in buffer as datalines with line wrapping and
274
 
   percent escaping.  This fucntion is used for GNU's custom streams */
 
315
   percent escaping.  This function is used for GNU's custom streams */
275
316
int
276
 
_assuan_cookie_write_data (void *cookie, const char *buffer, size_t size)
 
317
_assuan_cookie_write_data (void *cookie, const char *buffer, size_t orig_size)
277
318
{
278
319
  ASSUAN_CONTEXT ctx = cookie;
 
320
  size_t size = orig_size;
279
321
  char *line;
280
322
  size_t linelen;
281
323
 
317
359
        {
318
360
          if (ctx->log_fp)
319
361
            {
320
 
              fprintf (ctx->log_fp, "%s[%p] -> ",
321
 
                       assuan_get_assuan_log_prefix (), ctx);
 
362
              fprintf (ctx->log_fp, "%s[%u.%p] DBG: -> ",
 
363
                       assuan_get_assuan_log_prefix (),
 
364
                       (unsigned int)getpid (), ctx);
322
365
 
323
366
              if (ctx->confidential)
324
367
                fputs ("[Confidential data not shown]", ctx->log_fp);
341
384
    }
342
385
 
343
386
  ctx->outbound.data.linelen = linelen;
344
 
  return 0;
 
387
  return (int)orig_size;
345
388
}
346
389
 
347
390
 
348
391
/* Write out any buffered data 
349
 
   This fucntion is used for GNU's custom streams */
 
392
   This function is used for GNU's custom streams */
350
393
int
351
394
_assuan_cookie_write_flush (void *cookie)
352
395
{
364
407
    {
365
408
      if (ctx->log_fp)
366
409
        {
367
 
          fprintf (ctx->log_fp, "%s[%p] -> ",
368
 
                   assuan_get_assuan_log_prefix (), ctx);
 
410
          fprintf (ctx->log_fp, "%s[%u.%p] DBG: -> ",
 
411
                   assuan_get_assuan_log_prefix (),
 
412
                   (unsigned int)getpid (), ctx);
369
413
          if (ctx->confidential)
370
414
            fputs ("[Confidential data not shown]", ctx->log_fp);
371
415
          else
404
448
 * Return value: 0 on success or an error code
405
449
 **/
406
450
 
407
 
AssuanError
 
451
assuan_error_t
408
452
assuan_send_data (ASSUAN_CONTEXT ctx, const void *buffer, size_t length)
409
453
{
410
454
  if (!ctx)
430
474
  return 0;
431
475
}
432
476
 
433
 
AssuanError
 
477
assuan_error_t
434
478
assuan_sendfd (ASSUAN_CONTEXT ctx, int fd)
435
479
{
436
480
  if (! ctx->io->sendfd)
440
484
  return ctx->io->sendfd (ctx, fd);
441
485
}
442
486
 
443
 
AssuanError
 
487
assuan_error_t
444
488
assuan_receivefd (ASSUAN_CONTEXT ctx, int *fd)
445
489
{
446
490
  if (! ctx->io->receivefd)