Skip to content

Commit

Permalink
wip #1099
Browse files Browse the repository at this point in the history
  • Loading branch information
rrrooommmaaa committed Jun 21, 2020
1 parent aad7561 commit cc5c62e
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 5 deletions.
8 changes: 8 additions & 0 deletions include/rnp/rnp.h
Original file line number Diff line number Diff line change
Expand Up @@ -2277,6 +2277,14 @@ rnp_result_t rnp_identifier_iterator_destroy(rnp_identifier_iterator_t it);
*/
rnp_result_t rnp_pipe(rnp_input_t input, rnp_output_t output);

/** Set line length for armored output
*
* @param output stream to configure
* @param llen line length in characters
* @return RNP_SUCCESS on success, or any other value on error
*/
rnp_result_t rnp_output_armor_set_line_length(rnp_output_t output, int llen);

#if defined(__cplusplus)
}

Expand Down
9 changes: 9 additions & 0 deletions src/lib/rnp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7173,3 +7173,12 @@ rnp_pipe(rnp_input_t input, rnp_output_t output)
output->keep = !ret;
return ret;
}

rnp_result_t
rnp_output_armor_set_line_length(rnp_output_t output, int llen)
{
if (!output || llen <= 0) {
return RNP_ERROR_BAD_PARAMETERS;
}
return armored_dst_set_line_length(&output->dst, llen);
}
23 changes: 20 additions & 3 deletions src/librepgp/stream-armor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,23 @@ init_armored_dst(pgp_dest_t *dst, pgp_dest_t *writedst, pgp_armored_msg_t msgtyp
return ret;
}

static bool
is_armored_dest(pgp_dest_t *dst)
{
return dst->type == PGP_STREAM_ARMORED;
}

rnp_result_t
armored_dst_set_line_length(pgp_dest_t *dst, int llen)
{
if (!dst || !dst->param || !is_armored_dest(dst)) {
return RNP_ERROR_BAD_PARAMETERS;
}
auto param = (pgp_dest_armored_param_t *) dst->param;
param->llen = llen;
return RNP_SUCCESS;
}

bool
is_armored_source(pgp_source_t *src)
{
Expand Down Expand Up @@ -979,9 +996,9 @@ rnp_dearmor_source(pgp_source_t *src, pgp_dest_t *dst)
{
rnp_result_t res = RNP_ERROR_BAD_FORMAT;
pgp_source_t armorsrc = {0};
{
uint8_t readbuf[PGP_INPUT_CACHE_SIZE];
size_t read;
{
uint8_t readbuf[PGP_INPUT_CACHE_SIZE];
size_t read;

if (!src_peek(src, readbuf, strlen(ST_CLEAR_BEGIN) + 1, &read) ||
(read < strlen(ST_ARMOR_BEGIN))) {
Expand Down
2 changes: 2 additions & 0 deletions src/librepgp/stream-armor.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,6 @@ bool is_armored_source(pgp_source_t *src);
**/
bool is_cleartext_source(pgp_source_t *src);

rnp_result_t armored_dst_set_line_length(pgp_dest_t *dst, int llen);

#endif
3 changes: 1 addition & 2 deletions src/librepgp/stream-common.h
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,6 @@ rnp_result_t init_null_dest(pgp_dest_t *dst);
* @param dst initialized destination
* @return RNP_SUCCESS or error code
**/
rnp_result_t rnp_pipe_source(pgp_source_t *src,
pgp_dest_t * dst);
rnp_result_t rnp_pipe_source(pgp_source_t *src, pgp_dest_t *dst);

#endif
54 changes: 54 additions & 0 deletions src/tests/ffi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4500,6 +4500,60 @@ TEST_F(rnp_tests, test_ffi_enarmor_dearmor)
}
}

TEST_F(rnp_tests, test_ffi_customized_enarmor)
{
std::string data;

// enarmor plain message
const std::string msg("this is a test");
data.clear();
{
uint8_t * buf = NULL;
size_t buf_size = 0;
rnp_input_t input = NULL;
rnp_output_t output = NULL;
rnp_output_t armor_layer = NULL;

assert_rnp_success(
rnp_input_from_memory(&input, (const uint8_t *) msg.data(), msg.size(), true));
assert_rnp_success(rnp_output_to_memory(&output, 0));
assert_rnp_success(rnp_output_to_armor(output, &armor_layer, "message"));
assert_rnp_success(rnp_output_armor_set_line_length(armor_layer, 64));

assert_rnp_success(rnp_pipe(input, armor_layer));
assert_rnp_success(rnp_output_finish(armor_layer));

assert_rnp_success(rnp_output_memory_get_buf(output, &buf, &buf_size, false));
data = std::string(buf, buf + buf_size);
assert_true(starts_with(data, "-----BEGIN PGP MESSAGE-----\r\n"));
assert_true(ends_with(data, "-----END PGP MESSAGE-----\r\n"));

assert_rnp_success(rnp_input_destroy(input));
assert_rnp_success(rnp_output_destroy(armor_layer));
assert_rnp_success(rnp_output_destroy(output));
}
// test that the dearmored message is correct
{
uint8_t * buf = NULL;
size_t buf_size = 0;
rnp_input_t input = NULL;
rnp_output_t output = NULL;

assert_rnp_success(
rnp_input_from_memory(&input, (const uint8_t *) data.data(), data.size(), true));
assert_rnp_success(rnp_output_to_memory(&output, 0));

assert_rnp_success(rnp_dearmor(input, output));

assert_rnp_success(rnp_output_memory_get_buf(output, &buf, &buf_size, false));
std::string dearmored(buf, buf + buf_size);
assert_true(msg == dearmored);

assert_rnp_success(rnp_input_destroy(input));
assert_rnp_success(rnp_output_destroy(output));
}
}

TEST_F(rnp_tests, test_ffi_version)
{
const uint32_t version = rnp_version();
Expand Down
2 changes: 2 additions & 0 deletions src/tests/rnp_tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ void test_ffi_signatures_detached_memory_g10(void **state);

void test_ffi_enarmor_dearmor(void **state);

void test_ffi_customized_enarmor(void **state);

void test_ffi_version(void **state);

void test_ffi_key_export(void **state);
Expand Down

0 comments on commit cc5c62e

Please sign in to comment.