-
Notifications
You must be signed in to change notification settings - Fork 3
/
trace.c
61 lines (58 loc) · 1.51 KB
/
trace.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "trace.h"
void print_int_bits(rsa_int a, char *desc)
{
char s[33], b;
int i;
memset(s, '0', 33);
s[32] = '\0';
for (i = 0; a >> i && i <= 31; i++) {
b = (a & (1 << i)) != 0 ? '1' : '0';
s[31 - i] = b;
}
printf(" %s: %s\n", desc, s);
}
void print_char_bits(char a, char *desc)
{
char s[9], b;
int i;
memset(s, '0', 9);
s[8] = '\0';
for (i = 0; a >> i && i <= 7; i++) {
b = (a & (1 << i)) != 0 ? '1' : '0';
s[7 - i] = b;
}
printf(" %s: %s\n", desc, s);
}
void print_pair_bits(Pair *r, char *desc)
{
// size_t nc = NUM_NAME_CHARS;
int k, i;
size_t nc = 0;
char c[2];
printf(" Sequence of bits for %s:\n", desc);
printf(" Bytes 1 - 6:\n");
for (k = 0; k < NUM_NAME_CHARS; k++) {
sprintf(c, "%c", r->name[k]);
print_char_bits(r->name[k], c);
}
size_t ns = nc + 65; // 6 + 65 = 71
char s[ns], b;
memset(s, '0', nc + 65);
s[ns - 1] = '\0'; // ns - 1 = 70
// name: bytes 1 - 6 (Bit index 0 - 5)
strncpy(s, r->name, nc);
// n: bytes 7 - 10 (Bit index 6 - 37)
for (i = 0; r->n >> i && i <= 31; i++) {
b = (r->n & (1 << i)) != 0 ? '1' : '0';
s[nc + 31 - i] = b;
}
// e: bytes 11 - 14 (Bit index 38 - 69, 70 is '\0')
for (i = 0; r->e >> i && i <= 31; i++) {
b = (r->e & (1 << i)) != 0 ? '1' : '0';
s[nc + 63 - i] = b;
}
printf(" Bytes 7 - 14:\n %s\n", s);
}