File: transforms.cc

package info (click to toggle)
monotone 0.48-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 20,096 kB
  • ctags: 8,077
  • sloc: cpp: 81,000; sh: 6,402; perl: 1,241; lisp: 1,045; makefile: 655; python: 566; sql: 112; ansic: 52
file content (308 lines) | stat: -rw-r--r-- 9,677 bytes parent folder | download
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// Copyright (C) 2002 Graydon Hoare <graydon@pobox.com>
//
// This program is made available under the GNU GPL version 2.0 or
// greater. See the accompanying file COPYING for details.
//
// This program is distributed WITHOUT ANY WARRANTY; without even the
// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE.

#include "base.hh"
#include <iterator>
#include <botan/botan.h>
#include <botan/sha160.h>

#include "botan_pipe_cache.hh"
#include "gzip.hh"

#include "transforms.hh"
#include "char_classifiers.hh"

using std::string;
using Botan::Pipe;
using Botan::Base64_Encoder;
using Botan::Base64_Decoder;
using Botan::Hex_Encoder;
using Botan::Hex_Decoder;
using Botan::Gzip_Compression;
using Botan::Gzip_Decompression;
using Botan::Hash_Filter;

// this file contans various sorts of string transformations. each
// transformation should be self-explanatory from its type signature. see
// transforms.hh for the summary.

// NB this file uses very "value-centric" functional approach; even though
// many of the underlying transformations are "stream-centric" and the
// underlying libraries (eg. crypto++) are stream oriented. this will
// probably strike some people as contemptably inefficient, since it means
// that occasionally 1, 2, or even 3 copies of an entire file will wind up
// in memory at once. I am taking this approach for 3 reasons: first, I
// want the type system to help me and value types are much easier to work
// with than stream types. second, it is *much* easier to debug a program
// that operates on values than streams, and correctness takes precedence
// over all other features of this program. third, this is a peer-to-peer
// sort of program for small-ish source-code text files, not a fileserver,
// and is memory-limited anyways (for example, storing things in sqlite
// requires they be able to fit in memory). you're hopefully not going to
// be dealing with hundreds of users hammering on locks and memory
// concurrently.
//
// if future analysis proves these assumptions wrong, feel free to revisit
// the matter, but bring strong evidence along with you that the stream
// paradigm "must" be used. this program is intended for source code
// control and I make no bones about it.

NORETURN(static inline void error_in_transform(Botan::Exception & e));

static inline void
error_in_transform(Botan::Exception & e, origin::type caused_by)
{
  // these classes can all indicate data corruption
  if (typeid(e) == typeid(Botan::Encoding_Error)
      || typeid(e) == typeid(Botan::Decoding_Error)
      || typeid(e) == typeid(Botan::Stream_IO_Error)
      || typeid(e) == typeid(Botan::Integrity_Failure))
    {
      // clean up the what() string a little: throw away the
      // "botan: TYPE: " part...
      string w(e.what());
      string::size_type pos = w.find(':');
      pos = w.find(':', pos+1);
      w = string(w.begin() + pos + 2, w.end());

      // ... downcase the rest of it and replace underscores with spaces.
      for (string::iterator p = w.begin(); p != w.end(); p++)
        {
          *p = to_lower(*p);
          if (*p == '_')
            *p = ' ';
        }

      E(false, caused_by,
        F("%s\n"
          "this may be due to a memory glitch, data corruption during\n"
          "a network transfer, corruption of your database or workspace,\n"
          "or a bug in monotone.  if the error persists, please contact\n"
          "%s for assistance.\n")
        % w % PACKAGE_BUGREPORT);
    }
  else
    throw;

  I(false);  // can't get here
}

// full specializations for the usable cases of xform<XFM>()
// use extra error checking in base64 and hex decoding
#define SPECIALIZE_XFORM(T, carg)                               \
  template<> string xform<T>(string const & in, origin::type made_from) \
  {                                                             \
    string out;                                                 \
    static cached_botan_pipe pipe(new Pipe(new T(carg)));       \
    try                                                         \
      {                                                         \
        /* this might actually be a problem here */             \
        I(pipe->message_count() < Pipe::LAST_MESSAGE);          \
        pipe->process_msg(in);                                  \
        out = pipe->read_all_as_string(Pipe::LAST_MESSAGE);     \
      }                                                         \
    catch (Botan::Exception & e)                                \
      {                                                         \
        pipe.reset(new Pipe(new T(carg)));                      \
        error_in_transform(e, made_from);                       \
      }                                                         \
    return out;                                                 \
  }

