<commit_before>/*
 * ty, a collection of GUI and command-line tools to manage Teensy devices
 *
 * Distributed under the MIT license (see LICENSE.txt or http://opensource.org/licenses/MIT)
 * Copyright (c) 2015 <NAME> <<EMAIL>>
 */

#include "ty/common.h"
#include "compat.h"
#include "firmware_priv.h"
#include "ty/system.h"

int _ty_firmware_load_elf(ty_firmware *fw);
int _ty_firmware_load_ihex(ty_firmware *fw);

const ty_firmware_format ty_firmware_formats[] = {
    {"elf",  ".elf", _ty_firmware_load_elf},
    {"ihex", ".hex", _ty_firmware_load_ihex},
    {0}
};

#define FIRMWARE_STEP_SIZE 32768

static const char *get_basename(const char *filename)
{
    const char *basename = strrpbrk(filename, TY_PATH_SEPARATORS);
    if (!basename)
        return filename;

    return basename + 1;
}

int ty_firmware_load(const char *filename, const char *format_name, ty_firmware **rfw)
{
    assert(filename);
    assert(rfw);

    const ty_firmware_format *format;
    ty_firmware *fw = NULL;
    int r;

    if (format_name) {
        for (format = ty_firmware_formats; format->name; format++) {
            if (strcasecmp(format->name, format_name) == 0)
                break;
        }
        if (!format->name) {
            r = ty_error(TY_ERROR_UNSUPPORTED, "Firmware file format '%s' unknown", format_name);
            goto error;
        }
    } else {
        const char *ext = strrchr(filename, '.');

        for (format = ty_firmware_formats; format->name; format++) {
            if (strcmp(format->ext, ext) == 0)
                break;<commit_msg>Fix crash with extension-less filenames in ty_firmware_load()<commit_after>/*
 * ty, a collection of GUI and command-line tools to manage Teensy devices
 *
 * Distributed under the MIT license (see LICENSE.txt or http://opensource.org/licenses/MIT)
 * Copyright (c) 2015 <NAME> <<EMAIL>>
 */

#include "ty/common.h"
#include "compat.h"
#include "firmware_priv.h"
#include "ty/system.h"

int _ty_firmware_load_elf(ty_firmware *fw);
int _ty_firmware_load_ihex(ty_firmware *fw);

const ty_firmware_format ty_firmware_formats[] = {
    {"elf",  ".elf", _ty_firmware_load_elf},
    {"ihex", ".hex", _ty_firmware_load_ihex},
    {0}
};

#define FIRMWARE_STEP_SIZE 32768

static const char *get_basename(const char *filename)
{
    const char *basename = strrpbrk(filename, TY_PATH_SEPARATORS);
    if (!basename)
        return filename;

    return basename + 1;
}

int ty_firmware_load(const char *filename, const char *format_name, ty_firmware **rfw)
{
    assert(filename);
    assert(rfw);

    const ty_firmware_format *format;
    ty_firmware *fw = NULL;
    int r;

    if (format_name) {
        for (format = ty_firmware_formats; format->name; format++) {
            if (strcasecmp(format->name, format_name) == 0)
                break;
        }
        if (!format->name) {
            r = ty_error(TY_ERROR_UNSUPPORTED, "Firmware file format '%s' unknown", format_name);
            goto error;
        }
    } else {
        const char *ext = strrchr(filename, '.');
        if (!ext) {
            r = ty_error(TY_ERROR_UNSUPPORTED, "Firmware '%s' has no file extension", filename);
            goto error;
        }

        for (format = ty_firmware_formats; format->name; format++) {
            if (strcmp(format->ext, ext) == 0)
                break;
{'fraction_non_alphanumeric': 0.1247376311844078, 'fraction_numerical': 0.007796101949025487, 'mean_word_length': 3.0, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 19, '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': '7343716', 'n_tokens_mistral': 1195, 'n_tokens_neox': 1126, 'n_words': 278}