]> WPIA git - cassiopeia.git/blob - src/util.cpp
ADD: gcov make launch
[cassiopeia.git] / src / util.cpp
1 #include "util.h"
2
3 #include <sys/stat.h>
4
5 #include <fstream>
6 #include <iostream>
7 #include <sstream>
8 #include <time.h>
9 #include <stdexcept>
10
11 void writeFile( const std::string& name, const std::string& content ) {
12     std::ofstream file;
13
14     file.open( name );
15     file << content;
16     file.close();
17 }
18
19 std::string readFile( const std::string& name ) {
20     std::ifstream t( name );
21     std::string res = std::string( std::istreambuf_iterator<char>( t ), std::istreambuf_iterator<char>() );
22     t.close();
23
24     return res;
25 }
26
27 std::string writeBackFile( const std::string& serial, const std::string& cert, const std::string& keydir ) {
28     std::string filename = keydir;
29     mkdir( filename.c_str(), 0755 );
30     filename += "/crt";
31     mkdir( filename.c_str(), 0755 );
32     std::string first;
33
34     if( serial.length() < 3 ) {
35         first = "0";
36     } else {
37         first = serial.substr( 0, serial.length() - 3 );
38     }
39
40     filename += "/" + first;
41     mkdir( filename.c_str(), 0755 );
42     filename += "/" + serial + ".crt";
43     writeFile( filename, cert );
44
45     return filename;
46 }
47 bool isDigit( char c ) {
48     return ( c >= '0' ) && ( c <= '9' );
49 }
50
51 std::pair<bool, time_t> parseDate( const std::string& date ) {
52     if( date.size() != 10 || date[4] != '-' || date[7] != '-' ) {
53         return std::pair<bool, time_t>( false, 0 );
54     }
55
56     if( !isDigit( date[0] )
57             || !isDigit( date[1] )
58             || !isDigit( date[2] )
59             || !isDigit( date[3] )
60             || !isDigit( date[5] )
61             || !isDigit( date[6] )
62             || !isDigit( date[8] )
63             || !isDigit( date[9] ) ) {
64         return std::pair<bool, time_t>( false, 0 );
65     }
66
67     std::tm t;
68     t.tm_sec = 0;
69     t.tm_min = 0;
70     t.tm_hour = 0;
71     t.tm_year = std::stoi( date.substr( 0, 4 ) ) - 1900;
72     t.tm_mon = std::stoi( date.substr( 5, 2 ) ) - 1;
73     t.tm_mday = std::stoi( date.substr( 8, 2 ) );
74     setenv( "TZ", "UTC", 1 );
75     tzset();
76     std::time_t res = mktime( &t );
77     char check[11];
78     std::size_t siz = strftime( check, 11, "%Y-%m-%d", &t );
79
80     if( siz != 10 ) {
81         return std::pair<bool, time_t>( false, 0 ); // NO-COVERAGE (by contract of strftime)
82     }
83
84     std::string checkS( check, siz );
85
86     if( checkS != date ) {
87         return std::pair<bool, time_t>( false, 0 );
88     }
89
90     return std::pair<bool, time_t>( true, res );
91 }
92
93 std::pair<bool, time_t> addMonths( std::time_t t, int32_t count ) {
94     std::tm* parsed = gmtime( &t );
95
96     if( !parsed || count <= 0 || count > 24 ) { // FIXED MAX-Validity-Length
97         return std::pair<bool, time_t>( false, 0 );
98     }
99
100     parsed->tm_mon += count;
101     int oldday = parsed->tm_mday;
102     setenv( "TZ", "UTC", 1 );
103     tzset();
104     std::time_t res = mktime( parsed );
105
106     if( parsed->tm_mday != oldday ) {
107         parsed->tm_mday = 0;
108         res = mktime( parsed );
109     }
110
111     return std::pair<bool, time_t>( true, res );
112
113 }
114
115 std::pair<bool, time_t> parseMonthInterval( std::time_t t, const std::string& date ) {
116     if( date[date.size() - 1] != 'm' ) {
117         return  std::pair<bool, time_t>( false, 0 );
118     }
119
120     try {
121         size_t end = 0;
122         int num = std::stoi( date.substr( 0, date.size() - 1 ) , &end );
123
124         if( end != date.size() - 1 ) {
125             return  std::pair<bool, time_t>( false, 0 );
126         }
127
128         return addMonths( t, num );
129     } catch( const std::invalid_argument& a ) {
130         return std::pair<bool, time_t>( false, 0 );
131     } catch( const std::out_of_range& a ) {
132         return std::pair<bool, time_t>( false, 0 );
133     }
134 }
135 std::pair<bool, time_t> parseYearInterval( std::time_t t, const std::string& date ) {
136     if( date[date.size() - 1] != 'y' ) {
137         return  std::pair<bool, time_t>( false, 0 );
138     }
139
140     try {
141         size_t end = 0;
142         int num = std::stoi( date.substr( 0, date.size() - 1 ), &end );
143
144         if( end != date.size() - 1 ) {
145             return  std::pair<bool, time_t>( false, 0 );
146         }
147
148         return addMonths( t, num * 12 );
149     } catch( std::invalid_argument& a ) {
150         return std::pair<bool, time_t>( false, 0 );
151     } catch( std::out_of_range& a ) {
152         return std::pair<bool, time_t>( false, 0 );
153     }
154 }