static int ftp_passive_mode(FTPContext *s)

{

    char *res = NULL, *start, *end;

    int i;

    const char *command = "PASV\r\n";

    const int pasv_codes[] = {227, 0};



    if (!ftp_send_command(s, command, pasv_codes, &res))

        goto fail;



    start = NULL;

    for (i = 0; i < strlen(res); ++i) {

        if (res[i] == '(') {

            start = res + i + 1;

        } else if (res[i] == ')') {

            end = res + i;

            break;

        }

    }

    if (!start || !end)

        goto fail;



    *end  = '\0';

    /* skip ip */

    if (!av_strtok(start, ",", &end)) goto fail;

    if (!av_strtok(end, ",", &end)) goto fail;

    if (!av_strtok(end, ",", &end)) goto fail;

    if (!av_strtok(end, ",", &end)) goto fail;



    /* parse port number */

    start = av_strtok(end, ",", &end);

    if (!start) goto fail;

    s->server_data_port = atoi(start) * 256;

    start = av_strtok(end, ",", &end);

    if (!start) goto fail;

    s->server_data_port += atoi(start);

    av_dlog(s, "Server data port: %d\n", s->server_data_port);



    av_free(res);

    return 0;



  fail:

    av_free(res);

    s->server_data_port = -1;

    return AVERROR(EIO);

}

static int ftp_passive_mode(FTPContext *s)

{

    char *res = NULL, *start, *end;

    int i;

    const char *command = "PASV\r\n";

    const int pasv_codes[] = {227, 0};



    if (!ftp_send_command(s, command, pasv_codes, &res))

        goto fail;



    start = NULL;

    for (i = 0; i < strlen(res); ++i) {

        if (res[i] == '(') {

            start = res + i + 1;

        } else if (res[i] == ')') {

            end = res + i;

            break;

        }

    }

    if (!start || !end)

        goto fail;



    *end  = '\0';

    

    if (!av_strtok(start, ",", &end)) goto fail;

    if (!av_strtok(end, ",", &end)) goto fail;

    if (!av_strtok(end, ",", &end)) goto fail;

    if (!av_strtok(end, ",", &end)) goto fail;



    

    start = av_strtok(end, ",", &end);

    if (!start) goto fail;

    s->server_data_port = atoi(start) * 256;

    start = av_strtok(end, ",", &end);

    if (!start) goto fail;

    s->server_data_port += atoi(start);

    av_dlog(s, "Server data port: %d\n", s->server_data_port);



    av_free(res);

    return 0;



  fail:

    av_free(res);

    s->server_data_port = -1;

    return AVERROR(EIO);

}

static int FUNC_0(FTPContext *VAR_0)

{

    char *VAR_1 = NULL, *VAR_2, *VAR_3;

    int VAR_4;

    const char *VAR_5 = "PASV\r\n";

    const int VAR_6[] = {227, 0};



    if (!ftp_send_command(VAR_0, VAR_5, VAR_6, &VAR_1))

        goto fail;



    VAR_2 = NULL;

    for (VAR_4 = 0; VAR_4 < strlen(VAR_1); ++VAR_4) {

        if (VAR_1[VAR_4] == '(') {

            VAR_2 = VAR_1 + VAR_4 + 1;

        } else if (VAR_1[VAR_4] == ')') {

            VAR_3 = VAR_1 + VAR_4;

            break;

        }

    }

    if (!VAR_2 || !VAR_3)

        goto fail;



    *VAR_3  = '\0';

    

    if (!av_strtok(VAR_2, ",", &VAR_3)) goto fail;

    if (!av_strtok(VAR_3, ",", &VAR_3)) goto fail;

    if (!av_strtok(VAR_3, ",", &VAR_3)) goto fail;

    if (!av_strtok(VAR_3, ",", &VAR_3)) goto fail;



    

    VAR_2 = av_strtok(VAR_3, ",", &VAR_3);

    if (!VAR_2) goto fail;

    VAR_0->server_data_port = atoi(VAR_2) * 256;

    VAR_2 = av_strtok(VAR_3, ",", &VAR_3);

    if (!VAR_2) goto fail;

    VAR_0->server_data_port += atoi(VAR_2);

    av_dlog(VAR_0, "Server data port: %d\n", VAR_0->server_data_port);



    av_free(VAR_1);

    return 0;



  fail:

    av_free(VAR_1);

    VAR_0->server_data_port = -1;

    return AVERROR(EIO);

}

static int FUNC_0(FTPContext *VAR_0)
{ char *VAR_1 = NULL, *VAR_2, *VAR_3; int VAR_4; const char *VAR_5 = "PASV\r\n"; const int VAR_6[] = {227, 0}; if (!ftp_send_command(VAR_0, VAR_5, VAR_6, &VAR_1))
goto fail; VAR_2 = NULL; for (VAR_4 = 0; VAR_4 < strlen(VAR_1); ++VAR_4) { if (VAR_1[VAR_4] == '(') { VAR_2 = VAR_1 + VAR_4 + 1; } else if (VAR_1[VAR_4] == ')') { VAR_3 = VAR_1 + VAR_4; break; } } if (!VAR_2 || !VAR_3)
goto fail; *VAR_3  = '\0'; if (!av_strtok(VAR_2, ",", &VAR_3)) goto fail; if (!av_strtok(VAR_3, ",", &VAR_3)) goto fail; if (!av_strtok(VAR_3, ",", &VAR_3)) goto fail; if (!av_strtok(VAR_3, ",", &VAR_3)) goto fail; VAR_2 = av_strtok(VAR_3, ",", &VAR_3); if (!VAR_2) goto fail; VAR_0->server_data_port = atoi(VAR_2) * 256; VAR_2 = av_strtok(VAR_3, ",", &VAR_3); if (!VAR_2) goto fail; VAR_0->server_data_port += atoi(VAR_2); av_dlog(VAR_0, "Server data port: %d\n", VAR_0->server_data_port); av_free(VAR_1); return 0; fail:
av_free(VAR_1); VAR_0->server_data_port = -1; return AVERROR(EIO); }