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