-
Notifications
You must be signed in to change notification settings - Fork 50
/
gen.c
85 lines (73 loc) · 1.68 KB
/
gen.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
/* $Revision$ */
#include <math.h>
#include <string.h>
#include "gen.h"
void init_statst(stats_t *data)
{
memset(data, 0x00, sizeof(stats_t));
data -> min = MY_DOUBLE_INF;
data -> max = -data -> min;
}
void update_statst(stats_t *data, double in)
{
data -> cur = in;
data -> min = min(data -> min, in);
data -> max = max(data -> max, in);
data -> avg += in;
data -> sd += in * in;
(data -> n)++;
data -> valid = 1;
data -> cur_valid = 1;
}
void reset_statst_cur(stats_t *data)
{
data -> cur_valid = 0;
}
double calc_sd(stats_t *in)
{
double avg = 0.0;
if (in -> n == 0 || !in -> valid)
return 0;
avg = in -> avg / (double)in -> n;
return sqrt((in -> sd / (double)in -> n) - pow(avg, 2.0));
}
/* Base64 encoding start */
const char *alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
void encode_tryptique(char source[3], char result[4])
/* Encode 3 char in B64, result give 4 Char */
{
int tryptique, i;
tryptique = source[0];
tryptique *= 256;
tryptique += source[1];
tryptique *= 256;
tryptique += source[2];
for (i=0; i<4; i++)
{
result[3-i] = alphabet[tryptique%64];
tryptique /= 64;
}
}
int enc_b64(char *source, int source_lenght, char *target)
{
/* Divide string /3 and encode trio */
while (source_lenght >= 3) {
encode_tryptique(source, target);
source_lenght -= 3;
source += 3;
target += 4;
}
/* Add padding to the rest */
if (source_lenght > 0) {
char pad[3];
memset(pad, 0, sizeof pad);
memcpy(pad, source, source_lenght);
encode_tryptique(pad, target);
target[3] = '=';
if (source_lenght == 1) target[2] = '=';
target += 4;
}
target[0] = 0;
return 1;
}
/* Base64 encoding END */