JunkBox_Lib  1.10.2
asn1_node.c
Go to the documentation of this file.
1 
7 #include "asn1_node.h"
8 
9 
10 const char* _ASN1_NODE_CLASS[] =
11 {
12  "UNIVERSAL", // 0x00
13  "APPLICATION", // 0x40
14  "CONTEXT-DEFINED", // 0x80
15  "PRIVATE" // 0xc0
16 };
17 
18 
19 const char* _ASN1_NODE_CONST[] =
20 {
21  "PRIMITIVE", // 0x00
22  "CONSTRUCTED" // 0x20
23 };
24 
25 
26 const char* _ASN1_NODE_TAG[] =
27 {
28  "DATA_END", // 0x00
29  "BOOLEAN", // 0x01
30  "INTEGER", // 0x02
31  "BIT_STRING", // 0x03
32  "OCTET_STRING", // 0x04
33  "NULL", // 0x05
34  "OBJECT_IDENTIFIER", // 0x06
35  "OBJECT_DESCRITOR", // 0x07
36  "EXTERNAL", // 0x08
37  "REAL", // 0x09
38  "ENUMERATED", // 0x0a
39  "EMBEDDED PDV", // 0x0b
40  "UTF8_STRING", // 0x0c
41  "RELATIVE-OID", // 0x0d
42  "?", // 0x0e
43  "?", // 0x0f
44  "SEQUENCE", // 0x10
45  "SET", // 0x11
46  "NUMERIC_STRING", // 0x12
47  "PRINTABLE_STRING", // 0x13
48  "TELETEX_STRING", // 0x14
49  "VIDEOTEX_STRING", // 0x15
50  "IA5_STRING", // 0x16
51  "UTC_TIME", // 0x17
52  "GENERALIZED_TIME", // 0x18
53  "GRAPHIC_STRING", // 0x19
54  "VISIBLE_STRING", // 0x1a
55  "GENERAL_STRING", // 0x1b
56  "UNIVERSAL_STRING", // 0x1c
57  "CHARACTER_STRING", // 0x1d
58  "BMP_STRING", // 0x1e
59  "?", // 0x1f
60 };
61 
62 
63 void asn1_id_type(int id, int* type, int* cnst, int* tag)
64 {
65  if (cnst!=NULL) {
66  if (id & JBXL_ASN1_CNSTRCTD) *cnst = 1; // Constructed
67  else *cnst = 0;
68  }
69  if (type!=NULL) *type = (id & 0xc0) >> 6; // Class Type
70  if (tag!=NULL) *tag = id & 0x1f; // Tag
71 
72  return;
73 }
74 
75 
76 void asn1_print_id(FILE* fp, int id)
77 {
78  int cnst, type, tag;
79 
80  asn1_id_type(id, &type, &cnst, &tag);
81 
82  fprintf(fp, "[%02x] ", id); // Identifier data
83  fprintf(fp, "%s ", _ASN1_NODE_CLASS[type]); // Class
84  fprintf(fp, "%s ", _ASN1_NODE_CONST[cnst]); // Primitive/Constructed
85  if (type==0x00) fprintf(fp, "%s ", _ASN1_NODE_TAG[tag]); // Tag: if Class is UNIVERSAL
86  else fprintf(fp, "(%02x) ", tag); //
87 
88  return;
89 }
90 
91 
92 void asn1_print_tag_value(FILE* fp, int id, Buffer buf)
93 {
94  if (buf.vldsz<=0) return;
95 
96  fprintf(fp, ": ");
97 
98  int tag = id & 0x1f;
99  if (tag==JBXL_ASN1_INT) {
100  long int n = bin2int_DER(buf);
101  fprintf(fp, "INT = %ld ", n);
102  }
103  else if (tag==JBXL_ASN1_OCT) {
104  if (buf.buf!=NULL) fprintf(fp, "\"%s\" ", buf.buf);
105  else fprintf(fp, "\"\" ");
106  }
107  else if (buf.buf!=NULL) { // その他
108  fprintf(fp, "....... ");
109  }
110  return;
111 }
112 
const char * _ASN1_NODE_TAG[]
Definition: asn1_node.c:26
const char * _ASN1_NODE_CLASS[]
Definition: asn1_node.c:10
const char * _ASN1_NODE_CONST[]
Definition: asn1_node.c:19
void asn1_print_id(FILE *fp, int id)
Definition: asn1_node.c:76
void asn1_print_tag_value(FILE *fp, int id, Buffer buf)
Definition: asn1_node.c:92
void asn1_id_type(int id, int *type, int *cnst, int *tag)
Definition: asn1_node.c:63
#define JBXL_ASN1_INT
INTEGER.
Definition: asn1_node.h:23
#define JBXL_ASN1_OCT
OCTET_STRING.
Definition: asn1_node.h:25
#define JBXL_ASN1_CNSTRCTD
CONSTRUCTED 構造化フラグ
Definition: asn1_node.h:14
long int bin2int_DER(Buffer buf)
Definition: asn1_tool.c:163
unsigned char ** buf
Definition: jpeg_tool.h:96
Definition: buffer.h:35