static int mar_pack(lua_State *L, mar_Buffer *buf, int *idx) { /* size_t l; */ lua_pushnil(L); while (lua_next(L, -2) != 0) { pack_value(L, buf, -2, idx); pack_value(L, buf, -1, idx); lua_pop(L, 1); } return 1; }
std::string encode_base58(const data_chunk& unencoded) { size_t leading_zeros = count_leading_zeros(unencoded); // size = log(256) / log(58), rounded up. const size_t number_nonzero = unencoded.size() - leading_zeros; const size_t indexes_size = number_nonzero * 138 / 100 + 1; // Allocate enough space in big-endian base58 representation. data_chunk indexes(indexes_size); // Process the bytes. for (auto it = unencoded.begin() + leading_zeros; it != unencoded.end(); ++it) { pack_value(indexes, *it); } // Skip leading zeroes in base58 result. auto first_nonzero = search_first_nonzero(indexes); // Translate the result into a string. std::string encoded; const size_t estimated_size = leading_zeros + (indexes.end() - first_nonzero); encoded.reserve(estimated_size); encoded.assign(leading_zeros, '1'); // Set actual main bytes. for (auto it = first_nonzero; it != indexes.end(); ++it) { const size_t index = *it; encoded += base58_chars[index]; } return encoded; }
void json_string_to_msgpack(const char *json_str, msgpack_sbuffer *buf) { PackState state; JsonLexContext *lex = makeJsonLexContext(cstring_to_text(json_str), true); JsonSemAction *sem; msgpack_packer *pk; JsonContainer top_level; JsonValue top_level_value; /* initialize packer */ pk = msgpack_packer_new(buf, msgpack_sbuffer_write); /* initialize top level object */ top_level_value = palloc(sizeof(JsonValueData)); top_level = palloc(sizeof(JsonContainerData)); top_level->type = JSON_TOP_LEVEL; top_level->parent = NULL; top_level->via.top_level.value = top_level_value; /* initialize state object */ state = palloc(sizeof(PackStateData)); state->lex = lex; state->current_container = top_level; state->pk = pk; state->buf = buf; /* initialize sem action */ sem = palloc(sizeof(JsonSemAction)); sem->semstate = (void *) state; sem->object_start = sem_object_start; sem->object_end = sem_object_end; sem->array_start = sem_array_start; sem->array_end = sem_array_end; sem->object_field_start = sem_object_field_start; sem->object_field_end = NULL; sem->array_element_start = sem_array_element_start; sem->array_element_end = NULL; sem->scalar = sem_scalar; /* run parser */ pg_parse_json(lex, sem); /* pack top level value */ pack_value(pk, top_level_value); /* destroy packer */ msgpack_packer_free(pk); /* destroy top level object */ destroy_container(top_level); }
static void sem_object_end(void *state) { PackState _state = (PackState) state; msgpack_sbuffer *buf; JsonContainer current_container, parent_container; msgpack_packer *pk; JsonObjectField field; size_t packed_size; char *packed_data; JsonValue value; current_container = _state->current_container; pk = _state->pk; /* start to pack map */ msgpack_pack_map(pk, current_container->via.object.length); /* pack each field */ field = current_container->via.object.start; while (field != NULL) { /* pack key */ pack_string(pk, field->key); /* pack value */ pack_value(pk, field->value); /* next field*/ field = field->next; } /* retrieve packed binary and reset buffer of packer */ buf = _state->buf; packed_size = buf->size; packed_data = msgpack_sbuffer_release(buf); /* set packed binary to the last value of parent conationer */ parent_container = current_container->parent; value = value_to_be_set(parent_container); value->type = MSGPACK_BINARY; value->via.msg.size = packed_size; value->via.msg.data = packed_data; /* step back to parent level */ _state->current_container = parent_container; destroy_container(current_container); }
static void sem_array_end(void *state) { PackState _state = (PackState) state; msgpack_sbuffer *buf; JsonContainer current_container, parent_container; msgpack_packer *pk; JsonArrayElement element; size_t packed_size; char *packed_data; JsonValue value; current_container = _state->current_container; pk = _state->pk; /* start to pack array */ msgpack_pack_array(pk, current_container->via.array.length); /* pack each element */ element = current_container->via.array.start; while (element != NULL) { /* pack value */ pack_value(pk, element->value); /* next element */ element = element->next; } /* retrieve packed binary and reset buffer of packer */ buf = _state->buf; packed_size = buf->size; packed_data = msgpack_sbuffer_release(buf); /* set packed binary to the last value of parent conationer */ parent_container = current_container->parent; value = value_to_be_set(parent_container); value->type = MSGPACK_BINARY; value->via.msg.size = packed_size; value->via.msg.data = packed_data; /* step back to parent level */ _state->current_container = parent_container; destroy_container(current_container); }
int tlv_pack(tlv_t *tlv, uint8_t *out, uint32_t out_sz) { assert(tlv != NULL); uint32_t pack_sz = 0; pack_sz = tlv_get_packed_size(tlv); if (out_sz < pack_sz) { ph_debug("not enough size, out_sz:%d, pack_sz:%d", out_sz, pack_sz); return -1; } PUT16(out, htons(tlv->id)); ADVANCE16P(out); PUT16(out, htons(tlv->type)); ADVANCE16P(out); PUT16(out, htons(tlv->length)); ADVANCE16P(out); pack_sz -= sizeof(uint16_t) * 3; return pack_value(tlv, out, &pack_sz); }
int pack_array_values( byte *blob, void **array, char *description, ssize_t *offset, int (*pack)( void *value, char typechar, ssize_t size, ssize_t *offset, byte *blob ), cexception_t *ex ) { alloccell_t *blob_header; alloccell_t *array_header; char *array_ptr; int i; ssize_t size, length, count, start; ssize_t element_size; char *count_str; char typechar; blob_header = ((alloccell_t*)blob) - 1; array_header = ((alloccell_t*)array) - 1; element_size = array_header->nref > 0 ? REF_SIZE : sizeof(stackunion_t); array_ptr = (char*)array; start = 0; while( *description ) { size = count = 0; typechar = description[0]; if( isdigit( description[1] )) { if( description[0] != '\0' ) { size = strtol( description + 1, &count_str, 0 ); if( count_str && count_str[0] && count_str[1] ) { count = strtol( count_str + 1, &description, 0 ); } } } else if( description[1] != '\0' ) { count = strtol( description + 2, NULL, 0 ); } if( count <= 0 ) { BC_CHECK_PTR( array, ex ); count = ((alloccell_t*)array)[-1].length - start; } if( count > 0 ) { if( size > 0 ) { if( blob_header->size < size * count + *offset ) { interpret_raise_exception_with_bcalloc_message ( /* err_code = */ -1, /* message = */ "attempting to unpack values past the end of a blob", /* module_id = */ 0, /* exception_id = */ SL_EXCEPTION_BLOB_OVERFLOW, ex ); return 0; } } if( count > ((alloccell_t*)array)[-1].length - start ) { interpret_raise_exception_with_bcalloc_message ( /* err_code = */ -1, /* message = */ "attempting to pack more values than array has", /* module_id = */ 0, /* exception_id = */ SL_EXCEPTION_ARRAY_OVERFLOW, ex ); return 0; } if( typechar != 'X' && typechar != 'x' ) { for( i = 0; i < count; i ++ ) { if( pack_value( array_ptr + (start+i) * element_size, typechar, size, offset, blob, pack, ex ) == 0 ) { return 0; } } start += count; } else { if( size <= 0 ) { for( i = 0; i < count; i++ ) { length = strnlen( (char*)blob + *offset, blob_header->size - *offset ) + 1; *offset += length; } } else { *offset += size * count; } } } while( *description && *description != ',' ) description++; if( *description ) description++; } return 1; }
static int receive_xid(v42_state_t *ss, const uint8_t *frame, int len) { lapm_state_t *s; v42_config_parameters_t config; const uint8_t *buf; uint8_t group_id; uint16_t group_len; uint32_t param_val; uint8_t param_id; uint8_t param_len; s = &ss->lapm; if (frame[2] != FI_GENERAL) return -1; memset(&config, 0, sizeof(config)); /* Skip the header octets */ frame += 3; len -= 3; while (len > 0) { group_id = frame[0]; group_len = frame[1]; group_len = (group_len << 8) | frame[2]; frame += 3; len -= (3 + group_len); if (len < 0) break; buf = frame; frame += group_len; switch (group_id) { case GI_PARAM_NEGOTIATION: while (group_len > 0) { param_id = buf[0]; param_len = buf[1]; buf += 2; if (group_len < (2 + param_len)) break; group_len -= (2 + param_len); switch (param_id) { case PI_HDLC_OPTIONAL_FUNCTIONS: param_val = pack_value(buf, param_len); break; case PI_TX_INFO_MAXSIZE: param_val = pack_value(buf, param_len); param_val >>= 3; config.v42_tx_n401 = s->tx_n401 = set_param(s->tx_n401, param_val, ss->config.v42_tx_n401); break; case PI_RX_INFO_MAXSIZE: param_val = pack_value(buf, param_len); param_val >>= 3; config.v42_rx_n401 = s->rx_n401 = set_param(s->rx_n401, param_val, ss->config.v42_rx_n401); break; case PI_TX_WINDOW_SIZE: param_val = pack_value(buf, param_len); config.v42_tx_window_size_k = s->tx_window_size_k = set_param(s->tx_window_size_k, param_val, ss->config.v42_tx_window_size_k); break; case PI_RX_WINDOW_SIZE: param_val = pack_value(buf, param_len); config.v42_rx_window_size_k = s->rx_window_size_k = set_param(s->rx_window_size_k, param_val, ss->config.v42_rx_window_size_k); break; default: break; } buf += param_len; } break; case GI_PRIVATE_NEGOTIATION: while (group_len > 0) { param_id = buf[0]; param_len = buf[1]; buf += 2; if (group_len < 2 + param_len) break; group_len -= (2 + param_len); switch (param_id) { case PI_PARAMETER_SET_ID: /* This might be worth monitoring, but it doesn't serve mnuch other purpose */ break; case PI_V42BIS_COMPRESSION_REQUEST: config.comp = pack_value(buf, param_len); break; case PI_V42BIS_NUM_CODEWORDS: config.comp_dict_size = pack_value(buf, param_len); break; case PI_V42BIS_MAX_STRING_LENGTH: config.comp_max_string = pack_value(buf, param_len); break; default: break; } buf += param_len; } break; default: break; } } //v42_update_config(ss, &config); return 0; }