1 /*****************************************************************************
2 * rar.c: uncompressed RAR parser
3 *****************************************************************************
4 * Copyright (C) 2008-2010 Laurent Aimar
7 * Author: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_stream.h>
41 static const uint8_t rar_marker[] = {
42 0x52, 0x61, 0x72, 0x21, 0x1a, 0x07, 0x00
44 static const int rar_marker_size = sizeof(rar_marker);
46 void RarFileDelete(rar_file_t *file)
48 for (int i = 0; i < file->chunk_count; i++) {
49 free(file->chunk[i]->mrl);
66 RAR_BLOCK_MARKER = 0x72,
67 RAR_BLOCK_ARCHIVE = 0x73,
68 RAR_BLOCK_FILE = 0x74,
72 RAR_BLOCK_END_HAS_NEXT = 0x0001,
75 RAR_BLOCK_FILE_HAS_PREVIOUS = 0x0001,
76 RAR_BLOCK_FILE_HAS_NEXT = 0x0002,
77 RAR_BLOCK_FILE_HAS_HIGH = 0x0100,
80 static int PeekBlock(stream_t *s, rar_block_t *hdr)
83 int peek_size = stream_Peek(s, &peek, 11);
88 hdr->crc = GetWLE(&peek[0]);
90 hdr->flags = GetWLE(&peek[3]);
91 hdr->size = GetWLE(&peek[5]);
93 if (hdr->flags & 0x8000) {
96 hdr->add_size = GetDWLE(&peek[7]);
103 static int SkipBlock(stream_t *s, const rar_block_t *hdr)
105 uint64_t size = (uint64_t)hdr->size + hdr->add_size;
108 int skip = __MIN(size, INT_MAX);
109 if (stream_Read(s, NULL, skip) < skip)
117 static int IgnoreBlock(stream_t *s, int block)
121 if (PeekBlock(s, &bk) || bk.type != block)
123 return SkipBlock(s, &bk);
126 static int SkipEnd(stream_t *s, const rar_block_t *hdr)
128 if (!(hdr->flags & RAR_BLOCK_END_HAS_NEXT))
131 if (SkipBlock(s, hdr))
134 /* Now, we need to look for a marker block,
135 * It seems that there is garbage at EOF */
139 if (stream_Peek(s, &peek, rar_marker_size) < rar_marker_size)
142 if (!memcmp(peek, rar_marker, rar_marker_size))
145 if (stream_Read(s, NULL, 1) != 1)
149 /* Skip marker and archive blocks */
150 if (IgnoreBlock(s, RAR_BLOCK_MARKER))
152 if (IgnoreBlock(s, RAR_BLOCK_ARCHIVE))
158 static int SkipFile(stream_t *s, int *count, rar_file_t ***file,
159 const rar_block_t *hdr, const char *volume_mrl)
164 if (hdr->flags & RAR_BLOCK_FILE_HAS_HIGH)
166 if (hdr->size < (unsigned)min_size)
169 if (stream_Peek(s, &peek, min_size) < min_size)
173 uint32_t file_size_low = GetDWLE(&peek[7+4]);
174 uint8_t method = peek[7+18];
175 uint16_t name_size = GetWLE(&peek[7+19]);
176 uint32_t file_size_high = 0;
177 if (hdr->flags & RAR_BLOCK_FILE_HAS_HIGH)
178 file_size_high = GetDWLE(&peek[7+25]);
179 const uint64_t file_size = ((uint64_t)file_size_high << 32) | file_size_low;
181 char *name = calloc(1, name_size + 1);
185 const int name_offset = (hdr->flags & RAR_BLOCK_FILE_HAS_HIGH) ? (7+33) : (7+25);
186 if (name_offset + name_size <= hdr->size) {
187 const int max_size = name_offset + name_size;
188 if (stream_Peek(s, &peek, max_size) < max_size) {
192 memcpy(name, &peek[name_offset], name_size);
195 rar_file_t *current = NULL;
196 if (method != 0x30) {
197 msg_Warn(s, "Ignoring compressed file %s (method=0x%2.2x)", name, method);
203 current = (*file)[*count - 1];
206 (current->is_complete ||
207 strcmp(current->name, name) ||
208 (hdr->flags & RAR_BLOCK_FILE_HAS_PREVIOUS) == 0))
212 if (hdr->flags & RAR_BLOCK_FILE_HAS_PREVIOUS)
214 current = malloc(sizeof(*current));
217 TAB_APPEND(*count, *file, current);
219 current->name = name;
220 current->size = file_size;
221 current->is_complete = false;
222 current->real_size = 0;
223 TAB_INIT(current->chunk_count, current->chunk);
229 rar_file_chunk_t *chunk = malloc(sizeof(*chunk));
231 chunk->mrl = strdup(volume_mrl);
232 chunk->offset = stream_Tell(s) + hdr->size;
233 chunk->size = hdr->add_size;
234 chunk->cummulated_size = 0;
235 if (current->chunk_count > 0) {
236 rar_file_chunk_t *previous = current->chunk[current->chunk_count-1];
238 chunk->cummulated_size += previous->cummulated_size +
242 TAB_APPEND(current->chunk_count, current->chunk, chunk);
244 current->real_size += hdr->add_size;
246 if ((hdr->flags & RAR_BLOCK_FILE_HAS_NEXT) == 0)
247 current->is_complete = true;
253 /* We stop on the first non empty file if we cannot seek */
255 bool can_seek = false;
256 stream_Control(s, STREAM_CAN_SEEK, &can_seek);
257 if (!can_seek && current->size > 0)
261 if (SkipBlock(s, hdr))
266 int RarProbe(stream_t *s)
269 if (stream_Peek(s, &peek, rar_marker_size) < rar_marker_size)
271 if (memcmp(peek, rar_marker, rar_marker_size))
283 static const rar_pattern_t *FindVolumePattern(const char *location)
285 static const rar_pattern_t patterns[] = {
286 { ".part1.rar", "%s.part%.1d.rar", 2, 9 },
287 { ".part01.rar", "%s.part%.2d.rar", 2, 99, },
288 { ".part001.rar", "%s.part%.3d.rar", 2, 999 },
289 { ".rar", "%s.r%.2d", 0, 99 },
290 { NULL, NULL, 0, 0 },
293 const size_t location_size = strlen(location);
294 for (int i = 0; patterns[i].match != NULL; i++) {
295 const size_t match_size = strlen(patterns[i].match);
297 if (location_size < match_size)
299 if (!strcmp(&location[location_size - match_size], patterns[i].match))
305 int RarParse(stream_t *s, int *count, rar_file_t ***file)
310 const rar_pattern_t *pattern = FindVolumePattern(s->psz_path);
311 int volume_offset = 0;
314 if (asprintf(&volume_mrl, "%s://%s",
315 s->psz_access, s->psz_path) < 0)
320 /* Skip marker & archive */
321 if (IgnoreBlock(vol, RAR_BLOCK_MARKER) ||
322 IgnoreBlock(vol, RAR_BLOCK_ARCHIVE)) {
330 bool has_next = false;
335 if (PeekBlock(vol, &bk))
340 ret = SkipEnd(vol, &bk);
341 has_next = ret && (bk.flags & RAR_BLOCK_END_HAS_NEXT);
344 ret = SkipFile(vol, count, file, &bk, volume_mrl);
347 ret = SkipBlock(vol, &bk);
356 if (!has_next || !pattern ||
357 (*count > 0 && !(*file)[*count -1]->is_complete)) {
362 /* Open next volume */
363 const int volume_index = pattern->start + volume_offset++;
364 if (volume_index > pattern->stop) {
370 if (asprintf(&volume_base, "%s://%.*s",
372 (int)(strlen(s->psz_path) - strlen(pattern->match)), s->psz_path) < 0) {
378 if (asprintf(&volume_mrl, pattern->format, volume_base, volume_index) < 0)
384 vol = stream_UrlNew(s, volume_mrl);