File: TLVBuffer.cpp

package info (click to toggle)
beid 3.5.2.dfsg-10
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 147,240 kB
  • ctags: 34,507
  • sloc: cpp: 149,944; ansic: 41,577; java: 8,927; cs: 6,528; sh: 2,426; perl: 1,866; xml: 805; python: 463; makefile: 263; lex: 92
file content (466 lines) | stat: -rw-r--r-- 14,066 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/* ****************************************************************************

 * eID Middleware Project.
 * Copyright (C) 2008-2009 FedICT.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version
 * 3.0 as published by the Free Software Foundation.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, see
 * http://www.gnu.org/licenses/.

**************************************************************************** */
// TLVBuffer.cpp: implementation of the CTLVBuffer class.
//
//////////////////////////////////////////////////////////////////////

#include "TLVBuffer.h"

namespace eIDMW
{

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

char CTLVBuffer::hexChars[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

CTLVBuffer::CTLVBuffer()
{

}


CTLVBuffer::~CTLVBuffer()
{
    ITMap it;
    //free complete m_oMapTLV
    for (it = m_oMapTLV.begin(); it != m_oMapTLV.end(); ++it)
    {
        CTLV *pTemp = (*it).second;
        if(pTemp != NULL)
        {
            delete pTemp;
        }
    }
    m_oMapTLV.clear();
}

/** Unstream a multi-TLV-block into m_oMapTLV structure.
  User standard TLV syntax; extra length bytes when the MSB of a length-byte is set to 1.
  Each block should start with a 0-Tag, 0-tag can only be used as first TLV of a block.

 returns: false = invalid data
          true  = OK all done
*/
bool CTLVBuffer::ParseFileTLV(const unsigned char *pucData, unsigned long ulLen)
{
    bool bRet = false;
    
    if(pucData != NULL && ulLen > 0)
    {
        m_oMapTLV.clear();
        bRet = true;
        unsigned long ulIndex = 0;
        while ((ulIndex+1) < ulLen)      //at least 2 bytes; tag & length
        {
            //--- get & check TAG
            unsigned char ucTag      = pucData[ulIndex++];
            unsigned long ulFieldLen = 0;
            
            //the complete block should start with tag '0' and tag '0' may only be used as first TLV
            if((ucTag == 0x00 && ulIndex > 2) || (ucTag != 0x00 && ulIndex < 2))
            {
                bRet = false;
                break;
            }

            //--- get Length
//            int iNrBytes = ulLen - ulIndex - 1;     //calculate remaining bytes
            int iNrBytes = ulLen - ulIndex;     //calculate remaining bytes
            if (!TlvDecodeLen(pucData + ulIndex, &iNrBytes,  &ulFieldLen))            
            {
                bRet = false;
                break;
            }
            ulIndex += iNrBytes;

            if (((ulIndex == ulLen) && (ulFieldLen != 0)) || (ulIndex > ulLen))  //no data-bytes allowed when len=0           
            {
                bRet = false;
                break;
            }


            //--- add the decoded TLV 
            m_oMapTLV[ucTag] = new CTLV(ucTag, pucData + ulIndex, ulFieldLen);
            ulIndex += ulFieldLen;
        }
    }
    return bRet;
}

/** Unstream a multi-TLV-block into m_oMapTLV structure
 Use Fedict TLV syntax; extra length bytes when length-byte = FF
 0-tags can only be used as first TLV of a block
 returns: 0 = invalid data
          1 = OK all done

 */
int CTLVBuffer::ParseTLV(const unsigned char *pucData, unsigned long ulLen)
{
    int iRet = 0;
    
    if(pucData != NULL && ulLen > 0)
    {
        m_oMapTLV.clear();
        iRet = 1;
        unsigned long ulIndex = 0;
        while ((ulIndex+1) < ulLen)      //at least 2 bytes; tag & length
        {
            //--- get & check tag
            unsigned char ucTag = pucData[ulIndex++];

            // tag '0' may only be used as first TLV
            if(ucTag == 0x00 && ulIndex > 2)
            {
                iRet = 0;
                break;
            }

            //--- get Length
            unsigned long ulFieldLen = pucData[ulIndex];

            while(0xFF == pucData[ulIndex]) //add extra length-bytes
            {
                ulFieldLen += pucData[++ulIndex];
                if ((ulIndex+1) >= ulLen)
                {
                    iRet = 0;
                    break;
                }
            }
            ++ulIndex;

            //check if enough data available
            if ((ulIndex+ulFieldLen) > ulLen)
            {
                iRet = 0;
                break;
            }
            //get data
            m_oMapTLV[ucTag] = new CTLV(ucTag, pucData + ulIndex, ulFieldLen);
            ulIndex += ulFieldLen;
        }
    }
    return iRet;
}

CTLV *CTLVBuffer::GetTagData(unsigned char ucTag)
{
    ITMap it = m_oMapTLV.find(ucTag);
    if(it != m_oMapTLV.end())
    {
        return(*it).second;
    }	
    return NULL;
}

void CTLVBuffer::FillASCIIData(unsigned char ucTag, char *pData, unsigned long *pulLen)
{
    CTLV *pTagData = NULL;
    unsigned long ulLength;

    if((NULL != (pTagData = GetTagData(ucTag))) && (pData != NULL) && (pulLen != NULL) && (*pulLen >= (ulLength = pTagData->GetLength())))
    {
        memcpy(pData, pTagData->GetData(), ulLength);
        *pulLen = ulLength;
    }
}

void CTLVBuffer::FillUTF8Data(unsigned char ucTag, char *pData, unsigned long *pulLen)
{
    CTLV *pTagData = NULL;
    unsigned long ulLength;

    if((NULL != (pTagData = GetTagData(ucTag)))&& (pData != NULL) && (pulLen != NULL) && (*pulLen >= (ulLength = pTagData->GetLength())))
    {
        memcpy(pData, pTagData->GetData(), ulLength);
        *pulLen = ulLength;
    }
}

void CTLVBuffer::FillBinaryStringData(unsigned char ucTag, char *pData, unsigned long *pulLen)
{
    CTLV *pTagData = NULL;
    unsigned long ulLength;

    if((NULL != (pTagData = GetTagData(ucTag)))&& (pData != NULL) && (pulLen != NULL))
    {
        char *pszTemp = Hexify(pTagData->GetData(), pTagData->GetLength());
        if (*pulLen > (ulLength = (unsigned long)strlen(pszTemp)))
        {
            memcpy(pData, pszTemp,ulLength+1 );     //copy also terminating 0-char
            *pulLen = ulLength;                     //length without 0-char
        }else
            *pulLen = 0;
        delete [] pszTemp;
    }
}

void CTLVBuffer::FillLongData(unsigned char ucTag, long *piData)
{
    CTLV *pTagData = NULL;
    if(NULL != (pTagData = GetTagData(ucTag)))
    {
        char *pszTemp = new char[pTagData->GetLength() + 1];
        memset(pszTemp, 0, pTagData->GetLength() + 1);
        memcpy(pszTemp, pTagData->GetData(), pTagData->GetLength());
        *piData = atol(pszTemp);
        delete []pszTemp;
    }
}

void CTLVBuffer::FillBinaryData(unsigned char ucTag, 
                                unsigned char *pData, 
                                unsigned long *pulLen       /**< in/out: In=max.length, Out=current length */
                                )
{
    CTLV *pTagData = NULL;

    if(NULL != (pTagData = GetTagData(ucTag)))
    {
        unsigned long ulLength = pTagData->GetLength();

        if(*pulLen >= ulLength)      //length data
            memcpy(pData, pTagData->GetData(), ulLength);
        else
            ulLength = 0;
        *pulLen = ulLength;
    }
}

bool CTLVBuffer::FillBinaryDataCheck(unsigned char ucTag, unsigned char *pData, unsigned long *pulLen)
{
   bool    bRet        = false;
    CTLV *  pTagData    = NULL;

    if(NULL != (pTagData = GetTagData(ucTag)))
    {
      if (*pulLen >= pTagData->GetLength())
      {
         *pulLen = pTagData->GetLength();
         bRet = true;
      }
        memcpy(pData, pTagData->GetData(), *pulLen);
    }
   else
      *pulLen = 0;
   return bRet;
}

//binair to ascii-hex
char *CTLVBuffer::Hexify(unsigned char * pData, unsigned long ulLen) 
{
    char *pszHex = new char[ulLen*2 + 1];
    //memset(pszHex, 0, ulLen*2 + 1);
    if(pData != NULL)
    {
        int j = 0;
        for(unsigned long i = 0; i < ulLen; i++) 
        {
            pszHex[j++] = hexChars[pData[i]>>4 & 0x0F];
            pszHex[j++] = hexChars[pData[i] & 0x0F];
         }
         pszHex[j] = 0;
    }
    return pszHex;
}

unsigned long CTLVBuffer::GetLengthNeeded()
{
    unsigned long   lRet            = 0;
    ITMap           it;
   unsigned char   ucLenBuffer[5];
   int             iBufLen;

    for (it = m_oMapTLV.begin(); it != m_oMapTLV.end(); ++it)
    {
        CTLV *pTemp = (*it).second;
        if(pTemp != NULL)
        {
         memset(ucLenBuffer, 0, sizeof(ucLenBuffer));
         iBufLen=sizeof(ucLenBuffer);
         if(TlvEncodeLen(pTemp->GetLength(), ucLenBuffer, &iBufLen))
         {
            lRet ++;					//Tag
            lRet +=iBufLen;				//Length
            lRet += pTemp->GetLength();	//Data
         }
        }
    }
    return lRet;
}

/**********************************************************************************************************
Encode the length in the TLV format    

    Default one length byte.
    The highest bit = 1 means one additional length-byte.
    The lowest 7 bits of each byte are 7 bits of the length-number.

    1 length byte  will encode max 7-bit  numbers, between     00 -       7F  
    2 length bytes will encode max 14-bit numbers, between     80 -     3FFF
    3 length bytes will encode max 21-bit numbers, between    400 -  1F FFFF
    4 length bytes will encode max 28-bit numbers, between 20 000 - FFF FFFF
    
examples:
  len=7B        -> encoding =          7B    
  len=CD        -> encoding =       81 4D
  len=2039      -> encoding =       C0 39 
  len=43ABDA    -> encoding = 82 8E D7 5A
    
 **********************************************************************************************************/
bool CTLVBuffer::TlvEncodeLen(unsigned long ulLenVal,  //In: length-value to encode in the TLV-format
                    unsigned char *pucBufDest,  //In/Out: Destination buffer with the encoded length-stream
                    int *piBufLen  //In/out: Max buffer length, Out:length of encoded stream
                    )
{
    int iStreamLen = 1;         //length of the stream
    unsigned long  ulLenValLoc = 0;        //length
    unsigned char ucExtraByte = 0x00;

    //--- parameter check   
    if (pucBufDest == NULL || piBufLen == NULL)
    {
        return false;
    }

    //--- determinate number of length-bytes
    ulLenValLoc = ulLenVal>>7;

    while(ulLenValLoc)
    {
        iStreamLen++;
        ulLenValLoc >>= 7;
    }

    if (*piBufLen < iStreamLen)
    {
        return false;
    }
    *piBufLen = iStreamLen;

    //--- write encoded length
    ulLenValLoc = ulLenVal;
    pucBufDest[0] = 0;
    ucExtraByte = 0;
    while(iStreamLen--)
    {
        pucBufDest[iStreamLen] = ((unsigned char)ulLenValLoc & 0x7F) + ucExtraByte;
        ucExtraByte = 0x80;
        ulLenValLoc >>= 7;
    }

    return true;
}

/**********************************************************************************************************
Decode the length from the TLV format    
   
 **********************************************************************************************************/
bool CTLVBuffer::TlvDecodeLen(
        const unsigned char *   pucBufSrc, //In: Source buffer with the encoded length-stream
        int *                   piBufLen,  //In/Out: Maximum length of encoded stream, Out:number of length-bytes
        unsigned long *         pulLenVal  //Out: length-value from the TLV stream
                    )
{
    int  iStreamLenMax = 0; //length of the stream

    //--- parameter check   
    if (pucBufSrc == NULL || piBufLen == NULL || (*piBufLen == 0) || pulLenVal == NULL)
    {
        return false;
    }

    //--- decode first byte   
    iStreamLenMax = *piBufLen;
    *piBufLen     = 1;
    *pulLenVal    = 0x7F & *pucBufSrc;

    //--- handle additional length bytes   
    while( *(pucBufSrc++) & 0x80)
    {
        if (++(*piBufLen) > iStreamLenMax)
        {
            return false;
        }

        *pulLenVal = (*pulLenVal<<7) + (0x7F & *pucBufSrc);
    }
    return true;
}

void CTLVBuffer::SetTagData(unsigned char ucTag, const unsigned char *pucData, unsigned long ulLen)
{

   if(pucData)
   {
      ITMap itr;

      itr = m_oMapTLV.find(ucTag);
      if(itr==m_oMapTLV.end())
      {
         m_oMapTLV[ucTag] = new CTLV(ucTag, pucData, ulLen);
      }
      else
      {
         itr->second->ReplaceData(pucData, ulLen);
      }
   }
}

unsigned long CTLVBuffer::Extract(unsigned char *pucData, unsigned long ulLen)
{

   if(!pucData)
      return 0;

   CByteArray      oTempArray;
   unsigned char   ucLenBuffer[4]  = {0};
   int             iBufLen         = sizeof(ucLenBuffer);
   ITMap           it;

    for (it = m_oMapTLV.begin(); it != m_oMapTLV.end(); ++it)
    {

      iBufLen=sizeof(ucLenBuffer);
      memset(ucLenBuffer, 0, sizeof(ucLenBuffer));

      if(TlvEncodeLen(it->second->GetLength(), ucLenBuffer, &iBufLen))
      {
         oTempArray.Append(it->second->GetTag());
         oTempArray.Append(ucLenBuffer, iBufLen);         
         oTempArray.Append(it->second->GetData(), it->second->GetLength());         
      }
   }

    unsigned long ulSize = oTempArray.Size();
    if(ulLen >= ulSize)
    {
        memcpy(pucData, oTempArray.GetBytes(), ulSize);
      return ulSize;
   }

   return 0;
}


}  // namespace eIDMW