monotone

monotone Mtn Source Tree

Root/transforms.hh

1#ifndef __TRANSFORMS_HH__
2#define __TRANSFORMS_HH__
3
4// copyright (C) 2002, 2003 graydon hoare <graydon@pobox.com>
5// all rights reserved.
6// licensed to the public under the terms of the GNU GPL (>= 2)
7// see the file COPYING for details
8
9#include "vocab.hh"
10#include "manifest.hh"
11#include "lua.hh"
12
13#include <vector>
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 CryptoPP {
21 class Base64Encoder;
22 class Base64Decoder;
23 class HexEncoder;
24 class HexDecoder;
25 class Gzip;
26 class Gunzip;
27}
28
29template<typename XFM> std::string xform(std::string const &);
30extern template std::string xform<CryptoPP::Base64Encoder>(std::string const &);
31extern template std::string xform<CryptoPP::Base64Decoder>(std::string const &);
32extern template std::string xform<CryptoPP::HexEncoder>(std::string const &);
33extern template std::string xform<CryptoPP::HexDecoder>(std::string const &);
34extern template std::string xform<CryptoPP::Gzip>(std::string const &);
35extern template std::string xform<CryptoPP::Gunzip>(std::string const &);
36
37// base64 encoding
38
39template <typename T>
40void encode_base64(T const & in, base64<T> & out)
41{ out = xform<CryptoPP::Base64Encoder>(in()); }
42
43template <typename T>
44void decode_base64(base64<T> const & in, T & out)
45{ out = xform<CryptoPP::Base64Decoder>(in()); }
46
47
48// hex encoding
49
50std::string uppercase(std::string const & in);
51std::string lowercase(std::string const & in);
52
53template <typename T>
54void decode_hexenc(hexenc<T> const & in, T & out)
55{ out = xform<CryptoPP::HexDecoder>(uppercase(in())); }
56
57template <typename T>
58void encode_hexenc(T const & in, hexenc<T> & out)
59{ out = lowercase(xform<CryptoPP::HexEncoder>(in())); }
60
61
62// gzip
63
64template <typename T>
65void encode_gzip(T const & in, gzip<T> & out)
66{ out = xform<CryptoPP::Gzip>(in()); }
67
68template <typename T>
69void decode_gzip(gzip<T> const & in, T & out)
70{ out = xform<CryptoPP::Gunzip>(in()); }
71
72
73// both at once (this is relatively common)
74
75template <typename T>
76void pack(T const & in, base64< gzip<T> > & out)
77{
78 gzip<T> tmp;
79 encode_gzip(in, tmp);
80 encode_base64(tmp, out);
81}
82
83template <typename T>
84void unpack(base64< gzip<T> > const & in, T & out)
85{
86 gzip<T> tmp;
87 decode_base64(in, tmp);
88 decode_gzip(tmp, out);
89}
90
91
92// diffing and patching
93
94void diff(data const & olddata,
95 data const & newdata,
96 base64< gzip<delta> > & del);
97
98void diff(manifest_map const & oldman,
99 manifest_map const & newman,
100 base64< gzip<delta> > & del);
101
102void diff(base64< gzip<data> > const & old_data,
103 base64< gzip<data> > const & new_data,
104 base64< gzip<delta> > & delta);
105
106void patch(data const & olddata,
107 base64< gzip<delta> > const & del,
108 data & newdata);
109
110void patch(base64< gzip<data> > const & old_data,
111 base64< gzip<delta> > const & delta,
112 base64< gzip<data> > & new_data);
113
114// version (a.k.a. sha1 fingerprint) calculation
115
116void calculate_ident(data const & dat,
117 hexenc<id> & ident);
118
119void calculate_ident(base64< gzip<data> > const & dat,
120 hexenc<id> & ident);
121
122void calculate_ident(file_data const & dat,
123 file_id & ident);
124
125void calculate_ident(manifest_data const & dat,
126 manifest_id & ident);
127
128void calculate_ident(manifest_map const & mm,
129 manifest_id & ident);
130
131
132// quick streamy variant which doesn't necessarily load the whole file
133
134void calculate_ident(file_path const & file,
135 hexenc<id> & ident,
136 lua_hooks & lua);
137
138void split_into_lines(std::string const & in,
139 std::vector<std::string> & out);
140
141void join_lines(std::vector<std::string> const & in,
142std::string & out,
143std::string const & linesep);
144
145void join_lines(std::vector<std::string> const & in,
146std::string & out);
147
148// remove all whitespace
149std::string remove_ws(std::string const & s);
150
151// remove leading and trailing whitespace
152std::string trim_ws(std::string const & s);
153
154// canonicalize base64 encoding
155std::string canonical_base64(std::string const & s);
156
157// charset conversions
158void charset_convert(std::string const & src_charset, std::string const & dst_charset,
159 std::string const & src, std::string & dst);
160void system_to_utf8(external const & system, utf8 & utf);
161void utf8_to_system(utf8 const & utf, external & system);
162void ace_to_utf8(ace const & ac, utf8 & utf);
163void utf8_to_ace(utf8 const & utf, ace & a);
164void utf8_to_urlenc(utf8 const & utf, urlenc & u);
165void urlenc_to_utf8(urlenc const & u, utf8 & utf);
166
167// specific internal / external conversions for various vocab terms
168void internalize_url(utf8 const & utf, url & u);
169void internalize_url(external const & ext, url & u);
170void externalize_url(url const & u, utf8 & utf);
171void externalize_url(url const & u, external & ext);
172void internalize_cert_name(utf8 const & utf, cert_name & c);
173void internalize_cert_name(external const & ext, cert_name & c);
174void externalize_cert_name(cert_name const & c, utf8 & utf);
175void externalize_cert_name(cert_name const & c, external & ext);
176void internalize_rsa_keypair_id(utf8 const & utf, rsa_keypair_id & key);
177void internalize_rsa_keypair_id(external const & ext, rsa_keypair_id & key);
178void externalize_rsa_keypair_id(rsa_keypair_id const & key, utf8 & utf);
179void externalize_rsa_keypair_id(rsa_keypair_id const & key, external & ext);
180
181// line-ending conversion
182void line_end_convert(std::string const & linesep, std::string const & src, std::string & dst);
183
184
185
186#endif // __TRANSFORMS_HH__

Archive Download this file

Branches

Tags

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