Coverage Report

Created: 2025-06-24 07:14

/src/libodraw/libodraw/libodraw_sector_range.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Sector range functions
3
 *
4
 * Copyright (C) 2010-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 "libodraw_libcerror.h"
27
#include "libodraw_sector_range.h"
28
29
/* Creates a sector range
30
 * Make sure the value sector_range is referencing, is set to NULL
31
 * Returns 1 if successful or -1 on error
32
 */
33
int libodraw_sector_range_initialize(
34
     libodraw_sector_range_t **sector_range,
35
     libcerror_error_t **error )
36
0
{
37
0
  static char *function = "libodraw_sector_range_initialize";
38
39
0
  if( sector_range == 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 sector range.",
46
0
     function );
47
48
0
    return( -1 );
49
0
  }
50
0
  if( *sector_range != 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 sector range value already set.",
57
0
     function );
58
59
0
    return( -1 );
60
0
  }
61
0
  *sector_range = memory_allocate_structure(
62
0
                   libodraw_sector_range_t );
63
64
0
  if( *sector_range == 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 sector range.",
71
0
     function );
72
73
0
    goto on_error;
74
0
  }
75
0
  if( memory_set(
76
0
       *sector_range,
77
0
       0,
78
0
       sizeof( libodraw_sector_range_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 sector range.",
85
0
     function );
86
87
0
    goto on_error;
88
0
  }
89
0
  return( 1 );
90
91
0
on_error:
92
0
  if( *sector_range != NULL )
93
0
  {
94
0
    memory_free(
95
0
     *sector_range );
96
97
0
    *sector_range = NULL;
98
0
  }
99
0
  return( -1 );
100
0
}
101
102
/* Frees a sector range
103
 * Returns 1 if successful or -1 on error
104
 */
105
int libodraw_sector_range_free(
106
     libodraw_sector_range_t **sector_range,
107
     libcerror_error_t **error )
108
0
{
109
0
  static char *function = "libodraw_sector_range_free";
110
111
0
  if( sector_range == 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 sector range.",
118
0
     function );
119
120
0
    return( -1 );
121
0
  }
122
0
  if( *sector_range != NULL )
123
0
  {
124
0
    memory_free(
125
0
     *sector_range );
126
127
0
    *sector_range = NULL;
128
0
  }
129
0
  return( 1 );
130
0
}
131
132
/* Retrieves a sector range
133
 * Returns 1 if successful or -1 on error
134
 */
135
int libodraw_sector_range_get(
136
     libodraw_sector_range_t *sector_range,
137
     uint64_t *start_sector,
138
     uint64_t *number_of_sectors,
139
     libcerror_error_t **error )
140
0
{
141
0
  static char *function = "libodraw_sector_range_get";
142
143
0
  if( sector_range == NULL )
144
0
  {
145
0
    libcerror_error_set(
146
0
     error,
147
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
148
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
149
0
     "%s: invalid sector range.",
150
0
     function );
151
152
0
    return( -1 );
153
0
  }
154
0
  if( start_sector == NULL )
155
0
  {
156
0
    libcerror_error_set(
157
0
     error,
158
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
159
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
160
0
     "%s: invalid start sector.",
161
0
     function );
162
163
0
    return( -1 );
164
0
  }
165
0
  if( number_of_sectors == NULL )
166
0
  {
167
0
    libcerror_error_set(
168
0
     error,
169
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
170
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
171
0
     "%s: invalid number of sectors.",
172
0
     function );
173
174
0
    return( -1 );
175
0
  }
176
0
  *start_sector      = sector_range->start_sector;
177
0
  *number_of_sectors = sector_range->number_of_sectors;
178
179
0
  return( 1 );
180
0
}
181
182
/* Sets a sector range
183
 * Returns 1 if successful or -1 on error
184
 */
185
int libodraw_sector_range_set(
186
     libodraw_sector_range_t *sector_range,
187
     uint64_t start_sector,
188
     uint64_t number_of_sectors,
189
     libcerror_error_t **error )
190
0
{
191
0
  static char *function = "libodraw_sector_range_set";
192
193
0
  if( sector_range == NULL )
194
0
  {
195
0
    libcerror_error_set(
196
0
     error,
197
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
198
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
199
0
     "%s: invalid sector range.",
200
0
     function );
201
202
0
    return( -1 );
203
0
  }
204
0
  if( start_sector > (uint64_t) INT64_MAX )
205
0
  {
206
0
    libcerror_error_set(
207
0
     error,
208
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
209
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
210
0
     "%s: invalid start sector value exceeds maximum.",
211
0
     function );
212
213
0
    return( -1 );
214
0
  }
215
0
  if( number_of_sectors > (uint64_t) INT64_MAX )
216
0
  {
217
0
    libcerror_error_set(
218
0
     error,
219
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
220
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
221
0
     "%s: invalid number of sectors value exceeds maximum.",
222
0
     function );
223
224
0
    return( -1 );
225
0
  }
226
0
  sector_range->start_sector      = start_sector;
227
0
  sector_range->end_sector        = start_sector + number_of_sectors;
228
0
  sector_range->number_of_sectors = number_of_sectors;
229
230
0
  return( 1 );
231
0
}
232