gwenhywfar  4.99.8beta
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  GWEN_TAG16 *tlv;
34 
37 
38  return tlv;
39 }
40 
41 
42 
44  if (tlv) {
45  if (tlv->dataOwned)
46  free(tlv->tagData);
48  GWEN_FREE_OBJECT(tlv);
49  }
50 }
51 
52 
53 
54 unsigned int GWEN_Tag16_GetTagType(const GWEN_TAG16 *tlv) {
55  assert(tlv);
56  return tlv->tagType;
57 }
58 
59 
60 
61 unsigned int GWEN_Tag16_GetTagLength(const GWEN_TAG16 *tlv) {
62  assert(tlv);
63  return tlv->tagLength;
64 }
65 
66 
67 
68 unsigned int GWEN_Tag16_GetTagSize(const GWEN_TAG16 *tlv) {
69  assert(tlv);
70  return tlv->tagSize;
71 }
72 
73 
74 
75 const void *GWEN_Tag16_GetTagData(const GWEN_TAG16 *tlv) {
76  assert(tlv);
77  return tlv->tagData;
78 }
79 
80 
81 
83  const char *p;
84  unsigned int tagType;
85  unsigned int tagLength;
86  const char *tagData;
87  unsigned int size;
88  unsigned int pos;
89  unsigned int j;
90  GWEN_TAG16 *tlv;
91  uint32_t startPos;
92 
93  if (!GWEN_Buffer_GetBytesLeft(mbuf)) {
94  DBG_ERROR(0, "Buffer empty");
95  return 0;
96  }
97 
98  startPos=GWEN_Buffer_GetPos(mbuf);
99 
100  tagType=tagLength=0;
101 
103  pos=0;
104  size=GWEN_Buffer_GetBytesLeft(mbuf);
105 
106  /* get tag type */
107  if (size<2) {
108  DBG_ERROR(0, "Too few bytes for BER-TLV");
109  return 0;
110  }
111  j=(unsigned char)(p[pos]);
112  tagType=j;
113 
114  /* get length */
115  pos++;
116  if (pos+1>=size) {
117  DBG_ERROR(0, "Too few bytes");
118  return 0;
119  }
120  j=((unsigned char)(p[pos+1]))<<8;
121  j|=(unsigned char)(p[pos]);
122  pos+=2;
123  tagLength=j;
124  tagData=p+pos;
125  GWEN_Buffer_IncrementPos(mbuf, pos);
126 
127  tlv=GWEN_Tag16_new();
128  assert(tlv);
129  tlv->tagType=tagType;
130  tlv->tagLength=tagLength;
131  if (tagLength) {
132  tlv->tagData=(void*)malloc(tagLength);
133  memmove(tlv->tagData, tagData, tagLength);
134  tlv->dataOwned=1;
135  }
136 
137  GWEN_Buffer_IncrementPos(mbuf, tagLength);
138  tlv->tagSize=GWEN_Buffer_GetPos(mbuf)-startPos;
139  return tlv;
140 }
141 
142 
143 
144 GWEN_TAG16 *GWEN_Tag16_fromBuffer2(const uint8_t *p, uint32_t l, int doCopy) {
145  unsigned int tagType;
146  unsigned int tagLength;
147  const uint8_t *tagData;
148  unsigned int size;
149  unsigned int pos;
150  unsigned int j;
151  GWEN_TAG16 *tlv;
152 
153  if (l<1) {
154  DBG_ERROR(0, "Buffer empty");
155  return NULL;
156  }
157 
158  tagType=tagLength=0;
159 
160  pos=0;
161  size=l;
162 
163  /* get tag type */
164  if (size<2) {
165  DBG_ERROR(0, "Too few bytes for TLV");
166  return 0;
167  }
168  j=(unsigned char)(p[pos]);
169  tagType=j;
170 
171  /* get length */
172  pos++;
173  if (pos+1>=size) {
174  DBG_ERROR(0, "Too few bytes");
175  return 0;
176  }
177  j=((unsigned char)(p[pos+1]))<<8;
178  j|=(unsigned char)(p[pos]);
179  pos+=2;
180  tagLength=j;
181  tagData=p+pos;
182 
183  tlv=GWEN_Tag16_new();
184  assert(tlv);
185  tlv->tagType=tagType;
186  tlv->tagLength=tagLength;
187  if (tagLength) {
188  if (doCopy) {
189  tlv->tagData=(void*)malloc(tagLength);
190  memmove(tlv->tagData, tagData, tagLength);
191  tlv->dataOwned=1;
192  }
193  else {
194  tlv->tagData=(uint8_t*)tagData;
195  tlv->dataOwned=0;
196  }
197  }
198 
199  tlv->tagSize=tagLength+3;
200  return tlv;
201 }
202 
203 
204 
205 void GWEN_Tag16_DirectlyToBuffer(unsigned int tagType,
206  const char *p,
207  int size,
208  GWEN_BUFFER *buf) {
209  assert(buf);
210  if (size==-1) {
211  assert(p);
212  size=strlen(p);
213  }
214 
215  GWEN_Buffer_AppendByte(buf, tagType & 0xff);
216  GWEN_Buffer_AppendByte(buf, size & 0xff);
217  GWEN_Buffer_AppendByte(buf, (size>>8)&0xff);
218  if (size) {
219  assert(p);
220  GWEN_Buffer_AppendBytes(buf, p, size);
221  }
222 
223 }
224 
225 
226 
227 
228 
229 
230 
231 
uint32_t GWEN_Buffer_GetBytesLeft(GWEN_BUFFER *bf)
Definition: buffer.c:577
GWEN_TAG16 * GWEN_Tag16_fromBuffer(GWEN_BUFFER *mbuf, int isBerTlv)
Definition: tag16.c:82
#define GWEN_FREE_OBJECT(varname)
Definition: memory.h:92
#define NULL
Definition: binreloc.c:290
unsigned int GWEN_Tag16_GetTagSize(const GWEN_TAG16 *tlv)
Definition: tag16.c:68
uint32_t GWEN_Buffer_GetPos(const GWEN_BUFFER *bf)
Definition: buffer.c:239
struct GWEN_TAG16 GWEN_TAG16
Definition: tag16.h:21
char * GWEN_Buffer_GetPosPointer(const GWEN_BUFFER *bf)
Definition: buffer.c:588
void GWEN_Tag16_DirectlyToBuffer(unsigned int tagType, const char *p, int size, GWEN_BUFFER *buf)
Definition: tag16.c:205
int GWEN_Buffer_IncrementPos(GWEN_BUFFER *bf, uint32_t i)
Definition: buffer.c:495
const void * GWEN_Tag16_GetTagData(const GWEN_TAG16 *tlv)
Definition: tag16.c:75
#define GWEN_NEW_OBJECT(typ, varname)
Definition: memory.h:86
GWEN_TAG16 * GWEN_Tag16_new(void)
Definition: tag16.c:32
int GWEN_Buffer_AppendByte(GWEN_BUFFER *bf, char c)
Definition: buffer.c:380
struct GWEN_BUFFER GWEN_BUFFER
A dynamically resizeable text buffer.
Definition: buffer.h:41
unsigned int GWEN_Tag16_GetTagLength(const GWEN_TAG16 *tlv)
Definition: tag16.c:61
unsigned int GWEN_Tag16_GetTagType(const GWEN_TAG16 *tlv)
Definition: tag16.c:54
#define DBG_ERROR(dbg_logger, format, args...)
Definition: debug.h:97
#define GWEN_LIST_INIT(t, element)
Definition: list1.h:465
int GWEN_Buffer_AppendBytes(GWEN_BUFFER *bf, const char *buffer, uint32_t size)
Definition: buffer.c:348
#define GWEN_LIST_FUNCTIONS(t, pr)
Definition: list1.h:366
void GWEN_Tag16_free(GWEN_TAG16 *tlv)
Definition: tag16.c:43
#define GWEN_LIST_FINI(t, element)
Definition: list1.h:474
GWEN_TAG16 * GWEN_Tag16_fromBuffer2(const uint8_t *p, uint32_t l, int doCopy)
Definition: tag16.c:144