#include #include #include #include #include #include #include "wutils.h" #include #include #include #include #include "wtd.h" // // splits string s to vector v, using separator character sep // return value: v // It seems that it follows the python implementation of split() // std::vector split(std::vector &v, const std::string s, const char sep) { v.clear(); for (size_t p=0, q=0; q != s.npos; p = q + 1) v.push_back(s.substr(p,(q=s.find(sep,p))-p)); return v; } // converts string to char * // resulting string is created using malloc. Use free to free. char* str_to_charp(const std::string s) { return strdup(s.c_str()); } /* * converts string to hex representation */ std::string strtohex(const std::string s) { std::string a; const std::string c= "000102030405060708090a0b0c0d0e0f" "101112131415161718191a1b1c1d1e1f" "202122232425262728292a2b2c2d2e2f" "303132333435363738393a3b3c3d3e3f" "404142434445464748494a4b4c4d4e4f" "505152535455565758595a5b5c5d5e5f" "606162636465666768696a6b6c6d6e6f" "707172737475767778797a7b7c7d7e7f" "808182838485868788898a8b8c8d8e8f" "909192939495969798999a9b9c9d9e9f" "a0a1a2a3a4a5a6a7a8a9aaabacadaeaf" "b0b1b2b3b4b5b6b7b8b9babbbcbdbebf" "c0c1c2c3c4c5c6c7c8c9cacbcccdcecf" "d0d1d2d3d4d5d6d7d8d9dadbdcdddedf" "e0e1e2e3e4e5e6e7e8e9eaebecedeeef" "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"; a.resize(2*s.size()); int k = 0; for (unsigned int i=0; i= 0 && q >= 0) a[k++] = 16*p + q; else a[k++] = '?'; } if (l2 < l) a[k] = '?'; return a; } std::string zstrtohex(const std::string s) { return ZBEGIN_+strtohex(s)+ZEND_; } std::string zhextostr(const std::string s) { size_t lb = ZBEGIN_.size(); size_t le = ZEND_.size(); size_t ls = s.size(); if (ls < lb + le) return s; if (s.substr(0,lb) != ZBEGIN_) return s; if (s.substr(ls-le) != ZEND_) return s; return hextostr(s.substr(lb,ls-lb-le)); } //generates random passwoord of length len std::string gen_random(const int len) { timeval t1; gettimeofday(&t1, NULL); srand(t1.tv_usec * t1.tv_sec); static const char alphanum[] = "0123456789" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz"; std::string s; s.resize(len); for (int i = 0; i < len; ++i) { s[i] = alphanum[std::rand() % (sizeof(alphanum) - 1)]; } return s; } #include #include #include int get_file_contents(std::string &contents,const std::string &filename) { std::ifstream in(filename.c_str(), std::ios::in | std::ios::binary); if (in) { in.seekg(0, std::ios::end); contents.resize(in.tellg()); in.seekg(0, std::ios::beg); in.read(&contents[0], contents.size()); in.close(); return(0); } return -1; } // tries to mimic mkdir -p int mkdir_p(const std::string &d,mode_t mode) { size_t p=0; std::string s = d; if (s[s.size()-1] != '/') s += '/'; int rc; std::string t; while (( p = s.find_first_of('/',p)) != s.npos) { p++; t = s.substr(0,p); rc = mkdir (t.c_str(),mode); if (rc < 0) { if (errno == EEXIST) { struct stat buf; rc = lstat(t.c_str(),&buf); if (rc < 0) break; if (!S_ISDIR(buf.st_mode)) { rc = -1; break; } } else break; } if ( p == s.size()) break; } return rc; } std::string envtostr(const std::string &s) { char *a = getenv(s.c_str()); if (a == 0) return ""; return a; } int get_dir_list(std::list &v, const std::string &d) { DIR *dir; struct dirent *ent; dir = opendir(d.c_str()); if (dir == 0) return 1; while ((ent = readdir (dir)) != 0) v.push_back(ent->d_name); closedir (dir); return 0; } #include #include #include #include #include #include #include /** Compress a STL string using zlib with given compression level and return * the binary data. */ // http://panthema.net/2007/0328-ZLibString.html std::string compress_string(const std::string& str, int compressionlevel) { z_stream zs; // z_stream is zlib's control structure memset(&zs, 0, sizeof(zs)); if (deflateInit(&zs, compressionlevel) != Z_OK) throw(std::runtime_error("deflateInit failed while compressing.")); zs.next_in = (Bytef*)str.data(); zs.avail_in = str.size(); // set the z_stream's input int ret; char outbuffer[32768]; std::string outstring; // retrieve the compressed bytes blockwise do { zs.next_out = reinterpret_cast(outbuffer); zs.avail_out = sizeof(outbuffer); ret = deflate(&zs, Z_FINISH); if (outstring.size() < zs.total_out) { // append the block to the output string outstring.append(outbuffer, zs.total_out - outstring.size()); } } while (ret == Z_OK); deflateEnd(&zs); if (ret != Z_STREAM_END) { // an error occurred that was not EOF std::ostringstream oss; oss << "Exception during zlib compression: (" << ret << ") " << zs.msg; throw(std::runtime_error(oss.str())); } return outstring; } /** Decompress an STL string using zlib and return the original data. */ std::string decompress_string(const std::string& str) { z_stream zs; // z_stream is zlib's control structure memset(&zs, 0, sizeof(zs)); if (inflateInit(&zs) != Z_OK) throw(std::runtime_error("inflateInit failed while decompressing.")); zs.next_in = (Bytef*)str.data(); zs.avail_in = str.size(); int ret; char outbuffer[32768]; std::string outstring; // get the decompressed bytes blockwise using repeated calls to inflate do { zs.next_out = reinterpret_cast(outbuffer); zs.avail_out = sizeof(outbuffer); ret = inflate(&zs, 0); if (outstring.size() < zs.total_out) { outstring.append(outbuffer, zs.total_out - outstring.size()); } } while (ret == Z_OK); inflateEnd(&zs); if (ret != Z_STREAM_END) { // an error occurred that was not EOF std::ostringstream oss; oss << "Exception during zlib decompression: (" << ret << ") " << zs.msg; throw(std::runtime_error(oss.str())); } return outstring; }