-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathbinmode.c
153 lines (120 loc) · 4.05 KB
/
binmode.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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
SFSEXP: Small, Fast S-Expression Library version 1.3
Written by Matthew Sottile ([email protected])
Copyright (2003-2006). The Regents of the University of California. This
material was produced under U.S. Government contract W-7405-ENG-36 for Los
Alamos National Laboratory, which is operated by the University of
California for the U.S. Department of Energy. The U.S. Government has rights
to use, reproduce, and distribute this software. NEITHER THE GOVERNMENT NOR
THE UNIVERSITY MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY
LIABILITY FOR THE USE OF THIS SOFTWARE. If software is modified to produce
derivative works, such modified software should be clearly marked, so as not
to confuse it with the version available from LANL.
Additionally, this library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, U SA
LA-CC-04-094
**/
#include <sys/fcntl.h>
#include "sexp.h"
#include <assert.h>
#ifndef WIN32
# include <unistd.h>
#else
# define ssize_t int
# include <io.h>
# include <sys/types.h>
#endif
#include <stdio.h>
/**
* read a binary file from fd. Length is filled in, bss is buffer
* start size and bgs is buffer growth size for realloc()ing. returns
* pointer to byte-buffer. if NULL, then there was some sort of error.
*/
char *readfile(int fd, size_t *length, size_t bss, size_t bgs) {
char *buf, *tmp;
size_t bused, ballocd;
int amt;
/* check for either errors on the caller side */
assert(fd > 0);
bused = 0;
ballocd = bss;
buf = (char *)malloc(sizeof(char)*bss);
assert(buf != NULL);
while ((amt = read(fd,buf+bused,ballocd-bused)) > 0) {
bused += amt;
if (bused == ballocd) {
tmp = (char *)realloc(buf,ballocd+bgs);
assert(tmp != NULL);
buf = tmp;
ballocd += bgs;
}
}
if (bused == 0) return NULL;
*length = bused;
return buf;
}
int main(int argc, char **argv) {
sexp_t *sx_in, *sx_binatom, *sx_out;
int fd, status;
char *b;
size_t l = 0;
CSTRING *s = NULL;
pcont_t *pc;
/* read data */
fd = open("testdata",O_RDONLY);
if (fd <= 0) {
printf("Error opening test data file ``testdata''\n");
exit(EXIT_FAILURE);
}
b = readfile(fd,&l,1024,256);
close(fd);
/* report */
printf("Read %lu bytes of data.\n",(unsigned long)l);
sx_binatom = new_sexp_binary_atom(b, l);
assert(sx_binatom != NULL);
/* IMPORTANT: since sx_binatom is now being subsumed by
the list sx_in that contains it, we will not need to
explicitly free sx_binatom since it will be freed
during the traversal when sx_in is destroyed. */
sx_in = new_sexp_list(sx_binatom);
printf("Created expression.\n");
print_sexp_cstr(&s,sx_in,l+1024);
destroy_sexp(sx_in);
b = NULL;
sx_in = NULL;
printf("Destroyed AST and buffer.\n");
pc = init_continuation(NULL);
pc->mode = PARSER_INLINE_BINARY;
pc = cparse_sexp(s->base,s->len,pc);
sx_out = pc->last_sexp;
printf("Parsed unparsed version back to AST.\n");
assert(sx_out != NULL);
b = sx_out->list->bindata;
l = sx_out->list->binlength;
fd = open("testdata_out",O_RDWR|O_CREAT,0644);
if (fd <= 0) {
printf("Error opening ``testdata_out'': Create empty file to write to.\n");
exit(EXIT_FAILURE);
}
status = write(fd,b,l);
if (status < 0) {
printf("Write failed.\n");
exit(EXIT_FAILURE);
}
close(fd);
sdestroy(s);
destroy_continuation(pc);
destroy_sexp(sx_out);
sexp_cleanup();
printf("Extracted and wrote bindata from AST.\n");
exit(EXIT_SUCCESS);
}