libqaeda

Unnamed repository; edit this file 'description' to name the repository.
Info | Log | Files | Refs | README | LICENSE

trust.c (1459B)


      1 #include <stdio.h>
      2 
      3 #include "lq/trust.h"
      4 #include "lq/store.h"
      5 #include "lq/err.h"
      6 #include "lq/mem.h"
      7 
      8 
      9 int lq_trust_check(LQPubKey *pubkey, LQStore *store, enum trust_mode_e mode, const unsigned char *flags) {
     10 	int r;
     11 	size_t l;
     12 	int i;
     13 	int ii;
     14 	unsigned char m;
     15 	int match;
     16 	int match_req;
     17 	unsigned char v[3];
     18 	double z;
     19 	unsigned char key_flags[(int)((LQ_TRUST_FLAG_BITS - 1)/8+1)];
     20 	char *keydata;
     21 	size_t keylen;
     22 
     23 	lq_set(v, 0, 3);
     24 	l = (int)((LQ_TRUST_FLAG_BITS - 1)/8+1);
     25 	//r = store->get(LQ_CONTENT_KEY, store, pubkey->lokey, pubkey->lolen, key_flags, &l);
     26 	keylen = lq_publickey_bytes(pubkey, &keydata);
     27 	//r = store->get(LQ_CONTENT_KEY, store, keydata, keylen, (char*)key_flags, &l);
     28 	r = store->get(LQ_CONTENT_KEY_PUBLIC, store, keydata, keylen, (char*)key_flags, &l);
     29 	if (r != ERR_OK) {
     30 		return -1;
     31 	}
     32 
     33 	if (mode == TRUST_MATCH_NONE || LQ_TRUST_FLAG_BITS == 0) {
     34 		return 1000000;
     35 	}
     36 
     37 	match = 0;
     38 	match_req = 0;
     39 	z = 0;
     40 	m = 0;
     41 	ii = 0;
     42 
     43 	for (i = 0; i < LQ_TRUST_FLAG_BITS; i++) {
     44 		if (m == 0) {
     45 			v[1] = *(flags + ii);
     46 			v[2] = key_flags[ii];
     47 			m = 0x80;
     48 			ii++;
     49 		}
     50 		v[0] = v[1] & m;
     51 		if (v[0] > 0) {
     52 			match_req++;
     53 			if ((v[2] & m) > 0) {
     54 				match++;
     55 			}
     56 		}
     57 		if (match > 0) {
     58 			if (mode == TRUST_MATCH_ONE) {
     59 				return 1000000;
     60 			}
     61 		}
     62 		m >>= 1;
     63 	}
     64 	if (mode == TRUST_MATCH_ALL) { 
     65 		if (match != match_req) {
     66 			return 0;
     67 		}
     68 		return 1000000;
     69 	}
     70 	z = (double)match / (double)match_req;
     71 	return (int)(z * 1000000);
     72 }