static int _lang_code_lookup_add( lang_code_lookup_t* lookup_table, const lang_code_t *code, int (*func)(void*, void*) ) { lang_code_lookup_element_t *element; element = (lang_code_lookup_element_t *)calloc(1, sizeof(lang_code_lookup_element_t)); element->lang_code = code; RB_INSERT_SORTED(lookup_table, element, link, func); return 0; }
static int vimeo_callback(http_connection_t *hc, const char *remain, void *opaque) { state_entry_t *vce; static state_entry_t *skel; const char *referer = http_arg_get(&hc->hc_args, "referer"); if(referer == NULL || strcmp(referer, "https://movian.tv/")) return 403; const char *state = http_arg_get(&hc->hc_req_args, "state"); const char *code = http_arg_get(&hc->hc_req_args, "code"); if(state == NULL || code == NULL) return 400; if(skel == NULL) skel = calloc(1, sizeof(state_entry_t)); skel->se_state = strdup(state); skel->se_code = strdup(code); pthread_mutex_lock(&state_mutex); vce = RB_INSERT_SORTED(&state_entries, skel, se_entry, state_entry_cmp); if (vce != NULL) vce->se_code = strdup(code); else skel = NULL; pthread_mutex_unlock(&state_mutex); return 200; }
static jni_subscription_t * makesub(JNIEnv *env, prop_callback_t *cb, jint j_propid, jstring j_path, jobject j_cbif) { jni_subscription_t *js = malloc(sizeof(jni_subscription_t)); const char *path = (*env)->GetStringUTFChars(env, j_path, 0); js->js_cbif = (*env)->NewGlobalRef(env, j_cbif); prop_t *p = j_propid ? (prop_t *)j_propid : prop_get_global(); js->js_id = ++jni_sub_tally; if(RB_INSERT_SORTED(&jni_subscriptions, js, js_link, js_cmp)) abort(); js->js_sub = prop_subscribe(PROP_SUB_ALT_PATH, PROP_TAG_NAMESTR, path, PROP_TAG_CALLBACK, cb, js, PROP_TAG_ROOT, p, PROP_TAG_COURIER, jni_courier, NULL); (*env)->ReleaseStringUTFChars(env, j_path, path); return js; }
ssize_t intlconv_utf8( char *dst, size_t dst_size, const char *dst_charset_id, const char *src_utf8 ) { intlconv_cache_t templ, *ic; char **inbuf, **outbuf; size_t inbuf_left, outbuf_left; ssize_t res; if (dst_charset_id == NULL) { strncpy(dst, src_utf8, dst_size); dst[dst_size - 1] = '\0'; return strlen(dst); } templ.ic_charset_id = (char *)dst_charset_id; pthread_mutex_lock(&intlconv_lock); if (intlconv_last_ic && strcmp(intlconv_last_ic->ic_charset_id, dst_charset_id) == 0) { ic = intlconv_last_ic; goto found; } ic = RB_FIND(&intlconv_all, &templ, ic_link, intlconv_cmp); if (!ic) { iconv_t c = iconv_open(dst_charset_id, "UTF-8"); if ((iconv_t)-1 == c) { pthread_mutex_unlock(&intlconv_lock); return -EIO; } ic = malloc(sizeof(*ic)); if (ic == NULL) { pthread_mutex_unlock(&intlconv_lock); return -ENOMEM; } ic->ic_charset_id = strdup(dst_charset_id); if (ic->ic_charset_id == NULL) { pthread_mutex_unlock(&intlconv_lock); free(ic); iconv_close(c); return -ENOMEM; } ic->ic_handle = c; RB_INSERT_SORTED(&intlconv_all, ic, ic_link, intlconv_cmp); } found: inbuf = (char **)&src_utf8; inbuf_left = strlen(src_utf8); outbuf = &dst; outbuf_left = dst_size; res = iconv(ic->ic_handle, inbuf, &inbuf_left, outbuf, &outbuf_left); if (res == -1) { res = -errno; } else { intlconv_last_ic = ic; } pthread_mutex_unlock(&intlconv_lock); if (res >= 0) res = dst_size - outbuf_left; return res; }
void api_register ( const api_hook_t *hook ) { api_link_t *t; SKEL_ALLOC(api_skel); api_skel->hook = hook; t = RB_INSERT_SORTED(&api_hook_tree, api_skel, link, ah_cmp); if (t) { tvherror("api", "trying to re-register subsystem"); } else { SKEL_USED(api_skel); } }
void api_register ( const api_hook_t *hook ) { static api_link_t *t, *skel = NULL; if (!skel) skel = calloc(1, sizeof(api_link_t)); skel->hook = hook; t = RB_INSERT_SORTED(&api_hook_tree, skel, link, ah_cmp); if (t) tvherror("api", "trying to re-register subsystem"); else skel = NULL; }
bouquet_t * bouquet_create(const char *uuid, htsmsg_t *conf, const char *name, const char *src) { bouquet_t *bq, *bq2; int i; lock_assert(&global_lock); bq = calloc(1, sizeof(bouquet_t)); bq->bq_services = idnode_set_create(1); bq->bq_active_services = idnode_set_create(1); if (idnode_insert(&bq->bq_id, uuid, &bouquet_class, 0)) { if (uuid) tvherror("bouquet", "invalid uuid '%s'", uuid); free(bq); return NULL; } if (conf) { bq->bq_in_load = 1; idnode_load(&bq->bq_id, conf); bq->bq_in_load = 0; if (!htsmsg_get_bool(conf, "shield", &i) && i) bq->bq_shield = 1; } if (name) { free(bq->bq_name); bq->bq_name = strdup(name); } if (src) { free(bq->bq_src); bq->bq_src = strdup(src); } bq2 = RB_INSERT_SORTED(&bouquets, bq, bq_link, _bq_cmp); assert(bq2 == NULL); bq->bq_saveflag = 1; return bq; }
/* Internal insertion routine */ static int _lang_str_add ( lang_str_t *ls, const char *str, const char *lang, int update, int append ) { int save = 0; lang_str_ele_t *e; if (!str) return 0; /* Get proper code */ if (!lang) lang = lang_code_preferred(); if (!(lang = lang_code_get(lang))) return 0; /* Create skel */ SKEL_ALLOC(lang_str_ele_skel); lang_str_ele_skel->lang = lang; /* Create */ e = RB_INSERT_SORTED(ls, lang_str_ele_skel, link, _lang_cmp); if (!e) { lang_str_ele_skel->str = strdup(str); SKEL_USED(lang_str_ele_skel); save = 1; /* Append */ } else if (append) { e->str = realloc(e->str, strlen(e->str) + strlen(str) + 1); strcat(e->str, str); save = 1; /* Update */ } else if (update && strcmp(str, e->str)) { free(e->str); e->str = strdup(str); save = 1; } return save; }
ntv_t * libsvc_http_json_get(const char *url, const char *auth, char *errbuf, size_t errlen) { cache_entry_t *ce; static cache_entry_t *skel; time_t now = time(NULL); pthread_mutex_lock(&cache_mutex); if(skel == NULL) skel = calloc(1, sizeof(cache_entry_t)); skel->ce_url = (char *)url; skel->ce_auth = (char *)auth; ce = RB_INSERT_SORTED(&cache_entries, skel, ce_link, cache_entry_cmp); if(ce == NULL) { // Nothing found -> New item 'skel' was inserted ce = skel; skel = NULL; ce->ce_url = strdup(url); ce->ce_auth = auth ? strdup(auth) : NULL; } while(ce->ce_status == -1) pthread_cond_wait(&cache_cond, &cache_mutex); if(ce->ce_expire > now) { if(ce->ce_status == 200) { ntv_t *m = ntv_json_deserialize(ce->ce_response, errbuf, errlen); pthread_mutex_unlock(&cache_mutex); return m; } if(ce->ce_status >= 400) { snprintf(errbuf, errlen, "HTTP Error %d", ce->ce_status); pthread_mutex_unlock(&cache_mutex); return NULL; } } ce->ce_status = -1; char *out; size_t outlen; FILE *f = open_buffer(&out, &outlen); struct curl_slist *slist = NULL; CURL *curl = curl_easy_init(); curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); curl_easy_setopt(curl, CURLOPT_WRITEDATA, f); curl_easy_setopt(curl, CURLOPT_USERAGENT, "libsvc"); curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, hdrfunc); curl_easy_setopt(curl, CURLOPT_HEADERDATA, ce); slist = curl_slist_append(slist, "Accept: application/json"); if(auth != NULL) slist = curl_slist_append(slist, tsprintf("Authorization: %s", auth)); if(ce->ce_etag != NULL) slist = curl_slist_append(slist, tsprintf("If-None-Match: %s", ce->ce_etag)); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist); ce->ce_expire = 0; free(ce->ce_etag); ce->ce_etag = NULL; pthread_mutex_unlock(&cache_mutex); CURLcode result = curl_easy_perform(curl); curl_slist_free_all(slist); pthread_mutex_lock(&cache_mutex); pthread_cond_broadcast(&cache_cond); fwrite("", 1, 1, f); fclose(f); if(result) { snprintf(errbuf, errlen, "%s", curl_easy_strerror(result)); curl_easy_cleanup(curl); ce->ce_expire = 0; ce->ce_status = 0; free(ce->ce_response); ce->ce_response = NULL; pthread_mutex_unlock(&cache_mutex); free(out); return NULL; } long http_code = 0; curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code); ce->ce_status = http_code; if(http_code == 304) { ce->ce_status = 200; } else if(http_code == 200) { free(ce->ce_response); ce->ce_response = out; out = NULL; } else { snprintf(errbuf, errlen, "HTTP Error %lu", http_code); free(ce->ce_response); ce->ce_response = NULL; } free(out); curl_easy_cleanup(curl); ntv_t *m = NULL; if(ce->ce_response != NULL) m = ntv_json_deserialize(ce->ce_response, errbuf, errlen); pthread_mutex_unlock(&cache_mutex); return m; }