Coverage Report

Created: 2025-06-24 07:14

/src/libvslvm/libvslvm/libvslvm_raw_location_descriptor.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Raw location descriptor functions
3
 *
4
 * Copyright (C) 2014-2024, Joachim Metz <joachim.metz@gmail.com>
5
 *
6
 * Refer to AUTHORS for acknowledgements.
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Lesser General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public License
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
 */
21
22
#include <common.h>
23
#include <memory.h>
24
#include <types.h>
25
26
#include "libvslvm_libcerror.h"
27
#include "libvslvm_raw_location_descriptor.h"
28
29
/* Creates a raw location descriptor
30
 * Make sure the value raw_location_descriptor is referencing, is set to NULL
31
 * Returns 1 if successful or -1 on error
32
 */
33
int libvslvm_raw_location_descriptor_initialize(
34
     libvslvm_raw_location_descriptor_t **raw_location_descriptor,
35
     libcerror_error_t **error )
36
5.31k
{
37
5.31k
  static char *function = "libvslvm_raw_location_descriptor_initialize";
38
39
5.31k
  if( raw_location_descriptor == NULL )
40
0
  {
41
0
    libcerror_error_set(
42
0
     error,
43
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
44
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
45
0
     "%s: invalid raw location descriptor.",
46
0
     function );
47
48
0
    return( -1 );
49
0
  }
50
5.31k
  if( *raw_location_descriptor != NULL )
51
0
  {
52
0
    libcerror_error_set(
53
0
     error,
54
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
55
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
56
0
     "%s: invalid raw location descriptor value already set.",
57
0
     function );
58
59
0
    return( -1 );
60
0
  }
61
5.31k
  *raw_location_descriptor = memory_allocate_structure(
62
5.31k
                              libvslvm_raw_location_descriptor_t );
63
64
5.31k
  if( *raw_location_descriptor == NULL )
65
0
  {
66
0
    libcerror_error_set(
67
0
     error,
68
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
69
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
70
0
     "%s: unable to create raw location descriptor.",
71
0
     function );
72
73
0
    goto on_error;
74
0
  }
75
5.31k
  if( memory_set(
76
5.31k
       *raw_location_descriptor,
77
5.31k
       0,
78
5.31k
       sizeof( libvslvm_raw_location_descriptor_t ) ) == NULL )
79
0
  {
80
0
    libcerror_error_set(
81
0
     error,
82
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
83
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
84
0
     "%s: unable to clear raw location descriptor.",
85
0
     function );
86
87
0
    goto on_error;
88
0
  }
89
5.31k
  return( 1 );
90
91
0
on_error:
92
0
  if( *raw_location_descriptor != NULL )
93
0
  {
94
0
    memory_free(
95
0
     *raw_location_descriptor );
96
97
0
    *raw_location_descriptor = NULL;
98
0
  }
99
0
  return( -1 );
100
5.31k
}
101
102
/* Frees a raw location descriptor
103
 * Returns 1 if successful or -1 on error
104
 */
105
int libvslvm_raw_location_descriptor_free(
106
     libvslvm_raw_location_descriptor_t **raw_location_descriptor,
107
     libcerror_error_t **error )
108
5.31k
{
109
5.31k
  static char *function = "libvslvm_raw_location_descriptor_free";
110
111
5.31k
  if( raw_location_descriptor == NULL )
112
0
  {
113
0
    libcerror_error_set(
114
0
     error,
115
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
116
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
117
0
     "%s: invalid raw location descriptor.",
118
0
     function );
119
120
0
    return( -1 );
121
0
  }
122
5.31k
  if( *raw_location_descriptor != NULL )
123
5.31k
  {
124
5.31k
    memory_free(
125
5.31k
     *raw_location_descriptor );
126
127
5.31k
    *raw_location_descriptor = NULL;
128
5.31k
  }
129
5.31k
  return( 1 );
130
5.31k
}
131
132
/* Retrieves a raw location descriptor
133
 * Returns 1 if successful or -1 on error
134
 */
