ncdengine/engine/src/catalogsclientserverallocutils.cpp
changeset 0 ba25891c3a9e
equal deleted inserted replaced
-1:000000000000 0:ba25891c3a9e
       
     1 /*
       
     2 * Copyright (c) 2006 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 #include "catalogsclientserverallocutils.h"
       
    20 
       
    21 #include "catalogsserverdefines.h"
       
    22 
       
    23 #include "catalogsutils.h"
       
    24 #include "catalogsdebug.h"
       
    25 
       
    26 
       
    27 TInt AllocReturnBuf( TInt aExpectedLength, HBufC8*& aAllocatedBuf )
       
    28     {        
       
    29     TInt allocationLength = aExpectedLength;
       
    30 
       
    31     // Minimum length required for the message to enable internal
       
    32     // messaging in the case of too small descriptor
       
    33     if ( allocationLength < KCatalogsMinimumAllocLength )
       
    34         {
       
    35         allocationLength = KCatalogsMinimumAllocLength;
       
    36         }
       
    37     
       
    38     HBufC8* tempReturnBuf( HBufC8::New( allocationLength ) );
       
    39     if ( !tempReturnBuf )
       
    40         {
       
    41         return KErrNoMemory;        
       
    42         }
       
    43     aAllocatedBuf = tempReturnBuf;
       
    44     return KErrNone;
       
    45     }
       
    46 
       
    47 TInt AllocReturnBuf( TInt aExpectedLength, HBufC16*& aAllocatedBuf )
       
    48     {    
       
    49     TInt allocationLength = aExpectedLength;
       
    50 
       
    51     // Minimum length required for the message to enable internal
       
    52     // messaging in the case of too small descriptor
       
    53     if ( allocationLength < KCatalogsMinimumAllocLength )
       
    54         {
       
    55         allocationLength = KCatalogsMinimumAllocLength;
       
    56         }
       
    57     
       
    58     HBufC16* tempReturnBuf( HBufC16::New( allocationLength ) );
       
    59     if ( !tempReturnBuf )
       
    60         {
       
    61         return KErrNoMemory;        
       
    62         }
       
    63     aAllocatedBuf = tempReturnBuf;
       
    64     return KErrNone;
       
    65     }
       
    66 
       
    67 
       
    68 
       
    69 // ---------------------------------------------------------------------------
       
    70 // Realloc could be converted to creation of a new buffer and
       
    71 // deletion of old because the copying of the old contents
       
    72 // is not needed.
       
    73 // ---------------------------------------------------------------------------
       
    74 // 
       
    75 TInt RetrieveNewDescLengthAndReAlloc( HBufC8*& aTempReturnBuf,
       
    76                                       TInt& aIncompleteMessageHandle )
       
    77     {   
       
    78     TInt incompleteMessageHandle( -1 );
       
    79     TInt newDescriptorMaxLength( -1 );
       
    80     TInt error( 0 );                
       
    81     error = InterpretNewAllocInfo( *aTempReturnBuf,
       
    82                                    incompleteMessageHandle,
       
    83                                    newDescriptorMaxLength );
       
    84     DLTRACE(("new maxlength: %d", newDescriptorMaxLength ));                                   
       
    85     if ( error != KErrNone )
       
    86         {
       
    87         // We should never end up here because it means
       
    88         // that our internal messaging has done something wrong
       
    89             
       
    90         // Cannot delete server side incomplete message because
       
    91         // handle possible invalid.
       
    92         aIncompleteMessageHandle = -1;
       
    93         return error;           
       
    94         }
       
    95     else if ( newDescriptorMaxLength
       
    96               <= KCatalogsMinimumAllocLength )
       
    97         {
       
    98         // This should also not be able to happen
       
    99         aIncompleteMessageHandle = incompleteMessageHandle;
       
   100         return KErrGeneral;
       
   101         }
       
   102         
       
   103     aIncompleteMessageHandle = incompleteMessageHandle;
       
   104 
       
   105     
       
   106     HBufC8* temp = aTempReturnBuf->ReAlloc( newDescriptorMaxLength );
       
   107     
       
   108     if ( !temp ) 
       
   109         {
       
   110         return KErrNoMemory;
       
   111         }
       
   112     
       
   113     aTempReturnBuf = temp;
       
   114 
       
   115     return KErrNone;
       
   116     }
       
   117 
       
   118 // ---------------------------------------------------------------------------
       
   119 // Realloc could be converted to creation of a new buffer and
       
   120 // deletion of old because the copying of the old contents
       
   121 // is not needed.
       
   122 // ---------------------------------------------------------------------------
       
   123 // 
       
   124 TInt RetrieveNewDescLengthAndReAlloc( HBufC16*& aTempReturnBuf,
       
   125                                       TInt& aIncompleteMessageHandle )
       
   126     {
       
   127     TInt incompleteMessageHandle( -1 );
       
   128     TInt newDescriptorMaxLength( -1 );
       
   129     TInt error( 0 );                
       
   130     error = InterpretNewAllocInfo( *aTempReturnBuf,
       
   131                                    incompleteMessageHandle,
       
   132                                    newDescriptorMaxLength );
       
   133     if ( error != KErrNone )
       
   134         {
       
   135         // We should never end up here because it means
       
   136         // that our internal messaging has done something wrong
       
   137             
       
   138         // Cannot delete server side incomplete message because
       
   139         // handle possible invalid.
       
   140         aIncompleteMessageHandle = -1;
       
   141         return error;           
       
   142         }
       
   143     else if ( newDescriptorMaxLength
       
   144               <= KCatalogsMinimumAllocLength )
       
   145         {
       
   146         // This should also not be able to happen
       
   147         aIncompleteMessageHandle = incompleteMessageHandle;
       
   148         return KErrGeneral;
       
   149         }
       
   150         
       
   151     aIncompleteMessageHandle = incompleteMessageHandle;
       
   152 
       
   153     HBufC16* temp = aTempReturnBuf->ReAlloc( newDescriptorMaxLength );
       
   154     
       
   155     if ( !temp ) 
       
   156         {
       
   157         return KErrNoMemory;
       
   158         }
       
   159     
       
   160     aTempReturnBuf = temp;
       
   161 
       
   162     return KErrNone;    
       
   163     }
       
   164     
       
   165   
       
   166         
       
   167     
       
   168 // ---------------------------------------------------------------------------
       
   169 // At the moment this function expects aTempReturnBuf to contain
       
   170 // elements delimited with KCatalogsTooSmallDescMsgElementDivider.
       
   171 // <incomplete message's handle><KCatalogsTooSmallDescMsgElementDivider><new length>
       
   172 // ---------------------------------------------------------------------------
       
   173 //    
       
   174 TInt InterpretNewAllocInfo( const HBufC8& aTempReturnBuf,
       
   175                             TInt& aIncompleteMessageHandle,
       
   176                             TInt& aNewDescriptorMaxLength )
       
   177     {
       
   178     
       
   179     TInt messageLength = aTempReturnBuf.Length();
       
   180     if ( messageLength < 3 )
       
   181         {
       
   182         // Too small descriptor to contain valid information for us.
       
   183         return KErrGeneral;
       
   184         }
       
   185         
       
   186     TInt dividerIndex( aTempReturnBuf.Locate( 
       
   187                         KCatalogsTooSmallDescMsgElementDivider ) );
       
   188     if ( dividerIndex == KErrNotFound )
       
   189         {
       
   190         return KErrGeneral;
       
   191         }
       
   192         
       
   193     // Message's handle is the string from beginning of the descriptor
       
   194     // to the previous char before the divider. (length of this is the same
       
   195     // as index of the divider)
       
   196     TPtrC8 descHandle = aTempReturnBuf.Left( dividerIndex );
       
   197 
       
   198     TInt intHandle = 0;
       
   199     TInt error = DesDecToInt( descHandle, intHandle );
       
   200     if ( error != KErrNone )
       
   201         {
       
   202         return error;
       
   203         }
       
   204     
       
   205     
       
   206     TInt lengthFromRight = messageLength - ( dividerIndex + 1 );
       
   207     TPtrC8 descNewLength = aTempReturnBuf.Right( lengthFromRight );    
       
   208 
       
   209     TInt intNewLength = 0;
       
   210     error = DesDecToInt( descNewLength, intNewLength );
       
   211     if ( error != KErrNone )
       
   212         {
       
   213         return error;
       
   214         }    
       
   215     
       
   216     aIncompleteMessageHandle = intHandle;
       
   217     aNewDescriptorMaxLength = intNewLength; 
       
   218     
       
   219     return KErrNone;
       
   220     }
       
   221     
       
   222  // ---------------------------------------------------------------------------
       
   223 // At the moment this function expects aTempReturnBuf to contain
       
   224 // elements delimited with KCatalogsTooSmallDescMsgElementDivider.
       
   225 // <incomplete message's handle><KCatalogsTooSmallDescMsgElementDivider><new length>
       
   226 // ---------------------------------------------------------------------------
       
   227 //    
       
   228 TInt InterpretNewAllocInfo( const HBufC16& aTempReturnBuf,
       
   229                             TInt& aIncompleteMessageHandle,
       
   230                             TInt& aNewDescriptorMaxLength )
       
   231     {
       
   232     
       
   233     TInt messageLength = aTempReturnBuf.Length();
       
   234     if ( messageLength < 3 )
       
   235         {
       
   236         // Too small descriptor to contain valid information for us.
       
   237         return KErrGeneral;
       
   238         }
       
   239         
       
   240     TInt dividerIndex( aTempReturnBuf.Locate( 
       
   241                         KCatalogsTooSmallDescMsgElementDivider ) );
       
   242     if ( dividerIndex == KErrNotFound )
       
   243         {
       
   244         return KErrGeneral;
       
   245         }
       
   246         
       
   247     // Message's handle is the string from beginning of the descriptor
       
   248     // to the previous char before the divider. (length of this is the same
       
   249     // as index of the divider)
       
   250     TPtrC16 descHandle = aTempReturnBuf.Left( dividerIndex );
       
   251 
       
   252     TInt intHandle = 0;
       
   253     TInt error = DesDecToInt( descHandle, intHandle );
       
   254     if ( error != KErrNone )
       
   255         {
       
   256         return error;
       
   257         }
       
   258     
       
   259     
       
   260     TInt lengthFromRight = messageLength - ( dividerIndex + 1 );
       
   261     TPtrC16 descNewLength = aTempReturnBuf.Right( lengthFromRight );    
       
   262 
       
   263     TInt intNewLength = 0;
       
   264     error = DesDecToInt( descNewLength, intNewLength );
       
   265     if ( error != KErrNone )
       
   266         {
       
   267         return error;
       
   268         }    
       
   269     
       
   270     aIncompleteMessageHandle = intHandle;
       
   271     aNewDescriptorMaxLength = intNewLength; 
       
   272     
       
   273     return KErrNone;
       
   274     }