1 /*
2 BStone: A Source port of
3 Blake Stone: Aliens of Gold and Blake Stone: Planet Strike
4 
5 Copyright (c) 1992-2013 Apogee Entertainment, LLC
6 Copyright (c) 2013-2015 Boris I. Bendovsky (bibendovsky@hotmail.com)
7 
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
12 
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the
20 Free Software Foundation, Inc.,
21 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 */
23 
24 
25 #include "id_heads.h"
26 #include "jm_io.h"
27 #include "jm_lzh.h"
28 
29 
IO_LoadFile(const char * filename,void ** dst)30 int IO_LoadFile(
31     const char* filename,
32     void** dst)
33 {
34     char buffer[5] = { 0, 0, 0, 0, 0 };
35     bstone::FileStream handle;
36     int32_t size = 0;
37 
38     handle.open(filename);
39 
40     if (!handle.is_open()) {
41         return size;
42     }
43 
44     handle.read(buffer, 4);
45 
46     if (!strcmp(buffer, "JAMP")) {
47         struct JAMPHeader head;
48 
49         handle.read(&head, sizeof(head));
50         size = head.OriginalLen;
51         switch (head.CompType) {
52         case ct_LZH:
53             LZH_Startup();
54 
55             *dst = new char[head.OriginalLen];
56 
57             {
58                 std::unique_ptr<uint8_t> compressed_buffer(
59                     new uint8_t[head.CompressLen]);
60 
61                 ::LZH_Decompress(compressed_buffer.get(), *dst,
62                                  size, head.CompressLen);
63             }
64 
65             LZH_Shutdown();
66             break;
67 
68         case ct_LZW:
69             ::Quit("No code for LZW compression.");
70             break;
71 
72         default:
73             ::Quit("Unknown compression type.");
74             break;
75         }
76     } else {
77         handle.set_position(0);
78         size = static_cast<int32_t>(handle.get_size());
79         *dst = new char[size];
80         if (handle.read(*dst, size) != size) {
81             return size;
82         }
83     }
84 
85     return size;
86 }
87