- use const php_hash_ops*
[m6w6/ext-http] / php_http_cache_api.h
index bef8cc8b97d14cdf94548db85c82868e261e3ddb..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 "php_http_std_defs.h"
-#include "php_http.h"
-#include "php_http_api.h"
 #include "php_http_send_api.h"
 
-#ifdef HAVE_LIBMHASH
-#      include <mhash.h>
-#endif
-
-ZEND_EXTERN_MODULE_GLOBALS(http);
-
-typedef enum {
-       HTTP_ETAG_MD5 = -2,
-       HTTP_ETAG_SHA1 = -1,
-       HTTP_ETAG_MHASH = 0,
-} http_etag_mode;
+#include "ext/standard/crc32.h"
+#include "ext/standard/sha1.h"
+#include "ext/standard/md5.h"
 
-#ifdef HAVE_LIBMHASH
-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';
        
@@ -68,34 +45,24 @@ static inline char *_http_etag_digest(const unsigned char *digest, int len TSRML
 #define http_etag_init() _http_etag_init(TSRMLS_C)
 static inline void *_http_etag_init(TSRMLS_D)
 {
-       void *ctx;
-       long mode = INI_INT("http.etag_mode");
+       void *ctx = NULL;
+       char *mode = HTTP_G->etag.mode;
+       
+#ifdef HTTP_HAVE_HASH
+       const php_hash_ops *eho = NULL;
        
-       switch (mode)
-       {
-               case HTTP_ETAG_SHA1:
-                       PHP_SHA1Init(ctx = emalloc(sizeof(PHP_SHA1_CTX)));
-               break;
-               
-               case HTTP_ETAG_MD5:
-invalid_flag:
-                       PHP_MD5Init(ctx = emalloc(sizeof(PHP_MD5_CTX)));
-               break;
-               
-               default:
-               {
-#ifdef HAVE_LIBMHASH
-                       if ((mode >= 0) && (mode <= mhash_count())) {
-                               ctx = mhash_init(mode);
-                       }
-                       if ((!ctx) || (ctx == MHASH_FAILED))
+       if (mode && (eho = php_hash_fetch_ops(mode, strlen(mode)))) {
+               ctx = emalloc(eho->context_size);
+               eho->hash_init(ctx);
+       } else
 #endif
-                       {
-                               HTTP_G(etag).mode = HTTP_ETAG_MD5;
-                               goto invalid_flag;
-                       }
-               }
-               break;
+       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;
@@ -104,34 +71,28 @@ invalid_flag:
 #define http_etag_finish(c) _http_etag_finish((c) TSRMLS_CC)
 static inline char *_http_etag_finish(void *ctx TSRMLS_DC)
 {
-       char *etag = NULL;
-       unsigned char digest[20];
-       long mode = INI_INT("http.etag_mode");
+       unsigned char digest[128] = {0};
+       char *etag = NULL, *mode = HTTP_G->etag.mode;
        
-       switch (mode)
-       {
-               case HTTP_ETAG_SHA1:
-                       PHP_SHA1Final(digest, ctx);
-                       etag = http_etag_digest(digest, 20);
-                       efree(ctx);
-               break;
-               
-               case HTTP_ETAG_MD5:
-                       PHP_MD5Final(digest, ctx);
-                       etag = http_etag_digest(digest, 16);
-                       efree(ctx);
-               break;
-               
-               default:
-               {
-#ifdef HAVE_LIBMHASH
-                       unsigned char *mhash_digest = mhash_end_m(ctx, http_etag_alloc_mhash_digest);
-                       etag = http_etag_digest(mhash_digest, mhash_get_block_size(mode));
-                       efree(mhash_digest);
+#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
-               }
-               break;
+       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);
        }
+       efree(ctx);
        
        return etag;
 }
@@ -139,24 +100,30 @@ static inline char *_http_etag_finish(void *ctx 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_SHA1:
-                       PHP_SHA1Update(ctx, data_ptr, data_len);
-               break;
-               
-               case HTTP_ETAG_MD5:
-                       PHP_MD5Update(ctx, data_ptr, data_len);
-               break;
-               
-               default:
-#ifdef HAVE_LIBMHASH
-                       mhash(ctx, data_ptr, data_len);
+       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
-               break;
+       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);
        }
 }
 
+#define http_ob_etaghandler(o, l, ho, hl, m) _http_ob_etaghandler((o), (l), (ho), (hl), (m) TSRMLS_CC)
+extern void _http_ob_etaghandler(char *output, uint output_len, char **handled_output, uint *handled_output_len, int mode TSRMLS_DC);
+
 #define http_etag(p, l, m) _http_etag((p), (l), (m) TSRMLS_CC)
 PHP_HTTP_API char *_http_etag(const void *data_ptr, size_t data_len, http_send_mode data_mode TSRMLS_DC);
 
@@ -177,8 +144,10 @@ PHP_HTTP_API STATUS _http_cache_last_modified(time_t last_modified, time_t send_
 #define http_cache_etag(e, el, cc, ccl) _http_cache_etag((e), (el), (cc), (ccl) TSRMLS_CC)
 PHP_HTTP_API STATUS _http_cache_etag(const char *etag, size_t etag_len, const char *cache_control, size_t cc_len TSRMLS_DC);
 
-#define http_ob_etaghandler(o, l, ho, hl, m) _http_ob_etaghandler((o), (l), (ho), (hl), (m) TSRMLS_CC)
-PHP_HTTP_API void _http_ob_etaghandler(char *output, uint output_len, char **handled_output, uint *handled_output_len, int mode TSRMLS_DC);
+#define http_start_ob_etaghandler() _http_start_ob_etaghandler(TSRMLS_C)
+PHP_HTTP_API STATUS _http_start_ob_etaghandler(TSRMLS_D);
+#define http_interrupt_ob_etaghandler() _http_interrupt_ob_etaghandler(TSRMLS_C)
+PHP_HTTP_API zend_bool _http_interrupt_ob_etaghandler(TSRMLS_D);
 
 #endif