~ubuntu-branches/ubuntu/hardy/gnupg2/hardy-proposed

« back to all changes in this revision

Viewing changes to sm/base64.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Bienia
  • Date: 2007-05-15 13:54:55 UTC
  • mfrom: (1.1.8 upstream)
  • Revision ID: james.westby@ubuntu.com-20070515135455-89qfyalmgjy6gcqw
Tags: 2.0.4-1ubuntu1
* Merge from debian unstable, remaining changes:
  - Remove libpcsclite-dev, libopensc2-dev build dependencies (they are in
    universe).
  - Build-depend on libcurl3-gnutls-dev
  - g10/call-agent.c: set DBG_ASSUAN to 0 to suppress a debug message
  - Include /doc files as done with gnupg
  - debian/rules: add doc/com-certs.pem to the docs for gpgsm
  - debian/copyright: update download url
  - debian/README.Debian: remove note the gnupg2 isn't released yet.
  - debian/control: Change Maintainer/XSBC-Original-Maintainer field.

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
 
31
31
#include "gpgsm.h"
32
32
 
 
33
 
33
34
#include <ksba.h>
34
35
 
35
36
#include "i18n.h"
43
44
/* data used by the reader callbacks */
44
45
struct reader_cb_parm_s {
45
46
  FILE *fp;
 
47
  
46
48
  unsigned char line[1024];
47
49
  int linelen;
48
50
  int readpos;
71
73
 
72
74
/* data used by the writer callbacks */
73
75
struct writer_cb_parm_s {
74
 
