~ubuntu-branches/debian/wheezy/cuneiform/wheezy

« back to all changes in this revision

Viewing changes to cuneiform_src/Kern/cpage/sources/cpp/compress.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Baumann
  • Date: 2009-07-10 17:58:10 UTC
  • Revision ID: james.westby@ubuntu.com-20090710175810-rqc89d2i3tki9m89
Tags: upstream-0.7.0+dfsg
Import upstream version 0.7.0+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (c) 1993-2008, Cognitive Technologies
 
3
All rights reserved.
 
4
 
 
5
����������� ��������� ��������������� � ������������� ��� � ���� ��������� ����,
 
6
��� � � �������� �����, � ����������� ��� ���, ��� ���������� ��������� �������:
 
7
 
 
8
      * ��� ��������� ��������������� ��������� ���� ������ ���������� ���������
 
9
        ���� ����������� �� ��������� �����, ���� ������ ������� � �����������
 
10
        ����� �� ��������.
 
11
      * ��� ��������� ��������������� ��������� ���� � ������������ �/��� �
 
12
        ������ ����������, ������������ ��� ���������������, ������ �����������
 
13
        ��������� ���� ���������� �� ��������� �����, ���� ������ ������� �
 
14
        ����������� ����� �� ��������.
 
15
      * �� �������� Cognitive Technologies, �� ����� �� ����������� �� �����
 
16
        ���� ������������ � �������� �������� ��������� �/��� �����������
 
17
        ���������, ���������� �� ���� ��, ��� ���������������� �����������
 
18
        ����������.
 
19
 
 
20
��� ��������� ������������� ����������� ��������� ���� �/��� ������� ������ "���
 
21
��� ����" ��� ������-���� ���� ��������, ���������� ���� ��� ���������������,
 
22
������� �������� ������������ �������� � ����������� ��� ���������� ����, �� ��
 
23
������������� ���. �� �������� ��������� ���� � �� ���� ������ ����, �������
 
24
����� �������� �/��� �������� �������������� ���������, �� � ���� ������ ��
 
25
��Ѩ� ���������������, ������� ����� �����, ���������, ����������� ���
 
26
������������� ������, ��������� � �������������� ��� ���������� ����������
 
27
������������� ������������� ��������� (������� ������ ������, ��� ������,
 
28
������� ���������, ��� ������ �/��� ������ �������, ���������� ��-�� ��������
 
29
������� ��� �/��� ������ ��������� �������� ��������� � ������� �����������,
 
30
�� �� ������������� ����� ��������), �� �� ������������� ���, ���� ���� �����
 
31
�������� ��� ������ ���� ���� �������� � ����������� ����� ������� � ������.
 
32
 
 
33
Redistribution and use in source and binary forms, with or without modification,
 
34
are permitted provided that the following conditions are met:
 
35
 
 
36
    * Redistributions of source code must retain the above copyright notice,
 
37
      this list of conditions and the following disclaimer.
 
38
    * Redistributions in binary form must reproduce the above copyright notice,
 
39
      this list of conditions and the following disclaimer in the documentation
 
40
      and/or other materials provided with the distribution.
 
41
    * Neither the name of the Cognitive Technologies nor the names of its
 
42
      contributors may be used to endorse or promote products derived from this
 
43
      software without specific prior written permission.
 
44
 
 
45
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 
46
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 
47
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 
48
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
 
49
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 
50
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 
51
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 
52
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 
53
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
54
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
55
*/
 
56
 
 
57
#include "globus.h"
 
58
#include "cpage.h"
 
59
#include "backup.h"
 
60
#include "polyblock.h"
 
61
#include "resource.h"
 
62
 
 
63
void CleanData(Handle Type,void * lpData,Word32 Size)
 
64
// �������������� ����� �������� ��������� ������ ��� ������ ��������
 
65
{
 
66
    if(Type == TYPE_TEXT || Type == TYPE_IMAGE)
 
67
        {
 
68
                POLY_ *poly = (POLY_ *)lpData;
 
69
                COMMON *com = &poly->com;
 
70
                int maxnum=MaxNum;
 
71
                memset(&com->Vertex[com->count],0,(char*)&com->Vertex[maxnum]-(char*)&com->Vertex[com->count]);
 
72
                memset(&poly->mark[com->count],0,(char*)&poly->mark[maxnum]-(char*)&poly->mark[com->count]);
 
73
        }
 
74
        else
 
75
    if(Type == TYPE_TABLE)
 
76
        {
 
77
                TABLE_ *table = (TABLE_ *)lpData;
 
78
                COMMON *com = &table->com;
 
79
                memset(&com->Vertex[com->count],0,(char*)&com->Vertex[MaxNum]-(char*)&com->Vertex[com->count]);
 
80
                Int32 *line=table->LineY;
 
81
                memset(&line[table->num_rows+1],0,(char*)&line[MaxHorLines-1]-(char*)&line[table->num_rows+1]);
 
82
                line=table->LineX;
 
83
                memset(&line[table->num_colons+1],0,(char*)&line[MaxVerLines-1]-(char*)&line[table->num_colons+1]);
 
84
                for (Int32 j=0; j<MaxVerLines; j++)
 
85
                        for (Int32 i=0; i<MaxHorLines; i++)
 
86
                                if (i>=table->num_rows || j>=table->num_colons)
 
87
                                        table->Visible[i][j][0]=table->Visible[i][j][1]=table->TypeCell[i][j]=0;
 
88
        }
 
89
}
 
