forked from diffblue/cbmc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathirep_serialization.cpp
252 lines (202 loc) · 5.57 KB
/
irep_serialization.cpp
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*******************************************************************\
Module: binary irep conversions with hashing
Author: CM Wintersteiger
Date: May 2007
\*******************************************************************/
/// \file
/// binary irep conversions with hashing
#include "irep_serialization.h"
#include "exception_utils.h"
#include "string_container.h"
#include <climits>
#include <iostream>
void irep_serializationt::write_irep(
std::ostream &out,
const irept &irep)
{
write_string_ref(out, irep.id());
for(const auto &sub_irep : irep.get_sub())
{
out.put('S');
reference_convert(sub_irep, out);
}
for(const auto &sub_irep_entry : irep.get_named_sub())
{
out.put('N');
write_string_ref(out, sub_irep_entry.first);
reference_convert(sub_irep_entry.second, out);
}
out.put(0); // terminator
}
const irept &irep_serializationt::reference_convert(std::istream &in)
{
std::size_t id=read_gb_word(in);
if(
id >= ireps_container.ireps_on_read.size() ||
!ireps_container.ireps_on_read[id].first)
{
irept irep = read_irep(in);
if(id >= ireps_container.ireps_on_read.size())
ireps_container.ireps_on_read.resize(1 + id * 2, {false, get_nil_irep()});
// guard against self-referencing ireps
if(ireps_container.ireps_on_read[id].first)
throw deserialization_exceptiont("irep id read twice.");
ireps_container.ireps_on_read[id] = {true, std::move(irep)};
}
return ireps_container.ireps_on_read[id].second;
}
irept irep_serializationt::read_irep(std::istream &in)
{
irep_idt id = read_string_ref(in);
irept::subt sub;
irept::named_subt named_sub;
while(in.peek()=='S')
{
in.get();
sub.push_back(reference_convert(in));
}
while(in.peek()=='N')
{
in.get();
irep_idt id = read_string_ref(in);
named_sub.emplace(id, reference_convert(in));
}
while(in.peek()=='C')
{
in.get();
irep_idt id = read_string_ref(in);
named_sub.emplace(id, reference_convert(in));
}
if(in.get()!=0)
{
throw deserialization_exceptiont("irep not terminated");
}
return irept(std::move(id), std::move(named_sub), std::move(sub));
}
/// Serialize an irept
/// \param irep: source irept to serialize
/// \param out: target output stream
void irep_serializationt::reference_convert(
const irept &irep,
std::ostream &out)
{
std::size_t h=ireps_container.irep_full_hash_container.number(irep);
const auto res = ireps_container.ireps_on_write.insert(
{h, ireps_container.ireps_on_write.size()});
write_gb_word(out, res.first->second);
if(res.second)
write_irep(out, irep);
}
/// Write 7 bits of `u` each time, least-significant byte first, until we have
/// zero.
/// \param out: target stream
/// \param u: number to write
void write_gb_word(std::ostream &out, std::size_t u)
{
while(true)
{
unsigned char value=u&0x7f;
u>>=7;
if(u==0)
{
out.put(value);
break;
}
out.put(value | 0x80);
}
}
/// Interpret a stream of byte as a 7-bit encoded unsigned number.
/// \param in: input stream
/// \return decoded number
std::size_t irep_serializationt::read_gb_word(std::istream &in)
{
std::size_t res=0;
unsigned shift_distance=0;
while(in.good())
{
if(shift_distance >= sizeof(res) * CHAR_BIT)
throw deserialization_exceptiont("input number too large");
unsigned char ch=static_cast<unsigned char>(in.get());
res|=(size_t(ch&0x7f))<<shift_distance;
shift_distance+=7;
if((ch&0x80)==0)
break;
}
if(!in.good())
throw deserialization_exceptiont("unexpected end of input stream");
return res;
}
/// outputs the string and then a zero byte.
/// \param out: output stream
/// \param s: string to output
void write_gb_string(std::ostream &out, const std::string &s)
{
for(std::string::const_iterator it=s.begin();
it!=s.end();
++it)
{
if(*it==0 || *it=='\\')
out.put('\\'); // escape specials
out << *it;
}
out.put(0);
}
/// reads a string from the stream
/// \param in: input stream
/// \return a string
irep_idt irep_serializationt::read_gb_string(std::istream &in)
{
char c;
size_t length=0;
while((c=static_cast<char>(in.get()))!=0)
{
if(length>=read_buffer.size())
read_buffer.resize(read_buffer.size()*2, 0);
if(c=='\\') // escaped chars
read_buffer[length]=static_cast<char>(in.get());
else
read_buffer[length]=c;
length++;
}
return irep_idt(std::string(read_buffer.data(), length));
}
/// Output a string and maintain a reference to it
/// \param out: output stream
/// \param s: string to output
void irep_serializationt::write_string_ref(
std::ostream &out,
const irep_idt &s)
{
#ifdef USE_DSTRING
size_t id = s.get_no();
#else
size_t id = get_string_container()[s];
#endif
if(id>=ireps_container.string_map.size())
ireps_container.string_map.resize(id+1, false);
if(ireps_container.string_map[id])
write_gb_word(out, id);
else
{
ireps_container.string_map[id]=true;
write_gb_word(out, id);
write_gb_string(out, id2string(s));
}
}
/// Read a string reference from the stream
/// \param in: input stream
/// \return a string
irep_idt irep_serializationt::read_string_ref(std::istream &in)
{
std::size_t id=read_gb_word(in);
if(id>=ireps_container.string_rev_map.size())
ireps_container.string_rev_map.resize(1+id*2,
std::pair<bool, irep_idt>(false, irep_idt()));
if(!ireps_container.string_rev_map[id].first)
{
irep_idt s=read_gb_string(in);
ireps_container.string_rev_map[id]=
std::pair<bool, irep_idt>(true, s);
}
return ireps_container.string_rev_map[id].second;
}