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

« back to all changes in this revision

Viewing changes to common/estream.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:
1
1
/* estream.c - Extended Stream I/O Library
2
 
 * Copyright (C) 2004, 2006 g10 Code GmbH
 
2
 * Copyright (C) 2004, 2006, 2007 g10 Code GmbH
3
3
 *
4
4
 * This file is part of Libestream.
5
5
 *
50
50
# include <pth.h>
51
51
#endif
52
52
 
 
53
#ifdef GNUPG_MAJOR_VERSION
 
54
#include "../common/util.h"
 
55
#endif
 
56
 
53
57
#ifndef HAVE_MKSTEMP
54
58
int mkstemp (char *template);
55
59
#endif
206
210
/* Macros.  */
207
211
 
208
212
/* Calculate array dimension.  */
 
213
#ifndef DIM
209
214
#define DIM(array) (sizeof (array) / sizeof (*array))
 
215
#endif
210
216
 
211
217
/* Evaluate EXPRESSION, setting VARIABLE to the return code, if
212
218
   VARIABLE is zero.  */
741
747
static int
742
748
es_convert_mode (const char *mode, unsigned int *flags)
743
749
{
 
750
 
 
751
  /* FIXME: We need to allow all combinations for mode flags and for
 
752
     binary we need to do a
 
753
 
 
754
     #ifdef HAVE_DOSISH_SYSTEM
 
755
       setmode (fd, O_BINARY);
 
756
     #endif
 
757
  */
744
758
  struct
745
759
  {
746
760
    const char *mode;
2703
2717
}
2704
2718
 
2705
2719
 
 
2720
static int
 
2721
es_fprintf_unlocked (estream_t ES__RESTRICT stream,
 
2722
            const char *ES__RESTRICT format, ...)
 
2723
{
 
2724
  int ret;
 
2725
  
 
2726
  va_list ap;
 
2727
  va_start (ap, format);
 
2728
  ret = es_print (stream, format, ap);
 
2729
  va_end (ap);
 
2730
 
 
2731
  return ret;
 
2732
}
 
2733
 
 
2734
 
2706
2735
int
2707
2736
es_fprintf (estream_t ES__RESTRICT stream,
2708
2737
            const char *ES__RESTRICT format, ...)
2840
2869
  return opaque;
2841
2870
}
2842
2871
 
 
2872
 
 
2873
 
 
2874
/* Print a BUFFER to STREAM while replacing all control characters and
 
2875
   the characters in DELIMITERS by standard C escape sequences.
 
2876
   Returns 0 on success or -1 on error.  If BYTES_WRITTEN is not NULL
 
2877
   the number of bytes actually written are stored at this
 
2878
   address.  */
 
2879
int 
 
2880
es_write_sanitized (estream_t ES__RESTRICT stream,
 
2881
                    const void * ES__RESTRICT buffer, size_t length,
 
2882
                    const char * delimiters, 
 
2883
                    size_t * ES__RESTRICT bytes_written)
 
2884
{
 
2885
  const unsigned char *p = buffer;
 
2886
  size_t count = 0;
 
2887
  int ret;
 
2888
 
 
2889
  ESTREAM_LOCK (stream);
 
2890
  for (; length; length--, p++, count++)
 
2891
    {
 
2892
      if (*p < 0x20 
 
2893
          || (*p >= 0x7f && *p < 0xa0)
 
2894
          || (delimiters 
 
2895
              && (strchr (delimiters, *p) || *p == '\\')))
 
2896
        {
 
2897
          es_putc_unlocked ('\\', stream);
 
2898
          count++;
 
2899
          if (*p == '\n')
 
2900
            {
 
2901
              es_putc_unlocked ('n', stream);
 
2902
              count++;
 
2903
            }
 
2904
          else if (*p == '\r')
 
2905
            {
 
2906
              es_putc_unlocked ('r', stream);
 
2907
              count++;
 
2908
            }
 
2909
          else if (*p == '\f')
 
2910
            {
 
2911
              es_putc_unlocked ('f', stream);
 
2912
              count++;
 
2913
            }
 
2914
          else if (*p == '\v')
 
2915
            {
 
2916
              es_putc_unlocked ('v', stream);
 
2917
              count++;
 
2918
            }
 
2919
          else if (*p == '\b')
 
2920
            {
 
2921
              es_putc_unlocked ('b', stream);
 
2922
              count++;
 
2923
            }
 
2924
          else if (!*p)
 
2925
            {
 
2926
              es_putc_unlocked('0', stream);
 
2927
              count++;
 
2928
            }
 
2929
          else
 
2930
            {
 
2931
              es_fprintf_unlocked (stream, "x%02x", *p);
 
2932
              count += 3;
 
2933
            }
 
2934
        }
 
2935
      else
 
2936
        {
 
2937
          es_putc_unlocked (*p, stream);
 
2938
          count++;
 
2939
        }
 
2940
    }
 
2941
 
 
2942
  if (bytes_written)
 
2943
    *bytes_written = count;
 
2944
  ret =  es_ferror_unlocked (stream)? -1 : 0;
 
2945
  ESTREAM_UNLOCK (stream);
 
2946
 
 
2947
  return ret;
 
2948
}
 
