os/persistentdata/persistentstorage/store/INC/S32UCMP.H
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// Header for the Standard Compression Scheme for Unicode.
sl@0
    15
// This code is compiled only in the Unicode build.
sl@0
    16
// 
sl@0
    17
//
sl@0
    18
sl@0
    19
#ifndef __S32UCMP_H__
sl@0
    20
#define __S32UCMP_H__ 1
sl@0
    21
sl@0
    22
#ifdef _UNICODE
sl@0
    23
sl@0
    24
#include <e32std.h>
sl@0
    25
#include <s32mem.h>
sl@0
    26
sl@0
    27
/**
sl@0
    28
 * @publishedAll 
sl@0
    29
 * @released
sl@0
    30
 */
sl@0
    31
class TUnicodeCompressionState
sl@0
    32
	{
sl@0
    33
	public:
sl@0
    34
	TUnicodeCompressionState();
sl@0
    35
	void Reset();
sl@0
    36
	static TInt StaticWindowIndex(TUint16 aCode);
sl@0
    37
	static TInt DynamicWindowOffsetIndex(TUint16 aCode);
sl@0
    38
	static TUint32 DynamicWindowBase(TInt aOffsetIndex);
sl@0
    39
	static TBool EncodeAsIs(TUint16 aCode);
sl@0
    40
sl@0
    41
	enum TPanic
sl@0
    42
		{
sl@0
    43
		EUnhandledByte,			// expander code fails to handle all possible byte codes
sl@0
    44
		ENotUnicode,			// expander can't handle Unicode values outside range 0x0..0x10FFFF;
sl@0
    45
								// that is, 16-bit codes plus 32-bit codes that can be expressed using
sl@0
    46
								// 16-bit surrogates
sl@0
    47
		EOutputBufferOverflow	// output buffer is not big enough
sl@0
    48
		};
sl@0
    49
sl@0
    50
	static void Panic(TPanic aPanic);
sl@0
    51
sl@0
    52
	protected:
sl@0
    53
sl@0
    54
	enum
sl@0
    55
		{
sl@0
    56
		EStaticWindows = 8,
sl@0
    57
		EDynamicWindows = 8,
sl@0
    58
		ESpecialBases = 7
sl@0
    59
		};
sl@0
    60
sl@0
    61
	TBool iUnicodeMode;									// TRUE if in Unicode mode as opposed to single-byte mode
sl@0
    62
	TUint32 iActiveWindowBase;							// base of the active window - bases are 32-bit because they
sl@0
    63
														// can be set to the surrogate area, which represents codes
sl@0
    64
														// from 0x00010000 to 0x0010FFFF - planes 1-16 of ISO-10646.
sl@0
    65
	static const TUint32 iStaticWindow[EStaticWindows];	// bases of the static windows
sl@0
    66
	static const TUint32 iDynamicWindowDefault[EDynamicWindows];	// default bases of the dynamic windows
sl@0
    67
	static const TUint16 iSpecialBase[ESpecialBases];	// bases for window offsets F9..FF
sl@0
    68
sl@0
    69
	TUint32 iDynamicWindow[EDynamicWindows];			// bases of the dynamic windows
sl@0
    70
	TInt iUnicodeWords;									// Unicode words processed; read by compressor, written by expander
sl@0
    71
	TInt iMaxUnicodeWords;								// maximum number of Unicode words to read or write
sl@0
    72
	TInt iCompressedBytes;								// compressed bytes processed: read by expander, written by compressor
sl@0
    73
	TInt iMaxCompressedBytes;							// maximum number of compressed bytes to read or write
sl@0
    74
	};
sl@0
    75
sl@0
    76
/**
sl@0
    77
 * @publishedAll 
sl@0
    78
 * @released
sl@0
    79
 */
sl@0
    80
class MUnicodeSource
sl@0
    81
	{
sl@0
    82
	public:
sl@0
    83
	virtual TUint16 ReadUnicodeValueL() = 0;
sl@0
    84
	};
sl@0
    85
sl@0
    86
