gwenhywfar 4.0.3
|
00001 /*************************************************************************** 00002 begin : Sun Jun 13 2004 00003 copyright : (C) 2004 by Martin Preuss 00004 email : martin@libchipcard.de 00005 00006 *************************************************************************** 00007 * Please see toplevel file COPYING for license details * 00008 ***************************************************************************/ 00009 00010 00011 #ifdef HAVE_CONFIG_H 00012 # include <config.h> 00013 #endif 00014 00015 #define DISABLE_DEBUGLOG 00016 00017 00018 #include "tag16_p.h" 00019 #include <gwenhywfar/debug.h> 00020 #include <gwenhywfar/inherit.h> 00021 #include <gwenhywfar/misc.h> 00022 #include <gwenhywfar/text.h> 00023 00024 #include <stdlib.h> 00025 #include <assert.h> 00026 #include <string.h> 00027 00028 00029 GWEN_LIST_FUNCTIONS(GWEN_TAG16, GWEN_Tag16) 00030 00031 00032 GWEN_TAG16 *GWEN_Tag16_new() { 00033 GWEN_TAG16 *tlv; 00034 00035 GWEN_NEW_OBJECT(GWEN_TAG16, tlv); 00036 GWEN_LIST_INIT(GWEN_TAG16, tlv); 00037 00038 return tlv; 00039 } 00040 00041 00042 00043 void GWEN_Tag16_free(GWEN_TAG16 *tlv) { 00044 if (tlv) { 00045 if (tlv->dataOwned) 00046 free(tlv->tagData); 00047 GWEN_LIST_FINI(GWEN_TAG16, tlv); 00048 GWEN_FREE_OBJECT(tlv); 00049 } 00050 } 00051 00052 00053 00054 unsigned int GWEN_Tag16_GetTagType(const GWEN_TAG16 *tlv){ 00055 assert(tlv); 00056 return tlv->tagType; 00057 } 00058 00059 00060 00061 unsigned int GWEN_Tag16_GetTagLength(const GWEN_TAG16 *tlv){ 00062 assert(tlv); 00063 return tlv->tagLength; 00064 } 00065 00066 00067 00068 unsigned int GWEN_Tag16_GetTagSize(const GWEN_TAG16 *tlv){ 00069 assert(tlv); 00070 return tlv->tagSize; 00071 } 00072 00073 00074 00075 const void *GWEN_Tag16_GetTagData(const GWEN_TAG16 *tlv){ 00076 assert(tlv); 00077 return tlv->tagData; 00078 } 00079 00080 00081 00082 GWEN_TAG16 *GWEN_Tag16_fromBuffer(GWEN_BUFFER *mbuf, int isBerTlv) { 00083 const char *p; 00084 unsigned int tagMode; 00085 unsigned int tagType; 00086 unsigned int tagLength; 00087 const char *tagData; 00088 unsigned int size; 00089 unsigned int pos; 00090 unsigned int j; 00091 GWEN_TAG16 *tlv; 00092 uint32_t startPos; 00093 00094 if (!GWEN_Buffer_GetBytesLeft(mbuf)) { 00095 DBG_ERROR(0, "Buffer empty"); 00096 return 0; 00097 } 00098 00099 startPos=GWEN_Buffer_GetPos(mbuf); 00100 00101 tagMode=tagType=tagLength=0; 00102 00103 p=GWEN_Buffer_GetPosPointer(mbuf); 00104 pos=0; 00105 size=GWEN_Buffer_GetBytesLeft(mbuf); 00106 00107 /* get tag type */ 00108 if (size<2) { 00109 DBG_ERROR(0, "Too few bytes for BER-TLV"); 00110 return 0; 00111 } 00112 j=(unsigned char)(p[pos]); 00113 tagType=j; 00114 00115 /* get length */ 00116 pos++; 00117 if (pos+1>=size) { 00118 DBG_ERROR(0, "Too few bytes"); 00119 return 0; 00120 } 00121 j=((unsigned char)(p[pos+1]))<<8; 00122 j|=(unsigned char)(p[pos]); 00123 pos+=2; 00124 tagLength=j; 00125 tagData=p+pos; 00126 GWEN_Buffer_IncrementPos(mbuf, pos); 00127 00128 tlv=GWEN_Tag16_new(); 00129 assert(tlv); 00130 tlv->tagType=tagType; 00131 tlv->tagLength=tagLength; 00132 if (tagLength) { 00133 tlv->tagData=(void*)malloc(tagLength); 00134 memmove(tlv->tagData, tagData, tagLength); 00135 tlv->dataOwned=1; 00136 } 00137 00138 GWEN_Buffer_IncrementPos(mbuf, tagLength); 00139 tlv->tagSize=GWEN_Buffer_GetPos(mbuf)-startPos; 00140 return tlv; 00141 } 00142 00143 00144 00145 GWEN_TAG16 *GWEN_Tag16_fromBuffer2(const uint8_t *p, uint32_t l, int doCopy) { 00146 unsigned int tagType; 00147 unsigned int tagLength; 00148 const uint8_t *tagData; 00149 unsigned int size; 00150 unsigned int pos; 00151 unsigned int j; 00152 GWEN_TAG16 *tlv; 00153 00154 if (l<1) { 00155 DBG_ERROR(0, "Buffer empty"); 00156 return NULL; 00157 } 00158 00159 tagType=tagLength=0; 00160 00161 pos=0; 00162 size=l; 00163 00164 /* get tag type */ 00165 if (size<2) { 00166 DBG_ERROR(0, "Too few bytes for TLV"); 00167 return 0; 00168 } 00169 j=(unsigned char)(p[pos]); 00170 tagType=j; 00171 00172 /* get length */ 00173 pos++; 00174 if (pos+1>=size) { 00175 DBG_ERROR(0, "Too few bytes"); 00176 return 0; 00177 } 00178 j=((unsigned char)(p[pos+1]))<<8; 00179 j|=(unsigned char)(p[pos]); 00180 pos+=2; 00181 tagLength=j; 00182 tagData=p+pos; 00183 00184 tlv=GWEN_Tag16_new(); 00185 assert(tlv); 00186 tlv->tagType=tagType; 00187 tlv->tagLength=tagLength; 00188 if (tagLength) { 00189 if (doCopy) { 00190 tlv->tagData=(void*)malloc(tagLength); 00191 memmove(tlv->tagData, tagData, tagLength); 00192 tlv->dataOwned=1; 00193 } 00194 else { 00195 tlv->tagData=(uint8_t*)tagData; 00196 tlv->dataOwned=0; 00197 } 00198 } 00199 00200 tlv->tagSize=tagLength+3; 00201 return tlv; 00202 } 00203 00204 00205 00206 void GWEN_Tag16_DirectlyToBuffer(unsigned int tagType, 00207 const char *p, 00208 int size, 00209 GWEN_BUFFER *buf){ 00210 assert(buf); 00211 if (size==-1) { 00212 assert(p); 00213 size=strlen(p); 00214 } 00215 00216 GWEN_Buffer_AppendByte(buf, tagType & 0xff); 00217 GWEN_Buffer_AppendByte(buf, size & 0xff); 00218 GWEN_Buffer_AppendByte(buf, (size>>8)&0xff); 00219 if (size) { 00220 assert(p); 00221 GWEN_Buffer_AppendBytes(buf, p, size); 00222 } 00223 00224 } 00225 00226 00227 00228 00229 00230 00231 00232