build and file maintenance
[m6w6/ext-http] / php_http_etag.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2011, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 #include "php_http.h"
14
15 #if PHP_HTTP_HAVE_HASH
16 # include "php_hash.h"
17 #endif
18
19 #include <ext/standard/crc32.h>
20 #include <ext/standard/sha1.h>
21 #include <ext/standard/md5.h>
22
23 PHP_HTTP_API php_http_etag_t *php_http_etag_init(const char *mode TSRMLS_DC)
24 {
25 void *ctx;
26 php_http_etag_t *e;
27
28 #if PHP_HTTP_HAVE_HASH
29 const php_hash_ops *eho = NULL;
30
31 if (mode && (eho = php_hash_fetch_ops(mode, strlen(mode)))) {
32 ctx = emalloc(eho->context_size);
33 eho->hash_init(ctx);
34 } else
35 #endif
36 if (mode && ((!strcasecmp(mode, "crc32")) || (!strcasecmp(mode, "crc32b")))) {
37 ctx = emalloc(sizeof(uint));
38 *((uint *) ctx) = ~0;
39 } else if (mode && !strcasecmp(mode, "sha1")) {
40 PHP_SHA1Init(ctx = emalloc(sizeof(PHP_SHA1_CTX)));
41 } else if (mode && !strcasecmp(mode, "md5")) {
42 PHP_MD5Init(ctx = emalloc(sizeof(PHP_MD5_CTX)));
43 } else {
44 return NULL;
45 }
46
47 e = emalloc(sizeof(*e));
48 e->ctx = ctx;
49 e->mode = estrdup(mode);
50 TSRMLS_SET_CTX(e->ts);
51
52 return e;
53 }
54
55 PHP_HTTP_API char *php_http_etag_finish(php_http_etag_t *e)
56 {
57 unsigned char digest[128] = {0};
58 char *etag = NULL;
59
60 #if PHP_HTTP_HAVE_HASH
61 const php_hash_ops *eho = NULL;
62
63 if (e->mode && (eho = php_hash_fetch_ops(e->mode, strlen(e->mode)))) {
64 eho->hash_final(digest, e->ctx);
65 etag = php_http_etag_digest(digest, eho->digest_size);
66 } else
67 #endif
68 if (((!strcasecmp(e->mode, "crc32")) || (!strcasecmp(e->mode, "crc32b")))) {
69 *((uint *) e->ctx) = ~*((uint *) e->ctx);
70 etag = php_http_etag_digest((const unsigned char *) e->ctx, sizeof(uint));
71 } else if ((!strcasecmp(e->mode, "sha1"))) {
72 PHP_SHA1Final(digest, e->ctx);
73 etag = php_http_etag_digest(digest, 20);
74 } else {
75 PHP_MD5Final(digest, e->ctx);
76 etag = php_http_etag_digest(digest, 16);
77 }
78 efree(e->ctx);
79 efree(e->mode);
80 efree(e);
81
82 return etag;
83 }
84
85 PHP_HTTP_API size_t php_http_etag_update(php_http_etag_t *e, const char *data_ptr, size_t data_len)
86 {
87 #if PHP_HTTP_HAVE_HASH
88 const php_hash_ops *eho = NULL;
89
90 if (e->mode && (eho = php_hash_fetch_ops(e->mode, strlen(e->mode)))) {
91 eho->hash_update(e->ctx, (const unsigned char *) data_ptr, data_len);
92 } else
93 #endif
94 if (((!strcasecmp(e->mode, "crc32")) || (!strcasecmp(e->mode, "crc32b")))) {
95 uint i, c = *((uint *) e->ctx);
96 for (i = 0; i < data_len; ++i) {
97 CRC32(c, data_ptr[i]);
98 }
99 *((uint *) e->ctx) = c;
100 } else if ((!strcasecmp(e->mode, "sha1"))) {
101 PHP_SHA1Update(e->ctx, (const unsigned char *) data_ptr, data_len);
102 } else {
103 PHP_MD5Update(e->ctx, (const unsigned char *) data_ptr, data_len);
104 }
105
106 return data_len;
107 }
108
109
110 /*
111 * Local variables:
112 * tab-width: 4
113 * c-basic-offset: 4
114 * End:
115 * vim600: noet sw=4 ts=4 fdm=marker
116 * vim<600: noet sw=4 ts=4
117 */
118