4 #ifdef PHP_HTTP_HAVE_HASH
8 #include <ext/standard/crc32.h>
9 #include <ext/standard/sha1.h>
10 #include <ext/standard/md5.h>
12 PHP_HTTP_API php_http_etag_t
*php_http_etag_init(const char *mode TSRMLS_DC
)
17 #ifdef PHP_HTTP_HAVE_HASH
18 const php_hash_ops
*eho
= NULL
;
20 if (mode
&& (eho
= php_hash_fetch_ops(mode
, strlen(mode
)))) {
21 ctx
= emalloc(eho
->context_size
);
25 if (mode
&& ((!strcasecmp(mode
, "crc32")) || (!strcasecmp(mode
, "crc32b")))) {
26 ctx
= emalloc(sizeof(uint
));
28 } else if (mode
&& !strcasecmp(mode
, "sha1")) {
29 PHP_SHA1Init(ctx
= emalloc(sizeof(PHP_SHA1_CTX
)));
30 } else if (mode
&& !strcasecmp(mode
, "md5")) {
31 PHP_MD5Init(ctx
= emalloc(sizeof(PHP_MD5_CTX
)));
36 e
= emalloc(sizeof(*e
));
38 e
->mode
= estrdup(mode
);
39 TSRMLS_SET_CTX(e
->ts
);
44 PHP_HTTP_API
char *php_http_etag_finish(php_http_etag_t
*e
)
46 unsigned char digest
[128] = {0};
49 #ifdef PHP_HTTP_HAVE_HASH
50 const php_hash_ops
*eho
= NULL
;
52 if (e
->mode
&& (eho
= php_hash_fetch_ops(e
->mode
, strlen(e
->mode
)))) {
53 eho
->hash_final(digest
, e
->ctx
);
54 etag
= php_http_etag_digest(digest
, eho
->digest_size
);
57 if (((!strcasecmp(e
->mode
, "crc32")) || (!strcasecmp(e
->mode
, "crc32b")))) {
58 *((uint
*) e
->ctx
) = ~*((uint
*) e
->ctx
);
59 etag
= php_http_etag_digest((const unsigned char *) e
->ctx
, sizeof(uint
));
60 } else if ((!strcasecmp(e
->mode
, "sha1"))) {
61 PHP_SHA1Final(digest
, e
->ctx
);
62 etag
= php_http_etag_digest(digest
, 20);
64 PHP_MD5Final(digest
, e
->ctx
);
65 etag
= php_http_etag_digest(digest
, 16);
74 PHP_HTTP_API
size_t php_http_etag_update(php_http_etag_t
*e
, const char *data_ptr
, size_t data_len
)
76 #ifdef PHP_HTTP_HAVE_HASH
77 const php_hash_ops
*eho
= NULL
;
79 if (e
->mode
&& (eho
= php_hash_fetch_ops(e
->mode
, strlen(e
->mode
)))) {
80 eho
->hash_update(e
->ctx
, (const unsigned char *) data_ptr
, data_len
);
83 if (((!strcasecmp(e
->mode
, "crc32")) || (!strcasecmp(e
->mode
, "crc32b")))) {
84 uint i
, c
= *((uint
*) e
->ctx
);
85 for (i
= 0; i
< data_len
; ++i
) {
86 CRC32(c
, data_ptr
[i
]);
88 *((uint
*) e
->ctx
) = c
;
89 } else if ((!strcasecmp(e
->mode
, "sha1"))) {
90 PHP_SHA1Update(e
->ctx
, (const unsigned char *) data_ptr
, data_len
);
92 PHP_MD5Update(e
->ctx
, (const unsigned char *) data_ptr
, data_len
);