1 /*****************************************************************************
2 * access.c: uncompressed RAR access
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 it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
28 #include <vlc_common.h>
29 #include <vlc_plugin.h>
30 #include <vlc_access.h>
31 #include <vlc_stream.h>
42 const rar_file_chunk_t *chunk;
45 static int Seek(access_t *access, uint64_t position)
47 access_sys_t *sys = access->p_sys;
48 const rar_file_t *file = sys->file;
50 if (position > file->real_size)
51 position = file->real_size;
53 /* Search the chunk */
54 const rar_file_chunk_t *old_chunk = sys->chunk;
55 for (int i = 0; i < file->chunk_count; i++) {
56 sys->chunk = file->chunk[i];
57 if (position < sys->chunk->cummulated_size + sys->chunk->size)
60 access->info.i_pos = position;
61 access->info.b_eof = false;
63 const uint64_t offset = sys->chunk->offset +
64 (position - sys->chunk->cummulated_size);
66 if (strcmp(old_chunk->mrl, sys->chunk->mrl)) {
68 stream_Delete(sys->s);
69 sys->s = stream_UrlNew(access, sys->chunk->mrl);
71 return sys->s ? stream_Seek(sys->s, offset) : VLC_EGENERIC;
74 static ssize_t Read(access_t *access, uint8_t *data, size_t size)
76 access_sys_t *sys = access->p_sys;
79 while (total < size) {
80 const uint64_t chunk_end = sys->chunk->cummulated_size + sys->chunk->size;
81 int max = __MIN(__MIN((int64_t)(size - total), (int64_t)(chunk_end - access->info.i_pos)), INT_MAX);
85 int r = sys->s ? stream_Read(sys->s, data, max) : -1;
92 access->info.i_pos += r;
93 if (access->info.i_pos >= chunk_end &&
94 Seek(access, access->info.i_pos))
97 if (size > 0 && total <= 0)
98 access->info.b_eof = true;
103 static int Control(access_t *access, int query, va_list args)
105 access_sys_t *sys = access->p_sys;
106 stream_t *s = sys->s;
111 case ACCESS_CAN_SEEK: {
112 bool *b = va_arg(args, bool *);
113 return stream_Control(s, STREAM_CAN_SEEK, b);
115 case ACCESS_CAN_FASTSEEK: {
116 bool *b = va_arg(args, bool *);
117 return stream_Control(s, STREAM_CAN_FASTSEEK, b);
119 /* FIXME the following request should ask the underlying access object */
120 case ACCESS_CAN_PAUSE:
121 case ACCESS_CAN_CONTROL_PACE: {
122 bool *b = va_arg(args, bool *);
126 case ACCESS_GET_SIZE:
127 *va_arg(args, uint64_t *) = sys->file->size;
129 case ACCESS_GET_PTS_DELAY: {
130 int64_t *delay = va_arg(args, int64_t *);
131 *delay = DEFAULT_PTS_DELAY;
134 case ACCESS_SET_PAUSE_STATE:
142 int RarAccessOpen(vlc_object_t *object)
144 access_t *access = (access_t*)object;
146 if (!strchr(access->psz_location, '|'))
149 char *base = strdup(access->psz_location);
152 char *name = strchr(base, '|');
156 stream_t *s = stream_UrlNew(access, base);
157 if (!s || RarProbe(s))
164 unsigned int i_nbvols;
165 } newscheme = { 0, NULL, 0 }, oldscheme = { 0, NULL, 0 }, *p_scheme;
167 if (RarParse(s, &newscheme.filescount, &newscheme.files, &newscheme.i_nbvols, false)
168 || newscheme.filescount < 1 || newscheme.i_nbvols < 2 )
170 /* We might want to lookup old naming scheme, could be a part1.rar,part1.r00 */
172 RarParse(s, &oldscheme.filescount, &oldscheme.files, &oldscheme.i_nbvols, true);
175 if (oldscheme.filescount >= newscheme.filescount && oldscheme.i_nbvols > newscheme.i_nbvols)
177 for (int i = 0; i < newscheme.filescount; i++)
178 RarFileDelete(newscheme.files[i]);
179 free(newscheme.files);
180 p_scheme = &oldscheme;
181 msg_Dbg(s, "using rar old naming for %d files nbvols %u", p_scheme->filescount, oldscheme.i_nbvols);
183 else if (newscheme.filescount)
185 for (int i = 0; i < oldscheme.filescount; i++)
186 RarFileDelete(oldscheme.files[i]);
187 free(oldscheme.files);
188 p_scheme = &newscheme;
189 msg_Dbg(s, "using rar new naming for %d files nbvols %u", p_scheme->filescount, oldscheme.i_nbvols);
193 msg_Info(s, "Invalid or unsupported RAR archive");
194 for (int i = 0; i < oldscheme.filescount; i++)
195 RarFileDelete(oldscheme.files[i]);
196 free(oldscheme.files);
197 for (int i = 0; i < newscheme.filescount; i++)
198 RarFileDelete(newscheme.files[i]);
199 free(newscheme.files);
203 rar_file_t *file = NULL;
204 for (int i = 0; i < p_scheme->filescount; i++) {
205 if (!file && !strcmp(p_scheme->files[i]->name, name))
206 file = p_scheme->files[i];
208 RarFileDelete(p_scheme->files[i]);
210 free(p_scheme->files);
214 access_sys_t *sys = access->p_sys = malloc(sizeof(*sys));
218 access->pf_read = Read;
219 access->pf_block = NULL;
220 access->pf_control = Control;
221 access->pf_seek = Seek;
223 access_InitFields(access);
225 rar_file_chunk_t dummy = {
241 void RarAccessClose(vlc_object_t *object)
243 access_t *access = (access_t*)object;
244 access_sys_t *sys = access->p_sys;
247 stream_Delete(sys->s);
248 RarFileDelete(sys->file);