secureswitools/swisistools/source/sisxlibrary/siscompressed.cpp
changeset 0 ba25891c3a9e
equal deleted inserted replaced
-1:000000000000 0:ba25891c3a9e
       
     1 /*
       
     2 * Copyright (c) 2007-2009 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 the License "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  @file 
       
    21  @internalComponent
       
    22  @released
       
    23 */
       
    24 
       
    25 #ifdef _MSC_VER
       
    26 #pragma warning (disable: 4786)
       
    27 #endif // _MSC_VER
       
    28 
       
    29 #include "siswrite.h"
       
    30 #include "utility_interface.h"
       
    31 #include <zlib.h>
       
    32 
       
    33 bool CompressData (	const TUint8* aInputData, const CSISFieldRoot::TFieldSize aInputSize,
       
    34 					TUint8*& aOutputData, CSISFieldRoot::TFieldSize& aOutputSize,
       
    35 					const bool aCanAbandon)
       
    36 	{
       
    37 	assert (aOutputData == NULL);
       
    38 	assert (aInputData != NULL);
       
    39 	assert (! IsBadReadPtr (aInputData, aInputSize));
       
    40 	CSISFieldRoot::TFieldSize inputSize = aInputSize;
       
    41 	inputSize = inputSize + (inputSize / 64) + 16;
       
    42 		// see zlib library documentation for the reasons for this calculation
       
    43 	TUint8* buffer = new TUint8 [inputSize];
       
    44 	try
       
    45 		{
       
    46 		z_stream compressionState;
       
    47 		memset (&compressionState, 0, sizeof (z_stream));
       
    48 		compressionState.avail_out = inputSize;
       
    49 		compressionState.data_type = Z_BINARY;
       
    50 		compressionState.avail_in = aInputSize;
       
    51 		compressionState.next_out = buffer;
       
    52 		compressionState.next_in = const_cast <Bytef*> (aInputData);
       
    53 		try 
       
    54 			{
       
    55 			CSISException::ThrowIf (deflateInit (&compressionState, Z_DEFAULT_COMPRESSION) != Z_OK,
       
    56 									CSISException::ECompress,
       
    57 									"deflation progress");
       
    58 			CSISException::ThrowIf (deflate (&compressionState, Z_FINISH) != Z_STREAM_END,
       
    59 									CSISException::ECompress,
       
    60 									"deflation end");
       
    61 			}
       
    62 		catch (...)
       
    63 			{
       
    64 			deflateEnd (&compressionState);
       
    65 			throw;
       
    66 			}
       
    67 		deflateEnd (&compressionState);
       
    68 		aOutputData = buffer;
       
    69 		aOutputSize = static_cast <CSISFieldRoot::TFieldSize> (compressionState.total_out);
       
    70 		}
       
    71 	catch (...) 
       
    72 		{
       
    73 		delete [] buffer;
       
    74 		aOutputData = NULL;
       
    75 		aOutputSize = 0;
       
    76 		throw;
       
    77 		}
       
    78 	if ((aOutputSize < aInputSize) || ! aCanAbandon)
       
    79 		{
       
    80 		return true;
       
    81 		}
       
    82 	delete [] buffer;
       
    83 	aOutputData = NULL;
       
    84 	aOutputSize = 0;
       
    85 	return false;
       
    86 	}
       
    87 
       
    88 
       
    89 
       
    90 
       
    91 void ExpandStream (const TUint8* aInputData, const CSISFieldRoot::TFieldSize aInputSize, TSISStream& stream)
       
    92 	{
       
    93 	assert (aInputData != NULL);
       
    94 	assert (! IsBadReadPtr (aInputData, aInputSize));
       
    95 	z_stream compressionState;
       
    96 	memset (&compressionState, 0, sizeof (z_stream));
       
    97 	const uLong arbitraryBufferSize (262144);
       
    98 	TSISStream::pos_type start = stream.tell ();
       
    99 	TUint8* buffer = new TUint8 [arbitraryBufferSize];
       
   100 	try 
       
   101 		{
       
   102 		compressionState.avail_in = aInputSize;
       
   103 		compressionState.next_in = const_cast <Bytef*> (aInputData);
       
   104 		compressionState.avail_out = arbitraryBufferSize;
       
   105 		compressionState.next_out = buffer;
       
   106 		compressionState.data_type = Z_BINARY;
       
   107 		CSISException::ThrowIf (inflateInit (&compressionState) != Z_OK,
       
   108 								CSISException::ECompress,
       
   109 								"inflation initialisation");
       
   110 		int status;
       
   111 		while ((status = inflate (&compressionState, Z_NO_FLUSH)) == Z_OK)
       
   112 			{
       
   113 			stream.write (buffer, arbitraryBufferSize - compressionState.avail_out);
       
   114 			compressionState.avail_out = arbitraryBufferSize;
       
   115 			compressionState.next_out = buffer;
       
   116 			}
       
   117 		CSISException::ThrowIf (status != Z_STREAM_END,
       
   118 								CSISException::ECompress,
       
   119 								"inflation");
       
   120 		status = inflate (&compressionState, Z_FINISH);
       
   121 		CSISException::ThrowIf (status != Z_STREAM_END,
       
   122 								CSISException::ECompress,
       
   123 								"inflation");
       
   124 		stream.write (buffer, arbitraryBufferSize - compressionState.avail_out);
       
   125 		}
       
   126 	catch (...)
       
   127 		{
       
   128 		inflateEnd (&compressionState);
       
   129 		delete [] buffer;
       
   130 		stream.seek (start);
       
   131 		throw;
       
   132 		}
       
   133 	inflateEnd (&compressionState);
       
   134 	delete [] buffer;
       
   135 	}