-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.c
More file actions
121 lines (105 loc) · 1.67 KB
/
Copy pathtest.c
File metadata and controls
121 lines (105 loc) · 1.67 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cdata.h"
static size_t
pack_B_b
( int mode,
char * buffer,
char * var)
{
void **n = (void**)var;
if (mode == CDATA_READ)
{
*n = (void*)(cdata_decode16(buffer) / 2);
}
else if (mode == CDATA_WRITE)
{
if (buffer)
{
cdata_encode16(buffer, (long)*n * 2);
}
}
return 2;
}
PACKDEF (A_def) struct A {
char a[4];
int b DONOTPACK;
int c[2];
};
PACKDEF (B_def) typedef struct {
struct A a PACKNEST (A_def);
int *b PACK (pack_B_b);
} B;
#ifndef cdp_stage
#include "test-def.c"
#endif
static void
enpack
( struct pack_state * P,
B * bruh)
{
char *buffer = malloc(cdata_get_pack_size(P));
size_t n = cdata_pack(P, buffer);
fprintf(stderr, "%ld\n", n);
fwrite(buffer, 1, n, stdout);
}
static void
unpack
( struct pack_state * P,
B * bruh,
size_t n)
{
char *buffer = malloc(n);
fread(buffer, 1, n, stdin);
cdata_unpack(P, buffer);
}
static int
help (void)
{
fputs(
"./test c > file # encode test\n"
"./test d size < file # decode test\n",
stderr);
return 1;
}
static struct pack_state *
load (B *bruh)
{
struct pack_state *P = cdata_new(&B_def);
return cdata_load(P, bruh), P;
}
int
main
( int ac,
char ** av)
{
B bruh;
if (ac == 1)
return help();
if (!strcmp(av[1], "c"))
{
if (ac != 2)
return help();
strncpy(bruh.a.a, "Shit", 4);
bruh.a.c[0] = 127;
bruh.a.c[1] = 4096;
bruh.b = (void*)33;
enpack(load(&bruh), &bruh);
}
else if (!strcmp(av[1], "d"))
{
if (ac != 3)
return help();
unpack(load(&bruh), &bruh, atoi(av[2]));
printf(
"%.4s\n"
"%d, %d\n"
"%p\n",
bruh.a.a,
bruh.a.c[0],
bruh.a.c[1],
bruh.b);
}
return 0;
}