- use const php_hash_ops*
[m6w6/ext-http] / php_http_cache_api.h
index be73fa6aea310173cdc431441b7a290c701560ff..518a5f80f534e0f2d750bc0bb176943f516c2b2e 100644 (file)
@@ -1,16 +1,13 @@
 /*
-   +----------------------------------------------------------------------+
-   | PECL :: http                                                         |
-   +----------------------------------------------------------------------+
-   | This source file is subject to version 3.0 of the PHP license, that  |
-   | is bundled with this package in the file LICENSE, and is available   |
-   | through the world-wide-web at http://www.php.net/license/3_0.txt.    |
-   | If you did not receive a copy of the PHP license and are unable to   |
-   | obtain it through the world-wide-web, please send a note to          |
-   | license@php.net so we can mail you a copy immediately.               |
-   +----------------------------------------------------------------------+
-   | Copyright (c) 2004-2005 Michael Wallner <mike@php.net>               |
-   +----------------------------------------------------------------------+
+    +--------------------------------------------------------------------+
+    | PECL :: http                                                       |
+    +--------------------------------------------------------------------+
+    | Redistribution and use in source and binary forms, with or without |
+    | modification, are permitted provided that the conditions mentioned |
+    | in the accompanying LICENSE file are met.                          |
+    +--------------------------------------------------------------------+
+    | Copyright (c) 2004-2006, Michael Wallner <mike@php.net>            |
+    +--------------------------------------------------------------------+
 */
 
 /* $Id$ */
 #ifndef PHP_HTTP_CACHE_API_H
 #define PHP_HTTP_CACHE_API_H
 
-#include "zend_ini.h"
-
-#include "ext/standard/md5.h"
-#include "ext/standard/sha1.h"
-#include "ext/standard/crc32.h"
-
-#include "php_http_std_defs.h"
-#include "php_http.h"
-#include "php_http_api.h"
 #include "php_http_send_api.h"
 
-#ifdef HTTP_HAVE_MHASH
-#      include <mhash.h>
-#endif
-
-ZEND_EXTERN_MODULE_GLOBALS(http);
-
-#define http_cache_global_init() _http_cache_global_init(INIT_FUNC_ARGS_PASSTHRU)
-extern STATUS _http_cache_global_init(INIT_FUNC_ARGS);
-
-typedef enum {
-       HTTP_ETAG_CRC32 = -3,
-       HTTP_ETAG_MD5 = -2,
-       HTTP_ETAG_SHA1 = -1,
-} http_etag_mode;
+#include "ext/standard/crc32.h"
+#include "ext/standard/sha1.h"
+#include "ext/standard/md5.h"
 
-#ifdef HTTP_HAVE_MHASH
-static void *http_etag_alloc_mhash_digest(size_t size)
-{
-       return emalloc(size);
-}
+#ifdef HTTP_HAVE_HASH
+#      include "php_hash.h"
 #endif
 