135
int libvslvm_raw_location_descriptor_get(
136
     libvslvm_raw_location_descriptor_t *raw_location_descriptor,
137
     off64_t *offset,
138
     size64_t *size,
139
     uint32_t *checksum,
140
     uint32_t *flags,
141
     libcerror_error_t **error )
142
4.85k
{
143
4.85k
  static char *function = "libvslvm_raw_location_descriptor_get";
144
145
4.85k
  if( raw_location_descriptor == NULL )
146
0
  {
147
0
    libcerror_error_set(
148
0
     error,
149
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
150
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
151
0
     "%s: invalid raw location descriptor.",
152
0
     function );
153
154
0
    return( -1 );
155
0
  }
156
4.85k
  if( offset == NULL )
157
0
  {
158
0
    libcerror_error_set(
159
0
     error,
160
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
161
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
162
0
     "%s: invalid offset.",
163
0
     function );
164
165
0
    return( -1 );
166
0
  }
167
4.85k
  if( size == NULL )
168
0
  {
169
0
    libcerror_error_set(
170
0
     error,
171
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
172
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
173
0
     "%s: invalid size.",
174
0
     function );
175
176
0
    return( -1 );
177
0
  }
178
4.85k
  if( checksum == NULL )
179
0
  {
180
0
    libcerror_error_set(
181
0
     error,
182
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
183
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
184
0
     "%s: invalid checksum.",
185
0
     function );
186
187
0
    return( -1 );
188
0
  }
189
4.85k
  if( flags == NULL )
190
0
  {
191
0
    libcerror_error_set(
192
0
     error,
193
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
194
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
195
0
     "%s: invalid flags.",
196
0
     function );
197
198
0
    return( -1 );
199
0
  }
200
4.85k
  *offset   = raw_location_descriptor->offset;
201
4.85k
  *size     = raw_location_descriptor->size;
202
4.85k
  *checksum = raw_location_descriptor->checksum;
203
4.85k
  *flags    = raw_location_descriptor->flags;
204
205
4.85k
  return( 1 );
206
4.85k
}
207
208
/* Sets a raw location descriptor
209
 * Returns 1 if successful or -1 on error
210
 */
211
int libvslvm_raw_location_descriptor_set(
212
     libvslvm_raw_location_descriptor_t *raw_location_descriptor,
213
     off64_t offset,
214
     size64_t size,
215
     uint32_t checksum,
216
     uint32_t flags,
217
     libcerror_error_t **error )
218
5.31k
{
219
5.31k
  static char *function = "libvslvm_raw_location_descriptor_set";
220
221
5.31k
  if( raw_location_descriptor == NULL )
222
0
  {
223
0
    libcerror_error_set(
224
0
     error,
225
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
226
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
227
0
     "%s: invalid raw location descriptor.",
228
0
     function );
229
230
0
    return( -1 );
231
0
  }
232
5.31k
  if( offset < 0 )
233
120
  {
234
120
    libcerror_error_set(
235
120
     error,
236
120
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
237
120
     LIBCERROR_ARGUMENT_ERROR_VALUE_LESS_THAN_ZERO,
238
120
     "%s: invalid offset value less than zero.",
239
120
     function );
240
241
120
    return( -1 );
242
120
  }
243
5.19k
  if( size > (uint64_t) INT64_MAX )
244
127
  {
245
127
    libcerror_error_set(
246
127
     error,
247
127
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
248
127
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
249
127
     "%s: invalid size value exceeds maximum.",
250
127
     function );
251
252
127
    return( -1 );
253
127
  }
254
5.06k
  raw_location_descriptor->offset   = offset;
255
5.06k
  raw_location_descriptor->size     = size;
256
5.06k
  raw_location_descriptor->checksum = checksum;
257
5.06k
  raw_location_descriptor->flags    = flags;
258
259
5.06k
  return( 1 );
260
5.19k
}
261