172132Ssemenu/* 272132Ssemenu * Copyright (c) 2005 Kungliga Tekniska H��gskolan 372132Ssemenu * (Royal Institute of Technology, Stockholm, Sweden). 472132Ssemenu * All rights reserved. 572132Ssemenu * 672132Ssemenu * Redistribution and use in source and binary forms, with or without 772132Ssemenu * modification, are permitted provided that the following conditions 872132Ssemenu * are met: 972132Ssemenu * 1072132Ssemenu * 1. Redistributions of source code must retain the above copyright 1172132Ssemenu * notice, this list of conditions and the following disclaimer. 1272132Ssemenu * 1372132Ssemenu * 2. Redistributions in binary form must reproduce the above copyright 1472132Ssemenu * notice, this list of conditions and the following disclaimer in the 1572132Ssemenu * documentation and/or other materials provided with the distribution. 1672132Ssemenu * 1772132Ssemenu * 3. Neither the name of the Institute nor the names of its contributors 1872132Ssemenu * may be used to endorse or promote products derived from this software 1972132Ssemenu * without specific prior written permission. 2072132Ssemenu * 2172132Ssemenu * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 2272132Ssemenu * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2372132Ssemenu * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2472132Ssemenu * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 2572132Ssemenu * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2672132Ssemenu * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2772132Ssemenu * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2872132Ssemenu * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2972132Ssemenu * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3072132Ssemenu * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3172132Ssemenu * SUCH DAMAGE. 3272132Ssemenu */ 33119418Sobrien 34139749Simp#include <config.h> 3572132Ssemenu 3672132Ssemenu#include "roken.h" 3772132Ssemenu 3872132Ssemenu/* 3972132Ssemenu * Write datablob to a filename, don't care about errors. 4072132Ssemenu */ 4172132Ssemenu 4272132SsemenuROKEN_LIB_FUNCTION void ROKEN_LIB_CALL 4372132Ssemenurk_dumpdata (const char *filename, const void *buf, size_t size) 4472132Ssemenu{ 4572132Ssemenu int fd; 4672132Ssemenu 4772132Ssemenu fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0640); 4872132Ssemenu if (fd < 0) 4972132Ssemenu return; 5072132Ssemenu net_write(fd, buf, size); 5172132Ssemenu close(fd); 5272132Ssemenu} 5372132Ssemenu 5472132Ssemenu/* 5572132Ssemenu * Read all data from a filename, care about errors. 5672132Ssemenu */ 5772132Ssemenu 58129844SmariusROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 59129844Smariusrk_undumpdata(const char *filename, void **buf, size_t *size) 60129844Smarius{ 6172132Ssemenu struct stat sb; 6272132Ssemenu int fd, ret; 6372132Ssemenu ssize_t sret; 6472132Ssemenu 6572132Ssemenu *buf = NULL; 6672132Ssemenu 6772132Ssemenu fd = open(filename, O_RDONLY, 0); 6872132Ssemenu if (fd < 0) 6972132Ssemenu return errno; 7072132Ssemenu if (fstat(fd, &sb) != 0){ 7172132Ssemenu ret = errno; 7272132Ssemenu goto out; 7372132Ssemenu } 7472132Ssemenu *buf = malloc(sb.st_size); 7572132Ssemenu if (*buf == NULL) { 7672132Ssemenu ret = ENOMEM; 7772132Ssemenu goto out; 7872132Ssemenu } 79109514Sobrien *size = sb.st_size; 8072132Ssemenu 8172132Ssemenu sret = net_read(fd, *buf, *size); 8272132Ssemenu if (sret < 0) 8372132Ssemenu ret = errno; 8472132Ssemenu else if (sret != (ssize_t)*size) { 85105135Salfred ret = EINVAL; 86105135Salfred free(*buf); 8772132Ssemenu *buf = NULL; 8872132Ssemenu } else 8972132Ssemenu ret = 0; 9072132Ssemenu 9172132Ssemenu out: 9295722Sphk close(fd); 9372132Ssemenu return ret; 94227908Smarius} 9572132Ssemenu