1 /*****************************************************************************
2 * rar.h: 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++)
64 RAR_BLOCK_MARKER = 0x72,
65 RAR_BLOCK_ARCHIVE = 0x73,
66 RAR_BLOCK_FILE = 0x74,
70 RAR_BLOCK_END_HAS_NEXT = 0x0001,
73 RAR_BLOCK_FILE_HAS_PREVIOUS = 0x0001,
74 RAR_BLOCK_FILE_HAS_NEXT = 0x0002,
75 RAR_BLOCK_FILE_HAS_HIGH = 0x0100,
78 static int PeekBlock(stream_t *s, rar_block_t *hdr)
81 int peek_size = stream_Peek(s, &peek, 11);
86 hdr->crc = GetWLE(&peek[0]);
88 hdr->flags = GetWLE(&peek[3]);
89 hdr->size = GetWLE(&peek[5]);
91 if (hdr->flags & 0x8000) {
94 hdr->add_size = GetDWLE(&peek[7]);
101 static int SkipBlock(stream_t *s, const rar_block_t *hdr)
103 uint64_t size = (uint64_t)hdr->size + hdr->add_size;
106 int skip = __MIN(size, INT_MAX);
107 if (stream_Read(s, NULL, skip) < skip)
115 static int IgnoreBlock(stream_t *s, int block)
119 if (PeekBlock(s, &bk) || bk.type != block)
121 return SkipBlock(s, &bk);
124 static int SkipEnd(stream_t *s, const rar_block_t *hdr)
126 if (!(hdr->flags & RAR_BLOCK_END_HAS_NEXT))
129 if (SkipBlock(s, hdr))
132 /* Now, we need to look for a marker block,
133 * It seems that there is garbage at EOF */
137 if (stream_Peek(s, &peek, rar_marker_size) < rar_marker_size)
140 if (!memcmp(peek, rar_marker, rar_marker_size))
143 if (stream_Read(s, NULL, 1) != 1)
147 /* Skip marker and archive blocks */
148 if (IgnoreBlock(s, RAR_BLOCK_MARKER))
150 if (IgnoreBlock(s, RAR_BLOCK_ARCHIVE))
156 static int SkipFile(stream_t *s, int *count, rar_file_t ***file, const rar_block_t *hdr)
161 if (hdr->flags & RAR_BLOCK_FILE_HAS_HIGH)
163 if (hdr->size < (unsigned)min_size)
166 if (stream_Peek(s, &peek, min_size) < min_size)
170 uint32_t file_size_low = GetDWLE(&peek[7+4]);
171 uint8_t method = peek[7+18];
172 uint16_t name_size = GetWLE(&peek[7+19]);
173 uint32_t file_size_high = 0;
174 if (hdr->flags & RAR_BLOCK_FILE_HAS_HIGH)
175 file_size_high = GetDWLE(&peek[7+25]);
176 const uint64_t file_size = ((uint64_t)file_size_high << 32) | file_size_low;
178 char *name = calloc(1, name_size + 1);
182 const int name_offset = (hdr->flags & RAR_BLOCK_FILE_HAS_HIGH) ? (7+33) : (7+25);
183 if (name_offset + name_size <= hdr->size) {
184 const int max_size = name_offset + name_size;
185 if (stream_Peek(s, &peek, max_size) < max_size) {
189 memcpy(name, &peek[name_offset], name_size);
192 rar_file_t *current = NULL;
193 if (method != 0x30) {
194 msg_Warn(s, "Ignoring compressed file %s (method=0x%2.2x)", name, method);
200 current = (*file)[*count - 1];
203 (current->is_complete ||
204 current->size != file_size ||
205 strcmp(current->name, name) ||
206 (hdr->flags & RAR_BLOCK_FILE_HAS_PREVIOUS) == 0))
210 current = malloc(sizeof(*current));
213 TAB_APPEND(*count, *file, current);
215 current->name = name;
216 current->size = file_size;
217 current->is_complete = false;
218 current->real_size = 0;
219 TAB_INIT(current->chunk_count, current->chunk);
225 rar_file_chunk_t *chunk = malloc(sizeof(*chunk));
227 chunk->offset = stream_Tell(s) + hdr->size;
228 chunk->size = hdr->add_size;
229 chunk->cummulated_size = 0;
230 if (current->chunk_count > 0) {
231 rar_file_chunk_t *previous = current->chunk[current->chunk_count-1];
233 chunk->cummulated_size += previous->cummulated_size +
237 TAB_APPEND(current->chunk_count, current->chunk, chunk);
239 current->real_size += hdr->add_size;
241 if ((hdr->flags & RAR_BLOCK_FILE_HAS_NEXT) == 0)
242 current->is_complete = true;
248 /* We stop on the first non empty file if we cannot seek */
250 bool can_seek = false;
251 stream_Control(s, STREAM_CAN_SEEK, &can_seek);
252 if (!can_seek && current->size > 0)
256 if (SkipBlock(s, hdr))
261 int RarProbe(stream_t *s)
264 if (stream_Peek(s, &peek, rar_marker_size) < rar_marker_size)
266 if (memcmp(peek, rar_marker, rar_marker_size))
271 int RarParse(stream_t *s, int *count, rar_file_t ***file)
277 if (IgnoreBlock(s, RAR_BLOCK_MARKER))
281 if (IgnoreBlock(s, RAR_BLOCK_ARCHIVE))
289 if (PeekBlock(s, &bk))
294 ret = SkipEnd(s, &bk);
297 ret = SkipFile(s, count, file, &bk);
300 ret = SkipBlock(s, &bk);