File: offset.c

package info (click to toggle)
grub2 2.02+dfsg1-20
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 61,784 kB
  • sloc: ansic: 384,086; asm: 16,323; sh: 13,230; cpp: 1,994; makefile: 1,488; python: 1,458; sed: 423; lex: 393; yacc: 267; awk: 75; lisp: 50; perl: 31
file content (111 lines) | stat: -rw-r--r-- 2,775 bytes parent folder | download | duplicates (7)
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
109
110
111
/*
 *  GRUB  --  GRand Unified Bootloader
 *  Copyright (C) 2013  Free Software Foundation, Inc.
 *
 *  GRUB 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, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  GRUB is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <grub/file.h>
#include <grub/dl.h>

GRUB_MOD_LICENSE ("GPLv3+");

struct grub_offset_file
{
  grub_file_t parent;
  grub_off_t off;
};

static grub_ssize_t
grub_offset_read (grub_file_t file, char *buf, grub_size_t len)
{
  struct grub_offset_file *data = file->data;
  if (grub_file_seek (data->parent, data->off + file->offset) == (grub_off_t) -1)
    return -1;
  return grub_file_read (data->parent, buf, len);
}

static grub_err_t
grub_offset_close (grub_file_t file)
{
  struct grub_offset_file *data = file->data;

  if (data->parent)
    grub_file_close (data->parent);

  /* No need to close the same device twice.  */
  file->device = 0;

  return 0;
}

static struct grub_fs grub_offset_fs = {
  .name = "offset",
  .dir = 0,
  .open = 0,
  .read = grub_offset_read,
  .close = grub_offset_close,
  .label = 0,
  .next = 0
};

void
grub_file_offset_close (grub_file_t file)
{
  struct grub_offset_file *off_data = file->data;
  off_data->parent = NULL;
  grub_file_close (file);
}

grub_file_t
grub_file_offset_open (grub_file_t parent, grub_off_t start, grub_off_t size)
{
  struct grub_offset_file *off_data;
  grub_file_t off_file, last_off_file;
  grub_file_filter_id_t filter;

  off_file = grub_zalloc (sizeof (*off_file));
  off_data = grub_zalloc (sizeof (*off_data));
  if (!off_file || !off_data)
    {
      grub_free (off_file);
      grub_free (off_data);
      return 0;
    }

  off_data->off = start;
  off_data->parent = parent;

  off_file->device = parent->device;
  off_file->data = off_data;
  off_file->fs = &grub_offset_fs;
  off_file->size = size;

  last_off_file = NULL;
  for (filter = GRUB_FILE_FILTER_COMPRESSION_FIRST;
       off_file && filter <= GRUB_FILE_FILTER_COMPRESSION_LAST; filter++)
    if (grub_file_filters_enabled[filter])
      {
	last_off_file = off_file;
	off_file = grub_file_filters_enabled[filter] (off_file, parent->name);
      }

  if (!off_file)
    {
      off_data->parent = NULL;
      grub_file_close (last_off_file);
      return 0;
    }
  return off_file;
}