s3-libnetapi: add netapi_read_file helper
authorGünther Deschner <gd@samba.org>
Wed, 10 Feb 2021 11:21:31 +0000 (12:21 +0100)
committerGünther Deschner <gd@samba.org>
Wed, 14 Jul 2021 16:49:30 +0000 (16:49 +0000)
Guenther

Signed-off-by: Guenther Deschner <gd@samba.org>
Reviewed-by: Alexander Bokovoy <ab@samba.org>
source3/lib/netapi/examples/common.c
source3/lib/netapi/examples/common.h

index 358e0edc4c7ac3aad6b3cad491716576e7b82f4e..c7f59684363e92212c96680c1dd87d92067c6507 100644 (file)
@@ -6,6 +6,14 @@
 #include <popt.h>
 #include <netapi.h>
 
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#ifndef MIN
+#define MIN(a,b) ((a)<(b)?(a):(b))
+#endif
+
 #include "common.h"
 
 void popt_common_callback(poptContext con,
@@ -94,3 +102,83 @@ struct poptOption popt_common_netapi_examples[] = {
        },
        POPT_TABLEEND
 };
+
+char *netapi_read_file(const char *filename, uint32_t *psize)
+{
+       int fd;
+       FILE *file = NULL;
+       char *p = NULL;
+       size_t size = 0;
+       size_t chunk = 1024;
+       size_t maxsize = SIZE_MAX;
+       int err;
+
+       fd = open(filename, O_RDONLY);
+       if (fd == -1) {
+               goto fail;
+       }
+
+       file = fdopen(fd, "r");
+       if (file == NULL) {
+               goto fail;
+       }
+
+       while (size < maxsize) {
+               size_t newbufsize;
+               size_t nread;
+
+               chunk = MIN(chunk, (maxsize - size));
+
+               newbufsize = size + (chunk+1); /* chunk+1 can't overflow */
+               if (newbufsize < size) {
+                       goto fail; /* overflow */
+               }
+
+               p = realloc(p, sizeof(char)*newbufsize);
+               if (p == NULL) {
+                       goto fail;
+               }
+
+               nread = fread(p+size, 1, chunk, file);
+               size += nread;
+
+               if (nread != chunk) {
+                       break;
+               }
+       }
+
+       err = ferror(file);
+       if (err != 0) {
+               goto fail;
+       }
+
+       p[size] = '\0';
+
+       if (psize != NULL) {
+               *psize = size;
+       }
+ fail:
+       if (file != NULL) {
+               fclose(file);
+       }
+       close(fd);
+
+       return p;
+}
+
+int netapi_save_file(const char *fname, void *ppacket, size_t length)
+{
+       int fd;
+       fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
+       if (fd == -1) {
+               perror(fname);
+               return -1;
+       }
+       if (write(fd, ppacket, length) != length) {
+               fprintf(stderr,"Failed to write %s\n", fname);
+               close(fd);
+               return -1;
+       }
+       close(fd);
+       return 0;
+}
index 917698c9563b93f825d7e52666a2a415a6bdd08d..e852e817f7ccc6ce532459b2be5b7023429e8523 100644 (file)
@@ -13,3 +13,5 @@ extern struct poptOption popt_common_netapi_examples[];
 
 #define POPT_COMMON_LIBNETAPI_EXAMPLES { NULL, 0, POPT_ARG_INCLUDE_TABLE, popt_common_netapi_examples, 0, "Common samba netapi example options:", NULL },
 
+char *netapi_read_file(const char *filename, uint32_t *psize);
+int netapi_save_file(const char *fname, void *ppacket, size_t length);