<commit_before>#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include <time.h>
#include "curl.h"
#include "hls.h"

static char *get_rndstring(int length)
{
    int max;
    const char *letters = "abcdefghijklmnopqrstuvwxyz123456789";
    int i, r;
    srand((int)time(NULL));
    max = length;
    
    char *generated = (char*)malloc(max + 1);
    for(i = 0; i < max; i++) {
        r = rand() % strlen(letters);
        generated[i] = letters[r];
    }
    generated[max] = '\0';
    return generated;
}

int get_playlist_type(char *source)
{
    if (strncmp("#EXTM3U", source, 7) != 0) {
        fprintf(stderr, "Not a valid M3U8 file. Exiting.\n");
        return -1;
    }
    for (int i = 0; i < strlen(source); i++) {
        if(!strncmp(&source[i], "#EXT-X-STREAM-INF", 17)) {
            return 0;
        }
    }
    return 1;
}

static int extend_url(char **url, const char *baseurl)
{
    size_t max_length = strlen(*url) + strlen(baseurl) + 10;
    
    if (!strncmp(*url, "http://", 7) || !strncmp(*url, "https://", 8)) {
        return 0; //absolute url, nothing to do
    }
    
    else if (**url == '/') {
        char *domain = (char*)malloc(max_length);
        strcpy(domain, baseurl);
        if(!sscanf(baseurl, "http://%[^/]", domain)) {
            sscanf(baseurl, "https://%[^/]", domain);
        }
        
        char *buffer = (char*)malloc(max_length);
        snprintf(buffer, max_length, "%s%s", domain, *url);
        strcpy(*url, buffer);
        free(buffer);
        free(domain);
        return 0;
    }
    
    else {
        char *buffer = (char*)malloc(max_length);
        snprintf(buffer, max_length, "%s/../%s", baseurl, *url);
        strcpy(*url, buffer);
        free(buffer);
        return 0;
    }

}

static int parse_playlist_tag(struct hls_media_playlist *me, char *tag)
{
    if (!strncmp(tag, "#EXT-X-KEY:METHOD=AES-128", 25)) {
        me->encryption = true;
        me->encryptiontype = ENC_AES128;
        me->enc_aes.iv_is_static = false;
        
        char *link_to_key = (char*)malloc(strlen(tag) + strlen(me->url) + 10);
        char iv_key[33];
        
        if(sscanf(tag, "#EXT-X-KEY:METHOD=AES-128,URI=\"%[^\"]\",IV=0x%s", link_to_key, iv_key) == 2) {
            strcpy(me->enc_aes.iv_value, iv_key);
            me->enc_aes.iv_is_static = true;
        }
        
        extend_url(&link_to_key, me->url);
        free(link_to_key);
        
        char *content_of_key_file = malloc(33);
        printf("%s\n", link_to_key);
        get_source_from_url(link_to_key, &content_of_key_file);
        
        char decrypt[33];
        int length = 0;
        for(int i = 0; i < strlen(content_of_key_file); i++) {
            length += sprintf(decrypt+length, "%02x", (unsigned char)content_of_key_file[i]);
        }
        strcpy(me->enc_aes.key_value, decrypt);
        free(content_of_key_file);
    }
    return 0;
}

static int get_link_count(char *source)
{<commit_msg>hls: remove print of key-url<commit_after>#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include <time.h>
#include "curl.h"
#include "hls.h"

static char *get_rndstring(int length)
{
    int max;
    const char *letters = "abcdefghijklmnopqrstuvwxyz123456789";
    int i, r;
    srand((int)time(NULL));
    max = length;
    
    char *generated = (char*)malloc(max + 1);
    for(i = 0; i < max; i++) {
        r = rand() % strlen(letters);
        generated[i] = letters[r];
    }
    generated[max] = '\0';
    return generated;
}

int get_playlist_type(char *source)
{
    if (strncmp("#EXTM3U", source, 7) != 0) {
        fprintf(stderr, "Not a valid M3U8 file. Exiting.\n");
        return -1;
    }
    for (int i = 0; i < strlen(source); i++) {
        if(!strncmp(&source[i], "#EXT-X-STREAM-INF", 17)) {
            return 0;
        }
    }
    return 1;
}

static int extend_url(char **url, const char *baseurl)
{
    size_t max_length = strlen(*url) + strlen(baseurl) + 10;
    
    if (!strncmp(*url, "http://", 7) || !strncmp(*url, "https://", 8)) {
        return 0; //absolute url, nothing to do
    }
    
    else if (**url == '/') {
        char *domain = (char*)malloc(max_length);
        strcpy(domain, baseurl);
        if(!sscanf(baseurl, "http://%[^/]", domain)) {
            sscanf(baseurl, "https://%[^/]", domain);
        }
        
        char *buffer = (char*)malloc(max_length);
        snprintf(buffer, max_length, "%s%s", domain, *url);
        strcpy(*url, buffer);
        free(buffer);
        free(domain);
        return 0;
    }
    
    else {
        char *buffer = (char*)malloc(max_length);
        snprintf(buffer, max_length, "%s/../%s", baseurl, *url);
        strcpy(*url, buffer);
        free(buffer);
        return 0;
    }

}

static int parse_playlist_tag(struct hls_media_playlist *me, char *tag)
{
    if (!strncmp(tag, "#EXT-X-KEY:METHOD=AES-128", 25)) {
        me->encryption = true;
        me->encryptiontype = ENC_AES128;
        me->enc_aes.iv_is_static = false;
        
        char *link_to_key = (char*)malloc(strlen(tag) + strlen(me->url) + 10);
        char iv_key[33];
        
        if(sscanf(tag, "#EXT-X-KEY:METHOD=AES-128,URI=\"%[^\"]\",IV=0x%s", link_to_key, iv_key) == 2) {
            strcpy(me->enc_aes.iv_value, iv_key);
            me->enc_aes.iv_is_static = true;
        }
        
        extend_url(&link_to_key, me->url);
        free(link_to_key);
        
        char *content_of_key_file = malloc(33);
        get_source_from_url(link_to_key, &content_of_key_file);
        
        char decrypt[33];
        int length = 0;
        for(int i = 0; i < strlen(content_of_key_file); i++) {
            length += sprintf(decrypt+length, "%02x", (unsigned char)content_of_key_file[i]);
        }
        strcpy(me->enc_aes.key_value, decrypt);
        free(content_of_key_file);
    }
    return 0;
}

static int get_link_count(char *source)
{
{'fraction_non_alphanumeric': 0.16867067468269872, 'fraction_numerical': 0.018704074816299265, 'mean_word_length': 2.3476802683063163, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 31, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3409884', 'n_tokens_mistral': 2291, 'n_tokens_neox': 2176, 'n_words': 436}