Skip to content

Commit

Permalink
Refactor get_literal_src_hdr function.
Browse files Browse the repository at this point in the history
  • Loading branch information
ni4 committed Jul 25, 2023
1 parent 44a95bd commit 4ed00f1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 38 deletions.
38 changes: 16 additions & 22 deletions src/librepgp/stream-dump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1195,26 +1195,25 @@ stream_dump_compressed(rnp_dump_ctx_t *ctx, pgp_source_t *src, pgp_dest_t *dst)
static rnp_result_t
stream_dump_literal(pgp_source_t *src, pgp_dest_t *dst)
{
pgp_source_t lsrc = {0};
pgp_literal_hdr_t lhdr = {0};
rnp_result_t ret;
uint8_t readbuf[16384];
pgp_source_t lsrc = {0};
rnp_result_t ret = init_literal_src(&lsrc, src);

if ((ret = init_literal_src(&lsrc, src))) {
if (ret) {
return ret;
}

dst_printf(dst, "Literal data packet\n");
indent_dest_increase(dst);

get_literal_src_hdr(&lsrc, &lhdr);
auto lhdr = get_literal_src_hdr(lsrc);
dst_printf(dst, "data format: '%c'\n", lhdr.format);
dst_printf(dst, "filename: %s (len %d)\n", lhdr.fname, (int) lhdr.fname_len);
dst_printf(dst, "filename: %s (len %" PRIu8 ")\n", lhdr.fname, lhdr.fname_len);
dst_print_time(dst, "timestamp", lhdr.timestamp);

ret = RNP_SUCCESS;
while (!src_eof(&lsrc)) {
size_t read = 0;
uint8_t readbuf[16384];
size_t read = 0;
if (!src_read(&lsrc, readbuf, sizeof(readbuf), &read)) {
ret = RNP_ERROR_READ;
break;
Expand Down Expand Up @@ -2231,28 +2230,23 @@ stream_dump_compressed_json(rnp_dump_ctx_t *ctx, pgp_source_t *src, json_object
static rnp_result_t
stream_dump_literal_json(pgp_source_t *src, json_object *pkt)
{
pgp_source_t lsrc = {0};
pgp_literal_hdr_t lhdr = {0};
rnp_result_t ret;
uint8_t readbuf[16384];
pgp_source_t lsrc = {0};
rnp_result_t ret = init_literal_src(&lsrc, src);

if ((ret = init_literal_src(&lsrc, src))) {
if (ret) {
return ret;
}
ret = RNP_ERROR_OUT_OF_MEMORY;
get_literal_src_hdr(&lsrc, &lhdr);
if (!json_add(pkt, "format", (char *) &lhdr.format, 1)) {
goto done;
}
if (!json_add(pkt, "filename", (char *) lhdr.fname, lhdr.fname_len)) {
goto done;
}
if (!json_add(pkt, "timestamp", (uint64_t) lhdr.timestamp)) {
auto lhdr = get_literal_src_hdr(lsrc);
if (!json_add(pkt, "format", (char *) &lhdr.format, 1) ||
!json_add(pkt, "filename", (char *) lhdr.fname, lhdr.fname_len) ||
!json_add(pkt, "timestamp", (uint64_t) lhdr.timestamp)) {
goto done;
}

while (!src_eof(&lsrc)) {
size_t read = 0;
uint8_t readbuf[16384];
size_t read = 0;
if (!src_read(&lsrc, readbuf, sizeof(readbuf), &read)) {
ret = RNP_ERROR_READ;
goto done;
Expand Down
17 changes: 4 additions & 13 deletions src/librepgp/stream-parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1773,19 +1773,10 @@ init_literal_src(pgp_source_t *src, pgp_source_t *readsrc)
return ret;
}

bool
get_literal_src_hdr(pgp_source_t *src, pgp_literal_hdr_t *hdr)
const pgp_literal_hdr_t &
get_literal_src_hdr(pgp_source_t &src)
{
pgp_source_literal_param_t *param;

if (src->type != PGP_STREAM_LITERAL) {
RNP_LOG("wrong stream");
return false;
}

param = (pgp_source_literal_param_t *) src->param;
*hdr = param->hdr;
return true;
return (static_cast<pgp_source_literal_param_t *>(src.param))->hdr;
}

rnp_result_t
Expand Down Expand Up @@ -2573,7 +2564,7 @@ process_pgp_source(pgp_parse_handler_t *handler, pgp_source_t &src)
src_close(&datasrc);
} else {
if (handler->ctx->detached) {
RNP_LOG("Detached signature expected.");
RNP_LOG("Attached signature expected.");
res = RNP_ERROR_BAD_STATE;
goto finish;
}
Expand Down
5 changes: 2 additions & 3 deletions src/librepgp/stream-parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,9 @@ rnp_result_t init_literal_src(pgp_source_t *src, pgp_source_t *readsrc);

/* @brief Get the literal data packet information fields (not the OpenPGP packet header)
* @param src literal data source, initialized with init_literal_src
* @param hdr pointer to header structure, where result will be stored
* @return true on success or false otherwise
* @return reference to the structure
*/
bool get_literal_src_hdr(pgp_source_t *src, pgp_literal_hdr_t *hdr);
const pgp_literal_hdr_t &get_literal_src_hdr(pgp_source_t &src);

/* @brief Get the AEAD-encrypted packet information fields (not the OpenPGP packet header)
* @param src AEAD-encrypted data source (starting from packet data itself, not the header)
Expand Down

0 comments on commit 4ed00f1

Please sign in to comment.