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>
50base64<T> encode_base64(T const & in)
51{ return base64<T>(xform<Botan::Base64_Encoder>(in())); }
52
53template <typename T>
54T decode_base64(base64<T> const & in)
55{ return T(xform<Botan::Base64_Decoder>(in())); }
56
57template <typename T>
58T decode_base64_as(std::string const & in)
59{
60 return T(xform<Botan::Base64_Decoder>(in));
61}
62// hex encoding
63
64template <typename T>
65void encode_hexenc(T const & in, hexenc<T> & out)
66{ out = hexenc<T>(xform<Botan::Hex_Encoder>(in())); }
67
68template <typename T>
69void decode_hexenc(hexenc<T> const & in, T & out)
70{ out = T(xform<Botan::Hex_Decoder>(in())); }
71
72inline std::string encode_hexenc(std::string const & in)
73{ return xform<Botan::Hex_Encoder>(in); }
74inline std::string decode_hexenc(std::string const & in)
75{ return xform<Botan::Hex_Decoder>(in); }
76
77
78// gzip
79
80template <typename T>
81void encode_gzip(T const & in, gzip<T> & out)
82{ out = gzip<T>(xform<Botan::Gzip_Compression>(in())); }
83
84template <typename T>
85void decode_gzip(gzip<T> const & in, T & out)
86{ out = T(xform<Botan::Gzip_Decompression>(in())); }
87
88// string variant for netsync
89template <typename T>
90void encode_gzip(std::string const & in, gzip<T> & out)
91{ out = xform<Botan::Gzip_Compression>(in); }
92
93// both at once (this is relatively common)
94// these are usable for T = data and T = delta
95
96template <typename T>
97void pack(T const & in, base64< gzip<T> > & out);
98
99template <typename T>
100void unpack(base64< gzip<T> > const & in, T & out);
101
102
103// version (a.k.a. sha1 fingerprint) calculation
104
105void calculate_ident(data const & dat,
106 id & ident);
107
108void calculate_ident(file_data const & dat,
109 file_id & ident);
110
111void calculate_ident(manifest_data const & dat,
112 manifest_id & ident);
113
114void calculate_ident(revision_data const & dat,
115 revision_id & ident);
116
117// Local Variables:
118// mode: C++
119// fill-column: 76
120// c-file-style: "gnu"
121// indent-tabs-mode: nil
122// End:
123// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s:
124
125#endif // __TRANSFORMS_HH__

Archive Download this file

Branches

Tags

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