1 /*****************************************************************************
2 * rar.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
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 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_access.h>
34 #include <vlc_stream.h>
42 /*****************************************************************************
44 *****************************************************************************/
45 static int Open (vlc_object_t *);
46 static void Close(vlc_object_t *);
49 set_category(CAT_INPUT)
50 set_subcategory(SUBCAT_INPUT_STREAM_FILTER)
51 set_description(N_("Uncompressed RAR"))
52 set_capability("access", 0)
53 set_callbacks(Open, Close)
57 /*****************************************************************************
58 * Local definitions/prototypes
59 *****************************************************************************/
63 const rar_file_chunk_t *chunk;
66 static int Seek(access_t *access, uint64_t position)
68 access_sys_t *sys = access->p_sys;
69 const rar_file_t *file = sys->file;
71 if (position > file->real_size)
72 position = file->real_size;
74 /* Search the chunk */
75 for (int i = 0; i < file->chunk_count; i++) {
76 sys->chunk = file->chunk[i];
77 if (position < sys->chunk->cummulated_size + sys->chunk->size)
80 access->info.i_pos = position;
81 access->info.b_eof = false;
83 const uint64_t offset = sys->chunk->offset +
84 (position - sys->chunk->cummulated_size);
85 return stream_Seek(sys->s, offset);
88 static ssize_t Read(access_t *access, uint8_t *data, size_t size)
90 access_sys_t *sys = access->p_sys;
93 while (total < size) {
94 const uint64_t chunk_end = sys->chunk->cummulated_size + sys->chunk->size;
95 int max = __MIN(__MIN((int64_t)(size - total), (int64_t)(chunk_end - access->info.i_pos)), INT_MAX);
99 int r = stream_Read(sys->s, data, max);
106 access->info.i_pos += r;
107 if (access->info.i_pos >= chunk_end &&
108 Seek(access, access->info.i_pos))
111 if (size > 0 && total <= 0)
112 access->info.b_eof = true;
117 static int Control(access_t *access, int query, va_list args)
119 stream_t *s = access->p_sys->s;
121 case ACCESS_CAN_SEEK: {
122 bool *b = va_arg(args, bool *);
123 return stream_Control(s, STREAM_CAN_SEEK, b);
125 case ACCESS_CAN_FASTSEEK: {
126 bool *b = va_arg(args, bool *);
127 return stream_Control(s, STREAM_CAN_FASTSEEK, b);
129 /* FIXME the following request should ask the underlying access object */
130 case ACCESS_CAN_PAUSE:
131 case ACCESS_CAN_CONTROL_PACE: {
132 bool *b = va_arg(args, bool *);
136 case ACCESS_GET_PTS_DELAY: {
137 int64_t *delay = va_arg(args, int64_t *);
138 *delay = DEFAULT_PTS_DELAY;
141 case ACCESS_SET_PAUSE_STATE:
149 static int Open(vlc_object_t *object)
151 access_t *access = (access_t*)object;
153 if (!strchr(access->psz_location, '|'))
156 char *base = strdup(access->psz_location);
159 char *name = strchr(base, '|');
163 stream_t *s = stream_UrlNew(access, base);
168 if (RarProbe(s) || RarParse(s, &count, &files) || count <= 0)
170 rar_file_t *file = NULL;
171 for (int i = 0; i < count; i++) {
172 if (!file && !strcmp(files[i]->name, name))
175 RarFileDelete(files[i]);
181 access_sys_t *sys = access->p_sys = malloc(sizeof(*sys));
185 access->pf_read = Read;
186 access->pf_block = NULL;
187 access->pf_control = Control;
188 access->pf_seek = Seek;
190 access_InitFields(access);
191 access->info.i_size = file->size;
205 static void Close(vlc_object_t *object)
207 access_t *access = (access_t*)object;
208 access_sys_t *sys = access->p_sys;
210 stream_Delete(sys->s);
211 RarFileDelete(sys->file);