diff options
Diffstat (limited to 'common')
-rw-r--r-- | common/buffer.c | 926 | ||||
-rw-r--r-- | common/compat.c | 28 | ||||
-rw-r--r-- | common/compat.h | 14 | ||||
-rw-r--r-- | common/hash.c | 544 | ||||
-rw-r--r-- | common/md5.c | 296 | ||||
-rw-r--r-- | common/md5.h | 6 | ||||
-rw-r--r-- | common/sha1.c | 190 |
7 files changed, 1002 insertions, 1002 deletions
diff --git a/common/buffer.c b/common/buffer.c index 5a77922..07fa059 100644 --- a/common/buffer.c +++ b/common/buffer.c @@ -31,399 +31,399 @@ typedef struct ha_buffer_internal { - char* end; - struct ha_buffer_internal* next; + char* end; + struct ha_buffer_internal* next; } internal_t; void buffer_bump(ha_buffer_t* buf, int count) { - int allocated; - internal_t* intl; - - ASSERT(buf && count); - - if(ha_buferr(buf)) - return; - - ASSERT(BUF_DELTA < BUF_INITIAL); - - /* Handle this common case first for efficiency */ - if(buf->_rp + (count + BUF_DELTA) < buf->_dt->end) - return; - - /* Now when in joins we let it go closer */ - if(BUF_IN_JOIN(buf) && (buf->_rp + count < buf->_dt->end)) - return; - - /* Okay now we know we have to extend */ - allocated = buf->_dt->end - (char*)(buf->_dt); - ASSERT(allocated > 0 && (allocated % BUF_INITIAL) == 0); - - /* Enlarge the size as much as needed */ - do - { - allocated *= 2; - } - while(allocated < count); - - ASSERT(sizeof(internal_t) < allocated); - intl = (internal_t*)malloc(allocated); - if(!intl) - { - ha_messagex(LOG_CRIT, "out of memory"); - buf->_dt = NULL; - buf->_pp = buf->_rp = NULL; - return; - } - else - { - void* beg = INTERNAL_DATA(intl); - intl->end = ((char*)(intl) + allocated); - intl->next = NULL; + int allocated; + internal_t* intl; + + ASSERT(buf && count); + + if(ha_buferr(buf)) + return; + + ASSERT(BUF_DELTA < BUF_INITIAL); + + /* Handle this common case first for efficiency */ + if(buf->_rp + (count + BUF_DELTA) < buf->_dt->end) + return; + + /* Now when in joins we let it go closer */ + if(BUF_IN_JOIN(buf) && (buf->_rp + count < buf->_dt->end)) + return; + + /* Okay now we know we have to extend */ + allocated = buf->_dt->end - (char*)(buf->_dt); + ASSERT(allocated > 0 && (allocated % BUF_INITIAL) == 0); + + /* Enlarge the size as much as needed */ + do + { + allocated *= 2; + } + while(allocated < count); + + ASSERT(sizeof(internal_t) < allocated); + intl = (internal_t*)malloc(allocated); + if(!intl) + { + ha_messagex(NULL, LOG_CRIT, "out of memory"); + buf->_dt = NULL; + buf->_pp = buf->_rp = NULL; + return; + } + else + { + void* beg = INTERNAL_DATA(intl); + intl->end = ((char*)(intl) + allocated); + intl->next = NULL; #ifdef _DEBUG - FILL_BETWEEN(beg, intl->end, 0xCD); + FILL_BETWEEN(beg, intl->end, 0xCD); #endif - if(BUF_IN_JOIN(buf)) - { - int diff = buf->_rp - buf->_pp; + if(BUF_IN_JOIN(buf)) + { + int diff = buf->_rp - buf->_pp; - /* Always true in a join */ - ASSERT(buf->_pp < buf->_rp); + /* Always true in a join */ + ASSERT(buf->_pp < buf->_rp); - /* Copy the memory and blank out old */ - memcpy(beg, buf->_pp, diff); + /* Copy the memory and blank out old */ + memcpy(beg, buf->_pp, diff); #ifdef _DEBUG - FILL_BETWEEN(buf->_pp, buf->_rp, 0xDD); + FILL_BETWEEN(buf->_pp, buf->_rp, 0xDD); #endif - buf->_pp = beg; - buf->_rp = buf->_pp + diff; - } - else - { - buf->_rp = buf->_pp = INTERNAL_DATA(intl); - } + buf->_pp = beg; + buf->_rp = buf->_pp + diff; + } + else + { + buf->_rp = buf->_pp = INTERNAL_DATA(intl); + } - buf->_dt->next = intl; - buf->_dt = intl; - } + buf->_dt->next = intl; + buf->_dt = intl; + } } void ha_bufinit(ha_buffer_t* buf) { - ASSERT(buf); - memset(buf, 0, sizeof(*buf)); + ASSERT(buf); + memset(buf, 0, sizeof(*buf)); - ASSERT(BUF_INITIAL > sizeof(internal_t)); - buf->_ft = (internal_t*)malloc(BUF_INITIAL); - if(buf->_ft) - { - buf->_ft->end = ((char*)(buf->_ft) + BUF_INITIAL); - buf->_ft->next = NULL; + ASSERT(BUF_INITIAL > sizeof(internal_t)); + buf->_ft = (internal_t*)malloc(BUF_INITIAL); + if(buf->_ft) + { + buf->_ft->end = ((char*)(buf->_ft) + BUF_INITIAL); + buf->_ft->next = NULL; #ifdef _DEBUG - FILL_BETWEEN(INTERNAL_DATA(buf->_ft), buf->_ft->end, 0xCD); + FILL_BETWEEN(INTERNAL_DATA(buf->_ft), buf->_ft->end, 0xCD); #endif - ha_bufreset(buf); - } + ha_bufreset(buf); + } } void ha_bufreset(ha_buffer_t* buf) { #ifdef _DEBUG - internal_t* intl; + internal_t* intl; #endif - ASSERT(buf); - ASSERT(buf->_ft); + ASSERT(buf); + ASSERT(buf->_ft); #ifdef _DEBUG - /* Go through all the buffers and set them to 0xCD */ - for(intl = buf->_ft; intl; intl = intl->next) - FILL_BETWEEN(INTERNAL_DATA(intl), intl->end, 0xCD); + /* Go through all the buffers and set them to 0xCD */ + for(intl = buf->_ft; intl; intl = intl->next) + FILL_BETWEEN(INTERNAL_DATA(intl), intl->end, 0xCD); #endif - buf->_dt = buf->_ft; - buf->_rp = buf->_pp = (char*)INTERNAL_DATA(buf->_ft); + buf->_dt = buf->_ft; + buf->_rp = buf->_pp = (char*)INTERNAL_DATA(buf->_ft); } void ha_buffree(ha_buffer_t* buf) { - internal_t* intl; - internal_t* next; + internal_t* intl; + internal_t* next; - ASSERT(buf); + ASSERT(buf); - /* Go through free all the buffers and set them to 0xDD */ - for(intl = buf->_ft; intl; intl = next) - { - next = intl->next; - FILL_BETWEEN(INTERNAL_DATA(intl), intl->end, 0xDD); - free(intl); - } + /* Go through free all the buffers and set them to 0xDD */ + for(intl = buf->_ft; intl; intl = next) + { + next = intl->next; + FILL_BETWEEN(INTERNAL_DATA(intl), intl->end, 0xDD); + free(intl); + } - buf->_ft = buf->_dt = NULL; - buf->_rp = buf->_pp = NULL; + buf->_ft = buf->_dt = NULL; + buf->_rp = buf->_pp = NULL; } int ha_bufreadline(int fd, ha_buffer_t* buf) { - int l; - - ASSERT(buf); - ASSERT(fd != -1); + int l; - if(ha_buferr(buf)) - return 0; + ASSERT(buf); + ASSERT(fd != -1); - for(;;) - { - buffer_bump(buf, 1); - l = read(fd, (void*)buf->_rp, sizeof(char)); - - /* We got a character */ - if(l == 1) - { - /* Skip junky CRLFs */ - if(*(buf->_rp) == '\r') - { - *(buf->_rp) = ' '; - continue; - } - - /* End of line */ - else if(*(buf->_rp) == '\n') - { - buf->_rp++; - break; - } - - /* All other characters */ - else - { - buf->_rp++; - } - } - - /* If it's the end of file then return that */ - else if(l == 0) - return 0; - - /* Transient errors */ - else if(l == -1 && errno == EAGAIN) - continue; + if(ha_buferr(buf)) + return 0; - /* Fatal errors */ - else if(l == -1) + for(;;) { - if(errno != EINTR) - ha_message(LOG_ERR, "couldn't read data"); - - return 0; + buffer_bump(buf, 1); + l = read(fd, (void*)buf->_rp, sizeof(char)); + + /* We got a character */ + if(l == 1) + { + /* Skip junky CRLFs */ + if(*(buf->_rp) == '\r') + { + *(buf->_rp) = ' '; + continue; + } + + /* End of line */ + else if(*(buf->_rp) == '\n') + { + buf->_rp++; + break; + } + + /* All other characters */ + else + { + buf->_rp++; + } + } + + /* If it's the end of file then return that */ + else if(l == 0) + return 0; + + /* Transient errors */ + else if(l == -1 && errno == EAGAIN) + continue; + + /* Fatal errors */ + else if(l == -1) + { + if(errno != EINTR) + ha_message(NULL, LOG_ERR, "couldn't read data"); + + return 0; + } } - } - return 1; + return 1; } char* ha_bufparseword(ha_buffer_t* buf, const char* delims) { - char* word = NULL; + char* word = NULL; - ASSERT(buf && delims); + ASSERT(buf && delims); - if(ha_buferr(buf)) - return NULL; - - /* Knock out any previous delims */ - while(buf->_pp < buf->_rp && strchr(delims, *(buf->_pp))) - buf->_pp++; + if(ha_buferr(buf)) + return NULL; - /* If at end of buffer or end of line return null */ - if(buf->_pp == buf->_rp || *(buf->_pp) == '\n') - return NULL; + /* Knock out any previous delims */ + while(buf->_pp < buf->_rp && strchr(delims, *(buf->_pp))) + buf->_pp++; - /* We do this before we stash away a pointer */ - buffer_bump(buf, 1); + /* If at end of buffer or end of line return null */ + if(buf->_pp == buf->_rp || *(buf->_pp) == '\n') + return NULL; - word = buf->_pp; + /* We do this before we stash away a pointer */ + buffer_bump(buf, 1); - while(!strchr(delims, *(buf->_pp))) - { - buf->_pp++; + word = buf->_pp; - /* At the end of the buffer */ - if(buf->_pp == buf->_rp) + while(!strchr(delims, *(buf->_pp))) { - *(buf->_rp) = 0; - buf->_rp++; - break; + buf->_pp++; + + /* At the end of the buffer */ + if(buf->_pp == buf->_rp) + { + *(buf->_rp) = 0; + buf->_rp++; + break; + } + + /* At the end of a line */ + else if(*(buf->_pp) == '\n') + break; } - /* At the end of a line */ - else if(*(buf->_pp) == '\n') - break; - } - - /* Now null terminate what we found */ - *(buf->_pp) = 0; - buf->_pp++; + /* Now null terminate what we found */ + *(buf->_pp) = 0; + buf->_pp++; - /* We don't return empty strings */ - if(word[0] == 0) - return NULL; + /* We don't return empty strings */ + if(word[0] == 0) + return NULL; - return word; + return word; } char* ha_bufparseline(ha_buffer_t* buf, int trim) { - char* t; - char* line = NULL; + char* t; + char* line = NULL; - ASSERT(buf); + ASSERT(buf); - if(ha_buferr(buf)) - return NULL; + if(ha_buferr(buf)) + return NULL; - if(trim) - { - /* Knock out any previous whitespace */ - while(buf->_pp < buf->_rp && isblank(*(buf->_pp))) - buf->_pp++; - } + if(trim) + { + /* Knock out any previous whitespace */ + while(buf->_pp < buf->_rp && isblank(*(buf->_pp))) + buf->_pp++; + } - if(buf->_pp == buf->_rp) - return NULL; + if(buf->_pp == buf->_rp) + return NULL; - /* We do this before we stash away a pointer */ - buffer_bump(buf, 1); + /* We do this before we stash away a pointer */ + buffer_bump(buf, 1); - line = buf->_pp; + line = buf->_pp; - t = (char*)memchr(buf->_pp, '\n', ha_buflen(buf)); - if(t == NULL) - { - t = (buf->_rp); - buf->_rp++; - } + t = (char*)memchr(buf->_pp, '\n', ha_buflen(buf)); + if(t == NULL) + { + t = (buf->_rp); + buf->_rp++; + } - *t = 0; - buf->_pp = t + 1; + *t = 0; + buf->_pp = t + 1; - if(trim) - line = trim_space(line); + if(trim) + line = trim_space(line); - /* We don't return empty strings */ - if(line[0] == 0) - return NULL; + /* We don't return empty strings */ + if(line[0] == 0) + return NULL; - return line; + return line; } char* ha_bufmcat(ha_buffer_t* buf, ...) { - const char* str; - va_list ap; + const char* str; + va_list ap; - ASSERT(buf); + ASSERT(buf); - va_start(ap, buf); + va_start(ap, buf); - if(ha_buferr(buf)) - return NULL; + if(ha_buferr(buf)) + return NULL; - if(!BUF_IN_JOIN(buf)) - BUF_NEW_BLOCK(buf); + if(!BUF_IN_JOIN(buf)) + BUF_NEW_BLOCK(buf); - while((str = va_arg(ap, char*)) != NULL) - { - int len = strlen(str); + while((str = va_arg(ap, char*)) != NULL) + { + int len = strlen(str); - /* Always add one for the terminating char */ - buffer_bump(buf, len + 1); + /* Always add one for the terminating char */ + buffer_bump(buf, len + 1); - if(ha_buferr(buf)) - return NULL; + if(ha_buferr(buf)) + return NULL; - /* _rp always points to the next write point */ - strcpy(buf->_rp, str); - buf->_rp += len; - } + /* _rp always points to the next write point */ + strcpy(buf->_rp, str); + buf->_rp += len; + } - buf->_rp++; - return buf->_pp; + buf->_rp++; + return buf->_pp; } char* ha_bufcpy(ha_buffer_t* buf, const char* src) { - size_t len; + size_t len; - ASSERT(buf && src); + ASSERT(buf && src); - len = strlen(src); - return ha_bufncpy(buf, src, len); + len = strlen(src); + return ha_bufncpy(buf, src, len); } char* ha_bufncpy(ha_buffer_t* buf, const char* src, size_t len) { - ASSERT(buf && src); + ASSERT(buf && src); - if(ha_buferr(buf)) - return NULL; + if(ha_buferr(buf)) + return NULL; - /* Always add one for the terminating char */ - buffer_bump(buf, len + 1); + /* Always add one for the terminating char */ + buffer_bump(buf, len + 1); - if(ha_buferr(buf)) - return NULL; + if(ha_buferr(buf)) + return NULL; - if(!BUF_IN_JOIN(buf)) - BUF_NEW_BLOCK(buf); + if(!BUF_IN_JOIN(buf)) + BUF_NEW_BLOCK(buf); - memcpy(buf->_rp, src, len * sizeof(char)); - buf->_rp += (len + 1); - *(buf->_rp - 1) = 0; - return buf->_pp; + memcpy(buf->_rp, src, len * sizeof(char)); + buf->_rp += (len + 1); + *(buf->_rp - 1) = 0; + return buf->_pp; } void* ha_bufmalloc(ha_buffer_t* buf, size_t sz) { - void* ret; + void* ret; - ASSERT(buf && sz); + ASSERT(buf && sz); - if(ha_buferr(buf)) - return NULL; + if(ha_buferr(buf)) + return NULL; - /* TODO: Align memory on an appropriate boundary here */ - buffer_bump(buf, sz); + /* TODO: Align memory on an appropriate boundary here */ + buffer_bump(buf, sz); - if(ha_buferr(buf)) - return NULL; + if(ha_buferr(buf)) + return NULL; - BUF_NEW_BLOCK(buf); - ret = (void*)buf->_pp; - buf->_rp += sz; - buf->_pp = buf->_rp; - return ret; + BUF_NEW_BLOCK(buf); + ret = (void*)buf->_pp; + buf->_rp += sz; + buf->_pp = buf->_rp; + return ret; } void* ha_bufmemdup(ha_buffer_t* buf, const void* src, size_t bytes) { - void* mem; + void* mem; - ASSERT(buf && src && bytes); + ASSERT(buf && src && bytes); - if((mem = ha_bufmalloc(buf, bytes)) != NULL) - memcpy(mem, src, bytes); + if((mem = ha_bufmalloc(buf, bytes)) != NULL) + memcpy(mem, src, bytes); - return mem; + return mem; } /* @@ -457,251 +457,251 @@ static const char PAD64C = '='; char* ha_bufenc64(ha_buffer_t* buf, const void* source, size_t len) { - unsigned char input[3]; - unsigned char output[4]; - unsigned char* src = (unsigned char*)source; - size_t i; + unsigned char input[3]; + unsigned char output[4]; + unsigned char* src = (unsigned char*)source; + size_t i; - ASSERT(buf && source && len); - - if(ha_buferr(buf)) - return NULL; - - if(!BUF_IN_JOIN(buf)) - BUF_NEW_BLOCK(buf); - - while(2 < len) - { - input[0] = *src++; - input[1] = *src++; - input[2] = *src++; - len -= 3; - - output[0] = input[0] >> 2; - output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4); - output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6); - output[3] = input[2] & 0x3f; - - /* This also accounts for the null terminator */ - buffer_bump(buf, 5); + ASSERT(buf && source && len); if(ha_buferr(buf)) - return NULL; - - *(buf->_rp++) = BASE64C[output[0]]; - *(buf->_rp++) = BASE64C[output[1]]; - *(buf->_rp++) = BASE64C[output[2]]; - *(buf->_rp++) = BASE64C[output[3]]; - } + return NULL; - /* Now we worry about padding. */ - if(0 != len) - { - /* Get what's left. */ - input[0] = input[1] = input[2] = '\0'; - for(i = 0; i < len; i++) - input[i] = *src++; + if(!BUF_IN_JOIN(buf)) + BUF_NEW_BLOCK(buf); - output[0] = input[0] >> 2; - output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4); - output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6); - - /* This also accounts for the null terminator */ - buffer_bump(buf, 5); - - if(ha_buferr(buf)) - return NULL; + while(2 < len) + { + input[0] = *src++; + input[1] = *src++; + input[2] = *src++; + len -= 3; + + output[0] = input[0] >> 2; + output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4); + output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6); + output[3] = input[2] & 0x3f; + + /* This also accounts for the null terminator */ + buffer_bump(buf, 5); + + if(ha_buferr(buf)) + return NULL; + + *(buf->_rp++) = BASE64C[output[0]]; + *(buf->_rp++) = BASE64C[output[1]]; + *(buf->_rp++) = BASE64C[output[2]]; + *(buf->_rp++) = BASE64C[output[3]]; + } - *(buf->_rp++) = BASE64C[output[0]]; - *(buf->_rp++) = BASE64C[output[1]]; - if(len == 1) - *(buf->_rp++) = PAD64C; - else - *(buf->_rp++) = BASE64C[output[2]]; - *(buf->_rp++) = PAD64C; - } + /* Now we worry about padding. */ + if(0 != len) + { + /* Get what's left. */ + input[0] = input[1] = input[2] = '\0'; + for(i = 0; i < len; i++) + input[i] = *src++; + + output[0] = input[0] >> 2; + output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4); + output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6); + + /* This also accounts for the null terminator */ + buffer_bump(buf, 5); + + if(ha_buferr(buf)) + return NULL; + + *(buf->_rp++) = BASE64C[output[0]]; + *(buf->_rp++) = BASE64C[output[1]]; + if(len == 1) + *(buf->_rp++) = PAD64C; + else + *(buf->_rp++) = BASE64C[output[2]]; + *(buf->_rp++) = PAD64C; + } - *(buf->_rp++) = '\0'; - return buf->_pp; + *(buf->_rp++) = '\0'; + return buf->_pp; } void* ha_bufdec64(ha_buffer_t* buf, const char* src, size_t* bytes) { - int state = 0; - int ch; - char* pos; - void* ret; - size_t todo = 0; - size_t done = 0; - - ASSERT(buf && src); - - if(ha_buferr(buf)) - return NULL; - - BUF_NEW_BLOCK(buf); + int state = 0; + int ch; + char* pos; + void* ret; + size_t todo = 0; + size_t done = 0; - if(!bytes || *bytes == 0) - todo = ~0; - else - todo = *bytes; + ASSERT(buf && src); - while((ch = *src++) != '\0' && done < todo) - { - if(isspace(ch)) /* Skip whitespace anywhere. */ - continue; - - if(ch == PAD64C) - break; - - pos = strchr(BASE64C, ch); - if(pos == 0) /* A non-base64 character. */ - break; + if(ha_buferr(buf)) + return NULL; - buffer_bump(buf, 4); + BUF_NEW_BLOCK(buf); - if(ha_buferr(buf)) - return NULL; + if(!bytes || *bytes == 0) + todo = ~0; + else + todo = *bytes; - switch(state) + while((ch = *src++) != '\0' && done < todo) { - case 0: - *(buf->_rp) = (pos - BASE64C) << 2; - state = 1; - break; - - case 1: - *(buf->_rp++) |= (pos - BASE64C) >> 4; - done++; - *(buf->_rp) = ((pos - BASE64C) & 0x0f) << 4; - state = 2; - break; - - case 2: - *(buf->_rp++) |= (pos - BASE64C) >> 2; - done++; - *(buf->_rp) = ((pos - BASE64C) & 0x03) << 6; - state = 3; - break; - - case 3: - *(buf->_rp++) |= (pos - BASE64C); - done++; - state = 0; - break; - }; - } - - if(state != 3) - buf->_rp++; + if(isspace(ch)) /* Skip whitespace anywhere. */ + continue; + + if(ch == PAD64C) + break; + + pos = strchr(BASE64C, ch); + if(pos == 0) /* A non-base64 character. */ + break; + + buffer_bump(buf, 4); + + if(ha_buferr(buf)) + return NULL; + + switch(state) + { + case 0: + *(buf->_rp) = (pos - BASE64C) << 2; + state = 1; + break; + + case 1: + *(buf->_rp++) |= (pos - BASE64C) >> 4; + done++; + *(buf->_rp) = ((pos - BASE64C) & 0x0f) << 4; + state = 2; + break; + + case 2: + *(buf->_rp++) |= (pos - BASE64C) >> 2; + done++; + *(buf->_rp) = ((pos - BASE64C) & 0x03) << 6; + state = 3; + break; + + case 3: + *(buf->_rp++) |= (pos - BASE64C); + done++; + state = 0; + break; + }; + } + + if(state != 3) + buf->_rp++; - /* TODO: Validate ending and return error if invalid somehow */ + /* TODO: Validate ending and return error if invalid somehow */ - /* We always null terminate anyway */ - *(buf->_rp++) = 0; + /* We always null terminate anyway */ + *(buf->_rp++) = 0; - if(bytes) - *bytes = done; + if(bytes) + *bytes = done; - ret = (void*)buf->_pp; - buf->_pp = buf->_rp; - return ret; + ret = (void*)buf->_pp; + buf->_pp = buf->_rp; + return ret; } static const char HEXC[] = "0123456789abcdef"; char* ha_bufenchex(ha_buffer_t* buf, const void* source, size_t len) { - unsigned char* src = (unsigned char*)source; - unsigned char j; + unsigned char* src = (unsigned char*)source; + unsigned char j; - ASSERT(buf && source && len); + ASSERT(buf && source && len); - buffer_bump(buf, (len * 2) + 1); + buffer_bump(buf, (len * 2) + 1); - if(ha_buferr(buf)) - return NULL; + if(ha_buferr(buf)) + return NULL; - if(!BUF_IN_JOIN(buf)) - BUF_NEW_BLOCK(buf); + if(!BUF_IN_JOIN(buf)) + BUF_NEW_BLOCK(buf); - while(len > 0) - { - j = *(src) >> 4 & 0xf; - *(buf->_rp++) = HEXC[j]; + while(len > 0) + { + j = *(src) >> 4 & 0xf; + *(buf->_rp++) = HEXC[j]; - j = *(src++) & 0xf; - *(buf->_rp++) = HEXC[j]; + j = *(src++) & 0xf; + *(buf->_rp++) = HEXC[j]; - len--; - } + len--; + } - *(buf->_rp++) = 0; - return buf->_pp; + *(buf->_rp++) = 0; + return buf->_pp; } void* ha_bufdechex(ha_buffer_t* buf, const char* src, size_t* bytes) { - unsigned short j; - int state = 0; - char* pos; - void* ret; - size_t done = 0; - size_t todo = 0; - - ASSERT(buf && src); - - if(bytes && *bytes != 0) - { - buffer_bump(buf, *bytes + 1); - todo = *bytes; - } - else - { - todo = ~0; - buffer_bump(buf, (strlen(src) / 2) + 1); - } - - if(ha_buferr(buf)) - return NULL; - - BUF_NEW_BLOCK(buf); - - while(src[0] && done < todo) - { - /* Find the position */ - pos = strchr(HEXC, tolower(src[0])); - if(pos == 0) - break; - - j = pos - HEXC; - - if(!state) + unsigned short j; + int state = 0; + char* pos; + void* ret; + size_t done = 0; + size_t todo = 0; + + ASSERT(buf && src); + + if(bytes && *bytes != 0) { - *(buf->_rp) = (j & 0xf) << 4; - state = 1; + buffer_bump(buf, *bytes + 1); + todo = *bytes; } else { - *(buf->_rp++) |= (j & 0xf); - done++; - state = 0; + todo = ~0; + buffer_bump(buf, (strlen(src) / 2) + 1); } - src++; - } + if(ha_buferr(buf)) + return NULL; + + BUF_NEW_BLOCK(buf); + + while(src[0] && done < todo) + { + /* Find the position */ + pos = strchr(HEXC, tolower(src[0])); + if(pos == 0) + break; + + j = pos - HEXC; + + if(!state) + { + *(buf->_rp) = (j & 0xf) << 4; + state = 1; + } + else + { + *(buf->_rp++) |= (j & 0xf); + done++; + state = 0; + } + + src++; + } - /* We always null terminate anyway */ - *(buf->_rp++) = 0; + /* We always null terminate anyway */ + *(buf->_rp++) = 0; - /* All bytes have to come in pairs */ - if(state != 0) - return NULL; + /* All bytes have to come in pairs */ + if(state != 0) + return NULL; - if(bytes) - *bytes = done; + if(bytes) + *bytes = done; - ret = (void*)buf->_pp; - buf->_pp = buf->_rp; - return ret; + ret = (void*)buf->_pp; + buf->_pp = buf->_rp; + return ret; } diff --git a/common/compat.c b/common/compat.c index 5618ee5..daddbd2 100644 --- a/common/compat.c +++ b/common/compat.c @@ -19,25 +19,25 @@ void* reallocf(void* ptr, size_t size) #ifndef HAVE_STRLWR char* strlwr(char* s) { - char* t = s; - while(*t) - { - *t = tolower(*t); - t++; - } - return s; + char* t = s; + while(*t) + { + *t = tolower(*t); + t++; + } + return s; } #endif #ifndef HAVE_STRUPR char* strupr(char* s) { - char* t = s; - while(*t) - { - *t = toupper(*t); - t++; - } - return s; + char* t = s; + while(*t) + { + *t = toupper(*t); + t++; + } + return s; } #endif diff --git a/common/compat.h b/common/compat.h index bb7311c..c7f86cc 100644 --- a/common/compat.h +++ b/common/compat.h @@ -10,7 +10,7 @@ #include <sys/types.h> #ifndef HAVE_STDARG_H -#error ERROR: Must have a working stdarg.h header +# error ERROR: Must have a working stdarg.h header #else #include <stdarg.h> #endif @@ -23,13 +23,13 @@ void* reallocf(void* p, size_t sz); /* TODO: Move this logic to configure */ #if HAVE_ERR_MUTEX == 1 -# define HA_MUTEX_TYPE PTHREAD_MUTEX_ERRORCHECK_NP +# define HA_MUTEX_TYPE PTHREAD_MUTEX_ERRORCHECK_NP #else -# if HAVE_ERR_MUTEX == 2 -# define HA_MUTEX_TYPE PTHREAD_MUTEX_ERRORCHECK -# else -# error "Need error checking mutex functionality" -# endif +# if HAVE_ERR_MUTEX == 2 +# define HA_MUTEX_TYPE PTHREAD_MUTEX_ERRORCHECK +# else +# error "Need error checking mutex functionality" +# endif #endif #ifndef HAVE_STRLWR diff --git a/common/hash.c b/common/hash.c index 5f3308c..3ff3982 100644 --- a/common/hash.c +++ b/common/hash.c @@ -9,7 +9,7 @@ * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -27,9 +27,9 @@ #endif #ifdef HSH_COPYKEYS - #define KEY_DATA(he) (void*)(((unsigned char*)(he)) + sizeof(*(he))) + #define KEY_DATA(he) (void*)(((unsigned char*)(he)) + sizeof(*(he))) #else - #define KEY_DATA(he) ((he)->key) + #define KEY_DATA(he) ((he)->key) #endif /* @@ -45,15 +45,15 @@ typedef struct hsh_entry_t hsh_entry_t; struct hsh_entry_t { - hsh_entry_t* next; - unsigned int hash; + hsh_entry_t* next; + unsigned int hash; #ifndef HSH_COPYKEYS - const void* key; - size_t klen; + const void* key; + size_t klen; #endif - const void* val; + const void* val; #ifdef HSH_TIMESTAMP - time_t stamp; + time_t stamp; #endif }; @@ -66,10 +66,10 @@ struct hsh_entry_t */ struct hsh_index_t { - hsh_t* ht; - hsh_entry_t* ths; - hsh_entry_t* next; - unsigned int index; + hsh_t* ht; + hsh_entry_t* ths; + hsh_entry_t* next; + unsigned int index; }; /* @@ -81,15 +81,15 @@ struct hsh_index_t */ struct hsh_t { - hsh_entry_t** array; - hsh_index_t iterator; /* For hsh_first(...) */ - unsigned int count; - unsigned int max; + hsh_entry_t** array; + hsh_index_t iterator; /* For hsh_first(...) */ + unsigned int count; + unsigned int max; #ifdef HSH_COPYKEYS - unsigned int klen; + unsigned int klen; #endif #ifdef HSH_CALLBACKS - hsh_table_calls_t calls; + hsh_table_calls_t calls; #endif }; @@ -106,47 +106,47 @@ static hsh_memory_calls_t* g_memory_calls = NULL; static void* int_malloc(size_t len) { - if(g_memory_calls) - return (g_memory_calls->f_alloc)(g_memory_calls->arg, len); - else - return malloc(len); + if(g_memory_calls) + return (g_memory_calls->f_alloc)(g_memory_calls->arg, len); + else + return malloc(len); } static void* int_calloc(size_t len) { - void* p = int_malloc(len); - memset(p, 0, len); - return p; + void* p = int_malloc(len); + memset(p, 0, len); + return p; } static void int_free(void* ptr) { - if(g_memory_calls) - { - /* We allow for gc type memory allocation with a null free */ - if(g_memory_calls->f_free) - (g_memory_calls->f_free)(g_memory_calls->arg, ptr); - } - else - free(ptr); + if(g_memory_calls) + { + /* We allow for gc type memory allocation with a null free */ + if(g_memory_calls->f_free) + (g_memory_calls->f_free)(g_memory_calls->arg, ptr); + } + else + free(ptr); } void hsh_set_memory_calls(hsh_memory_calls_t* hmc) { - if(hmc == NULL) - { - g_memory_calls = NULL; - } - else - { - memcpy(&g_memory_calls_cpy, hmc, sizeof(g_memory_calls_cpy)); - g_memory_calls = &g_memory_calls_cpy; - } + if(hmc == NULL) + { + g_memory_calls = NULL; + } + else + { + memcpy(&g_memory_calls_cpy, hmc, sizeof(g_memory_calls_cpy)); + g_memory_calls = &g_memory_calls_cpy; + } } void hsh_set_table_calls(hsh_t* ht, hsh_table_calls_t* htc) { - memcpy(&(ht->calls), htc, sizeof(ht->calls)); + memcpy(&(ht->calls), htc, sizeof(ht->calls)); } #else @@ -163,7 +163,7 @@ void hsh_set_table_calls(hsh_t* ht, hsh_table_calls_t* htc) static hsh_entry_t** alloc_array(hsh_t* ht, unsigned int max) { - return int_calloc(sizeof(*(ht->array)) * (max + 1)); + return int_calloc(sizeof(*(ht->array)) * (max + 1)); } #ifdef HSH_COPYKEYS @@ -172,41 +172,41 @@ hsh_t* hsh_create(size_t klen) hsh_t* hsh_create() #endif { - hsh_t* ht = int_malloc(sizeof(hsh_t)); - if(ht) - { - ht->count = 0; - ht->max = INITIAL_MAX; - ht->array = alloc_array(ht, ht->max); + hsh_t* ht = int_malloc(sizeof(hsh_t)); + if(ht) + { + ht->count = 0; + ht->max = INITIAL_MAX; + ht->array = alloc_array(ht, ht->max); #ifdef HSH_COPYKEYS - ht->klen = klen; + ht->klen = klen; #endif - if(!ht->array) - { - int_free(ht); - return NULL; + if(!ht->array) + { + int_free(ht); + return NULL; + } } - } - return ht; + return ht; } void hsh_free(hsh_t* ht) { - hsh_index_t* hi; + hsh_index_t* hi; - for(hi = hsh_first(ht); hi; hi = hsh_next(hi)) - { + for(hi = hsh_first(ht); hi; hi = hsh_next(hi)) + { #ifdef HSH_CALLBACKS - if(hi->ths->val && ht->calls.f_freeval) - (ht->calls.f_freeval)(ht->calls.arg, (void*)hi->ths->val); + if(hi->ths->val && ht->calls.f_freeval) + (ht->calls.f_freeval)(ht->calls.arg, (void*)hi->ths->val); #endif - int_free(hi->ths); - } + int_free(hi->ths); + } - if(ht->array) - int_free(ht->array); + if(ht->array) + int_free(ht->array); - int_free(ht); + int_free(ht); } /* @@ -215,27 +215,27 @@ void hsh_free(hsh_t* ht) hsh_index_t* hsh_next(hsh_index_t* hi) { - hi->ths = hi->next; - while(!hi->ths) - { - if(hi->index > hi->ht->max) - return NULL; - - hi->ths = hi->ht->array[hi->index++]; - } - hi->next = hi->ths->next; - return hi; + hi->ths = hi->next; + while(!hi->ths) + { + if(hi->index > hi->ht->max) + return NULL; + + hi->ths = hi->ht->array[hi->index++]; + } + hi->next = hi->ths->next; + return hi; } hsh_index_t* hsh_first(hsh_t* ht) { - hsh_index_t* hi = &ht->iterator; + hsh_index_t* hi = &ht->iterator; - hi->ht = ht; - hi->index = 0; - hi->ths = NULL; - hi->next = NULL; - return hsh_next(hi); + hi->ht = ht; + hi->index = 0; + hi->ths = NULL; + hi->next = NULL; + return hsh_next(hi); } #ifdef HSH_COPYKEYS @@ -244,15 +244,15 @@ void* hsh_this(hsh_index_t* hi, const void** key) void* hsh_this(hsh_index_t* hi, const void** key, size_t* klen) #endif { - if(key) - *key = KEY_DATA(hi->ths); + if(key) + *key = KEY_DATA(hi->ths); #ifndef HSH_COPYKEYS - if(klen) - *klen = hi->ths->klen; + if(klen) + *klen = hi->ths->klen; #endif - return (void*)hi->ths->val; + return (void*)hi->ths->val; } @@ -262,29 +262,29 @@ void* hsh_this(hsh_index_t* hi, const void** key, size_t* klen) static int expand_array(hsh_t* ht) { - hsh_index_t* hi; - hsh_entry_t** new_array; - unsigned int new_max; + hsh_index_t* hi; + hsh_entry_t** new_array; + unsigned int new_max; - new_max = ht->max * 2 + 1; - new_array = alloc_array(ht, new_max); + new_max = ht->max * 2 + 1; + new_array = alloc_array(ht, new_max); - if(!new_array) - return 0; + if(!new_array) + return 0; - for(hi = hsh_first(ht); hi; hi = hsh_next(hi)) - { - unsigned int i = hi->ths->hash & new_max; - hi->ths->next = new_array[i]; - new_array[i] = hi->ths; - } + for(hi = hsh_first(ht); hi; hi = hsh_next(hi)) + { + unsigned int i = hi->ths->hash & new_max; + hi->ths->next = new_array[i]; + new_array[i] = hi->ths; + } - if(ht->array) - free(ht->array); + if(ht->array) + free(ht->array); - ht->array = new_array; - ht->max = new_max; - return 1; + ht->array = new_array; + ht->max = new_max; + return 1; } /* @@ -302,283 +302,283 @@ static hsh_entry_t** find_entry(hsh_t* ht, const void* key, const void* val) static hsh_entry_t** find_entry(hsh_t* ht, const void* key, size_t klen, const void* val) #endif { - hsh_entry_t** hep; - hsh_entry_t* he; - const unsigned char* p; - unsigned int hash; - size_t i; + hsh_entry_t** hep; + hsh_entry_t* he; + const unsigned char* p; + unsigned int hash; + size_t i; #ifdef HSH_COPYKEYS - size_t klen = ht->klen; + size_t klen = ht->klen; #endif - /* - * This is the popular `times 33' hash algorithm which is used by - * perl and also appears in Berkeley DB. This is one of the best - * known hash functions for strings because it is both computed - * very fast and distributes very well. - * - * The originator may be Dan Bernstein but the code in Berkeley DB - * cites Chris Torek as the source. The best citation I have found - * is "Chris Torek, Hash function for text in C, Usenet message - * <27038@mimsy.umd.edu> in comp.lang.c , October, 1990." in Rich - * Salz's USENIX 1992 paper about INN which can be found at - * <http://citeseer.nj.nec.com/salz92internetnews.html>. - * - * The magic of number 33, i.e. why it works better than many other - * constants, prime or not, has never been adequately explained by - * anyone. So I try an explanation: if one experimentally tests all - * multipliers between 1 and 256 (as I did while writing a low-level - * data structure library some time ago) one detects that even - * numbers are not useable at all. The remaining 128 odd numbers - * (except for the number 1) work more or less all equally well. - * They all distribute in an acceptable way and this way fill a hash - * table with an average percent of approx. 86%. - * - * If one compares the chi^2 values of the variants (see - * Bob Jenkins ``Hashing Frequently Asked Questions'' at - * http://burtleburtle.net/bob/hash/hashfaq.html for a description - * of chi^2), the number 33 not even has the best value. But the - * number 33 and a few other equally good numbers like 17, 31, 63, - * 127 and 129 have nevertheless a great advantage to the remaining - * numbers in the large set of possible multipliers: their multiply - * operation can be replaced by a faster operation based on just one - * shift plus either a single addition or subtraction operation. And - * because a hash function has to both distribute good _and_ has to - * be very fast to compute, those few numbers should be preferred. - * - * -- Ralf S. Engelschall <rse@engelschall.com> - */ - hash = 0; + /* + * This is the popular `times 33' hash algorithm which is used by + * perl and also appears in Berkeley DB. This is one of the best + * known hash functions for strings because it is both computed + * very fast and distributes very well. + * + * The originator may be Dan Bernstein but the code in Berkeley DB + * cites Chris Torek as the source. The best citation I have found + * is "Chris Torek, Hash function for text in C, Usenet message + * <27038@mimsy.umd.edu> in comp.lang.c , October, 1990." in Rich + * Salz's USENIX 1992 paper about INN which can be found at + * <http://citeseer.nj.nec.com/salz92internetnews.html>. + * + * The magic of number 33, i.e. why it works better than many other + * constants, prime or not, has never been adequately explained by + * anyone. So I try an explanation: if one experimentally tests all + * multipliers between 1 and 256 (as I did while writing a low-level + * data structure library some time ago) one detects that even + * numbers are not useable at all. The remaining 128 odd numbers + * (except for the number 1) work more or less all equally well. + * They all distribute in an acceptable way and this way fill a hash + * table with an average percent of approx. 86%. + * + * If one compares the chi^2 values of the variants (see + * Bob Jenkins ``Hashing Frequently Asked Questions'' at + * http://burtleburtle.net/bob/hash/hashfaq.html for a description + * of chi^2), the number 33 not even has the best value. But the + * number 33 and a few other equally good numbers like 17, 31, 63, + * 127 and 129 have nevertheless a great advantage to the remaining + * numbers in the large set of possible multipliers: their multiply + * operation can be replaced by a faster operation based on just one + * shift plus either a single addition or subtraction operation. And + * because a hash function has to both distribute good _and_ has to + * be very fast to compute, those few numbers should be preferred. + * + * -- Ralf S. Engelschall <rse@engelschall.com> + */ + hash = 0; #ifndef HSH_COPYKEYS - if(klen == HSH_KEY_STRING) - { - for(p = key; *p; p++) - hash = hash * 33 + *p; - - klen = p - (const unsigned char *)key; - } - else + if(klen == HSH_KEY_STRING) + { + for(p = key; *p; p++) + hash = hash * 33 + *p; + + klen = p - (const unsigned char *)key; + } + else #endif - { - for(p = key, i = klen; i; i--, p++) - hash = hash * 33 + *p; - } - - /* scan linked list */ - for(hep = &ht->array[hash & ht->max], he = *hep; - he; hep = &he->next, he = *hep) - { - if(he->hash == hash && + { + for(p = key, i = klen; i; i--, p++) + hash = hash * 33 + *p; + } + + /* scan linked list */ + for(hep = &ht->array[hash & ht->max], he = *hep; + he; hep = &he->next, he = *hep) + { + if(he->hash == hash && #ifndef HSH_COPYKEYS - he->klen == klen && + he->klen == klen && #endif - memcmp(KEY_DATA(he), key, klen) == 0) - break; - } + memcmp(KEY_DATA(he), key, klen) == 0) + break; + } - if(he || !val) - return hep; + if(he || !val) + return hep; - /* add a new entry for non-NULL val */ + /* add a new entry for non-NULL val */ #ifdef HSH_COPYKEYS - he = int_malloc(sizeof(*he) + klen); + he = int_malloc(sizeof(*he) + klen); #else - he = int_malloc(sizeof(*he)); + he = int_malloc(sizeof(*he)); #endif - if(he) - { + if(he) + { #ifdef HSH_COPYKEYS - /* Key data points past end of entry */ - memcpy(KEY_DATA(he), key, klen); + /* Key data points past end of entry */ + memcpy(KEY_DATA(he), key, klen); #else - /* Key points to external data */ - he->key = key; - he->klen = klen; + /* Key points to external data */ + he->key = key; + he->klen = klen; #endif - he->next = NULL; - he->hash = hash; - he->val = val; + he->next = NULL; + he->hash = hash; + he->val = val; #ifdef HSH_TIMESTAMP - he->stamp = 0; + he->stamp = 0; #endif - *hep = he; - ht->count++; - } + *hep = he; + ht->count++; + } - return hep; + return hep; } #ifdef HSH_COPYKEYS void* hsh_get(hsh_t* ht, const void *key) { - hsh_entry_t** he = find_entry(ht, key, NULL); + hsh_entry_t** he = find_entry(ht, key, NULL); #else void* hsh_get(hsh_t* ht, const void *key, size_t klen) { - hsh_entry_t** he = find_entry(ht, key, klen, NULL); + hsh_entry_t** he = find_entry(ht, key, klen, NULL); #endif - if(he && *he) - return (void*)((*he)->val); - else - return NULL; + if(he && *he) + return (void*)((*he)->val); + else + return NULL; } #ifdef HSH_COPYKEYS int hsh_set(hsh_t* ht, const void* key, void* val) { - hsh_entry_t** hep = find_entry(ht, key, val); + hsh_entry_t** hep = find_entry(ht, key, val); #else int hsh_set(hsh_t* ht, const void* key, size_t klen, void* val) { - hsh_entry_t** hep = find_entry(ht, key, klen, val); + hsh_entry_t** hep = find_entry(ht, key, klen, val); #endif - if(hep && *hep) - { + if(hep && *hep) + { #ifdef HSH_CALLBACKS - if((*hep)->val && (*hep)->val != val && ht->calls.f_freeval) - (ht->calls.f_freeval)(ht->calls.arg, (void*)((*hep)->val)); + if((*hep)->val && (*hep)->val != val && ht->calls.f_freeval) + (ht->calls.f_freeval)(ht->calls.arg, (void*)((*hep)->val)); #endif - /* replace entry */ - (*hep)->val = val; + /* replace entry */ + (*hep)->val = val; #ifdef HSH_TIMESTAMP - /* Update or set the timestamp */ - (*hep)->stamp = time(NULL); + /* Update or set the timestamp */ + (*hep)->stamp = time(NULL); #endif - /* check that the collision rate isn't too high */ - if(ht->count > ht->max) - { - if(!expand_array(ht)) - return 0; - } + /* check that the collision rate isn't too high */ + if(ht->count > ht->max) + { + if(!expand_array(ht)) + return 0; + } - return 1; - } + return 1; + } - return 0; + return 0; } #ifdef HSH_COPYKEYS void* hsh_rem(hsh_t* ht, const void* key) { - hsh_entry_t** hep = find_entry(ht, key, NULL); + hsh_entry_t** hep = find_entry(ht, key, NULL); #else void* hsh_rem(hsh_t* ht, const void* key, size_t klen) { - hsh_entry_t** hep = find_entry(ht, key, klen, NULL); + hsh_entry_t** hep = find_entry(ht, key, klen, NULL); #endif - void* val = NULL; - - if(hep && *hep) - { - hsh_entry_t* old = *hep; - *hep = (*hep)->next; - --ht->count; - val = (void*)old->val; - free(old); - } - - return val; + void* val = NULL; + + if(hep && *hep) + { + hsh_entry_t* old = *hep; + *hep = (*hep)->next; + --ht->count; + val = (void*)old->val; + free(old); + } + + return val; } unsigned int hsh_count(hsh_t* ht) { - return ht->count; + return ht->count; } #ifdef HSH_TIMESTAMP int hsh_purge(hsh_t* ht, time_t stamp) { - hsh_index_t* hi; - int r = 0; - void* val; + hsh_index_t* hi; + int r = 0; + void* val; - for(hi = hsh_first(ht); hi; hi = hsh_next(hi)) - { - if(hi->ths->stamp < stamp) + for(hi = hsh_first(ht); hi; hi = hsh_next(hi)) { - /* No need to check for errors as we're deleting */ + if(hi->ths->stamp < stamp) + { + /* No need to check for errors as we're deleting */ #ifdef HSH_COPYKEYS - val = hsh_rem(ht, KEY_DATA(hi->ths)); + val = hsh_rem(ht, KEY_DATA(hi->ths)); #else - val = hsh_rem(ht, hi->ths->key, hi->ths->klen); + val = hsh_rem(ht, hi->ths->key, hi->ths->klen); #endif #ifdef HSH_CALLBACKS - if(val && ht->calls.f_freeval) - (ht->calls.f_freeval)(ht->calls.arg, val); + if(val && ht->calls.f_freeval) + (ht->calls.f_freeval)(ht->calls.arg, val); #endif - r++; + r++; + } } - } - return r; + return r; } #ifdef HSH_COPYKEYS void hsh_touch(hsh_t* ht, const void* key) { - hsh_entry_t** hep = find_entry(ht, key, NULL); + hsh_entry_t** hep = find_entry(ht, key, NULL); #else void hsh_touch(hsh_t* ht, const void* key, size_t* klen) { - hsh_entry_t** hep = find_entry(ht, key, klen, NULL); + hsh_entry_t** hep = find_entry(ht, key, klen, NULL); #endif - if(hep && *hep) - ((*hep)->stamp) = time(NULL); + if(hep && *hep) + ((*hep)->stamp) = time(NULL); } int hsh_bump(hsh_t* ht) { - hsh_index_t* hi; - void* key = NULL; - void* val = NULL; - time_t least = 0; + hsh_index_t* hi; + void* key = NULL; + void* val = NULL; + time_t least = 0; #ifndef HSH_COPYKEYS - size_t klen = 0; + size_t klen = 0; #endif - for(hi = hsh_first(ht); hi; hi = hsh_next(hi)) - { - if(least == 0 || hi->ths->stamp < least) + for(hi = hsh_first(ht); hi; hi = hsh_next(hi)) { - least = hi->ths->stamp; - key = KEY_DATA(hi->ths); + if(least == 0 || hi->ths->stamp < least) + { + least = hi->ths->stamp; + key = KEY_DATA(hi->ths); #ifndef HSH_COPYKEYS - klen = hi->this->klen; + klen = hi->this->klen; #endif + } } - } - if(key) - { + if(key) + { #ifdef HSH_COPYKEYS - val = hsh_rem(ht, key); + val = hsh_rem(ht, key); #else - val = hsh_rem(ht, key, klen); + val = hsh_rem(ht, key, klen); #endif #ifdef HSH_CALLBACKS - if(val && ht->calls.f_freeval) - (ht->calls.f_freeval)(ht->calls.arg, (void*)val); + if(val && ht->calls.f_freeval) + (ht->calls.f_freeval)(ht->calls.arg, (void*)val); #endif - return 1; - } + return 1; + } - return 0; + return 0; } #endif /* HSH_TIMESTAMP */ diff --git a/common/md5.c b/common/md5.c index 2f92bec..00058ef 100644 --- a/common/md5.c +++ b/common/md5.c @@ -24,13 +24,13 @@ void md5_transform(unsigned int buf[4], unsigned int const in[16]); void byteSwap(unsigned int *buf, unsigned words) { - unsigned char *p = (unsigned char *)buf; + unsigned char *p = (unsigned char *)buf; - do { - *buf++ = (unsigned int)((unsigned)p[3] << 8 | p[2]) << 16 | - ((unsigned)p[1] << 8 | p[0]); - p += 4; - } while (--words); + do { + *buf++ = (unsigned int)((unsigned)p[3] << 8 | p[2]) << 16 | + ((unsigned)p[1] << 8 | p[0]); + p += 4; + } while (--words); } #else #define byteSwap(buf,words) @@ -43,13 +43,13 @@ byteSwap(unsigned int *buf, unsigned words) void md5_init(md5_ctx_t* ctx) { - ctx->buf[0] = 0x67452301; - ctx->buf[1] = 0xefcdab89; - ctx->buf[2] = 0x98badcfe; - ctx->buf[3] = 0x10325476; + ctx->buf[0] = 0x67452301; + ctx->buf[1] = 0xefcdab89; + ctx->buf[2] = 0x98badcfe; + ctx->buf[3] = 0x10325476; - ctx->bytes[0] = 0; - ctx->bytes[1] = 0; + ctx->bytes[0] = 0; + ctx->bytes[1] = 0; } /* @@ -59,38 +59,38 @@ md5_init(md5_ctx_t* ctx) void md5_update(md5_ctx_t* ctx, const void* b, unsigned len) { - unsigned int t; - const unsigned char* buf = (const unsigned char*)b; - - /* Update byte count */ - - t = ctx->bytes[0]; - if ((ctx->bytes[0] = t + len) < t) - ctx->bytes[1]++; /* Carry from low to high */ - - t = 64 - (t & 0x3f); /* Space available in ctx->in (at least 1) */ - if (t > len) { - memcpy((unsigned char *)ctx->in + 64 - t, buf, len); - return; - } - /* First chunk is an odd size */ - memcpy((unsigned char *)ctx->in + 64 - t, buf, t); + unsigned int t; + const unsigned char* buf = (const unsigned char*)b; + + /* Update byte count */ + + t = ctx->bytes[0]; + if ((ctx->bytes[0] = t + len) < t) + ctx->bytes[1]++; /* Carry from low to high */ + + t = 64 - (t & 0x3f); /* Space available in ctx->in (at least 1) */ + if (t > len) { + memcpy((unsigned char *)ctx->in + 64 - t, buf, len); + return; + } + /* First chunk is an odd size */ + memcpy((unsigned char *)ctx->in + 64 - t, buf, t); + byteSwap(ctx->in, 16); + md5_transform(ctx->buf, ctx->in); + buf += t; + len -= t; + + /* Process data in 64-byte chunks */ + while (len >= 64) { + memcpy(ctx->in, buf, 64); byteSwap(ctx->in, 16); md5_transform(ctx->buf, ctx->in); - buf += t; - len -= t; - - /* Process data in 64-byte chunks */ - while (len >= 64) { - memcpy(ctx->in, buf, 64); - byteSwap(ctx->in, 16); - md5_transform(ctx->buf, ctx->in); - buf += 64; - len -= 64; - } - - /* Handle any remaining bytes of data. */ - memcpy(ctx->in, buf, len); + buf += 64; + len -= 64; + } + + /* Handle any remaining bytes of data. */ + memcpy(ctx->in, buf, len); } /* @@ -100,33 +100,33 @@ md5_update(md5_ctx_t* ctx, const void* b, unsigned len) void md5_final(unsigned char digest[16], md5_ctx_t* ctx) { - int count = ctx->bytes[0] & 0x3f; /* Number of bytes in ctx->in */ - unsigned char *p = (unsigned char *)ctx->in + count; - - /* Set the first char of padding to 0x80. There is always room. */ - *p++ = 0x80; - - /* Bytes of padding needed to make 56 bytes (-8..55) */ - count = 56 - 1 - count; - - if (count < 0) { /* Padding forces an extra block */ - memset(p, 0, (unsigned int)count + 8); - byteSwap(ctx->in, 16); - md5_transform(ctx->buf, ctx->in); - p = (unsigned char *)ctx->in; - count = 56; - } - memset(p, 0, (unsigned int)count); - byteSwap(ctx->in, 14); - - /* Append length in bits and transform */ - ctx->in[14] = ctx->bytes[0] << 3; - ctx->in[15] = ctx->bytes[1] << 3 | ctx->bytes[0] >> 29; - md5_transform(ctx->buf, ctx->in); + int count = ctx->bytes[0] & 0x3f; /* Number of bytes in ctx->in */ + unsigned char *p = (unsigned char *)ctx->in + count; + + /* Set the first char of padding to 0x80. There is always room. */ + *p++ = 0x80; - byteSwap(ctx->buf, 4); - memcpy(digest, ctx->buf, 16); - memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */ + /* Bytes of padding needed to make 56 bytes (-8..55) */ + count = 56 - 1 - count; + + if (count < 0) { /* Padding forces an extra block */ + memset(p, 0, (unsigned int)count + 8); + byteSwap(ctx->in, 16); + md5_transform(ctx->buf, ctx->in); + p = (unsigned char *)ctx->in; + count = 56; + } + memset(p, 0, (unsigned int)count); + byteSwap(ctx->in, 14); + + /* Append length in bits and transform */ + ctx->in[14] = ctx->bytes[0] << 3; + ctx->in[15] = ctx->bytes[1] << 3 | ctx->bytes[0] >> 29; + md5_transform(ctx->buf, ctx->in); + + byteSwap(ctx->buf, 4); + memcpy(digest, ctx->buf, 16); + memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */ } void @@ -156,7 +156,7 @@ md5_strcmp(unsigned char digest[MD5_LEN], const char* str) /* This is the central step in the MD5 algorithm. */ #define MD5STEP(f,w,x,y,z,in,s) \ - (w += f(x,y,z) + in, w = (w<<s | w>>(32-s)) + x) + (w += f(x,y,z) + in, w = (w<<s | w>>(32-s)) + x) /* * The core of the MD5 algorithm, this alters an existing MD5 hash to @@ -166,83 +166,83 @@ md5_strcmp(unsigned char digest[MD5_LEN], const char* str) void md5_transform(unsigned int buf[4], unsigned int const in[16]) { - register unsigned int a, b, c, d; - - a = buf[0]; - b = buf[1]; - c = buf[2]; - d = buf[3]; - - MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7); - MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12); - MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17); - MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22); - MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7); - MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12); - MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17); - MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22); - MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7); - MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12); - MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17); - MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22); - MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7); - MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12); - MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17); - MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22); - - MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5); - MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9); - MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14); - MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20); - MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5); - MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9); - MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14); - MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20); - MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5); - MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9); - MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14); - MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20); - MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5); - MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9); - MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14); - MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20); - - MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4); - MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11); - MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16); - MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23); - MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4); - MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11); - MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16); - MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23); - MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4); - MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11); - MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16); - MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23); - MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4); - MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11); - MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16); - MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23); - - MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6); - MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10); - MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15); - MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21); - MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6); - MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10); - MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15); - MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21); - MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6); - MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10); - MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15); - MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21); - MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6); - MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10); - MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15); - MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21); - - buf[0] += a; - buf[1] += b; - buf[2] += c; - buf[3] += d; + register unsigned int a, b, c, d; + + a = buf[0]; + b = buf[1]; + c = buf[2]; + d = buf[3]; + + MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7); + MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12); + MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17); + MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22); + MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7); + MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12); + MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17); + MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22); + MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7); + MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12); + MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17); + MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22); + MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7); + MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12); + MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17); + MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22); + + MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5); + MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9); + MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14); + MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20); + MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5); + MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9); + MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14); + MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20); + MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5); + MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9); + MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14); + MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20); + MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5); + MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9); + MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14); + MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20); + + MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4); + MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11); + MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16); + MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23); + MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4); + MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11); + MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16); + MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23); + MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4); + MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11); + MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16); + MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23); + MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4); + MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11); + MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16); + MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23); + + MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6); + MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10); + MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15); + MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21); + MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6); + MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10); + MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15); + MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21); + MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6); + MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10); + MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15); + MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21); + MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6); + MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10); + MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15); + MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21); + + buf[0] += a; + buf[1] += b; + buf[2] += c; + buf[3] += d; } diff --git a/common/md5.h b/common/md5.h index de431da..d503f11 100644 --- a/common/md5.h +++ b/common/md5.h @@ -32,9 +32,9 @@ extern "C" { typedef struct md5_ctx { - unsigned int buf[4]; - unsigned int bytes[2]; - unsigned int in[16]; + unsigned int buf[4]; + unsigned int bytes[2]; + unsigned int in[16]; } md5_ctx_t; diff --git a/common/sha1.c b/common/sha1.c index a40732e..0666837 100644 --- a/common/sha1.c +++ b/common/sha1.c @@ -1,6 +1,6 @@ /* - * Fetched from ftp://ftp.funet.fi/pub/crypt/hash/sha/sha1.c on May 4, 1999 - * Modified by EPG May 6, 1999 + * Fetched from ftp://ftp.funet.fi/pub/crypt/hash/sha/sha1.c on May 4, 1999 + * Modified by EPG May 6, 1999 */ /* @@ -23,12 +23,12 @@ void SHA1Transform(ulong state[5], const uchar buffer[64]); /* I got the idea of expanding during the round function from SSLeay */ #ifdef LITTLE_ENDIAN #define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \ - |(rol(block->l[i],8)&0x00FF00FF)) + |(rol(block->l[i],8)&0x00FF00FF)) #else #define blk0(i) block->l[i] #endif #define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \ - ^block->l[(i+2)&15]^block->l[i&15],1)) + ^block->l[(i+2)&15]^block->l[i&15],1)) /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */ #define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30); @@ -42,54 +42,54 @@ void SHA1Transform(ulong state[5], const uchar buffer[64]); void SHA1Transform(ulong state[5], const uchar buffer[64]) { - ulong a, b, c, d, e; - typedef union { - uchar c[64]; - ulong l[16]; - } CHAR64LONG16; - CHAR64LONG16* block; + ulong a, b, c, d, e; + typedef union { + uchar c[64]; + ulong l[16]; + } CHAR64LONG16; + CHAR64LONG16* block; #ifdef SHA1HANDSOFF - static uchar workspace[64]; - block = (CHAR64LONG16*)workspace; - memcpy(block, buffer, 64); + static uchar workspace[64]; + block = (CHAR64LONG16*)workspace; + memcpy(block, buffer, 64); #else - block = (CHAR64LONG16*)buffer; + block = (CHAR64LONG16*)buffer; #endif - /* Copy context->state[] to working vars */ - a = state[0]; - b = state[1]; - c = state[2]; - d = state[3]; - e = state[4]; - /* 4 rounds of 20 operations each. Loop unrolled. */ - R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3); - R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7); - R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11); - R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15); - R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19); - R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23); - R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27); - R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31); - R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35); - R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39); - R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43); - R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47); - R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51); - R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55); - R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59); - R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63); - R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67); - R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71); - R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75); - R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79); - /* Add the working vars back into context.state[] */ - state[0] += a; - state[1] += b; - state[2] += c; - state[3] += d; - state[4] += e; - /* Wipe variables */ - a = b = c = d = e = 0; + /* Copy context->state[] to working vars */ + a = state[0]; + b = state[1]; + c = state[2]; + d = state[3]; + e = state[4]; + /* 4 rounds of 20 operations each. Loop unrolled. */ + R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3); + R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7); + R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11); + R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15); + R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19); + R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23); + R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27); + R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31); + R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35); + R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39); + R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43); + R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47); + R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51); + R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55); + R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59); + R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63); + R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67); + R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71); + R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75); + R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79); + /* Add the working vars back into context.state[] */ + state[0] += a; + state[1] += b; + state[2] += c; + state[3] += d; + state[4] += e; + /* Wipe variables */ + a = b = c = d = e = 0; } @@ -97,13 +97,13 @@ void SHA1Transform(ulong state[5], const uchar buffer[64]) void sha1_init(sha1_ctx_t* context) { - /* SHA1 initialization constants */ - context->state[0] = 0x67452301; - context->state[1] = 0xEFCDAB89; - context->state[2] = 0x98BADCFE; - context->state[3] = 0x10325476; - context->state[4] = 0xC3D2E1F0; - context->count[0] = context->count[1] = 0; + /* SHA1 initialization constants */ + context->state[0] = 0x67452301; + context->state[1] = 0xEFCDAB89; + context->state[2] = 0x98BADCFE; + context->state[3] = 0x10325476; + context->state[4] = 0xC3D2E1F0; + context->count[0] = context->count[1] = 0; } @@ -111,22 +111,22 @@ void sha1_init(sha1_ctx_t* context) void sha1_update(sha1_ctx_t* context, const void* d, unsigned int len) { - unsigned int i, j; - const uchar* data = (uchar*)d; - - j = (context->count[0] >> 3) & 63; - if ((context->count[0] += len << 3) < (len << 3)) context->count[1]++; - context->count[1] += (len >> 29); - if ((j + len) > 63) { - memcpy(&context->buffer[j], data, (i = 64-j)); - SHA1Transform(context->state, context->buffer); - for ( ; i + 63 < len; i += 64) { - SHA1Transform(context->state, &data[i]); + unsigned int i, j; + const uchar* data = (uchar*)d; + + j = (context->count[0] >> 3) & 63; + if ((context->count[0] += len << 3) < (len << 3)) context->count[1]++; + context->count[1] += (len >> 29); + if ((j + len) > 63) { + memcpy(&context->buffer[j], data, (i = 64-j)); + SHA1Transform(context->state, context->buffer); + for ( ; i + 63 < len; i += 64) { + SHA1Transform(context->state, &data[i]); + } + j = 0; } - j = 0; - } - else i = 0; - memcpy(&context->buffer[j], &data[i], len - i); + else i = 0; + memcpy(&context->buffer[j], &data[i], len - i); } @@ -134,30 +134,30 @@ void sha1_update(sha1_ctx_t* context, const void* d, unsigned int len) void sha1_final(uchar digest[20], sha1_ctx_t* context) { - ulong i; - uchar finalcount[8]; - - for (i = 0; i < 8; i++) { - finalcount[i] = (uchar)((context->count[(i >= 4 ? 0 : 1)] - >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */ - } - sha1_update(context, (uchar *)"\200", 1); - while ((context->count[0] & 504) != 448) { - sha1_update(context, (uchar *)"\0", 1); - } - sha1_update(context, finalcount, 8); /* Should cause a SHA1Transform() */ - for (i = 0; i < 20; i++) { - digest[i] = (uchar) - ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255); - } - /* Wipe variables */ - i = 0; - memset(context->buffer, 0, 64); - memset(context->state, 0, 20); - memset(context->count, 0, 8); - memset(&finalcount, 0, 8); -#ifdef SHA1HANDSOFF /* make SHA1Transform overwrite it's own static vars */ - SHA1Transform(context->state, context->buffer); + ulong i; + uchar finalcount[8]; + + for (i = 0; i < 8; i++) { + finalcount[i] = (uchar)((context->count[(i >= 4 ? 0 : 1)] + >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */ + } + sha1_update(context, (uchar *)"\200", 1); + while ((context->count[0] & 504) != 448) { + sha1_update(context, (uchar *)"\0", 1); + } + sha1_update(context, finalcount, 8); /* Should cause a SHA1Transform() */ + for (i = 0; i < 20; i++) { + digest[i] = (uchar) + ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255); + } + /* Wipe variables */ + i = 0; + memset(context->buffer, 0, 64); + memset(context->state, 0, 20); + memset(context->count, 0, 8); + memset(&finalcount, 0, 8); +#ifdef SHA1HANDSOFF /* make SHA1Transform overwrite it's own static vars */ + SHA1Transform(context->state, context->buffer); #endif } |