monotone

monotone Mtn Source Tree

Root/transforms.hh

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
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
33xform(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
41template<> std::string
42xform<Botan::Base64_Encoder>(std::string const &, origin::type);
43template<> std::string
44xform<Botan::Base64_Decoder>(std::string const &, origin::type);
45template<> std::string
46xform<Botan::Hex_Encoder>(std::string const &, origin::type);
47template<> std::string
48xform<Botan::Hex_Decoder>(std::string const &, origin::type);
49template<> std::string
50xform<Botan::Gzip_Compression>(std::string const &, origin::type);
51template<> std::string
52xform<Botan::Gzip_Decompression>(std::string const &, origin::type);
53
54// base64 encoding
55
56template <typename T>
57base64<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
63template <typename T>
64T decode_base64(base64<T> const & in)
65{
66 return T(xform<Botan::Base64_Decoder>(in(), in.made_from), in.made_from);
67}
68
69template <typename T>
70T 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
75template <>
76std::string decode_base64_as<std::string>(std::string const & in,
77 origin::type made_from);
78
79// hex encoding
80
81template <typename T>
82void 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
88template <typename T>
89void 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
95inline std::string encode_hexenc(std::string const & in,
96 origin::type made_from)
97{ return xform<Botan::Hex_Encoder>(in, made_from); }
98inline std::string decode_hexenc(std::string const & in,
99 origin::type made_from)
100{ return xform<Botan::Hex_Decoder>(in, made_from); }
101template<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
108template <typename T>
109void 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
115template <typename T>
116void 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
123template <typename T>
124void 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
130template <typename T>
131void pack(T const & in, base64< gzip<T> > & out);
132
133template <typename T>
134void unpack(base64< gzip<T> > const & in, T & out);
135
136
137// version (a.k.a. sha1 fingerprint) calculation
138
139void calculate_ident(data const & dat,
140 id & ident);
141
142void calculate_ident(file_data const & dat,
143 file_id & ident);
144
145void calculate_ident(manifest_data const & dat,
146 manifest_id & ident);
147
148void 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:

Archive Download this file

Branches

Tags

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