/**
sl@0
    87
 * @publishedAll 
sl@0
    88
 * @released
sl@0
    89
 A class to read Unicode values directly from memory.
sl@0
    90
 */
sl@0
    91
class TMemoryUnicodeSource: public MUnicodeSource
sl@0
    92
	{
sl@0
    93
	public:
sl@0
    94
	inline TMemoryUnicodeSource(const TUint16* aPtr);
sl@0
    95
	inline TUint16 ReadUnicodeValueL();
sl@0
    96
sl@0
    97
	private:
sl@0
    98
	const TUint16* iPtr;
sl@0
    99
	};
sl@0
   100
sl@0
   101
/**
sl@0
   102
 * @publishedAll 
sl@0
   103
 * @released
sl@0
   104
 A class to read Unicode values from a stream built on a memory object.
sl@0
   105
 */
sl@0
   106
class TMemoryStreamUnicodeSource: public MUnicodeSource
sl@0
   107
	{
sl@0
   108
	public:
sl@0
   109
	inline TMemoryStreamUnicodeSource(RReadStream& aStream);
sl@0
   110
	inline TUint16 ReadUnicodeValueL();
sl@0
   111
sl@0
   112
	private:
sl@0
   113
	RReadStream& iStream;
sl@0
   114
	};
sl@0
   115
sl@0
   116
/**
sl@0
   117
 * @publishedAll 
sl@0
   118
 * @released
sl@0
   119
 */
sl@0
   120
class MUnicodeSink
sl@0
   121
	{
sl@0
   122
	public:
sl@0
   123
	virtual void WriteUnicodeValueL(TUint16 aValue) = 0;
sl@0
   124
	};
sl@0
   125
sl@0
   126
/**
sl@0
   127
 * @publishedAll 
sl@0
   128
 * @released
sl@0
   129
 A class to write Unicode values directly to memory.
sl@0
   130
 */
sl@0
   131
class TMemoryUnicodeSink: public MUnicodeSink
sl@0
   132
	{
sl@0
   133
	public:
sl@0
   134
	inline TMemoryUnicodeSink(TUint16* aPtr);
sl@0
   135
	inline void WriteUnicodeValueL(TUint16 aValue);
sl@0
   136
sl@0
   137
	private:
sl@0
   138
	TUint16* iPtr;
sl@0
   139
	};
sl@0
   140
sl@0
   141
/**
sl@0
   142
 * @publishedAll 
sl@0
   143
 * @released
sl@0
   144
 A class to write Unicode values to a stream built on a memory object.
sl@0
   145
 */
sl@0
   146
class TMemoryStreamUnicodeSink: public MUnicodeSink
sl@0
   147
	{
sl@0
   148
	public:
sl@0
   149
	inline TMemoryStreamUnicodeSink(RWriteStream& aStream);
sl@0
   150
	inline void WriteUnicodeValueL(TUint16 aValue);
sl@0
   151
sl@0
   152
	private:
sl@0
   153
	RWriteStream& iStream;
sl@0
   154
	};
sl@0
   155
sl@0
   156
/**
sl@0
   157
 * @publishedAll 
sl@0
   158
 * @released
sl@0
   159
 
sl@0
   160
A class to hold functions to compress text using the Standard Compression Scheme for Unicode.
sl@0
   161
sl@0
   162
A note on error handling and leaving.
sl@0
   163
sl@0
   164
Although all the public functions except the constructor can leave, it is possible to guarantee success: that is,
sl@0
   165
guarantee that a call will not leave, and that compression will be completed. To do this, (i) supply a MUnicodeSource
sl@0
   166
object with a non-leaving ReadUnicodeValueL function, such as a TMemoryUnicodeSource; (ii) write output to a
sl@0
   167
RWriteStream with a non-leaving WriteL function, or to a buffer that you already know to be big enough, which can be
sl@0
   168
found out using CompressedSizeL.
sl@0
   169
sl@0
   170
This guarantee of success is particularly useful when compressing from one memory buffer to another.
sl@0
   171
*/
sl@0
   172
