void ReadWriteXMLBinary::WriteValue(XMLWritingMachine& writer, const TreeValue* value) const { // binary object const TreeBinary* binary = static_cast<const TreeBinary*>( value ); // binary data writer.OS() << "\n"; if (binary->SizeBytes()) { // init library base64_encodestate state; base64_init_encodestate(&state); // text buffer std::string encoded_data(2*binary->SizeBytes(), ' '); // convert int numchars = base64_encode_block((const char*)binary->Data(), binary->SizeBytes(), &encoded_data[0], &state); numchars += base64_encode_blockend(&encoded_data[0] + numchars, &state); // shrink to fit encoded_data.resize(numchars); // write writer.OS() << encoded_data; } }
/** * handle acks to the prepare requests. * if a majority of yes, decide use which value to do the accept requests. * with a majority of empty, use the initial value I have. * with some already accepted value, choose one with the highest ballot * if a majority of no, * restart with a higher ballot */ AckType Proposer::handle_msg_promise(MsgAckPrepare *msg_ack_pre) { // std::lock_guard<std::mutex> lock(prepare_mutex_); //DROP Out of Date & Already enter Phase II // std::cout << "\tProposer handle_msg_promise start" << std::endl; if (msg_ack_pre->ballot_id() < curr_ballot_ || curr_value_) { LOG_TRACE_PRO("<handle_msg_promise> ---- DROP! --NodeID %u", msg_ack_pre->msg_header().node_id()); // std::cout << "\tProposer promise ---- DROP! --NodeID" << msg_ack_pre->msg_header().node_id() << std::endl; return DROP; } node_id_t node_id = (uint16_t)msg_ack_pre->msg_header().node_id(); // std::cout << "Proposer handle_msg_promise from node_id: " << node_id << std::endl; msg_ack_prepare_[node_id] = msg_ack_pre; // NOT_ENOUGH if (msg_ack_prepare_.size() < qr_) { LOG_TRACE_PRO("<handle_msg_promise> ---- NOT_ENOUGH! --NodeID %u", msg_ack_pre->msg_header().node_id()); // std::cout << "\tProposer promise ---- NOT_ENOUGH!" << std::endl; return NOT_ENOUGH; } uint32_t true_counter = 0; std::map<node_id_t, MsgAckPrepare *>::iterator it; for (it = msg_ack_prepare_.begin(); it != msg_ack_prepare_.end(); it++) { if (it->second->reply()) { if (it->second->max_ballot_id() > max_ballot_) { max_ballot_ = it->second->max_ballot_id(); max_value_ = it->second->mutable_max_prop_value(); } true_counter++; } } if (true_counter >= qr_) { // CONTINUE if (max_value_) { #if MODE_TYPE == 1 // only for RS value_id_t max_value_id = max_value_->id(); if (!curr_value_ || curr_value_->id() != max_value_id) { std::vector<std::string> encoded_data(view_->rs_n(), ""); std::vector<bool> index(view_->rs_n(), false); unsigned lost_num = view_->rs_n(); for (it = msg_ack_prepare_.begin(); it != msg_ack_prepare_.end(); it++) { if (it->second->reply() && (max_value_id == it->second->mutable_max_prop_value()->id())) { encoded_data[it->first] = it->second->mutable_max_prop_value()->data(); index[it->first] = true; lost_num--; } } if (lost_num > view_->rs_f()) return RESTART; std::string decoded_data = rs_decoder_->rs_decode(encoded_data, index, lost_num); std::cout << "decoded_data: " << decoded_data << std::endl; curr_value_ = new PropValue(); curr_value_->set_id(max_value_id); curr_value_->set_data(decoded_data); max_value_ = curr_value_; rs_values_ = rs_encoder_->rs_encode(decoded_data); } #else curr_value_ = max_value_; #endif } else curr_value_ = init_value_; LOG_TRACE_PRO("<handle_msg_promise> ---- CONTINUE into Phase II! --NodeID %u", msg_ack_pre->msg_header().node_id()); return CONTINUE; } else {// RESTART LOG_TRACE_PRO("<handle_msg_promise> ---- RESTART! --NodeID %u", msg_ack_pre->msg_header().node_id()); return RESTART; } }
eap_status_e asn1_der_type_c::encode_oid_from_string(eap_const_string oid, const u32_t oid_length, eap_variable_data_c * const buffer) const { if (oid == 0 || buffer == 0 || buffer->get_is_valid() == false) { EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT); return EAP_STATUS_RETURN(m_am_tools, eap_status_illegal_parameter); } eap_status_e status(eap_status_process_general_error); const eap_char * oid_char = oid; const eap_char * end_char = &oid[oid_length]; u32_t remaining_length(oid_length); u32_t first_component(0ul); u32_t component_index(0ul); status = buffer->set_data_length(0ul); if (status != eap_status_ok) { EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT); return EAP_STATUS_RETURN(m_am_tools, status); } while(oid_char < end_char) { // Search next dot (.). const eap_char * dot = reinterpret_cast<const eap_char *>(m_am_tools->memchr(oid_char, '.', remaining_length)); if (dot == 0) { // The last component. dot = reinterpret_cast<const eap_char *>(oid_char + remaining_length); if (dot == 0 || dot != end_char) { EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT); return EAP_STATUS_RETURN(m_am_tools, eap_status_illegal_parameter); } } u32_t integer(0ul); status = m_am_tools->number_string_to_u32( reinterpret_cast<const u8_t *>(oid_char), dot - oid_char, &integer); if (status != eap_status_ok) { EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT); return EAP_STATUS_RETURN(m_am_tools, status); } if (component_index == 0ul) { // The first component is encoded with the second component. first_component = integer; } else if (component_index == 1ul) { if (first_component < 2ul && integer > 39ul) { EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT); return EAP_STATUS_RETURN(m_am_tools, eap_status_illegal_parameter); } else if (first_component > 2ul) { EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT); return EAP_STATUS_RETURN(m_am_tools, eap_status_illegal_parameter); } const u32_t oid_value = first_component * 40ul + integer; if (oid_value > 0xff) { EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT); return EAP_STATUS_RETURN(m_am_tools, eap_status_illegal_parameter); } const u8_t oid_octet(static_cast<u8_t>(oid_value)); status = buffer->add_data(&oid_octet, sizeof(oid_octet)); if (status != eap_status_ok) { EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT); return EAP_STATUS_RETURN(m_am_tools, status); } } else { eap_variable_data_c encoded_data(m_am_tools); if (encoded_data.get_is_valid() == false) { EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT); return EAP_STATUS_RETURN(m_am_tools, eap_status_allocation_error); } const u32_t ENCODE_BASE = 128ul; while(integer > 0ul) { const u8_t oid_octet = static_cast<u8_t>(integer % ENCODE_BASE); // Encodes the octets to reverse order. status = encoded_data.add_data(&oid_octet, sizeof(oid_octet)); if (status != eap_status_ok) { EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT); return EAP_STATUS_RETURN(m_am_tools, status); } integer = integer / ENCODE_BASE; } // while() for (u32_t ind = encoded_data.get_data_length(); ind > 0ul; --ind) { // reads the octets on reverse order. u8_t * oid_octet = encoded_data.get_data_offset(ind-1ul, sizeof(u8_t)); if (oid_octet == 0) { EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT); return EAP_STATUS_RETURN(m_am_tools, eap_status_illegal_parameter); } if (ind > 1ul) { // All but the last octet have high bit set. *oid_octet |= static_cast<u8_t>(asn1_high_bit_mask_tag); } status = buffer->add_data(oid_octet, sizeof(*oid_octet)); if (status != eap_status_ok) { EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT); return EAP_STATUS_RETURN(m_am_tools, status); } } // for() } remaining_length -= (dot - oid_char) + 1ul; oid_char = dot+1ul; ++component_index; } // while() EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT); return EAP_STATUS_RETURN(m_am_tools, status); }