Package: milkytracker / 0.90.86+dfsg-2

04_use-lhasa.patch Patch series | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
Description: Implement lha decompression using lhasa
 The original lha decompressor supplied with MilkyTracker is non-free, so
 reimplement the decompression module using the free lhasa library.
Author: James Cowgill <jcowgill@debian.org>
Forwarded: no
---
This patch header follows DEP-3: http://dep.debian.net/deps/dep3/
--- a/src/compression/Makefile.am
+++ b/src/compression/Makefile.am
@@ -1,17 +1,14 @@
 noinst_LIBRARIES = libcompression.a
 
 libcompression_a_SOURCES =  PP20.cpp ZipExtractor.cpp zziplib/MyIO.cpp \
-Decompressor.cpp DecompressorUMX.cpp DecompressorPP20.cpp lha/unlha.cpp \
+Decompressor.cpp DecompressorUMX.cpp DecompressorPP20.cpp \
 DecompressorZIP.cpp DecompressorLHA.cpp DecompressorGZIP.cpp \
 DecompressorLZX.cpp unlzx.cpp
 
 noinst_HEADERS =  PP20.h ZipExtractor.h zziplib/MyIO.h DecompressorLHA.h \
 DecompressorUMX.h DecompressorGZIP.h AIFFWriter.h Decompressor.h \
-DecompressorPP20.h DecompressorQT.h lha/lharc.h DecompressorZIP.h \
-DecompressorLZX.h unlzx.h \
-lha/dhuf.inl lha/extract.inl lha/header.inl lha/huf.inl lha/larc.inl \
-lha/lharc.h lha/lhext.inl lha/maketbl.inl lha/shuf.inl lha/slidehuf.h \
-lha/slide.inl lha/unlha32.h
+DecompressorPP20.h DecompressorQT.h DecompressorZIP.h \
+DecompressorLZX.h unlzx.h
 
 EXTRA_DIST = DecompressorQT.mm AIFFWriter.m zlib/generic/adler32.cpp \
 zlib/generic/compress.cpp zlib/generic/crc32.cpp zlib/generic/crc32.h \
@@ -27,6 +24,6 @@ zlib/generic/zutil.h
 
 AM_CPPFLAGS = -I$(top_srcdir)/src/milkyplay -I$(top_srcdir)/src/ppui \
 -I$(top_srcdir)/src/ppui/osinterface/posix \
--I$(top_srcdir)/src/compression/lha -I$(top_srcdir)/src/compression/zziplib
+-I$(top_srcdir)/src/compression/zziplib -I/usr/include/liblhasa-1.0
 
 #AM_CPPFLAGS = -I../ppui -I../ppui/osinterface -I../ppui/osinterface/posix
--- a/src/tracker/Makefile.am
+++ b/src/tracker/Makefile.am
@@ -67,8 +67,7 @@ $(top_builddir)/src/compression/Decompre
 $(top_builddir)/src/compression/DecompressorZIP.o \
 $(top_builddir)/src/compression/zziplib/MyIO.o \
 $(top_builddir)/src/compression/PP20.o \
-$(top_builddir)/src/compression/ZipExtractor.o \
-$(top_builddir)/src/compression/lha/unlha.o
+$(top_builddir)/src/compression/ZipExtractor.o
 
 # add -lGL for experimental OpenGL support (and see ../ppui/Makefile.am)
 milkytracker_LDADD = $(top_builddir)/src/milkyplay/libmilkyplay.a \
@@ -76,7 +75,7 @@ $(top_builddir)/src/ppui/osinterface/lib
 $(top_builddir)/src/ppui/libppui.a \
 $(top_builddir)/src/ppui/osinterface/libosinterface.a \
 $(top_builddir)/src/fx/libfx.a $(COMPRESSION_LIB) $(MIDI_LIB) \
-$(SDL_LIBS) $(ALSA_LIBS) $(Z_LIBS) $(RTMIDI_LIBS) $(ZZIP_LIBS)
+$(SDL_LIBS) $(ALSA_LIBS) $(Z_LIBS) $(RTMIDI_LIBS) $(ZZIP_LIBS) -llhasa
 
 EXTRA_DIST = carbon/Carbon_Definitions.h carbon/Carbon_main.cpp \
 carbon/InfoPlist.strings carbon/KeyTranslation.cpp carbon/KeyTranslation.h \
--- a/src/compression/DecompressorLHA.cpp
+++ b/src/compression/DecompressorLHA.cpp
@@ -28,72 +28,74 @@
  *
  */
 
+#include <lhasa.h>
+
 #include "DecompressorLHA.h"
 #include "XMFile.h"
 #include "XModule.h"
-#include "unlha32.h"
 