-#define http_etag_digest(d, l) _http_etag_digest((d), (l) TSRMLS_CC)
-static inline char *_http_etag_digest(const unsigned char *digest, int len TSRMLS_DC)
+#define http_etag_digest(d, l) _http_etag_digest((d), (l))
+static inline char *_http_etag_digest(const unsigned char *digest, int len)
 {
+       static const char hexdigits[17] = "0123456789abcdef";
        int i;
        char *hex = emalloc(len * 2 + 1);
        char *ptr = hex;
        
-       /* optimize this --
-               look at apache's make_etag */
        for (i = 0; i < len; ++i) {
-               sprintf(ptr, "%02x", digest[i]);
-               ptr += 2;
+               *ptr++ = hexdigits[digest[i] >> 4];
+               *ptr++ = hexdigits[digest[i] & 0xF];
        }
        *ptr = '\0';
        
@@ -73,122 +46,53 @@ static inline char *_http_etag_digest(const unsigned char *digest, int len TSRML
 static inline void *_http_etag_init(TSRMLS_D)
 {
        void *ctx = NULL;
-       long mode = HTTP_G(etag).mode;
+       char *mode = HTTP_G->etag.mode;
        
-       switch (mode)
-       {
-               case HTTP_ETAG_CRC32:
-                       ctx = emalloc(sizeof(unsigned int));
-                       memset(ctx, 1, sizeof(unsigned int));
-               break;
-               
-               case HTTP_ETAG_SHA1:
-                       PHP_SHA1Init(ctx = emalloc(sizeof(PHP_SHA1_CTX)));
-               break;
-               
-               case HTTP_ETAG_MD5:
-#ifndef HTTP_HAVE_MHASH
-               default:
-#endif
-                       PHP_MD5Init(ctx = emalloc(sizeof(PHP_MD5_CTX)));
-               break;
-               
-#ifdef HTTP_HAVE_MHASH
-               default:
-                       if ((mode < 0) || ((ulong)mode > mhash_count()) || (!(ctx = mhash_init(mode)))) {
-                               http_error_ex(HE_ERROR, HTTP_E_RUNTIME, "Invalid ETag mode: %ld", mode);
-                       }
-               break;
+#ifdef HTTP_HAVE_HASH
+       const php_hash_ops *eho = NULL;
+       
+       if (mode && (eho = php_hash_fetch_ops(mode, strlen(mode)))) {
+               ctx = emalloc(eho->context_size);
+               eho->hash_init(ctx);
+       } else
 #endif
+       if (mode && ((!strcasecmp(mode, "crc32")) || (!strcasecmp(mode, "crc32b")))) {
+               ctx = emalloc(sizeof(uint));
+               *((uint *) ctx) = ~0;
+       } else if (mode && !strcasecmp(mode, "sha1")) {
+               PHP_SHA1Init(ctx = emalloc(sizeof(PHP_SHA1_CTX)));
+       } else {
+               PHP_MD5Init(ctx = emalloc(sizeof(PHP_MD5_CTX)));
        }
        
        return ctx;
 }
 
-#define http_etag_free(cp) _http_etag_free((cp) TSRMLS_CC)
-static inline void _http_etag_free(void **ctx_ptr TSRMLS_DC)
-{
-       long mode = HTTP_G(etag).mode;
-       
-       switch (mode)
-       {
-               case HTTP_ETAG_CRC32:
-                       if (*((unsigned int **) ctx_ptr)) {
-                               efree(*((unsigned int **) ctx_ptr));
-                               *((unsigned int **) ctx_ptr) = NULL;
-                       }
-               break;
-               
-               case HTTP_ETAG_SHA1:
-                       if (*((PHP_SHA1_CTX **) ctx_ptr)) {
-                               efree(*((PHP_SHA1_CTX **) ctx_ptr));
-                               *((PHP_SHA1_CTX **) ctx_ptr) = NULL;
-                       }
-               break;
-               
-               case HTTP_ETAG_MD5:
-#ifndef HTTP_HAVE_MHASH
-               default:
-#endif
-                       if (*((PHP_MD5_CTX **) ctx_ptr)) {
-                               efree(*((PHP_MD5_CTX **) ctx_ptr));
-                               *((PHP_MD5_CTX **) ctx_ptr) = NULL;
-                       }
-               break;
-               
-#ifdef HTTP_HAVE_MHASH
-               default:
-                       /* mhash gets already freed in http_etag_finish() */
-                       if (*((MHASH *) ctx_ptr)) {
-                               mhash_deinit(*((MHASH *) ctx_ptr), NULL);
-                               *((MHASH *) ctx_ptr) = NULL;
-                       }
-               break;
-#endif
-       }
-}
-
 #define http_etag_finish(c) _http_etag_finish((c) TSRMLS_CC)
-static inline char *_http_etag_finish(void **ctx_ptr TSRMLS_DC)
+static inline char *_http_etag_finish(void *ctx TSRMLS_DC)
 {
-       char *etag = NULL;
-       unsigned char digest[20];
-       long mode = HTTP_G(etag).mode;
+       unsigned char digest[128] = {0};
+       char *etag = NULL, *mode = HTTP_G->etag.mode;
        
-       switch (mode)
-       {
-               case HTTP_ETAG_CRC32:
-                       **((unsigned int **) ctx_ptr) = ~**((unsigned int **) ctx_ptr);
-                       etag = http_etag_digest(*((const unsigned char **) ctx_ptr), sizeof(unsigned int));
-               break;
-               
-               case HTTP_ETAG_SHA1:
-                       PHP_SHA1Final(digest, *((PHP_SHA1_CTX **) ctx_ptr));
-                       etag = http_etag_digest(digest, 20);
-               break;
-               
-               case HTTP_ETAG_MD5:
-#ifndef HTTP_HAVE_MHASH
-               default:
-#endif
-                       PHP_MD5Final(digest, *((PHP_MD5_CTX **) ctx_ptr));
-                       etag = http_etag_digest(digest, 16);
-               break;
-               
-#ifdef HTTP_HAVE_MHASH
-               default:
-               {
-                       unsigned char *mhash_digest = mhash_end_m(*((MHASH *) ctx_ptr), http_etag_alloc_mhash_digest);
-                       etag = http_etag_digest(mhash_digest, mhash_get_block_size(mode));
-                       efree(mhash_digest);
-                       /* avoid double free */
-                       *((MHASH *) ctx_ptr) = NULL;
-               }
-               break;
+#ifdef HTTP_HAVE_HASH
+       const php_hash_ops *eho = NULL;
+       
+       if (mode && (eho = php_hash_fetch_ops(mode, strlen(mode)))) {
+               eho->hash_final(digest, ctx);
+               etag = http_etag_digest(digest, eho->digest_size);
+       } else
 #endif
+       if (mode && ((!strcasecmp(mode, "crc32")) || (!strcasecmp(mode, "crc32b")))) {
+               *((uint *) ctx) = ~*((uint *) ctx);
+               etag = http_etag_digest((const unsigned char *) ctx, sizeof(uint));
+       } else if (mode && (!strcasecmp(mode, "sha1"))) {
+               PHP_SHA1Final(digest, ctx);
+               etag = http_etag_digest(digest, 20);
+       } else {
+               PHP_MD5Final(digest, ctx);
+               etag = http_etag_digest(digest, 16);
        }
-       
-       http_etag_free(ctx_ptr);
+       efree(ctx);
        
        return etag;
 }
@@ -196,35 +100,24 @@ static inline char *_http_etag_finish(void **ctx_ptr TSRMLS_DC)
 #define http_etag_update(c, d, l) _http_etag_update((c), (d), (l) TSRMLS_CC)
 static inline void _http_etag_update(void *ctx, const char *data_ptr, size_t data_len TSRMLS_DC)
 {
-       switch (INI_INT("http.etag_mode"))
-       {
-               case HTTP_ETAG_CRC32:
-               {
-                       unsigned int i, c = *((unsigned int *) ctx);
-                       
-                       for (i = 0; i < data_len; ++i) {
-                               c = CRC32(c, data_ptr[i]);
-                       }
-                       *((unsigned int *)ctx) = c;
-               }
-               break;
-               
-               case HTTP_ETAG_SHA1:
-                       PHP_SHA1Update(ctx, (const unsigned char *) data_ptr, data_len);
-               break;
-               
-               case HTTP_ETAG_MD5:
-#ifndef HTTP_HAVE_MHASH
-               default:
-#endif
-                       PHP_MD5Update(ctx, (const unsigned char *) data_ptr, data_len);
-               break;
-               
-#ifdef HTTP_HAVE_MHASH
-               default:
-                       mhash(ctx, data_ptr, data_len);
-               break;
+       char *mode = HTTP_G->etag.mode;
+#ifdef HTTP_HAVE_HASH
+       const php_hash_ops *eho = NULL;
+       
+       if (mode && (eho = php_hash_fetch_ops(mode, strlen(mode)))) {
+               eho->hash_update(ctx, (const unsigned char *) data_ptr, data_len);
+       } else
 #endif
+       if (mode && ((!strcasecmp(mode, "crc32")) || (!strcasecmp(mode, "crc32b")))) {
+               uint i, c = *((uint *) ctx);
+               for (i = 0; i < data_len; ++i) {
+                       CRC32(c, data_ptr[i]);
+               }
+               *((uint *)ctx) = c;
+       } else if (mode && (!strcasecmp(mode, "sha1"))) {
+               PHP_SHA1Update(ctx, (const unsigned char *) data_ptr, data_len);
+       } else {
+               PHP_MD5Update(ctx, (const unsigned char *) data_ptr, data_len);
        }
 }