summaryrefslogtreecommitdiff
path: root/tools/hex2bin-2.0/src/common.h
blob: 64caff5c47596719af74ab878660957155ecfd99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include "binary.h"
#include "libcrc.h"

/* To compile with Microsoft Visual Studio */
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif

#if defined(MSDOS) || defined(__DOS__) || defined(__MSDOS__) || defined(_MSDOS)
#define _IS_OPTION_(x)	 (((x) == '-') || ((x) == '/'))
#else
/* Assume unix and similar */
/* We don't accept an option beginning with a '/' because it could be a file name. */
#define _IS_OPTION_(x)	 ((x) == '-')
#endif

/* We use buffer to speed disk access. */
#ifdef USE_FILE_BUFFERS
#define BUFFSZ 4096
#endif

/* FIXME how to get it from the system/OS? */
#define MAX_FILE_NAME_SIZE 81

#ifdef DOS
#define MAX_EXTENSION_SIZE 4
#else
#define MAX_EXTENSION_SIZE 16
#endif

/* The data records can contain 255 bytes: this means 512 characters. */
#define MAX_LINE_SIZE 1024

typedef char filetype[MAX_FILE_NAME_SIZE];
typedef unsigned char byte;
typedef unsigned short word;

#define LAST_CHECK_METHOD 4

typedef enum Crc
{
    CHK8_SUM =0,
    CHK16,
    CRC8,
    CRC16,
    CRC32
} t_CRC;

extern const char *Pgm_Name;
void usage(void);
void DisplayCheckMethods(void);

void *NoFailMalloc (size_t size);
void NoFailOpenInputFile (char *Flnm);
void NoFailOpenOutputFile (char *Flnm);
void GetLine(char* str,FILE *in);
int GetBin(const char *str);
int GetDec(const char *str);
int GetHex(const char *str);
bool GetBoolean(const char *str);
void GetExtension(const char *str,char *ext);
void PutExtension(char *Flnm, char *Extension);

filetype    Filename;           /* string for opening files */
char        Extension[MAX_EXTENSION_SIZE];       /* filename extension for output files */

FILE        *Filin,             /* input files */
            *Filout;            /* output files */

#ifdef USE_FILE_BUFFERS
char		*FilinBuf,          /* text buffer for file input */
            *FiloutBuf;         /* text buffer for file output */
#endif

int Pad_Byte;
bool Enable_Checksum_Error;
bool Status_Checksum_Error;
byte 	Checksum;
unsigned int Record_Nb;

/* This will hold binary codes translated from hex file. */
byte *Memory_Block;
unsigned int Lowest_Address, Highest_Address;
unsigned int Starting_Address;
unsigned int Max_Length;
unsigned int Minimum_Block_Size;
int Module;
bool Minimum_Block_Size_Setted;
bool Starting_Address_Setted;
bool Max_Length_Setted;
bool Swap_Wordwise;

int Endian;

t_CRC Cks_Type;
unsigned int Cks_Start, Cks_End, Cks_Addr, Cks_Value;
bool Cks_range_set;
bool Cks_Addr_set;
bool Force_Value;

unsigned int Crc_Poly, Crc_Init, Crc_XorOut;
bool Crc_RefIn;
bool Crc_RefOut;

void VerifyChecksumValue(void);
void CrcParamsCheck(void);
void WriteMemBlock16(uint16_t Value);
void WriteMemBlock32(uint32_t Value);
void WriteMemory(void);