bool coind_submitgetauxblock(YAAMP_COIND *coind, const char *hash, const char *block) { int paramlen = strlen(block); char *params = (char *)malloc(paramlen+1024); if(!params) return false; sprintf(params, "[\"%s\",\"%s\"]", hash, block); json_value *json = rpc_call(&coind->rpc, "getauxblock", params); free(params); if(!json) return false; json_value *json_error = json_get_object(json, "error"); if(json_error && json_error->type != json_null) { const char *p = json_get_string(json_error, "message"); if(p) stratumlog("ERROR %s %s\n", coind->name, p); // job_reset(); json_value_free(json); return false; } json_value *json_result = json_get_object(json, "result"); bool b = json_result && json_result->type == json_boolean && json_result->u.boolean; json_value_free(json); return b; }
int entrypoint () { int i; int32_t type, obj, objarr, objit, arrlen, strlen; char str[STR_MAXLEN]; /* check is json is available, alerts on inactive (optional) */ if (!json_is_active()) { return -1; } /* acquire array of internal contained objects */ objarr = json_get_object("ContainedObjects", 16, 0); type = json_get_type(objarr); /* debug print uint (no '\n' or prepended message */ debug_print_uint(type); if (type != JSON_TYPE_ARRAY) { return -1; } /* check array length for iteration over elements */ arrlen = json_get_array_length(objarr); for (i = 0; i < arrlen; ++i) { /* acquire json object @ idx i */ objit = json_get_array_idx(i, objarr); if (objit <= 0) continue; /* acquire FileType object of the array element @ idx i */ obj = json_get_object("FileType", 8, objit); if (obj <= 0) continue; /* acquire and check type */ type = json_get_type(obj); if (type == JSON_TYPE_STRING) { /* acquire string length, note +1 is for the NULL terminator */ strlen = json_get_string_length(obj)+1; /* prevent buffer overflow */ if (strlen > STR_MAXLEN) strlen = STR_MAXLEN; /* acquire string data, note strlen includes NULL terminator */ if (json_get_string(str, strlen, obj)) { /* debug print str (with '\n' and prepended message */ debug_print_str(str,strlen); /* check the contained object's type */ if (strlen == 14 && !memcmp(str, "CL_TYPE_MSEXE", 14)) { //if (!strcmp(str, strlen, "CL_TYPE_MSEXE", strlen)) { /* alert for submission */ foundVirus("EmbedPE"); return 0; } } } } return 0; }
YAAMP_JOB_TEMPLATE *coind_create_template_memorypool(YAAMP_COIND *coind) { json_value *json = rpc_call(&coind->rpc, "getmemorypool"); if(!json || json->type == json_null) { coind_error(coind, "getmemorypool"); return NULL; } json_value *json_result = json_get_object(json, "result"); if(!json_result || json_result->type == json_null) { coind_error(coind, "getmemorypool"); json_value_free(json); return NULL; } YAAMP_JOB_TEMPLATE *templ = new YAAMP_JOB_TEMPLATE; memset(templ, 0, sizeof(YAAMP_JOB_TEMPLATE)); templ->created = time(NULL); templ->value = json_get_int(json_result, "coinbasevalue"); // templ->height = json_get_int(json_result, "height"); sprintf(templ->version, "%08x", (unsigned int)json_get_int(json_result, "version")); sprintf(templ->ntime, "%08x", (unsigned int)json_get_int(json_result, "time")); strcpy(templ->nbits, json_get_string(json_result, "bits")); strcpy(templ->prevhash_hex, json_get_string(json_result, "previousblockhash")); json_value_free(json); json = rpc_call(&coind->rpc, "getinfo", "[]"); if(!json || json->type == json_null) { coind_error(coind, "coind_getinfo"); return NULL; } json_result = json_get_object(json, "result"); if(!json_result || json_result->type == json_null) { coind_error(coind, "coind_getinfo"); json_value_free(json); return NULL; } templ->height = json_get_int(json_result, "blocks")+1; json_value_free(json); if(coind->isaux) coind_getauxblock(coind); coind->usememorypool = true; return templ; }
void coind_getauxblock(YAAMP_COIND *coind) { if(!coind->isaux) return; json_value *json = rpc_call(&coind->rpc, "getauxblock", "[]"); if(!json) { coind_error(coind, "coind_getauxblock"); return; } json_value *json_result = json_get_object(json, "result"); if(!json_result) { coind_error(coind, "coind_getauxblock"); return; } // coind->aux.height = coind->height+1; coind->aux.chainid = json_get_int(json_result, "chainid"); const char *p = json_get_string(json_result, "target"); if(p) strcpy(coind->aux.target, p); p = json_get_string(json_result, "hash"); if(p) strcpy(coind->aux.hash, p); // if(strcmp(coind->symbol, "UNO") == 0) // { // string_be1(coind->aux.target); // string_be1(coind->aux.hash); // } json_value_free(json); }
bool coind_validate_address(YAAMP_COIND *coind) { if(!coind->wallet[0]) return false; char params[YAAMP_SMALLBUFSIZE]; sprintf(params, "[\"%s\"]", coind->wallet); json_value *json = rpc_call(&coind->rpc, "validateaddress", params); if(!json) return false; json_value *json_result = json_get_object(json, "result"); if(!json_result) { json_value_free(json); return false; } bool isvalid = json_get_bool(json_result, "isvalid"); if(!isvalid) stratumlog("%s wallet %s is not valid.\n", coind->name, coind->wallet); bool ismine = json_get_bool(json_result, "ismine"); if(!ismine) stratumlog("%s wallet %s is not mine.\n", coind->name, coind->wallet); const char *p = json_get_string(json_result, "pubkey"); if(p) strcpy(coind->pubkey, p); json_value_free(json); base58_decode(coind->wallet, coind->script_pubkey); return isvalid && ismine; }
void print_bitcoin_ticker(struct json_value *ticker) { struct hashtable *rootobj = json_get_object(ticker); struct hashtable_iterator iter; hashtable_iterator_init(&iter, rootobj); const char *key; struct json_value *val; while (hashtable_iterator_next(&iter, (void**)&key, (void **)&val)) { printf(" %s (%s) / BTC: %.2lf\n", key, JSON_GET(JSON_OBJECT_LOOKUP(val, "symbol"), const char*), JSON_GET(JSON_OBJECT_LOOKUP(val, "15m"), double)); } }
YAAMP_JOB_TEMPLATE *coind_create_template(YAAMP_COIND *coind) { if(coind->usememorypool) return coind_create_template_memorypool(coind); char params[4*1024] = "[{}]"; if(!strcmp(coind->symbol, "PPC")) strcpy(params, "[]"); json_value *json = rpc_call(&coind->rpc, "getblocktemplate", params); if(!json || json->type == json_null) { coind_error(coind, "getblocktemplate"); return NULL; } json_value *json_result = json_get_object(json, "result"); if(!json_result || json_result->type == json_null) { coind_error(coind, "getblocktemplate"); json_value_free(json); return NULL; } json_value *json_tx = json_get_array(json_result, "transactions"); if(!json_tx) { coind_error(coind, "getblocktemplate"); json_value_free(json); return NULL; } json_value *json_coinbaseaux = json_get_object(json_result, "coinbaseaux"); if(!json_coinbaseaux) { coind_error(coind, "getblocktemplate"); json_value_free(json); return NULL; } YAAMP_JOB_TEMPLATE *templ = new YAAMP_JOB_TEMPLATE; memset(templ, 0, sizeof(YAAMP_JOB_TEMPLATE)); templ->created = time(NULL); templ->value = json_get_int(json_result, "coinbasevalue"); templ->height = json_get_int(json_result, "height"); sprintf(templ->version, "%08x", (unsigned int)json_get_int(json_result, "version")); sprintf(templ->ntime, "%08x", (unsigned int)json_get_int(json_result, "curtime")); strcpy(templ->nbits, json_get_string(json_result, "bits")); strcpy(templ->prevhash_hex, json_get_string(json_result, "previousblockhash")); strcpy(templ->flags, json_get_string(json_coinbaseaux, "flags")); // debuglog("%s ntime %s\n", coind->symbol, templ->ntime); // uint64_t target = decode_compact(json_get_string(json_result, "bits")); // coind->difficulty = target_to_diff(target); // string_lower(templ->ntime); // string_lower(templ->nbits); // char target[1024]; // strcpy(target, json_get_string(json_result, "target")); // uint64_t coin_target = decode_compact(templ->nbits); // debuglog("%s\n", templ->nbits); // debuglog("%s\n", target); // debuglog("0000%016llx\n", coin_target); if(coind->isaux) { json_value_free(json); coind_getauxblock(coind); return templ; } ////////////////////////////////////////////////////////////////////////////////////////// vector<string> txhashes; txhashes.push_back(""); for(int i = 0; i < json_tx->u.array.length; i++) { const char *p = json_get_string(json_tx->u.array.values[i], "hash"); char hash_be[1024]; memset(hash_be, 0, 1024); string_be(p, hash_be); txhashes.push_back(hash_be); const char *d = json_get_string(json_tx->u.array.values[i], "data"); templ->txdata.push_back(d); } templ->txmerkles[0] = 0; templ->txcount = txhashes.size(); templ->txsteps = merkle_steps(txhashes); vector<string>::const_iterator i; for(i = templ->txsteps.begin(); i != templ->txsteps.end(); ++i) sprintf(templ->txmerkles + strlen(templ->txmerkles), "\"%s\",", (*i).c_str()); if(templ->txmerkles[0]) templ->txmerkles[strlen(templ->txmerkles)-1] = 0; // debuglog("merkle transactions %d [%s]\n", templ->txcount, templ->txmerkles); ser_string_be2(templ->prevhash_hex, templ->prevhash_be, 8); if(!coind->pos) coind_aux_build_auxs(templ); coinbase_create(coind, templ, json_result); json_value_free(json); return templ; }
static void linda_store_handler(void *p1, const uuid *u, const void *_s, int len) { linda *l = (linda*)p1; const char *s = (const char*)_s; if (len > 0) // add { json *j = json_open(s); json *j1 = json_get_object(j); json *jid = json_find(j1, LINDA_ID); if (!jid) return; if (json_is_integer(jid)) { long long k = json_get_integer(jid); if (l->sl && !l->is_int) { printf("linda_store_handler: expected integer id\n"); return; } if (!l->sl) { l->sl = sb_int_uuid_create2(); l->is_int = 1; } sb_int_uuid_set(l->sl, k, u); } else if (json_is_string(jid)) { const char *k = json_get_string(jid); if (l->sl && !l->is_string) { printf("linda_store_handler: expected string id\n"); return; } if (!l->sl) { l->sl = sb_string_uuid_create2(); l->is_string = 1; } sb_string_uuid_set(l->sl, k, u); } json_close(j); } else if (len < 0) // remove (with hint) { json *j = json_open(s); json *j1 = json_get_object(j); json *jid = json_find(j1, LINDA_ID); if (!jid) return; if (json_is_integer(jid)) { long long k = json_get_integer(jid); if (l->sl && !l->is_int) { printf("linda_out: expected integer id\n"); return; } if (!l->sl) { l->sl = sb_int_uuid_create2(); l->is_int = 1; } sb_int_uuid_erase(l->sl, k, u); } else if (json_is_string(jid)) { const char *k = json_get_string(jid); if (l->sl && !l->is_string) { printf("linda_out: expected string id\n"); return; } if (!l->sl) { l->sl = sb_string_uuid_create2(); l->is_string = 1; } sb_string_uuid_erase(l->sl, k, u); } json_close(j); } else // remove (brute search) { sb_uuid_efface(l->sl, u); } }
int linda_rm(hlinda *h, const char *s) { json *j = json_open(s); json *j1 = json_get_object(j); int is_int = 0, is_string = 0; const char *string_id = NULL; long long int_id = 0; uuid u; json *joid = json_find(j1, LINDA_OID); if (!joid) { json_close(j); return 0; } uuid_from_string(json_get_string(joid), &u); json *jid = json_find(j1, LINDA_ID); if (jid) { if (h->l->is_int && !json_is_integer(jid)) { printf("linda_read: expected integer id\n"); json_close(j); return 0; } else if (h->l->is_string && !json_is_string(jid)) { printf("linda_read: expected string id\n"); json_close(j); return 0; } if (json_is_integer(jid)) { int_id = json_get_integer(jid); is_int = 1; } else if (json_is_string(jid)) { string_id = json_get_string(jid); json_close(h->jquery); is_string = 1; } else { json_close(j); return 0; } } json_close(j); if (is_int) { char tmpbuf[1024]; int tmplen = sprintf(tmpbuf, "{\"%s\":%lld}\n", LINDA_ID, int_id); store_hrem2(h->hst, &u, tmpbuf, tmplen); sb_int_uuid_erase(h->l->sl, int_id, &u); } else if (is_string) { char tmpbuf[1024], tmpbuf2[1024]; json_format_string(string_id, tmpbuf2, sizeof(tmpbuf2)); int tmplen = sprintf(tmpbuf, "{\"%s\":\"%s\"}\n", LINDA_ID, tmpbuf2); store_hrem2(h->hst, &u, tmpbuf, tmplen); sb_string_uuid_erase(h->l->sl, string_id, &u); } else { store_rem(h->l->st, &u); sb_uuid_efface(h->l->sl, &u); } return 1; }
int linda_out(hlinda *h, const char *s) { if (!h) return 0; json *j = json_open(s); json *j1 = json_get_object(j); json *joid = json_find(j1, LINDA_OID); uuid u; if (joid) { uuid_from_string(json_get_string(joid), &u); } else uuid_gen(&u); json *jid = json_find(j1, LINDA_ID); if (jid) { if (json_is_integer(jid)) { long long k = json_get_integer(jid); if (h->l->sl && !h->l->is_int) { printf("linda_out: expected integer id\n"); return 0; } if (!h->l->sl) { h->l->sl = sb_int_uuid_create2(); h->l->is_int = 1; } sb_int_uuid_set(h->l->sl, k, &u); } else if (json_is_string(jid)) { const char *k = json_get_string(jid); if (h->l->sl && !h->l->is_string) { printf("linda_out: expected string id\n"); return 0; } if (!h->l->sl) { h->l->sl = sb_string_uuid_create2(); h->l->is_string = 1; } sb_string_uuid_set(h->l->sl, k, &u); } } store_hadd(h->hst, &u, s, strlen(s)); h->last_oid = u; json_close(j); return 0; }
static int linda_read(hlinda *h, const char *s, const char **buf, int rm, int nowait) { json *j = json_open(s); json *j1 = json_get_object(j); h->oid.u1 = h->oid.u2 = 0; int is_int = 0, is_string = 0; json *jid = json_find(j1, LINDA_ID); if (jid) { if (h->l->is_int && !json_is_integer(jid)) { printf("linda_read: expected integer id\n"); json_close(j); return 0; } else if (h->l->is_string && !json_is_string(jid)) { printf("linda_read: expected string id\n"); json_close(j); return 0; } if (json_is_integer(jid)) { h->int_id = json_get_integer(jid); h->jquery = json_open(s); sb_int_uuid_find(h->l->sl, h->int_id, &read_int_handler, h); json_close(h->jquery); is_int = 1; } else if (json_is_string(jid)) { h->string_id = json_get_string(jid); h->jquery = json_open(s); sb_string_uuid_find(h->l->sl, h->string_id, &read_string_handler, h); json_close(h->jquery); is_string = 1; } else { json_close(j); return 0; } } else { h->jquery = json_open(s); sb_iter(h->l->sl, &read_handler, h); json_close(h->jquery); } json_close(j); if (!h->oid.u1 && !h->oid.u2) return 0; if (rm) { if (is_int) { char tmpbuf[1024]; int tmplen = sprintf(tmpbuf, "{\"%s\":%lld}\n", LINDA_ID, h->int_id); store_hrem2(h->hst, &h->oid, tmpbuf, tmplen); sb_int_uuid_erase(h->l->sl, h->int_id, &h->oid); } else if (is_string) { char tmpbuf[1024], tmpbuf2[1024]; json_format_string(h->string_id, tmpbuf2, sizeof(tmpbuf2)); int tmplen = sprintf(tmpbuf, "{\"%s\":\"%s\"}\n", LINDA_ID, tmpbuf2); store_hrem2(h->hst, &h->oid, tmpbuf, tmplen); sb_string_uuid_erase(h->l->sl, h->string_id, &h->oid); } else { store_hrem(h->hst, &h->oid); sb_uuid_efface(h->l->sl, &h->oid); } } *buf = h->dst; return 1; }
static int read_handler(void *arg, void *k, void *v) { hlinda *h = (hlinda*)arg; uuid *u = (uuid*)v; if (!store_get(h->l->st, u, (void**)&h->dst, &h->len)) return 0; int match = 1; json *j1 = json_get_object(h->jquery); json *jdst = json_open(h->dst); json *j2 = json_get_object(jdst); size_t i, cnt = json_count(j1); for (i = 0; i < cnt; i++) { json *j1it = json_index(j1, i); const char *name = json_get_string(j1it); if (name[0] == '$') continue; json *j2it = json_find(j2, name); if (!j2it) { match = 0; continue; } if (json_is_integer(j1it)) { if (!json_is_integer(j2it)) { match = 0; break; } if (json_get_integer(j1it) != json_get_integer(j2it)) { match = 0; break; } } else if (json_is_real(j1it)) { if (!json_is_real(j2it)) { match = 0; break; } if (json_get_real(j1it) != json_get_real(j2it)) { match = 0; break; } } else if (json_is_string(j1it)) { if (!json_is_string(j2it)) { match = 0; break; } if (strcmp(json_get_string(j1it), json_get_string(j2it))) { match = 0; break; } } else if (json_is_true(j1it)) { if (!json_is_true(j2it)) { match = 0; break; } } else if (json_is_false(j1it)) { if (!json_is_false(j2it)) { match = 0; break; } } else if (json_is_null(j1it)) { if (!json_is_null(j2it)) { match = 0; break; } } } json_close(jdst); if (!match) return 1; h->oid.u1 = u->u1; h->oid.u2 = u->u2; return 0; }
int test_basic (void) { json_t *root; json_t *o; json_t *i; char *str; const char *s; int n; double d; int boolval; str = " {\"l1\" : 1111, " " \"l2\" : {\"l3\": {\"l4\": \"s2\", \"l5\": 2222 } }, " " \"l6\" : [1,2,3,4,5], " " \"l7\" : { \"l8\" : \"Hello World\" }, " " \"float\": 273.93," " \"b1\": trUe," " \"nil\": nulL," " \"b2\": false} \n\t"; str = strdup (str); root = json_from_string (str, NULL); assert (root); n = json_get_int (root, "l1", NULL); assert (n == 1111); d = json_get_double (root, "float", NULL); assert (d == 273.93); i = json_get_array (root, "l6", NULL); assert (i->type == JSON_TYPE_ARRAY); i = json_get_first (i); for (n = 0; i; n++, i = i->next) { int data[] = {1,2,3,4,5}; assert (i->type == JSON_TYPE_INT); assert (i->val.num == data[n]); } o = json_get (root, "nil", NULL); assert (o->type == JSON_TYPE_NULL); o = json_get (root, "b1", NULL); assert (o->type == JSON_TYPE_TRUE); o = json_get (root, "b2", NULL); assert (o->type == JSON_TYPE_FALSE); boolval = json_get_bool (root, "b1", NULL); assert (boolval == 1); boolval = json_get_bool (root, "b2", NULL); assert (boolval == 0); boolval = json_get_bool (root, "nonexisting", NULL); assert (boolval == -1); o = json_get_object (root, "l7", NULL); assert (o); if (o) { s = json_get_string (o, "l8", NULL); printf ("str = %s\n", s); } printf ("\n"); json_print (root); printf ("\n"); json_free (root); free (str); return 0; }