#include "extensions.h" #include "md5.h" using namespace std; int fd_copy(fd_set *from, fd_set *to) { memcpy(to, from, sizeof(fd_set)); return 0; } unsigned int bitcount(void *start, size_t size) { size_t count = 0; const unsigned long *part=(const unsigned long*)start; for (size_t i=0; i bs(part[i]); count += bs.count(); } return (unsigned int)count; } bool iszero(void *start, size_t size) { const unsigned long *part = (const unsigned long*)start; for (size_t i = 0; i < size / sizeof(long); i++) if (part[i]) return false; return true; } const char *strndupCheck(const char *_value, size_t size) { //caller responsible for freeing char *temp = 0; if (_value) { temp = (char*)mallocCheck(size+1); if (!temp) { DEBUGERROR0("Out Of Memory (strdup)!"); throw bad_alloc(); } memcpy(temp, _value, size); temp[size]=0; } return temp; } const char *strdupCheck(const char *_value) { //caller responsible for freeing const char *temp = 0; if (_value) { temp = _STRDUP(_value); if (!temp) { DEBUGERROR0("Out Of Memory (strdup)!"); throw bad_alloc(); } } return temp; } int atoin(const char *str, const size_t len) { int i; char *valueCopy=(char*)mallocCheck(len+1); strncpy(valueCopy, str, len); valueCopy[len]=0; i=atoi(valueCopy); free(valueCopy); return i; } const int xtoi(const char* xs, unsigned int* result) { //converts a hexidecimal string to an unsigned integer x20 -> 32 //should be using strtol size_t szlen = strlen(xs); int xv, fact; char c; if (!szlen) return 1; //nothing to convert if (szlen>8) return 2; //too big // Begin conversion here *result = 0; fact = 1; // Run until no more character to convert for (int i=(int)szlen-1;i>=0;i--) { c=*(xs+i); if (c!=' '&&c!='\n'&&c!='\r') { if (!isxdigit(c)) return 4; //non-hex digit if (c>=97) xv = (c-97) + 10; else if (c>=65) xv = (c - 65) + 10; else xv = c - 48; *result += (xv * fact); fact *= 16; } } return 0; } const int itox(const unsigned char c, char* result) { //maximum char value 255 (*unsigned* char) static char hex[] = "0123456789ABCDEF"; *result++ = hex[c/16]; *result++ = hex[c & 15]; return 0; } void *mallocCheck(size_t amount) { void *memory = malloc(amount); if (!memory) { DEBUGERROR0("Out Of Memory!"); throw bad_alloc(); } return memory; } bool strlicmp(const char *s1, const char *s2) { //compare 2 strings returning true ignoring differences in length //case insensitive //e.g. "" == "test", "te" == "tEst" //A - a = 97-65 = 32 if (!s1 || !s2) return true; char c1, c2; while ((c1 = *s1++) && (c2 = *s2++) && (c1 == c2 || c1 == c2 + 32 || c1 == c2 - 32)) 000; return (!c1 || !c2); //got to the end of one of the strings } char *stristr(char *haystack, const char *needle) { return (char*)stristr((const char*)haystack, needle); } const char *stristr(const char *haystack, const char *needle) { const char *pos1; char *pos2; char c, ncL, ncU; char *needleLower; size_t len; //invalid input cases if (!needle || !*needle) return haystack; if (!haystack || !*haystack) return 0; //both strings are valid //make lower case version of the needle len = strlen(needle); needleLower = (char*)malloc(len+1); pos1 = needle; pos2 = needleLower; while (c = *pos1++) { if (c >= 'A' && c <= 'Z') *pos2 = c + ('a' - 'A'); else *pos2 = c; pos2++; } *pos2 = 0; //search pos1 = haystack; pos2 = needleLower; ncL = *pos2; if (ncL >= 'a' && ncL <= 'z') ncU = ncL - ('a' - 'A'); else ncU = ncL; //search while (ncL && (c = *pos1++)) { if (c == ncL || c == ncU) { //match success during test ncL = *++pos2; if (ncL >= 'a' && ncL <= 'z') ncU = ncL - ('a' - 'A'); else ncU = ncL; } else if (pos2 != needleLower) { //match fail during test, back to beginning pos2 = needleLower; ncL = *pos2; if (ncL >= 'a' && ncL <= 'z') ncU = ncL - ('a' - 'A'); else ncU = ncL; } } free(needleLower); if (ncL) return 0; //return 0 if not found at all return pos1 - len; } const char *md5(const char *subject, char out[33]) { //caller manages out //the development DB uses a different MD5 algorithum out[0] = 0; out[32] = 0; if (subject) { #ifdef LIVEDB //caller creates and handles result MD5_CTX context; unsigned char digest[16]; //the numerical value of the digest MD5Init(&context); MD5Update(&context, (const unsigned char*) subject, strlen(subject)); MD5Final(digest, &context); //make a string version of the numeric digest value for (int i = 0; i < 16; i++) sprintf(&out[i*2], "%02x", digest[i]); #else //dev DB uses a non-standard MD5 constructed from the subject and length _STRNCPY(out, subject, 9); _SNPRINTF(out + 9, 22, "%u", strlen(subject)); #endif } return out; }