-
Notifications
You must be signed in to change notification settings - Fork 0
/
avrdis.c
114 lines (97 loc) · 2.28 KB
/
avrdis.c
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
/*****************************************************************************
*
* Description:
* Common module for the avrdis project.
*
* Author:
* Imre Horvath <imi [dot] horvath [at] gmail [dot] com> (c) 2023
*
* License:
* GNU GPLv3
*
*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "avrdis.h"
void freewordlist(struct wordlist *wl)
{
struct wordlist *temp;
while (wl) {
temp = wl;
wl = wl->next;
free(temp);
}
}
struct regionstruct *allocregions(void)
{
struct regionstruct *rs = malloc(sizeof(struct regionstruct));
if (rs)
memset(rs, 0, sizeof(struct regionstruct));
return rs;
}
void freeregions(struct regionstruct *rs)
{
struct region *r, *temp;
for (r = rs->first; r;) {
temp = r;
r = r->next;
free(temp);
}
free(rs);
}
int addregion(struct regionstruct *rs, uint32_t begin, uint32_t end)
{
struct region *r = malloc(sizeof(struct region));
if (!r)
return 0;
memset(r, 0, sizeof(struct region));
r->begin = begin;
r->end = end;
if (!rs->first)
rs->first = r;
else
rs->last->next = r;
rs->last = r;
return 1;
}
struct region *inregionswithprev(struct regionstruct *rs, uint32_t wordaddress, struct region **prev)
{
struct region *r, *pr = NULL;
for (r = rs->first; r; r = r->next) {
if (r->begin <= wordaddress && wordaddress <= r->end) {
if (prev)
*prev = pr;
return r;
}
pr = r;
}
return NULL;
}
struct region *inregions(struct regionstruct *rs, uint32_t wordaddress)
{
return inregionswithprev(rs, wordaddress, NULL);
}
void printregions(struct regionstruct *rs)
{
struct region *r;
for (r = rs->first; r; r = r->next)
printf("0x%04x:0x%04x\n", r->begin, r->end);
}
int strcmpnocase(const char *lhs, const char *rhs)
{
while (*lhs && *rhs) {
if (tolower(*lhs) < tolower(*rhs))
return -1;
if (tolower(*lhs) > tolower(*rhs))
return 1;
lhs++;
rhs++;
}
if (!*lhs && !*rhs)
return 0;
if (*lhs)
return 1;
return -1;
}