Skip to content

Commit d0d2b83

Browse files
authored
Merge pull request #8 from cdoco/develop
Optimize the code (PHP_MINIT_FUNCTION)
2 parents 15ed8d1 + 8e69907 commit d0d2b83

File tree

1 file changed

+36
-37
lines changed

1 file changed

+36
-37
lines changed

jwt.c

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_jwt_decode, 0, 0, 2)
6262
ZEND_ARG_INFO(0, options)
6363
ZEND_END_ARG_INFO()
6464

65+
/* register internal class */
66+
static zend_class_entry *jwt_register_class(const char *name)
67+
{
68+
zend_class_entry ce;
69+
70+
INIT_CLASS_ENTRY_EX(ce, name, strlen(name), NULL);
71+
return zend_register_internal_class_ex(&ce, zend_ce_exception);
72+
}
73+
6574
/* string to algorithm */
6675
jwt_alg_t jwt_str_alg(const char *alg)
6776
{
@@ -146,6 +155,7 @@ static int jwt_verify(jwt_t *jwt, const char *sig)
146155
}
147156
}
148157

158+
/* jwt new */
149159
int jwt_new(jwt_t **jwt)
150160
{
151161
if (!jwt) {
@@ -162,6 +172,7 @@ int jwt_new(jwt_t **jwt)
162172
return 0;
163173
}
164174

175+
/* jwt free */
165176
void jwt_free(jwt_t *jwt)
166177
{
167178
if (!jwt) {
@@ -171,6 +182,7 @@ void jwt_free(jwt_t *jwt)
171182
efree(jwt);
172183
}
173184

185+
/* base64 url safe encode */
174186
void jwt_b64_url_encode_ex(char *str)
175187
{
176188
int len = strlen(str);
@@ -194,6 +206,7 @@ void jwt_b64_url_encode_ex(char *str)
194206
str[t] = '\0';
195207
}
196208

209+
/* base64 encode */
197210
char *jwt_b64_url_encode(zend_string *input)
198211
{
199212
zend_string *b64_str = NULL;
@@ -210,6 +223,7 @@ char *jwt_b64_url_encode(zend_string *input)
210223
return ZSTR_VAL(new);
211224
}
212225

226+
/* base64 decode */
213227
zend_string *jwt_b64_url_decode(const char *src)
214228
{
215229
char *new;
@@ -246,6 +260,7 @@ zend_string *jwt_b64_url_decode(const char *src)
246260
return php_base64_decode_ex((const unsigned char *)new, strlen(new), 1);
247261
}
248262

263+
/* hash find string */
249264
char *jwt_hash_str_find_str(zval *arr, char *key)
250265
{
251266
char *str = NULL;
@@ -262,6 +277,7 @@ char *jwt_hash_str_find_str(zval *arr, char *key)
262277
return str;
263278
}
264279

280+
/* hash find long */
265281
long jwt_hash_str_find_long(zval *arr, char *key)
266282
{
267283
zval *zv = zend_hash_str_find(Z_ARRVAL_P(arr), key, strlen(key));
@@ -277,6 +293,7 @@ long jwt_hash_str_find_long(zval *arr, char *key)
277293
return 0;
278294
}
279295

296+
/* hash find zend_array */
280297
zend_array *jwt_hash_str_find_ht(zval *arr, char *key)
281298
{
282299
zval *zv = zend_hash_str_find(Z_ARRVAL_P(arr), key, strlen(key));
@@ -292,7 +309,8 @@ zend_array *jwt_hash_str_find_ht(zval *arr, char *key)
292309
return NULL;
293310
}
294311

295-
int jwt_verify_claims(zval *arr, char *key, char *str)
312+
/* verify string claims */
313+
int jwt_verify_claims_str(zval *arr, char *key, char *str)
296314
{
297315
char *rs = jwt_hash_str_find_str(arr, key);
298316
if (rs && str && strcmp(rs, str)) {
@@ -302,6 +320,7 @@ int jwt_verify_claims(zval *arr, char *key, char *str)
302320
return 0;
303321
}
304322

323+
/* array equals */
305324
int jwt_array_equals(zend_array *arr1, zend_array *arr2) {
306325
zend_ulong i;
307326
zval *value = NULL;
@@ -329,6 +348,7 @@ int jwt_array_equals(zend_array *arr1, zend_array *arr2) {
329348
return 0;
330349
}
331350

351+
/* verify body */
332352
int jwt_verify_body(char *body, zval *return_value)
333353
{
334354
zend_class_entry *ce;
@@ -358,7 +378,7 @@ int jwt_verify_body(char *body, zval *return_value)
358378
}
359379

360380
/* iss */
361-
if (jwt_verify_claims(return_value, "iss", JWT_G(iss))) {
381+
if (jwt_verify_claims_str(return_value, "iss", JWT_G(iss))) {
362382
ce = jwt_invalid_issuer_cex;
363383
err_msg = "Invalid Issuer";
364384
}
@@ -375,7 +395,7 @@ int jwt_verify_body(char *body, zval *return_value)
375395
}
376396

377397
/* jti */
378-
if (jwt_verify_claims(return_value, "jti", JWT_G(jti))) {
398+
if (jwt_verify_claims_str(return_value, "jti", JWT_G(jti))) {
379399
ce = jwt_invalid_jti_cex;
380400
err_msg = "Invalid Jti";
381401
}
@@ -387,7 +407,7 @@ int jwt_verify_body(char *body, zval *return_value)
387407
}
388408

389409
/* sub */
390-
if (jwt_verify_claims(return_value, "sub", JWT_G(sub))) {
410+
if (jwt_verify_claims_str(return_value, "sub", JWT_G(sub))) {
391411
ce = jwt_invalid_sub_cex;
392412
err_msg = "Invalid Sub";
393413
}
@@ -400,6 +420,7 @@ int jwt_verify_body(char *body, zval *return_value)
400420
return 0;
401421
}
402422

423+
/* parse options */
403424
int jwt_parse_options(zval *options)
404425
{
405426
/* check options */
@@ -433,6 +454,7 @@ int jwt_parse_options(zval *options)
433454
return 0;
434455
}
435456

457+
/* function jwt_encode() */
436458
PHP_FUNCTION(jwt_encode)
437459
{
438460
zval *payload = NULL, header;
@@ -523,6 +545,7 @@ PHP_FUNCTION(jwt_encode)
523545
}
524546
}
525547

548+
/* function jwt_decode() */
526549
PHP_FUNCTION(jwt_decode)
527550
{
528551
zend_string *token = NULL, *key = NULL;
@@ -650,39 +673,15 @@ PHP_GINIT_FUNCTION(jwt) {
650673

651674
PHP_MINIT_FUNCTION(jwt)
652675
{
653-
zend_class_entry ce;
654-
655-
/* SignatureInvalidException */
656-
INIT_CLASS_ENTRY(ce, "SignatureInvalidException", NULL);
657-
jwt_signature_invalid_cex = zend_register_internal_class_ex(&ce, zend_ce_exception);
658-
659-
/* BeforeValidException */
660-
INIT_CLASS_ENTRY(ce, "BeforeValidException", NULL);
661-
jwt_before_valid_cex = zend_register_internal_class_ex(&ce, zend_ce_exception);
662-
663-
/* ExpiredSignatureException */
664-
INIT_CLASS_ENTRY(ce, "ExpiredSignatureException", NULL);
665-
jwt_expired_signature_cex = zend_register_internal_class_ex(&ce, zend_ce_exception);
666-
667-
/* InvalidIssuerException */
668-
INIT_CLASS_ENTRY(ce, "InvalidIssuerException", NULL);
669-
jwt_invalid_issuer_cex = zend_register_internal_class_ex(&ce, zend_ce_exception);
670-
671-
/* InvalidAudException */
672-
INIT_CLASS_ENTRY(ce, "InvalidAudException", NULL);
673-
jwt_invalid_aud_cex = zend_register_internal_class_ex(&ce, zend_ce_exception);
674-
675-
/* InvalidJtiException */
676-
INIT_CLASS_ENTRY(ce, "InvalidJtiException", NULL);
677-
jwt_invalid_jti_cex = zend_register_internal_class_ex(&ce, zend_ce_exception);
678-
679-
/* InvalidIatException */
680-
INIT_CLASS_ENTRY(ce, "InvalidIatException", NULL);
681-
jwt_invalid_iat_cex = zend_register_internal_class_ex(&ce, zend_ce_exception);
682-
683-
/* InvalidSubException */
684-
INIT_CLASS_ENTRY(ce, "InvalidSubException", NULL);
685-
jwt_invalid_sub_cex = zend_register_internal_class_ex(&ce, zend_ce_exception);
676+
/* register exception class */
677+
jwt_signature_invalid_cex = jwt_register_class("SignatureInvalidException");
678+
jwt_before_valid_cex = jwt_register_class("BeforeValidException");
679+
jwt_expired_signature_cex = jwt_register_class("ExpiredSignatureException");
680+
jwt_invalid_issuer_cex = jwt_register_class("InvalidIssuerException");
681+
jwt_invalid_aud_cex = jwt_register_class("InvalidAudException");
682+
jwt_invalid_jti_cex = jwt_register_class("InvalidJtiException");
683+
jwt_invalid_iat_cex = jwt_register_class("InvalidIatException");
684+
jwt_invalid_sub_cex = jwt_register_class("InvalidSubException");
686685

687686
return SUCCESS;
688687
}

0 commit comments

Comments
 (0)