File: arc_zip.c

package info (click to toggle)
timidity 2.9.1-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 5,464 kB
  • ctags: 10,525
  • sloc: ansic: 104,500; sh: 1,939; tcl: 1,045; makefile: 686; lisp: 499
file content (236 lines) | stat: -rw-r--r-- 5,389 bytes parent folder | 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
/*
    TiMidity++ -- MIDI to WAVE converter and player
    Copyright (C) 1999,2000 Masanao Izumo <mo@goice.co.jp>
    Copyright (C) 1995 Tuukka Toivonen <tt@cgs.fi>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
#include <stdio.h>
#include <stdlib.h>
#include "timidity.h"
#include "arc.h"

#define LOCSIG    0x04034b50
#define EXTLOCSIG 0x08074b50L

static unsigned short get_short(char *s)
{
    unsigned char *p = (unsigned char *)s;
    return ((unsigned short)p[0] |
	    (unsigned short)p[1]<<8);
}

static unsigned long get_long(char *s)
{
    unsigned char *p = (unsigned char *)s;
    return ((unsigned long)p[0] |
	    (unsigned long)p[1]<<8 |
	    (unsigned long)p[2]<<16 |
	    (unsigned long)p[3]<<24);
}

ArchiveEntryNode *next_zip_entry(void)
{
    unsigned long magic;
    unsigned short flen, elen, hdrsiz;
    URL url;
    long compsize, origsize;
    char buff[BUFSIZ];
    ArchiveEntryNode *entry;
    int method;
    unsigned short flags;
    int macbin_check;

    url = arc_handler.url;
    macbin_check = (arc_handler.counter == 0);

  retry_read:
    if(url_read(url, buff, 4) != 4)
	return NULL;

    hdrsiz = 4;

    magic = get_long(buff);
    if(magic == EXTLOCSIG)
    {
	/* ignored */
	if(url_read(url, buff, 20) != 20)
	    return NULL;
	magic = get_long(buff + 16);
	hdrsiz += 20;
    }
    else if(macbin_check && buff[0] == '0')
    {
	macbin_check = 0;
	url_skip(url, 128-4);
	if(arc_handler.isfile)
	    arc_handler.pos += 128;
	goto retry_read;
    }

    if(magic != LOCSIG)
	return NULL;

    /* Version needed to extract */
    url_skip(url, 2);
    hdrsiz += 2;

    /* General purpose bit flag */
    if(url_read(url, buff, 2) != 2)
	return NULL;
    flags = get_short(buff);
    hdrsiz += 2;

    /* Compression method */
    if(url_read(url, buff, 2) != 2)
	return NULL;
    method = get_short(buff);
    hdrsiz += 2;

    switch(method)
    {
      case 0: /* The file is stored (no compression) */
	method = ARCHIVEC_STORED;
	break;
      case 1: /* The file is Shrunk */
	method = ARCHIVEC_SHRUNKED;
	break;
      case 2: /* The file is Reduced with compression factor 1 */
	method = ARCHIVEC_REDUCED1;
	break;
      case 3: /* The file is Reduced with compression factor 2 */
	method = ARCHIVEC_REDUCED2;
	break;
      case 4: /* The file is Reduced with compression factor 3 */
	method = ARCHIVEC_REDUCED3;
	break;
      case 5: /* The file is Reduced with compression factor 4 */
	method = ARCHIVEC_REDUCED4;
	break;
      case 6: /* The file is Imploded */
	if(flags & 4)
	{
	    if(flags & 2)
		method = ARCHIVEC_IMPLODED_LIT8;
	    else
		method = ARCHIVEC_IMPLODED_LIT4;
	}
	else if(flags & 2)
	    method = ARCHIVEC_IMPLODED_NOLIT8;
	else
	    method = ARCHIVEC_IMPLODED_NOLIT4;
	break;
      case 7: /* Reserved for Tokenizing compression algorithm */
	method = -1;
	break;
      case 8: /* The file is Deflated */
	method = ARCHIVEC_DEFLATED;
	break;
      default:
	return NULL;
    }

    /* Last mod file time */
    url_skip(url, 2);
    hdrsiz += 2;

    /* Last mod file date */
    url_skip(url, 2);
    hdrsiz += 2;

    /* CRC-32 */
    url_skip(url, 4);
    hdrsiz += 4;

    /* Compressed size */
    if(url_read(url, buff, 4) != 4)
	return NULL;
    hdrsiz += 4;
    compsize = (long)get_long(buff);

    /* Uncompressed size */
    if(url_read(url, buff, 4) != 4)
	return NULL;
    hdrsiz += 4;
    origsize = (long)get_long(buff);

    /* Filename length */
    if(url_read(url, buff, 2) != 2)
	return NULL;
    hdrsiz += 2;
    flen = get_short(buff);
    if(flen >= sizeof(buff)-1)
	return NULL;

    /* Extra field length */
    if(url_read(url, buff, 2) != 2)
	return NULL;
    hdrsiz += 2;
    elen = get_short(buff);

    /* filename */
    if(url_read(url, buff, flen) != flen)
	return NULL;
    hdrsiz += flen;
    buff[flen] = '\0';

    if(compsize == 0 && flen > 0 &&
       (buff[flen - 1] == '/' || buff[flen - 1] == '\\'))
    {
	url_skip(url, elen);
	hdrsiz += elen;
	if(arc_handler.isfile)
	  arc_handler.pos += hdrsiz;
	goto retry_read;
    }

    entry = new_entry_node(buff, flen);
    if(entry == NULL)
	return NULL;

    entry->comptype = method;
    entry->origsize = origsize;
    entry->compsize = compsize;

    /* Extra field */
    url_skip(url, elen);
    hdrsiz += elen;

    if(arc_handler.isfile)
    {
      arc_handler.pos += hdrsiz;
	entry->start = arc_handler.pos;
	entry->cache = NULL;
	url_skip(url, compsize);
	arc_handler.pos += compsize;
    }
    else
    {
      long n;
      entry->start = 0;
      entry->cache = url_dump(url, compsize, &n);
      if(n != compsize)
	{
	  free_entry_node(entry);
	  return NULL;
	}
    }

    return entry;
}