monotone

monotone Mtn Source Tree

Root/transforms.hh

1#ifndef __TRANSFORMS_HH__
2#define __TRANSFORMS_HH__
3
4// Copyright (C) 2002 Graydon Hoare <graydon@pobox.com>
5//
6// This program is made available under the GNU GPL version 2.0 or
7// greater. See the accompanying file COPYING for details.
8//
9// This program is distributed WITHOUT ANY WARRANTY; without even the
10// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11// PURPOSE.
12
13#include "vocab.hh"
14
15// this file contans various sorts of string transformations. each
16// transformation should be self-explanatory from its type signature. see
17// transforms.cc for the implementations (most of which are delegations to
18// crypto++ and librsync)
19
20namespace Botan {
21 class Base64_Encoder;
22 class Base64_Decoder;
23 class Hex_Encoder;
24 class Hex_Decoder;
25 class Gzip_Compression;
26 class Gzip_Decompression;
27}
28
29// this generic template cannot actually be used, except with the
30// specializations given below. give a compile error instead of a link
31// error.
32template<typename XFM> std::string xform(std::string const & in)
33{
34 enum dummy { d = (sizeof(struct xform_must_be_specialized_for_this_type)
35 == sizeof(XFM)) };
36 return in; // avoid warnings about no return statement
37}
38
39// these specializations of the template are defined in transforms.cc
40template<> std::string xform<Botan::Base64_Encoder>(std::string const &);
41template<> std::string xform<Botan::Base64_Decoder>(std::string const &);
42template<> std::string xform<Botan::Hex_Encoder>(std::string const &);
43template<> std::string xform<Botan::Hex_Decoder>(std::string const &);
44template<> std::string xform<Botan::Gzip_Compression>(std::string const &);
45template<> std::string xform<Botan::Gzip_Decompression>(std::string const &);
46
47// base64 encoding
48
49template <typename T>
50void encode_base64(T const & in, base64<T> & out)
51{ out = base64<T>(T(xform<Botan::Base64_Encoder>(in()))); }
52
53template <typename T>
54void decode_base64(base64<T> const & in, T & out)
55{ out = T(xform<Botan::Base64_Decoder>(in())); }
56
57
58// hex encoding
59
60template <typename T>
61void encode_hexenc(T const & in, hexenc<T> & out)
62{ out = hexenc<T>(T(xform<Botan::Hex_Encoder>(in()))); }
63
64template <typename T>
65void decode_hexenc(hexenc<T> const & in, T & out)
66{ out = T(xform<Botan::Hex_Decoder>(in())); }
67
68inline std::string encode_hexenc(std::string const & in)
69{ return xform<Botan::Hex_Encoder>(in); }
70inline std::string decode_hexenc(std::string const & in)
71{ return xform<Botan::Hex_Decoder>(in); }
72
73
74// gzip
75
76template <typename T>
77void encode_gzip(T const & in, gzip<T> & out)
78{ out = gzip<T>(xform<Botan::Gzip_Compression>(in())); }
79
80template <typename T>
81void decode_gzip(gzip<T> const & in, T & out)
82{ out = T(xform<Botan::Gzip_Decompression>(in())); }
83
84// string variant for netsync
85template <typename T>
86void encode_gzip(std::string const & in, gzip<T> & out)
87{ out = xform<Botan::Gzip_Compression>(in); }
88
89// both at once (this is relatively common)
90// these are usable for T = data and T = delta
91
92template <typename T>
93void pack(T const & in, base64< gzip<T> > & out);
94
95template <typename T>
96void unpack(base64< gzip<T> > const & in, T & out);
97
98
99// diffing and patching
100
101void diff(data const & olddata,
102 data const & newdata,
103 delta & del);
104
105void patch(data const & olddata,
106 delta const & del,
107 data & newdata);
108
109
110// version (a.k.a. sha1 fingerprint) calculation
111
112void calculate_ident(data const & dat,
113 hexenc<id> & ident);
114
115void calculate_ident(file_data const & dat,
116 file_id & ident);
117
118void calculate_ident(manifest_data const & dat,
119 manifest_id & ident);
120
121void calculate_ident(revision_data const & dat,
122 revision_id & ident);
123
124// canonicalize base64 encoding
125std::string canonical_base64(std::string const & s);
126
127
128// Local Variables:
129// mode: C++
130// fill-column: 76
131// c-file-style: "gnu"
132// indent-tabs-mode: nil
133// End:
134// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s:
135
136#endif // __TRANSFORMS_HH__

Archive Download this file

Branches

Tags

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