-
Notifications
You must be signed in to change notification settings - Fork 1
/
assist.c
77 lines (69 loc) · 1.19 KB
/
assist.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
#include "assist.h"
void err (const char *s)
{
fprintf(stderr, "err:%s\n", s);
exit (1);
}
void swap_int (int *n1, int *n2)
{
if (n1 == n2) return;
*n1 = *n1 ^ *n2;
*n2 = *n1 ^ *n2;
*n1 = *n1 ^ *n2;
}
void p_err (const char *s)
{
perror(s);
exit (1);
}
void swap_byte (unsigned char *c1, unsigned char *c2)
{
if (c1 == c2) return;
*c1 = *c1 ^ *c2;
*c2 = *c1 ^ *c2;
*c1 = *c1 ^ *c2;
}
int network_to_host (int bytes)
{
short s = 1;
unsigned char *a, *b;
unsigned char *num = (unsigned char*)&bytes;
if (*(char*)&s == 1){ //little endian
a = num;
b = &num[3];
swap_byte(a, b);
a = &num[1];
b = &num[2];
swap_byte(a, b);
return bytes;
}
else return bytes;
}
int vm (void *pos, int bytes)
{
int i;
unsigned char *ch;
if (pos == NULL)
{
printf ("in func vm, err: NULL pointer\n");
return -1;
}
if (bytes == 0)
{
printf ("0 bytes to view\n");
return -1;
}
ch = (unsigned char*) pos;
printf ("binary view is\n");
for (i = 0; i <= bytes - 1; i++)
{
int k;
for (k = 7; k >= 0; k--)
{
printf ("%u", (ch[i] & ( 1 << k)) >> k);
}
printf (" ");
}
printf ("\n");
return 0;
}