json_object * tg_shared_preferences_find_parent_of_leaf(SharedPreferences* thiz,const CHAR* key_path,CHAR** leaf_key) { struct json_object *jso=NULL; struct array_list* key_list = NULL; INT32 key_list_len = 0; INT32 idx = 0; return_val_if_fail((thiz&&key_path),NULL); key_list = array_list_new(tg_shared_preferences_key_free); return_val_if_fail((key_list),NULL); return_val_if_fail(tg_shared_preferences_parse_keypath(key_path,key_list,&key_list_len),NULL); for (jso=thiz->obj; idx<key_list_len-1; idx++) { jso = json_object_object_get(jso,(CHAR*)array_list_get_idx(key_list,idx)); if (jso==NULL) break; } if (jso!=NULL) { CHAR* key = (CHAR*)array_list_get_idx(key_list,key_list_len-1); //ASSERT(key); *leaf_key = TG_CALLOC((strlen(key)+1),1); strcpy(*leaf_key,key); } array_list_free(key_list); return jso; }
bool Bitmaszyna::getAccountInfo() { struct json_object *json,*jtmp,*jtmp2,*jtmp3; struct array_list *arr,*arr2; int i,j; curl_mutex.lock(); if (makeApiCall("accountInfo",&json,string("nonce=")+to_stringl(getctime()).toStdString())) { if (!fetchData(&json,&jtmp)) return(false); log(string(chunk.memory)+"\n"); json_object_object_get_ex(json,"deposits",&jtmp); arr=json_object_get_array(jtmp); deposits.clear(); for(i=0;i<arr->length;i++) { Deposit d; jtmp2=(struct json_object *)array_list_get_idx(arr,i); json_object_object_get_ex(jtmp2,"currency",&jtmp3); d.currency=json_object_get_string(jtmp3); json_object_object_get_ex(jtmp2,"bank_name",&jtmp3); d.bank_name=json_object_get_string(jtmp3); json_object_object_get_ex(jtmp2,"pay_to",&jtmp3); d.pay_to=json_object_get_string(jtmp3); json_object_object_get_ex(jtmp2,"acc_num",&jtmp3); d.acc_num=json_object_get_string(jtmp3); json_object_object_get_ex(jtmp2,"swift_code",&jtmp3); d.swift_code=json_object_get_string(jtmp3); json_object_object_get_ex(jtmp2,"transfer_title",&jtmp3); d.transfer_title=json_object_get_string(jtmp3); deposits.push_back(d); } json_object_object_get_ex(json,"withdrawalAccounts",&jtmp); arr=json_object_get_array(jtmp); for(j=0;j<arr->length;j++) { withdrawalaccounts[j].clear(); arr2=json_object_get_array((struct json_object *)array_list_get_idx(arr,j)); for(i=0;i<arr2->length;) { WithdrawalAccount w; jtmp2=(struct json_object *)array_list_get_idx(arr2,i); w.account=json_object_get_string(jtmp2); i++; jtmp2=(struct json_object *)array_list_get_idx(arr2,i); w.name=json_object_get_string(jtmp2); i++; if (w.account.size()>0) withdrawalaccounts[adjustCurrency(j)].push_back(w); } } json_object_put(json); logged=true; curl_mutex.unlock(); return(true); } curl_mutex.unlock(); return(false); }
bool Bitmaszyna::lasttrades(string market) { struct json_object *json,*jtmp,*jtmp2; struct array_list *arr; int i; curl_mutex.lock(); trades.clear(); if (getTickerData(market+"/transactions.json",&json)) { arr=json_object_get_array(json); for(i=0;i<arr->length;i++) { Offer offer; jtmp=(struct json_object *)array_list_get_idx(arr,i); json_object_object_get_ex(jtmp,"date",&jtmp2); offer.time=json_object_get_int64(jtmp2); json_object_object_get_ex(jtmp,"price",&jtmp2); offer.price=json_object_get_double(jtmp2); json_object_object_get_ex(jtmp,"amount",&jtmp2); offer.amount=json_object_get_double(jtmp2); json_object_object_get_ex(jtmp,"type",&jtmp2); offer.type=json_object_get_int(jtmp2); json_object_object_get_ex(jtmp,"type",&jtmp2); offer.id=json_object_get_int(jtmp2); trades.push_back(offer); } json_object_put(json); curl_mutex.unlock(); return(true); } curl_mutex.unlock(); return(false); }
static void tg_shared_preferences_travel_register_list() { INT32 i = 0; INT32 len = 0; CHAR str[200]; Shared_Preferences_Register_Item* item =NULL; struct array_list* list = s_shared_preferences_register_list.list; //sem_wait (&s_shared_preferences_sem); tg_os_WaitSemaphore(s_shared_preferences_sem); len = array_list_length(list); //output_2_console_1( "travel register list"); for (;i<len;i++) { item = (Shared_Preferences_Register_Item*)array_list_get_idx(list, i); if(item) { sprintf(str,"Path:%s; Key:%s; Type:%d;",item->path,item->keys,item->type); // output_2_console_1( str); } } //sem_post (&s_shared_preferences_sem); tg_os_SignalSemaphore(s_shared_preferences_sem); }
static Shared_Preferences_Register_Item* tg_shared_preferences_find_register_item(const CHAR* path,const CHAR* normalize_key,SharedPreferences_Notification_Callback cb,SharedPreferences_WRITE_TYPE type,INT32* idx) { INT32 i = 0; INT32 len = 0; Shared_Preferences_Register_Item* item =NULL; struct array_list* list = s_shared_preferences_register_list.list; return_val_if_fail(path,NULL); return_val_if_fail(list,NULL); //sem_wait (&s_shared_preferences_sem); tg_os_WaitSemaphore(s_shared_preferences_sem); len = array_list_length(list); for (;i<len;i++) { item = (Shared_Preferences_Register_Item*)array_list_get_idx(list, i); if (item==NULL || item->path==NULL ||item->keys==NULL) continue; else if ((item->cb==cb) && (item->type==type) && (strcmp(item->path,path)==0 )&& (strcmp(item->keys,normalize_key)==0 )) { break; } } //sem_post (&s_shared_preferences_sem); tg_os_SignalSemaphore(s_shared_preferences_sem); *idx= i; return i<len?item:NULL; }
std::vector<tagged_feedurl> ttrss_api::get_subscribed_urls() { std::vector<tagged_feedurl> feeds; struct json_object * content = run_op("getCategories", std::map<std::string, std::string>()); if (!content) return feeds; if (json_object_get_type(content) != json_type_array) return feeds; struct array_list * categories = json_object_get_array(content); int catsize = array_list_length(categories); // first fetch feeds within no category fetch_feeds_per_category(NULL, feeds); // then fetch the feeds of all categories for (int i=0;i<catsize;i++) { struct json_object * cat = (struct json_object *)array_list_get_idx(categories, i); fetch_feeds_per_category(cat, feeds); } json_object_put(content); return feeds; }
static int ac_update_configuration_datachannelinterfaces(void* data, void* param) { int i; int mtu; int length; const char* bridge; struct ac_if_datachannel* iface = (struct ac_if_datachannel*)data; struct array_list* interfaces = (struct array_list*)param; /* Search interface */ length = array_list_length(interfaces); for (i = 0; i < length; i++) { struct json_object* jsonvalue = array_list_get_idx(interfaces, i); if (jsonvalue && (json_object_get_type(jsonvalue) == json_type_object)) { struct json_object* jsonindex = compat_json_object_object_get(jsonvalue, "Index"); if (jsonindex && (json_object_get_type(jsonindex) == json_type_int)) { if (iface->index == (unsigned long)json_object_get_int(jsonindex)) { if (!ac_update_configuration_getdatachannel_params(jsonvalue, &mtu, &bridge)) { /* TODO update interface */ } /* Interface found */ array_list_put_idx(interfaces, i, NULL); break; } } } } return ((i == length) ? HASH_DELETE_AND_CONTINUE : HASH_CONTINUE); }
bool Bitmaszyna::marketChart(string market,long starttime,long endtime,long interval) { struct json_object *json,*jtmp2,*jtmp3; struct array_list *arr; int i; curl_mutex.lock(); if (getTickerData(market+"/chartData.json?starttime="+to_stringl(starttime).toStdString()+"&endtime="+to_stringl(endtime).toStdString()+"&interval="+to_stringl(interval).toStdString(),&json)) { base->swieczki.clear(); arr=json_object_get_array(json); for(i=0;i<arr->length;i++) { Ohlc s; jtmp2=(struct json_object *)array_list_get_idx(arr,i); json_object_object_get_ex(jtmp2,"time",&jtmp3); s.t=json_object_get_int64(jtmp3); json_object_object_get_ex(jtmp2,"open",&jtmp3); s.o=json_object_get_double(jtmp3); json_object_object_get_ex(jtmp2,"close",&jtmp3); s.c=json_object_get_double(jtmp3); json_object_object_get_ex(jtmp2,"high",&jtmp3); s.h=json_object_get_double(jtmp3); json_object_object_get_ex(jtmp2,"low",&jtmp3); s.l=json_object_get_double(jtmp3); base->swieczki.push_back(s); } json_object_put(json); curl_mutex.unlock(); return(true); } curl_mutex.unlock(); return(false); }
int CIsa100::MhUnmarshaller::setPublisher( RPCCommand& cmd ) { char * szEUI64, * szConcentrator; struct array_list *pChannels ; cmd.outObj = json_object_new_object(); JSON_GET_MANDATORY_STRING_PTR( "eui64", szEUI64 ); #if defined( RELEASE_ISA ) JSON_GET_MANDATORY_STRING_PTR( "concentrator", szConcentrator ); JSON_GET_MANDATORY_ARRAY( "channels", pChannels ); #elif defined( RELEASE_WHART ) char * szTrigger; JSON_GET_MANDATORY_STRING_PTR( "burst", szConcentrator ); JSON_GET_MANDATORY_ARRAY( "variables", pChannels ); JSON_GET_MANDATORY_STRING_PTR( "trigger", szTrigger ); #endif ChannelsVector oVectorChannels; oVectorChannels.reserve( pChannels->length ); for ( int i=0; i < pChannels->length; ++i ) { oVectorChannels.push_back( json_object_get_string((json_object*)array_list_get_idx( pChannels, i )) ) ; } #if defined( RELEASE_ISA ) int nRet = m_oMh.setPublisher( szEUI64, szConcentrator, &oVectorChannels ); #elif defined( RELEASE_WHART ) int nRet = m_oMh.setPublisher( szEUI64, szConcentrator, &oVectorChannels, szTrigger ); #endif JSON_RETURN_BOOL( nRet, "isa100.mh.setPublisher" ); }
static int extract_field_values(string_list *values, json_object *obj, const string_list *field_path, int value_type) { string_list_item *item = NULL; char string_value[MAX_INT_STR_LEN]; struct array_list *list_val = NULL; json_object *val = NULL; int i, integer_value, boolean_value; int status = U1DB_OK; val = obj; if (val == NULL) goto finish; for (item = field_path->head; item != NULL; item = item->next) { val = json_object_object_get(val, item->data); if (val == NULL) goto finish; } if (json_object_is_type(val, json_type_string) && value_type == json_type_string) { if ((status = append(values, json_object_get_string(val))) != U1DB_OK) goto finish; } else if (json_object_is_type(val, json_type_int) && value_type == json_type_int) { integer_value = json_object_get_int(val); snprintf(string_value, MAX_INT_STR_LEN, "%d", integer_value); if (status != U1DB_OK) goto finish; if ((status = append(values, string_value)) != U1DB_OK) goto finish; } else if (json_object_is_type(val, json_type_boolean) && value_type == json_type_boolean) { boolean_value = json_object_get_boolean(val); if (boolean_value) { status = append(values, "1"); if (status != U1DB_OK) goto finish; } else { status = append(values, "0"); if (status != U1DB_OK) goto finish; } } else if (json_object_is_type(val, json_type_array)) { // TODO: recursively check the types list_val = json_object_get_array(val); for (i = 0; i < list_val->length; i++) { if ((status = append(values, json_object_get_string( array_list_get_idx( list_val, i)))) != U1DB_OK) goto finish; } } finish: return status; }
void CANGenPlugin::parseMappingTable(const std::string& table) { scoped_lock<interprocess_recursive_mutex> lock(mutex); std::string json(table); std::replace(json.begin(), json.end(), '\'', '"');// replace all ' to " std::unique_ptr<json_object, decltype(&json_object_put)> rootobject(json_tokener_parse(json.c_str()), &json_object_put); if(!rootobject) { LOG_ERROR("Failed to parse json: " << json); return; } // Success, use json_obj here. mappingTable.clear(); json_object *sources = json_object_object_get(rootobject.get(),"sources"); if(!sources) return; array_list* arraySources = json_object_get_array(sources); if(!arraySources) return; for(int i=0; i < array_list_length(arraySources); ++i) { json_object *rootsource = static_cast<json_object*>(array_list_get_idx(arraySources,i)); if(!rootsource) continue; json_object* source = json_object_object_get(rootsource, "source"); if(!source) continue; json_object* guid = json_object_object_get(source, "guid"); const std::string guidstr(guid ? json_object_get_string(guid) : ""); json_object* signals = json_object_object_get(rootsource, "signals"); if(!signals) continue; array_list* arraySignals = json_object_get_array(signals); for(int j = 0; j < array_list_length(arraySignals); ++j) { json_object *signal = static_cast<json_object*>(array_list_get_idx(arraySignals,j)); if(!signal) continue; mappingTable.addProperty(guidstr, signal); }// signals array loop }// sources array loop }
void ttrss_api::fetch_feeds_per_category(struct json_object * cat, std::vector<tagged_feedurl>& feeds) { const char * cat_name = NULL; struct json_object * cat_title_obj = NULL; int cat_id; if (cat) { struct json_object * cat_id_obj = json_object_object_get(cat, "id"); cat_id = json_object_get_int(cat_id_obj); // ignore special categories, for now if(cat_id < 0) return; cat_title_obj = json_object_object_get(cat, "title"); cat_name = json_object_get_string(cat_title_obj); LOG(LOG_DEBUG, "ttrss_api::fetch_feeds_per_category: id = %d title = %s", cat_id, cat_name); } else { // As uncategorized is a category itself (id = 0) and the default value // for a getFeeds is id = 0, the feeds in uncategorized will appear twice return; } std::map<std::string, std::string> args; if (cat) args["cat_id"] = utils::to_string<int>(cat_id); struct json_object * feed_list_obj = run_op("getFeeds", args); if (!feed_list_obj) return; struct array_list * feed_list = json_object_get_array(feed_list_obj); int feed_list_size = array_list_length(feed_list); for (int j=0;j<feed_list_size;j++) { struct json_object * feed = (struct json_object *)array_list_get_idx(feed_list, j); int feed_id = json_object_get_int(json_object_object_get(feed, "id")); const char * feed_title = json_object_get_string(json_object_object_get(feed, "title")); const char * feed_url = json_object_get_string(json_object_object_get(feed, "feed_url")); std::vector<std::string> tags; tags.push_back(std::string("~") + feed_title); if (cat_name) { tags.push_back(cat_name); } feeds.push_back(tagged_feedurl(utils::strprintf("%s#%d", feed_url, feed_id), tags)); // TODO: cache feed_id -> feed_url (or feed_url -> feed_id ?) } json_object_put(feed_list_obj); }
static Shared_Preferences_Register_Item* tg_shared_preferences_get_register_item(INT32 idx) { Shared_Preferences_Register_Item* item =NULL; //sem_wait (&s_shared_preferences_sem); tg_os_WaitSemaphore(s_shared_preferences_sem); item = (Shared_Preferences_Register_Item*)array_list_get_idx(s_shared_preferences_register_list.list, idx); //sem_post (&s_shared_preferences_sem); tg_os_SignalSemaphore(s_shared_preferences_sem); return item; }
bool OGRAmigoCloudDataSource::ListDatasets() { std::stringstream url; url << std::string(GetAPIURL()) << "/users/0/projects/" << std::string(GetProjectId()) << "/datasets/?summary"; json_object* result = RunGET(url.str().c_str()); if( result == nullptr ) { CPLError(CE_Failure, CPLE_AppDefined, "AmigoCloud:get failed."); return false; } if( result != nullptr ) { auto type = json_object_get_type(result); if(type == json_type_object) { json_object *poResults = CPL_json_object_object_get(result, "results"); if(poResults != nullptr) { array_list *res = json_object_get_array(poResults); if(res != nullptr) { CPLprintf("List of available datasets for project id: %s\n", GetProjectId()); CPLprintf("| id \t | name\n"); CPLprintf("|--------|-------------------\n"); for(decltype(res->length) i = 0; i < res->length; i++) { json_object *ds = (json_object*)array_list_get_idx(res, i); if(ds!=nullptr) { const char *name = nullptr; int64_t dataset_id = 0; json_object *poName = CPL_json_object_object_get(ds, "name"); if (poName != nullptr) { name = json_object_get_string(poName); } json_object *poId = CPL_json_object_object_get(ds, "id"); if (poId != nullptr) { dataset_id = json_object_get_int64(poId); } if (name != nullptr) { std::stringstream str; str << "| " << dataset_id << "\t | " << name; CPLprintf("%s\n", str.str().c_str()); } } } } } } json_object_put(result); } return true; }
static INT32 tg_shared_preferences_get_first_free_slot(array_list* list) { INT32 i = 0; INT32 len = 0; return_val_if_fail(list,-1); len = array_list_length(list); for (;i<len;i++) { if (array_list_get_idx(list, i)==NULL) break; } return i; }
bool Bitmaszyna::getdepth(string market,Tables& tables) { struct json_object *json,*jtmp; struct array_list *arr,*arr2; int i; double sum; curl_mutex.lock(); tables.bids.clear(); tables.asks.clear(); if (getTickerData(market+"/depthSimple.json",&json)) { json_object_object_get_ex(json,"bids",&jtmp); arr=json_object_get_array(jtmp); sum=0.0; for(i=0;i<arr->length;i++) { Offer offer; jtmp=(struct json_object *)array_list_get_idx(arr,i); arr2=json_object_get_array(jtmp); offer.type=BID; offer.price=json_object_get_double((struct json_object *)array_list_get_idx(arr2,0)); offer.amount=json_object_get_double((struct json_object *)array_list_get_idx(arr2,1)); offer.sum=sum; sum+=offer.amount; tables.bids.push_back(offer); } json_object_object_get_ex(json,"asks",&jtmp); arr=json_object_get_array(jtmp); sum=0.0; for(i=0;i<arr->length;i++) { Offer offer; jtmp=(struct json_object *)array_list_get_idx(arr,i); arr2=json_object_get_array(jtmp); offer.type=BID; offer.price=json_object_get_double((struct json_object *)array_list_get_idx(arr2,0)); offer.amount=json_object_get_double((struct json_object *)array_list_get_idx(arr2,1)); offer.sumLower=sum; sum+=offer.amount*offer.price; tables.asks.push_back(offer); } json_object_put(json); curl_mutex.unlock(); return(true); } curl_mutex.unlock(); return(false); }
int read_channels_json(json_object *obj, fm_channel_t **channels, int *number) { int ret = -1; int i; if (obj) { array_list *channel_objs = json_object_get_array(json_object_object_get(obj, "channels")); if (channel_objs) { *number = array_list_length(channel_objs); *channels = (fm_channel_t *) malloc(*number * sizeof(fm_channel_t)); for (i = 0; i < *number; i++) { json_object *o = (json_object*) array_list_get_idx(channel_objs, i); int id = json_object_get_int(json_object_object_get(o, "channel_id")); (*channels)[i].id = id; (*channels)[i].name = strdup(json_object_get_string(json_object_object_get(o, "name"))); } ret = 0; } json_object_put(obj); } return ret; }
void *import_start_thread(void *arg) { struct status *status; struct mbox_data *data; int i; status = (struct status *) arg; DEBUG(1, ("[*] Importing thread started\n")); status->state = STATE_IMPORTING; status->start_time = time(NULL); for (i = 0; i < array_list_length(status->mbox_list); i++) { data = (struct mbox_data *) array_list_get_idx(status->mbox_list, i); // FIXME: first argument should be a TALLOC_CTX *mem_ctx!!! import_mailbox(data, status->local.session, data); } status->state = STATE_IMPORTED; status->end_time = time(NULL); DEBUG(1, ("[*] Importing thread stopped\n")); return NULL; }
/* require interface */ json_object * tg_shared_preferences_find_leaf_obj(SharedPreferences* thiz,const CHAR* key_path) { struct json_object *jso=NULL; struct array_list* key_list = NULL; INT32 key_list_len = 0; INT32 idx = 0; return_val_if_fail((thiz&&key_path),NULL); if (strcmp(key_path,"/")==0) return thiz->obj; key_list = array_list_new(tg_shared_preferences_key_free); return_val_if_fail((key_list),NULL); return_val_if_fail(tg_shared_preferences_parse_keypath(key_path,key_list,&key_list_len),NULL); for (jso=thiz->obj; idx<key_list_len &&jso; idx++) { jso = json_object_object_get(jso,(CHAR*)array_list_get_idx(key_list,idx)); } array_list_free(key_list); return jso; }
static BOOL tg_shared_preferences_find_register_items(const CHAR* path,INT32** idx_list,INT32* idx_list_len) { INT32 i = 0; INT32 len = 0; INT32 count = 0; INT32* found_list= NULL; Shared_Preferences_Register_Item* item =NULL; struct array_list* list = s_shared_preferences_register_list.list; return_val_if_fail(path,FALSE); return_val_if_fail(list,FALSE); //sem_wait (&s_shared_preferences_sem); tg_os_WaitSemaphore(s_shared_preferences_sem); len = array_list_length(list); *idx_list_len=0; for (;i<len;i++) { item = (Shared_Preferences_Register_Item*)array_list_get_idx(list, i); if (item==NULL || item->path==NULL ||item->keys==NULL) continue; else if (strcmp(item->path,path)==0 ) { if (found_list==NULL) found_list=TG_CALLOC_V2(len*sizeof(INT32)); found_list[count]=i; count++; } } //sem_post (&s_shared_preferences_sem); tg_os_SignalSemaphore(s_shared_preferences_sem); if (found_list) { *idx_list = found_list; } *idx_list_len=count; return count>0?TRUE:FALSE; }
static INT32 tg_shared_preferences_find_lock_path(const CHAR* path) { INT32 i = 0; INT32 len = 0; struct array_list* list = s_shared_preferences_lock_list.lock_list; return_val_if_fail(path,-1); return_val_if_fail(list,-1); //sem_wait (&s_shared_preferences_sem); tg_os_WaitSemaphore(s_shared_preferences_sem); len = array_list_length(list); for (;i<len;i++) { CHAR* lock_path = (CHAR*)array_list_get_idx(list, i); if (lock_path==NULL) continue; else if (strcmp(lock_path,path)==0) break; } //sem_post (&s_shared_preferences_sem); tg_os_SignalSemaphore(s_shared_preferences_sem); return i<len?i:-1; }
void DatabaseSink::parseConfig() { json_object *rootobject; json_tokener *tokener = json_tokener_new(); enum json_tokener_error err; do { rootobject = json_tokener_parse_ex(tokener, configuration["properties"].c_str(),configuration["properties"].size()); } while ((err = json_tokener_get_error(tokener)) == json_tokener_continue); if (err != json_tokener_success) { fprintf(stderr, "Error: %s\n", json_tokener_error_desc(err)); } if (tokener->char_offset < configuration["properties"].size()) // XXX shouldn't access internal fields { //Should handle the extra data here sometime... } json_object *propobject = json_object_object_get(rootobject,"properties"); g_assert(json_object_get_type(propobject) == json_type_array); array_list *proplist = json_object_get_array(propobject); for(int i=0; i < array_list_length(proplist); i++) { json_object *idxobj = (json_object*)array_list_get_idx(proplist,i); std::string prop = json_object_get_string(idxobj); propertiesToSubscribeTo.push_back(prop); DebugOut()<<"DatabaseSink logging: "<<prop<<endl; } json_object_put(propobject); json_object_put(rootobject); }
bool Bitmaszyna::getopenorders(string) { struct json_object *json,*jtmp; struct array_list *arr; int i; Offer o; curl_mutex.lock(); orders.clear(); if (makeApiCall("offers",&json,string("nonce=")+to_stringl(getctime()).toStdString())) { if (!fetchData(&json,&jtmp)) return(false); json_object_object_get_ex(json,"orders",&jtmp); arr=json_object_get_array(jtmp); for(i=0;i<arr->length;i++) { struct json_object *jtmp3,*jtmp4; jtmp3=(struct json_object *)array_list_get_idx(arr,i); json_object_object_get_ex(jtmp3,"id",&jtmp4); o.id=to_stringl(json_object_get_int(jtmp4)).toStdString(); json_object_object_get_ex(jtmp3,"price",&jtmp4); o.price=json_object_get_double(jtmp4); json_object_object_get_ex(jtmp3,"amount",&jtmp4); o.amount=json_object_get_double(jtmp4); json_object_object_get_ex(jtmp3,"type",&jtmp4); o.type=json_object_get_double(jtmp4); orders.push_back(o); } json_object_put(json); curl_mutex.unlock(); return(true); } curl_mutex.unlock(); return(false); }
/* *打印JSON object类型的值 *返回类型: none *返回值: none *传入参数json_object *类型 *author mleaf_hexi *mail:[email protected] */ static void json_print_object(json_object *obj) { char *configData="configData"; char *requestId="requestId"; char *delim=":/,";//分割字符串 char *p; char *s; char *name,*value,*next,*value1,*next1,*next2; int i; char buff[50],buff2[50]; if(!obj) return; //遍历json对象的key和值 //Linux内核2.6.29,说明了strtok()已经不再使用,由速度更快的strsep()代替。 json_object_object_foreach(obj,key,val) { //printf("%s => ",key); if(strcmp(key,configData)==0)//取出通讯用url { printf("configData checking out\n"); printf("configData=%s\n",json_object_get_string(val)); s=(char*)json_object_get_string(val); printf("%s\n",s); //wss: //area1.dts.mpush.brxy-cloud.com/websocket/connHandler/v2.0,wss: //primary.dts.mpush.brxy-cloud.com/websocket/connHandler/v2.0 value = strdup(s); for(i=0 ;i<2 ;i++) { // 第一次执行时 name = strsep(&value,":"); // 以":"分割字符串,这时strsep函数返回值为 "wss",即":"号之前的字符串 next =value; // 这时指针value指向":"号后面的字符串,即 //area1.dts.mpush.brxy-cloud.com/websocket/connHandler/v2.0,wss: //primary.dts.mpush.brxy-cloud.com/websocket/connHandler/v2.0 printf(" name= %s\n",name); //打印出一轮分割后name的值 name = strsep(&value,"/");// 这时通过"/"分割字符串 next =value; name = strsep(&value,"/");//去掉第二个/ next =value; if(i==0) { value=strsep(&next,",");// 以","分割字符串,这时strsep函数返回值为 "area1.dts.mpush.brxy-cloud.com/websocket/connHandler/v2.0",即","号之前的字符串 printf("value= %s\n",value); next2 =value; value=strsep(&next2,"/"); printf("value= %s\n",value);//area1.dts.mpush.brxy-cloud.com sprintf(buff2,"/%s",next2); printf("next2= %s\n",buff2);/* /websocket/connHandler/v2.0 */ } if(i==1)//第二轮循环 { value1=strsep(&next,"");// 以/0分割字符串,这时strsep函数返回值为 "primary.dts.mpush.brxy-cloud.com/websocket/connHandler/v2.0",即wss: //之后"/0"之前的字符串 printf("value1= %s\n",value1); next1 =value1; value1=strsep(&next1,"/"); printf("value2= %s\n",value1);//primary.dts.mpush.brxy-cloud.com sprintf(buff,"/%s",next1); printf("next1= %s\n",buff);/* /websocket/connHandler/v2.0 */ } value=next; } // char *source = strdup(s); // char *token; // for(token = strsep(&source, delim); token != NULL; token = strsep(&source, delim)) // { // printf("%s",token); // printf("\n"); // } } else if (strcmp(key,requestId) == 0)//取出requestId 号 { array_list* arr = json_object_get_array(val); json_object* obj = (json_object*)array_list_get_idx(arr,0); printf("requestId checking out\n"); printf("requestId=%s\n", json_object_get_string(obj)); } json_print_value(val); } }
void LoginOutputData::parse(json_object* obj) { if (obj == NULL) return; json_object* node; node = json_object_object_get(obj, "accountid"); this->accountid = json_object_get_int(node); node = json_object_object_get(obj, "nickname"); this->nickname.assign(json_object_get_string(node), json_object_get_string_len(node)); node = json_object_object_get(obj, "gender"); this->gender = json_object_get_int(node); node = json_object_object_get(obj, "birthday"); this->birthday.assign(json_object_get_string(node), json_object_get_string_len(node)); node = json_object_object_get(obj, "rank"); this->rank.assign(json_object_get_string(node), json_object_get_string_len(node)); node = json_object_object_get(obj, "introduction"); this->introduction.assign(json_object_get_string(node), json_object_get_string_len(node)); node = json_object_object_get(obj, "points"); this->points = json_object_get_int(node); node = json_object_object_get(obj, "impactpower"); this->impactpower = json_object_get_int(node); node = json_object_object_get(obj, "listenpower"); this->listenpower = json_object_get_int(node); node = json_object_object_get(obj, "avatar"); this->avatar.assign(json_object_get_string(node), json_object_get_string_len(node)); node = json_object_object_get(obj, "voice"); this->voice.assign(json_object_get_string(node), json_object_get_string_len(node)); node = json_object_object_get(obj, "voicetime"); this->voicetime = json_object_get_int(node); node = json_object_object_get(obj, "viptypeid"); this->viptypeid = json_object_get_int(node); node = json_object_object_get(obj, "vipstarttime"); this->vipstarttime.assign(json_object_get_string(node), json_object_get_string_len(node)); node = json_object_object_get(obj, "vipexpiretime"); this->vipexpiretime.assign(json_object_get_string(node), json_object_get_string_len(node)); node = json_object_object_get(obj, "created"); this->created.assign(json_object_get_string(node), json_object_get_string_len(node)); node = json_object_object_get(obj, "status"); this->status = json_object_get_int(node); node = json_object_object_get(obj, "kind"); this->kind = json_object_get_int(node); node = json_object_object_get(obj, "gold"); this->gold = json_object_get_int(node); node = json_object_object_get(obj, "nativeplace"); this->nativeplace.assign(json_object_get_string(node), json_object_get_string_len(node)); node = json_object_object_get(obj, "occupation"); this->occupation.assign(json_object_get_string(node), json_object_get_string_len(node)); node = json_object_object_get(obj, "dialect"); this->dialect.assign(json_object_get_string(node), json_object_get_string_len(node)); node = json_object_object_get(obj, "devicetoken"); this->devicetoken.assign(json_object_get_string(node), json_object_get_string_len(node)); node = json_object_object_get(obj, "ipowerlevel"); this->ipowerlevel = json_object_get_int(node); node = json_object_object_get(obj, "lpowerlevel"); this->lpowerlevel = json_object_get_int(node); node = json_object_object_get(obj, "isaskpermit"); this->isaskpermit = json_object_get_int(node); node = json_object_object_get(obj, "isagainstpermit"); this->isagainstpermit = json_object_get_int(node); node = json_object_object_get(obj, "giftcount"); this->giftcount = json_object_get_int(node); node = json_object_object_get(obj, "pickuppoints"); this->pickuppoints = json_object_get_int(node); node = json_object_object_get(obj, "levelconfig"); node = json_object_object_get(obj, "isquickuser"); this->isquickuser = json_object_get_int(node); node = json_object_object_get(obj, "isplisttester"); this->isplisttester = json_object_get_int(node); node = json_object_object_get(obj, "clientversion"); this->clientversion.assign(json_object_get_string(node), json_object_get_string_len(node)); node = json_object_object_get(obj, "updateurl"); this->updateurl.assign(json_object_get_string(node), json_object_get_string_len(node)); node = json_object_object_get(obj, "updatedesc"); this->updatedesc.assign(json_object_get_string(node), json_object_get_string_len(node)); node = json_object_object_get(obj, "restoredefaultplist"); this->restoredefaultplist = json_object_get_int(node); node = json_object_object_get(obj, "bindplatformlist"); array_list* array = json_object_get_array(obj); if (array) { int len = array_list_length(array); for (int i = 0; i < len; i++) { json_object *obj = (json_object*)array_list_get_idx(array, i); if (!obj) continue; json_type type = json_object_get_type(obj); if (type == json_type_object) { //* tmp = new (); //tmp.parse(obj); //bindplatformlist.push_back(tmp); } else if (type == json_type_int) { //bindplatformlist.push_back(itoa(json_object_get_int(obj))); } else if (type == json_type_double) { //bindplatformlist.push_back(json_object_get_double(obj)); } else if (type == json_type_string) { string str; bindplatformlist.push_back(str.assign(json_object_get_string(obj), json_object_get_string_len(obj))); } } } node = json_object_object_get(obj, "username"); this->username.assign(json_object_get_string(node), json_object_get_string_len(node)); node = json_object_object_get(obj, "platform"); this->platform.assign(json_object_get_string(node), json_object_get_string_len(node)); node = json_object_object_get(obj, "askpoints1"); this->askpoints1 = json_object_get_int(node); node = json_object_object_get(obj, "askpoints2"); this->askpoints2 = json_object_get_int(node); node = json_object_object_get(obj, "background"); this->background.assign(json_object_get_string(node), json_object_get_string_len(node)); node = json_object_object_get(obj, "pushmessage"); this->pushmessage = json_object_get_int(node); node = json_object_object_get(obj, "lbs"); this->lbs = json_object_get_int(node); node = json_object_object_get(obj, "weixinur"); this->weixinur = json_object_get_int(node); node = json_object_object_get(obj, "activitystatus"); this->activitystatus = json_object_get_int(node); node = json_object_object_get(obj, "shareversions"); this->shareversions = json_object_get_int(node); node = json_object_object_get(obj, "isnewdialect"); this->isnewdialect = json_object_get_int(node); node = json_object_object_get(obj, "vcolor"); this->vcolor = json_object_get_int(node); node = json_object_object_get(obj, "isready"); this->isready = json_object_get_int(node); node = json_object_object_get(obj, "tabidx"); this->tabidx = json_object_get_int(node); }
void LocalePrefsHandler::readRegionFile() { // Read the locale file char* jsonStr = Utils::readFile(s_custRegionFile); if (!jsonStr) jsonStr = Utils::readFile(s_defaultRegionFile); if (!jsonStr) { //luna_critical(s_logChannel, "Failed to load region files: [%s] nor [%s]", s_custRegionFile,s_defaultRegionFile); qCritical() << "Failed to load region files: [" << s_custRegionFile << "] nor [" << s_defaultRegionFile << "]"; return; } json_object* root = 0; json_object* label = 0; array_list* regionArray = 0; root = json_tokener_parse(jsonStr); if (!root || is_error(root)) { //luna_critical(s_logChannel, "Failed to parse region file contents into json"); qCritical() << "Failed to parse region file contents into json"; goto Done; } label = json_object_object_get(root, "region"); if (!label || is_error(label)) { //luna_critical(s_logChannel, "Failed to get region entry from region file"); qCritical() << "Failed to get region entry from region file"; goto Done; } regionArray = json_object_get_array(label); if (!regionArray) { //luna_critical(s_logChannel, "Failed to get region array from region file"); qCritical() << "Failed to get region array from region file"; goto Done; } for (int i = 0; i < array_list_length(regionArray); i++) { json_object* obj = (json_object*) array_list_get_idx(regionArray, i); RegionEntry regionEntry; label = json_object_object_get(obj, "countryName"); if (!label || is_error(label)) continue; regionEntry.region[1] = std::string(json_object_get_string(label)); label = json_object_object_get(obj, "shortCountryName"); if (!label || is_error(label)) regionEntry.region[0] = regionEntry.region[1]; else regionEntry.region[0] = std::string(json_object_get_string(label)); label = json_object_object_get(obj, "countryCode"); if (!label || is_error(label)) continue; regionEntry.region[2] = std::string(json_object_get_string(label)); m_regionEntryList.push_back(regionEntry); } Done: if (root && !is_error(root)) json_object_put(root); delete [] jsonStr; }
void LocalePrefsHandler::readLocaleFile() { // Read the locale file char* jsonStr = Utils::readFile(s_custLocaleFile); if (!jsonStr) jsonStr = Utils::readFile(s_defaultLocaleFile); if (!jsonStr) { //luna_critical(s_logChannel, "Failed to load locale files: [%s] nor [%s]", s_custLocaleFile,s_defaultLocaleFile); qCritical() << "Failed to load locale files: [" << s_custLocaleFile << "] nor [" << s_defaultLocaleFile << "]"; return; } json_object* root = 0; json_object* label = 0; array_list* localeArray = 0; root = json_tokener_parse(jsonStr); if (!root || is_error(root)) { //luna_critical(s_logChannel, "Failed to parse locale file contents into json"); qCritical() << "Failed to parse locale file contents into json"; goto Done; } label = json_object_object_get(root, "locale"); if (!label || is_error(label)) { //luna_critical(s_logChannel, "Failed to get locale entry from locale file"); qCritical() << "Failed to get locale entry from locale file"; goto Done; } localeArray = json_object_get_array(label); if (!localeArray) { //luna_critical(s_logChannel, "Failed to get locale array from locale file"); qCritical() << "Failed to get locale array from locale file"; goto Done; } for (int i = 0; i < array_list_length(localeArray); i++) { json_object* obj = (json_object*) array_list_get_idx(localeArray, i); LocaleEntry localeEntry; array_list* countryArray = 0; label = json_object_object_get(obj, "languageName"); if (!label || is_error(label)) continue; localeEntry.language.first = json_object_get_string(label); label = json_object_object_get(obj, "languageCode"); if (!label || is_error(label)) continue; localeEntry.language.second = json_object_get_string(label); label = json_object_object_get(obj, "countries"); if (!label || is_error(label)) continue; countryArray = json_object_get_array(label); for (int j = 0; j < array_list_length(countryArray); j++) { json_object* countryObj = (json_object*) array_list_get_idx(countryArray, j); NameCodePair country; label = json_object_object_get(countryObj, "countryName"); if (!label || is_error(label)) continue; country.first = json_object_get_string(label); label = json_object_object_get(countryObj, "countryCode"); if (!label || is_error(label)) continue; country.second = json_object_get_string(label); localeEntry.countries.push_back(country); } m_localeEntryList.push_back(localeEntry); } Done: if (root && !is_error(root)) json_object_put(root); delete [] jsonStr; }
int main(int argc, char **argv) { struct json_object *json; struct array_list *tests; struct lh_entry *entry; char *key; struct json_object *val; int i; context ctx; if (argc != 2) { printf("Usage: %s <filename>\n", argv[0]); return 1; } json = json_object_from_file(argv[1]); assert(!is_error(json)); assert(strcmp((char *) ((json_object_get_object(json)->head)->k), "tests") == 0); /* Get array of tests */ tests = json_object_get_array((struct json_object *) (json_object_get_object(json)->head)->v); for (i = 0; i < array_list_length(tests); i++) { /* Get test */ struct json_object *test = (struct json_object *) array_list_get_idx(tests, i); ctx.last_start_tag = NULL; ctx.content_model = NULL; ctx.process_cdata = false; /* Extract settings */ for (entry = json_object_get_object(test)->head; entry; entry = entry->next) { key = (char *) entry->k; val = (struct json_object *) entry->v; if (strcmp(key, "description") == 0) { printf("Test: %s\n", json_object_get_string(val)); } else if (strcmp(key, "input") == 0) { ctx.input = (const uint8_t *) json_object_get_string(val); ctx.input_len = json_object_get_string_len(val); } else if (strcmp(key, "output") == 0) { ctx.output = json_object_get_array(val); ctx.output_index = 0; ctx.char_off = 0; } else if (strcmp(key, "lastStartTag") == 0) { ctx.last_start_tag = (const char *) json_object_get_string(val); } else if (strcmp(key, "contentModelFlags") == 0) { ctx.content_model = json_object_get_array(val); } else if (strcmp(key, "processCDATA") == 0) { ctx.process_cdata = json_object_get_boolean(val); } } /* And run the test */ run_test(&ctx); } json_object_put(json); printf("PASS\n"); return 0; }
int main( int argc, char** argv ) { PGconn* t_PGconn = NULL; PGresult* t_PGresult_select = NULL; PGresult* t_PGresult; int t_returnCode = EXIT_FAILURE; int i = -1; int j; int k; int q; CURLcode t_curlCode; CURL* t_hEasy = NULL; char t_curlErrorMessage[CURL_ERROR_SIZE]; tDataBuffer t_responseBuffer = { NULL, 0 }; long t_httpResponseCode; json_object* j_getSTH = NULL; json_object* j_treeSize = NULL; json_object* j_timestamp = NULL; json_object* j_getEntries = NULL; json_object* j_entries = NULL; json_object* j_entry = NULL; json_object* j_leafInput = NULL; json_object* j_extraData = NULL; array_list* t_entriesArr = NULL; uint32_t t_entryID; uint32_t t_confirmedEntryID = -1; uint64_t t_timestamp; int64_t t_sthTimestamp; int t_treeSize; uint16_t t_logEntryType; char t_temp[255]; char* t_pointer; char* t_pointer1; char* t_b64Data; char* t_data = NULL; int32_t t_totalLength; X509* t_x509 = NULL; uint32_t t_certSize; char* t_query[32]; char* t_subjectName; uint8_t* t_cachedCACerts = NULL; uint32_t t_nCachedCACerts = 0; uint32_t t_nCertsInChain; EVP_MD_CTX t_mdctx; const EVP_MD* t_md; unsigned char t_sha256Hash_data[32]; uint32_t t_sha256Hash_size; /* Initialize the OpenSSL library */ OpenSSL_add_all_algorithms(); t_md = EVP_sha256(); EVP_MD_CTX_init(&t_mdctx); /* Install signal handlers */ signal(SIGHUP, signalHandler); signal(SIGINT, signalHandler); signal(SIGQUIT, signalHandler); signal(SIGTERM, signalHandler); for (q = 0; q < 32; q++) t_query[q] = malloc(128 * 1024); /* Connect to the database */ t_PGconn = PQconnectdb( "user=crtsh dbname=certwatch" " connect_timeout=5 client_encoding=auto" " application_name=ct_monitor" ); if (PQstatus(t_PGconn) != CONNECTION_OK) { printError("PQconnectdb()", PQerrorMessage(t_PGconn)); return EXIT_FAILURE; } printError("Connected OK", NULL); /* Get the latest CT Entry ID that we've added to the DB already */ sprintf( t_query[0], "SELECT ctl.ID, ctl.URL, ctl.LATEST_ENTRY_ID, ctl.NAME" " FROM ct_log ctl" " WHERE ctl.IS_ACTIVE" ); if (argc > 1) /* Only process one log */ sprintf( t_query[0] + strlen(t_query[0]), " AND ctl.ID = %s", argv[1] ); else /* Process all logs */ strcat( t_query[0], " ORDER BY ctl.ID" ); t_PGresult_select = PQexec( t_PGconn, t_query[0] ); if (PQresultStatus(t_PGresult_select) != PGRES_TUPLES_OK) { /* The SQL query failed */ printError("Query failed", PQerrorMessage(t_PGconn)); goto label_exit; } /* curl_global_init() must be called EXACTLY once */ t_curlCode = curl_global_init(CURL_GLOBAL_ALL); if (t_curlCode != CURLE_OK) { printError( "curl_global_init()", curl_easy_strerror(t_curlCode) ); /* Something went wrong, so we cannot continue */ return EXIT_FAILURE; } for (i = 0; i < PQntuples(t_PGresult_select); i++) { t_confirmedEntryID = -1; /* Initialize the "easy handle" */ t_hEasy = curl_easy_init(); if (!t_hEasy) { printError("curl_easy_init()", "returned NULL"); goto label_exit; } /* SETUP CURL BEHAVIOUR OPTIONS */ /* CURLOPT_NOPROGRESS: No progress meter */ CURL_EASY_SETOPT(CURLOPT_NOPROGRESS, 0) /* CURLOPT_NOSIGNAL: Don't use signals */ CURL_EASY_SETOPT(CURLOPT_NOSIGNAL, 1) /* SETUP CURL CALLBACK OPTIONS */ /* CURLOPT_WRITEFUNCTION: "Data Received" Callback Function */ CURL_EASY_SETOPT(CURLOPT_WRITEFUNCTION, curlDataReceivedFunction) /* CURLOPT_WRITEDATA: Data Pointer to pass to "Data Received" Callback Function */ CURL_EASY_SETOPT(CURLOPT_WRITEDATA, &t_responseBuffer) /* SETUP CURL ERROR OPTIONS */ /* CURLOPT_ERRORBUFFER: Buffer for potential error message */ CURL_EASY_SETOPT(CURLOPT_ERRORBUFFER, t_curlErrorMessage) /* SETUP CURL NETWORK OPTIONS */ /* CURLOPT_URL: The URL to deal with */ sprintf(t_temp, "%s/ct/v1/get-sth", PQgetvalue(t_PGresult_select, i, 1)); printError(t_temp, PQgetvalue(t_PGresult_select, i, 3)); CURL_EASY_SETOPT(CURLOPT_URL, t_temp) /* Use IPv4 rather than IPv6 */ CURL_EASY_SETOPT(CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4) /* SETUP CURL HTTP OPTIONS */ /* CURLOPT_FOLLOWLOCATION: Don't follow "Location:" redirects */ CURL_EASY_SETOPT(CURLOPT_FOLLOWLOCATION, 0) /* SETUP CURL CONNECTION OPTIONS */ /* CURLOPT_TIMEOUT: Transfer Timeout (in seconds) */ CURL_EASY_SETOPT(CURLOPT_TIMEOUT, 300) /* CURLOPT_CONNECTTIMEOUT: Connect Timeout (in seconds) */ CURL_EASY_SETOPT(CURLOPT_CONNECTTIMEOUT, 300) /* Perform the transfer */ t_curlCode = curl_easy_perform(t_hEasy); if (t_curlCode != CURLE_OK) { printError("curl_easy_perform()", t_curlErrorMessage); goto label_exit; } /* Get the HTTP response code */ t_curlCode = curl_easy_getinfo( t_hEasy, CURLINFO_RESPONSE_CODE, &t_httpResponseCode ); if (t_curlCode != CURLE_OK) { printError("curl_easy_getinfo()", t_curlErrorMessage); goto label_exit; } else if (t_httpResponseCode != 200) { printError( "curl_easy_getinfo()", "Unexpected HTTP Response Code" ); goto label_exit; } if (PQgetisnull(t_PGresult_select, i, 2)) t_entryID = -1; else t_entryID = strtoul( PQgetvalue(t_PGresult_select, i, 2), NULL, 10 ); printf("Highest Entry ID stored: %d\n", t_entryID); j_getSTH = json_tokener_parse(t_responseBuffer.data); if (!json_object_object_get_ex(j_getSTH, "tree_size", &j_treeSize)) goto label_exit; t_treeSize = json_object_get_int(j_treeSize); printf("Current Tree Size: %d\n", t_treeSize); if (!json_object_object_get_ex(j_getSTH, "timestamp", &j_timestamp)) goto label_exit; t_sthTimestamp = json_object_get_int64(j_timestamp); printf("Timestamp: %" LENGTH64 "d\n", t_sthTimestamp); if (json_object_put(j_getSTH) != 1) { printError("json_object_put(j_getSTH)", "Did not return 1"); goto label_exit; } free(t_responseBuffer.data); t_responseBuffer.data = NULL; t_responseBuffer.size = 0; /* TODO: Verify the STH signature */ for (t_entryID++; t_entryID < t_treeSize; t_entryID = t_confirmedEntryID + 1) { sprintf( t_temp, "%s/ct/v1/get-entries?start=%d&end=%d", PQgetvalue(t_PGresult_select, i, 1), t_entryID, (t_treeSize > (t_entryID + 1023)) ? (t_entryID + 1023) : (t_treeSize - 1) ); printError(t_temp, NULL); CURL_EASY_SETOPT(CURLOPT_URL, t_temp) /* Perform the transfer */ t_curlCode = curl_easy_perform(t_hEasy); if (t_curlCode != CURLE_OK) { printError("curl_easy_perform()", t_curlErrorMessage); goto label_exit; } /* Get the HTTP response code */ t_curlCode = curl_easy_getinfo( t_hEasy, CURLINFO_RESPONSE_CODE, &t_httpResponseCode ); if (t_curlCode != CURLE_OK) { printError("curl_easy_getinfo()", t_curlErrorMessage); goto label_exit; } else if (t_httpResponseCode != 200) { printError( "curl_easy_getinfo()", "Unexpected HTTP Response Code" ); goto label_exit; } /* Update the "Last Contacted" timestamp */ sprintf( t_query[0], "UPDATE ct_log" " SET LATEST_UPDATE=statement_timestamp()" " WHERE ID=%s", PQgetvalue(t_PGresult_select, i, 0) ); t_PGresult = PQexec(t_PGconn, t_query[0]); if (PQresultStatus(t_PGresult) != PGRES_COMMAND_OK) { /* The SQL query failed */ printError( "UPDATE Query failed", PQerrorMessage(t_PGconn) ); } PQclear(t_PGresult); /* Parse the JSON response */ j_getEntries = json_tokener_parse(t_responseBuffer.data); if (!json_object_object_get_ex(j_getEntries, "entries", &j_entries)) goto label_exit; t_entriesArr = json_object_get_array(j_entries); for (j = 0; j < json_object_array_length(j_entries); j++) { j_entry = array_list_get_idx(t_entriesArr, j); if (!json_object_object_get_ex(j_entry, "leaf_input", &j_leafInput)) goto label_exit; /* Decode the Base64 leaf_input string */ t_b64Data = (char*)json_object_get_string(j_leafInput); t_data = NULL; Base64Decode(t_b64Data, &t_data); if (!t_data) { printError("Base64 decode error", ""); goto label_exit; } /* Check the header fields */ if (*(unsigned char*)t_data != 0) { sprintf( t_temp, "%u", *(unsigned char*)t_data ); printError("Unexpected Version", t_temp); goto label_exit; } if (*(unsigned char*)(t_data + 1) != 0) { sprintf( t_temp, "%u", *(unsigned char*)(t_data + 1) ); printError("Unexpected MerkleLeafType", t_temp); goto label_exit; } t_timestamp = be64toh(*(uint64_t*)(t_data + 2)); t_logEntryType = be16toh(*(uint16_t*)(t_data + 10)); if (t_logEntryType == 1) { /* Precertificate. The leaf_input contains a SHA-256 hash of the Issuer Public Key, then the TBSCertificate. Ignore both of these. The submitted Precertificate is the first cert in the extra_data */ } else if (t_logEntryType == 0) { /* Parse the certificate */ t_certSize = 0; memcpy(((char*)&t_certSize) + 1, t_data + 12, 3); t_certSize = be32toh(t_certSize); printf("%d: ", (t_entryID + j)); t_pointer = t_data + 15; t_x509 = d2i_X509( NULL, (const unsigned char**)&t_pointer, t_certSize ); if (t_x509) { t_subjectName = X509_NAME_oneline( X509_get_subject_name(t_x509), NULL, 0 ); if (t_subjectName) { printf("%s\n", t_subjectName); OPENSSL_free(t_subjectName); } X509_free(t_x509); if (t_certSize != (t_pointer - (t_data + 15))) { printError("Additional data after EE cert", t_b64Data); t_certSize = t_pointer - (t_data + 15); } } else printError("Failed to decode EE cert", t_b64Data); /* Construct the "INSERT" query */ sprintf(t_query[0], "SELECT import_ct_cert(%s::smallint, %d, %" LENGTH64 "u, E'\\\\x", PQgetvalue(t_PGresult_select, i, 0), (t_entryID + j), t_timestamp); for (k = 0; k < t_certSize; k++) sprintf( t_query[0] + strlen(t_query[0]), "%02X", *(unsigned char*)(t_data + 15 + k)); strcat(t_query[0], "')"); } else { sprintf(t_temp, "%u", t_logEntryType); printError("Unexpected LogEntryType", t_temp); goto label_exit; } free(t_data); t_nCertsInChain = 1; if (!json_object_object_get_ex(j_entry, "extra_data", &j_extraData)) goto label_addCerts; /* Decode the Base64 extra_data string */ t_b64Data = (char*)json_object_get_string(j_extraData); t_data = NULL; Base64Decode(t_b64Data, &t_data); if (!t_data) { printError("Base64 decode error", ""); goto label_exit; } t_pointer1 = t_data; if (t_logEntryType == 1) { t_certSize = 0; memcpy(((char*)&t_certSize) + 1, t_pointer1, 3); t_certSize = be32toh(t_certSize); t_pointer1 += 3; t_pointer = t_pointer1; t_x509 = d2i_X509( NULL, (const unsigned char**)&t_pointer, t_certSize ); if (!t_x509) { printError("Failed to decode Precertificate", t_b64Data); goto label_exit; } if (t_certSize != (t_pointer - t_pointer1)) { printError("Additional data after Precertificate", t_b64Data); t_certSize = t_pointer - t_pointer1; } t_subjectName = X509_NAME_oneline( X509_get_subject_name(t_x509), NULL, 0 ); printf("Precertificate: %s\n", t_subjectName); X509_free(t_x509); if (t_subjectName) OPENSSL_free(t_subjectName); /* Construct the "INSERT" query */ sprintf(t_query[0], "SELECT import_ct_cert(%s::smallint, %d, %" LENGTH64 "u, E'\\\\x", PQgetvalue(t_PGresult_select, i, 0), (t_entryID + j), t_timestamp); for (k = 0; k < t_certSize; k++) sprintf( t_query[0] + strlen(t_query[0]), "%02X", *(unsigned char*)(t_pointer1 + k)); strcat(t_query[0], "')"); t_pointer1 = t_pointer; } /* Find the total length of the CA certificate array */ t_totalLength = 0; memcpy(((char*)&t_totalLength) + 1, t_pointer1, 3); t_totalLength = be32toh(t_totalLength); t_pointer1 += 3; /* Parse each CA Certificate */ while (t_totalLength > 0) { t_certSize = 0; memcpy(((char*)&t_certSize) + 1, t_pointer1, 3); t_certSize = be32toh(t_certSize); t_totalLength -= 3; t_pointer1 += 3; t_pointer = t_pointer1; t_x509 = d2i_X509( NULL, (const unsigned char**)&t_pointer, t_certSize ); if (!t_x509) { printError("Failed to decode CA cert", t_b64Data); goto label_exit; } if (t_certSize != (t_pointer - t_pointer1)) { printError("Additional data after CA cert", t_b64Data); t_certSize = t_pointer - t_pointer1; } t_subjectName = X509_NAME_oneline( X509_get_subject_name(t_x509), NULL, 0 ); printf("CA: %s", t_subjectName); X509_free(t_x509); if (t_subjectName) OPENSSL_free(t_subjectName); /* Generate SHA-256(CACertificate) */ EVP_DigestInit_ex(&t_mdctx, t_md, NULL); EVP_DigestUpdate(&t_mdctx, t_pointer1, t_certSize); EVP_DigestFinal_ex(&t_mdctx, t_sha256Hash_data, &t_sha256Hash_size); if ((!t_cachedCACerts) || (!bsearch(t_sha256Hash_data, t_cachedCACerts, t_nCachedCACerts, 32, compareSHA256Hashes))) { /* We've not cached this CA Certificate yet, so let's "INSERT" it and cache it */ /* Construct the "INSERT" query */ strcpy(t_query[t_nCertsInChain], "SELECT import_cert(E'\\\\x"); for (k = 0; k < t_certSize; k++) sprintf( t_query[t_nCertsInChain] + strlen(t_query[t_nCertsInChain]), "%02X", *(unsigned char*)(t_pointer1 + k)); strcat(t_query[t_nCertsInChain], "')"); t_nCertsInChain++; /* Cache this SHA-256(CACertificate), then re-sort the list */ t_nCachedCACerts++; t_cachedCACerts = realloc(t_cachedCACerts, t_nCachedCACerts * 32); (void)memcpy(t_cachedCACerts + ((t_nCachedCACerts - 1) * 32), t_sha256Hash_data, t_sha256Hash_size); qsort(t_cachedCACerts, t_nCachedCACerts, 32, compareSHA256Hashes); } else printf(" (Already cached)"); printf("\n"); t_totalLength -= (t_pointer - t_pointer1); t_pointer1 = t_pointer; } free(t_data); label_addCerts: /* Execute the "INSERT" quer(ies) */ printf("Import %d cert%s: ", t_nCertsInChain, (t_nCertsInChain == 1) ? "" : "s"); for (q = t_nCertsInChain - 1; q >= 0; q--) { t_PGresult = PQexec(t_PGconn, t_query[q]); if (PQresultStatus(t_PGresult) != PGRES_TUPLES_OK) { /* The SQL query failed */ printError("Query failed", PQerrorMessage(t_PGconn)); goto label_exit; } else if (PQgetisnull(t_PGresult, 0, 0)) { /* The SQL query failed */ printError("Query failed", t_query[q]); goto label_exit; } PQclear(t_PGresult); } printf("OK\n"); t_confirmedEntryID = t_entryID + j; if (g_terminateNow) goto label_exit; printf("\n"); } if (json_object_put(j_getEntries) != 1) { printError( "json_object_put(j_getEntries)", "Did not return 1" ); goto label_exit; } free(t_responseBuffer.data); t_responseBuffer.data = NULL; t_responseBuffer.size = 0; if (g_terminateNow) goto label_exit; } /* Cleanup the "easy handle" */ curl_easy_cleanup(t_hEasy); t_hEasy = NULL; if (t_confirmedEntryID == -1) t_entryID--; else t_entryID = t_confirmedEntryID; /* Update the "Latest STH" timestamp, now that we've processed all of the entries covered by this STH */ sprintf( t_query[0], "UPDATE ct_log" " SET LATEST_UPDATE=statement_timestamp()," " LATEST_STH_TIMESTAMP=TIMESTAMP WITH TIME ZONE 'epoch'" " + interval'%" LENGTH64 "d seconds'" " + interval'%" LENGTH64 "d milliseconds'" " WHERE ID=%s", t_sthTimestamp / 1000, t_sthTimestamp % 1000, PQgetvalue(t_PGresult_select, i, 0) ); t_PGresult = PQexec(t_PGconn, t_query[0]); if (PQresultStatus(t_PGresult) != PGRES_COMMAND_OK) { /* The SQL query failed */ printError( "UPDATE Query failed", PQerrorMessage(t_PGconn) ); } PQclear(t_PGresult); } t_returnCode = EXIT_SUCCESS; label_exit: if (t_returnCode == EXIT_FAILURE) if (t_confirmedEntryID != -1) { sprintf( t_query[0], "UPDATE ct_log" " SET LATEST_UPDATE=statement_timestamp()" " WHERE ID=%s", PQgetvalue(t_PGresult_select, i, 0) ); t_PGresult = PQexec(t_PGconn, t_query[0]); if (PQresultStatus(t_PGresult) != PGRES_COMMAND_OK) { /* The SQL query failed */ printError( "UPDATE Query failed", PQerrorMessage(t_PGconn) ); } PQclear(t_PGresult); } printError("Terminated", NULL); /* Clear the query results */ if (t_PGresult_select) PQclear(t_PGresult_select); /* Close this DB connection */ if (t_PGconn) PQfinish(t_PGconn); /* Free the Response Buffer */ if (t_responseBuffer.data) free(t_responseBuffer.data); /* Cleanup the "easy handle" */ if (t_hEasy) curl_easy_cleanup(t_hEasy); /* curl_global_cleanup() must be called EXACTLY once */ curl_global_cleanup(); for (q = 0; q < 32; q++) if (t_query[q]) free(t_query[q]); if (t_cachedCACerts) free(t_cachedCACerts); EVP_MD_CTX_cleanup(&t_mdctx); return t_returnCode; }
int OGRGMELayer::FetchDescribe() { CPLString osRequest = "tables/" + osTableId; CPLHTTPResult *psDescribe = poDS->MakeRequest(osRequest); if (psDescribe == NULL) return FALSE; CPLDebug("GME", "table doc = %s\n", psDescribe->pabyData); json_object *table_doc = OGRGMEParseJSON((const char *) psDescribe->pabyData); CPLHTTPDestroyResult(psDescribe); osTableName = OGRGMEGetJSONString(table_doc, "name"); poFeatureDefn = new OGRFeatureDefn(osTableName); poFeatureDefn->Reference(); json_object *schema_doc = json_object_object_get(table_doc, "schema"); json_object *columns_doc = json_object_object_get(schema_doc, "columns"); array_list *column_list = json_object_get_array(columns_doc); CPLString osLastGeomColumn; int field_count = array_list_length(column_list); for( int i = 0; i < field_count; i++ ) { OGRwkbGeometryType eFieldGeomType = wkbNone; json_object *field_obj = (json_object*) array_list_get_idx(column_list, i); const char* name = OGRGMEGetJSONString(field_obj, "name"); OGRFieldDefn oFieldDefn(name, OFTString); const char *type = OGRGMEGetJSONString(field_obj, "type"); if (EQUAL(type, "integer")) oFieldDefn.SetType(OFTInteger); else if (EQUAL(type, "double")) oFieldDefn.SetType(OFTReal); else if (EQUAL(type, "boolean")) oFieldDefn.SetType(OFTInteger); else if (EQUAL(type, "string")) oFieldDefn.SetType(OFTString); else if (EQUAL(type, "string")) { if (EQUAL(name, "gx_id")) { iGxIdField = i; } oFieldDefn.SetType(OFTString); } else if (EQUAL(type, "points")) eFieldGeomType = wkbPoint; else if (EQUAL(type, "linestrings")) eFieldGeomType = wkbLineString; else if (EQUAL(type, "polygons")) eFieldGeomType = wkbPolygon; else if (EQUAL(type, "mixedGeometry")) eFieldGeomType = wkbGeometryCollection; if (eFieldGeomType == wkbNone) { poFeatureDefn->AddFieldDefn(&oFieldDefn); } else { CPLAssert(EQUAL(osGeomColumnName,"")); osGeomColumnName = oFieldDefn.GetNameRef(); poFeatureDefn->SetGeomType(eFieldGeomType); poFeatureDefn->GetGeomFieldDefn(0)->SetSpatialRef(poSRS); } } json_object_put(table_doc); return TRUE; }