1 /* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 */
22
23 #include "bladerunner/decompress_lcw.h"
24
25 #include "common/util.h"
26
27 namespace BladeRunner {
28
decompress_lcw(uint8 * inBuf,uint32 inLen,uint8 * outBuf,uint32 outLen)29 uint32 decompress_lcw(uint8 *inBuf, uint32 inLen, uint8 *outBuf, uint32 outLen) {
30 int version = 1;
31 int count, i, color, pos, relpos;
32
33 uint8 *src = inBuf;
34 uint8 *dst = outBuf;
35 uint8 *outEnd = dst + outLen;
36
37 if (src[0] == 0) {
38 version = 2;
39 ++src;
40 }
41
42 while (src < inBuf + inLen && dst < outEnd && src[0] != 0x80) {
43 int out_remain = (int)(outEnd - dst);
44
45 if (src[0] == 0xff) { // 0b11111111
46 count = src[1] | (src[2] << 8);
47 pos = src[3] | (src[4] << 8);
48 src += 5;
49 count = MIN(count, out_remain);
50
51 if (version == 1) {
52 for (i = 0; i < count; ++i)
53 dst[i] = outBuf[i + pos];
54 } else {
55 for (i = 0; i < count; ++i)
56 dst[i] = *(dst + i - pos);
57 }
58 } else if (src[0] == 0xfe) { // 0b11111110
59 count = src[1] | (src[2] << 8);
60 color = src[3];
61 src += 4;
62 count = MIN(count, out_remain);
63
64 memset(dst, color, count);
65 } else if (src[0] >= 0xc0) { // 0b11??????
66 count = (src[0] & 0x3f) + 3;
67 pos = src[1] | (src[2] << 8);
68 src += 3;
69 count = MIN(count, out_remain);
70
71 if (version == 1) {
72 for (i = 0; i < count; ++i)
73 dst[i] = outBuf[i + pos];
74 } else {
75 for (i = 0; i < count; ++i)
76 dst[i] = *(dst + i - pos);
77 }
78 } else if (src[0] >= 0x80) { // 0b10??????
79 count = src[0] & 0x3f;
80 ++src;
81 count = MIN(count, out_remain);
82
83 memcpy(dst, src, count);
84 src += count;
85 } else { // 0b0???????
86 count = ((src[0] & 0x70) >> 4) + 3;
87 relpos = ((src[0] & 0x0f) << 8) | src[1];
88 src += 2;
89 count = MIN(count, out_remain);
90
91 for (i = 0; i < count; ++i) {
92 dst[i] = *(dst + i - relpos);
93 }
94 }
95
96 dst += count;
97 }
98
99 return uint32(dst - outBuf);
100 }
101
decompress_lcw_output_size(uint8 * inBuf,uint32 inLen)102 uint32 decompress_lcw_output_size(uint8 *inBuf, uint32 inLen) {
103 int count;
104 uint8 *src = inBuf;
105 uint32 outsize = 0;
106
107 if (src[0] == 0)
108 ++src;
109
110 while (src[0] != 0x80 && src < inBuf + inLen) {
111 if (src[0] == 0xff) { // 0b11111111
112 count = src[1] | (src[2] << 8);
113 src += 5;
114 } else if (src[0] == 0xfe) { // 0b11111110
115 count = src[1] | (src[2] << 8);
116 src += 4;
117 } else if (src[0] >= 0xc0) { // 0b11??????
118 count = (src[0] & 0x3f) + 3;
119 src += 3;
120 } else if (src[0] & 0x80) { // 0b10??????
121 count = src[0] & 0x3f;
122 src += count + 1;
123 } else { // 0b0???????
124 count = ((src[0] & 0x70) >> 4) + 3;
125 src += 2;
126 }
127
128 outsize += count;
129 }
130
131 return outsize;
132 }
133
134 } // End of namespace BladeRunner
135