static int parse_playlist(AppleHTTPContext *c, const char *url,

                          struct variant *var, AVIOContext *in)

{

    int ret = 0, duration = 0, is_segment = 0, is_variant = 0, bandwidth = 0;

    enum KeyType key_type = KEY_NONE;

    uint8_t iv[16] = "";

    int has_iv = 0;

    char key[MAX_URL_SIZE];

    char line[1024];

    const char *ptr;

    int close_in = 0;



    if (!in) {

        close_in = 1;

        if ((ret = avio_open2(&in, url, AVIO_FLAG_READ,

                              c->interrupt_callback, NULL)) < 0)

            return ret;

    }



    read_chomp_line(in, line, sizeof(line));

    if (strcmp(line, "#EXTM3U")) {

        ret = AVERROR_INVALIDDATA;

        goto fail;

    }



    if (var) {

        free_segment_list(var);

        var->finished = 0;

    }

    while (!in->eof_reached) {

        read_chomp_line(in, line, sizeof(line));

        if (av_strstart(line, "#EXT-X-STREAM-INF:", &ptr)) {

            struct variant_info info = {{0}};

            is_variant = 1;

            ff_parse_key_value(ptr, (ff_parse_key_val_cb) handle_variant_args,

                               &info);

            bandwidth = atoi(info.bandwidth);

        } else if (av_strstart(line, "#EXT-X-KEY:", &ptr)) {

            struct key_info info = {{0}};

            ff_parse_key_value(ptr, (ff_parse_key_val_cb) handle_key_args,

                               &info);

            key_type = KEY_NONE;

            has_iv = 0;

            if (!strcmp(info.method, "AES-128"))

                key_type = KEY_AES_128;

            if (!strncmp(info.iv, "0x", 2) || !strncmp(info.iv, "0X", 2)) {

                ff_hex_to_data(iv, info.iv + 2);

                has_iv = 1;

            }

            av_strlcpy(key, info.uri, sizeof(key));

        } else if (av_strstart(line, "#EXT-X-TARGETDURATION:", &ptr)) {

            if (!var) {

                var = new_variant(c, 0, url, NULL);

                if (!var) {

                    ret = AVERROR(ENOMEM);

                    goto fail;

                }

            }

            var->target_duration = atoi(ptr);

        } else if (av_strstart(line, "#EXT-X-MEDIA-SEQUENCE:", &ptr)) {

            if (!var) {

                var = new_variant(c, 0, url, NULL);

                if (!var) {

                    ret = AVERROR(ENOMEM);

                    goto fail;

                }

            }

            var->start_seq_no = atoi(ptr);

        } else if (av_strstart(line, "#EXT-X-ENDLIST", &ptr)) {

            if (var)

                var->finished = 1;

        } else if (av_strstart(line, "#EXTINF:", &ptr)) {

            is_segment = 1;

            duration   = atoi(ptr);

        } else if (av_strstart(line, "#", NULL)) {

            continue;

        } else if (line[0]) {

            if (is_variant) {

                if (!new_variant(c, bandwidth, line, url)) {

                    ret = AVERROR(ENOMEM);

                    goto fail;

                }

                is_variant = 0;

                bandwidth  = 0;

            }

            if (is_segment) {

                struct segment *seg;

                if (!var) {

                    var = new_variant(c, 0, url, NULL);

                    if (!var) {

                        ret = AVERROR(ENOMEM);

                        goto fail;

                    }

                }

                seg = av_malloc(sizeof(struct segment));

                if (!seg) {

                    ret = AVERROR(ENOMEM);

                    goto fail;

                }

                seg->duration = duration;

                seg->key_type = key_type;

                if (has_iv) {

                    memcpy(seg->iv, iv, sizeof(iv));

                } else {

                    int seq = var->start_seq_no + var->n_segments;

                    memset(seg->iv, 0, sizeof(seg->iv));

                    AV_WB32(seg->iv + 12, seq);

                }

                ff_make_absolute_url(seg->key, sizeof(seg->key), url, key);

                ff_make_absolute_url(seg->url, sizeof(seg->url), url, line);

                dynarray_add(&var->segments, &var->n_segments, seg);

                is_segment = 0;

            }

        }

    }

    if (var)

        var->last_load_time = av_gettime();



fail:

    if (close_in)

        avio_close(in);

    return ret;

}

static int parse_playlist(AppleHTTPContext *c, const char *url,

                          struct variant *var, AVIOContext *in)

{

    int ret = 0, duration = 0, is_segment = 0, is_variant = 0, bandwidth = 0;

    enum KeyType key_type = KEY_NONE;

    uint8_t iv[16] = "";

    int has_iv = 0;

    char key[MAX_URL_SIZE];

    char line[1024];

    const char *ptr;

    int close_in = 0;