class TUnicodeCompressor: public TUnicodeCompressionState
sl@0
   173
	{
sl@0
   174
	public:
sl@0
   175
	IMPORT_C TUnicodeCompressor();
sl@0
   176
	IMPORT_C void CompressL(RWriteStream& aOutput,MUnicodeSource& aInput,
sl@0
   177
							TInt aMaxOutputBytes = KMaxTInt,TInt aMaxInputWords = KMaxTInt,
sl@0
   178
							TInt* aOutputBytes = NULL,TInt* aInputWords = NULL);
sl@0
   179
	IMPORT_C void CompressL(TUint8* aOutput,MUnicodeSource& aInput,
sl@0
   180
							TInt aMaxOutputBytes = KMaxTInt,TInt aMaxInputWords = KMaxTInt,
sl@0
   181
							TInt* aOutputBytes = NULL,TInt* aInputWords = NULL);
sl@0
   182
	IMPORT_C TInt FlushL(RWriteStream& aOutput,TInt aMaxOutputBytes,TInt& aOutputBytes);
sl@0
   183
	IMPORT_C TInt FlushL(TUint8* aOutput,TInt aMaxOutputBytes,TInt& aOutputBytes);
sl@0
   184
	IMPORT_C static TInt CompressedSizeL(MUnicodeSource& aInput,TInt aInputWords);
sl@0
   185
sl@0
   186
	private:
sl@0
   187
sl@0
   188
	 // A structure to store a character and its treatment code
sl@0
   189
	struct TAction
sl@0
   190
		{
sl@0
   191
		// Treatment codes: static and dynamic window numbers, plain ASCII or plain Unicode
sl@0
   192
		enum
sl@0
   193
			{
sl@0
   194
			EPlainUnicode = -2,	// character cannot be expressed as ASCII or using static or dynamic windows
sl@0
   195
			EPlainASCII = -1,	// character can be emitted as an ASCII code
sl@0
   196
			EFirstDynamic = 0,	// values 0..255 are for dynamic windows with offsets at these places in the offset table
sl@0
   197
			ELastDynamic = 255,
sl@0
   198
			EFirstStatic = 256,	// values 256..263 are for static windows 0..7
sl@0
   199
			ELastStatic = 263
sl@0
   200
			};
sl@0
   201
sl@0
   202
		inline TAction();
sl@0
   203
		TAction(TUint16 aCode);
sl@0
   204
sl@0
   205
		TUint16 iCode;		// Unicode value of the character
sl@0
   206
		TInt iTreatment;	// treatment code: see above
sl@0
   207
		};
sl@0
   208
sl@0
   209
	void DoCompressL(RWriteStream* aOutputStream,TUint8* aOutputPointer,MUnicodeSource* aInput,
sl@0
   210
					 TInt aMaxCompressedBytes,TInt aMaxUnicodeWords,
sl@0
   211
					 TInt* aCompressedBytes,TInt* aUnicodeWords);
sl@0
   212
	void FlushInputBufferL();
sl@0
   213
	void FlushOutputBufferL();
sl@0
   214
	void WriteRunL();
sl@0
   215
	void WriteCharacter(const TAction& aAction);
sl@0
   216
	void WriteSCharacter(const TAction& aAction);
sl@0
   217
	void WriteUCharacter(TUint16 aCode);
sl@0
   218
	void WriteByte(TUint aByte);
sl@0
   219
	void WriteCharacterFromBuffer();
sl@0
   220
	void SelectTreatment(TInt aTreatment);
sl@0
   221
sl@0
   222
	enum
sl@0
   223
		{
sl@0
   224
		EMaxInputBufferSize = 4,
sl@0
   225
		EMaxOutputBufferSize = EMaxInputBufferSize * 3	// no Unicode character can be encoded as more than three bytes
sl@0
   226
		};
sl@0
   227
	TAction iInputBuffer[EMaxInputBufferSize];			// circular buffer; queue of Unicode characters to be processed
sl@0
   228
	TInt iInputBufferStart;								// position of first Unicode character to be processed
sl@0
   229
	TInt iInputBufferSize;								// characters in the input buffer
sl@0
   230
	TUint8 iOutputBuffer[EMaxOutputBufferSize];			// circular buffer; queue of compressed bytes to be output
sl@0
   231
	TInt iOutputBufferStart;							// position of first compressed byte to be output
sl@0
   232
	TInt iOutputBufferSize;								// characters in the output buffer
sl@0
   233
	TInt iDynamicWindowIndex;							// index of the current dynamic window
sl@0
   234
	RWriteStream* iOutputStream;						// if non-null, output is to this stream
sl@0
   235
	TUint8* iOutputPointer;								// if non-null, output is to memory
sl@0
   236
	MUnicodeSource* iInput;								// input object
sl@0
   237
	};
