2 +--------------------------------------------------------------------+
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 +--------------------------------------------------------------------+
13 #include "php_http_api.h"
15 #ifdef PHP_HTTP_HAVE_HASH
16 # include "php_hash.h"
19 #include <ext/standard/crc32.h>
20 #include <ext/standard/sha1.h>
21 #include <ext/standard/md5.h>
23 PHP_HTTP_API php_http_etag_t
*php_http_etag_init(const char *mode TSRMLS_DC
)
28 if (mode
&& (!strcasecmp(mode
, "crc32b"))) {
29 ctx
= emalloc(sizeof(uint
));
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
)));
36 #ifdef PHP_HTTP_HAVE_HASH
37 const php_hash_ops
*eho
= NULL
;
39 if (mode
&& (eho
= php_hash_fetch_ops(mode
, strlen(mode
)))) {
40 ctx
= emalloc(eho
->context_size
);
47 e
= emalloc(sizeof(*e
));
49 e
->mode
= estrdup(mode
);
50 TSRMLS_SET_CTX(e
->ts
);
55 PHP_HTTP_API
char *php_http_etag_finish(php_http_etag_t
*e
)
57 unsigned char digest
[128] = {0};
60 if (!strcasecmp(e
->mode
, "crc32b")) {
63 *((uint
*) e
->ctx
) = ~*((uint
*) e
->ctx
);
64 buf
[0] = ((unsigned char *) e
->ctx
)[3];
65 buf
[1] = ((unsigned char *) e
->ctx
)[2];
66 buf
[2] = ((unsigned char *) e
->ctx
)[1];
67 buf
[3] = ((unsigned char *) e
->ctx
)[0];
68 etag
= php_http_etag_digest(buf
, 4);
69 } else if ((!strcasecmp(e
->mode
, "sha1"))) {
70 PHP_SHA1Final(digest
, e
->ctx
);
71 etag
= php_http_etag_digest(digest
, 20);
72 } else if ((!strcasecmp(e
->mode
, "md5"))) {
73 PHP_MD5Final(digest
, e
->ctx
);
74 etag
= php_http_etag_digest(digest
, 16);
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_final(digest
, e
->ctx
);
81 etag
= php_http_etag_digest(digest
, eho
->digest_size
);
93 PHP_HTTP_API
size_t php_http_etag_update(php_http_etag_t
*e
, const char *data_ptr
, size_t data_len
)
95 if (!strcasecmp(e
->mode
, "crc32b")) {
96 uint i
, c
= *((uint
*) e
->ctx
);
97 for (i
= 0; i
< data_len
; ++i
) {
98 CRC32(c
, data_ptr
[i
]);
100 *((uint
*) e
->ctx
) = c
;
101 } else if ((!strcasecmp(e
->mode
, "sha1"))) {
102 PHP_SHA1Update(e
->ctx
, (const unsigned char *) data_ptr
, data_len
);
103 } else if ((!strcasecmp(e
->mode
, "md5"))) {
104 PHP_MD5Update(e
->ctx
, (const unsigned char *) data_ptr
, data_len
);
106 #ifdef PHP_HTTP_HAVE_HASH
107 const php_hash_ops
*eho
= NULL
;
109 if (e
->mode
&& (eho
= php_hash_fetch_ops(e
->mode
, strlen(e
->mode
)))) {
110 eho
->hash_update(e
->ctx
, (const unsigned char *) data_ptr
, data_len
);
124 * vim600: noet sw=4 ts=4 fdm=marker
125 * vim<600: noet sw=4 ts=4