    if (!in) {

        close_in = 1;

        if ((ret = avio_open2(&in, url, AVIO_FLAG_READ,

                              c->interrupt_callback, NULL)) < 0)

            return ret;

    }



    read_chomp_line(in, line, sizeof(line));

    if (strcmp(line, "#EXTM3U")) {

        ret = AVERROR_INVALIDDATA;

        goto fail;

    }



    if (var) {

        free_segment_list(var);

        var->finished = 0;

    }

    while (!in->eof_reached) {

        read_chomp_line(in, line, sizeof(line));

        if (av_strstart(line, "#EXT-X-STREAM-INF:", &ptr)) {

            struct variant_info info = {{0}};

            is_variant = 1;

            ff_parse_key_value(ptr, (ff_parse_key_val_cb) handle_variant_args,

                               &info);

            bandwidth = atoi(info.bandwidth);

        } else if (av_strstart(line, "#EXT-X-KEY:", &ptr)) {

            struct key_info info = {{0}};

            ff_parse_key_value(ptr, (ff_parse_key_val_cb) handle_key_args,

                               &info);

            key_type = KEY_NONE;

            has_iv = 0;

            if (!strcmp(info.method, "AES-128"))

                key_type = KEY_AES_128;

            if (!strncmp(info.iv, "0x", 2) || !strncmp(info.iv, "0X", 2)) {

                ff_hex_to_data(iv, info.iv + 2);

                has_iv = 1;

            }

            av_strlcpy(key, info.uri, sizeof(key));

        } else if (av_strstart(line, "#EXT-X-TARGETDURATION:", &ptr)) {

            if (!var) {

                var = new_variant(c, 0, url, NULL);

                if (!var) {

                    ret = AVERROR(ENOMEM);

                    goto fail;

                }

            }

            var->target_duration = atoi(ptr);

        } else if (av_strstart(line, "#EXT-X-MEDIA-SEQUENCE:", &ptr)) {

            if (!var) {

                var = new_variant(c, 0, url, NULL);

                if (!var) {

                    ret = AVERROR(ENOMEM);

                    goto fail;

                }

            }

            var->start_seq_no = atoi(ptr);

        } else if (av_strstart(line, "#EXT-X-ENDLIST", &ptr)) {

            if (var)

                var->finished = 1;

        } else if (av_strstart(line, "#EXTINF:", &ptr)) {

            is_segment = 1;

            duration   = atoi(ptr);

        } else if (av_strstart(line, "#", NULL)) {

            continue;

        } else if (line[0]) {

            if (is_variant) {

                if (!new_variant(c, bandwidth, line, url)) {

                    ret = AVERROR(ENOMEM);

                    goto fail;

                }

                is_variant = 0;

                bandwidth  = 0;

            }

            if (is_segment) {

                struct segment *seg;

                if (!var) {

                    var = new_variant(c, 0, url, NULL);

                    if (!var) {

                        ret = AVERROR(ENOMEM);

                        goto fail;

                    }

                }

                seg = av_malloc(sizeof(struct segment));

                if (!seg) {

                    ret = AVERROR(ENOMEM);

                    goto fail;

                }

                seg->duration = duration;

                seg->key_type = key_type;

                if (has_iv) {

                    memcpy(seg->iv, iv, sizeof(iv));

                } else {

                    int seq = var->start_seq_no + var->n_segments;

                    memset(seg->iv, 0, sizeof(seg->iv));

                    AV_WB32(seg->iv + 12, seq);

                }

                ff_make_absolute_url(seg->key, sizeof(seg->key), url, key);

                ff_make_absolute_url(seg->url, sizeof(seg->url), url, line);

                dynarray_add(&var->segments, &var->n_segments, seg);

                is_segment = 0;

            }

        }

    }

    if (var)

        var->last_load_time = av_gettime();



fail:

    if (close_in)

        avio_close(in);

    return ret;

}

    char key[MAX_URL_SIZE];
static int FUNC_0(AppleHTTPContext *VAR_0, const char *VAR_1,

                          struct variant *VAR_2, AVIOContext *VAR_3)

{

    int VAR_4 = 0, VAR_5 = 0, VAR_6 = 0, VAR_7 = 0, VAR_8 = 0;

    enum KeyType VAR_9 = KEY_NONE;

    uint8_t iv[16] = "";

    int VAR_10 = 0;

    char VAR_11[MAX_URL_SIZE];

    char VAR_12[1024];

    const char *VAR_13;

    int VAR_14 = 0;



    if (!VAR_3) {

        VAR_14 = 1;

        if ((VAR_4 = avio_open2(&VAR_3, VAR_1, AVIO_FLAG_READ,

                              VAR_0->interrupt_callback, NULL)) < 0)

            return VAR_4;

    }



    read_chomp_line(VAR_3, VAR_12, sizeof(VAR_12));

