monotone

monotone Mtn Source Tree

Root/vocab.cc

1// copyright (C) 2002, 2003 graydon hoare <graydon@pobox.com>
2// all rights reserved.
3// licensed to the public under the terms of the GNU GPL (>= 2)
4// see the file COPYING for details
5
6#include <string>
7#include <iostream>
8
9#include "constants.hh"
10#include "sanity.hh"
11#include "vocab.hh"
12
13// verifiers for various types of data
14
15using namespace std;
16
17// the verify() stuff gets a little complicated; there doesn't seem to be a
18// really nice way to achieve what we want with c++'s type system. the
19// problem is this: we want to give verify(file_path) and verify(local_path)
20// access to the internals of file_path and local_path, i.e. make them
21// friends, so they can normalize the file paths they're given. this means
22// that verify() needs to be declared publically, so that the definition of
23// these classes can refer to them. it also means that they -- and all other
24// ATOMIC types -- cannot fall back on a templated version of verify if no
25// other version is defined, because, well, the friend thing and the template
26// thing just don't work out, as far as I can tell. So, every ATOMIC type
27// needs an explicitly defined verify() function, so we have both ATOMIC() and
28// ATOMIC_NOVERIFY() macros, the latter of which defines a type-specific noop
29// verify function. DECORATE and ENCODING, on the other hand, cannot make use
30// of a trick like these, because they are template types themselves, and we
31// want to be able to define verify(hexenc<id>) without defining
32// verify(hexenc<data>) at the same time, for instance. Fortunately, these
33// types never need to be friends with their verify functions (yet...), so we
34// _can_ use a templated fallback function. This templated function is used
35// _only_ by DECORATE and ENCODING; it would be nice to make it take an
36// argument of type T1<T2> to document that, but for some reason that doesn't
37// work either.
38template <typename T>
39static inline void
40verify(T & val)
41{}
42
43inline void
44verify(hexenc<id> & val)
45{
46 if (val.ok)
47 return;
48
49 if (val() == "")
50 return;
51
52 N(val().size() == constants::idlen,
53 F("hex encoded ID '%s' size != %d") % val % constants::idlen);
54 string::size_type pos = val().find_first_not_of(constants::legal_id_bytes);
55 N(pos == string::npos,
56 F("bad character '%c' in id name '%s'") % val().at(pos) % val);
57
58 val.ok = true;
59}
60
61inline void
62verify(ace & val)
63{
64 if (val.ok)
65 return;
66
67 string::size_type pos = val().find_first_not_of(constants::legal_ace_bytes);
68 N(pos == string::npos,
69 F("bad character '%c' in ace string '%s'") % val().at(pos) % val);
70
71 val.ok = true;
72}
73
74
75inline void
76verify(cert_name & val)
77{
78 if (val.ok)
79 return;
80
81 string::size_type pos = val().find_first_not_of(constants::legal_cert_name_bytes);
82 N(pos == string::npos,
83 F("bad character '%c' in cert name '%s'") % val().at(pos) % val);
84
85 val.ok = true;
86}
87
88inline void
89verify(rsa_keypair_id & val)
90{
91 if (val.ok)
92 return;
93
94 string::size_type pos = val().find_first_not_of(constants::legal_key_name_bytes);
95 N(pos == string::npos,
96 F("bad character '%c' in key name '%s'") % val().at(pos) % val);
97
98 val.ok = true;
99}
100
101inline void
102verify(netsync_session_key & val)
103{
104 if (val.ok)
105 return;
106
107 if (val().size() == 0)
108 {
109 val.s.append(constants::netsync_session_key_length_in_bytes, 0);
110 return;
111 }
112
113 N(val().size() == constants::netsync_session_key_length_in_bytes,
114 F("Invalid key length of %d bytes") % val().length());
115
116 val.ok = true;
117}
118
119inline void
120verify(netsync_hmac_value & val)
121{
122 if (val.ok)
123 return;
124
125 if (val().size() == 0)
126 {
127 val.s.append(constants::netsync_hmac_value_length_in_bytes, 0);
128 return;
129 }
130
131 N(val().size() == constants::netsync_hmac_value_length_in_bytes,
132 F("Invalid hmac length of %d bytes") % val().length());
133
134 val.ok = true;
135}
136
137
138// instantiation of various vocab functions
139
140#define ATOMIC(ty) \
141 \
142ty::ty(string const & str) : \
143 s(str), ok(false) \
144{ verify(*this); } \
145 \
146ty::ty(ty const & other) : \
147 s(other.s), ok(other.ok) \
148{ verify(*this); } \
149 \
150ty const & ty::operator=(ty const & other) \
151{ s = other.s; ok = other.ok; \
152 verify(*this); return *this; } \
153 \
154ostream & operator<<(ostream & o, \
155 ty const & a) \
156{ return (o << a.s); } \
157 \
158void dump(ty const & obj, std::string & out) \
159{ out = obj(); }
160
161#define ATOMIC_NOVERIFY(ty) ATOMIC(ty)
162
163
164
165#define ENCODING(enc) \
166 \
167template<typename INNER> \
168enc<INNER>::enc(string const & s) : i(s), ok(false) \
169 { verify(*this); } \
170 \
171template<typename INNER> \
172enc<INNER>::enc(enc<INNER> const & other) \
173 : i(other.i()), ok(other.ok) { verify(*this); } \
174 \
175template<typename INNER> \
176enc<INNER>::enc(INNER const & inner) : \
177 i(inner), ok(false) \
178 { verify(*this); } \
179 \
180template<typename INNER> \
181enc<INNER> const & \
182enc<INNER>::operator=(enc<INNER> const & other) \
183 { i = other.i; ok = other.ok; \
184 verify(*this); return *this;} \
185 \
186template <typename INNER> \
187ostream & operator<<(ostream & o, enc<INNER> const & e) \
188{ return (o << e.i); } \
189 \
190template <typename INNER> \
191void dump(enc<INNER> const & obj, std::string & out) \
192{ out = obj(); }
193
194
195#define DECORATE(dec) \
196 \
197template<typename INNER> \
198dec<INNER>::dec(dec<INNER> const & other) \
199 : i(other.i), ok(other.ok) { verify(*this); } \
200 \
201template<typename INNER> \
202dec<INNER>::dec(INNER const & inner) : \
203 i(inner), ok(false) \
204 { verify(*this); } \
205 \
206template<typename INNER> \
207dec<INNER> const & \
208dec<INNER>::operator=(dec<INNER> const & other) \
209 { i = other.i; ok = other.ok; \
210 verify(*this); return *this;} \
211 \
212template <typename INNER> \
213ostream & operator<<(ostream & o, dec<INNER> const & d) \
214{ return (o << d.i); } \
215 \
216template <typename INNER> \
217void dump(dec<INNER> const & obj, std::string & out) \
218{ dump(obj.inner(), out); }
219
220#define EXTERN
221
222#include "vocab_terms.hh"
223
224#undef EXTERN
225#undef ATOMIC
226#undef DECORATE
227
228template class revision<cert>;
229template class manifest<cert>;
230
231// the rest is unit tests
232
233#ifdef BUILD_UNIT_TESTS
234#include "unit_tests.hh"
235
236void add_vocab_tests(test_suite * suite)
237{
238 I(suite);
239 // None, ATM.
240}
241
242#endif // BUILD_UNIT_TESTS

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status