2018-07-25 14:10:09 +00:00
|
|
|
#include "id.h"
|
|
|
|
|
2018-08-14 15:59:55 +00:00
|
|
|
static _Bool if_rand = 0;
|
|
|
|
|
2018-07-25 14:10:09 +00:00
|
|
|
void init_rand(void) {
|
|
|
|
srand((unsigned)time(NULL));
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long long getId(void) {
|
|
|
|
int i;
|
|
|
|
unsigned long long id = 0;
|
|
|
|
id = ((rand() % 9) + 1);
|
|
|
|
for (i = 0; i < 15; i++) {
|
|
|
|
id *= 10;
|
|
|
|
id += rand() % 10;
|
|
|
|
}
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2018-07-30 09:45:33 +00:00
|
|
|
SID *initS_id(unsigned int deep_level){
|
2018-08-21 08:26:31 +00:00
|
|
|
if (!if_rand){
|
|
|
|
init_rand();
|
|
|
|
if_rand = 1;
|
|
|
|
}
|
2018-08-03 10:32:20 +00:00
|
|
|
SID *p_sid = (SID *) malloc(sizeof(SID));
|
2018-08-03 10:58:49 +00:00
|
|
|
if(p_sid == NULL){
|
|
|
|
printf("\ninitS_id(): Error in getting the memory of sid.\n");
|
|
|
|
}
|
2018-08-14 15:59:55 +00:00
|
|
|
p_sid->sr = malloc(sizeof(struct sid_raw));
|
|
|
|
p_sid->sr->type = VOID;
|
2018-07-30 09:45:33 +00:00
|
|
|
p_sid->deep = deep_level;
|
2018-08-14 15:59:55 +00:00
|
|
|
p_sid->sr->value = NULL;
|
|
|
|
p_sid->sr->value_deeper = NULL;
|
|
|
|
p_sid->sr->value_deepest = NULL;
|
|
|
|
p_sid->md5 = NULL;
|
2018-08-21 08:26:31 +00:00
|
|
|
p_sid->decrypt_str = NULL;
|
|
|
|
p_sid->decrypt_hex = NULL;
|
2018-08-17 08:58:15 +00:00
|
|
|
return p_sid;
|
|
|
|
}
|
|
|
|
|
|
|
|
void getRawS_id(SID *p_sid, unsigned int type, unsigned int deep_level){
|
2018-08-03 10:58:49 +00:00
|
|
|
if (deep_level > 0) {
|
2018-08-14 15:59:55 +00:00
|
|
|
p_sid->sr->value = (unsigned int *)malloc(sizeof(unsigned int)*DEEPC_LEN);
|
|
|
|
if(p_sid->sr->value == NULL){
|
2018-08-03 10:58:49 +00:00
|
|
|
printf("\ninitS_id(): Error in getting the memory of sid.value.\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (deep_level > 1){
|
2018-08-14 15:59:55 +00:00
|
|
|
p_sid->sr->value_deeper = (unsigned int *)malloc(sizeof(unsigned int)*DEEPB_LEN);
|
|
|
|
if(p_sid->sr->value_deeper == NULL){
|
2018-08-03 10:58:49 +00:00
|
|
|
printf("\ninitS_id(): Error in getting the memory of sid.value_deeper.\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (deep_level > 2){
|
2018-08-14 15:59:55 +00:00
|
|
|
p_sid->sr->value_deepest = (unsigned int *)malloc(sizeof(unsigned int)*DEEPA_LEN);
|
|
|
|
if(p_sid->sr->value_deepest == NULL){
|
2018-08-03 10:58:49 +00:00
|
|
|
printf("\ninitS_id(): Error in getting the memory of sid.value_deepest.\n");
|
|
|
|
}
|
|
|
|
}
|
2018-08-14 15:59:55 +00:00
|
|
|
p_sid->sr->type = type;
|
2018-07-30 09:45:33 +00:00
|
|
|
if(deep_level > 0){
|
2018-08-14 15:59:55 +00:00
|
|
|
for(register int i = 0; i < DEEPC_LEN; i++) p_sid->sr->value[i] = rand()%65535;
|
|
|
|
if(deep_level > 1) for(register int i = 0; i < DEEPB_LEN; i++) p_sid->sr->value_deeper[i] = rand()%65535;
|
|
|
|
if (deep_level > 2) for(register int i = 0; i < DEEPA_LEN; i++) p_sid->sr->value_deepest[i] = rand()%65535;
|
2018-07-30 09:45:33 +00:00
|
|
|
}
|
2018-08-17 08:58:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SID *getS_id(unsigned int type, unsigned int deep_level){
|
|
|
|
SID *p_sid = initS_id(deep_level);
|
|
|
|
getRawS_id(p_sid, type, deep_level);
|
2018-08-14 15:59:55 +00:00
|
|
|
s_idToMD5(p_sid);
|
2018-08-21 08:26:31 +00:00
|
|
|
setSidToASCIIString(p_sid);
|
2018-07-30 09:45:33 +00:00
|
|
|
return p_sid;
|
|
|
|
}
|
|
|
|
|
2018-08-14 15:59:55 +00:00
|
|
|
int fitS_id(SID * const fs_id, SID * const ss_id){
|
2018-08-21 08:26:31 +00:00
|
|
|
if(fs_id->decrypt_str == NULL) setSidToASCIIString(fs_id);
|
|
|
|
if(ss_id->decrypt_str == NULL) setSidToASCIIString(ss_id);
|
2018-08-14 15:59:55 +00:00
|
|
|
return strcmp(fs_id->decrypt_str, ss_id->decrypt_str);
|
2018-07-30 09:45:33 +00:00
|
|
|
}
|
|
|
|
|
2018-08-21 08:26:31 +00:00
|
|
|
void setSidToASCIIString(SID * const s_id){
|
2018-08-14 15:59:55 +00:00
|
|
|
if(s_id->decrypt_str == NULL){
|
|
|
|
s_id->decrypt_str = malloc(sizeof(char) * 33);
|
|
|
|
s_id->decrypt_str[32] = '\0';
|
|
|
|
for(register int i = 0; i < 16; i++){
|
2018-08-21 08:26:31 +00:00
|
|
|
unsigned int temp_dyt = s_id->decrypt_hex[i];
|
|
|
|
//printf("%d\n",(unsigned int)temp_dyt);
|
|
|
|
unsigned int k = 0;
|
|
|
|
for (k = 0; (temp_dyt - (k * 16)) >= 16; k++);
|
2018-08-14 15:59:55 +00:00
|
|
|
s_id->decrypt_str[i * 2] = hexToChar(k);
|
2018-08-21 08:26:31 +00:00
|
|
|
//printf("HEX:%c",hexToChar(k));
|
|
|
|
s_id->decrypt_str[i * 2 + 1] = hexToChar(temp_dyt - k * 16);
|
|
|
|
//printf("HEX:%c",hexToChar(temp_dyt - k * 16));
|
2018-08-14 15:59:55 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-21 08:26:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int simFitS_id(SID * fs_id, SID * ss_id){
|
|
|
|
return !fitS_id(fs_id, ss_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *s_idToASCIIString(SID * const s_id){
|
|
|
|
if(s_id->decrypt_str == NULL){
|
|
|
|
setSidToASCIIString(s_id);
|
|
|
|
}
|
|
|
|
free(s_id->md5);
|
|
|
|
s_id->md5 = NULL;
|
|
|
|
//printf("s_id->decrypt_str: %s",s_id->decrypt_str);
|
2018-08-17 08:58:15 +00:00
|
|
|
char *rtn_str = malloc(sizeof(char) * 33);
|
|
|
|
strcpy(rtn_str, s_id->decrypt_str);
|
2018-08-21 08:26:31 +00:00
|
|
|
free(s_id->decrypt_hex);
|
|
|
|
s_id->decrypt_hex = NULL;
|
2018-08-17 08:58:15 +00:00
|
|
|
return rtn_str;
|
2018-08-14 15:59:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char *s_idToASCIIRawString(SID * const s_id){
|
2018-07-30 09:45:33 +00:00
|
|
|
char *string = NULL;
|
|
|
|
int deep_len = 0, temp, buff_count, string_count;
|
|
|
|
unsigned int buff[DATA_BIT];
|
|
|
|
if(s_id->deep > 0){
|
|
|
|
if (s_id->deep == DEEPC){
|
2018-08-03 10:32:20 +00:00
|
|
|
string = (char *) malloc(sizeof(char) * (DEEPC_LEN + 1) * DATA_BIT);
|
2018-07-30 09:45:33 +00:00
|
|
|
deep_len = DEEPC_LEN + 1;
|
|
|
|
deep_len *= DATA_BIT;
|
|
|
|
}
|
|
|
|
else if (s_id->deep == DEEPB){
|
2018-08-03 10:32:20 +00:00
|
|
|
string = (char *) malloc(sizeof(char) * (DEEPC_LEN + DEEPB_LEN + 1) * DATA_BIT);
|
2018-07-30 09:45:33 +00:00
|
|
|
deep_len = DEEPC_LEN + DEEPB_LEN + 1;
|
|
|
|
deep_len *= DATA_BIT;
|
|
|
|
}
|
|
|
|
else{
|
2018-08-03 10:32:20 +00:00
|
|
|
string = (char *)malloc(sizeof(char) * (DEEPC_LEN + DEEPB_LEN + DEEPA_LEN + 1) * DATA_BIT);
|
2018-07-30 09:45:33 +00:00
|
|
|
deep_len = DEEPC_LEN + DEEPB_LEN + DEEPA_LEN + 1;
|
|
|
|
deep_len *= DATA_BIT;
|
|
|
|
}
|
2018-08-03 10:58:49 +00:00
|
|
|
if(string == NULL){
|
|
|
|
printf("\ns_idToASCIIString(): Error in getting the memory of string.\n");
|
|
|
|
}
|
2018-07-30 09:45:33 +00:00
|
|
|
string[deep_len] = '\0';
|
2018-08-14 15:59:55 +00:00
|
|
|
temp = s_id->sr->type;
|
2018-07-30 09:45:33 +00:00
|
|
|
buff_count = DATA_BIT - 1;
|
|
|
|
for (int i = 0; i < DATA_BIT; i++) buff[i] = 0;
|
|
|
|
while(buff_count >= 0){
|
|
|
|
buff[buff_count] = temp % 10;
|
|
|
|
string[buff_count] = buff[buff_count] + 48;
|
|
|
|
temp /= 10;
|
|
|
|
buff_count--;
|
|
|
|
}
|
|
|
|
deep_len -= DATA_BIT;
|
|
|
|
for(int i = 0; i < DEEPC_LEN; i++){
|
2018-08-14 15:59:55 +00:00
|
|
|
temp = s_id->sr->value[i];
|
2018-07-30 09:45:33 +00:00
|
|
|
for (int i = 0; i < DATA_BIT; i++) buff[i] = 0;
|
|
|
|
string_count = TYPE_LEN + (i) * 5;
|
|
|
|
buff_count = DATA_BIT - 1;
|
|
|
|
while (buff_count >= 0) {
|
|
|
|
buff[buff_count] = temp % 10;
|
|
|
|
temp /= 10;
|
|
|
|
buff_count--;
|
|
|
|
}
|
|
|
|
for(int i = DATA_BIT - 1; i >= 0; i--) string[string_count + i] = buff[i] + 48;
|
|
|
|
}
|
|
|
|
deep_len -= DEEPC_LEN * DATA_BIT;
|
|
|
|
if(deep_len > 0)
|
|
|
|
for(int i = 0; i < DEEPB_LEN; i++){
|
2018-08-14 15:59:55 +00:00
|
|
|
temp = s_id->sr->value_deeper[i];
|
2018-07-30 09:45:33 +00:00
|
|
|
for (int i = 0; i < DATA_BIT; i++) buff[i] = 0;
|
|
|
|
string_count = TYPE_LEN + DEEPC_LEN * DATA_BIT + (i) * DATA_BIT;
|
|
|
|
buff_count = DATA_BIT - 1;
|
|
|
|
while (buff_count >= 0) {
|
|
|
|
buff[buff_count] = temp % 10;
|
|
|
|
temp /= 10;
|
|
|
|
buff_count--;
|
|
|
|
}
|
|
|
|
for(int i = DATA_BIT - 1; i >= 0; i--) string[string_count + i] = buff[i] + 48;
|
|
|
|
}
|
|
|
|
|
|
|
|
deep_len -= DEEPB_LEN * DATA_BIT;
|
|
|
|
if(deep_len > 0)
|
|
|
|
for(int i = 0; i < DEEPA_LEN; i++){
|
2018-08-14 15:59:55 +00:00
|
|
|
temp = s_id->sr->value_deepest[i];
|
2018-07-30 09:45:33 +00:00
|
|
|
for (int i = 0; i < DATA_BIT; i++) buff[i] = 0;
|
|
|
|
string_count = TYPE_LEN + (DEEPC_LEN + DEEPB_LEN) * DATA_BIT + (i) * DATA_BIT;
|
|
|
|
buff_count = DATA_BIT - 1;
|
|
|
|
while (buff_count >= 0) {
|
|
|
|
buff[buff_count] = temp % 10;
|
|
|
|
string[string_count] = buff[buff_count] + 48;
|
|
|
|
temp /= 10;
|
|
|
|
buff_count--;
|
|
|
|
}
|
|
|
|
for(int i = DATA_BIT - 1; i >= 0; i--) string[string_count + i] = buff[i] + 48;
|
|
|
|
}
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-08-21 08:26:31 +00:00
|
|
|
SID *asciiRawStringToS_id(char * const string){
|
2018-07-30 09:45:33 +00:00
|
|
|
SID *s_id = NULL;
|
|
|
|
unsigned long long string_len = strlen(string);
|
2018-08-03 10:32:20 +00:00
|
|
|
|
2018-07-30 09:45:33 +00:00
|
|
|
if (string_len == 25) s_id = initS_id(1);
|
|
|
|
else if (string_len == 65) s_id = initS_id(2);
|
|
|
|
else if (string_len == 225) s_id = initS_id(3);
|
|
|
|
else return NULL;
|
|
|
|
|
2018-08-03 10:32:20 +00:00
|
|
|
unsigned int *buff = (unsigned int *) malloc(sizeof(unsigned int) * (string_len));
|
2018-08-03 10:58:49 +00:00
|
|
|
if(buff == NULL){
|
|
|
|
printf("\ns_idToASCIIString(): Error in getting the memory of string.\n");
|
|
|
|
}
|
2018-08-03 10:32:20 +00:00
|
|
|
for(int i = 0; i < string_len; i++){
|
|
|
|
buff[i] = (unsigned int)string[i] - 48;
|
|
|
|
}
|
2018-07-30 09:45:33 +00:00
|
|
|
|
2018-08-14 15:59:55 +00:00
|
|
|
s_id->sr->type = 0;
|
2018-07-30 09:45:33 +00:00
|
|
|
for (int i = 0; i < 5; i++){
|
2018-08-14 15:59:55 +00:00
|
|
|
s_id->sr->type += buff[i] ;
|
|
|
|
s_id->sr->type *= 10u;
|
2018-07-30 09:45:33 +00:00
|
|
|
}
|
2018-08-14 15:59:55 +00:00
|
|
|
s_id->sr->type /= 10u;
|
2018-07-30 09:45:33 +00:00
|
|
|
|
|
|
|
if (string_len >= 25){
|
|
|
|
for(int i = 0; i < DEEPC_LEN; i++){
|
2018-08-14 15:59:55 +00:00
|
|
|
s_id->sr->value[i] = 0;
|
2018-07-30 09:45:33 +00:00
|
|
|
for (int j = 0; j < 5; j++){
|
2018-08-14 15:59:55 +00:00
|
|
|
s_id->sr->value[i] += (unsigned int)buff[5 + i * 5 + j];
|
|
|
|
if(j < 4) s_id->sr->value[i] *= 10;
|
2018-07-30 09:45:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (string_len >= 65){
|
|
|
|
for(int i = 0; i < DEEPB_LEN; i++){
|
2018-08-14 15:59:55 +00:00
|
|
|
s_id->sr->value_deeper[i] = 0;
|
2018-07-30 09:45:33 +00:00
|
|
|
for (int j = 0; j < 5; j++){
|
2018-08-14 15:59:55 +00:00
|
|
|
s_id->sr->value_deeper[i] += buff[25 + i * 5 + j];
|
|
|
|
if(j < 4) s_id->sr->value_deeper[i] *= 10;
|
2018-07-30 09:45:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (string_len >= 225){
|
|
|
|
for(int i = 0; i < DEEPA_LEN; i++){
|
2018-08-14 15:59:55 +00:00
|
|
|
s_id->sr->value_deepest[i] = 0;
|
2018-07-30 09:45:33 +00:00
|
|
|
for (int j = 0; j < 5; j++){
|
2018-08-14 15:59:55 +00:00
|
|
|
s_id->sr->value_deepest[i] += buff[65 + i * 5 + j];
|
|
|
|
if(j < 4) s_id->sr->value_deepest[i] *= 10;
|
2018-07-30 09:45:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-03 03:38:19 +00:00
|
|
|
free(buff);
|
2018-07-30 09:45:33 +00:00
|
|
|
return s_id;
|
|
|
|
}
|
2018-07-30 10:07:24 +00:00
|
|
|
|
|
|
|
int freeS_id(SID *s_id){
|
2018-08-21 08:26:31 +00:00
|
|
|
if(s_id == NULL) return 0;
|
|
|
|
if(s_id->decrypt_hex != NULL) free(s_id->decrypt_hex);
|
2018-08-14 15:59:55 +00:00
|
|
|
freeSidRaw(s_id);
|
|
|
|
if (s_id->md5 != NULL) free(s_id->md5);
|
2018-08-21 08:26:31 +00:00
|
|
|
free(s_id->decrypt_str);
|
2018-07-30 10:07:24 +00:00
|
|
|
free(s_id);
|
|
|
|
return 0;
|
|
|
|
}
|
2018-08-14 15:59:55 +00:00
|
|
|
|
|
|
|
int freeSidRaw(SID *s_id){
|
2018-08-21 08:26:31 +00:00
|
|
|
if (s_id != NULL && s_id->sr != NULL){
|
2018-08-14 15:59:55 +00:00
|
|
|
if(s_id->sr->value != NULL){
|
|
|
|
free(s_id->sr->value);
|
|
|
|
s_id->sr->value = NULL;
|
|
|
|
}
|
|
|
|
if(s_id->sr->value_deeper != NULL){
|
|
|
|
free(s_id->sr->value_deeper);
|
|
|
|
s_id->sr->value_deeper = NULL;
|
|
|
|
}
|
|
|
|
if(s_id->sr->value_deepest != NULL){
|
|
|
|
free(s_id->sr->value_deepest);
|
|
|
|
s_id->sr->value_deepest = NULL;
|
|
|
|
}
|
2018-08-21 08:26:31 +00:00
|
|
|
free(s_id->sr);
|
|
|
|
s_id->sr = NULL;
|
2018-08-14 15:59:55 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void s_idToMD5(SID *s_id){
|
|
|
|
if(s_id->md5 == NULL) s_id->md5 = malloc(sizeof(MD5_CTX));
|
|
|
|
char *sid_string = s_idToASCIIRawString(s_id);
|
2018-08-21 08:26:31 +00:00
|
|
|
s_id->decrypt_hex = malloc(sizeof(unsigned char) * 16);
|
|
|
|
//printf("%s\n",sid_string);
|
2018-08-14 15:59:55 +00:00
|
|
|
MD5Init(s_id->md5);
|
|
|
|
MD5Update(s_id->md5, (unsigned char *) sid_string, strlen(sid_string));
|
|
|
|
MD5Final(s_id->md5, s_id->decrypt_hex);
|
|
|
|
freeSidRaw(s_id);
|
2018-08-21 08:26:31 +00:00
|
|
|
free(sid_string);
|
2018-08-14 15:59:55 +00:00
|
|
|
}
|
|
|
|
|
2018-08-21 08:26:31 +00:00
|
|
|
char hexToChar(unsigned int n){
|
2018-08-14 15:59:55 +00:00
|
|
|
switch (n) {
|
|
|
|
case 1:return '1';
|
|
|
|
case 2:return '2';
|
|
|
|
case 3:return '3';
|
|
|
|
case 4:return '4';
|
|
|
|
case 5:return '5';
|
|
|
|
case 6:return '6';
|
|
|
|
case 7:return '7';
|
|
|
|
case 8:return '8';
|
|
|
|
case 9:return '9';
|
|
|
|
case 10:return 'a';
|
|
|
|
case 11:return 'b';
|
|
|
|
case 12:return 'c';
|
|
|
|
case 13:return 'd';
|
|
|
|
case 14:return 'e';
|
|
|
|
case 15:return 'f';
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return '0';
|
|
|
|
}
|
2018-08-17 08:58:15 +00:00
|
|
|
|
|
|
|
SID *copyS_id(SID *f_sid){
|
|
|
|
SID *s_sid = initS_id(f_sid->deep);
|
2018-08-21 08:26:31 +00:00
|
|
|
if(s_sid->decrypt_str == NULL) s_sid->decrypt_str = malloc(sizeof(char) * SID_LEN);
|
|
|
|
if (f_sid->decrypt_str != NULL) strcpy(s_sid->decrypt_str, f_sid->decrypt_str);
|
|
|
|
else{
|
|
|
|
freeS_id(s_sid);
|
|
|
|
return NULL;
|
|
|
|
};
|
|
|
|
return s_sid;
|
|
|
|
}
|
|
|
|
|
|
|
|
SID *setS_idWithString(char *p_string){
|
|
|
|
if(p_string == NULL) return NULL;
|
|
|
|
SID *p_sid = initS_id(0);
|
|
|
|
if(p_sid->decrypt_str == NULL) p_sid->decrypt_str = malloc(sizeof(char) * SID_LEN);
|
|
|
|
strcpy(p_sid->decrypt_str, p_string);
|
|
|
|
return p_sid;
|
2018-08-17 08:58:15 +00:00
|
|
|
}
|