1 /***************************************************************************
2     begin       : Sun Jun 13 2004
3     copyright   : (C) 2004-2011 by Martin Preuss
4     email       : martin@libchipcard.de
5 
6  ***************************************************************************
7  *          Please see toplevel file COPYING for license details           *
8  ***************************************************************************/
9 
10 
11 #ifndef GWENHYWFAR_TLV_H
12 #define GWENHYWFAR_TLV_H
13 
14 #include <gwenhywfar/buffer.h>
15 #include <gwenhywfar/misc.h>
16 #include <gwenhywfar/db.h>
17 
18 
19 typedef struct GWEN_TLV GWEN_TLV;
20 
21 GWEN_LIST_FUNCTION_LIB_DEFS(GWEN_TLV, GWEN_TLV, GWENHYWFAR_API)
22 
23 
24 GWENHYWFAR_API
25 GWEN_TLV *GWEN_TLV_new(void);
26 GWENHYWFAR_API
27 void GWEN_TLV_free(GWEN_TLV *tlv);
28 
29 GWENHYWFAR_API
30 GWEN_TLV *GWEN_TLV_create(unsigned int tagType,
31                           unsigned int tagMode,
32                           const void *p,
33                           unsigned int dlen,
34                           int isBerTlv);
35 
36 
37 GWENHYWFAR_API
38 GWEN_TLV *GWEN_TLV_fromBuffer(GWEN_BUFFER *mbuf, int isBerTlv);
39 
40 GWENHYWFAR_API
41 int GWEN_TLV_toBuffer(GWEN_TLV *tlv, GWEN_BUFFER *mbuf);
42 
43 GWENHYWFAR_API
44 int GWEN_TLV_DirectlyToBuffer(unsigned int tagType,
45                               unsigned int tagMode,
46                               const void *tagData,
47                               int tagLength,
48                               int isBerTlv,
49                               GWEN_BUFFER *mbuf);
50 
51 
52 GWENHYWFAR_API
53 int GWEN_TLV_IsBerTlv(const GWEN_TLV *tlv);
54 
55 GWENHYWFAR_API
56 unsigned int GWEN_TLV_GetTagType(const GWEN_TLV *tlv);
57 GWENHYWFAR_API
58 unsigned int GWEN_TLV_GetTagLength(const GWEN_TLV *tlv);
59 GWENHYWFAR_API
60 const void *GWEN_TLV_GetTagData(const GWEN_TLV *tlv);
61 
62 GWENHYWFAR_API
63 int GWEN_TLV_IsContructed(const GWEN_TLV *tlv);
64 GWENHYWFAR_API
65 unsigned int GWEN_TLV_GetClass(const GWEN_TLV *tlv);
66 GWENHYWFAR_API
67 unsigned int GWEN_TLV_GetTagSize(const GWEN_TLV *tlv);
68 
69 
70 /**
71  * Reads a TLV header from a buffer (tag and length) and returns the number of bytes
72  * used by the header.
73  * @return number of bytes used to specify tag and length (i.e. TLV size without the data part)
74  * @param tlv TLV to read the header into
75  * @param p pointer to a buffer containing at least the header
76  * @param size siez of the buffer pointed to by @b p
77  * @param isBerTlv if !=0 then the TLV is supposed to be a BER-TLV object
78  */
79 GWENHYWFAR_API
80 int GWEN_TLV_ReadHeader(GWEN_TLV *tlv, const uint8_t *p, uint32_t size, int isBerTlv);
81 
82 
83 /**
84  * Directly writes a TLV header (tag and length) to the given buffer.
85  * @return 0 if ok, error code otherwise
86  * @param tagType tag id
87  * @param tagMode bits 5, 6 and 7 of the first TLV byte (bits 6/7: class, bit 5: constructed)
88  * @param tagLength length of the data part introduced by the TLV header
89  * @param isBerTlv if !=0 then the TLV is supposed to be a BER-TLV object
90  * @param mbuf buffer to receive the TLV header
91  */
92 GWENHYWFAR_API
93 int GWEN_TLV_WriteHeader(unsigned int tagType,
94                          unsigned int tagMode,
95                          uint64_t tagLength,
96                          int isBerTlv,
97                          GWEN_BUFFER *mbuf);
98 
99 
100 /**
101  * Directly parses a BER-TLV structure do a DB.
102  * @return number of bytes parsed
103  * @param dbRecord GWEN_DB database with the parsed TLV structure
104  * @param mbuf buffer holding the TLV data
105  * @param len length of the data in the buffer
106  */
107 
108 GWENHYWFAR_API
109 int GWEN_TLV_Buffer_To_DB(GWEN_DB_NODE *dbRecord, GWEN_BUFFER *mbuf, int len);
110 
111 /**
112  * Parses length of a BER-TLV structure.
113  * @return number of bytes including the tag itself
114  * @param mbuf buffer holding the TLV data
115  * @param tag_len_len number of bytes used for tag identifier octets and length octets
116  */
117 
118 GWENHYWFAR_API
119 uint32_t GWEN_TLV_ParseLength(GWEN_BUFFER *mbuf, uint32_t *tag_len_len);
120 
121 #endif /* GWENHYWFAR_TLV_H */
122 
123