view unrar_exec.c @ 27518:e54c9b7eb0d8

Revert bad changes to SSA/ASS subtitle packet format The following commits are reverted partially or completely: "a valid ASS line contains 9 ',' before actual text" "demux_mkv: output correctly formated ASS packets" "libass: add a new ass_process_data() to process demuxed subtitle packets" These commits converted the internal representation of SSA/ASS subtitle packets from the format used by Matroska to a custom format where each packet has contents exactly matching one line in complete SSA script files. AFAIK no files natively use such a format for muxed subtitles. The stated reason for this change was to use a format that could in principle be muxed into a maximal number of containers. SSA subtitles do not have an implicit duration so both start time and duration or end time need to be specified explicitly; the new format moved timing information inside the codec packet data so it could be muxed without modification into containers that can represent only start time at the container level. However such a change is wrong from the viewpoint of program architecture. Timing information belongs to the demuxer level, but these commits moved not only the duration but also the authoritative value of the start time to inside the codec data. Additionally the new format lost the value of the Matroska ReadOrder field which is used by MPlayer. This commit changes the internal packet format back to that used by Matroska and makes the internal Matroska demuxer output that format again. Libavformat still outputs the "new" format; it could be converted back to the Matroska format in demux_lavf.c, but I'm not adding that code at least yet. The current lavf code has similar problems as the reverted code in MPlayer, and it also currently fails to provide any way to access the value of the ReadOrder field. I hope that the lavf side will be improved; if it isn't conversion can be added later. For now I'll make MPlayer default to the internal Matroska demuxer instead of the lavf one in a separate commit.
author uau
date Mon, 08 Sep 2008 21:26:22 +0000
parents 0f42fb42843c
children 32725ca88fed
line wrap: on
line source

/*
 * List files and extract file from rars by using external executable unrar.
 *
 * Copyright (C) 2005 Jindrich Makovicka <makovick gmail com>
 * Copyright (C) 2007 Ulion <ulion2002 gmail com>
 *
 * This file is part of MPlayer.
 *
 * MPlayer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * MPlayer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <locale.h>
#include "unrar_exec.h"

#include "mp_msg.h"

#define UNRAR_LIST 1
#define UNRAR_EXTRACT 2

char* unrar_executable = NULL;

static FILE* launch_pipe(pid_t *apid, const char *executable, int action,
                         const char *archive, const char *filename)
{
  if (!executable || access(executable, R_OK | X_OK)) return NULL;
  if (access(archive, R_OK)) return NULL;
  {
    int mypipe[2];
    pid_t pid;

    if (pipe(mypipe)) {
        mp_msg(MSGT_GLOBAL, MSGL_ERR, "UnRAR: Cannot create pipe.\n");
        return NULL;
    }

    pid = fork();
    if (pid == 0) {
        /* This is the child process. Execute the unrar executable. */
        close(mypipe[0]);
        // Close MPlayer's stdin, stdout and stderr so the unrar binary
        // can not mess them up.
        // TODO: Close all other files except the pipe.
        close(0); close(1); close(2);
        // Assign new stdin, stdout and stderr and check they actually got the
        // right descriptors.
        if (open("/dev/null", O_RDONLY) != 0 || dup(mypipe[1]) != 1
                || open("/dev/null", O_WRONLY) != 2)
            _exit(EXIT_FAILURE);
        if (action == UNRAR_LIST)
            execl(executable, executable, "v", archive, NULL);
        else if (action == UNRAR_EXTRACT)
            execl(executable, executable, "p", "-inul", "-p-",
                  archive,filename,NULL);
        mp_msg(MSGT_GLOBAL, MSGL_ERR, "UnRAR: Cannot execute %s\n", executable);
        _exit(EXIT_FAILURE);
    }
    if (pid < 0) {
        /* The fork failed.  Report failure.  */
        mp_msg(MSGT_GLOBAL, MSGL_ERR, "UnRAR: Fork failed\n");
        return NULL;
    }
    /* This is the parent process. Prepare the pipe stream. */
    close(mypipe[1]);
    *apid = pid;
    if (action == UNRAR_LIST)
        mp_msg(MSGT_GLOBAL, MSGL_V,
               "UnRAR: call unrar with command line: %s v %s\n",
               executable, archive);
    else if (action == UNRAR_EXTRACT)
        mp_msg(MSGT_GLOBAL, MSGL_V,
               "UnRAR: call unrar with command line: %s p -inul -p- %s %s\n",
               executable, archive, filename);
    return fdopen(mypipe[0], "r");
  }
}

