gwenhywfar 5.10.1
tag16.c
Go to the documentation of this file.
1/***************************************************************************
2 begin : Sun Jun 13 2004
3 copyright : (C) 2004 by Martin Preuss
4 email : martin@libchipcard.de
5
6 ***************************************************************************
7 * Please see toplevel file COPYING for license details *
8 ***************************************************************************/
9
10
11#ifdef HAVE_CONFIG_H
12# include <config.h>
13#endif
14
15#define DISABLE_DEBUGLOG
16
17
18#include "tag16_p.h"
19#include <gwenhywfar/debug.h>
20#include <gwenhywfar/inherit.h>
21#include <gwenhywfar/misc.h>
22#include <gwenhywfar/text.h>
23
24#include <stdlib.h>
25#include <assert.h>
26#include <string.h>
27
28
30
31
33{
34 GWEN_TAG16 *tlv;
35
38
39 return tlv;
40}
41
42
43
45{
46 if (tlv) {
47 if (tlv->dataOwned)
48 free(tlv->tagData);
51 }
52}
53
54
55
56unsigned int GWEN_Tag16_GetTagType(const GWEN_TAG16 *tlv)
57{
58 assert(tlv);
59 return tlv->tagType;
60}
61
62
63
64unsigned int GWEN_Tag16_GetTagLength(const GWEN_TAG16 *tlv)
65{
66 assert(tlv);
67 return tlv->tagLength;
68}
69
70
71
72unsigned int GWEN_Tag16_GetTagSize(const GWEN_TAG16 *tlv)
73{
74 assert(tlv);
75 return tlv->tagSize;
76}
77
78
79
80const void *GWEN_Tag16_GetTagData(const GWEN_TAG16 *tlv)
81{
82 assert(tlv);
83 return tlv->tagData;
84}
85
86
87
89{
90 const char *p;
91 unsigned int tagType;
92 unsigned int tagLength;
93 const char *tagData;
94 unsigned int size;
95 unsigned int pos;
96 unsigned int j;
97 GWEN_TAG16 *tlv;
98 uint32_t startPos;
99
100 if (!GWEN_Buffer_GetBytesLeft(mbuf)) {
101 DBG_ERROR(0, "Buffer empty");
102 return 0;
103 }
104
105 startPos=GWEN_Buffer_GetPos(mbuf);
106
107 tagType=tagLength=0;
108
110 pos=0;
111 size=GWEN_Buffer_GetBytesLeft(mbuf);
112
113 /* get tag type */
114 if (size<2) {
115 DBG_ERROR(0, "Too few bytes for BER-TLV");
116 return 0;
117 }
118 j=(unsigned char)(p[pos]);
119 tagType=j;
120
121 /* get length */
122 pos++;
123 if (pos+1>=size) {
124 DBG_ERROR(0, "Too few bytes");
125 return 0;
126 }
127 j=((unsigned char)(p[pos+1]))<<8;
128 j|=(unsigned char)(p[pos]);
129 pos+=2;
130 tagLength=j;
131 tagData=p+pos;
132 GWEN_Buffer_IncrementPos(mbuf, pos);
133
134 tlv=GWEN_Tag16_new();
135 assert(tlv);
136 tlv->tagType=tagType;
137 tlv->tagLength=tagLength;
138 if (tagLength) {
139 tlv->tagData=(void *)malloc(tagLength);
140 memmove(tlv->tagData, tagData, tagLength);
141 tlv->dataOwned=1;
142 }
143
144 GWEN_Buffer_IncrementPos(mbuf, tagLength);
145 tlv->tagSize=GWEN_Buffer_GetPos(mbuf)-startPos;
146 return tlv;
147}
148
149
150
151GWEN_TAG16 *GWEN_Tag16_fromBuffer2(const uint8_t *p, uint32_t l, int doCopy)
152{
153 unsigned int tagType;
154 unsigned int tagLength;
155 const uint8_t *tagData;
156 unsigned int size;
157 unsigned int pos;
158 unsigned int j;
159 GWEN_TAG16 *tlv;
160
161 if (l<1) {
162 DBG_ERROR(0, "Buffer empty");
163 return NULL;
164 }
165
166 tagType=tagLength=0;
167
168 pos=0;
169 size=l;
170
171 /* get tag type */
172 if (size<2) {
173 DBG_ERROR(0, "Too few bytes for TLV");
174 return 0;
175 }
176 j=(unsigned char)(p[pos]);
177 tagType=j;
178
179 /* get length */
180 pos++;
181 if (pos+1>=size) {
182 DBG_ERROR(0, "Too few bytes");
183 return 0;
184 }
185 j=((unsigned char)(p[pos+1]))<<8;
186 j|=(unsigned char)(p[pos]);
187 pos+=2;
188 tagLength=j;
189 tagData=p+pos;
190
191 tlv=GWEN_Tag16_new();
192 assert(tlv);
193 tlv->tagType=tagType;
194 tlv->tagLength=tagLength;
195 if (tagLength) {
196 if (doCopy) {
197 tlv->tagData=(void *)malloc(tagLength);
198 memmove(tlv->tagData, tagData, tagLength);
199 tlv->dataOwned=1;
200 }
201 else {
202 tlv->tagData=(uint8_t *)tagData;
203 tlv->dataOwned=0;
204 }
205 }
206
207 tlv->tagSize=tagLength+3;
208 return tlv;
209}
210
211
212
213void GWEN_Tag16_DirectlyToBuffer(unsigned int tagType,
214 const char *p,
215 int size,
216 GWEN_BUFFER *buf)
217{
218 assert(buf);
219 if (size==-1) {
220 assert(p);
221 size=strlen(p);
222 }
223
224 GWEN_Buffer_AppendByte(buf, tagType & 0xff);
225 GWEN_Buffer_AppendByte(buf, size & 0xff);
226 GWEN_Buffer_AppendByte(buf, (size>>8)&0xff);
227 if (size) {
228 assert(p);
229 GWEN_Buffer_AppendBytes(buf, p, size);
230 }
231
232}
233
234
235
236
237
238
239
240
#define NULL
Definition: binreloc.c:300
int GWEN_Buffer_IncrementPos(GWEN_BUFFER *bf, uint32_t i)
Definition: buffer.c:452
uint32_t GWEN_Buffer_GetBytesLeft(GWEN_BUFFER *bf)
Definition: buffer.c:537
char * GWEN_Buffer_GetPosPointer(const GWEN_BUFFER *bf)
Definition: buffer.c:549
int GWEN_Buffer_AppendBytes(GWEN_BUFFER *bf, const char *buffer, uint32_t size)
Definition: buffer.c:361
uint32_t GWEN_Buffer_GetPos(const GWEN_BUFFER *bf)
Definition: buffer.c:253
int GWEN_Buffer_AppendByte(GWEN_BUFFER *bf, char c)
Definition: buffer.c:394
#define DBG_ERROR(dbg_logger, format, args...)
Definition: debug.h:97
struct GWEN_BUFFER GWEN_BUFFER
A dynamically resizeable text buffer.
Definition: buffer.h:38
#define GWEN_UNUSED
#define GWEN_LIST_FINI(t, element)
Definition: list1.h:474
#define GWEN_LIST_FUNCTIONS(t, pr)
Definition: list1.h:366
#define GWEN_LIST_INIT(t, element)
Definition: list1.h:465
#define GWEN_FREE_OBJECT(varname)
Definition: memory.h:61
#define GWEN_NEW_OBJECT(typ, varname)
Definition: memory.h:55
GWEN_TAG16 * GWEN_Tag16_new(void)
Definition: tag16.c:32
void GWEN_Tag16_DirectlyToBuffer(unsigned int tagType, const char *p, int size, GWEN_BUFFER *buf)
Definition: tag16.c:213
GWEN_TAG16 * GWEN_Tag16_fromBuffer(GWEN_BUFFER *mbuf, GWEN_UNUSED int isBerTlv)
Definition: tag16.c:88
GWEN_TAG16 * GWEN_Tag16_fromBuffer2(const uint8_t *p, uint32_t l, int doCopy)
Definition: tag16.c:151
const void * GWEN_Tag16_GetTagData(const GWEN_TAG16 *tlv)
Definition: tag16.c:80
unsigned int GWEN_Tag16_GetTagType(const GWEN_TAG16 *tlv)
Definition: tag16.c:56
unsigned int GWEN_Tag16_GetTagLength(const GWEN_TAG16 *tlv)
Definition: tag16.c:64
void GWEN_Tag16_free(GWEN_TAG16 *tlv)
Definition: tag16.c:44
unsigned int GWEN_Tag16_GetTagSize(const GWEN_TAG16 *tlv)
Definition: tag16.c:72
struct GWEN_TAG16 GWEN_TAG16
Definition: tag16.h:18