crashanalysercmd/PerfToolsSharedLibraries/Engine/SymbianNativeTools/Source/byte_pair.cpp
changeset 0 818e61de6cd1
equal deleted inserted replaced
-1:000000000000 0:818e61de6cd1
       
     1 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 #include "byte_pair.h"
       
    16 
       
    17 // User includes
       
    18 #include "e32errwrap.h"
       
    19 
       
    20 TInt UnpackBytePair(TUint8* dst, TInt dstSize, TUint8* src, TInt srcSize, TUint8*& srcNext)
       
    21 	{
       
    22 	TUint8* dstStart = dst;
       
    23 	TUint8* dstEnd = dst+dstSize;
       
    24 	TUint8* srcEnd = src+srcSize;
       
    25 
       
    26 	TUint32 LUT[0x100/2];
       
    27 	TUint8* LUT0 = (TUint8*)LUT;
       
    28 	TUint8* LUT1 = LUT0+0x100;
       
    29 
       
    30 	TUint8 stack[0x100];
       
    31 	TUint8* stackStart = stack+sizeof(stack);
       
    32 	TUint8* sp = stackStart;
       
    33 
       
    34 	TUint32 marker = ~0u;
       
    35 	TInt numTokens;
       
    36 	TUint32 p1;
       
    37 	TUint32 p2;
       
    38 
       
    39 	TUint32* l = (TUint32*)LUT;
       
    40 	TUint32 b = 0x03020100;
       
    41 	TUint32 step = 0x04040404;
       
    42 	do
       
    43 		{
       
    44 		*l++ = b;
       
    45 		b += step;
       
    46 		}
       
    47 	while(b>step);
       
    48 
       
    49 	if(src>=srcEnd)
       
    50 		goto error;
       
    51 	numTokens = *src++;
       
    52 	if(numTokens)
       
    53 		{
       
    54 		if(src>=srcEnd)
       
    55 			goto error;
       
    56 		marker = *src++;
       
    57 		LUT0[marker] = (TUint8)~marker;
       
    58 
       
    59 		if(numTokens<32)
       
    60 			{
       
    61 			TUint8* tokenEnd = src+3*numTokens;
       
    62 			if(tokenEnd>srcEnd)
       
    63 				goto error;
       
    64 			do
       
    65 				{
       
    66 				TInt b = *src++;
       
    67 				TInt p1 = *src++;
       
    68 				TInt p2 = *src++;
       
    69 				LUT0[b] = (TUint8)p1;
       
    70 				LUT1[b] = (TUint8)p2;
       
    71 				}
       
    72 			while(src<tokenEnd);
       
    73 			}
       
    74 		else
       
    75 			{
       
    76 			TUint8* bitMask = src;
       
    77 			src += 32;
       
    78 			if(src>srcEnd)
       
    79 				goto error;
       
    80 			TInt b=0;
       
    81 			do
       
    82 				{
       
    83 				TUint8 mask = bitMask[b>>3];
       
    84 				if(mask&(1<<(b&7)))
       
    85 					{
       
    86 					if(src>srcEnd)
       
    87 						goto error;
       
    88 					TInt p1 = *src++;
       
    89 					if(src>srcEnd)
       
    90 						goto error;
       
    91 					TInt p2 = *src++;
       
    92 					LUT0[b] = (TUint8)p1;
       
    93 					LUT1[b] = (TUint8)p2;		
       
    94 					--numTokens;
       
    95 					}
       
    96 				++b;
       
    97 				}
       
    98 			while(b<0x100);
       
    99 			if(numTokens)
       
   100 				goto error;
       
   101 			}
       
   102 		}
       
   103 
       
   104 	if(src>=srcEnd)
       
   105 		goto error;
       
   106 	b = *src++;
       
   107 	if(dst>=dstEnd)
       
   108 		goto error;
       
   109 	p1 = LUT0[b];
       
   110 	if(p1!=b)
       
   111 		goto not_single;
       
   112 next:
       
   113 	if(src>=srcEnd)
       
   114 		goto done_s;
       
   115 	b = *src++;
       
   116 	*dst++ = (TUint8)p1;
       
   117 	if(dst>=dstEnd)
       
   118 		goto done_d;
       
   119 	p1 = LUT0[b];
       
   120 	if(p1==b)
       
   121 		goto next;
       
   122 
       
   123 not_single:
       
   124 	if(b==marker)
       
   125 		goto do_marker;
       
   126 
       
   127 do_pair:
       
   128 	p2 = LUT1[b];
       
   129 	b = p1;
       
   130 	p1 = LUT0[b];
       
   131 	if(sp<=stack)
       
   132 		goto error;
       
   133 	*--sp = (TUint8)p2;
       
   134 
       
   135 recurse:
       
   136 	if(b!=p1)
       
   137 		goto do_pair;
       
   138 
       
   139 	if(sp==stackStart)
       
   140 		goto next;
       
   141 	b = *sp++;
       
   142 	if(dst>=dstEnd)
       
   143 		goto error;
       
   144 	*dst++ = (TUint8)p1;
       
   145 	p1 = LUT0[b];
       
   146 	goto recurse;
       
   147 
       
   148 do_marker:
       
   149 	if(src>=srcEnd)
       
   150 		goto error;
       
   151 	p1 = *src++;
       
   152 	goto next;
       
   153 
       
   154 error:
       
   155 	srcNext = 0;
       
   156 	return KErrCorrupt;
       
   157 
       
   158 done_s:
       
   159 	*dst++ = (TUint8)p1;
       
   160 	srcNext = src;
       
   161 	return dst-dstStart;
       
   162 
       
   163 done_d:
       
   164 	if(dst>=dstEnd)
       
   165 		--src;
       
   166 	srcNext = src;
       
   167 	return dst-dstStart;
       
   168 	}
       
   169 
       
   170 
       
   171