natfw/natfwunsaf_protocols/unsaf_codec/src/natfwunsafattribute.cpp
changeset 0 1bce908db942
equal deleted inserted replaced
-1:000000000000 0:1bce908db942
       
     1 /*
       
     2 * Copyright (c) 2006-2007 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:   
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 #include <es_sock.h>
       
    22 #include "natfwunsafattribute.h"
       
    23 #include "natfwunsafutils.h"
       
    24 
       
    25 // -----------------------------------------------------------------------------
       
    26 // CNATFWUNSAFAttribute::CNATFWUNSAFAttribute
       
    27 // -----------------------------------------------------------------------------
       
    28 //
       
    29 CNATFWUNSAFAttribute::CNATFWUNSAFAttribute()
       
    30     {
       
    31     }
       
    32 
       
    33 // -----------------------------------------------------------------------------
       
    34 // CNATFWUNSAFAttribute::~CNATFWUNSAFAttribute
       
    35 // -----------------------------------------------------------------------------
       
    36 //
       
    37 CNATFWUNSAFAttribute::~CNATFWUNSAFAttribute()
       
    38     {
       
    39     }
       
    40 
       
    41 // -----------------------------------------------------------------------------
       
    42 // CNATFWUNSAFAttribute::EncodeL
       
    43 // -----------------------------------------------------------------------------
       
    44 //
       
    45 HBufC8* CNATFWUNSAFAttribute::EncodeL() const
       
    46     {
       
    47     HBufC8* value = EncodeValueL();
       
    48     CleanupStack::PushL(value);
       
    49     TInt attrValLength = value->Length();
       
    50 
       
    51     //Pad non-DWORD-boundary aligned attributes with spaces if needed. Spaces
       
    52     //used instead of \0 in order to not mess up buggy C implementations.
       
    53     const TInt KGranularity = 4;
       
    54     TInt bytesInLastBlock = attrValLength % KGranularity;
       
    55     TInt bytesToAppend = KGranularity - bytesInLastBlock;
       
    56     if (0 < bytesInLastBlock && !IsWordBoundaryAligned(Type()))
       
    57         {
       
    58         CBufBase* valueBuf = CBufFlat::NewL(attrValLength + bytesToAppend);
       
    59         CleanupStack::PushL(valueBuf);
       
    60         valueBuf->InsertL(0, *value, attrValLength);
       
    61         const TChar KSpace(' ');
       
    62         for (TInt i = 0; i < bytesToAppend; ++i)
       
    63             {
       
    64             valueBuf->InsertL(valueBuf->Size(), &KSpace, 1);
       
    65             }
       
    66         // Store value pointer for proper cleanupstack handling
       
    67         HBufC8* oldValue = value;
       
    68         value = valueBuf->Ptr(0).AllocL();
       
    69         CleanupStack::PopAndDestroy(valueBuf);
       
    70         CleanupStack::PopAndDestroy( oldValue );
       
    71         CleanupStack::PushL( value );
       
    72         }
       
    73 
       
    74     HBufC8* attribute = HBufC8::NewLC(value->Length() + EValueOffset);
       
    75     TPtr8 ptr = attribute->Des();
       
    76     ptr.FillZ(EValueOffset);
       
    77 
       
    78     NATFWUNSAFUtils::WriteNetworkOrder16L(ptr, ETypeOffset, Type());
       
    79     NATFWUNSAFUtils::WriteNetworkOrder16L(ptr,
       
    80                                      ELengthOffset,
       
    81                                      static_cast<TUint16>(attrValLength));
       
    82     ptr.Append(*value);
       
    83     CleanupStack::Pop(attribute);
       
    84     CleanupStack::PopAndDestroy(value);
       
    85 
       
    86     return attribute;
       
    87     }
       
    88 
       
    89 // -----------------------------------------------------------------------------
       
    90 // CNATFWUNSAFAttribute::ParseLengthL
       
    91 // -----------------------------------------------------------------------------
       
    92 //
       
    93 TUint16 CNATFWUNSAFAttribute::ParseLengthL(const TDesC8& aByteStream)
       
    94     {
       
    95     __ASSERT_ALWAYS(aByteStream.Length() >= EValueOffset,
       
    96                     User::Leave(KErrArgument));
       
    97 
       
    98     return BigEndian::Get16(aByteStream.Mid(ELengthOffset).Ptr());
       
    99     }
       
   100 
       
   101 // -----------------------------------------------------------------------------
       
   102 // CNATFWUNSAFAttribute::ParseTypeAndLengthL
       
   103 // -----------------------------------------------------------------------------
       
   104 //
       
   105 void CNATFWUNSAFAttribute::ParseTypeAndLengthL(const TDesC8& aByteStream,
       
   106                                            TUint16& aType,
       
   107                                            TUint16& aTotalLength)
       
   108     {
       
   109     //Stream must have enough data to contain at least an attribute with
       
   110     //zero-length value
       
   111     __ASSERT_ALWAYS(aByteStream.Length() >= EValueOffset,
       
   112                     User::Leave(KErrArgument));
       
   113 
       
   114     TUint16 totalLength = ParseLengthL(aByteStream) + EValueOffset;
       
   115 
       
   116     //Increase total length if it's not a multiple of 4 bytes.
       
   117     //This is because of possible padding in text attributes not visible from
       
   118     //attribute length field.
       
   119     const TUint16 KGranularity = 4;
       
   120     TUint16 bytesInLastBlock = totalLength % KGranularity;
       
   121     TUint16 bytesPadded = KGranularity - bytesInLastBlock;
       
   122     if (0 < bytesInLastBlock )
       
   123         {
       
   124         totalLength += bytesPadded;
       
   125         }
       
   126 
       
   127     __ASSERT_ALWAYS(aByteStream.Length() >= totalLength,
       
   128                     User::Leave(KErrArgument));
       
   129 
       
   130     aTotalLength = totalLength;
       
   131     aType = BigEndian::Get16(aByteStream.Mid(ETypeOffset).Ptr());
       
   132     }
       
   133 
       
   134 // -----------------------------------------------------------------------------
       
   135 // CNATFWUNSAFAttribute::IsMandatory
       
   136 // -----------------------------------------------------------------------------
       
   137 //
       
   138 TBool CNATFWUNSAFAttribute::IsMandatory(TUint16 aType)
       
   139     {
       
   140     const TInt KLargestMandatoryValue = 0x7fff; //RFC 3489
       
   141     return aType <= KLargestMandatoryValue;
       
   142     }
       
   143 
       
   144 // -----------------------------------------------------------------------------
       
   145 // CNATFWUNSAFAttribute::IsWordBoundaryAligned
       
   146 // -----------------------------------------------------------------------------
       
   147 //
       
   148 TBool CNATFWUNSAFAttribute::IsWordBoundaryAligned(TUint16 aType)
       
   149     {
       
   150     return !(aType == EUsername ||
       
   151              aType == EPassword ||
       
   152              aType == EData     ||
       
   153              aType == EServer   ||
       
   154              aType == ENonce    ||
       
   155              aType == ERealm    ||
       
   156              aType == EErrorCode);
       
   157     }