summaryrefslogtreecommitdiffstats
path: root/test/Unpack.java
blob: dc302ea4ad001c2b065e8253f5979c318f8af17f (plain)
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
/*
 * Unpack -- a completely non-object oriented utility...
 *
 */

import java.io.*;

class Unpack
{
	static final int IDPAKHEADER= (('K' << 24) + ('C' << 16) + ('A' << 8) + 'P');

	static int intSwap(int i)
	{
		int a, b, c, d;

		a= i & 255;
		b= (i >> 8) & 255;
		c= (i >> 16) & 255;
		d= (i >> 24) & 255;

		return (a << 24) + (b << 16) + (c << 8) + d;
	}

	static boolean patternMatch(String pattern, String s)
	{
		int index;
		int remaining;

		if (pattern.equals(s))
		{
			return true;
		}

		// fairly lame single wildcard matching
		index= pattern.indexOf('*');
		if (index == -1)
		{
			return false;
		}
		if (!pattern.regionMatches(0, s, 0, index))
		{
			return false;
		}

		index += 1; // skip the *
		remaining= pattern.length() - index;
		if (s.length() < remaining)
		{
			return false;
		}

		if (!pattern.regionMatches(index, s, s.length() - remaining, remaining))
		{
			return false;
		}

		return true;
	}

	static void usage()
	{
		System.out.println("Usage: unpack <packfile> <match> <basedir>");
		System.out.println("   or: unpack -list <packfile>");
		System.out.println("<match> may contain a single * wildcard");
		System.exit(1);
	}

	public static void main(String[] args)
	{
		int ident;
		int dirofs;
		int dirlen;
		int i;
		int numLumps;
		byte[] name= new byte[56];
		String nameString;
		int filepos;
		int filelen;
		RandomAccessFile readLump;
		DataInputStream directory;
		String pakName;
		String pattern;

		if (args.length == 2)
		{
			if (!args[0].equals("-list"))
			{
				usage();
			}
			pakName= args[1];
			pattern= null;
		}
		else if (args.length == 3)
		{
			pakName= args[0];
			pattern= args[1];
		}
		else
		{
			pakName= null;
			pattern= null;
			usage();
		}

		try
		{
			// one stream to read the directory
			directory= new DataInputStream(new FileInputStream(pakName));

			// another to read lumps
			readLump= new RandomAccessFile(pakName, "r");

			// read the header
			ident= intSwap(directory.readInt());
			dirofs= intSwap(directory.readInt());
			dirlen= intSwap(directory.readInt());

			if (ident != IDPAKHEADER)
			{
				System.out.println(pakName + " is not a pakfile.");
				System.exit(1);
			}

			// read the directory
			directory.skipBytes(dirofs - 12);
			numLumps= dirlen / 64;

			System.out.println(numLumps + " lumps in " + pakName);

			for (i= 0; i < numLumps; i++)
			{
				directory.readFully(name);
				filepos= intSwap(directory.readInt());
				filelen= intSwap(directory.readInt());

				nameString= new String(name);
				// chop to the first 0 byte
				nameString= nameString.substring(0, nameString.indexOf(0));

				if (pattern == null)
				{
					// listing mode
					System.out.println(nameString + " : " + filelen + "bytes");
				}
				else if (patternMatch(pattern, nameString))
				{
					File writeFile;
					DataOutputStream writeLump;
					byte[] buffer= new byte[filelen];
					StringBuffer fixedString;
					String finalName;
					int index;

					System.out.println("Unpaking " + nameString + " " + filelen + " bytes");

					// load the lump
					readLump.seek(filepos);
					readLump.readFully(buffer);

					// quake uses forward slashes, but java requires
					// they only by the host's seperator, which
					// varies from win to unix
					fixedString= new StringBuffer(args[2] + File.separator + nameString);
					for (index= 0; index < fixedString.length(); index++)
					{
						if (fixedString.charAt(index) == '/')
						{
							fixedString.setCharAt(index, File.separatorChar);
						}
					}
					finalName= fixedString.toString();

					index= finalName.lastIndexOf(File.separatorChar);
					if (index != -1)
					{
						String finalPath;
						File writePath;

						finalPath= finalName.substring(0, index);
						writePath= new File(finalPath);
						writePath.mkdirs();
					}

					writeFile= new File(finalName);
					writeLump= new DataOutputStream(new FileOutputStream(writeFile));
					writeLump.write(buffer);
					writeLump.close();

				}
			}

			readLump.close();
			directory.close();

		}
		catch (IOException e)
		{
			System.out.println(e.toString());
		}
	}

}