/ libder / ders.c
ders.c
 1  #include <stdio.h>
 2  #include "ders.h"
 3  #include "der-data.h"
 4  
 5  static const char *universal_names[] = {
 6    "EOC",                        /* 0 */
 7    "BOOLEAN",                    /* 1 */
 8    "INTEGER",                    /* 2 */
 9    "BIT_STRING",                 /* 3 */
10    "OCTET_STRING",               /* 4 */
11    "NULL",                       /* 5 */
12    "OBJECT",                     /* 6 */
13    "OBJECT_DESCRIPTOR",          /* 7 */
14    "EXTERNAL",                   /* 8 */
15    "REAL",                       /* 9 */
16    "ENUMERATED",                 /* 10 */
17    "",                           /* 11? */
18    "UTF8STRING",                 /* 12 */
19    "",                           /* 13? */
20    "",                           /* 14? */
21    "",                           /* 15? */
22    "SEQUENCE",                   /* 16 */
23    "SET",                        /* 17 */
24    "NUMERICSTRING",              /* 18 */
25    "PRINTABLESTRING",            /* 19 */
26    "T61STRING",                  /* 20 */
27    "VIDEOTEXSTRING",             /* 21 */
28    "IA5STRING",                  /* 22 */
29    "UTCTIME",                    /* 23 */
30    "GENERALIZEDTIME",            /* 24 */
31    "GRAPHICSTRING",              /* 25 */
32    "ISO64STRING",                /* 26 */
33    "GENERALSTRING",              /* 27 */
34    "UNIVERSALSTRING",            /* 28 */
35    "",                           /* 29? */
36    "BMPSTRING",                  /* 30 */
37    "DATE",                       /* 31 */
38    "TIME_OF_DAY",                /* 32 */
39    "DATE_TIME",                  /* 33 */
40    "DURATION",                   /* 34 */
41  };
42  
43  int ders(unsigned int der_class, _Bool constructed, unsigned int der_tag,
44           char buf[], size_t bufsize)
45  {
46    int ret = -1;
47    switch (der_class) {
48    case DERC_UNIVERSAL:
49      ret = snprintf(buf, bufsize, "%s", universal_names[der_tag]);
50      break;
51    case DERC_APPLICATION:
52      ret = snprintf(buf, bufsize, "[APP:%u]", der_tag);
53      break;
54    case DERC_CONTEXT:
55      ret = snprintf(buf, bufsize, "[%u]", der_tag);
56      break;
57    case DERC_PRIVATE:
58      ret = snprintf(buf, bufsize, "[PRIV:%u]", der_tag);
59      break;
60    }
61    return ret;
62  }