diff options
author | Luca Deri <deri@ntop.org> | 2024-09-24 12:28:21 +0200 |
---|---|---|
committer | Luca Deri <deri@ntop.org> | 2024-09-24 12:28:21 +0200 |
commit | 42cfd29cc3d7dd2c883c8fd3c5f53319f752fbfc (patch) | |
tree | f1354055bb664356182151bf7def999afe02a97b /src | |
parent | 806f47337d591b82ba2db211629b2b25429cc21e (diff) |
Added new API calls
u_int ndpi_hex2bin(u_char *out, u_int out_len, u_char* in, u_int in_len);
u_int ndpi_bin2hex(u_char *out, u_int out_len, u_char* in, u_int in_len);
Diffstat (limited to 'src')
-rw-r--r-- | src/include/ndpi_api.h | 5 | ||||
-rw-r--r-- | src/lib/ndpi_utils.c | 39 |
2 files changed, 36 insertions, 8 deletions
diff --git a/src/include/ndpi_api.h b/src/include/ndpi_api.h index 14ca651ef..7e695cc32 100644 --- a/src/include/ndpi_api.h +++ b/src/include/ndpi_api.h @@ -1772,6 +1772,11 @@ extern "C" { /* ******************************* */ + u_int ndpi_hex2bin(u_char *out, u_int out_len, u_char* in, u_int in_len); + u_int ndpi_bin2hex(u_char *out, u_int out_len, u_char* in, u_int in_len); + + /* ******************************* */ + int ndpi_des_init(struct ndpi_des_struct *des, double alpha, double beta, float significance); int ndpi_des_add_value(struct ndpi_des_struct *des, const double _value, double *forecast, double *confidence_band); void ndpi_des_fitting(double *values, u_int32_t num_values, float *ret_alpha, float *ret_beta); diff --git a/src/lib/ndpi_utils.c b/src/lib/ndpi_utils.c index 46cf41a8d..ebe057c56 100644 --- a/src/lib/ndpi_utils.c +++ b/src/lib/ndpi_utils.c @@ -3689,23 +3689,46 @@ bool ndpi_serialize_flow_fingerprint(struct ndpi_detection_module_struct *ndpi_s } /* ****************************************************** */ -/* ****************************************************** */ - -#include "third_party/include/aes.h" -static void ndpi_key_hex2bin(u_char *out, u_char* key, u_int key_len) { +u_int ndpi_hex2bin(u_char *out, u_int out_len, u_char* in, u_int in_len) { u_int i, j; - for(i=0, j=0; i<key_len; i += 2, j++) { + if(((in_len+1) / 2) > out_len) + return(0); + + for(i=0, j=0; i<in_len; i += 2, j++) { char buf[3]; - buf[0] = key[i], buf[1] = key[i+1], buf[2] = '\0'; + buf[0] = in[i], buf[1] = in[i+1], buf[2] = '\0'; out[j] = strtol(buf, NULL, 16); } + + return(j); } /* ****************************************************** */ +u_int ndpi_bin2hex(u_char *out, u_int out_len, u_char* in, u_int in_len) { + u_int i, j; + + if (out_len < (in_len*2)+1) { + out[0] = '\0'; + return(0); + } + + for(i=0, j=0; i<in_len; i++) { + snprintf((char*)&out[j], out_len-j, "%02X", in[i]); + j += 2; + } + + return(j); +} + +/* ****************************************************** */ +/* ****************************************************** */ + +#include "third_party/include/aes.h" + /* IMPORTANT: the returned string (if not NULL) must be freed */ @@ -3729,7 +3752,7 @@ char* ndpi_quick_encrypt(const char *cleartext_msg, return(NULL); } - ndpi_key_hex2bin(binary_encrypt_key, (u_char*)encrypt_key, 64); + ndpi_hex2bin(binary_encrypt_key, sizeof(binary_encrypt_key), (u_char*)encrypt_key, 64); memcpy(encoded_buf, cleartext_msg, cleartext_msg_len); @@ -3766,7 +3789,7 @@ char* ndpi_quick_decrypt(const char *encrypted_msg, return(NULL); } - ndpi_key_hex2bin(binary_decrypt_key, (u_char*)decrypt_key, 64); + ndpi_hex2bin(binary_decrypt_key, sizeof(binary_decrypt_key), (u_char*)decrypt_key, 64); content = ndpi_base64_decode((const u_char*)encrypted_msg, encrypted_msg_len, &content_len); |