3 #include <ext/standard/crc32.h>
4 #include <ext/standard/sha1.h>
5 #include <ext/standard/md5.h>
7 PHP_HTTP_API
void *php_http_etag_init(TSRMLS_D
)
10 char *mode
= PHP_HTTP_G
->env
.etag_mode
;
12 #ifdef PHP_HTTP_HAVE_HASH
13 const php_hash_ops
*eho
= NULL
;
15 if (mode
&& (eho
= php_hash_fetch_ops(mode
, strlen(mode
)))) {
16 ctx
= emalloc(eho
->context_size
);
20 if (mode
&& ((!strcasecmp(mode
, "crc32")) || (!strcasecmp(mode
, "crc32b")))) {
21 ctx
= emalloc(sizeof(uint
));
23 } else if (mode
&& !strcasecmp(mode
, "sha1")) {
24 PHP_SHA1Init(ctx
= emalloc(sizeof(PHP_SHA1_CTX
)));
26 PHP_MD5Init(ctx
= emalloc(sizeof(PHP_MD5_CTX
)));
32 PHP_HTTP_API
char *php_http_etag_finish(void *ctx TSRMLS_DC
)
34 unsigned char digest
[128] = {0};
35 char *etag
= NULL
, *mode
= PHP_HTTP_G
->env
.etag_mode
;
37 #ifdef PHP_HTTP_HAVE_HASH
38 const php_hash_ops
*eho
= NULL
;
40 if (mode
&& (eho
= php_hash_fetch_ops(mode
, strlen(mode
)))) {
41 eho
->hash_final(digest
, ctx
);
42 etag
= php_http_etag_digest(digest
, eho
->digest_size
);
45 if (mode
&& ((!strcasecmp(mode
, "crc32")) || (!strcasecmp(mode
, "crc32b")))) {
46 *((uint
*) ctx
) = ~*((uint
*) ctx
);
47 etag
= php_http_etag_digest((const unsigned char *) ctx
, sizeof(uint
));
48 } else if (mode
&& (!strcasecmp(mode
, "sha1"))) {
49 PHP_SHA1Final(digest
, ctx
);
50 etag
= php_http_etag_digest(digest
, 20);
52 PHP_MD5Final(digest
, ctx
);
53 etag
= php_http_etag_digest(digest
, 16);
60 PHP_HTTP_API
size_t php_http_etag_update(void *ctx
, const char *data_ptr
, size_t data_len TSRMLS_DC
)
62 char *mode
= PHP_HTTP_G
->env
.etag_mode
;
63 #ifdef PHP_HTTP_HAVE_HASH
64 const php_hash_ops
*eho
= NULL
;
66 if (mode
&& (eho
= php_hash_fetch_ops(mode
, strlen(mode
)))) {
67 eho
->hash_update(ctx
, (const unsigned char *) data_ptr
, data_len
);
70 if (mode
&& ((!strcasecmp(mode
, "crc32")) || (!strcasecmp(mode
, "crc32b")))) {
71 uint i
, c
= *((uint
*) ctx
);
72 for (i
= 0; i
< data_len
; ++i
) {
73 CRC32(c
, data_ptr
[i
]);
76 } else if (mode
&& (!strcasecmp(mode
, "sha1"))) {
77 PHP_SHA1Update(ctx
, (const unsigned char *) data_ptr
, data_len
);
79 PHP_MD5Update(ctx
, (const unsigned char *) data_ptr
, data_len
);