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 "revision.hh"
10#include "lua.hh"
11#include "manifest.hh"
12#include "vocab.hh"
13
14#include <vector>
15
16// this file contans various sorts of string transformations. each
17// transformation should be self-explanatory from its type signature. see
18// transforms.cc for the implementations (most of which are delegations to
19// crypto++ and librsync)
20
21namespace CryptoPP {
22 class Base64Encoder;
23 class Base64Decoder;
24 class HexEncoder;
25 class HexDecoder;
26 class Gzip;
27 class Gunzip;
28}
29
30template<typename XFM> std::string xform(std::string const &);
31extern template std::string xform<CryptoPP::Base64Encoder>(std::string const &);
32extern template std::string xform<CryptoPP::Base64Decoder>(std::string const &);
33extern template std::string xform<CryptoPP::HexEncoder>(std::string const &);
34extern template std::string xform<CryptoPP::HexDecoder>(std::string const &);
35extern template std::string xform<CryptoPP::Gzip>(std::string const &);
36extern template std::string xform<CryptoPP::Gunzip>(std::string const &);
37
38// base64 encoding
39
40template <typename T>
41void encode_base64(T const & in, base64<T> & out)
42{ out = xform<CryptoPP::Base64Encoder>(in()); }
43
44template <typename T>
45void decode_base64(base64<T> const & in, T & out)
46{ out = xform<CryptoPP::Base64Decoder>(in()); }
47
48
49// hex encoding
50
51std::string uppercase(std::string const & in);
52std::string lowercase(std::string const & in);
53
54template <typename T>
55void decode_hexenc(hexenc<T> const & in, T & out)
56{ out = xform<CryptoPP::HexDecoder>(uppercase(in())); }
57
58template <typename T>
59void encode_hexenc(T const & in, hexenc<T> & out)
60{ out = lowercase(xform<CryptoPP::HexEncoder>(in())); }
61
62
63// gzip
64
65template <typename T>
66void encode_gzip(T const & in, gzip<T> & out)
67{ out = xform<CryptoPP::Gzip>(in()); }
68
69template <typename T>
70void decode_gzip(gzip<T> const & in, T & out)
71{ out = xform<CryptoPP::Gunzip>(in()); }
72
73
74// both at once (this is relatively common)
75
76template <typename T>
77void pack(T const & in, base64< gzip<T> > & out)
78{
79 gzip<T> tmp;
80 encode_gzip(in, tmp);
81 encode_base64(tmp, out);
82}
83
84template <typename T>
85void unpack(base64< gzip<T> > const & in, T & out)
86{
87 gzip<T> tmp;
88 decode_base64(in, tmp);
89 decode_gzip(tmp, out);
90}
91
92
93// diffing and patching
94
95void diff(data const & olddata,
96 data const & newdata,
97 base64< gzip<delta> > & del);
98
99void diff(manifest_map const & oldman,
100 manifest_map const & newman,
101 base64< gzip<delta> > & del);
102
103void diff(base64< gzip<data> > const & old_data,
104 base64< gzip<data> > const & new_data,
105 base64< gzip<delta> > & delta);
106
107void patch(data const & olddata,
108 base64< gzip<delta> > const & del,
109 data & newdata);
110
111void patch(base64< gzip<data> > const & old_data,
112 base64< gzip<delta> > const & delta,
113 base64< gzip<data> > & new_data);
114
115// version (a.k.a. sha1 fingerprint) calculation
116
117void calculate_ident(data const & dat,
118 hexenc<id> & ident);
119
120void calculate_ident(base64< gzip<data> > const & dat,
121 hexenc<id> & ident);
122
123void calculate_ident(file_data const & dat,
124 file_id & ident);
125
126void calculate_ident(manifest_data const & dat,
127 manifest_id & ident);
128
129void calculate_ident(manifest_map const & mm,
130 manifest_id & ident);
131
132void calculate_ident(revision_data const & dat,
133 revision_id & ident);
134
135void calculate_ident(revision_set const & cs,
136 revision_id & ident);
137
138
139// quick streamy variant which doesn't necessarily load the whole file
140
141void calculate_ident(file_path const & file,
142 hexenc<id> & ident,
143 lua_hooks & lua);
144
145void split_into_lines(std::string const & in,
146 std::vector<std::string> & out);
147
148void split_into_lines(std::string const & in,
149 std::string const & encoding,
150 std::vector<std::string> & out);
151
152void join_lines(std::vector<std::string> const & in,
153 std::string & out,
154 std::string const & linesep);
155
156void join_lines(std::vector<std::string> const & in,
157 std::string & out);
158
159void prefix_lines_with(std::string const & prefix,
160 std::string const & lines,
161 std::string & out);
162
163// remove all whitespace
164std::string remove_ws(std::string const & s);
165
166// remove leading and trailing whitespace
167std::string trim_ws(std::string const & s);
168
169// canonicalize base64 encoding
170std::string canonical_base64(std::string const & s);
171
172// charset conversions
173void charset_convert(std::string const & src_charset, std::string const & dst_charset,
174 std::string const & src, std::string & dst);
175void system_to_utf8(external const & system, utf8 & utf);
176void utf8_to_system(utf8 const & utf, external & system);
177void ace_to_utf8(ace const & ac, utf8 & utf);
178void utf8_to_ace(utf8 const & utf, ace & a);
179
180// specific internal / external conversions for various vocab terms
181void internalize_cert_name(utf8 const & utf, cert_name & c);
182void internalize_cert_name(external const & ext, cert_name & c);
183void externalize_cert_name(cert_name const & c, utf8 & utf);
184void externalize_cert_name(cert_name const & c, external & ext);
185void internalize_rsa_keypair_id(utf8 const & utf, rsa_keypair_id & key);
186void internalize_rsa_keypair_id(external const & ext, rsa_keypair_id & key);
187void externalize_rsa_keypair_id(rsa_keypair_id const & key, utf8 & utf);
188void externalize_rsa_keypair_id(rsa_keypair_id const & key, external & ext);
189void internalize_var_domain(utf8 const & utf, var_domain & d);
190void internalize_var_domain(external const & ext, var_domain & d);
191void externalize_var_domain(var_domain const & d, utf8 & utf);
192void externalize_var_domain(var_domain const & d, external & ext);
193
194// line-ending conversion
195void line_end_convert(std::string const & linesep, std::string const & src, std::string & dst);
196
197
198
199#endif // __TRANSFORMS_HH__

Archive Download this file

Branches

Tags

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