From 63552852bf191985b4b55aa524bc397c5b1d1515 Mon Sep 17 00:00:00 2001 From: "Neal H. Walfield" Date: Fri, 13 Mar 2015 13:42:00 +0100 Subject: [PATCH] Move armor_data to misc.c. * dirmngr/ks-engine-hkp.c (armor_data): Move function from here... * dirmngr/misc.c (armor_data): ... to here and drop static qualifier. * dirmngr/misc.h: New declaration. -- Signed-off-by: Neal H. Walfield --- dirmngr/ks-engine-hkp.c | 58 ----------------------------------------- dirmngr/misc.c | 57 ++++++++++++++++++++++++++++++++++++++++ dirmngr/misc.h | 4 +++ 3 files changed, 61 insertions(+), 58 deletions(-) diff --git a/dirmngr/ks-engine-hkp.c b/dirmngr/ks-engine-hkp.c index 056809431..960d5fcc5 100644 --- a/dirmngr/ks-engine-hkp.c +++ b/dirmngr/ks-engine-hkp.c @@ -1114,64 +1114,6 @@ handle_send_request_error (gpg_error_t err, const char *request, return retry; } -static gpg_error_t -armor_data (char **r_string, const void *data, size_t datalen) -{ - gpg_error_t err; - struct b64state b64state; - estream_t fp; - long length; - char *buffer; - size_t nread; - - *r_string = NULL; - - fp = es_fopenmem (0, "rw,samethread"); - if (!fp) - return gpg_error_from_syserror (); - - if ((err=b64enc_start_es (&b64state, fp, "PGP PUBLIC KEY BLOCK")) - || (err=b64enc_write (&b64state, data, datalen)) - || (err = b64enc_finish (&b64state))) - { - es_fclose (fp); - return err; - } - - /* FIXME: To avoid the extra buffer allocation estream should - provide a function to snatch the internal allocated memory from - such a memory stream. */ - length = es_ftell (fp); - if (length < 0) - { - err = gpg_error_from_syserror (); - es_fclose (fp); - return err; - } - - buffer = xtrymalloc (length+1); - if (!buffer) - { - err = gpg_error_from_syserror (); - es_fclose (fp); - return err; - } - - es_rewind (fp); - if (es_read (fp, buffer, length, &nread)) - { - err = gpg_error_from_syserror (); - es_fclose (fp); - return err; - } - buffer[nread] = 0; - es_fclose (fp); - - *r_string = buffer; - return 0; -} - - /* Search the keyserver identified by URI for keys matching PATTERN. On success R_FP has an open stream to read the data. */ diff --git a/dirmngr/misc.c b/dirmngr/misc.c index 25652a252..53d0099d2 100644 --- a/dirmngr/misc.c +++ b/dirmngr/misc.c @@ -562,3 +562,60 @@ create_estream_ksba_reader (ksba_reader_t *r_reader, estream_t fp) *r_reader = reader; return 0; } + +gpg_error_t +armor_data (char **r_string, const void *data, size_t datalen) +{ + gpg_error_t err; + struct b64state b64state; + estream_t fp; + long length; + char *buffer; + size_t nread; + + *r_string = NULL; + + fp = es_fopenmem (0, "rw,samethread"); + if (!fp) + return gpg_error_from_syserror (); + + if ((err=b64enc_start_es (&b64state, fp, "PGP PUBLIC KEY BLOCK")) + || (err=b64enc_write (&b64state, data, datalen)) + || (err = b64enc_finish (&b64state))) + { + es_fclose (fp); + return err; + } + + /* FIXME: To avoid the extra buffer allocation estream should + provide a function to snatch the internal allocated memory from + such a memory stream. */ + length = es_ftell (fp); + if (length < 0) + { + err = gpg_error_from_syserror (); + es_fclose (fp); + return err; + } + + buffer = xtrymalloc (length+1); + if (!buffer) + { + err = gpg_error_from_syserror (); + es_fclose (fp); + return err; + } + + es_rewind (fp); + if (es_read (fp, buffer, length, &nread)) + { + err = gpg_error_from_syserror (); + es_fclose (fp); + return err; + } + buffer[nread] = 0; + es_fclose (fp); + + *r_string = buffer; + return 0; +} diff --git a/dirmngr/misc.h b/dirmngr/misc.h index 2dc298557..e98b79162 100644 --- a/dirmngr/misc.h +++ b/dirmngr/misc.h @@ -80,6 +80,10 @@ char *host_and_port_from_url (const char *url, int *port); /* Create a KSBA reader object and connect it to the estream FP. */ gpg_error_t create_estream_ksba_reader (ksba_reader_t *r_reader, estream_t fp); +/* Encode the binary data in {DATA,DATALEN} as ASCII-armored data and + stored it as a NUL-terminated string in *R_STRING. The caller is + responsible for freeing *R_STRING. */ +gpg_error_t armor_data (char **r_string, const void *data, size_t datalen); #endif /* MISC_H */