-// -- LHA --------------------------------------------------------------------
-class XMFileStreamer : public CLhaArchive::StreamerBase
+#define LHA_BUFFER_SIZE 0x10000
+
+namespace
 {
-private:
-	XMFile& f;
+	static int lha_read_callback(void *handle, void *buf, size_t buf_len)
+	{
+		return static_cast<XMFile*>(handle)->read(buf, 1, buf_len);
+	}
 
-	pp_uint32 currentPos;
+	static const LHAInputStreamType lha_callbacks =
+	{
+		lha_read_callback,
+		NULL,
+		NULL
+	};
 
-protected:
-	virtual pp_uint8 get(pp_uint32 pos)
+	// Simple wrapper for lha_reader_*
+	class LHAReaderWrapper
 	{
-		pp_uint8 result;
-		if (currentPos != pos)
+	public:
+		explicit LHAReaderWrapper(XMFile& file)
 		{
-			f.seek(pos);
-			result = f.readByte();
-			currentPos = f.pos();
+			// Open input stream
+			input_stream = lha_input_stream_new(&lha_callbacks, &file);
+			if (input_stream == NULL)
+				return;
+
+			// Open reader
+			reader = lha_reader_new(input_stream);
 		}
-		else
+
+		bool isOpen() const
 		{
-			result = f.readByte();
-			currentPos = f.pos();
+			return reader != NULL;
 		}
-		
-		return result;
-	}
 
-public:		
-	XMFileStreamer(XMFile& f) :
-		f(f)
-	{
-		currentPos = f.pos();
-	}
+		LHAFileHeader* nextFile()
+		{
+			return lha_reader_next_file(reader);
+		}
 
-	virtual void read(void* buffer, pp_uint32 from, pp_uint32 len)
-	{
-		if (currentPos != from)
+		size_t read(void* buf, size_t length)
 		{
-			f.seek(from);
-			f.read(buffer, 1, len);
-			currentPos = f.pos();
+			return lha_reader_read(reader, buf, length);
 		}
-		else
+
+		~LHAReaderWrapper()
 		{
-			f.read(buffer, 1, len);
-			currentPos = f.pos();
+			if (reader != NULL)
+				lha_reader_free(reader);
+			if (input_stream != NULL)
+				lha_input_stream_free(input_stream);
 		}
-	}
-};
 
-struct ModuleIdentifyNotifier : public CLhaArchive::IDNotifier 
-{
-	virtual bool identify(void* buffer, pp_uint32 len) const
-	{
-		mp_ubyte buff[XModule::IdentificationBufferSize];
-		memset(buff, 0, sizeof(buff));
-		memcpy(buff, buffer, len > sizeof(buff) ? sizeof(buff) : len);
-		
-		return XModule::identifyModule(buff) != NULL;
-	}
-};
+	private:
+		LHAInputStream* input_stream = NULL;
+		LHAReader* reader = NULL;
+
+		LHAReaderWrapper(const LHAReaderWrapper&) {}
+		LHAReaderWrapper& operator=(const LHAReaderWrapper&) {}
+	};
+}
 
 DecompressorLHA::DecompressorLHA(const PPSystemString& filename) :
 	DecompressorBase(filename)
@@ -102,15 +104,14 @@ DecompressorLHA::DecompressorLHA(const P
 
 bool DecompressorLHA::identify(XMFile& f)
 {
-	f.seek(0);	
-
-	XMFileStreamer streamer(f);
+	f.seek(0);
 
-	CLhaArchive archive(streamer, f.size(), NULL);
-
-	bool res = archive.IsArchive() != 0;
+	// Attempt to create the reader and read the header of the first file
+	LHAReaderWrapper reader(f);
+	if (!reader.isOpen())
+		return false;
 
-	return res;
+	return reader.nextFile() != NULL;
 }	
 	
 const PPSimpleVector<Descriptor>& DecompressorLHA::getDescriptors(Hints hint) const
@@ -127,31 +128,46 @@ bool DecompressorLHA::decompress(const P
 	if (!f.isOpen())
 		return false;
 
-	XMFileStreamer streamer(f);
-
-	ModuleIdentifyNotifier idnotifier;
-
-	CLhaArchive archive(streamer, f.size(), &idnotifier);
-
-	if (!archive.IsArchive())
+	// Create reader object
+	LHAReaderWrapper reader(f);
+	if (!reader.isOpen())
 		return false;
 
-	pp_uint32 result = archive.ExtractFile();
-	
-	if (result && archive.GetOutputFile())
+	// Loop through each file until we find a sutible module
+	while (1)
 	{
-		const char* id = XModule::identifyModule(archive.GetOutputFile());
-		if (id)
+		LHAFileHeader* header = reader.nextFile();
+		if (header == NULL)
+			break;
+
+		// Skip directories and symlinks
+		if (strcmp(header->compress_method, LHA_COMPRESS_TYPE_DIR) == 0)
+			continue;
+
+		// Identify the current file
+		mp_ubyte buf[LHA_BUFFER_SIZE];
+		memset(buf, 0, sizeof(buf));
+		size_t bytes_read = reader.read(buf, sizeof(buf));
+
+		if (bytes_read > 0 && XModule::identifyModule(buf) != NULL)
 		{
+			// Write to output file
 			XMFile outFile(outFilename, true);
 			if (!outFile.isOpenForWriting())
 				return false;
-				
-			outFile.write(archive.GetOutputFile(), 1, archive.GetOutputFileLength());						
-			return true;
+
+			// Decompress into outFile
+			do
+			{
+				outFile.write(buf, 1, bytes_read);
+			}
+			while ((bytes_read = reader.read(buf, sizeof(buf))) > 0);
+
+			return (bytes_read == 0);
 		}
 	}
-	
+
+	// No sutible modules found
 	return false;
 }