This repository has been archived by the owner on Aug 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpytox.c
179 lines (147 loc) · 4.66 KB
/
pytox.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
* pytoxcore
*
* Copyright (C) 2015 Anton Batenev <antonbatenev@yandex.ru>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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 the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//----------------------------------------------------------------------------------------------
#include "pytoxav.h"
#include "pytoxdns.h"
//----------------------------------------------------------------------------------------------
void bytes_to_hex_string(const uint8_t* digest, size_t length, uint8_t* hex_digest)
{
hex_digest[2 * length] = 0;
size_t i;
size_t j;
for (i = j = 0; i < length; i++) {
char c;
c = (digest[i] >> 4) & 0xF;
c = (c > 9) ? c + 'A' - 10 : c + '0';
hex_digest[j++] = c;
c = (digest[i] & 0xF);
c = (c > 9) ? c + 'A' - 10 : c + '0';
hex_digest[j++] = c;
}
}
//----------------------------------------------------------------------------------------------
static int hex_char_to_int(char c)
{
int val = 0;
if (c >= '0' && c <= '9')
val = c - '0';
else if (c >= 'A' && c <= 'F')
val = c - 'A' + 10;
else if (c >= 'a' && c <= 'f')
val = c - 'a' + 10;
else
val = -1;
return val;
}
//----------------------------------------------------------------------------------------------
bool hex_string_to_bytes(const uint8_t* hexstr, size_t length, uint8_t* bytes)
{
size_t i;
for (i = 0; i < length; i++) {
int i1 = hex_char_to_int(hexstr[2 * i]);
if (i1 == -1)
return false;
int i2 = hex_char_to_int(hexstr[2 * i + 1]);
if (i2 == -1)
return false;
bytes[i] = (i1 << 4) | i2;
}
return true;
}
//----------------------------------------------------------------------------------------------
PyObject* PyNone_New(void)
{
Py_INCREF(Py_None);
return Py_None;
}
//----------------------------------------------------------------------------------------------
#if PY_MAJOR_VERSION >= 3
struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"pytoxcore",
"Python binding for ToxCore",
-1,
NULL,
NULL,
NULL,
NULL,
NULL
};
#endif
//----------------------------------------------------------------------------------------------
#if PY_MAJOR_VERSION >= 3
PyMODINIT_FUNC PyInit_pytoxcore(void)
{
PyObject* module = PyModule_Create(&moduledef);
#else
PyMODINIT_FUNC initpytoxcore(void)
{
PyObject* module = Py_InitModule("pytoxcore", NULL);
#endif
if (module == NULL)
goto error;
if (sodium_init() == -1)
goto error;
//
// initialize pytoxcore
//
ToxCore_install_dict();
if (PyType_Ready(&ToxCoreType) < 0) {
fprintf(stderr, "Invalid PyTypeObject 'ToxCoreType'\n");
goto error;
}
Py_INCREF(&ToxCoreType);
PyModule_AddObject(module, "ToxCore", (PyObject*)&ToxCoreType);
ToxCoreException = PyErr_NewException("pytoxcore.ToxCoreException", NULL, NULL);
PyModule_AddObject(module, "ToxCoreException", (PyObject*)ToxCoreException);
//
// initialize pytoxav
//
ToxAV_install_dict();
if (PyType_Ready(&ToxAVType) < 0) {
fprintf(stderr, "Invalid PyTypeObject 'ToxAVType'\n");
goto error;
}
Py_INCREF(&ToxAVType);
PyModule_AddObject(module, "ToxAV", (PyObject*)&ToxAVType);
ToxAVException = PyErr_NewException("pytoxcore.ToxAVException", NULL, NULL);
PyModule_AddObject(module, "ToxAVException", (PyObject*)ToxAVException);
//
// initialize pytoxdns
//
ToxDNS_install_dict();
if (PyType_Ready(&ToxDNSType) < 0) {
fprintf(stderr, "Invalid PyTypeObject 'ToxDNSType'\n");
goto error;
}
Py_INCREF(&ToxDNSType);
PyModule_AddObject(module, "ToxDNS", (PyObject*)&ToxDNSType);
ToxDNSException = PyErr_NewException("pytoxcore.ToxDNSException", NULL, NULL);
PyModule_AddObject(module, "ToxDNSException", (PyObject*)ToxDNSException);
#if PY_MAJOR_VERSION >= 3
return module;
#endif
error:
#if PY_MAJOR_VERSION >= 3
return NULL;
#else
return;
#endif
}
//----------------------------------------------------------------------------------------------