summaryrefslogtreecommitdiff
path: root/ext/mhash/mhash.c
blob: 6d031a694fcf133ec0e84dbb0e1b4e74387971c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
   +----------------------------------------------------------------------+
   | PHP HTML Embedded Scripting Language Version 4.0                     |
   +----------------------------------------------------------------------+
   | Copyright (c) 1999 PHP Development Team (See Credits file)           |
   +----------------------------------------------------------------------+
   | This program is free software; you can redistribute it and/or modify |
   | it under the terms of one of the following licenses:                 |
   |                                                                      |
   |  A) the GNU General Public License as published by the Free Software |
   |     Foundation; either version 2 of the License, or (at your option) |
   |     any later version.                                               |
   |                                                                      |
   |  B) the PHP License as published by the PHP Development Team and     |
   |     included in the distribution in the file: LICENSE                |
   |                                                                      |
   | This program is distributed in the hope that it will be useful,      |
   | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
   | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        |
   | GNU General Public License for more details.                         |
   |                                                                      |
   | You should have received a copy of both licenses referred to here.   |
   | If you did not, or have any questions about PHP licensing, please    |
   | contact core@php.net.                                                |
   +----------------------------------------------------------------------+
   | Authors: Sascha Schumann <sascha@schumann.2ns.de>                    |
   +----------------------------------------------------------------------+
 */

#include "php.h"

#if HAVE_LIBMHASH

#include "php_mhash.h"

#include "mhash.h"

function_entry mhash_functions[] = {
	PHP_FE(mhash_get_block_size, NULL)
	PHP_FE(mhash, NULL)
	{0},
};

static int php_minit_mhash(INIT_FUNC_ARGS);

zend_module_entry mhash_module_entry = {
	"mhash", 
	mhash_functions,
	php_minit_mhash, NULL,
	NULL, NULL,
	NULL,
	STANDARD_MODULE_PROPERTIES,
};

#define MHASH_FAILED "mhash initialization failed"

#define MHASH_ENTRY(a) REGISTER_LONG_CONSTANT("MHASH_" #a, a, 0)

static int php_minit_mhash(INIT_FUNC_ARGS)
{
	/* hashes */
	MHASH_ENTRY(CRC32);
	MHASH_ENTRY(MD5);
	MHASH_ENTRY(SHA1);
	MHASH_ENTRY(HAVAL);
	MHASH_ENTRY(RIPEMD128);
	MHASH_ENTRY(RIPEMD160);
	MHASH_ENTRY(TIGER);
	MHASH_ENTRY(SNEFRU);
	MHASH_ENTRY(GOST);
	
	return SUCCESS;
}

/* proto mhash_get_block_size(int hash)
   get the block size of hash */
PHP_FUNCTION(mhash_get_block_size)
{
	pval *hash;

	if(ARG_COUNT(ht) != 1 || getParameters(ht, 1, &hash) == FAILURE) {
		WRONG_PARAM_COUNT;
	}

	convert_to_long(hash);

	RETURN_LONG(mhash_get_block_size(hash->value.lval));
}

/* proto mhash(int hash, string data)
   hash data with hash */
PHP_FUNCTION(mhash)
{
	pval *hash, *data;
	int td;
	int bsize;
	unsigned char *hash_data;
	int i;

	if(ARG_COUNT(ht) != 2 || getParameters(ht, 2, &hash, &data) == FAILURE) {
		WRONG_PARAM_COUNT;
	}

	convert_to_long(hash);
	convert_to_string(data);

	bsize = mhash_get_block_size(hash->value.lval);
	td = init_mhash(hash->value.lval);
	if(td == -1) {
		php3_error(E_WARNING, MHASH_FAILED);
		RETURN_FALSE;
	}

	mhash(td, data->value.str.val, data->value.str.len);

	hash_data = (char *) end_mhash(td);
	
	RETVAL_STRINGL(hash_data, bsize, 1);
	
	free(hash_data);
}

#endif