[9] | 1 | #include <string> |
---|
| 2 | #include <vector> |
---|
| 3 | #include <string.h> |
---|
| 4 | #include <iostream> |
---|
| 5 | #include <algorithm> |
---|
| 6 | #include <sys/time.h> |
---|
| 7 | #include "wutils.h" |
---|
| 8 | #include <sys/stat.h> |
---|
| 9 | #include <sys/types.h> |
---|
| 10 | #include <errno.h> |
---|
| 11 | #include <stdlib.h> |
---|
| 12 | #include "wtd.h" |
---|
| 13 | |
---|
| 14 | // |
---|
| 15 | // splits string s to vector v, using separator character sep |
---|
| 16 | // return value: v |
---|
| 17 | // It seems that it follows the python implementation of split() |
---|
| 18 | // |
---|
| 19 | std::vector <std::string> split(std::vector <std::string> &v, const std::string s, const char sep) |
---|
| 20 | { |
---|
| 21 | v.clear(); |
---|
| 22 | for (size_t p=0, q=0; q != s.npos; p = q + 1) |
---|
| 23 | v.push_back(s.substr(p,(q=s.find(sep,p))-p)); |
---|
| 24 | return v; |
---|
| 25 | } |
---|
| 26 | |
---|
| 27 | // converts string to char * |
---|
| 28 | // resulting string is created using malloc. Use free to free. |
---|
| 29 | char* str_to_charp(const std::string s) |
---|
| 30 | { |
---|
| 31 | return strdup(s.c_str()); |
---|
| 32 | } |
---|
| 33 | |
---|
| 34 | /* |
---|
| 35 | * converts string to hex representation |
---|
| 36 | */ |
---|
| 37 | std::string strtohex(const std::string s) |
---|
| 38 | { |
---|
| 39 | std::string a; |
---|
| 40 | const std::string c= |
---|
| 41 | "000102030405060708090a0b0c0d0e0f" |
---|
| 42 | "101112131415161718191a1b1c1d1e1f" |
---|
| 43 | "202122232425262728292a2b2c2d2e2f" |
---|
| 44 | "303132333435363738393a3b3c3d3e3f" |
---|
| 45 | "404142434445464748494a4b4c4d4e4f" |
---|
| 46 | "505152535455565758595a5b5c5d5e5f" |
---|
| 47 | "606162636465666768696a6b6c6d6e6f" |
---|
| 48 | "707172737475767778797a7b7c7d7e7f" |
---|
| 49 | "808182838485868788898a8b8c8d8e8f" |
---|
| 50 | "909192939495969798999a9b9c9d9e9f" |
---|
| 51 | "a0a1a2a3a4a5a6a7a8a9aaabacadaeaf" |
---|
| 52 | "b0b1b2b3b4b5b6b7b8b9babbbcbdbebf" |
---|
| 53 | "c0c1c2c3c4c5c6c7c8c9cacbcccdcecf" |
---|
| 54 | "d0d1d2d3d4d5d6d7d8d9dadbdcdddedf" |
---|
| 55 | "e0e1e2e3e4e5e6e7e8e9eaebecedeeef" |
---|
| 56 | "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"; |
---|
| 57 | |
---|
| 58 | a.resize(2*s.size()); |
---|
| 59 | int k = 0; |
---|
| 60 | for (unsigned int i=0; i<s.size(); i++) |
---|
| 61 | { |
---|
| 62 | int p = 2*(unsigned char)s[i]; |
---|
| 63 | a[k++] = c[p++]; |
---|
| 64 | a[k++] = c[p]; |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | return a; |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | /* |
---|
| 71 | * convert hex represented string to string |
---|
| 72 | * unknown hex numbers result in '?' |
---|
| 73 | */ |
---|
| 74 | std::string hextostr(const std:: string s) |
---|
| 75 | { |
---|
| 76 | const char c[] = { |
---|
| 77 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
---|
| 78 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
---|
| 79 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
---|
| 80 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1, |
---|
| 81 | -1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
---|
| 82 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
---|
| 83 | -1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
---|
| 84 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
---|
| 85 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
---|
| 86 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
---|
| 87 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
---|
| 88 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
---|
| 89 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
---|
| 90 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
---|
| 91 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
---|
| 92 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 |
---|
| 93 | }; |
---|
| 94 | |
---|
| 95 | std::string a; |
---|
| 96 | unsigned int l = s.size(); |
---|
| 97 | a.resize((l+1)/2); |
---|
| 98 | |
---|
| 99 | unsigned int k=0; |
---|
| 100 | unsigned int l2 = (l/2)*2; |
---|
| 101 | for (unsigned int i=0; i<l2; i+=2) |
---|
| 102 | { |
---|
| 103 | int p = c[(unsigned int)s[i]]; |
---|
| 104 | int q = c[(unsigned int)s[i+1]]; |
---|
| 105 | if (p >= 0 && q >= 0) |
---|
| 106 | a[k++] = 16*p + q; |
---|
| 107 | else |
---|
| 108 | a[k++] = '?'; |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | if (l2 < l) |
---|
| 112 | a[k] = '?'; |
---|
| 113 | |
---|
| 114 | return a; |
---|
| 115 | |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | std::string zstrtohex(const std::string s) |
---|
| 119 | { |
---|
| 120 | return ZBEGIN_+strtohex(s)+ZEND_; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | std::string zhextostr(const std::string s) |
---|
| 124 | { |
---|
| 125 | size_t lb = ZBEGIN_.size(); |
---|
| 126 | size_t le = ZEND_.size(); |
---|
| 127 | size_t ls = s.size(); |
---|
| 128 | |
---|
| 129 | if (ls < lb + le) |
---|
| 130 | return s; |
---|
| 131 | |
---|
| 132 | if (s.substr(0,lb) != ZBEGIN_) |
---|
| 133 | return s; |
---|
| 134 | |
---|
| 135 | if (s.substr(ls-le) != ZEND_) |
---|
| 136 | return s; |
---|
| 137 | |
---|
| 138 | return hextostr(s.substr(lb,ls-lb-le)); |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | //generates random passwoord of length len |
---|
| 142 | std::string gen_random(const int len) { |
---|
| 143 | timeval t1; |
---|
| 144 | gettimeofday(&t1, NULL); |
---|
| 145 | srand(t1.tv_usec * t1.tv_sec); |
---|
| 146 | static const char alphanum[] = |
---|
| 147 | "0123456789" |
---|
| 148 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
---|
| 149 | "abcdefghijklmnopqrstuvwxyz"; |
---|
| 150 | |
---|
| 151 | std::string s; |
---|
| 152 | s.resize(len); |
---|
| 153 | for (int i = 0; i < len; ++i) |
---|
| 154 | { |
---|
| 155 | s[i] = alphanum[std::rand() % (sizeof(alphanum) - 1)]; |
---|
| 156 | } |
---|
| 157 | return s; |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | #include <fstream> |
---|
| 161 | #include <string> |
---|
| 162 | #include <cerrno> |
---|
| 163 | |
---|
| 164 | int get_file_contents(std::string &contents,const std::string &filename) |
---|
| 165 | { |
---|
| 166 | std::ifstream in(filename.c_str(), std::ios::in | std::ios::binary); |
---|
| 167 | if (in) |
---|
| 168 | { |
---|
| 169 | in.seekg(0, std::ios::end); |
---|
| 170 | contents.resize(in.tellg()); |
---|
| 171 | in.seekg(0, std::ios::beg); |
---|
| 172 | in.read(&contents[0], contents.size()); |
---|
| 173 | in.close(); |
---|
| 174 | return(0); |
---|
| 175 | } |
---|
| 176 | return -1; |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | // tries to mimic mkdir -p |
---|
| 180 | int mkdir_p(const std::string &d,mode_t mode) |
---|
| 181 | { |
---|
| 182 | size_t p=0; |
---|
| 183 | |
---|
| 184 | std::string s = d; |
---|
| 185 | if (s[s.size()-1] != '/') |
---|
| 186 | s += '/'; |
---|
| 187 | |
---|
| 188 | int rc; |
---|
| 189 | std::string t; |
---|
| 190 | while (( p = s.find_first_of('/',p)) != s.npos) |
---|
| 191 | { |
---|
| 192 | p++; |
---|
| 193 | t = s.substr(0,p); |
---|
| 194 | rc = mkdir (t.c_str(),mode); |
---|
| 195 | if (rc < 0) |
---|
| 196 | { |
---|
| 197 | if (errno == EEXIST) |
---|
| 198 | { |
---|
| 199 | struct stat buf; |
---|
| 200 | rc = lstat(t.c_str(),&buf); |
---|
| 201 | if (rc < 0) |
---|
| 202 | break; |
---|
| 203 | if (!S_ISDIR(buf.st_mode)) |
---|
| 204 | { |
---|
| 205 | rc = -1; |
---|
| 206 | break; |
---|
| 207 | } |
---|
| 208 | } |
---|
| 209 | else |
---|
| 210 | break; |
---|
| 211 | } |
---|
| 212 | if ( p == s.size()) |
---|
| 213 | break; |
---|
| 214 | } |
---|
| 215 | return rc; |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | std::string envtostr(const std::string &s) |
---|
| 219 | { |
---|
| 220 | char *a = getenv(s.c_str()); |
---|
| 221 | if (a == 0) |
---|
| 222 | return ""; |
---|
| 223 | |
---|
| 224 | return a; |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | int get_dir_list(std::list <std::string> &v, |
---|
| 228 | const std::string &d) |
---|
| 229 | { |
---|
| 230 | DIR *dir; |
---|
| 231 | struct dirent *ent; |
---|
| 232 | dir = opendir(d.c_str()); |
---|
| 233 | if (dir == 0) |
---|
| 234 | return 1; |
---|
| 235 | |
---|
| 236 | while ((ent = readdir (dir)) != 0) |
---|
| 237 | v.push_back(ent->d_name); |
---|
| 238 | closedir (dir); |
---|
| 239 | return 0; |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | #include <string> |
---|
| 243 | #include <stdexcept> |
---|
| 244 | #include <iostream> |
---|
| 245 | #include <iomanip> |
---|
| 246 | #include <sstream> |
---|
| 247 | |
---|
| 248 | #include <string.h> |
---|
| 249 | |
---|
| 250 | #include <zlib.h> |
---|
| 251 | |
---|
| 252 | /** Compress a STL string using zlib with given compression level and return |
---|
| 253 | * the binary data. */ |
---|
| 254 | // http://panthema.net/2007/0328-ZLibString.html |
---|
| 255 | std::string compress_string(const std::string& str, |
---|
| 256 | int compressionlevel) |
---|
| 257 | { |
---|
| 258 | z_stream zs; // z_stream is zlib's control structure |
---|
| 259 | memset(&zs, 0, sizeof(zs)); |
---|
| 260 | |
---|
| 261 | if (deflateInit(&zs, compressionlevel) != Z_OK) |
---|
| 262 | throw(std::runtime_error("deflateInit failed while compressing.")); |
---|
| 263 | |
---|
| 264 | zs.next_in = (Bytef*)str.data(); |
---|
| 265 | zs.avail_in = str.size(); // set the z_stream's input |
---|
| 266 | |
---|
| 267 | int ret; |
---|
| 268 | char outbuffer[32768]; |
---|
| 269 | std::string outstring; |
---|
| 270 | |
---|
| 271 | // retrieve the compressed bytes blockwise |
---|
| 272 | do { |
---|
| 273 | zs.next_out = reinterpret_cast<Bytef*>(outbuffer); |
---|
| 274 | zs.avail_out = sizeof(outbuffer); |
---|
| 275 | |
---|
| 276 | ret = deflate(&zs, Z_FINISH); |
---|
| 277 | |
---|
| 278 | if (outstring.size() < zs.total_out) { |
---|
| 279 | // append the block to the output string |
---|
| 280 | outstring.append(outbuffer, |
---|
| 281 | zs.total_out - outstring.size()); |
---|
| 282 | } |
---|
| 283 | } while (ret == Z_OK); |
---|
| 284 | |
---|
| 285 | deflateEnd(&zs); |
---|
| 286 | |
---|
| 287 | if (ret != Z_STREAM_END) { // an error occurred that was not EOF |
---|
| 288 | std::ostringstream oss; |
---|
| 289 | oss << "Exception during zlib compression: (" << ret << ") " << zs.msg; |
---|
| 290 | throw(std::runtime_error(oss.str())); |
---|
| 291 | } |
---|
| 292 | |
---|
| 293 | return outstring; |
---|
| 294 | } |
---|
| 295 | |
---|
| 296 | /** Decompress an STL string using zlib and return the original data. */ |
---|
| 297 | std::string decompress_string(const std::string& str) |
---|
| 298 | { |
---|
| 299 | z_stream zs; // z_stream is zlib's control structure |
---|
| 300 | memset(&zs, 0, sizeof(zs)); |
---|
| 301 | |
---|
| 302 | if (inflateInit(&zs) != Z_OK) |
---|
| 303 | throw(std::runtime_error("inflateInit failed while decompressing.")); |
---|
| 304 | |
---|
| 305 | zs.next_in = (Bytef*)str.data(); |
---|
| 306 | zs.avail_in = str.size(); |
---|
| 307 | |
---|
| 308 | int ret; |
---|
| 309 | char outbuffer[32768]; |
---|
| 310 | std::string outstring; |
---|
| 311 | |
---|
| 312 | // get the decompressed bytes blockwise using repeated calls to inflate |
---|
| 313 | do { |
---|
| 314 | zs.next_out = reinterpret_cast<Bytef*>(outbuffer); |
---|
| 315 | zs.avail_out = sizeof(outbuffer); |
---|
| 316 | |
---|
| 317 | ret = inflate(&zs, 0); |
---|
| 318 | |
---|
| 319 | if (outstring.size() < zs.total_out) { |
---|
| 320 | outstring.append(outbuffer, |
---|
| 321 | zs.total_out - outstring.size()); |
---|
| 322 | } |
---|
| 323 | |
---|
| 324 | } while (ret == Z_OK); |
---|
| 325 | |
---|
| 326 | inflateEnd(&zs); |
---|
| 327 | |
---|
| 328 | if (ret != Z_STREAM_END) { // an error occurred that was not EOF |
---|
| 329 | std::ostringstream oss; |
---|
| 330 | oss << "Exception during zlib decompression: (" << ret << ") " |
---|
| 331 | << zs.msg; |
---|
| 332 | throw(std::runtime_error(oss.str())); |
---|
| 333 | } |
---|
| 334 | |
---|
| 335 | return outstring; |
---|
| 336 | } |
---|
| 337 | |
---|