SPECIALIZE_XFORM(Base64_Encoder,);
SPECIALIZE_XFORM(Base64_Decoder, Botan::IGNORE_WS);
//SPECIALIZE_XFORM(Hex_Encoder, Hex_Encoder::Lowercase);
template<> string xform<Botan::Hex_Encoder>(string const & in,
                                            origin::type made_from)
{
  string out;
  out.reserve(in.size()<<1);
  for (string::const_iterator i = in.begin();
       i != in.end(); ++i)
    {
      int h = (*i>>4) & 0x0f;
      if (h < 10)
        out.push_back(h + '0');
      else
        out.push_back(h + 'a' - 10);
      int l = *i & 0x0f;
      if (l < 10)
        out.push_back(l + '0');
      else
        out.push_back(l + 'a' - 10);
    }
  return out;
}
//SPECIALIZE_XFORM(Hex_Decoder, Botan::IGNORE_WS);
template<> string xform<Botan::Hex_Decoder>(string const & in,
                                            origin::type made_from)
{
  string out;
  out.reserve(in.size()>>1);
  bool high(true);
  int o = 0;
  for (string::const_iterator i = in.begin();
       i != in.end(); ++i)
    {
      int c = *i;
      if (c >= '0' && c <= '9')
        {
          o += (c - '0');
        }
      else if (c >= 'a' && c <= 'f')
        {
          o += (c - 'a' + 10);
        }
      else if (c >= 'A' && c <= 'F')
        {
          o += (c - 'A' + 10);
        }
      else if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
        {
          continue;
        }
      else // garbage
        {
          try
            {
              throw Botan::Decoding_Error(string("invalid hex character '") + (char)c + "'");
            }
          catch(Botan::Exception & e)
            {
              error_in_transform(e, made_from);
            }
        }
      if (high)
        {
          o <<= 4;
        }
      else
        {
          out.push_back(o);
          o = 0;
        }
      high = !high;
    }
  if (!high)
    { // Hex string wasn't a whole number of bytes
      //I(false); // Drop the last char (!!)
    }
  return out;
}
SPECIALIZE_XFORM(Gzip_Compression,);
SPECIALIZE_XFORM(Gzip_Decompression,);

template <>
std::string decode_base64_as<std::string>(std::string const & in,
                                          origin::type made_from)
{
  return xform<Botan::Base64_Decoder>(in, made_from);
}

template <typename T>
void pack(T const & in, base64< gzip<T> > & out)
{
  string tmp;
  tmp.reserve(in().size()); // FIXME: do some benchmarking and make this a constant::

  static cached_botan_pipe pipe(new Pipe(new Gzip_Compression,
                                         new Base64_Encoder));
  try
    {
      pipe->process_msg(in());
      tmp = pipe->read_all_as_string(Pipe::LAST_MESSAGE);
      out = base64< gzip<T> >(tmp, in.made_from);
    }
  catch (Botan::Exception & e)
    {
      pipe.reset(new Pipe(new Gzip_Compression,
                          new Base64_Encoder));
      error_in_transform(e, in.made_from);
    }
}

template <typename T>
void unpack(base64< gzip<T> > const & in, T & out)
{
  static cached_botan_pipe pipe(new Pipe(new Base64_Decoder,
                                         new Gzip_Decompression));
  try
    {
      pipe->process_msg(in());
      out = T(pipe->read_all_as_string(Pipe::LAST_MESSAGE), in.made_from);
    }
  catch (Botan::Exception & e)
    {
      pipe.reset(new Pipe(new Base64_Decoder,
                          new Gzip_Decompression));
      error_in_transform(e, in.made_from);
    }
}

// specialise them
template void pack<data>(data const &, base64< gzip<data> > &);
template void pack<delta>(delta const &, base64< gzip<delta> > &);
template void unpack<data>(base64< gzip<data> > const &, data &);
template void unpack<delta>(base64< gzip<delta> > const &, delta &);


// identifier (a.k.a. sha1 signature) calculation

void
calculate_ident(data const & dat,
                id & ident)
{
  static cached_botan_pipe p(new Pipe(new Hash_Filter("SHA-160")));
  try
    {
      p->process_msg(dat());
      ident = id(p->read_all_as_string(Pipe::LAST_MESSAGE), dat.made_from);
    }
  catch (Botan::Exception & e)
    {
      p.reset(new Pipe(new Hash_Filter("SHA-160")));
      error_in_transform(e, dat.made_from);
    }
}

void
calculate_ident(file_data const & dat,
                file_id & ident)
{
  id tmp;
  calculate_ident(dat.inner(), tmp);
  ident = file_id(tmp);
}

void
calculate_ident(manifest_data const & dat,
                manifest_id & ident)
{
  id tmp;
  calculate_ident(dat.inner(), tmp);
  ident = manifest_id(tmp);
}

void
calculate_ident(revision_data const & dat,
                revision_id & ident)
{
  id tmp;
  calculate_ident(dat.inner(), tmp);
  ident = revision_id(tmp);
}


// Local Variables:
// mode: C++
// fill-column: 76
// c-file-style: "gnu"
// indent-tabs-mode: nil
// End:
// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s: