/
secure_int64.c
150 lines (119 loc) · 3.98 KB
/
secure_int64.c
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// include the PHP API itself
#include <php.h>
// then include the header of your extension
#include "secure_int64.h"
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <inttypes.h>
#include <linux/types.h>
// register our function to the PHP API
// so that PHP knows, which functions are in this module
zend_function_entry secure_int64_functions[] = {
PHP_FE(sint64_array_to_string, NULL)
PHP_FE(sint64_string_to_array, NULL)
PHP_FE(uint64_array_to_string, NULL)
PHP_FE(uint64_string_to_array, NULL)
{NULL, NULL, NULL}
};
// some pieces of information about our module
zend_module_entry secure_int64_module_entry = {
STANDARD_MODULE_HEADER,
secure_int64_EXTNAME,
secure_int64_functions,
NULL,
NULL,
NULL,
NULL,
NULL,
secure_int64_VERSION,
STANDARD_MODULE_PROPERTIES
};
ZEND_GET_MODULE(secure_int64)
unsigned long long _parseBytes(zval *bytes) {
long long number = 0;
}
PHP_FUNCTION(sint64_array_to_string) {
long long number = 0;
zval *bytes = NULL;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_ZVAL(bytes)
ZEND_PARSE_PARAMETERS_END();
if(Z_TYPE_P(bytes) == IS_ARRAY) {
int len = zend_array_count( Z_ARR_P(bytes) );
if(len > 8)
zend_error(E_WARNING, "secure_int64 will only handle the first 8 bytes of passed array");
for(int e=0;e<len;e++) {
zval *data = zend_hash_index_find(Z_ARR_P(bytes), e);
if(Z_TYPE_P(data) == IS_LONG) {
long value = Z_LVAL_P(data);
if(value >= 0 && value <=255) {
number <<= 8;
number += value;
} else {
zend_error(E_WARNING, "secure_int64 expects an array containing integer values between 0 and 255");
}
}
}
}
char buffer[40];
sprintf(buffer, "%lld", number);
ZVAL_STRING(return_value, buffer);
}
PHP_FUNCTION(uint64_array_to_string) {
unsigned long long number = 0;
zval *bytes = NULL;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_ZVAL(bytes)
ZEND_PARSE_PARAMETERS_END();
if(Z_TYPE_P(bytes) == IS_ARRAY) {
int len = zend_array_count( Z_ARR_P(bytes) );
if(len > 8)
zend_error(E_WARNING, "secure_int64 will only handle the first 8 bytes of passed array");
for(int e=0;e<len;e++) {
zval *data = zend_hash_index_find(Z_ARR_P(bytes), e);
if(Z_TYPE_P(data) == IS_LONG) {
long value = Z_LVAL_P(data);
if(value >= 0 && value <=255) {
number <<= 8;
number += value;
} else {
zend_error(E_WARNING, "secure_int64 expects an array containing integer values between 0 and 255");
}
}
}
}
char buffer[40];
sprintf(buffer, "%llu", number);
ZVAL_STRING(return_value, buffer);
}
PHP_FUNCTION(sint64_string_to_array) {
long long number = 0;
char *buffer = "0000000000000000000000000000000000000000";
size_t length = 0;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STRING(buffer, length)
ZEND_PARSE_PARAMETERS_END();
sscanf(buffer, "%lld", &number);
zend_array *ret = zend_new_array(8);
RETVAL_ARR(ret);
for(int e=0;e<8;e++) {
add_next_index_long(return_value, (number >> (56-e*8)) & 0xFF);
}
}
PHP_FUNCTION(uint64_string_to_array) {
unsigned long long number = 0;
char *buffer = "0000000000000000000000000000000000000000";
size_t length = 0;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STRING(buffer, length)
ZEND_PARSE_PARAMETERS_END();
sscanf(buffer, "%llu", &number);
zend_array *ret = zend_new_array(8);
RETVAL_ARR(ret);
for(int e=0;e<8;e++) {
add_next_index_long(return_value, (number >> (56-e*8)) & 0xFF);
}
}