  FILE *fp;
 
76
  FILE *fp;            /* FP is only used if STREAM is NULL.  */
 
77
  estream_t stream;    /* Alternative output if not NULL.  */
 
78
 
75
79
  const char *pem_name;
76
80
  
77
81
  int wrote_begin;
400
404
 
401
405
 
402
406
 
 
407
/* Call either es_putc or the plain putc.  */
 
408
static void
 
409
do_putc (int value, FILE *fp, estream_t stream)
 
410
{
 
411
  if (stream)
 
412
    es_putc (value, stream);
 
413
  else
 
414
    putc (value, fp);
 
415
}
 
416
 
 
417
/* Call either es_fputs or the plain fputs.  */
 
418
static void
 
419
do_fputs (const char *string, FILE *fp, estream_t stream)
 
420
{
 
421
  if (stream)
 
422
    es_fputs (string, stream);
 
423
  else
 
424
    fputs (string, fp);
 
425
}
 
426
 
 
427
 
403
428
static int
404
429
base64_writer_cb (void *cb_value, const void *buffer, size_t count)
405
430
{
408
433
  int i, c, idx, quad_count;
409
434
  const unsigned char *p;
410
435
  FILE *fp = parm->fp;
 
436
  estream_t stream = parm->stream;
411
437
 
412
438
  if (!count)
413
439
    return 0;
416
442
    {
417
443
      if (parm->pem_name)
418
444
        {
419
 
          fputs ("-----BEGIN ", fp);
420
 
          fputs (parm->pem_name, fp);
421
 
          fputs ("-----\n", fp);
 
445
          do_fputs ("-----BEGIN ", fp, stream);
 
446
          do_fputs (parm->pem_name, fp, stream);
 
447
          do_fputs ("-----\n", fp, stream);
422
448
        }
423
449
      parm->wrote_begin = 1;
424
450
      parm->base64.idx = 0;
437
463
        {
438
464
          idx = 0;
439
465
          c = bintoasc[(*radbuf >> 2) & 077];
440
 
          putc (c, fp);
 
466
          do_putc (c, fp, stream);
441
467
          c = bintoasc[(((*radbuf<<4)&060)|((radbuf[1] >> 4)&017))&077];
442
 
          putc (c, fp);
 
468
          do_putc (c, fp, stream);
443
469
          c = bintoasc[(((radbuf[1]<<2)&074)|((radbuf[2]>>6)&03))&077];
444
 
          putc (c, fp);
 
470
          do_putc (c, fp, stream);
445
471
          c = bintoasc[radbuf[2]&077];
446
 
          putc (c, fp);
 
472
          do_putc (c, fp, stream);
447
473
          if (++quad_count >= (64/4)) 
448
474
            {
449
 
              fputs (LF, fp);
 
475
              do_fputs (LF, fp, stream);
450
476
              quad_count = 0;
451
477
            }
452
478
        }
456
482
  parm->base64.idx = idx;
457
483
  parm->base64.quad_count = quad_count;
458
484
 
459
 
  return ferror (fp) ? gpg_error_from_syserror () : 0;
 
485
  return ((stream? es_ferror (stream) : ferror (fp)) 
 
486
          ? gpg_error_from_syserror () 
 
487
          : 0);
 
488
}
 
489
 
 
490
/* This callback is only used in stream mode.  Hiowever, we don't
 
491
   restrict it to this.  */
 
492
static int
 
493
plain_writer_cb (void *cb_value, const void *buffer, size_t count)
 
494
{
 
495
  struct writer_cb_parm_s *parm = cb_value;
 
496
  FILE *fp = parm->fp;
 
497
  estream_t stream = parm->stream;
 
498
 
 
499
  if (!count)
 
500
    return 0;
 
501
 
 
502
  if (stream)
 
503
    es_write (stream, buffer, count, NULL);
 
504
  else
 
505
    fwrite (buffer, count, 1, fp);
 
506
 
 
507
  return ((stream? es_ferror (stream) : ferror (fp)) 
 
508
          ? gpg_error_from_syserror () 
 
509
          : 0);
460
510
}
461
511
 
462
512
static int
465
515
  unsigned char radbuf[4];
466
516
  int i, c, idx, quad_count;
467
517
  FILE *fp = parm->fp;
 
518
  estream_t stream = parm->stream;
468
519
 
469
520
  if (!parm->wrote_begin)
470
 
    return 0; /* nothing written */
 
521
    return 0; /* Nothing written or we are not called in base-64 mode. */
471
522
 
472
523
  /* flush the base64 encoding */
473
524
  idx = parm->base64.idx;
478
529
  if (idx)
479
530
    {
480
531
      c = bintoasc[(*radbuf>>2)&077];
481
 
      putc (c, fp);
 
532
      do_putc (c, fp, stream);
482
533
      if (idx == 1)
483
534
        {
484
535
          c = bintoasc[((*radbuf << 4) & 060) & 077];
485
 
          putc (c, fp);
486
 
          putc ('=', fp);
487
 
          putc ('=', fp);
 
536
          do_putc (c, fp, stream);
 
537
          do_putc ('=', fp, stream);
 
538
          do_putc ('=', fp, stream);
488
539
        }
489
540
      else 
490
541
        { 
491
542
          c = bintoasc[(((*radbuf<<4)&060)|((radbuf[1]>>4)&017))&077];
492
 
          putc (c, fp);
 
543
          do_putc (c, fp, stream);
493
544
          c = bintoasc[((radbuf[1] << 2) & 074) & 077];
494
 
          putc (c, fp);
495
 
          putc ('=', fp);
 
545
          do_putc (c, fp, stream);
 
546
          do_putc ('=', fp, stream);
496
547
 
497
548
        }
498
549
      if (++quad_count >= (64/4)) 
499
550
        {
500
 
          fputs (LF, fp);
 
551
          do_fputs (LF, fp, stream);
501
552
          quad_count = 0;
502
553
        }
503
554
    }
504
555
 
505
556
  if (quad_count)
506
 
    fputs (LF, fp);
 
557
    do_fputs (LF, fp, stream);
507
558
 
508
559
  if (parm->pem_name)
509
560
    {
510
 
      fputs ("-----END ", fp);
511
 
      fputs (parm->pem_name, fp);
512
 
      fputs ("-----\n", fp);
 
561
      do_fputs ("-----END ", fp, stream);
 
562
      do_fputs (parm->pem_name, fp, stream);
 
563
      do_fputs ("-----\n", fp, stream);
513
564
    }
514
 
  return ferror (fp)? gpg_error (gpg_err_code_from_errno (errno)) : 0;
 
565
 
 
566
  return ((stream? es_ferror (stream) : ferror (fp)) 
 
567
          ? gpg_error_from_syserror () 
 
568
          : 0);
515
569
}
516
570
 
517
571
 
597
651
 
598
652
 
599
653
 
600
 
/* Create a writer for the given stream.  Depending on the control
601
 
   information an output encoding is automagically choosen.  The
602
 
   function returns a Base64Context object which must be passed to the
603
 
   gpgme_destroy_writer function.  The created KsbaWriter object is
604
 
   also returned, but the caller must not call the ksba_reader_release
605
 
   function on. */
 
654
/* Create a writer for the given stream FP or STREAM.  Depending on
 
655
   the control information an output encoding is automagically
 
656
   choosen.  The function returns a Base64Context object which must be
 
657
   passed to the gpgme_destroy_writer function.  The created
 
658
   KsbaWriter object is also returned, but the caller must not call
 
659
   the ksba_reader_release function on. */
606
660
int
607
661
gpgsm_create_writer (Base64Context *ctx,
608
 
                     ctrl_t ctrl, FILE *fp, ksba_writer_t *r_writer)
 
662
                     ctrl_t ctrl, FILE *fp, estream_t stream,
 
663
                     ksba_writer_t *r_writer)
609
664
{
610
665
  int rc;
611
666
  ksba_writer_t w;
625
680
  if (ctrl->create_pem || ctrl->create_base64)
626
681
    {
627
682
      (*ctx)->u.wparm.fp = fp;
 
683
      (*ctx)->u.wparm.stream = stream;
628
684
      if (ctrl->create_pem)
629
685
        (*ctx)->u.wparm.pem_name = ctrl->pem_name? ctrl->pem_name
630
686
                                                 : "CMS OBJECT";
631
687
      rc = ksba_writer_set_cb (w, base64_writer_cb, &(*ctx)->u.wparm);
632
688
    }
 
689
  else if (stream)
 
690
    {
 
691
      (*ctx)->u.wparm.fp = fp;
 
692
      (*ctx)->u.wparm.stream = stream;
 
693
      rc = ksba_writer_set_cb (w, plain_writer_cb, &(*ctx)->u.wparm);
 
694
    }
633
695
  else
634
696
    rc = ksba_writer_set_file (w, fp);
635
697
 
655
717
    return gpg_error (GPG_ERR_INV_VALUE);
656
718
  parm = &ctx->u.wparm;
657
719
  if (parm->did_finish)
658
 
    return 0; /* already done */
 
720
    return 0; /* Already done. */
659
721
  parm->did_finish = 1;
660
 
  if (!parm->fp)
661
 
    return 0; /* callback was not used */
 
722
  if (!parm->fp && !parm->stream)
 
723
    return 0; /* Callback was not used.  */
662
724
  return base64_finish_write (parm);
663
725
}
664
726