Clean up the namespace.
[fides] / lib / utility.cc
1 /* fides.cc - Light-weight, decentralised trust and authorisation management
2    Copyright (C) 2008-2009  Guus Sliepen <guus@tinc-vpn.org>
3   
4    Fides is free software; you can redistribute it and/or modify
5    it under the terms of the GNU Lesser General Public License as
6    published by the Free Software Foundation; either version 2.1 of
7    the License, or (at your option) any later version.
8   
9    Fides is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12    GNU Lesser General Public License for more details.
13   
14    You should have received a copy of the GNU Lesser General Public
15    License along with this program; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <iostream>
19 #include <fstream>
20 #include <cstdlib>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <unistd.h>
24 #include <dirent.h>
25 #include <botan/types.h>
26 #include <botan/botan.h>
27 #include <botan/ecdsa.h>
28 #include <botan/look_pk.h>
29 #include <botan/lookup.h>
30 #include <botan/filters.h>
31 #include <botan/sha2_32.h>
32 #include <list>
33
34 #include "fides.h"
35
36 #ifndef FIDES_DEBUG
37 #define FIDES_DEBUG false
38 #endif
39
40 #define debug if(FIDES_DEBUG)
41
42 using namespace std;
43
44 namespace Fides {
45         // Base64 and hex encoding/decoding functions
46
47         /// Hexadecimal encode data.
48         //
49         /// @param in A string containing raw data.
50         /// @return A string containing the hexadecimal encoded data.
51         string hexencode(const std::string &in) {
52                 Botan::Pipe pipe(new Botan::Hex_Encoder);
53                 pipe.process_msg((Botan::byte *)in.data(), in.size());
54                 return pipe.read_all_as_string();
55         }
56
57         /// Decode hexadecimal data.
58         //
59         /// @param in A string containing hexadecimal encoded data.
60         /// @return A string containing the raw data.
61         string hexdecode(const std::string &in) {
62                 Botan::Pipe pipe(new Botan::Hex_Decoder);
63                 pipe.process_msg((Botan::byte *)in.data(), in.size());
64                 return pipe.read_all_as_string();
65         }
66
67         /// Base-64 encode data.
68         //
69         /// @param in A string containing raw data.
70         /// @return A string containing the base-64 encoded data.
71         string b64encode(const std::string &in) {
72                 Botan::Pipe pipe(new Botan::Base64_Encoder);
73                 pipe.process_msg((Botan::byte *)in.data(), in.size());
74                 return pipe.read_all_as_string();
75         }
76
77         /// Decode base-64 data.
78         //
79         /// @param in A string containing base-64 encoded data.
80         /// @return A string containing the raw data.
81         string b64decode(const std::string &in) {
82                 Botan::Pipe pipe(new Botan::Base64_Decoder);
83                 pipe.process_msg((Botan::byte *)in.data(), in.size());
84                 return pipe.read_all_as_string();
85         }
86
87         /// Return the names of all the files in a directory.
88         //
89         /// @param path Directory to search for files.
90         /// @return A vector of strings with the name of each file in the directory.
91         ///         The filename does not contain the leading path.
92         vector<string> dirlist(const std::string &path) {
93                 vector<string> files;
94
95                 DIR *dir = opendir(path.c_str());
96                 if(!dir)
97                         return files;
98
99                 struct dirent entry, *result = &entry;
100                 
101                 while(result) {
102                         readdir_r(dir, &entry, &result);
103                         if(!result)
104                                 break;
105                         struct stat st;
106                         if(result->d_type == DT_UNKNOWN) {
107                                 if(stat((path + "/" + result->d_name).c_str(), &st))
108                                         continue;
109                                 if(S_ISREG(st.st_mode))
110                                         files.push_back(result->d_name);
111                         } else if(result->d_type == DT_REG) {
112                                 files.push_back(result->d_name);
113                         }
114                 }
115
116                 closedir(dir);
117
118                 return files;
119         }
120 }