gwenhywfar  4.6.0beta
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 tagMode;
85  unsigned int tagType;
86  unsigned int tagLength;
87  const char *tagData;
88  unsigned int size;
89  unsigned int pos;
90  unsigned int j;
91  GWEN_TAG16 *tlv;
92  uint32_t startPos;
93 
94  if (!GWEN_Buffer_GetBytesLeft(mbuf)) {
95  DBG_ERROR(0, "Buffer empty");
96  return 0;
97  }
98 
99  startPos=GWEN_Buffer_GetPos(mbuf);
100 
101  tagMode=tagType=tagLength=0;
102 
104  pos=0;
105  size=GWEN_Buffer_GetBytesLeft(mbuf);
106 
107  /* get tag type */
108  if (size<2) {
109  DBG_ERROR(0, "Too few bytes for BER-TLV");
110  return 0;
111  }
112  j=(unsigned char)(p[pos]);
113  tagType=j;
114 
115  /* get length */
116  pos++;
117  if (pos+1>=size) {
118  DBG_ERROR(0, "Too few bytes");
119  return 0;
120  }
121  j=((unsigned char)(p[pos+1]))<<8;
122  j|=(unsigned char)(p[pos]);
123  pos+=2;
124  tagLength=j;
125  tagData=p+pos;
126  GWEN_Buffer_IncrementPos(mbuf, pos);
127 
128  tlv=GWEN_Tag16_new();
129  assert(tlv);
130  tlv->tagType=tagType;
131  tlv->tagLength=tagLength;
132  if (tagLength) {
133  tlv->tagData=(void*)malloc(tagLength);
134  memmove(tlv->tagData, tagData, tagLength);
135  tlv->dataOwned=1;
136  }
137 
138  GWEN_Buffer_IncrementPos(mbuf, tagLength);
139  tlv->tagSize=GWEN_Buffer_GetPos(mbuf)-startPos;
140  return tlv;
141 }
142 
143 
144 
145 GWEN_TAG16 *GWEN_Tag16_fromBuffer2(const uint8_t *p, uint32_t l, int doCopy) {
146  unsigned int tagType;
147  unsigned int tagLength;
148  const uint8_t *tagData;
149  unsigned int size;
150  unsigned int pos;
151  unsigned int j;
152  GWEN_TAG16 *tlv;
153 
154  if (l<1) {
155  DBG_ERROR(0, "Buffer empty");
156  return NULL;
157  }
158 
159  tagType=tagLength=0;
160 
161  pos=0;
162  size=l;
163 
164  /* get tag type */
165  if (size<2) {
166  DBG_ERROR(0, "Too few bytes for TLV");
167  return 0;
168  }
169  j=(unsigned char)(p[pos]);
170  tagType=j;
171 
172  /* get length */
173  pos++;
174  if (pos+1>=size) {
175  DBG_ERROR(0, "Too few bytes");
176  return 0;
177  }
178  j=((unsigned char)(p[pos+1]))<<8;
179  j|=(unsigned char)(p[pos]);
180  pos+=2;
181  tagLength=j;
182  tagData=p+pos;
183 
184  tlv=GWEN_Tag16_new();
185  assert(tlv);
186  tlv->tagType=tagType;
187  tlv->tagLength=tagLength;
188  if (tagLength) {
189  if (doCopy) {
190  tlv->tagData=(void*)malloc(tagLength);
191  memmove(tlv->tagData, tagData, tagLength);
192  tlv->dataOwned=1;
193  }
194  else {
195  tlv->tagData=(uint8_t*)tagData;
196  tlv->dataOwned=0;
197  }
198  }
199 
200  tlv->tagSize=tagLength+3;
201  return tlv;
202 }
203 
204 
205 
206 void GWEN_Tag16_DirectlyToBuffer(unsigned int tagType,
207  const char *p,
208  int size,
209  GWEN_BUFFER *buf){
210  assert(buf);
211  if (size==-1) {
212  assert(p);
213  size=strlen(p);
214  }
215 
216  GWEN_Buffer_AppendByte(buf, tagType & 0xff);
217  GWEN_Buffer_AppendByte(buf, size & 0xff);
218  GWEN_Buffer_AppendByte(buf, (size>>8)&0xff);
219  if (size) {
220  assert(p);
221  GWEN_Buffer_AppendBytes(buf, p, size);
222  }
223 
224 }
225 
226 
227 
228 
229 
230 
231 
232