diff options
author | Günther Deschner <gd@samba.org> | 2021-02-10 12:21:31 +0100 |
---|---|---|
committer | Günther Deschner <gd@samba.org> | 2021-07-14 16:49:30 +0000 |
commit | 3cfe663651f353f69833ffc46687d9602c3cc900 (patch) | |
tree | afae08c63e83a62a412e22c3573bec20528c95fd /source3/lib/netapi | |
parent | 44bd5049e14556d3c9c803a50830a289c6ca2cdb (diff) | |
download | samba-3cfe663651f353f69833ffc46687d9602c3cc900.tar.gz |
s3-libnetapi: add netapi_read_file helper
Guenther
Signed-off-by: Guenther Deschner <gd@samba.org>
Reviewed-by: Alexander Bokovoy <ab@samba.org>
Diffstat (limited to 'source3/lib/netapi')
-rw-r--r-- | source3/lib/netapi/examples/common.c | 88 | ||||
-rw-r--r-- | source3/lib/netapi/examples/common.h | 2 |
2 files changed, 90 insertions, 0 deletions
diff --git a/source3/lib/netapi/examples/common.c b/source3/lib/netapi/examples/common.c index 358e0edc4c7..c7f59684363 100644 --- a/source3/lib/netapi/examples/common.c +++ b/source3/lib/netapi/examples/common.c @@ -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; +} diff --git a/source3/lib/netapi/examples/common.h b/source3/lib/netapi/examples/common.h index 917698c9563..e852e817f7c 100644 --- a/source3/lib/netapi/examples/common.h +++ b/source3/lib/netapi/examples/common.h @@ -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); |