File: offset-list.c

package info (click to toggle)
hivex 1.3.24-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,924 kB
  • sloc: ansic: 7,385; sh: 5,314; ml: 4,424; makefile: 559; perl: 528; python: 253; ruby: 123; xml: 104; sed: 16
file content (121 lines) | stat: -rw-r--r-- 2,710 bytes parent folder | download | duplicates (9)
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
112
113
114
115
116
117
118
119
120
121
/* hivex - Windows Registry "hive" extraction library.
 * Copyright (C) 2013 Red Hat Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * See file LICENSE for the full license.
 */

/* Structure for returning 0-terminated lists of offsets (nodes,
 * values, etc).
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <errno.h>
#include <assert.h>

#include "hivex.h"
#include "hivex-internal.h"

void
_hivex_init_offset_list (hive_h *h, offset_list *list)
{
  list->h = h;
  list->len = 0;
  list->alloc = 0;
  list->offsets = NULL;
  list->limit = SIZE_MAX;
}

/* Preallocates the offset_list, but doesn't make the contents longer. */
int
_hivex_grow_offset_list (offset_list *list, size_t alloc)
{
  assert (alloc >= list->len);
  size_t *p = realloc (list->offsets, alloc * sizeof (size_t));
  if (p == NULL)
    return -1;
  list->offsets = p;
  list->alloc = alloc;
  return 0;
}

static int
add_to_offset_list (offset_list *list, size_t offset)
{
  if (list->len >= list->alloc) {
    if (_hivex_grow_offset_list (list, list->alloc ? list->alloc * 2 : 4) == -1)
      return -1;
  }
  list->offsets[list->len] = offset;
  list->len++;
  return 0;
}

int
_hivex_add_to_offset_list (offset_list *list, size_t offset)
{
  assert (offset != 0);         /* internal error if this happens */

  if (list->len >= list->limit) {
    hive_h *h = list->h;        /* for SET_ERRNO macro */
    SET_ERRNO (ERANGE,
               "list of offsets has exceeded limit (limit = %zu)",
               list->limit);
    return -1;
  }

  return add_to_offset_list (list, offset);
}

size_t
_hivex_get_offset_list_length (offset_list *list)
{
  return list->len;
}

void
_hivex_set_offset_list_limit (offset_list *list, size_t limit)
{
  list->limit = limit;
}

void
_hivex_free_offset_list (offset_list *list)
{
  free (list->offsets);
}

size_t *
_hivex_return_offset_list (offset_list *list)
{
  if (add_to_offset_list (list, 0) == -1)
    return NULL;
  return list->offsets;         /* caller frees */
}

void
_hivex_print_offset_list (offset_list *list, FILE *fp)
{
  size_t i;

  fprintf (fp, "[");
  for (i = 0; i < list->len; ++i) {
    if (i > 0)
      fputc (',', fp);
    fprintf (fp, "%zu", list->offsets[i]);
  }
  fprintf (fp, "]");
}