void read_data_block(char *data, const Uint count, const Uint block_idx) { static const std::string block_prefix("__CFDATA_BEGIN"); XmlNode block_node = get_block_node(block_idx); const Uint block_begin = from_str<Uint>(block_node.attribute_value("begin")); const Uint block_end = from_str<Uint>(block_node.attribute_value("end")); const Uint compressed_size = block_end - block_begin - block_prefix.size(); // Check the prefix binary_file.seekg(block_begin); std::vector<char> prefix_buf(block_prefix.size()); binary_file.read(&prefix_buf[0], block_prefix.size()); const std::string read_prefix(prefix_buf.begin(), prefix_buf.end()); if(read_prefix != block_prefix) throw SetupError(FromHere(), "Bad block prefix for block " + to_str(block_idx)); if(count != 0) { // Build a decompressing stream boost::iostreams::filtering_istream decompressing_stream; decompressing_stream.set_auto_close(false); decompressing_stream.push(boost::iostreams::zlib_decompressor()); decompressing_stream.push(boost::iostreams::restrict(binary_file, 0, compressed_size)); // Read the data decompressing_stream.read(data, count); decompressing_stream.pop(); } cf3_assert(binary_file.tellg() == block_end); }
static int format_data(sc_card_t *card, const struct iso_sm_ctx *ctx, const u8 *data, size_t datalen, struct sc_asn1_entry *formatted_encrypted_data_entry, u8 **formatted_data, size_t *formatted_data_len) { int r; u8 *pad_data = NULL; size_t pad_data_len = 0; if (!ctx || !formatted_data || !formatted_data_len) { r = SC_ERROR_INVALID_ARGUMENTS; goto err; } r = add_padding(ctx, data, datalen, &pad_data); if (r < 0) { sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Could not add padding to data: %s", sc_strerror(r)); goto err; } pad_data_len = r; bin_log(card->ctx, SC_LOG_DEBUG_NORMAL, "Data to encrypt", pad_data, pad_data_len); r = ctx->encrypt(card, ctx, pad_data, pad_data_len, formatted_data); if (r < 0) { sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Could not encrypt the data"); goto err; } bin_log(card->ctx, SC_LOG_DEBUG_NORMAL, "Cryptogram", *formatted_data, r); r = prefix_buf(ctx->padding_indicator, *formatted_data, r, formatted_data); if (r < 0) { sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Could not prepend padding indicator to formatted " "data: %s", sc_strerror(r)); goto err; } *formatted_data_len = r; sc_format_asn1_entry(formatted_encrypted_data_entry, *formatted_data, formatted_data_len, SC_ASN1_PRESENT); r = SC_SUCCESS; err: if (pad_data) { sc_mem_clear(pad_data, pad_data_len); free(pad_data); } return r; }