sl@0
   238
sl@0
   239
/**
sl@0
   240
 * @publishedAll 
sl@0
   241
 * @released
sl@0
   242
sl@0
   243
A class to hold functions to expand text using the Standard Compression Scheme for Unicode.
sl@0
   244
sl@0
   245
A note on error handling and leaving.
sl@0
   246
sl@0
   247
Although all the public functions except the constructor can leave, it is possible to guarantee success: that is,
sl@0
   248
guarantee that a call will not leave, and that expansion will be completed. To do this, (i) supply a MUnicodeSink
sl@0
   249
object with a non-leaving WriteUnicodeValueL function, such as a TMemoryUnicodeSink; (ii) read input from a RReadStream
sl@0
   250
with a non-leaving ReadL function; (iii) supply a big enough buffer to write the ouput; you can find out how big by
sl@0
   251
calling ExpandedSizeL, using methods (i) and (ii) to guarantee success.
sl@0
   252
sl@0
   253
This guarantee of success is particularly useful when expanding from one memory buffer to another.
sl@0
   254
*/
sl@0
   255
class TUnicodeExpander: public TUnicodeCompressionState
sl@0
   256
	{
sl@0
   257
	public:
sl@0
   258
	IMPORT_C TUnicodeExpander();
sl@0
   259
	IMPORT_C void ExpandL(MUnicodeSink& aOutput,RReadStream& aInput,
sl@0
   260
						  TInt aMaxOutputWords = KMaxTInt,TInt aMaxInputBytes = KMaxTInt,
sl@0
   261
						  TInt* aOutputWords = NULL,TInt* aInputBytes = NULL);
sl@0
   262
	IMPORT_C void ExpandL(MUnicodeSink& aOutput,const TUint8* aInput,
sl@0
   263
						  TInt aMaxOutputWords = KMaxTInt,TInt aMaxInputBytes = KMaxTInt,
sl@0
   264
						  TInt* aOutputWords = NULL,TInt* aInputBytes = NULL);
sl@0
   265
	IMPORT_C TInt FlushL(MUnicodeSink& aOutput,TInt aMaxOutputWords,TInt& aOutputWords);
sl@0
   266
	IMPORT_C static TInt ExpandedSizeL(RReadStream& aInput,TInt aInputBytes);
sl@0
   267
	IMPORT_C static TInt ExpandedSizeL(const TUint8* aInput,TInt aInputBytes);
sl@0
   268
sl@0
   269
	private:
sl@0
   270
	void DoExpandL(MUnicodeSink* aOutput,RReadStream* aInputStream,const TUint8* aInputPointer,
sl@0
   271
				   TInt aMaxOutputWords,TInt aMaxInputBytes,
sl@0
   272
				   TInt* aOutputWords,TInt* aInputBytes);
sl@0
   273
	void HandleByteL();
sl@0
   274
	void FlushOutputBufferL();
sl@0
   275
	TBool HandleSByteL(TUint8 aByte);
sl@0
   276
	TBool HandleUByteL(TUint8 aByte);
sl@0
   277
	TBool ReadByteL(TUint8& aByte);
sl@0
   278
	TBool QuoteUnicodeL();
sl@0
   279
	TBool DefineWindowL(TInt aIndex);
sl@0
   280
	TBool DefineExpansionWindowL();
sl@0
   281
	void WriteChar(TText aChar);
sl@0
   282
	void WriteChar32(TUint aChar);
sl@0
   283
sl@0
   284
	enum
sl@0
   285
		{
sl@0
   286
		EMaxInputBufferSize = 3,						// no Unicode character can be encoded as more than 3 bytes
sl@0
   287
		EMaxOutputBufferSize = 2						// no byte can be expanded into more than 2 Unicode characters
sl@0
   288
		};
sl@0
   289
	TUint8 iInputBuffer[EMaxInputBufferSize];			// buffer containing a group of compressed bytes representing
sl@0
   290
														// a single operation; when an input source ends in the
sl@0
   291
														// middle of an operation, this buffer enables the next
sl@0
   292
														// expansion to start in the correct state
sl@0
   293
	TInt iInputBufferStart;								// next read position in the input buffer
sl@0
   294
	TInt iInputBufferSize;								// bytes in the input buffer
sl@0
   295
	TUint16 iOutputBuffer[EMaxOutputBufferSize];		// circular buffer; queue of Unicode characters to be output
sl@0
   296
	TInt iOutputBufferStart;							// position of first Unicode character to be output
sl@0
   297
	TInt iOutputBufferSize;								// characters in the output buffer
sl@0
   298
	MUnicodeSink* iOutput;								// output object
sl@0
   299
	RReadStream* iInputStream;							// if non-null, input is from this stream
sl@0
   300
	const TUint8* iInputPointer;						// if non-null, input is from memory
sl@0
   301
	};
