1 // Copyright (C) 2002 Graydon Hoare <graydon@pobox.com>
2 //
3 // This program is made available under the GNU GPL version 2.0 or
4 // greater. See the accompanying file COPYING for details.
5 //
6 // This program is distributed WITHOUT ANY WARRANTY; without even the
7 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE.
9 
10 #ifndef __TRANSFORMS_HH__
11 #define __TRANSFORMS_HH__
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 
20 namespace 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.
32 template<typename XFM> std::string
xform(std::string const & in,origin::type made_from)33 xform(std::string const & in, origin::type made_from)
34 {
35   enum dummy { d = (sizeof(struct xform_must_be_specialized_for_this_type)
36                     == sizeof(XFM)) };
37   return in; // avoid warnings about no return statement
38 }
39 
40 // these specializations of the template are defined in transforms.cc
41 template<> std::string
42 xform<Botan::Base64_Encoder>(std::string const &, origin::type);
43 template<> std::string
44 xform<Botan::Base64_Decoder>(std::string const &, origin::type);
45 template<> std::string
46 xform<Botan::Hex_Encoder>(std::string const &, origin::type);
47 template<> std::string
48 xform<Botan::Hex_Decoder>(std::string const &, origin::type);
49 template<> std::string
50 xform<Botan::Gzip_Compression>(std::string const &, origin::type);
51 template<> std::string
52 xform<Botan::Gzip_Decompression>(std::string const &, origin::type);
53 
54 // base64 encoding
55 
56 template <typename T>
encode_base64(T const & in)57 base64<T> encode_base64(T const & in)
58 {
59   return base64<T>(xform<Botan::Base64_Encoder>(in(), in.made_from),
60                    in.made_from);
61 }
62 
63 template <typename T>
decode_base64(base64<T> const & in)64 T decode_base64(base64<T> const & in)
65 {
66   return T(xform<Botan::Base64_Decoder>(in(), in.made_from), in.made_from);
67 }
68 
69 template <typename T>
decode_base64_as(std::string const & in,origin::type made_from)70 T decode_base64_as(std::string const & in, origin::type made_from)
71 {
72   return T(xform<Botan::Base64_Decoder>(in, made_from), made_from);
73 }
74 
75 template <>
76 std::string decode_base64_as<std::string>(std::string const & in,
77                                           origin::type made_from);
78 
79 // hex encoding
80 
81 template <typename T>
encode_hexenc(T const & in,hexenc<T> & out)82 void encode_hexenc(T const & in, hexenc<T> & out)
83 {
84   out = hexenc<T>(xform<Botan::Hex_Encoder>(in(), in.made_from),
85                   in.made_from);
86 }
87 
88 template <typename T>
decode_hexenc(hexenc<T> const & in,T & out)89 void decode_hexenc(hexenc<T> const & in, T & out)
90 {
91   out = T(xform<Botan::Hex_Decoder>(in(), in.made_from),
92           in.made_from);
93 }
94 
encode_hexenc(std::string const & in,origin::type made_from)95 inline std::string encode_hexenc(std::string const & in,
96                                  origin::type made_from)
97 { return xform<Botan::Hex_Encoder>(in, made_from); }
decode_hexenc(std::string const & in,origin::type made_from)98 inline std::string decode_hexenc(std::string const & in,
99                                  origin::type made_from)
100 { return xform<Botan::Hex_Decoder>(in, made_from); }
decode_hexenc_as(std::string const & in,origin::type made_from)101 template<typename T> T decode_hexenc_as(std::string const & in,
102                                                origin::type made_from)
103 { return T(decode_hexenc(in, made_from), made_from); }
104 
105 
106 // gzip
107 
108 template <typename T>
encode_gzip(T const & in,gzip<T> & out)109 void encode_gzip(T const & in, gzip<T> & out)
110 {
111   out = gzip<T>(xform<Botan::Gzip_Compression>(in(), in.made_from),
112                 in.made_from);
113 }
114 
115 template <typename T>
decode_gzip(gzip<T> const & in,T & out)116 void decode_gzip(gzip<T> const & in, T & out)
117 {
118   out = T(xform<Botan::Gzip_Decompression>(in(), in.made_from),
119           in.made_from);
120 }
121 
122 // string variant for netsync
123 template <typename T>
encode_gzip(std::string const & in,gzip<T> & out,origin::type made_from)124 void encode_gzip(std::string const & in, gzip<T> & out, origin::type made_from)
125 { out = xform<Botan::Gzip_Compression>(in, made_from); }
126 
127 // both at once (this is relatively common)
128 // these are usable for T = data and T = delta
129 
130 template <typename T>
131 void pack(T const & in, base64< gzip<T> > & out);
132 
133 template <typename T>
134 void unpack(base64< gzip<T> > const & in, T & out);
135 
136 
137 // version (a.k.a. sha1 fingerprint) calculation
138 
139 void calculate_ident(data const & dat,
140                      id & ident);
141 
142 void calculate_ident(file_data const & dat,
143                      file_id & ident);
144 
145 void calculate_ident(manifest_data const & dat,
146                      manifest_id & ident);
147 
148 void calculate_ident(revision_data const & dat,
149                      revision_id & ident);
150 
151 #endif // __TRANSFORMS_HH__
152 
153 // Local Variables:
154 // mode: C++
155 // fill-column: 76
156 // c-file-style: "gnu"
157 // indent-tabs-mode: nil
158 // End:
159 // vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s:
160