1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/persistentdata/persistentstorage/store/INC/S32UCMP.H Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,355 @@
1.4 +// Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +// Header for the Standard Compression Scheme for Unicode.
1.18 +// This code is compiled only in the Unicode build.
1.19 +//
1.20 +//
1.21 +
1.22 +#ifndef __S32UCMP_H__
1.23 +#define __S32UCMP_H__ 1
1.24 +
1.25 +#ifdef _UNICODE
1.26 +
1.27 +#include <e32std.h>
1.28 +#include <s32mem.h>
1.29 +
1.30 +/**
1.31 + * @publishedAll
1.32 + * @released
1.33 + */
1.34 +class TUnicodeCompressionState
1.35 + {
1.36 + public:
1.37 + TUnicodeCompressionState();
1.38 + void Reset();
1.39 + static TInt StaticWindowIndex(TUint16 aCode);
1.40 + static TInt DynamicWindowOffsetIndex(TUint16 aCode);
1.41 + static TUint32 DynamicWindowBase(TInt aOffsetIndex);
1.42 + static TBool EncodeAsIs(TUint16 aCode);
1.43 +
1.44 + enum TPanic
1.45 + {
1.46 + EUnhandledByte, // expander code fails to handle all possible byte codes
1.47 + ENotUnicode, // expander can't handle Unicode values outside range 0x0..0x10FFFF;
1.48 + // that is, 16-bit codes plus 32-bit codes that can be expressed using
1.49 + // 16-bit surrogates
1.50 + EOutputBufferOverflow // output buffer is not big enough
1.51 + };
1.52 +
1.53 + static void Panic(TPanic aPanic);
1.54 +
1.55 + protected:
1.56 +
1.57 + enum
1.58 + {
1.59 + EStaticWindows = 8,
1.60 + EDynamicWindows = 8,
1.61 + ESpecialBases = 7
1.62 + };
1.63 +
1.64 + TBool iUnicodeMode; // TRUE if in Unicode mode as opposed to single-byte mode
1.65 + TUint32 iActiveWindowBase; // base of the active window - bases are 32-bit because they
1.66 + // can be set to the surrogate area, which represents codes
1.67 + // from 0x00010000 to 0x0010FFFF - planes 1-16 of ISO-10646.
1.68 + static const TUint32 iStaticWindow[EStaticWindows]; // bases of the static windows
1.69 + static const TUint32 iDynamicWindowDefault[EDynamicWindows]; // default bases of the dynamic windows
1.70 + static const TUint16 iSpecialBase[ESpecialBases]; // bases for window offsets F9..FF
1.71 +
1.72 + TUint32 iDynamicWindow[EDynamicWindows]; // bases of the dynamic windows
1.73 + TInt iUnicodeWords; // Unicode words processed; read by compressor, written by expander
1.74 + TInt iMaxUnicodeWords; // maximum number of Unicode words to read or write
1.75 + TInt iCompressedBytes; // compressed bytes processed: read by expander, written by compressor
1.76 + TInt iMaxCompressedBytes; // maximum number of compressed bytes to read or write
1.77 + };
1.78 +
1.79 +/**
1.80 + * @publishedAll
1.81 + * @released
1.82 + */
1.83 +class MUnicodeSource
1.84 + {
1.85 + public:
1.86 + virtual TUint16 ReadUnicodeValueL() = 0;
1.87 + };
1.88 +
1.89 +/**
1.90 + * @publishedAll
1.91 + * @released
1.92 + A class to read Unicode values directly from memory.
1.93 + */
1.94 +class TMemoryUnicodeSource: public MUnicodeSource
1.95 + {
1.96 + public:
1.97 + inline TMemoryUnicodeSource(const TUint16* aPtr);
1.98 + inline TUint16 ReadUnicodeValueL();
1.99 +
1.100 + private:
1.101 + const TUint16* iPtr;
1.102 + };
1.103 +
1.104 +/**
1.105 + * @publishedAll
1.106 + * @released
1.107 + A class to read Unicode values from a stream built on a memory object.
1.108 + */
1.109 +class TMemoryStreamUnicodeSource: public MUnicodeSource
1.110 + {
1.111 + public:
1.112 + inline TMemoryStreamUnicodeSource(RReadStream& aStream);
1.113 + inline TUint16 ReadUnicodeValueL();
1.114 +
1.115 + private:
1.116 + RReadStream& iStream;
1.117 + };
1.118 +
1.119 +/**
1.120 + * @publishedAll
1.121 + * @released
1.122 + */
1.123 +class MUnicodeSink
1.124 + {
1.125 + public:
1.126 + virtual void WriteUnicodeValueL(TUint16 aValue) = 0;
1.127 + };
1.128 +
1.129 +/**
1.130 + * @publishedAll
1.131 + * @released
1.132 + A class to write Unicode values directly to memory.
1.133 + */
1.134 +class TMemoryUnicodeSink: public MUnicodeSink
1.135 + {
1.136 + public:
1.137 + inline TMemoryUnicodeSink(TUint16* aPtr);
1.138 + inline void WriteUnicodeValueL(TUint16 aValue);
1.139 +
1.140 + private:
1.141 + TUint16* iPtr;
1.142 + };
1.143 +
1.144 +/**
1.145 + * @publishedAll
1.146 + * @released
1.147 + A class to write Unicode values to a stream built on a memory object.
1.148 + */
1.149 +class TMemoryStreamUnicodeSink: public MUnicodeSink
1.150 + {
1.151 + public:
1.152 + inline TMemoryStreamUnicodeSink(RWriteStream& aStream);
1.153 + inline void WriteUnicodeValueL(TUint16 aValue);
1.154 +
1.155 + private:
1.156 + RWriteStream& iStream;
1.157 + };
1.158 +
1.159 +/**
1.160 + * @publishedAll
1.161 + * @released
1.162 +
1.163 +A class to hold functions to compress text using the Standard Compression Scheme for Unicode.
1.164 +
1.165 +A note on error handling and leaving.
1.166 +
1.167 +Although all the public functions except the constructor can leave, it is possible to guarantee success: that is,
1.168 +guarantee that a call will not leave, and that compression will be completed. To do this, (i) supply a MUnicodeSource
1.169 +object with a non-leaving ReadUnicodeValueL function, such as a TMemoryUnicodeSource; (ii) write output to a
1.170 +RWriteStream with a non-leaving WriteL function, or to a buffer that you already know to be big enough, which can be
1.171 +found out using CompressedSizeL.
1.172 +
1.173 +This guarantee of success is particularly useful when compressing from one memory buffer to another.
1.174 +*/
1.175 +class TUnicodeCompressor: public TUnicodeCompressionState
1.176 + {
1.177 + public:
1.178 + IMPORT_C TUnicodeCompressor();
1.179 + IMPORT_C void CompressL(RWriteStream& aOutput,MUnicodeSource& aInput,
1.180 + TInt aMaxOutputBytes = KMaxTInt,TInt aMaxInputWords = KMaxTInt,
1.181 + TInt* aOutputBytes = NULL,TInt* aInputWords = NULL);
1.182 + IMPORT_C void CompressL(TUint8* aOutput,MUnicodeSource& aInput,
1.183 + TInt aMaxOutputBytes = KMaxTInt,TInt aMaxInputWords = KMaxTInt,
1.184 + TInt* aOutputBytes = NULL,TInt* aInputWords = NULL);
1.185 + IMPORT_C TInt FlushL(RWriteStream& aOutput,TInt aMaxOutputBytes,TInt& aOutputBytes);
1.186 + IMPORT_C TInt FlushL(TUint8* aOutput,TInt aMaxOutputBytes,TInt& aOutputBytes);
1.187 + IMPORT_C static TInt CompressedSizeL(MUnicodeSource& aInput,TInt aInputWords);
1.188 +
1.189 + private:
1.190 +
1.191 + // A structure to store a character and its treatment code
1.192 + struct TAction
1.193 + {
1.194 + // Treatment codes: static and dynamic window numbers, plain ASCII or plain Unicode
1.195 + enum
1.196 + {
1.197 + EPlainUnicode = -2, // character cannot be expressed as ASCII or using static or dynamic windows
1.198 + EPlainASCII = -1, // character can be emitted as an ASCII code
1.199 + EFirstDynamic = 0, // values 0..255 are for dynamic windows with offsets at these places in the offset table
1.200 + ELastDynamic = 255,
1.201 + EFirstStatic = 256, // values 256..263 are for static windows 0..7
1.202 + ELastStatic = 263
1.203 + };
1.204 +
1.205 + inline TAction();
1.206 + TAction(TUint16 aCode);
1.207 +
1.208 + TUint16 iCode; // Unicode value of the character
1.209 + TInt iTreatment; // treatment code: see above
1.210 + };
1.211 +
1.212 + void DoCompressL(RWriteStream* aOutputStream,TUint8* aOutputPointer,MUnicodeSource* aInput,
1.213 + TInt aMaxCompressedBytes,TInt aMaxUnicodeWords,
1.214 + TInt* aCompressedBytes,TInt* aUnicodeWords);
1.215 + void FlushInputBufferL();
1.216 + void FlushOutputBufferL();
1.217 + void WriteRunL();
1.218 + void WriteCharacter(const TAction& aAction);
1.219 + void WriteSCharacter(const TAction& aAction);
1.220 + void WriteUCharacter(TUint16 aCode);
1.221 + void WriteByte(TUint aByte);
1.222 + void WriteCharacterFromBuffer();
1.223 + void SelectTreatment(TInt aTreatment);
1.224 +
1.225 + enum
1.226 + {
1.227 + EMaxInputBufferSize = 4,
1.228 + EMaxOutputBufferSize = EMaxInputBufferSize * 3 // no Unicode character can be encoded as more than three bytes
1.229 + };
1.230 + TAction iInputBuffer[EMaxInputBufferSize]; // circular buffer; queue of Unicode characters to be processed
1.231 + TInt iInputBufferStart; // position of first Unicode character to be processed
1.232 + TInt iInputBufferSize; // characters in the input buffer
1.233 + TUint8 iOutputBuffer[EMaxOutputBufferSize]; // circular buffer; queue of compressed bytes to be output
1.234 + TInt iOutputBufferStart; // position of first compressed byte to be output
1.235 + TInt iOutputBufferSize; // characters in the output buffer
1.236 + TInt iDynamicWindowIndex; // index of the current dynamic window
1.237 + RWriteStream* iOutputStream; // if non-null, output is to this stream
1.238 + TUint8* iOutputPointer; // if non-null, output is to memory
1.239 + MUnicodeSource* iInput; // input object
1.240 + };
1.241 +
1.242 +/**
1.243 + * @publishedAll
1.244 + * @released
1.245 +
1.246 +A class to hold functions to expand text using the Standard Compression Scheme for Unicode.
1.247 +
1.248 +A note on error handling and leaving.
1.249 +
1.250 +Although all the public functions except the constructor can leave, it is possible to guarantee success: that is,
1.251 +guarantee that a call will not leave, and that expansion will be completed. To do this, (i) supply a MUnicodeSink
1.252 +object with a non-leaving WriteUnicodeValueL function, such as a TMemoryUnicodeSink; (ii) read input from a RReadStream
1.253 +with a non-leaving ReadL function; (iii) supply a big enough buffer to write the ouput; you can find out how big by
1.254 +calling ExpandedSizeL, using methods (i) and (ii) to guarantee success.
1.255 +
1.256 +This guarantee of success is particularly useful when expanding from one memory buffer to another.
1.257 +*/
1.258 +class TUnicodeExpander: public TUnicodeCompressionState
1.259 + {
1.260 + public:
1.261 + IMPORT_C TUnicodeExpander();
1.262 + IMPORT_C void ExpandL(MUnicodeSink& aOutput,RReadStream& aInput,
1.263 + TInt aMaxOutputWords = KMaxTInt,TInt aMaxInputBytes = KMaxTInt,
1.264 + TInt* aOutputWords = NULL,TInt* aInputBytes = NULL);
1.265 + IMPORT_C void ExpandL(MUnicodeSink& aOutput,const TUint8* aInput,
1.266 + TInt aMaxOutputWords = KMaxTInt,TInt aMaxInputBytes = KMaxTInt,
1.267 + TInt* aOutputWords = NULL,TInt* aInputBytes = NULL);
1.268 + IMPORT_C TInt FlushL(MUnicodeSink& aOutput,TInt aMaxOutputWords,TInt& aOutputWords);
1.269 + IMPORT_C static TInt ExpandedSizeL(RReadStream& aInput,TInt aInputBytes);
1.270 + IMPORT_C static TInt ExpandedSizeL(const TUint8* aInput,TInt aInputBytes);
1.271 +
1.272 + private:
1.273 + void DoExpandL(MUnicodeSink* aOutput,RReadStream* aInputStream,const TUint8* aInputPointer,
1.274 + TInt aMaxOutputWords,TInt aMaxInputBytes,
1.275 + TInt* aOutputWords,TInt* aInputBytes);
1.276 + void HandleByteL();
1.277 + void FlushOutputBufferL();
1.278 + TBool HandleSByteL(TUint8 aByte);
1.279 + TBool HandleUByteL(TUint8 aByte);
1.280 + TBool ReadByteL(TUint8& aByte);
1.281 + TBool QuoteUnicodeL();
1.282 + TBool DefineWindowL(TInt aIndex);
1.283 + TBool DefineExpansionWindowL();
1.284 + void WriteChar(TText aChar);
1.285 + void WriteChar32(TUint aChar);
1.286 +
1.287 + enum
1.288 + {
1.289 + EMaxInputBufferSize = 3, // no Unicode character can be encoded as more than 3 bytes
1.290 + EMaxOutputBufferSize = 2 // no byte can be expanded into more than 2 Unicode characters
1.291 + };
1.292 + TUint8 iInputBuffer[EMaxInputBufferSize]; // buffer containing a group of compressed bytes representing
1.293 + // a single operation; when an input source ends in the
1.294 + // middle of an operation, this buffer enables the next
1.295 + // expansion to start in the correct state
1.296 + TInt iInputBufferStart; // next read position in the input buffer
1.297 + TInt iInputBufferSize; // bytes in the input buffer
1.298 + TUint16 iOutputBuffer[EMaxOutputBufferSize]; // circular buffer; queue of Unicode characters to be output
1.299 + TInt iOutputBufferStart; // position of first Unicode character to be output
1.300 + TInt iOutputBufferSize; // characters in the output buffer
1.301 + MUnicodeSink* iOutput; // output object
1.302 + RReadStream* iInputStream; // if non-null, input is from this stream
1.303 + const TUint8* iInputPointer; // if non-null, input is from memory
1.304 + };
1.305 +
1.306 +// inline functions start here
1.307 +
1.308 +inline TMemoryUnicodeSource::TMemoryUnicodeSource(const TUint16* aPtr):
1.309 + iPtr(aPtr)
1.310 + {
1.311 + }
1.312 +
1.313 +inline TUint16 TMemoryUnicodeSource::ReadUnicodeValueL()
1.314 + {
1.315 + return *iPtr++;
1.316 + }
1.317 +
1.318 +inline TMemoryStreamUnicodeSource::TMemoryStreamUnicodeSource(RReadStream& aStream):
1.319 + iStream(aStream)
1.320 + {
1.321 + }
1.322 +
1.323 +inline TUint16 TMemoryStreamUnicodeSource::ReadUnicodeValueL()
1.324 + {
1.325 + TUint16 x;
1.326 + iStream.ReadL((TUint8*)&x,sizeof(TUint16));
1.327 + return x;
1.328 + }
1.329 +
1.330 +inline TMemoryUnicodeSink::TMemoryUnicodeSink(TUint16* aPtr):
1.331 + iPtr(aPtr)
1.332 + {
1.333 + }
1.334 +
1.335 +inline void TMemoryUnicodeSink::WriteUnicodeValueL(TUint16 aValue)
1.336 + {
1.337 + *iPtr++ = aValue;
1.338 + }
1.339 +
1.340 +inline TMemoryStreamUnicodeSink::TMemoryStreamUnicodeSink(RWriteStream& aStream):
1.341 + iStream(aStream)
1.342 + {
1.343 + }
1.344 +
1.345 +inline void TMemoryStreamUnicodeSink::WriteUnicodeValueL(TUint16 aValue)
1.346 + {
1.347 + iStream.WriteL((TUint8*)&aValue,sizeof(TUint16));
1.348 + }
1.349 +
1.350 +inline TUnicodeCompressor::TAction::TAction():
1.351 + iCode(0),
1.352 + iTreatment(EPlainUnicode)
1.353 + {
1.354 + }
1.355 +
1.356 +#endif // _UNICODE
1.357 +
1.358 +#endif // __S32UCMP_H__