1223534Shselasky/* $FreeBSD$ */ 2223534Shselasky 3223534Shselasky/*- 4223534Shselasky * Copyright (c) 2011 Hans Petter Selasky. All rights reserved. 5223534Shselasky * 6223534Shselasky * Redistribution and use in source and binary forms, with or without 7223534Shselasky * modification, are permitted provided that the following conditions 8223534Shselasky * are met: 9223534Shselasky * 1. Redistributions of source code must retain the above copyright 10223534Shselasky * notice, this list of conditions and the following disclaimer. 11223534Shselasky * 2. Redistributions in binary form must reproduce the above copyright 12223534Shselasky * notice, this list of conditions and the following disclaimer in the 13223534Shselasky * documentation and/or other materials provided with the distribution. 14223534Shselasky * 15223534Shselasky * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16223534Shselasky * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17223534Shselasky * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18223534Shselasky * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19223534Shselasky * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20223534Shselasky * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21223534Shselasky * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22223534Shselasky * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23223534Shselasky * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24223534Shselasky * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25223534Shselasky * SUCH DAMAGE. 26223534Shselasky */ 27223534Shselasky 28223534Shselasky#include <stdio.h> 29223534Shselasky#include <stdint.h> 30223534Shselasky#include <stdlib.h> 31223534Shselasky#include <fcntl.h> 32223534Shselasky#include <err.h> 33223534Shselasky#include <sysexits.h> 34223534Shselasky#include <unistd.h> 35223534Shselasky 36223534Shselasky#include "bus_load_file.h" 37223534Shselasky 38223534Shselaskyvoid 39223534Shselaskyload_file(const char *fname, uint8_t **pptr, uint32_t *plen) 40223534Shselasky{ 41223534Shselasky uint8_t *ptr; 42255122Sian ssize_t len; 43223534Shselasky off_t off; 44223534Shselasky int f; 45223534Shselasky 46223534Shselasky f = open(fname, O_RDONLY); 47223534Shselasky if (f < 0) 48223534Shselasky err(EX_NOINPUT, "Cannot open file '%s'", fname); 49223534Shselasky 50223534Shselasky off = lseek(f, 0, SEEK_END); 51223535Shselasky if (off < 0) { 52223535Shselasky err(EX_NOINPUT, "Cannot seek to " 53223535Shselasky "end of file '%s'", fname); 54223535Shselasky } 55223534Shselasky 56223535Shselasky if (lseek(f, 0, SEEK_SET) < 0) { 57223535Shselasky err(EX_NOINPUT, "Cannot seek to " 58223535Shselasky "beginning of file '%s'", fname); 59223535Shselasky } 60223534Shselasky 61223534Shselasky len = off; 62223534Shselasky if (len != off) 63223534Shselasky err(EX_NOINPUT, "File '%s' is too big", fname); 64223534Shselasky 65223534Shselasky ptr = malloc(len); 66223534Shselasky if (ptr == NULL) 67223534Shselasky errx(EX_SOFTWARE, "Out of memory"); 68223534Shselasky 69223534Shselasky if (read(f, ptr, len) != len) 70223534Shselasky err(EX_NOINPUT, "Cannot read all data"); 71223534Shselasky 72223534Shselasky close(f); 73223534Shselasky 74223534Shselasky *pptr = ptr; 75223534Shselasky *plen = len; 76223534Shselasky} 77