aboutsummaryrefslogtreecommitdiffstats
path: root/com32/lib/syslinux/loadfile.c
blob: f5479dab992892056592fd3e9e0e72ed45e013d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* ----------------------------------------------------------------------- *
 *
 *   Copyright 2005-2008 H. Peter Anvin - All Rights Reserved
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, Inc., 53 Temple Place Ste 330,
 *   Boston MA 02111-1307, USA; either version 2 of the License, or
 *   (at your option) any later version; incorporated herein by reference.
 *
 * ----------------------------------------------------------------------- */

/*
 * loadfile.c
 *
 * Read the contents of a data file into a malloc'd buffer
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>

#include <syslinux/loadfile.h>

#define INCREMENTAL_CHUNK 1024*1024

int loadfile(const char *filename, void **ptr, size_t *len)
{
  FILE *f;
  int rv;

  f = fopen(filename, "r");
  if ( !f )
    return -1;

  rv = floadfile(f, ptr, len, NULL, 0);
  fclose(f);

  return rv;
}

int floadfile(FILE *f, void **ptr, size_t *len, const void *prefix,
	      size_t prefix_len)
{
  struct stat st;
  void *data, *dp;
  size_t alen, clen, rlen, xlen;

  clen = alen = 0;
  data = NULL;

  if ( fstat(fileno(f), &st) )
    goto err;

  if (!S_ISREG(st.st_mode)) {
    /* Not a regular file, we can't assume we know the file size */
    if (prefix_len) {
      clen = alen = prefix_len;
      data = malloc(prefix_len);
      if (!data)
	goto err;

      memcpy(data, prefix, prefix_len);
    }

    do {
      alen += INCREMENTAL_CHUNK;
      dp = realloc(data, alen);
      if (!dp)
	goto err;
      data = dp;

      rlen = fread((char *)data+clen, 1, alen-clen, f);
      clen += rlen;
    } while (clen == alen);

    *len = clen;
    xlen = (clen + LOADFILE_ZERO_PAD-1) & ~(LOADFILE_ZERO_PAD-1);
    dp = realloc(data, xlen);
    if (dp)
      data = dp;
    *ptr = data;
  } else {
    *len = clen = st.st_size + prefix_len - ftell(f);
    xlen = (clen + LOADFILE_ZERO_PAD-1) & ~(LOADFILE_ZERO_PAD-1);

    *ptr = data = malloc(xlen);
    if ( !data )
      return -1;

    memcpy(data, prefix, prefix_len);

    if ( (off_t)fread((char *)data+prefix_len, 1, clen-prefix_len, f)
	 != clen-prefix_len )
      goto err;
  }

  memset((char *)data + clen, 0, xlen-clen);
  return 0;

 err:
  if (data)
    free(data);
  return -1;
}