-
Notifications
You must be signed in to change notification settings - Fork 2
/
GeneralFunctions.c
204 lines (149 loc) · 3.81 KB
/
GeneralFunctions.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include "includes.h"
#include "Encodings.h"
#include "Hash.h"
#include "Time.h"
#include "StrLenCache.h"
void Destroy(void *Obj)
{
if (Obj)
{
//If the object is a cached string, the remove it from cache
StrLenCacheDel(Obj);
free(Obj);
}
}
uint32_t reverse_uint32(uint32_t Input)
{
uint32_t Output;
Output = (Input & 0xFF) << 24;
Output |= (Input & 0xFF00) << 8;
Output |= (Input & 0xFF0000) >> 8;
Output |= (Input & 0xFF000000) >> 24;
return(Output);
}
uint8_t parse_bcd_byte(const char *In)
{
uint8_t val=0;
int bit, len, i;
const char *ptr;
len=StrLen(In);
if (len < 1) return(0);
if (len > 8) len=8;
bit=1 << (len -1);
ptr=In;
for (i=0; i < len; i++)
{
if (*ptr=='1') val |= bit;
ptr++;
bit=bit >> 1;
}
return(val);
}
char *encode_bcd_bytes(char *RetStr, unsigned const char *Bytes, int Len)
{
unsigned const char *ptr, *end;
int bit, i;
end=Bytes+Len;
for (ptr=Bytes; ptr < end; ptr++)
{
bit=128;
for (i=0; i < 8; i++)
{
if (*ptr & bit) RetStr=CatStr(RetStr, "1");
else RetStr=CatStr(RetStr, "0");
bit = bit >> 1;
}
}
return(RetStr);
}
//xmemset uses a 'volatile' pointer so that it won't be optimized out
void xmemset(volatile char *Str, char fill, off_t size)
{
volatile char *p;
for (p=Str; p < (Str+size); p++) *p=fill;
}
int xsetenv(const char *Name, const char *Value)
{
if (setenv(Name, Value,TRUE)==0) return(TRUE);
return(FALSE);
}
int ptr_incr(const char **ptr, int count)
{
const char *end;
end=(*ptr) + count;
for (; (*ptr) < end; (*ptr)++)
{
if ((**ptr) == '\0') return(FALSE);
}
return(TRUE);
}
const char *traverse_until(const char *ptr, char terminator)
{
while ((*ptr != terminator) && (*ptr != '\0'))
{
//handle quoted chars
if ((*ptr=='\\') && (*(ptr+1) != '\0')) ptr++;
ptr++;
}
return(ptr);
}
const char *traverse_quoted(const char *ptr)
{
char Quote;
Quote=*ptr;
ptr++;
return(traverse_until(ptr, Quote));
}
#define FNV_INIT_VAL 2166136261
unsigned int fnv_hash(unsigned const char *key, int NoOfItems)
{
unsigned const char *p;
unsigned int h = FNV_INIT_VAL;
for (p=key; *p !='\0' ; p++ ) h = ( h * 16777619 ) ^ *p;
return(h % NoOfItems);
}
char *CommaList(char *RetStr, const char *AddStr)
{
if (StrValid(RetStr)) RetStr=MCatStr(RetStr,", ",AddStr,NULL);
else RetStr=CopyStr(RetStr,AddStr);
return(RetStr);
}
//remap one fd to another, usually used to change stdin, stdout or stderr
int fd_remap(int fd, int newfd)
{
int result;
close(fd);
result=dup(newfd);
return(result);
}
//open a file and *only* remap it to the given fd if it opens successfully
int fd_remap_path(int fd, const char *Path, int Flags)
{
int newfd;
int result;
newfd=open(Path, Flags);
if (newfd==-1) return(FALSE);
result=fd_remap(fd, newfd);
close(newfd);
return(result);
}
//This Function eliminates characters from a string that can be used to trivially achieve code-exec via the shell
char *MakeShellSafeString(char *RetStr, const char *String, int SafeLevel)
{
char *Tempstr=NULL;
char *BadChars=";|&`$";
int ErrFlags=0;
if (SafeLevel==SHELLSAFE_BLANK)
{
Tempstr=CopyStr(RetStr,String);
strmrep(Tempstr,BadChars,' ');
}
else Tempstr=QuoteCharsInStr(RetStr,String,BadChars);
if ( (SafeLevel & (SHELLSAFE_REPORT | SHELLSAFE_ABORT)) && (CompareStr(Tempstr,String) !=0) )
{
ErrFlags=ERRFLAG_SYSLOG;
if (SafeLevel & SHELLSAFE_ABORT) ErrFlags |= ERRFLAG_ABORT;
RaiseError(ErrFlags, "MakeShellSafeString", "unsafe chars found in %s", String);
}
return(Tempstr);
}