void encode_dict_aux(struct Dict *d, char **si) { if (d == NULL) { **si = 'e'; (*si)++; **si = '\0'; return; } switch(d->key_type) { case 'l': encode_list(d->key, si); break; case 'd': encode_dict(d->key, si); break; case 's': encode_str(d->key, si); break; case 'i': encode_int(d->key, si); break; default: fprintf(stderr, "error: encode_dict encountered poorly formed subtype\n"); exit(1); } switch(d->value_type) { case 'l': encode_list(d->value, si); break; case 'd': encode_dict(d->value, si); break; case 's': encode_str(d->value, si); break; case 'i': encode_int(d->value, si); break; default: fprintf(stderr, "error: encode_dict encountered poorly formed subtype\n"); exit(1); } encode_dict_aux(d->next, si); }
void encode_list_aux(struct List *l, char **si) { // end with e if (l == NULL) { **si = 'e'; (*si)++; **si = '\0'; return; } switch(l->type) { case 'l': encode_list(l->cnt, si); break; case 'd': encode_dict(l->cnt, si); break; case 's': encode_str(l->cnt, si); break; case 'i': encode_int(l->cnt, si); break; default: fprintf(stderr, "error: encode_list encountered poorly formed subtype\n"); exit(1); } encode_list_aux(l->next, si); }
bool CoreRP::addBlockDevProps() { if (!m_impl->info->device()) { return true; } std::vector<unsigned char> contents; m_impl->cpio->contents("default.prop", &contents); std::vector<std::string> lines = StringUtils::splitData(contents, '\n'); for (auto it = lines.begin(); it != lines.end();) { if (StringUtils::starts_with(*it, "ro.patcher.blockdevs.")) { it = lines.erase(it); } else { ++it; } } Device *device = m_impl->info->device(); std::string encoded; encoded = "ro.patcher.blockdevs.base="; encoded += encode_list(device->blockDevBaseDirs()); lines.push_back(encoded); encoded = "ro.patcher.blockdevs.system="; encoded += encode_list(device->systemBlockDevs()); lines.push_back(encoded); encoded = "ro.patcher.blockdevs.cache="; encoded += encode_list(device->cacheBlockDevs()); lines.push_back(encoded); encoded = "ro.patcher.blockdevs.data="; encoded += encode_list(device->dataBlockDevs()); lines.push_back(encoded); encoded = "ro.patcher.blockdevs.boot="; encoded += encode_list(device->bootBlockDevs()); lines.push_back(encoded); encoded = "ro.patcher.blockdevs.recovery="; encoded += encode_list(device->recoveryBlockDevs()); lines.push_back(encoded); encoded = "ro.patcher.blockdevs.extra="; encoded += encode_list(device->extraBlockDevs()); lines.push_back(encoded); if (device->cryptoOptions()->supported) { encoded = "ro.patcher.cryptfs_header_path="; encoded += device->cryptoOptions()->headerPath; lines.push_back(encoded); } contents = StringUtils::joinData(lines, '\n'); m_impl->cpio->setContents("default.prop", std::move(contents)); return true; }
/* Encode an IB list into JSON */ ib_status_t ib_json_encode( ib_mpool_t *mpool, const ib_list_t *list, bool pretty, char **obuf, size_t *olen) { assert(mpool != NULL); assert(list != NULL); assert(obuf != NULL); yajl_gen handle; json_yajl_alloc_context_t alloc_ctx = { mpool, IB_OK }; ib_status_t rc; yajl_gen_status status; yajl_alloc_funcs alloc_fns = { json_yajl_alloc, json_yajl_realloc, json_yajl_free, &alloc_ctx }; handle = yajl_gen_alloc(&alloc_fns); /* Probably should validate the handle here, but it's not clear from the * YAJL documentation how to do that */ /* Set pretty option */ if (pretty) { int opt = yajl_gen_config(handle, yajl_gen_beautify); if (opt == 0) { return IB_EINVAL; } } rc = encode_list(handle, NULL, 0, list); if (rc != IB_OK) { return rc; } status = yajl_gen_get_buf(handle, (const unsigned char **)obuf, olen); if (status != yajl_gen_status_ok) { return IB_EUNKNOWN; } return rc; }
/* Encodes the struct in the bcont list. The encoding is null terminated. The size * parameter does not account for the null at the end. * NOTE: bfree DOES NOT FREE THIS STRUCT OR ITS CONTENT! * Accepts: a Bcont struct * Returns: a Bencoded struct */ struct Bencoded *encode(struct Bcont *bcont) { int size; char *res; char *si; switch(bcont->type) { case 'l': size = blen_list(bcont->cnt); res = (char *)malloc_eoe((1 + size) * sizeof(char)); si = res; encode_list(bcont->cnt, &si); break; case 'd': size = blen_dict(bcont->cnt); res = (char *)malloc_eoe((1 + size) * sizeof(char)); si = res; encode_dict(bcont->cnt, &si); break; case 's': size = blen_str(bcont->cnt); res = (char *)malloc_eoe((1 + size) * sizeof(char)); si = res; encode_str(bcont->cnt, &si); break; case 'i': size = blen_int(bcont->cnt); res = (char *)malloc_eoe((1 + size) * sizeof(char)); si = res; encode_int(bcont->cnt, &si); break; default: fprintf(stderr, "error: bencode encountered poorly formed subtype\n"); exit(1); } struct Bencoded *benc = (struct Bencoded *)malloc_eoe(sizeof(struct Bencoded)); benc->size = size; benc->cnt = res; return benc; }
/** * JSON YAJL encode: Encode a list * * @param[in,out] handle YAJL generator handle * @param[in] name Name of @a list (or NULL) * @param[in] nlen Length of @a name * @param[in] list IronBee list to encode * * @returns IronBee status code */ static ib_status_t encode_list( yajl_gen handle, const char *name, size_t nlen, const ib_list_t *list) { ib_status_t rc = IB_OK; const ib_list_node_t *node; yajl_gen_status status; int errors = 0; /* Encode the name */ if ( (name != NULL) && (nlen != 0) ) { status = yajl_gen_string(handle, (const unsigned char *)name, nlen); if (status != yajl_gen_status_ok) { return IB_EUNKNOWN; } } /* Encode the map start */ status = yajl_gen_map_open(handle); if (status != yajl_gen_status_ok) { return IB_EUNKNOWN; } IB_LIST_LOOP_CONST(list, node) { const ib_field_t *field = (const ib_field_t *)node->data; ib_status_t tmprc; status = yajl_gen_string(handle, (const unsigned char *)field->name, field->nlen); if (status != yajl_gen_status_ok) { rc = IB_EUNKNOWN; ++errors; continue; } switch(field->type) { case IB_FTYPE_LIST: { const ib_list_t *list2; tmprc = ib_field_value(field, ib_ftype_list_out(&list2)); if ( (tmprc != IB_OK) && (rc == IB_OK) ) { rc = tmprc; ++errors; } else { tmprc = encode_list(handle, NULL, 0, list2); if ( (tmprc != IB_OK) && (rc == IB_OK) ) { rc = tmprc; ++errors; } } break; } case IB_FTYPE_NUM: { ib_num_t num; tmprc = ib_field_value(field, ib_ftype_num_out(&num)); if ( (tmprc != IB_OK) && (rc == IB_OK) ) { rc = tmprc; ++errors; } else { status = yajl_gen_integer(handle, num); if (status != yajl_gen_status_ok) { if (rc != IB_OK) { rc = IB_EUNKNOWN; } ++errors; } } break; } case IB_FTYPE_FLOAT: { ib_float_t fnum; tmprc = ib_field_value(field, ib_ftype_float_out(&fnum)); if ( (tmprc != IB_OK) && (rc == IB_OK) ) { rc = tmprc; ++errors; } else { char buf[float_buf_size+1]; snprintf(buf, float_buf_size, "%#.8Lg", fnum); status = yajl_gen_number(handle, buf, strlen(buf)); if (status != yajl_gen_status_ok) { if (rc != IB_OK) { rc = IB_EUNKNOWN; } ++errors; } } break; } case IB_FTYPE_NULSTR: { const char *str; tmprc = ib_field_value(field, ib_ftype_nulstr_out(&str)); if ( (tmprc != IB_OK) && (rc == IB_OK) ) { rc = tmprc; ++errors; } else { status = yajl_gen_string(handle, (unsigned char *)str, strlen(str)); if (status != yajl_gen_status_ok) { if (rc != IB_OK) { rc = IB_EUNKNOWN; } ++errors; } } break; } case IB_FTYPE_BYTESTR: { const ib_bytestr_t *bs; tmprc = ib_field_value(field, ib_ftype_bytestr_out(&bs)); if ( (tmprc != IB_OK) && (rc == IB_OK) ) { rc = tmprc; ++errors; } else { status = yajl_gen_string(handle, ib_bytestr_const_ptr(bs), ib_bytestr_length(bs)); if (status != yajl_gen_status_ok) { if (rc != IB_OK) { rc = IB_EUNKNOWN; } ++errors; } } break; } default: /* Just ignore it */ break; } /* switch(f->type) */ } /* Encode the map end */ status = yajl_gen_map_close(handle); if (status != yajl_gen_status_ok) { return IB_EUNKNOWN; } return IB_OK; }