    if (strcmp(VAR_12, "#EXTM3U")) {

        VAR_4 = AVERROR_INVALIDDATA;

        goto fail;

    }



    if (VAR_2) {

        free_segment_list(VAR_2);

        VAR_2->finished = 0;

    }

    while (!VAR_3->eof_reached) {

        read_chomp_line(VAR_3, VAR_12, sizeof(VAR_12));

        if (av_strstart(VAR_12, "#EXT-X-STREAM-INF:", &VAR_13)) {

            struct variant_info VAR_16 = {{0}};

            VAR_7 = 1;

            ff_parse_key_value(VAR_13, (ff_parse_key_val_cb) handle_variant_args,

                               &VAR_16);

            VAR_8 = atoi(VAR_16.VAR_8);

        } else if (av_strstart(VAR_12, "#EXT-X-KEY:", &VAR_13)) {

            struct key_info VAR_16 = {{0}};

            ff_parse_key_value(VAR_13, (ff_parse_key_val_cb) handle_key_args,

                               &VAR_16);

            VAR_9 = KEY_NONE;

            VAR_10 = 0;

            if (!strcmp(VAR_16.method, "AES-128"))

                VAR_9 = KEY_AES_128;

            if (!strncmp(VAR_16.iv, "0x", 2) || !strncmp(VAR_16.iv, "0X", 2)) {

                ff_hex_to_data(iv, VAR_16.iv + 2);

                VAR_10 = 1;

            }

            av_strlcpy(VAR_11, VAR_16.uri, sizeof(VAR_11));

        } else if (av_strstart(VAR_12, "#EXT-X-TARGETDURATION:", &VAR_13)) {

            if (!VAR_2) {

                VAR_2 = new_variant(VAR_0, 0, VAR_1, NULL);

                if (!VAR_2) {

                    VAR_4 = AVERROR(ENOMEM);

                    goto fail;

                }

            }

            VAR_2->target_duration = atoi(VAR_13);

        } else if (av_strstart(VAR_12, "#EXT-X-MEDIA-SEQUENCE:", &VAR_13)) {

            if (!VAR_2) {

                VAR_2 = new_variant(VAR_0, 0, VAR_1, NULL);

                if (!VAR_2) {

                    VAR_4 = AVERROR(ENOMEM);

                    goto fail;

                }

            }

            VAR_2->start_seq_no = atoi(VAR_13);

        } else if (av_strstart(VAR_12, "#EXT-X-ENDLIST", &VAR_13)) {

            if (VAR_2)

                VAR_2->finished = 1;

        } else if (av_strstart(VAR_12, "#EXTINF:", &VAR_13)) {

            VAR_6 = 1;

            VAR_5   = atoi(VAR_13);

        } else if (av_strstart(VAR_12, "#", NULL)) {

            continue;

        } else if (VAR_12[0]) {

            if (VAR_7) {

                if (!new_variant(VAR_0, VAR_8, VAR_12, VAR_1)) {

                    VAR_4 = AVERROR(ENOMEM);

                    goto fail;

                }

                VAR_7 = 0;

                VAR_8  = 0;

            }

            if (VAR_6) {

                struct segment *VAR_16;

                if (!VAR_2) {

                    VAR_2 = new_variant(VAR_0, 0, VAR_1, NULL);

                    if (!VAR_2) {

                        VAR_4 = AVERROR(ENOMEM);

                        goto fail;

                    }

                }

                VAR_16 = av_malloc(sizeof(struct segment));

                if (!VAR_16) {

                    VAR_4 = AVERROR(ENOMEM);

                    goto fail;

                }

                VAR_16->VAR_5 = VAR_5;

                VAR_16->VAR_9 = VAR_9;

                if (VAR_10) {

                    memcpy(VAR_16->iv, iv, sizeof(iv));

                } else {

                    int VAR_17 = VAR_2->start_seq_no + VAR_2->n_segments;

                    memset(VAR_16->iv, 0, sizeof(VAR_16->iv));

                    AV_WB32(VAR_16->iv + 12, VAR_17);

                }

                ff_make_absolute_url(VAR_16->VAR_11, sizeof(VAR_16->VAR_11), VAR_1, VAR_11);

                ff_make_absolute_url(VAR_16->VAR_1, sizeof(VAR_16->VAR_1), VAR_1, VAR_12);

                dynarray_add(&VAR_2->segments, &VAR_2->n_segments, VAR_16);

                VAR_6 = 0;

            }

        }

    }

    if (VAR_2)

        VAR_2->last_load_time = av_gettime();



fail:

    if (VAR_14)

        avio_close(VAR_3);

    return VAR_4;

}

