Scratching out parsers

This commit is contained in:
Ben V. Brown 2023-08-01 08:26:56 +10:00
parent 013c1d0141
commit cf00bedeaf
3 changed files with 51 additions and 3 deletions

View File

@ -0,0 +1,25 @@
//
// Created by ralim on 01/08/23.
//
#ifndef BLISP_BIN_FILE_H
#define BLISP_BIN_FILE_H
#include <malloc.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#ifdef __cplusplus
extern "C" {
#endif
int bin_file_parse(const char* file_path_on_disk,
uint8_t** payload,
size_t* payload_length,
size_t* payload_address);
#ifdef __cplusplus
};
#endif
#endif // BLISP_BIN_FILE_H

View File

@ -1,7 +1,29 @@
#include "parse_file.h" #include "parse_file.h"
#include <string.h>
#include "dfu_file.h"
const char* get_filename_ext(const char* filename) {
const char* dot = strrchr(filename, '.');
if (!dot || dot == filename)
return "";
return dot + 1;
}
int parse_firmware_file(const char* file_path_on_disk, int parse_firmware_file(const char* file_path_on_disk,
parsed_firmware_file_t* parsed_results) { parsed_firmware_file_t* parsed_results) {
// Switchcase on the extension of the file // Switchcase on the extension of the file
const char* ext = get_filename_ext(file_path_on_disk);
if (strncmp(ext, "dfu", 3) == 0 || strncmp(ext, "DFU", 3) == 0) {
printf("Input file identified as a .dfu file\r\n");
// Handle as a .dfu file
return dfu_file_parse("test.dfu", &parsed_results->payload,
&parsed_results->payload_length,
&parsed_results->payload_address);
} else if (strncmp(ext, "bin", 3) == 0 || strncmp(ext, "BIN", 3) == 0) {
printf("Input file identified as a .bin file\r\n");
// Raw binary file
}
// TODO: Hex files?
return PARSED_ERROR_INVALID_FILETYPE;
} }

View File

@ -1,8 +1,9 @@
#pragma once #pragma once
#include "parsed_firmware_file.h" #include "parsed_firmware_file.h"
#define PARSED_ERROR_TOO_BIG = -0x1000 /* Input expands to be too big */ #define PARSED_ERROR_INVALID_FILETYPE -0x1000
#define PARSED_ERROR_BAD_DFU = -0x1001 /* DFU file provided but not valid */ #define PARSED_ERROR_TOO_BIG -0x1001 /* Input expands to be too big */
#define PARSED_ERROR_BAD_DFU -0x1002 /* DFU file provided but not valid */
// This attempts to parse the given file, and returns the parsed version of that // This attempts to parse the given file, and returns the parsed version of that
// file. This will handle any repacking required to create one contigious file // file. This will handle any repacking required to create one contigious file