<commit_before>#include <errno.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdio.h>

#include "files.h"
#include "log.h"
#include "string-bytes.h"

static bool get_file_size(FILE *fp, long *file_size, const char *path) {

    long current = ftell(fp);
    if (current < 0) {
        LOG_ERR("Error getting current file offset for file \"%s\" error: %s", path, strerror(errno));
        return false;
    }

    int rc = fseek(fp, 0, SEEK_END);
    if (rc < 0) {
        LOG_ERR("Error seeking to end of file \"%s\" error: %s", path, strerror(errno));
        return false;
    }

    long size = ftell(fp);
    if (size < 0) {
        LOG_ERR("ftell on file \"%s\" failed: %s", path, strerror(errno));
        return false;
    }

    rc = fseek(fp, current, SEEK_SET);
    if (rc < 0) {
        LOG_ERR("Could not restore initial stream position for file \"%s\" failed: %s", path, strerror(errno));
        return false;
    }

    *file_size = size;
    return true;
}

bool files_load_bytes_from_file(const char *path, UINT8 *buf, UINT16 *size) {

    if (!buf || !size || !path) {
        return false;
    }

    FILE *f = fopen(path, "rb");
    if (!f) {
        LOG_ERR("Could not open file \"%s\" error %s", path, strerror(errno));
        return false;
    }

    long file_size;
    bool result = get_file_size(f, &file_size, path);
    if (!result) {
        /* get_file_size() logs errors */
        goto err;
    }

    /* max is bounded on UINT16 */
    if (file_size > *size) {
        LOG_ERR(
                "File \"%s\" size is larger than buffer, got %ld expected less than %u",
                path, file_size, *size);
        goto err;
    }

    result = files_read_bytes(f, buf, file_size);
    if (!result) {
        LOG_ERR("Could not read data from file \"%s\"", path);
        goto err;
    }

    *size = file_size;
    /* result set on files_read_bytes */

err:
    fclose(f);
    return result;
}

bool files_save_bytes_to_file(const char *path, UINT8 *buf, UINT16 size) {

    if (!path || !buf) {
        return false;
    }

    FILE *fp = fopen(path, "wb+");

    bool result = files_write_bytes(fp, buf, size);
    if (!result) {
        LOG_ERR("Could not write data to file \"%s\"", path);
    }
    fclose(fp);
    return result;
}

/*
 * Current version to write TPMS_CONTEXT to disk.
 */
#define CONTEXT_VERSION 1

bool files_save_tpm_context_to_file(TSS2_SYS_CONTEXT *sysContext, TPM_HANDLE handle,<commit_msg>files: check return of fopen<commit_after>#include <errno.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdio.h>

#include "files.h"
#include "log.h"
#include "string-bytes.h"

static bool get_file_size(FILE *fp, long *file_size, const char *path) {

    long current = ftell(fp);
    if (current < 0) {
        LOG_ERR("Error getting current file offset for file \"%s\" error: %s", path, strerror(errno));
        return false;
    }

    int rc = fseek(fp, 0, SEEK_END);
    if (rc < 0) {
        LOG_ERR("Error seeking to end of file \"%s\" error: %s", path, strerror(errno));
        return false;
    }

    long size = ftell(fp);
    if (size < 0) {
        LOG_ERR("ftell on file \"%s\" failed: %s", path, strerror(errno));
        return false;
    }

    rc = fseek(fp, current, SEEK_SET);
    if (rc < 0) {
        LOG_ERR("Could not restore initial stream position for file \"%s\" failed: %s", path, strerror(errno));
        return false;
    }

    *file_size = size;
    return true;
}

bool files_load_bytes_from_file(const char *path, UINT8 *buf, UINT16 *size) {

    if (!buf || !size || !path) {
        return false;
    }

    FILE *f = fopen(path, "rb");
    if (!f) {
        LOG_ERR("Could not open file \"%s\" error %s", path, strerror(errno));
        return false;
    }

    long file_size;
    bool result = get_file_size(f, &file_size, path);
    if (!result) {
        /* get_file_size() logs errors */
        goto err;
    }

    /* max is bounded on UINT16 */
    if (file_size > *size) {
        LOG_ERR(
                "File \"%s\" size is larger than buffer, got %ld expected less than %u",
                path, file_size, *size);
        goto err;
    }

    result = files_read_bytes(f, buf, file_size);
    if (!result) {
        LOG_ERR("Could not read data from file \"%s\"", path);
        goto err;
    }

    *size = file_size;
    /* result set on files_read_bytes */

err:
    fclose(f);
    return result;
}

bool files_save_bytes_to_file(const char *path, UINT8 *buf, UINT16 size) {

    if (!path || !buf) {
        return false;
    }

    FILE *fp = fopen(path, "wb+");
    if (!fp) {
        LOG_ERR("Could not open file \"%s\", error: %s", path, strerror(errno));
        return false;
    }

    bool result = files_write_bytes(fp, buf, size);
    if (!result) {
        LOG_ERR("Could not write data to file \"%s\"", path);
    }
    fclose(fp);
    return result;
}

/*
 * Current version to write TPMS_CONTEXT to disk.
 */
#define CONTEXT_VERSION 1

bool files_save_tpm_context_to_file(TSS2_SYS_CONTEXT *sysContext, TPM_HANDLE handle,
{'fraction_non_alphanumeric': 0.14249062561673573, 'fraction_numerical': 0.005920663114268798, 'mean_word_length': 2.442934782608696, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 13, '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': '1616531', 'n_tokens_mistral': 1794, 'n_tokens_neox': 1739, 'n_words': 524}