tag16.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #ifdef HAVE_CONFIG_H
00015 # include <config.h>
00016 #endif
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 free(tlv->tagData);
00046 GWEN_LIST_FINI(GWEN_TAG16, tlv);
00047 GWEN_FREE_OBJECT(tlv);
00048 }
00049 }
00050
00051
00052
00053 unsigned int GWEN_Tag16_GetTagType(const GWEN_TAG16 *tlv){
00054 assert(tlv);
00055 return tlv->tagType;
00056 }
00057
00058
00059
00060 unsigned int GWEN_Tag16_GetTagLength(const GWEN_TAG16 *tlv){
00061 assert(tlv);
00062 return tlv->tagLength;
00063 }
00064
00065
00066
00067 unsigned int GWEN_Tag16_GetTagSize(const GWEN_TAG16 *tlv){
00068 assert(tlv);
00069 return tlv->tagSize;
00070 }
00071
00072
00073
00074 const void *GWEN_Tag16_GetTagData(const GWEN_TAG16 *tlv){
00075 assert(tlv);
00076 return tlv->tagData;
00077 }
00078
00079
00080
00081 GWEN_TAG16 *GWEN_Tag16_fromBuffer(GWEN_BUFFER *mbuf, int isBerTlv) {
00082 const char *p;
00083 unsigned int tagMode;
00084 unsigned int tagType;
00085 unsigned int tagLength;
00086 const char *tagData;
00087 unsigned int size;
00088 unsigned int pos;
00089 unsigned int j;
00090 GWEN_TAG16 *tlv;
00091 uint32_t startPos;
00092
00093 if (!GWEN_Buffer_GetBytesLeft(mbuf)) {
00094 DBG_ERROR(0, "Buffer empty");
00095 return 0;
00096 }
00097
00098 startPos=GWEN_Buffer_GetPos(mbuf);
00099
00100 tagMode=tagType=tagLength=0;
00101
00102 p=GWEN_Buffer_GetPosPointer(mbuf);
00103 pos=0;
00104 size=GWEN_Buffer_GetBytesLeft(mbuf);
00105
00106
00107 if (size<2) {
00108 DBG_ERROR(0, "Too few bytes for BER-TLV");
00109 return 0;
00110 }
00111 j=(unsigned char)(p[pos]);
00112 tagType=j;
00113
00114
00115 pos++;
00116 if (pos+1>=size) {
00117 DBG_ERROR(0, "Too few bytes");
00118 return 0;
00119 }
00120 j=((unsigned char)(p[pos+1]))<<8;
00121 j|=(unsigned char)(p[pos]);
00122 pos+=2;
00123 tagLength=j;
00124 tagData=p+pos;
00125 GWEN_Buffer_IncrementPos(mbuf, pos);
00126
00127 tlv=GWEN_Tag16_new();
00128 assert(tlv);
00129 tlv->tagType=tagType;
00130 tlv->tagLength=tagLength;
00131 if (tagLength) {
00132 tlv->tagData=(void*)malloc(tagLength);
00133 memmove(tlv->tagData, tagData, tagLength);
00134 }
00135
00136 GWEN_Buffer_IncrementPos(mbuf, tagLength);
00137 tlv->tagSize=GWEN_Buffer_GetPos(mbuf)-startPos;
00138 return tlv;
00139 }
00140
00141
00142
00143 void GWEN_Tag16_DirectlyToBuffer(unsigned int tagType,
00144 const char *p,
00145 int size,
00146 GWEN_BUFFER *buf){
00147 assert(buf);
00148 if (size==-1) {
00149 assert(p);
00150 size=strlen(p);
00151 }
00152
00153 GWEN_Buffer_AppendByte(buf, tagType & 0xff);
00154 GWEN_Buffer_AppendByte(buf, size & 0xff);
00155 GWEN_Buffer_AppendByte(buf, (size>>8)&0xff);
00156 if (size) {
00157 assert(p);
00158 GWEN_Buffer_AppendBytes(buf, p, size);
00159 }
00160
00161 }
00162
00163
00164
00165
00166
00167
00168
00169