static int FUNC_0(AppleHTTPContext *VAR_0, const char *VAR_1,
struct variant *VAR_2, AVIOContext *VAR_3)
{ int VAR_4 = 0, VAR_5 = 0, VAR_6 = 0, VAR_7 = 0, VAR_8 = 0; enum KeyType VAR_9 = KEY_NONE; uint8_t iv[16] = ""; int VAR_10 = 0; char VAR_11[MAX_URL_SIZE]; char VAR_12[1024]; const char *VAR_13; int VAR_14 = 0; if (!VAR_3) { VAR_14 = 1; if ((VAR_4 = avio_open2(&VAR_3, VAR_1, AVIO_FLAG_READ,
VAR_0->interrupt_callback, NULL)) < 0)
return VAR_4; } read_chomp_line(VAR_3, VAR_12, sizeof(VAR_12)); if (strcmp(VAR_12, "#EXTM3U")) { VAR_4 = AVERROR_INVALIDDATA; goto fail; } if (VAR_2) { free_segment_list(VAR_2); VAR_2->finished = 0; } while (!VAR_3->eof_reached) { read_chomp_line(VAR_3, VAR_12, sizeof(VAR_12)); if (av_strstart(VAR_12, "#EXT-X-STREAM-INF:", &VAR_13)) { struct variant_info VAR_16 = {{0}}; VAR_7 = 1; ff_parse_key_value(VAR_13, (ff_parse_key_val_cb) handle_variant_args,
&VAR_16); VAR_8 = atoi(VAR_16.VAR_8); } else if (av_strstart(VAR_12, "#EXT-X-KEY:", &VAR_13)) { struct key_info VAR_16 = {{0}}; ff_parse_key_value(VAR_13, (ff_parse_key_val_cb) handle_key_args,
&VAR_16); VAR_9 = KEY_NONE; VAR_10 = 0; if (!strcmp(VAR_16.method, "AES-128"))
VAR_9 = KEY_AES_128; if (!strncmp(VAR_16.iv, "0x", 2) || !strncmp(VAR_16.iv, "0X", 2)) { ff_hex_to_data(iv, VAR_16.iv + 2); VAR_10 = 1; } av_strlcpy(VAR_11, VAR_16.uri, sizeof(VAR_11)); } else if (av_strstart(VAR_12, "#EXT-X-TARGETDURATION:", &VAR_13)) { if (!VAR_2) { VAR_2 = new_variant(VAR_0, 0, VAR_1, NULL); if (!VAR_2) { VAR_4 = AVERROR(ENOMEM); goto fail; } } VAR_2->target_duration = atoi(VAR_13); } else if (av_strstart(VAR_12, "#EXT-X-MEDIA-SEQUENCE:", &VAR_13)) { if (!VAR_2) { VAR_2 = new_variant(VAR_0, 0, VAR_1, NULL); if (!VAR_2) { VAR_4 = AVERROR(ENOMEM); goto fail; } } VAR_2->start_seq_no = atoi(VAR_13); } else if (av_strstart(VAR_12, "#EXT-X-ENDLIST", &VAR_13)) { if (VAR_2)
VAR_2->finished = 1; } else if (av_strstart(VAR_12, "#EXTINF:", &VAR_13)) { VAR_6 = 1; VAR_5   = atoi(VAR_13); } else if (av_strstart(VAR_12, "#", NULL)) { continue; } else if (VAR_12[0]) { if (VAR_7) { if (!new_variant(VAR_0, VAR_8, VAR_12, VAR_1)) { VAR_4 = AVERROR(ENOMEM); goto fail; } VAR_7 = 0; VAR_8  = 0; } if (VAR_6) { struct segment *VAR_16; if (!VAR_2) { VAR_2 = new_variant(VAR_0, 0, VAR_1, NULL); if (!VAR_2) { VAR_4 = AVERROR(ENOMEM); goto fail; } } VAR_16 = av_malloc(sizeof(struct segment)); if (!VAR_16) { VAR_4 = AVERROR(ENOMEM); goto fail; } VAR_16->VAR_5 = VAR_5; VAR_16->VAR_9 = VAR_9; if (VAR_10) { memcpy(VAR_16->iv, iv, sizeof(iv)); } else { int VAR_17 = VAR_2->start_seq_no + VAR_2->n_segments; memset(VAR_16->iv, 0, sizeof(VAR_16->iv)); AV_WB32(VAR_16->iv + 12, VAR_17); } ff_make_absolute_url(VAR_16->VAR_11, sizeof(VAR_16->VAR_11), VAR_1, VAR_11); ff_make_absolute_url(VAR_16->VAR_1, sizeof(VAR_16->VAR_1), VAR_1, VAR_12); dynarray_add(&VAR_2->segments, &VAR_2->n_segments, VAR_16); VAR_6 = 0; } } } if (VAR_2)
VAR_2->last_load_time = av_gettime(); fail:
if (VAR_14)
avio_close(VAR_3); return VAR_4; }