Re: rpki-client adjust tal parse to the BIO free world

2021-10-26 Thread Job Snijders
OK job@

On Tue, Oct 26, 2021 at 05:43:32PM +0200, Claudio Jeker wrote:
> This is part 3 of the BIO removal. Switch tal_parse to pass a file buffer
> like all other callers. The parent process can now just use load_file()
> and pass that buffer to the parser. From there on the magic just happens.
> 
> -- 
> :wq Claudio
> 
> Index: encoding.c
> ===
> RCS file: /cvs/src/usr.sbin/rpki-client/encoding.c,v
> retrieving revision 1.4
> diff -u -p -r1.4 encoding.c
> --- encoding.c11 Oct 2021 16:06:36 -  1.4
> +++ encoding.c26 Oct 2021 14:40:47 -
> @@ -29,11 +29,11 @@
>   * Returns 0 on success or -1 for any errors.
>   */
>  int
> -base64_decode(const unsigned char *in, unsigned char **out, size_t *outlen)
> +base64_decode(const unsigned char *in, size_t inlen,
> +unsigned char **out, size_t *outlen)
>  {
>   static EVP_ENCODE_CTX *ctx;
>   unsigned char *to;
> - size_t inlen;
>   int tolen;
>  
>   if (ctx == NULL && (ctx = EVP_ENCODE_CTX_new()) == NULL)
> @@ -42,7 +42,6 @@ base64_decode(const unsigned char *in, u
>   *out = NULL;
>   *outlen = 0;
>  
> - inlen = strlen(in);
>   if (inlen >= INT_MAX - 3)
>   return -1;
>   tolen = ((inlen + 3) / 4) * 3 + 1;
> Index: extern.h
> ===
> RCS file: /cvs/src/usr.sbin/rpki-client/extern.h,v
> retrieving revision 1.79
> diff -u -p -r1.79 extern.h
> --- extern.h  26 Oct 2021 13:31:05 -  1.79
> +++ extern.h  26 Oct 2021 14:40:37 -
> @@ -337,12 +337,12 @@ enum publish_type {
>   * and parsed.
>   */
>  struct entity {
> - enum rtype   type; /* type of entity (not RTYPE_EOF) */
> - char*file; /* local path to file */
> - int  has_pkey; /* whether pkey/sz is specified */
> - unsigned char   *pkey; /* public key (optional) */
> - size_t   pkeysz; /* public key length (optional) */
> - char*descr; /* tal description */
> + enum rtype   type;  /* type of entity (not RTYPE_EOF) */
> + char*file;  /* local path to file */
> + int  has_data;  /* whether data blob is specified */
> + unsigned char   *data;  /* optional data blob */
> + size_t   datasz;/* length of optional data blob */
> + char*descr; /* tal description */
>   TAILQ_ENTRY(entity) entries;
>  };
>  TAILQ_HEAD(entityq, entity);
> @@ -397,8 +397,7 @@ extern int verbose;
>  
>  void  tal_buffer(struct ibuf *, const struct tal *);
>  void  tal_free(struct tal *);
> -struct tal   *tal_parse(const char *, char *);
> -char *tal_read_file(const char *);
> +struct tal   *tal_parse(const char *, char *, size_t);
>  struct tal   *tal_read(struct ibuf *);
>  
>  void  cert_buffer(struct ibuf *, const struct cert *);
> @@ -534,8 +533,8 @@ void   cryptoerrx(const char *, ...)
>  
>  /* Encoding functions for hex and base64. */
>  
> -int   base64_decode(const unsigned char *, unsigned char **,
> - size_t *);
> +int   base64_decode(const unsigned char *, size_t,
> + unsigned char **, size_t *);
>  int   base64_encode(const unsigned char *, size_t, char **);
>  char *hex_encode(const unsigned char *, size_t);
>  
> @@ -595,8 +594,9 @@ intoutput_csv(FILE *, struct vrp_tree
>  int   output_json(FILE *, struct vrp_tree *, struct brk_tree *,
>   struct stats *);
>  
> -void logx(const char *fmt, ...)
> +void logx(const char *fmt, ...)
>   __attribute__((format(printf, 1, 2)));
> +unsigned char*load_file(const char *, size_t *);
>  
>  int  mkpath(const char *);
>  
> Index: main.c
> ===
> RCS file: /cvs/src/usr.sbin/rpki-client/main.c,v
> retrieving revision 1.154
> diff -u -p -r1.154 main.c
> --- main.c24 Oct 2021 21:24:19 -  1.154
> +++ main.c26 Oct 2021 14:38:40 -
> @@ -20,6 +20,7 @@
>  #include 
>  #include 
>  #include 
> +#include 
>  #include 
>  #include 
>  #include 
> @@ -82,13 +83,46 @@ logx(const char *fmt, ...)
>   }
>  }
>  
> +unsigned char *
> +load_file(const char *name, size_t *len)
> +{
> + unsigned char *buf = NULL;
> + struct stat st;
> + ssize_t n;
> + size_t size;
> + int fd;
> +
> + *len = 0;
> +
> + if ((fd = open(name, O_RDONLY)) == -1)
> + return NULL;
> + if (fstat(fd, ) != 0)
> + goto err;
> + if (st.st_size < 0)
> + goto err;
> + size = (size_t)st.st_size;
> + if ((buf = malloc(size)) == NULL)
> + goto err;
> + n = read(fd, buf, size);
> + if (n < 0 || (size_t)n != size)
> + goto err;
> + close(fd);
> + *len = size;
> 

rpki-client adjust tal parse to the BIO free world

2021-10-26 Thread Claudio Jeker
This is part 3 of the BIO removal. Switch tal_parse to pass a file buffer
like all other callers. The parent process can now just use load_file()
and pass that buffer to the parser. From there on the magic just happens.

-- 
:wq Claudio

Index: encoding.c
===
RCS file: /cvs/src/usr.sbin/rpki-client/encoding.c,v
retrieving revision 1.4
diff -u -p -r1.4 encoding.c
--- encoding.c  11 Oct 2021 16:06:36 -  1.4
+++ encoding.c  26 Oct 2021 14:40:47 -
@@ -29,11 +29,11 @@
  * Returns 0 on success or -1 for any errors.
  */
 int
-base64_decode(const unsigned char *in, unsigned char **out, size_t *outlen)
+base64_decode(const unsigned char *in, size_t inlen,
+unsigned char **out, size_t *outlen)
 {
static EVP_ENCODE_CTX *ctx;
unsigned char *to;
-   size_t inlen;
int tolen;
 
if (ctx == NULL && (ctx = EVP_ENCODE_CTX_new()) == NULL)
@@ -42,7 +42,6 @@ base64_decode(const unsigned char *in, u
*out = NULL;
*outlen = 0;
 
-   inlen = strlen(in);
if (inlen >= INT_MAX - 3)
return -1;
tolen = ((inlen + 3) / 4) * 3 + 1;
Index: extern.h
===
RCS file: /cvs/src/usr.sbin/rpki-client/extern.h,v
retrieving revision 1.79
diff -u -p -r1.79 extern.h
--- extern.h26 Oct 2021 13:31:05 -  1.79
+++ extern.h26 Oct 2021 14:40:37 -
@@ -337,12 +337,12 @@ enum publish_type {
  * and parsed.
  */
 struct entity {
-   enum rtype   type; /* type of entity (not RTYPE_EOF) */
-   char*file; /* local path to file */
-   int  has_pkey; /* whether pkey/sz is specified */
-   unsigned char   *pkey; /* public key (optional) */
-   size_t   pkeysz; /* public key length (optional) */
-   char*descr; /* tal description */
+   enum rtype   type;  /* type of entity (not RTYPE_EOF) */
+   char*file;  /* local path to file */
+   int  has_data;  /* whether data blob is specified */
+   unsigned char   *data;  /* optional data blob */
+   size_t   datasz;/* length of optional data blob */
+   char*descr; /* tal description */
TAILQ_ENTRY(entity) entries;
 };
 TAILQ_HEAD(entityq, entity);
@@ -397,8 +397,7 @@ extern int verbose;
 
 voidtal_buffer(struct ibuf *, const struct tal *);
 voidtal_free(struct tal *);
-struct tal *tal_parse(const char *, char *);
-char   *tal_read_file(const char *);
+struct tal *tal_parse(const char *, char *, size_t);
 struct tal *tal_read(struct ibuf *);
 
 voidcert_buffer(struct ibuf *, const struct cert *);
@@ -534,8 +533,8 @@ void cryptoerrx(const char *, ...)
 
 /* Encoding functions for hex and base64. */
 
-int base64_decode(const unsigned char *, unsigned char **,
-   size_t *);
+int base64_decode(const unsigned char *, size_t,
+   unsigned char **, size_t *);
 int base64_encode(const unsigned char *, size_t, char **);
 char   *hex_encode(const unsigned char *, size_t);
 
@@ -595,8 +594,9 @@ int  output_csv(FILE *, struct vrp_tree
 int output_json(FILE *, struct vrp_tree *, struct brk_tree *,
struct stats *);
 
-void   logx(const char *fmt, ...)
+void   logx(const char *fmt, ...)
__attribute__((format(printf, 1, 2)));
+unsigned char  *load_file(const char *, size_t *);
 
 intmkpath(const char *);
 
Index: main.c
===
RCS file: /cvs/src/usr.sbin/rpki-client/main.c,v
retrieving revision 1.154
diff -u -p -r1.154 main.c
--- main.c  24 Oct 2021 21:24:19 -  1.154
+++ main.c  26 Oct 2021 14:38:40 -
@@ -20,6 +20,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -82,13 +83,46 @@ logx(const char *fmt, ...)
}
 }
 
+unsigned char *
+load_file(const char *name, size_t *len)
+{
+   unsigned char *buf = NULL;
+   struct stat st;
+   ssize_t n;
+   size_t size;
+   int fd;
+
+   *len = 0;
+
+   if ((fd = open(name, O_RDONLY)) == -1)
+   return NULL;
+   if (fstat(fd, ) != 0)
+   goto err;
+   if (st.st_size < 0)
+   goto err;
+   size = (size_t)st.st_size;
+   if ((buf = malloc(size)) == NULL)
+   goto err;
+   n = read(fd, buf, size);
+   if (n < 0 || (size_t)n != size)
+   goto err;
+   close(fd);
+   *len = size;
+   return buf;
+
+err:
+   close(fd);
+   free(buf);
+   return NULL;
+}
+
 void
 entity_free(struct entity *ent)
 {
if (ent == NULL)
return;
 
-   free(ent->pkey);
+   free(ent->data);