trust.c (1371B)
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 if (r != ERR_OK) { 29 return -1; 30 } 31 32 if (mode == TRUST_MATCH_NONE || LQ_TRUST_FLAG_BITS == 0) { 33 return 1000000; 34 } 35 36 match = 0; 37 match_req = 0; 38 z = 0; 39 m = 0; 40 ii = 0; 41 42 for (i = 0; i < LQ_TRUST_FLAG_BITS; i++) { 43 if (m == 0) { 44 v[1] = *(flags + ii); 45 v[2] = key_flags[ii]; 46 m = 0x80; 47 ii++; 48 } 49 v[0] = v[1] & m; 50 if (v[0] > 0) { 51 match_req++; 52 if ((v[2] & m) > 0) { 53 match++; 54 } 55 } 56 if (match > 0) { 57 if (mode == TRUST_MATCH_ONE) { 58 return 1000000; 59 } 60 } 61 m >>= 1; 62 } 63 if (mode == TRUST_MATCH_ALL) { 64 if (match != match_req) { 65 return 0; 66 } 67 return 1000000; 68 } 69 z = (double)match / (double)match_req; 70 return (int)(z * 1000000); 71 }