sl@0
   302
sl@0
   303
// inline functions start here
sl@0
   304
sl@0
   305
inline TMemoryUnicodeSource::TMemoryUnicodeSource(const TUint16* aPtr):
sl@0
   306
	iPtr(aPtr)
sl@0
   307
	{
sl@0
   308
	}
sl@0
   309
sl@0
   310
inline TUint16 TMemoryUnicodeSource::ReadUnicodeValueL()
sl@0
   311
	{
sl@0
   312
	return *iPtr++;
sl@0
   313
	}
sl@0
   314
sl@0
   315
inline TMemoryStreamUnicodeSource::TMemoryStreamUnicodeSource(RReadStream& aStream):
sl@0
   316
	iStream(aStream)
sl@0
   317
	{
sl@0
   318
	}
sl@0
   319
sl@0
   320
inline TUint16 TMemoryStreamUnicodeSource::ReadUnicodeValueL()
sl@0
   321
	{
sl@0
   322
	TUint16 x;
sl@0
   323
	iStream.ReadL((TUint8*)&x,sizeof(TUint16));
sl@0
   324
	return x;
sl@0
   325
	}
sl@0
   326
sl@0
   327
inline TMemoryUnicodeSink::TMemoryUnicodeSink(TUint16* aPtr):
sl@0
   328
	iPtr(aPtr)
sl@0
   329
	{
sl@0
   330
	}
sl@0
   331
sl@0
   332
inline void TMemoryUnicodeSink::WriteUnicodeValueL(TUint16 aValue)
sl@0
   333
	{
sl@0
   334
	*iPtr++ = aValue;
sl@0
   335
	}
sl@0
   336
sl@0
   337
inline TMemoryStreamUnicodeSink::TMemoryStreamUnicodeSink(RWriteStream& aStream):
sl@0
   338
	iStream(aStream)
sl@0
   339
	{
sl@0
   340
	}
sl@0
   341
sl@0
   342
inline void TMemoryStreamUnicodeSink::WriteUnicodeValueL(TUint16 aValue)
sl@0
   343
	{
sl@0
   344
	iStream.WriteL((TUint8*)&aValue,sizeof(TUint16));
sl@0
   345
	}
sl@0
   346
sl@0
   347
inline TUnicodeCompressor::TAction::TAction():
sl@0
   348
	iCode(0),
sl@0
   349
	iTreatment(EPlainUnicode)
sl@0
   350
	{
sl@0
   351
	}
sl@0
   352
sl@0
   353
#endif // _UNICODE
sl@0
   354
sl@0
   355
#endif // __S32UCMP_H__