From 9315bbfd0f9e51da5438d29681cab6f9a6533d89 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Sun, 28 Aug 2016 00:30:02 -0400 Subject: split up fhp.c --- fhp.c | 203 +----------------------------------------------------------------- 1 file changed, 1 insertion(+), 202 deletions(-) (limited to 'fhp.c') diff --git a/fhp.c b/fhp.c index fd1f746..917141d 100644 --- a/fhp.c +++ b/fhp.c @@ -175,190 +175,9 @@ case '\t': // -// hash functions (djb2) -// (see http://aras-p.info/blog/2016/08/02/Hash-Functions-all-the-way-down/) +// strings // -uint32_t -fhp_hash_init(void) { - return 5381; -} - -uint32_t -fhp_hash_push(uint32_t hash, uint8_t * const buf, size_t len) { - for (size_t i = 0; i < len; i++) - hash = ((hash << 5) + hash) + buf[len]; - - return hash; -} - -uint32_t -fhp_hash_string(char * const str) { - uint32_t r = fhp_hash_init(); - return fhp_hash_push(r, (uint8_t*) str, strlen(str)); -} - -uint32_t -fhp_lc_hash_push( - uint32_t hash, - uint8_t * const buf, - size_t len -) { - for (size_t i = 0; i < len; i++) { - uint8_t c = buf[len]; - - if (c >= 'A' && c <= 'Z') - c = (c - 'A') + 'a'; - - hash = ((hash << 5) + hash) + c; - } - - return hash; -} - -uint32_t -fhp_lc_hash_string(char * const str) { - uint32_t r = fhp_hash_init(); - return fhp_lc_hash_push(r, (uint8_t*) str, strlen(str)); -} - -// -// error functions -// - -static const char * -fhp_errors[] = { - "OK", - "callback error", - "bad state", - "invalid character", - "invalid character in HTTP method", - "invalid character in HTTP URL", - "invalid percent-encoded character in HTTP URL", - "invalid character in HTTP version", - "invalid character after carriage return", - "invalid character in HTTP header name", - "invalid error code", - "buffer too small", - "invalid body type", - "too many transfer encodings", - "invalid character in transfer encoding name", - "invalid character in transfer encoding", - "bad transfer encoding state", - "transfer encoding parser already done", -}; - -fhp_err_t -fhp_strerror( - fhp_err_t err, - char * const buf, - size_t len -) { - // check error code - if (err >= FHP_ERR_LAST) - return FHP_ERR_INVALID_ERROR; - - // check buffer size - size_t err_len = strlen(fhp_errors[err]) + 1; - if (len < err_len) - return FHP_ERR_BUFFER_TOO_SMALL; - - // copy string - memcpy(buf, fhp_errors[err], err_len); - - // return success - return FHP_OK; -} - -// -// token functions -// - -static const char * -fhp_tokens[] = { - "METHOD_START", - "METHOD_FRAGMENT", - "METHOD_END", - - "METHOD_GET", - "METHOD_POST", - "METHOD_HEAD", - "METHOD_PUT", - "METHOD_DELETE", - "METHOD_OPTIONS", - "METHOD_OTHER", - - "URL_START", - "URL_FRAGMENT", - "URL_END", - - "VERSION_START", - "VERSION_FRAGMENT", - "VERSION_END", - - "VERSION_HTTP_10", - "VERSION_HTTP_11", - "VERSION_OTHER", - - "HEADER_NAME_START", - "HEADER_NAME_FRAGMENT", - "HEADER_NAME_END", - - "HEADER_VALUE_START", - "HEADER_VALUE_FRAGMENT", - "HEADER_VALUE_END", - - "LAST" -}; - -fhp_err_t -fhp_strtoken( - fhp_token_t token, - char * const buf, - size_t len -) { - // check token code - if (token >= FHP_TOKEN_LAST) - return FHP_ERR_INVALID_ERROR; - - // check buffer size - size_t str_len = strlen(fhp_tokens[token]) + 1; - if (len < str_len) - return FHP_ERR_BUFFER_TOO_SMALL; - - // copy string - memcpy(buf, fhp_tokens[token], str_len); - - // return success - return FHP_OK; -} - -// -// string functions -// - -static char * const -fhp_strings[] = { - "GET", - "POST", - "HEAD", - "PUT", - "DELETE", - "OPTIONS", - "HTTP/1.0", - "HTTP/1.1", - "content-length", - "transfer-encoding", - "gzip", - "x-gzip", - "deflate", - "x-deflate", - "compress", - "x-compress", - "chunked", - NULL -}; - typedef enum { FHP_STR_GET, FHP_STR_POST, @@ -380,26 +199,6 @@ typedef enum { FHP_STR_LAST } fhp_str_t; -void -fhp_env_init(fhp_env_t * const env) { - for (size_t i = 0; i < FHP_STR_LAST; i++) - env->hashes[i] = fhp_lc_hash_string(fhp_strings[i]); -} - -static fhp_env_t fhp_default_env; - -fhp_env_t * -fhp_get_default_env(void) { - static fhp_env_t *r = NULL; - - if (!r) { - r = &fhp_default_env; - fhp_env_init(r); - } - - return r; -} - // // transfer encoding buffer functions // -- cgit v1.2.3