90
 
 
91
Bool32 ComplianceVersions(Handle Type, char ** lpData, Word32 *Size)
 
92
//���������� ������ ����� ������ � �������� ���������;
 
93
// ���� ��������� ������ - ����������� ������, ���� ������ - ������
 
94
{
 
95
        Word32 NewSize=*Size;
 
96
        if (Type==TYPE_TEXT  ||  Type==TYPE_IMAGE)
 
97
                 NewSize = sizeof(POLY_);
 
98
        else
 
99
        if (Type==TYPE_TABLE)
 
100
                 NewSize = sizeof(TABLE_);
 
101
        if (NewSize < *Size)
 
102
                return FALSE;
 
103
        if (NewSize > *Size)
 
104
        {
 
105
                char *lpNewData = new char[NewSize];
 
106
                if (!lpNewData)
 
107
                        return FALSE;
 
108
                memcpy(lpNewData,*lpData,*Size);
 
109
                memset(lpNewData+(*Size),0,NewSize-(*Size));
 
110
                delete *lpData;
 
111
                *lpData=lpNewData;      *Size=NewSize;
 
112
        }
 
113
        return TRUE;
 
114
}
 
115
 
 
116
Bool32 Compress(char * lpData, Word32 Size, char ** compressedData, Word32 * compressedSize)
 
117
{
 
118
// �������� ������ �� �� ����� MIN_REPEAT ���������� �������� �� ������� ����������
 
119
#define MIN_REPEAT 2*sizeof(CompressHeader)
 
120
 
 
121
        if (Size==0)
 
122
                return FALSE;
 
123
 
 
124
   char *newData = new char[Size+sizeof(CompressHeader)]; //������ ������ �����, ���� ��������� �� ����������,
 
125
   if (!newData)                                                                                  //����� - �� ������ ���������
 
126
           return FALSE;
 
127
   char *lpNewData = newData;
 
128
 
 
129
        // ������� ���� - ������� �������� � ��������, ����������� ����������� ���������;
 
130
        // ����� ��� ����������
 
131
        char * ordinary=lpData,   //������� ��������
 
132
                 * end=ordinary+Size;
 
133
        do
 
134
        {
 
135
                Word32 count=1;
 
136
                char * current=ordinary+1,
 
137
                         * repeating=ordinary; //��������, ����������� ����������� ���������;
 
138
                while (current<end)
 
139
                {
 
140
                        if (*current != *repeating)
 
141
                        {
 
142
                                if (current-repeating>=MIN_REPEAT)  break;
 
143
                                repeating=current;
 
144
                        }
 
145
                        current++;
 
146
                }
 
147
                count=current-repeating;
 
148
 
 
149
                if (count<MIN_REPEAT)  //����� �� �����, � ���������� ���� - ���������
 
150
                {
 
151
                        repeating += count;  count=0;
 
152
                }
 
153
                if (repeating>ordinary)  //������� ��������
 
154
                {
 
155
                        CompressHeader head={0};
 
156
                        head.bCompressed=FALSE;
 
157
                        head.wCount=repeating-ordinary;
 
158
                        memcpy(lpNewData,&head,sizeof(head));    lpNewData += sizeof(head);
 
159
                        memcpy(lpNewData,ordinary,head.wCount); lpNewData += head.wCount;
 
160
                }
 
161
                if (count)                               //��������, ����������� �������� (*repeated)
 
162
                {
 
163
                        CompressHeader head={0};
 
164
                        head.bCompressed=TRUE;
 
165
                        head.cRepeater=*repeating;
 
166
                        head.wCount=count;
 
167
                        memcpy(lpNewData,&head,sizeof(head));    lpNewData += sizeof(head);
 
168
                }
 
169
                ordinary=current;
 
170
        }
 
171
        while(ordinary<end);
 
172
        *compressedData = newData;  *compressedSize = lpNewData-newData;
 
173
        return TRUE;
 
174
}
 
175
 
 
176
//#################################
 
177
Bool32 Decompress(char * lpData, Word32 Size, char ** decomData, Word32 * decomSize)
 
178
{
 
179
        if (Size==0)
 
180
                return FALSE;
 
181
 
 
182
        char * old=lpData, *end=lpData+Size;
 
183
 
 
184
        //���������� ������ ����� ������������
 
185
        Word32 newSize=0;
 
186
        while (old<end)
 
187
        {
 
188
                CompressHeader *head = (CompressHeader *)old;
 
189
                old += sizeof(CompressHeader);
 
190
                if (!head->bCompressed) old += head->wCount;
 
191
                newSize += head->wCount;
 
192
        }
 
193
 
 
194
        //�������������
 
195
        char *newData = new char[newSize], *modern=newData;
 
196
        if (!newData)
 
197
                return FALSE;
 
198
 
 
199
        old=lpData;
 
200
        while (old<end)
 
201
        {
 
202
                CompressHeader *head = (CompressHeader *)old;
 
203
                old += sizeof(CompressHeader);
 
204
                if (head->bCompressed)
 
205
                        memset(modern,head->cRepeater,head->wCount);
 
206
                else
 
207
                {
 
208
                        memcpy(modern,old,head->wCount);
 
209
                        old += head->wCount;
 
210
                }
 
211
                modern += head->wCount;
 
212
        }
 
213
        *decomData=newData;  *decomSize=newSize;
 
214
        return TRUE;
 
215
}
 
216
 
 
217
 
 
218
 
 
219