#define ALLOC_INCR 1 * 1024 * 1024
int unrar_exec_get(unsigned char **output, unsigned long *size,
                   const char *filename, const char *rarfile)
{
    int bufsize = ALLOC_INCR, bytesread;
    pid_t pid;
    int status = 0;
    FILE *rar_pipe;

    rar_pipe=launch_pipe(&pid,unrar_executable,UNRAR_EXTRACT,rarfile,filename);
    if (!rar_pipe) return 0;

    *size = 0;

    *output = malloc(bufsize);

    while (*output) {
        bytesread=fread(*output+*size, 1, bufsize-*size, rar_pipe);
        if (bytesread <= 0)
            break;
        *size += bytesread;
        if (*size == bufsize) {
            char *p;
            bufsize += ALLOC_INCR;
            p = realloc(*output, bufsize);
            if (!p)
                free(*output);
            *output = p;
        }
    }
    fclose(rar_pipe);
    pid = waitpid(pid, &status, 0);
    if (!*output || !*size || (pid == -1 && errno != ECHILD) ||
            (pid > 0 && status)) {
        free(*output);
        *output = NULL;
        *size = 0;
        return 0;
    }
    if (bufsize > *size) {
        char *p = realloc(*output, *size);
        if (p)
            *output = p;
    }
    mp_msg(MSGT_GLOBAL, MSGL_V, "UnRAR: got file %s len %lu\n", filename,*size);
    return 1;
}

#define PARSE_NAME 0
#define PARSE_PROPS 1

int unrar_exec_list(const char *rarfile, ArchiveList_struct **list)
{
    char buf[1024], fname[1024];
    char *p;
    pid_t pid;
    int status = 0, file_num = -1, ignore_next_line = 0, state = PARSE_NAME;
    FILE *rar_pipe;
    ArchiveList_struct *alist = NULL, *current = NULL, *new;

    rar_pipe = launch_pipe(&pid, unrar_executable, UNRAR_LIST, rarfile, NULL);
    if (!rar_pipe) return -1;
    while (fgets(buf, sizeof(buf), rar_pipe)) {
        int packsize, unpsize, ratio, day, month, year, hour, min;
        int llen = strlen(buf);
        // If read nothing, we got a file_num -1.
        if (file_num == -1)
            file_num = 0;
        if (buf[llen-1] != '\n')
            // The line is too long, ignore it.
            ignore_next_line = 2;
        if (ignore_next_line) {
            --ignore_next_line;
            state = PARSE_NAME;
            continue;
        }
        // Trim the line.
        while (llen > 0 && strchr(" \t\n\r\v\f", buf[llen-1]))
            --llen;
        buf[llen] = '\0';
        p = buf;
        while (*p && strchr(" \t\n\r\v\f", *p))
            ++p;
        if (!*p) {
            state = PARSE_NAME;
            continue;
        }

        if (state == PARSE_PROPS && sscanf(p, "%d %d %d%% %d-%d-%d %d:%d",
                                           &unpsize, &packsize, &ratio, &day,
                                           &month, &year, &hour, &min) == 8) {
            new = calloc(1, sizeof(ArchiveList_struct));
            if (!new) {
                file_num = -1;
                break;
            }
            if (!current)
                alist = new;
            else
                current->next = new;
            current = new;
            current->item.Name = strdup(fname);
            state = PARSE_NAME;
            if (!current->item.Name) {
                file_num = -1;
                break;
            }
            current->item.PackSize = packsize;
            current->item.UnpSize = unpsize;
            ++file_num;
            continue;
        }
        strcpy(fname, p);
        state = PARSE_PROPS;
    }
    fclose(rar_pipe);
    pid = waitpid(pid, &status, 0);
    if (file_num < 0 || (pid == -1 && errno != ECHILD) ||
            (pid > 0 && status)) {
        unrar_exec_freelist(alist);
        return -1;
    }
    if (!alist)
        return -1;
    *list = alist;
    mp_msg(MSGT_GLOBAL, MSGL_V, "UnRAR: list got %d files\n", file_num);
    return file_num;
}

void unrar_exec_freelist(ArchiveList_struct *list)
{
    ArchiveList_struct* tmp;

    while (list) {
        tmp = list->next;
        free(list->item.Name);
        free(list);
        list = tmp;
    }
}