@@ -79,7 +79,7 @@ typedef struct {
7979 bool can_run_simd256 ;
8080} Blake2State ;
8181
82- static inline Blake2State *
82+ static inline Blake2State *
8383blake2_get_state (PyObject * module )
8484{
8585 void * state = _PyModule_GetState (module );
@@ -88,7 +88,7 @@ blake2_get_state(PyObject *module)
8888}
8989
9090#if defined(HACL_CAN_COMPILE_SIMD128 ) || defined(HACL_CAN_COMPILE_SIMD256 )
91- static inline Blake2State *
91+ static inline Blake2State *
9292blake2_get_state_from_type (PyTypeObject * module )
9393{
9494 void * state = _PyType_GetModuleState (module );
@@ -201,29 +201,32 @@ blake2module_init_cpu_features(Blake2State *state)
201201#endif
202202}
203203
204- #define ADD_INT (d , name , value ) do { \
205- PyObject *x = PyLong_FromLong(value); \
206- if (!x) \
207- return -1; \
208- if (PyDict_SetItemString(d, name, x) < 0) { \
209- Py_DECREF(x); \
210- return -1; \
211- } \
212- Py_DECREF(x); \
213- } while(0)
214-
215- #define ADD_INT_CONST (NAME , VALUE ) do { \
216- if (PyModule_AddIntConstant(m, NAME, VALUE) < 0) { \
217- return -1; \
218- } \
219- } while (0)
220-
221204static int
222205blake2_exec (PyObject * m )
223206{
224207 Blake2State * st = blake2_get_state (m );
225208 blake2module_init_cpu_features (st );
226209
210+ #define ADD_INT (DICT , NAME , VALUE ) \
211+ do { \
212+ PyObject *x = PyLong_FromLong(VALUE); \
213+ if (x == NULL) { \
214+ return -1; \
215+ } \
216+ int rc = PyDict_SetItemString(DICT, NAME, x); \
217+ Py_DECREF(x); \
218+ if (rc < 0) { \
219+ return -1; \
220+ } \
221+ } while(0)
222+
223+ #define ADD_INT_CONST (NAME , VALUE ) \
224+ do { \
225+ if (PyModule_AddIntConstant(m, NAME, VALUE) < 0) { \
226+ return -1; \
227+ } \
228+ } while (0)
229+
227230 ADD_INT_CONST ("_GIL_MINSIZE" , HASHLIB_GIL_MINSIZE );
228231
229232 st -> blake2b_type = (PyTypeObject * )PyType_FromModuleAndSpec (
@@ -232,7 +235,6 @@ blake2_exec(PyObject *m)
232235 if (st -> blake2b_type == NULL ) {
233236 return -1 ;
234237 }
235- /* BLAKE2b */
236238 if (PyModule_AddType (m , st -> blake2b_type ) < 0 ) {
237239 return -1 ;
238240 }
@@ -252,9 +254,9 @@ blake2_exec(PyObject *m)
252254 st -> blake2s_type = (PyTypeObject * )PyType_FromModuleAndSpec (
253255 m , & blake2s_type_spec , NULL );
254256
255- if (NULL == st -> blake2s_type )
257+ if (st -> blake2s_type == NULL ) {
256258 return -1 ;
257-
259+ }
258260 if (PyModule_AddType (m , st -> blake2s_type ) < 0 ) {
259261 return -1 ;
260262 }
@@ -270,12 +272,11 @@ blake2_exec(PyObject *m)
270272 ADD_INT_CONST ("BLAKE2S_MAX_KEY_SIZE" , HACL_HASH_BLAKE2S_KEY_BYTES );
271273 ADD_INT_CONST ("BLAKE2S_MAX_DIGEST_SIZE" , HACL_HASH_BLAKE2S_OUT_BYTES );
272274
275+ #undef ADD_INT_CONST
276+ #undef ADD_INT
273277 return 0 ;
274278}
275279
276- #undef ADD_INT
277- #undef ADD_INT_CONST
278-
279280static PyModuleDef_Slot _blake2_slots [] = {
280281 {Py_mod_exec , blake2_exec },
281282 {Py_mod_multiple_interpreters , Py_MOD_PER_INTERPRETER_GIL_SUPPORTED },
@@ -315,16 +316,21 @@ PyInit__blake2(void)
315316// set.
316317typedef enum { Blake2s , Blake2b , Blake2s_128 , Blake2b_256 } blake2_impl ;
317318
318- static inline bool is_blake2b (blake2_impl impl ) {
319- return impl == Blake2b || impl == Blake2b_256 ;
319+ static inline bool
320+ is_blake2b (blake2_impl impl )
321+ {
322+ return impl == Blake2b || impl == Blake2b_256 ;
320323}
321324
322- static inline bool is_blake2s (blake2_impl impl ) {
323- return !is_blake2b (impl );
325+ static inline bool
326+ is_blake2s (blake2_impl impl )
327+ {
328+ return impl == Blake2s || impl == Blake2s_128 ;
324329}
325330
326331static inline blake2_impl
327- type_to_impl (PyTypeObject * type ) {
332+ type_to_impl (PyTypeObject * type )
333+ {
328334#if defined(HACL_CAN_COMPILE_SIMD128 ) || defined(HACL_CAN_COMPILE_SIMD256 )
329335 Blake2State * st = blake2_get_state_from_type (type );
330336#endif
@@ -423,19 +429,23 @@ update(Blake2Object *self, uint8_t *buf, Py_ssize_t len)
423429 // otherwise this results in an unresolved symbol at link-time.
424430#if HACL_CAN_COMPILE_SIMD256
425431 case Blake2b_256 :
426- HACL_UPDATE (Hacl_Hash_Blake2b_Simd256_update ,self -> blake2b_256_state , buf , len );
432+ HACL_UPDATE (Hacl_Hash_Blake2b_Simd256_update ,
433+ self -> blake2b_256_state , buf , len );
427434 return ;
428435#endif
429436#if HACL_CAN_COMPILE_SIMD128
430437 case Blake2s_128 :
431- HACL_UPDATE (Hacl_Hash_Blake2s_Simd128_update ,self -> blake2s_128_state , buf , len );
438+ HACL_UPDATE (Hacl_Hash_Blake2s_Simd128_update ,
439+ self -> blake2s_128_state , buf , len );
432440 return ;
433441#endif
434442 case Blake2b :
435- HACL_UPDATE (Hacl_Hash_Blake2b_update ,self -> blake2b_state , buf , len );
443+ HACL_UPDATE (Hacl_Hash_Blake2b_update ,
444+ self -> blake2b_state , buf , len );
436445 return ;
437446 case Blake2s :
438- HACL_UPDATE (Hacl_Hash_Blake2s_update ,self -> blake2s_state , buf , len );
447+ HACL_UPDATE (Hacl_Hash_Blake2s_update ,
448+ self -> blake2s_state , buf , len );
439449 return ;
440450 default :
441451 Py_UNREACHABLE ();
@@ -825,7 +835,8 @@ _blake2_blake2b_update_impl(Blake2Object *self, PyObject *data)
825835 update (self , buf .buf , buf .len );
826836 PyMutex_Unlock (& self -> mutex );
827837 Py_END_ALLOW_THREADS
828- } else {
838+ }
839+ else {
829840 update (self , buf .buf , buf .len );
830841 }
831842
@@ -1021,7 +1032,7 @@ static PyType_Slot blake2b_type_slots[] = {
10211032 {Py_tp_methods , py_blake2b_methods },
10221033 {Py_tp_getset , py_blake2b_getsetters },
10231034 {Py_tp_new , py_blake2b_new },
1024- {0 ,0 }
1035+ {0 , 0 }
10251036};
10261037
10271038static PyType_Slot blake2s_type_slots [] = {
@@ -1034,20 +1045,20 @@ static PyType_Slot blake2s_type_slots[] = {
10341045 // only the constructor differs, so that it can receive a clinic-generated
10351046 // default digest length suitable for blake2s
10361047 {Py_tp_new , py_blake2s_new },
1037- {0 ,0 }
1048+ {0 , 0 }
10381049};
10391050
10401051static PyType_Spec blake2b_type_spec = {
10411052 .name = "_blake2.blake2b" ,
1042- .basicsize = sizeof (Blake2Object ),
1053+ .basicsize = sizeof (Blake2Object ),
10431054 .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE
10441055 | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HEAPTYPE ,
10451056 .slots = blake2b_type_slots
10461057};
10471058
10481059static PyType_Spec blake2s_type_spec = {
10491060 .name = "_blake2.blake2s" ,
1050- .basicsize = sizeof (Blake2Object ),
1061+ .basicsize = sizeof (Blake2Object ),
10511062 .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE
10521063 | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HEAPTYPE ,
10531064 .slots = blake2s_type_slots
0 commit comments