commands/chkdeps/byte_pair.cpp
changeset 0 7f656887cf89
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     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 the License "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 // e32\kernel\byte_pair.cpp
       
    15 // 
       
    16 //
       
    17 
       
    18 #include <e32cia.h>
       
    19 #include "u32std.h"
       
    20 
       
    21 TInt BytePairDecompress(TUint8* dst, TInt dstSize, TUint8* src, TInt srcSize, TUint8*& srcNext)
       
    22 	{
       
    23 	TUint8* dstStart = dst;
       
    24 	TUint8* dstEnd = dst+dstSize;
       
    25 	TUint8* srcEnd = src+srcSize;
       
    26 
       
    27 	TUint32 LUT[0x100/2];
       
    28 	TUint8* LUT0 = (TUint8*)LUT;
       
    29 	TUint8* LUT1 = LUT0+0x100;
       
    30 
       
    31 	TUint8 stack[0x100];
       
    32 	TUint8* stackStart = stack+sizeof(stack);
       
    33 	TUint8* sp = stackStart;
       
    34 
       
    35 	TUint32 marker = ~0u;
       
    36 	TInt numTokens;
       
    37 	TUint32 p1;
       
    38 	TUint32 p2;
       
    39 
       
    40 	TUint32* l = (TUint32*)LUT;
       
    41 	TUint32 b = 0x03020100;
       
    42 	TUint32 step = 0x04040404;
       
    43 	do
       
    44 		{
       
    45 		*l++ = b;
       
    46 		b += step;
       
    47 		}
       
    48 	while(b>step);
       
    49 
       
    50 	if(src>=srcEnd)
       
    51 		goto error;
       
    52 	numTokens = *src++;
       
    53 	if(numTokens)
       
    54 		{
       
    55 		if(src>=srcEnd)
       
    56 			goto error;
       
    57 		marker = *src++;
       
    58 		LUT0[marker] = (TUint8)~marker;
       
    59 
       
    60 		if(numTokens<32)
       
    61 			{
       
    62 			TUint8* tokenEnd = src+3*numTokens;
       
    63 			if(tokenEnd>srcEnd)
       
    64 				goto error;
       
    65 			do
       
    66 				{
       
    67 				TInt b = *src++;
       
    68 				TInt p1 = *src++;
       
    69 				TInt p2 = *src++;
       
    70 				LUT0[b] = (TUint8)p1;
       
    71 				LUT1[b] = (TUint8)p2;
       
    72 				}
       
    73 			while(src<tokenEnd);
       
    74 			}
       
    75 		else
       
    76 			{
       
    77 			TUint8* bitMask = src;
       
    78 			src += 32;
       
    79 			if(src>srcEnd)
       
    80 				goto error;
       
    81 			TInt b=0;
       
    82 			do
       
    83 				{
       
    84 				TUint8 mask = bitMask[b>>3];
       
    85 				if(mask&(1<<(b&7)))
       
    86 					{
       
    87 					if(src>=srcEnd)
       
    88 						goto error;
       
    89 					TInt p1 = *src++;
       
    90 					if(src>=srcEnd)
       
    91 						goto error;
       
    92 					TInt p2 = *src++;
       
    93 					LUT0[b] = (TUint8)p1;
       
    94 					LUT1[b] = (TUint8)p2;		
       
    95 					--numTokens;
       
    96 					}
       
    97 				++b;
       
    98 				}
       
    99 			while(b<0x100);
       
   100 			if(numTokens)
       
   101 				goto error;
       
   102 			}
       
   103 		}
       
   104 
       
   105 	if(src>=srcEnd)
       
   106 		goto error;
       
   107 	b = *src++;
       
   108 	if(dst>=dstEnd)
       
   109 		goto error;
       
   110 	p1 = LUT0[b];
       
   111 	if(p1!=b)
       
   112 		goto not_single;
       
   113 next:
       
   114 	if(src>=srcEnd)
       
   115 		goto done_s;
       
   116 	b = *src++;
       
   117 	*dst++ = (TUint8)p1;
       
   118 	if(dst>=dstEnd)
       
   119 		goto done_d;
       
   120 	p1 = LUT0[b];
       
   121 	if(p1==b)
       
   122 		goto next;
       
   123 
       
   124 not_single:
       
   125 	if(b==marker)
       
   126 		goto do_marker;
       
   127 
       
   128 do_pair:
       
   129 	p2 = LUT1[b];
       
   130 	b = p1;
       
   131 	p1 = LUT0[b];
       
   132 	if(sp<=stack)
       
   133 		goto error;
       
   134 	*--sp = (TUint8)p2;
       
   135 
       
   136 recurse:
       
   137 	if(b!=p1)
       
   138 		goto do_pair;
       
   139 
       
   140 	if(sp==stackStart)
       
   141 		goto next;
       
   142 	b = *sp++;
       
   143 	*dst++ = (TUint8)p1;
       
   144 	if(dst>=dstEnd)
       
   145 		goto error;
       
   146 	p1 = LUT0[b];
       
   147 	goto recurse;
       
   148 
       
   149 do_marker:
       
   150 	if(src>=srcEnd)
       
   151 		goto error;
       
   152 	p1 = *src++;
       
   153 	goto next;
       
   154 
       
   155 error:
       
   156 	srcNext = 0;
       
   157 	return KErrCorrupt;
       
   158 
       
   159 done_s:
       
   160 	*dst++ = (TUint8)p1;
       
   161 	srcNext = src;
       
   162 	return dst-dstStart;
       
   163 
       
   164 done_d:
       
   165 	--src;
       
   166 	srcNext = src;
       
   167 	return dst-dstStart;
       
   168 	}
       
   169 
       
   170