int main() { // this test has a space after the null character. check that it's still included const char *input = " \0 "; const char *expected = "\" \\u0000 \""; struct json_object *string = json_object_new_string_len(input, 3); const char *json = json_object_to_json_string(string); int strings_match = !strcmp( expected, json); int retval = 0; if (strings_match) { printf("JSON write result is correct: %s\n", json); printf("PASS\n"); } else { printf("JSON write result doesn't match expected string\n"); printf("expected string: "); printf("%s\n", expected); printf("parsed string: "); printf("%s\n", json); printf("FAIL\n"); retval=1; } json_object_put(string); return retval; }
static int json_spit_log(u_int64_t idx, const struct timeval *whence, const char *log, size_t len, void *closure) { struct json_object *doc = (struct json_object *)closure; struct json_object *o, *wo; u_int64_t ms; o = json_object_new_object(); wo = json_object_new_int(idx); json_object_set_int_overflow(wo, json_overflow_uint64); json_object_set_uint64(wo, idx); json_object_object_add(o, "idx", wo); ms = whence->tv_sec; ms *= 1000ULL; ms += whence->tv_usec/1000; wo = json_object_new_int(ms); json_object_set_int_overflow(wo, json_overflow_uint64); json_object_set_uint64(wo, ms); json_object_object_add(o, "whence", wo); json_object_object_add(o, "line", json_object_new_string_len(log, len)); json_object_array_add(doc, o); return 0; }
iot_json_t *iot_json_add_member(iot_json_t *o, const char *key, iot_json_type_t type, ...) { iot_json_t *m; const char *s; bool b; int i, l; double d; va_list ap; va_start(ap, type); switch (type) { case IOT_JSON_STRING: s = va_arg(ap, const char *); l = va_arg(ap, int); if (l < 0) m = json_object_new_string(s); else m = json_object_new_string_len(s, l); break; case IOT_JSON_BOOLEAN: b = va_arg(ap, int); m = json_object_new_boolean(b); break; case IOT_JSON_INTEGER: i = va_arg(ap, int); m = json_object_new_int(i); break; case IOT_JSON_DOUBLE: d = va_arg(ap, double); m = json_object_new_double(d); break; case IOT_JSON_OBJECT: m = json_object_new_object(); break; case IOT_JSON_ARRAY: m = json_object_new_array(); break; default: m = NULL; errno = EINVAL; } va_end(ap); if (m != NULL) json_object_object_add(o, key, m); return m; }
iot_json_t *iot_json_create(iot_json_type_t type, ...) { iot_json_t *o; const char *s; bool b; int i, l; double d; va_list ap; va_start(ap, type); switch (type) { case IOT_JSON_STRING: s = va_arg(ap, const char *); l = va_arg(ap, int); if (l < 0) o = json_object_new_string(s); else o = json_object_new_string_len(s, l); break; case IOT_JSON_BOOLEAN: b = va_arg(ap, int); o = json_object_new_boolean(b); break; case IOT_JSON_INTEGER: i = va_arg(ap, int); o = json_object_new_int(i); break; case IOT_JSON_DOUBLE: d = va_arg(ap, double); o = json_object_new_double(d); break; case IOT_JSON_OBJECT: o = json_object_new_object(); break; case IOT_JSON_ARRAY: o = json_object_new_array(); break; default: o = NULL; } va_end(ap); return o; }
int cli_jsonstrlen(json_object *obj, const char* key, const char* s, int len) { json_type objty; json_object *fpobj; if (NULL == obj) { cli_dbgmsg("json: null 'obj' specified to cli_jsonstr\n"); return CL_ENULLARG; } objty = json_object_get_type(obj); if (objty == json_type_object) { if (NULL == key) { cli_dbgmsg("json: null string specified as 'key' to cli_jsonstr\n"); return CL_ENULLARG; } } else if (objty != json_type_array) { return CL_EARG; } if (NULL == s) { cli_dbgmsg("json: null string specified as 's' to cli_jsonstr\n"); return CL_ENULLARG; } fpobj = json_object_new_string_len(s, len); if (NULL == fpobj) { cli_errmsg("json: no memory for json string object\n"); return CL_EMEM; } if (objty == json_type_object) json_object_object_add(obj, key, fpobj); else if (objty == json_type_array) json_object_array_add(obj, fpobj); return CL_SUCCESS; }
int pv_set_json (struct sip_msg* msg, pv_param_t* pvp, int flag , pv_value_t* val) { json_t * obj; enum json_tokener_error parse_status; if( expand_tag_list( msg, ((json_name *)pvp->pvn.u.dname)->tags ) < 0) { LM_ERR("Cannot expand variables in path\n"); return -1; } /* delete value */ if( val == NULL) { return pv_add_json(pvp,NULL); } /* If we want the value to be interpreted prepare the object */ if( flag == COLONEQ_T ) { if( ! (val->flags & PV_VAL_STR) ) { LM_ERR("Trying to interpret a non-string value\n"); return -1; } obj = json_parse( val->rs.s, val->rs.len,&parse_status); if (obj == NULL) { LM_ERR("Error parsing json: %s\n", #if JSON_C_VERSION_NUM >= JSON_C_VERSION_010 json_tokener_error_desc(parse_status) #else json_tokener_errors[(unsigned long)obj] #endif ); pv_add_json(pvp, NULL); return -1; } } else { if( val->flags & PV_VAL_INT ) { obj = json_object_new_int(val->ri); } else { obj = json_object_new_string_len( val->rs.s, val->rs.len); } } return pv_add_json(pvp,obj); }
/** * Append a value to the array * * @param value The value to add * @param size The number of characters in the value */ void Array::append(const char *value, size_t size) { // add property json_object_array_add(_json, json_object_new_string_len(value, size)); }
/** * Append a value to the array * @param value */ void Array::append(const std::string &value) { // add property json_object_array_add(_json, json_object_new_string_len(value.c_str(), value.size())); }
struct json_object * pubnub_encrypt(const char *cipher_key, const char *message_str) { unsigned char iv[] = "0123456789012345"; /* Pre-process (hash) encryption key */ unsigned char cipher_hash[33]; pubnub_sha256_cipher_key(cipher_key, cipher_hash); /* Encrypt the message */ EVP_CIPHER_CTX aes256; EVP_CIPHER_CTX_init(&aes256); if (!EVP_EncryptInit_ex(&aes256, EVP_aes_256_cbc(), NULL, cipher_hash, iv)) { DBGMSG("EncryptInit error\n"); return NULL; } int message_len = strlen(message_str); unsigned char *cipher_data = (unsigned char*)malloc(message_len + EVP_CIPHER_block_size(EVP_aes_256_cbc())); int cipher_len = 0; if (!EVP_EncryptUpdate(&aes256, cipher_data, &cipher_len, (unsigned char *) message_str, message_len)) { DBGMSG("EncryptUpdate error\n"); return NULL; } int cipher_flen; if (!EVP_EncryptFinal_ex(&aes256, cipher_data + cipher_len, &cipher_flen)) { DBGMSG("EncryptFinal error\n"); return NULL; } cipher_len += cipher_flen; EVP_CIPHER_CTX_cleanup(&aes256); /* Convert to base64 representation */ BIO *b64f = BIO_new(BIO_f_base64()); BIO_set_flags(b64f, BIO_FLAGS_BASE64_NO_NL); BIO *bmem = BIO_new(BIO_s_mem()); BIO *b64 = BIO_push(b64f, bmem); if (BIO_write(b64, cipher_data, cipher_len) != cipher_len) { DBGMSG("b64 write error\n"); return NULL; } if (BIO_flush(b64) != 1) { DBGMSG("b64 flush error\n"); return NULL; } /* Conjure up JSON object */ unsigned char *b64_str; long b64_len = BIO_get_mem_data(b64, &b64_str); struct json_object *message = json_object_new_string_len((char *) b64_str, b64_len); /* Clean up. */ BIO_free_all(b64); free(cipher_data); return message; }
int send_json (msg_t *msg) { rc_info_t *rcinfo = NULL; unsigned int idx = 0; json_object *jobj_reply = NULL; sip_msg_t *sipPacket = NULL; const char *message = NULL; static int errors = 0; char tmpser[100]; jobj_reply = json_object_new_object(); idx = get_profile_index_by_name(msg->profile_name); rcinfo = &msg->rcinfo; if(msg->parsed_data && rcinfo->proto_type == 1) sipPacket = (sip_msg_t *) msg->parsed_data; stats.recieved_packets_total++; /* workaround for old json */ snprintf(tmpser, 100, "%" PRId64, (int64_t) stats.recieved_packets_total); json_object_object_add(jobj_reply, "packet_id", json_object_new_string(tmpser)); json_object_object_add(jobj_reply, "my_time", json_object_new_int(time(0))); json_object_object_add(jobj_reply, "ip_family", json_object_new_int(rcinfo->ip_family)); json_object_object_add(jobj_reply, "ip_proto", json_object_new_int(rcinfo->ip_proto)); if(rcinfo->ip_family == AF_INET) { json_object_object_add(jobj_reply, "src_ip4", json_object_new_string(rcinfo->src_ip)); json_object_object_add(jobj_reply, "dst_ip4", json_object_new_string(rcinfo->dst_ip)); } else { json_object_object_add(jobj_reply, "src_ip6", json_object_new_string(rcinfo->src_ip)); json_object_object_add(jobj_reply, "dst_ip6", json_object_new_string(rcinfo->dst_ip)); } json_object_object_add(jobj_reply, "src_port", json_object_new_int(rcinfo->src_port)); json_object_object_add(jobj_reply, "dst_port", json_object_new_int(rcinfo->dst_port)); json_object_object_add(jobj_reply, "tss", json_object_new_int(rcinfo->time_sec)); json_object_object_add(jobj_reply, "tsu", json_object_new_int(rcinfo->time_usec)); /* payload */ if(profile_transport[idx].flag == 1) json_object_object_add(jobj_reply, "payload", json_object_new_string(msg->data)); if(rcinfo->correlation_id.s && rcinfo->correlation_id.len > 0) { json_object_object_add(jobj_reply, "corr_id", json_object_new_string_len(rcinfo->correlation_id.s, rcinfo->correlation_id.len)); } json_object_object_add(jobj_reply, "proto_type", json_object_new_int(rcinfo->proto_type)); json_object_object_add(jobj_reply, "capt_id", json_object_new_int(profile_transport[idx].capt_id)); if(sipPacket != NULL) { if(sipPacket->callId.s && sipPacket->callId.len > 0) json_object_object_add(jobj_reply, "sip_callid", json_object_new_string_len(sipPacket->callId.s, sipPacket->callId.len)); if(sipPacket->isRequest && sipPacket->methodString.s && sipPacket->methodString.len > 0) json_object_object_add(jobj_reply, "sip_method", json_object_new_string_len(sipPacket->methodString.s, sipPacket->methodString.len)); else if(sipPacket->responseCode > 0) json_object_object_add(jobj_reply, "sip_response", json_object_new_int(sipPacket->responseCode)); if(sipPacket->cSeqMethodString.s && sipPacket->cSeqMethodString.len > 0) json_object_object_add(jobj_reply, "sip_cseq", json_object_new_string_len(sipPacket->cSeqMethodString.s, sipPacket->cSeqMethodString.len)); if(sipPacket->cSeqMethodString.s && sipPacket->cSeqMethodString.len > 0) json_object_object_add(jobj_reply, "sip_cseq", json_object_new_string_len(sipPacket->cSeqMethodString.s, sipPacket->cSeqMethodString.len)); if(sipPacket->fromURI.s && sipPacket->fromURI.len > 0) json_object_object_add(jobj_reply, "sip_from_uri", json_object_new_string_len(sipPacket->fromURI.s, sipPacket->fromURI.len)); if(sipPacket->toURI.s && sipPacket->toURI.len > 0) json_object_object_add(jobj_reply, "sip_to_uri", json_object_new_string_len(sipPacket->toURI.s, sipPacket->toURI.len)); if(sipPacket->requestURI.s && sipPacket->requestURI.len > 0) json_object_object_add(jobj_reply, "sip_request_uri", json_object_new_string_len(sipPacket->requestURI.s, sipPacket->requestURI.len)); if(sipPacket->paiUser.s && sipPacket->paiUser.len > 0) json_object_object_add(jobj_reply, "sip_pai_user", json_object_new_string_len(sipPacket->paiUser.s, sipPacket->paiUser.len)); if(sipPacket->hasSdp) json_object_object_add(jobj_reply, "sip_sdp", json_object_new_int(1)); } message = json_object_to_json_string(jobj_reply); /* make sleep after 100 errors */ if(errors > 30) { sleep (2); errors = 0; } /* send this packet out of our socket */ if(send_data(message, strlen(message), idx) < 0) { stats.errors_total++; LERR( "JSON server is down..."); if(!profile_transport[idx].usessl) { if(init_jsonsocket_blocking(idx)) { profile_transport[idx].initfails++; } errors=0; } #ifdef USE_SSL else { if(initSSL(idx)) profile_transport[idx].initfails++; errors=0; } #endif /* USE SSL */ } json_object_put(jobj_reply); if(msg->mfree == 1) free(msg->data); if(msg->corrdata) { free(msg->corrdata); msg->corrdata = NULL; } return 1; }
static int json_luks1_keyslot(const struct luks_phdr *hdr_v1, int keyslot, struct json_object **keyslot_object) { char *base64_str, cipher[LUKS_CIPHERNAME_L+LUKS_CIPHERMODE_L]; size_t base64_len; struct json_object *keyslot_obj, *field, *jobj_kdf, *jobj_af, *jobj_area; uint64_t offset, area_size, offs_a, offs_b, length; keyslot_obj = json_object_new_object(); json_object_object_add(keyslot_obj, "type", json_object_new_string("luks2")); json_object_object_add(keyslot_obj, "key_size", json_object_new_int64(hdr_v1->keyBytes)); /* KDF */ jobj_kdf = json_object_new_object(); json_object_object_add(jobj_kdf, "type", json_object_new_string(CRYPT_KDF_PBKDF2)); json_object_object_add(jobj_kdf, "hash", json_object_new_string(hdr_v1->hashSpec)); json_object_object_add(jobj_kdf, "iterations", json_object_new_int64(hdr_v1->keyblock[keyslot].passwordIterations)); /* salt field */ base64_len = base64_encode_alloc(hdr_v1->keyblock[keyslot].passwordSalt, LUKS_SALTSIZE, &base64_str); if (!base64_str) { json_object_put(keyslot_obj); json_object_put(jobj_kdf); if (!base64_len) return -EINVAL; return -ENOMEM; } field = json_object_new_string_len(base64_str, base64_len); free(base64_str); json_object_object_add(jobj_kdf, "salt", field); json_object_object_add(keyslot_obj, "kdf", jobj_kdf); /* AF */ jobj_af = json_object_new_object(); json_object_object_add(jobj_af, "type", json_object_new_string("luks1")); json_object_object_add(jobj_af, "hash", json_object_new_string(hdr_v1->hashSpec)); /* stripes field ignored, fixed to LUKS_STRIPES (4000) */ json_object_object_add(jobj_af, "stripes", json_object_new_int(4000)); json_object_object_add(keyslot_obj, "af", jobj_af); /* Area */ jobj_area = json_object_new_object(); json_object_object_add(jobj_area, "type", json_object_new_string("raw")); /* encryption algorithm field */ if (*hdr_v1->cipherMode != '\0') { (void) snprintf(cipher, sizeof(cipher), "%s-%s", hdr_v1->cipherName, hdr_v1->cipherMode); json_object_object_add(jobj_area, "encryption", json_object_new_string(cipher)); } else json_object_object_add(jobj_area, "encryption", json_object_new_string(hdr_v1->cipherName)); /* area */ if (LUKS_keyslot_area(hdr_v1, 0, &offs_a, &length) || LUKS_keyslot_area(hdr_v1, 1, &offs_b, &length) || LUKS_keyslot_area(hdr_v1, keyslot, &offset, &length)) { json_object_put(keyslot_obj); json_object_put(jobj_area); return -EINVAL; } area_size = offs_b - offs_a; json_object_object_add(jobj_area, "key_size", json_object_new_int(hdr_v1->keyBytes)); json_object_object_add(jobj_area, "offset", json_object_new_uint64(offset)); json_object_object_add(jobj_area, "size", json_object_new_uint64(area_size)); json_object_object_add(keyslot_obj, "area", jobj_area); *keyslot_object = keyslot_obj; return 0; }
static int json_luks1_digest(const struct luks_phdr *hdr_v1, struct json_object **digest_object) { char keyslot_str[2], *base64_str; int ks; size_t base64_len; struct json_object *digest_obj, *array, *field; digest_obj = json_object_new_object(); if (!digest_obj) return -ENOMEM; /* type field */ field = json_object_new_string("pbkdf2"); if (!field) { json_object_put(digest_obj); return -ENOMEM; } json_object_object_add(digest_obj, "type", field); /* keyslots array */ array = json_object_new_array(); if (!array) { json_object_put(digest_obj); return -ENOMEM; } json_object_object_add(digest_obj, "keyslots", json_object_get(array)); for (ks = 0; ks < LUKS_NUMKEYS; ks++) { if (hdr_v1->keyblock[ks].active != LUKS_KEY_ENABLED) continue; (void) snprintf(keyslot_str, sizeof(keyslot_str), "%d", ks); field = json_object_new_string(keyslot_str); if (!field || json_object_array_add(array, field) < 0) { json_object_put(field); json_object_put(array); json_object_put(digest_obj); return -ENOMEM; } } json_object_put(array); /* segments array */ array = json_object_new_array(); if (!array) { json_object_put(digest_obj); return -ENOMEM; } json_object_object_add(digest_obj, "segments", json_object_get(array)); field = json_object_new_string("0"); if (!field || json_object_array_add(array, field) < 0) { json_object_put(field); json_object_put(array); json_object_put(digest_obj); return -ENOMEM; } json_object_put(array); /* hash field */ field = json_object_new_string(hdr_v1->hashSpec); if (!field) { json_object_put(digest_obj); return -ENOMEM; } json_object_object_add(digest_obj, "hash", field); /* salt field */ base64_len = base64_encode_alloc(hdr_v1->mkDigestSalt, LUKS_SALTSIZE, &base64_str); if (!base64_str) { json_object_put(digest_obj); if (!base64_len) return -EINVAL; return -ENOMEM; } field = json_object_new_string_len(base64_str, base64_len); free(base64_str); if (!field) { json_object_put(digest_obj); return -ENOMEM; } json_object_object_add(digest_obj, "salt", field); /* digest field */ base64_len = base64_encode_alloc(hdr_v1->mkDigest, LUKS_DIGESTSIZE, &base64_str); if (!base64_str) { json_object_put(digest_obj); if (!base64_len) return -EINVAL; return -ENOMEM; } field = json_object_new_string_len(base64_str, base64_len); free(base64_str); if (!field) { json_object_put(digest_obj); return -ENOMEM; } json_object_object_add(digest_obj, "digest", field); /* iterations field */ field = json_object_new_int64(hdr_v1->mkDigestIterations); if (!field) { json_object_put(digest_obj); return -ENOMEM; } json_object_object_add(digest_obj, "iterations", field); *digest_object = digest_obj; return 0; }