75 #define CHARTYPE_BS_ESC (ASN1_STRFLGS_ESC_2253 | CHARTYPE_FIRST_ESC_2253 | CHARTYPE_LAST_ESC_2253)
77 #define ESC_FLAGS (ASN1_STRFLGS_ESC_2253 | \
78 ASN1_STRFLGS_ESC_QUOTE | \
79 ASN1_STRFLGS_ESC_CTRL | \
87 static int send_mem_chars(
void *
arg,
const void *buf,
int len)
91 memcpy(*out, buf, len);
97 static int send_bio_chars(
void *
arg,
const void *buf,
int len)
100 if(
BIO_write(arg, buf, len) != len)
return 0;
104 static int send_fp_chars(
void *arg,
const void *buf,
int len)
107 if(fwrite(buf, 1, len, arg) != (
unsigned int)len)
return 0;
111 typedef int char_io(
void *arg,
const void *buf,
int len);
120 static int do_esc_char(
unsigned long c,
unsigned char flags,
char *do_quotes,
char_io *io_ch,
void *arg)
122 unsigned char chflgs, chtmp;
129 if(!io_ch(arg, tmphex, 10))
return -1;
134 if(!io_ch(arg, tmphex, 6))
return -1;
137 chtmp = (
unsigned char)c;
139 else chflgs = char_type[chtmp] &
flags;
143 if(do_quotes) *do_quotes = 1;
144 if(!io_ch(arg, &chtmp, 1))
return -1;
147 if(!io_ch(arg,
"\\", 1))
return -1;
148 if(!io_ch(arg, &chtmp, 1))
return -1;
153 if(!io_ch(arg, tmphex, 3))
return -1;
159 if (chtmp ==
'\\' && flags &
ESC_FLAGS) {
160 if(!io_ch(arg,
"\\\\", 2))
return -1;
163 if(!io_ch(arg, &chtmp, 1))
return -1;
167 #define BUF_TYPE_WIDTH_MASK 0x7
168 #define BUF_TYPE_CONVUTF8 0x8
175 static int do_buf(
unsigned char *buf,
int buflen,
176 int type,
unsigned char flags,
char *quotes,
char_io *io_ch,
void *arg)
179 unsigned char orflags, *
p, *
q;
189 c = ((
unsigned long)*p++) << 24;
190 c |= ((
unsigned long)*p++) << 16;
191 c |= ((
unsigned long)*p++) << 8;
196 c = ((
unsigned long)*p++) << 8;
214 unsigned char utfbuf[6];
216 utflen =
UTF8_putc(utfbuf,
sizeof utfbuf, c);
217 for(i = 0; i < utflen; i++) {
223 len = do_esc_char(utfbuf[i], (
unsigned char)(flags | orflags), quotes, io_ch, arg);
224 if(len < 0)
return -1;
228 len = do_esc_char(c, (
unsigned char)(flags | orflags), quotes, io_ch, arg);
229 if(len < 0)
return -1;
238 static int do_hex_dump(
char_io *io_ch,
void *arg,
unsigned char *buf,
int buflen)
240 static const char hexdig[] =
"0123456789ABCDEF";
241 unsigned char *
p, *
q;
247 hextmp[0] = hexdig[*p >> 4];
248 hextmp[1] = hexdig[*p & 0xf];
249 if(!io_ch(arg, hextmp, 2))
return -1;
268 unsigned char *der_buf, *
p;
271 if(!io_ch(arg,
"#", 1))
return -1;
274 outlen = do_hex_dump(io_ch, arg, str->
data, str->
length);
275 if(outlen < 0)
return -1;
280 der_len = i2d_ASN1_TYPE(&t, NULL);
282 if(!der_buf)
return -1;
284 i2d_ASN1_TYPE(&t, &p);
285 outlen = do_hex_dump(io_ch, arg, der_buf, der_len);
287 if(outlen < 0)
return -1;
296 static const signed char tag2nbyte[] = {
314 static int do_print_ex(
char_io *io_ch,
void *arg,
unsigned long lflags,
ASN1_STRING *str)
322 flags = (
unsigned char)(lflags & ESC_FLAGS);
332 outlen += strlen(tagname);
333 if(!io_ch(arg, tagname, outlen) || !io_ch(arg,
":", 1))
return -1;
345 if((type > 0) && (type < 31)) type = tag2nbyte[
type];
351 len = do_dump(lflags, io_ch, arg, str);
352 if(len < 0)
return -1;
367 len = do_buf(str->
data, str->
length, type, flags, "es, io_ch, NULL);
368 if(len < 0)
return -1;
370 if(quotes) outlen += 2;
371 if(!arg)
return outlen;
372 if(quotes && !io_ch(arg,
"\"", 1))
return -1;
373 if(do_buf(str->
data, str->
length, type, flags, NULL, io_ch, arg) < 0)
375 if(quotes && !io_ch(arg,
"\"", 1))
return -1;
381 static int do_indent(
char_io *io_ch,
void *arg,
int indent)
384 for(i = 0; i < indent; i++)
385 if(!io_ch(arg,
" ", 1))
return 0;
389 #define FN_WIDTH_LN 25
390 #define FN_WIDTH_SN 10
393 int indent,
unsigned long flags)
395 int i, prev = -1, orflags, cnt;
403 char *sep_dn, *sep_mv, *sep_eq;
404 int sep_dn_len, sep_mv_len, sep_eq_len;
405 if(indent < 0) indent = 0;
407 if(!do_indent(io_ch, arg, indent))
return -1;
456 for(i = 0; i < cnt; i++) {
461 if(prev == ent->
set) {
462 if(!io_ch(arg, sep_mv, sep_mv_len))
return -1;
463 outlen += sep_mv_len;
465 if(!io_ch(arg, sep_dn, sep_dn_len))
return -1;
466 outlen += sep_dn_len;
467 if(!do_indent(io_ch, arg, indent))
return -1;
493 objlen = strlen(objbuf);
494 if(!io_ch(arg, objbuf, objlen))
return -1;
496 if (!do_indent(io_ch, arg, fld_len - objlen))
return -1;
497 outlen += fld_len - objlen;
499 if(!io_ch(arg, sep_eq, sep_eq_len))
return -1;
500 outlen += objlen + sep_eq_len;
510 len = do_print_ex(io_ch, arg, flags | orflags, val);
511 if(len < 0)
return -1;
523 return do_name_ex(send_bio_chars, out, nm, indent, flags);
526 #ifndef OPENSSL_NO_FP_API
539 return do_name_ex(send_fp_chars, fp, nm, indent, flags);
545 return do_print_ex(send_bio_chars, out, flags, str);
548 #ifndef OPENSSL_NO_FP_API
551 return do_print_ex(send_fp_chars, fp, flags, str);
562 int mbflag,
type, ret;
565 if((type < 0) || (type > 30))
return -1;
566 mbflag = tag2nbyte[
type];
567 if(mbflag == -1)
return -1;
571 if(ret < 0)
return ret;