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
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)
2885
const unsigned char *p = buffer;
2889
ESTREAM_LOCK (stream);
2890
for (; length; length--, p++, count++)
2893
|| (*p >= 0x7f && *p < 0xa0)
2895
&& (strchr (delimiters, *p) || *p == '\\')))
2897
es_putc_unlocked ('\\', stream);
2901
es_putc_unlocked ('n', stream);
2904
else if (*p == '\r')
2906
es_putc_unlocked ('r', stream);
2909
else if (*p == '\f')
2911
es_putc_unlocked ('f', stream);
2914
else if (*p == '\v')
2916
es_putc_unlocked ('v', stream);
2919
else if (*p == '\b')
2921
es_putc_unlocked ('b', stream);
2926
es_putc_unlocked('0', stream);
2931
es_fprintf_unlocked (stream, "x%02x", *p);
2937
es_putc_unlocked (*p, stream);
2943
*bytes_written = count;
2944
ret = es_ferror_unlocked (stream)? -1 : 0;
2945
ESTREAM_UNLOCK (stream);
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. */
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 )
2961
const unsigned char *s;
2964
#define tohex(n) ((n) < 10 ? ((n) + '0') : (((n) - 10) + 'A'))
2969
ESTREAM_LOCK (stream);
2971
for (s = buffer; length; s++, length--)
2973
es_putc_unlocked ( tohex ((*s>>4)&15), stream);
2974
es_putc_unlocked ( tohex (*s&15), stream);
2979
*bytes_written = count;
2980
ret = es_ferror_unlocked (stream)? -1 : 0;
2982
ESTREAM_UNLOCK (stream);
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.
2996
THIS IS NOT A STANDARD ESTREAM FUNCTION AND ONLY USED BY GNUPG. */
2998
es_write_sanitized_utf8_buffer (estream_t stream,
2999
const void *buffer, size_t length,
3000
const char *delimiters, size_t *bytes_written)
3002
const char *p = buffer;
3005
/* We can handle plain ascii simpler, so check for it first. */
3006
for (i=0; i < length; i++ )
3008
if ( (p[i] & 0x80) )
3013
int delim = delimiters? *delimiters : 0;
3017
/*(utf8 conversion already does the control character quoting). */
3018
buf = utf8_to_native (p, length, delim);
3020
*bytes_written = strlen (buf);
3021
ret = es_fputs (buf, stream);
3026
return es_write_sanitized (stream, p, length, delimiters, bytes_written);
3028
#endif /*GNUPG_MAJOR_VERSION*/