forked from BulldogDrummond/aldl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseful.c
134 lines (121 loc) · 2.92 KB
/
useful.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include "aldl-types.h"
#include "useful.h"
#include "error.h"
/************ SCOPE *********************************
Useful but generic functions.
****************************************************/
timespec_t get_time() {
timespec_t currenttime;
#ifdef USEFUL_BETTERCLOCK
clock_gettime(CLOCK_MONOTONIC,¤ttime);
#else
gettimeofday(¤ttime,NULL);
#endif
return currenttime;
};
unsigned long get_elapsed_ms(timespec_t timestamp) {
timespec_t currenttime = get_time();
unsigned long seconds = currenttime.tv_sec - timestamp.tv_sec;
#ifdef USEFUL_BETTERCLOCK
unsigned long milliseconds =(currenttime.tv_nsec-timestamp.tv_nsec) / 1000000;
#else
unsigned long milliseconds =(currenttime.tv_usec-timestamp.tv_usec) / 1000;
#endif
return ( seconds * 1000 ) + milliseconds;
};
inline int faststrcmp(char *a, char *b) {
#ifdef RETARDED
retardptr(a,"faststrcmp a");
retardptr(b,"faststrcmp b");
#endif
int x = 0;
while(a[x] == b[x]) {
x++;
if(a[x] == 0 || b[x] == 0) {
if(a[x] == 0 && b[x] == 0) {
return 1;
} else {
return 0;
};
};
};
return 0;
};
char faststrcmp_list(char *str, char *list) {
#ifdef RETARDED
retardptr(str,"faststrcmp str");
retardptr(list,"faststrcmp list");
#endif
int x = 0;
int y = 0;
while(list[x] != 0) {
y = 0;
while(str[y] != 0) {
if(str[y] == list[x]) return list[x];
y++;
};
x++;
};
return 0;
};
byte checksum_generate(byte *buf, int len) {
#ifdef RETARDED
retardptr(buf,"checksum buf");
#endif
int x = 0;
unsigned int sum = 0;
for(x=0;x<len;x++) sum += buf[x];
return ( 256 - ( sum % 256 ) );
};
int checksum_test(byte *buf, int len) {
int x = 0;
unsigned int sum = 0;
for(x=0;x<len;x++) sum += buf[x];
if(( sum & 0xFF ) == 0) return 1;
return 0;
};
int cmp_bytestring(byte *h, int hsize, byte *n, int nsize) {
if(nsize > hsize) return 0; /* needle is larger than haystack */
if(hsize < 1 || nsize < 1) return 0;
int cursor = 0; /* haystack compare cursor */
int matched = 0; /* needle compare cursor */
while(cursor <= hsize) {
if(nsize == matched) return 1;
if(h[cursor] != n[matched]) { /* reset match */
matched = 0;
} else {
matched++;
};
cursor++;
};
return 0;
};
void printhexstring(byte *str, int length) {
int x;
for(x=0;x<length;x++) printf("%X ",(unsigned int)str[x]);
printf("\n");
};
#ifdef MALLOC_ERRCHECK
void *smalloc(size_t size) {
void *m = malloc(size);
if(m == NULL) {
fprintf(stderr, "Out of memory trying to alloc %u bytes",(unsigned int)size);
exit(1);
};
return m;
};
#endif
int clamp_int(int min, int max, int in) {
if(in > max) return max;
if(in < min) return min;
return in;
};
float clamp_float(float min, float max, float in) {
if(in > max) return max;
if(in < min) return min;
return in;
};