"Fossies" - the Fresh Open Source Software Archive 
Member "HTTPing-2.9/gen.c" (29 Oct 2022, 1708 Bytes) of package /linux/www/HTTPing-2.9.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "gen.c" see the
Fossies "Dox" file reference documentation.
1 #include <math.h>
2 #include <string.h>
3
4 #include "gen.h"
5
6 void init_statst(stats_t *data)
7 {
8 memset(data, 0x00, sizeof(stats_t));
9
10 data -> min = MY_DOUBLE_INF;
11 data -> max = -data -> min;
12 }
13
14 void update_statst(stats_t *data, double in)
15 {
16 data -> cur = in;
17 data -> min = min(data -> min, in);
18 data -> max = max(data -> max, in);
19 data -> avg += in;
20 data -> sd += in * in;
21 (data -> n)++;
22 data -> valid = 1;
23 data -> cur_valid = 1;
24 }
25
26 void reset_statst_cur(stats_t *data)
27 {
28 data -> cur_valid = 0;
29 }
30
31 double calc_sd(stats_t *in)
32 {
33 double avg = 0.0;
34
35 if (in -> n == 0 || !in -> valid)
36 return 0;
37
38 avg = in -> avg / (double)in -> n;
39
40 return sqrt((in -> sd / (double)in -> n) - pow(avg, 2.0));
41 }
42
43 /* Base64 encoding start */
44 const char *alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
45
46 void encode_tryptique(char source[3], char result[4])
47 /* Encode 3 char in B64, result give 4 Char */
48 {
49 int tryptique, i;
50 tryptique = source[0];
51 tryptique *= 256;
52 tryptique += source[1];
53 tryptique *= 256;
54 tryptique += source[2];
55 for (i=0; i<4; i++)
56 {
57 result[3-i] = alphabet[tryptique%64];
58 tryptique /= 64;
59 }
60 }
61
62 int enc_b64(char *source, int source_lenght, char *target)
63 {
64 /* Divide string /3 and encode trio */
65 while (source_lenght >= 3) {
66 encode_tryptique(source, target);
67 source_lenght -= 3;
68 source += 3;
69 target += 4;
70 }
71 /* Add padding to the rest */
72 if (source_lenght > 0) {
73 char pad[3];
74 memset(pad, 0, sizeof pad);
75 memcpy(pad, source, source_lenght);
76 encode_tryptique(pad, target);
77 target[3] = '=';
78 if (source_lenght == 1) target[2] = '=';
79 target += 4;
80 }
81 target[0] = 0;
82 return 1;
83 }
84 /* Base64 encoding END */