2949
 
 
2950
 
 
2951
/* Write LENGTH bytes of BUFFER to STREAM as a hex encoded string.
 
2952
   RESERVED must be 0.  Returns 0 on success or -1 on error.  If
 
2953
   BYTES_WRITTEN is not NULL the number of bytes actually written are
 
2954
   stored at this address.  */
 
2955
int
 
2956
es_write_hexstring (estream_t ES__RESTRICT stream,
 
2957
                    const void *ES__RESTRICT buffer, size_t length,
 
2958
                    int reserved, size_t *ES__RESTRICT bytes_written )
 
2959
{
 
2960
  int ret;
 
2961
  const unsigned char *s;
 
2962
  size_t count = 0;
 
2963
 
 
2964
#define tohex(n) ((n) < 10 ? ((n) + '0') : (((n) - 10) + 'A'))
 
2965
 
 
2966
  if (!length)
 
2967
    return 0;
 
2968
 
 
2969
  ESTREAM_LOCK (stream);
 
2970
 
 
2971
  for (s = buffer; length; s++, length--)
 
2972
    {
 
2973
      es_putc_unlocked ( tohex ((*s>>4)&15), stream);
 
2974
      es_putc_unlocked ( tohex (*s&15), stream);
 
2975
      count += 2;
 
2976
    }
 
2977
 
 
2978
  if (bytes_written)
 
2979
    *bytes_written = count;
 
2980
  ret = es_ferror_unlocked (stream)? -1 : 0;
 
2981
 
 
2982
  ESTREAM_UNLOCK (stream);
 
2983
 
 
2984
  return ret;
 
2985
 
 
2986
#undef tohex
 
2987
}
 
2988
 
 
2989
 
 
2990
 
 
2991
#ifdef GNUPG_MAJOR_VERSION
 
2992
/* Special estream function to print an UTF8 string in the native
 
2993
   encoding.  The interface is the same as es_write_sanitized, however
 
2994
   only one delimiter may be supported. 
 
2995
 
 
2996
   THIS IS NOT A STANDARD ESTREAM FUNCTION AND ONLY USED BY GNUPG. */
 
2997
int
 
2998
es_write_sanitized_utf8_buffer (estream_t stream,
 
2999
                                const void *buffer, size_t length, 
 
3000
                                const char *delimiters, size_t *bytes_written)
 
3001
{
 
3002
  const char *p = buffer;
 
3003
  size_t i;
 
3004
 
 
3005
  /* We can handle plain ascii simpler, so check for it first. */
 
3006
  for (i=0; i < length; i++ ) 
 
3007
    {
 
3008
      if ( (p[i] & 0x80) )
 
3009
        break;
 
3010
    }
 
3011
  if (i < length)
 
3012
    {
 
3013
      int delim = delimiters? *delimiters : 0;
 
3014
      char *buf;
 
3015
      int ret;
 
3016
 
 
3017
      /*(utf8 conversion already does the control character quoting). */
 
3018
      buf = utf8_to_native (p, length, delim);
 
3019
      if (bytes_written)
 
3020
        *bytes_written = strlen (buf);
 
3021
      ret = es_fputs (buf, stream);
 
3022
      xfree (buf);
 
3023
      return i;
 
3024
    }
 
3025
  else
 
3026
    return es_write_sanitized (stream, p, length, delimiters, bytes_written);
 
3027
}
 
3028
#endif /*GNUPG_MAJOR_VERSION*/
 
3029
 
 
3030
 
 
3031