Path: blob/main/crypto/heimdal/lib/roken/dumpdata.c
39536 views
/*1* Copyright (c) 2005 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* 3. Neither the name of the Institute nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#include <config.h>3435#include "roken.h"3637/*38* Write datablob to a filename, don't care about errors.39*/4041ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL42rk_dumpdata (const char *filename, const void *buf, size_t size)43{44int fd;4546fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0640);47if (fd < 0)48return;49net_write(fd, buf, size);50close(fd);51}5253/*54* Read all data from a filename, care about errors.55*/5657ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL58rk_undumpdata(const char *filename, void **buf, size_t *size)59{60struct stat sb;61int fd, ret;62ssize_t sret;6364*buf = NULL;6566fd = open(filename, O_RDONLY, 0);67if (fd < 0)68return errno;69if (fstat(fd, &sb) != 0){70ret = errno;71goto out;72}73*buf = malloc(sb.st_size);74if (*buf == NULL) {75ret = ENOMEM;76goto out;77}78*size = sb.st_size;7980sret = net_read(fd, *buf, *size);81if (sret < 0)82ret = errno;83else if (sret != (ssize_t)*size) {84ret = EINVAL;85free(*buf);86*buf = NULL;87} else88ret = 0;8990out:91close(fd);92return ret;93}949596