<commit_before>/*
 * This file is part of the id3v2lib library
 *
 * Copyright (c) 2013, <NAME>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "id3v2lib.h"


ID3v2_tag* load_tag(const char* file_name)
{
    char *buffer;
    FILE *file;
    int header_size;
    ID3v2_tag *tag;

    // get header size
    ID3v2_header *tag_header = get_tag_header(file_name);
    if(tag_header == NULL) {
        return NULL;
    }
    header_size = tag_header->tag_size;
    free(tag_header);

    // allocate buffer and fetch header
    file = fopen(file_name, "rb");
    if(file == NULL)
    {
        perror("Error opening file");
        return NULL;
    }
    buffer = (char*) malloc((10+header_size) * sizeof(char));
    if(buffer == NULL) {
        perror("Could not allocate buffer");
        fclose(file);
        return NULL;
    }
    //fseek(file, 10, SEEK_SET);
    fread(buffer, header_size+10, 1, file);
    fclose(file);


    //parse free and return
    tag = load_tag_with_buffer(buffer, header_size);
    free(buffer);

    return tag;
}

ID3v2_tag* load_tag_with_buffer(char *bytes, int length)
{
    // Declaration
    ID3v2_frame *frame;
    int offset = 0;
    ID3v2_tag* tag;
    ID3v2_header* tag_header;

    // Initialization
    tag_header = get_tag_header_with_buffer(bytes, length);

    if(tag_header == NULL) // no valid header found
      return NULL;

    if(get_tag_version(tag_header) == NO_COMPATIBLE_TAG)
    {
        // no supported id3 tag found
        free(tag_header);
        return NULL;
    }

    if(tag_header->tag_size < length)
    {
        // Not enough bytes provided to parse completely. TODO: how to communicate to the user the lack of bytes?
        return NULL;
    }

    tag = new_tag();

    // Associations
    tag->tag_header = tag_header;

    tag->raw = (char*) malloc(tag->tag_header->tag_size * sizeof(char));
    memcpy(tag->raw, bytes, length);

    while(offset < tag->tag_header->tag_size)
    {
        frame = parse_frame(tag->raw, offset, get_tag_version(tag_header));

        if(frame != NULL)
        {
            offset += frame->size + 10;
            add_to_list(tag->frames, frame);
        }
        else
        {
            break;
        }
    }

    return tag;
}
<commit_msg>- another possible memleak fix<commit_after>/*
 * This file is part of the id3v2lib library
 *
 * Copyright (c) 2013, <NAME>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "id3v2lib.h"


ID3v2_tag* load_tag(const char* file_name)
{
    char *buffer;
    FILE *file;
    int header_size;
    ID3v2_tag *tag;

    // get header size
    ID3v2_header *tag_header = get_tag_header(file_name);
    if(tag_header == NULL) {
        return NULL;
    }
    header_size = tag_header->tag_size;
    free(tag_header);

    // allocate buffer and fetch header
    file = fopen(file_name, "rb");
    if(file == NULL)
    {
        perror("Error opening file");
        return NULL;
    }
    buffer = (char*) malloc((10+header_size) * sizeof(char));
    if(buffer == NULL) {
        perror("Could not allocate buffer");
        fclose(file);
        return NULL;
    }
    //fseek(file, 10, SEEK_SET);
    fread(buffer, header_size+10, 1, file);
    fclose(file);


    //parse free and return
    tag = load_tag_with_buffer(buffer, header_size);
    free(buffer);

    return tag;
}

ID3v2_tag* load_tag_with_buffer(char *bytes, int length)
{
    // Declaration
    ID3v2_frame *frame;
    int offset = 0;
    ID3v2_tag* tag;
    ID3v2_header* tag_header;

    // Initialization
    tag_header = get_tag_header_with_buffer(bytes, length);

    if(tag_header == NULL) // no valid header found
      return NULL;

    if(get_tag_version(tag_header) == NO_COMPATIBLE_TAG)
    {
        // no supported id3 tag found
        free(tag_header);
        return NULL;
    }

    if(tag_header->tag_size < length)
    {
        // Not enough bytes provided to parse completely. TODO: how to communicate to the user the lack of bytes?
        free(tag_header);
        return NULL;
    }

    tag = new_tag();

    // Associations
    tag->tag_header = tag_header;

    tag->raw = (char*) malloc(tag->tag_header->tag_size * sizeof(char));
    memcpy(tag->raw, bytes, length);

    while(offset < tag->tag_header->tag_size)
    {
        frame = parse_frame(tag->raw, offset, get_tag_version(tag_header));

        if(frame != NULL)
        {
            offset += frame->size + 10;
            add_to_list(tag->frames, frame);
        }
        else
        {
            break;
        }
    }

    return tag;
}

{'fraction_non_alphanumeric': 0.11575695159629248, 'fraction_numerical': 0.013594232749742533, 'mean_word_length': 2.4221282593375615, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 35, 'https://': 0, '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': '4461713', 'n_tokens_mistral': 1665, 'n_tokens_neox': 1593, 'n_words': 403}