1 /*
2  * List files and extract file from rars by using external executable unrar.
3  *
4  * Copyright (C) 2005 Jindrich Makovicka <makovick gmail com>
5  * Copyright (C) 2007 Ulion <ulion2002 gmail com>
6  *
7  * This file is part of MPlayer.
8  *
9  * MPlayer 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.
13  *
14  * MPlayer 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.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with MPlayer; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23 
24 #include <sys/wait.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <errno.h>
31 #include <locale.h>
32 #include "unrar_exec.h"
33 
34 #include "mp_msg.h"
35 
36 #define UNRAR_LIST 1
37 #define UNRAR_EXTRACT 2
38 
39 char* unrar_executable = NULL;
40 
launch_pipe(pid_t * apid,const char * executable,int action,const char * archive,const char * filename)41 static FILE* launch_pipe(pid_t *apid, const char *executable, int action,
42                          const char *archive, const char *filename)
43 {
44   if (!executable || access(executable, R_OK | X_OK)) return NULL;
45   if (access(archive, R_OK)) return NULL;
46   {
47     int mypipe[2];
48     pid_t pid;
49 
50     if (pipe(mypipe)) {
51         mp_msg(MSGT_GLOBAL, MSGL_ERR, "UnRAR: Cannot create pipe.\n");
52         return NULL;
53     }
54 
55     pid = fork();
56     if (pid == 0) {
57         /* This is the child process. Execute the unrar executable. */
58         close(mypipe[0]);
59         // Close MPlayer's stdin, stdout and stderr so the unrar binary
60         // can not mess them up.
61         // TODO: Close all other files except the pipe.
62         close(0); close(1); close(2);
63         // Assign new stdin, stdout and stderr and check they actually got the
64         // right descriptors.
65         if (open("/dev/null", O_RDONLY) != 0 || dup(mypipe[1]) != 1
66                 || open("/dev/null", O_WRONLY) != 2)
67             _exit(EXIT_FAILURE);
68         if (action == UNRAR_LIST)
69             execl(executable, executable, "v", archive, NULL);
70         else if (action == UNRAR_EXTRACT)
71             execl(executable, executable, "p", "-inul", "-p-",
72                   archive,filename,NULL);
73         mp_msg(MSGT_GLOBAL, MSGL_ERR, "UnRAR: Cannot execute %s\n", executable);
74         _exit(EXIT_FAILURE);
75     }
76     if (pid < 0) {
77         /* The fork failed.  Report failure.  */
78         mp_msg(MSGT_GLOBAL, MSGL_ERR, "UnRAR: Fork failed\n");
79         return NULL;
80     }
81     /* This is the parent process. Prepare the pipe stream. */
82     close(mypipe[1]);
83     *apid = pid;
84     if (action == UNRAR_LIST)
85         mp_msg(MSGT_GLOBAL, MSGL_V,
86                "UnRAR: call unrar with command line: %s v %s\n",
87                executable, archive);
88     else if (action == UNRAR_EXTRACT)
89         mp_msg(MSGT_GLOBAL, MSGL_V,
90                "UnRAR: call unrar with command line: %s p -inul -p- %s %s\n",
91                executable, archive, filename);
92     return fdopen(mypipe[0], "r");
93   }
94 }
95 
96 #define ALLOC_INCR 1 * 1024 * 1024
unrar_exec_get(unsigned char ** output,unsigned long * size,const char * filename,const char * rarfile)97 int unrar_exec_get(unsigned char **output, unsigned long *size,
98                    const char *filename, const char *rarfile)
99 {
100     int bufsize = ALLOC_INCR, bytesread;
101     pid_t pid;
102     int status = 0;
103     FILE *rar_pipe;
104 
105     rar_pipe=launch_pipe(&pid,unrar_executable,UNRAR_EXTRACT,rarfile,filename);
106     if (!rar_pipe) return 0;
107 
108     *size = 0;
109 
110     *output = malloc(bufsize);
111 
112     while (*output) {
113         bytesread=fread(*output+*size, 1, bufsize-*size, rar_pipe);
114         if (bytesread <= 0)
115             break;
116         *size += bytesread;
117         if (*size == bufsize) {
118             char *p;
119             if (bufsize >= 0x7fffffff - ALLOC_INCR)
120                 break;
121             bufsize += ALLOC_INCR;
122             p = realloc(*output, bufsize);
123             if (!p)
124                 free(*output);
125             *output = p;
126         }
127     }
128     fclose(rar_pipe);
129     pid = waitpid(pid, &status, 0);
130     if (!*output || !*size || (pid == -1 && errno != ECHILD) ||
131             (pid > 0 && status)) {
132         free(*output);
133         *output = NULL;
134         *size = 0;
135         return 0;
136     }
137     if (bufsize > *size) {
138         char *p = realloc(*output, *size);
139         if (p)
140             *output = p;
141     }
142     mp_msg(MSGT_GLOBAL, MSGL_V, "UnRAR: got file %s len %lu\n", filename,*size);
143     return 1;
144 }
145 
146 #define PARSE_NAME 0
147 #define PARSE_PROPS 1
148 
unrar_exec_list(const char * rarfile,ArchiveList_struct ** list)149 int unrar_exec_list(const char *rarfile, ArchiveList_struct **list)
150 {
151     char buf[1024], fname[1024];
152     char *p;
153     pid_t pid;
154     int status = 0, file_num = -1, ignore_next_line = 0, state = PARSE_NAME;
155     FILE *rar_pipe;
156     ArchiveList_struct *alist = NULL, *current = NULL, *new;
157 
158     rar_pipe = launch_pipe(&pid, unrar_executable, UNRAR_LIST, rarfile, NULL);
159     if (!rar_pipe) return -1;
160     while (fgets(buf, sizeof(buf), rar_pipe)) {
161         int packsize, unpsize, ratio, day, month, year, hour, min;
162         int llen = strlen(buf);
163         // If read nothing, we got a file_num -1.
164         if (file_num == -1)
165             file_num = 0;
166         if (buf[llen-1] != '\n')
167             // The line is too long, ignore it.
168             ignore_next_line = 2;
169         if (ignore_next_line) {
170             --ignore_next_line;
171             state = PARSE_NAME;
172             continue;
173         }
174         // Trim the line.
175         while (llen > 0 && strchr(" \t\n\r\v\f", buf[llen-1]))
176             --llen;
177         buf[llen] = '\0';
178         p = buf;
179         while (*p && strchr(" \t\n\r\v\f", *p))
180             ++p;
181         if (!*p) {
182             state = PARSE_NAME;
183             continue;
184         }
185 
186         if (state == PARSE_PROPS && sscanf(p, "%d %d %d%% %d-%d-%d %d:%d",
187                                            &unpsize, &packsize, &ratio, &day,
188                                            &month, &year, &hour, &min) == 8) {
189             new = calloc(1, sizeof(ArchiveList_struct));
190             if (!new) {
191                 file_num = -1;
192                 break;
193             }
194             if (!current)
195                 alist = new;
196             else
197                 current->next = new;
198             current = new;
199             current->item.Name = strdup(fname);
200             state = PARSE_NAME;
201             if (!current->item.Name) {
202                 file_num = -1;
203                 break;
204             }
205             current->item.PackSize = packsize;
206             current->item.UnpSize = unpsize;
207             ++file_num;
208             continue;
209         }
210         strcpy(fname, p);
211         state = PARSE_PROPS;
212     }
213     fclose(rar_pipe);
214     pid = waitpid(pid, &status, 0);
215     if (file_num < 0 || (pid == -1 && errno != ECHILD) ||
216             (pid > 0 && status)) {
217         unrar_exec_freelist(alist);
218         return -1;
219     }
220     if (!alist)
221         return -1;
222     *list = alist;
223     mp_msg(MSGT_GLOBAL, MSGL_V, "UnRAR: list got %d files\n", file_num);
224     return file_num;
225 }
226 
unrar_exec_freelist(ArchiveList_struct * list)227 void unrar_exec_freelist(ArchiveList_struct *list)
228 {
229     ArchiveList_struct* tmp;
230 
231     while (list) {
232         tmp = list->next;
233         free(list->item.Name);
234         free(list);
235         list = tmp;
236     }
237 }
238