048af87c8b63a60fd881a0cb319b3637f64f6549
[m6w6/ext-http] / src / 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-2014, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 #include "php_http_api.h"
14
15 #if PHP_HTTP_HAVE_HASH
16 # include "ext/hash/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_etag_t *php_http_etag_init(const char *mode)
24 {
25 void *ctx;
26 php_http_etag_t *e;
27
28 if (mode && (!strcasecmp(mode, "crc32b"))) {
29 ctx = emalloc(sizeof(uint32_t));
30 *((uint32_t *) ctx) = ~0;
31 } else if (mode && !strcasecmp(mode, "sha1")) {
32 PHP_SHA1Init(ctx = emalloc(sizeof(PHP_SHA1_CTX)));
33 } else if (mode && !strcasecmp(mode, "md5")) {
34 PHP_MD5Init(ctx = emalloc(sizeof(PHP_MD5_CTX)));
35 } else {
36 #if PHP_HTTP_HAVE_HASH
37 const php_hash_ops *eho = NULL;
38
39 if (mode && (eho = php_hash_fetch_ops(mode, strlen(mode)))) {
40 ctx = emalloc(eho->context_size);
41 eho->hash_init(ctx);
42 } else
43 #endif
44 return NULL;
45 }
46
47 e = emalloc(sizeof(*e));
48 e->ctx = ctx;
49 e->mode = estrdup(mode);
50
51 return e;
52 }
53
54 char *php_http_etag_finish(php_http_etag_t *e)
55 {
56 unsigned char digest[128] = {0};
57 char *etag = NULL;
58
59 if (!strcasecmp(e->mode, "crc32b")) {
60 uint32_t e_ctx;
61 memcpy(&e_ctx, e->ctx, 4);
62 e_ctx = ntohl(~e_ctx);
63 etag = php_http_etag_digest((unsigned char *) &e_ctx, 4);
64 } else if ((!strcasecmp(e->mode, "sha1"))) {
65 PHP_SHA1Final(digest, e->ctx);
66 etag = php_http_etag_digest(digest, 20);
67 } else if ((!strcasecmp(e->mode, "md5"))) {
68 PHP_MD5Final(digest, e->ctx);
69 etag = php_http_etag_digest(digest, 16);
70 } else {
71 #if PHP_HTTP_HAVE_HASH
72 const php_hash_ops *eho = NULL;
73
74 if ((eho = php_hash_fetch_ops(e->mode, strlen(e->mode)))) {
75 eho->hash_final(digest, e->ctx);
76 etag = php_http_etag_digest(digest, eho->digest_size);
77 }
78 #endif
79 }
80
81 efree(e->ctx);
82 efree(e->mode);
83 efree(e);
84
85 return etag;
86 }
87
88 size_t php_http_etag_update(php_http_etag_t *e, const char *data_ptr, size_t data_len)
89 {
90 if (!strcasecmp(e->mode, "crc32b")) {
91 uint32_t i, c = *((uint32_t *) e->ctx);
92 for (i = 0; i < data_len; ++i) {
93 CRC32(c, data_ptr[i]);
94 }
95 *((uint32_t *) e->ctx) = c;
96 } else if ((!strcasecmp(e->mode, "sha1"))) {
97 PHP_SHA1Update(e->ctx, (const unsigned char *) data_ptr, data_len);
98 } else if ((!strcasecmp(e->mode, "md5"))) {
99 PHP_MD5Update(e->ctx, (const unsigned char *) data_ptr, data_len);
100 } else {
101 #if PHP_HTTP_HAVE_HASH
102 const php_hash_ops *eho = NULL;
103
104 if ((eho = php_hash_fetch_ops(e->mode, strlen(e->mode)))) {
105 eho->hash_update(e->ctx, (const unsigned char *) data_ptr, data_len);
106 }
107 #endif
108 }
109
110 return data_len;
111 }
112
113
114 /*
115 * Local variables:
116 * tab-width: 4
117 * c-basic-offset: 4
118 * End:
119 * vim600: noet sw=4 ts=4 fdm=marker
120 * vim<600: noet sw=4 ts=4
121 */
122