Coverage Report

Created: 2024-02-25 07:20

/src/libmdmp/libmdmp/libmdmp_stream_descriptor.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Section decriptor 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 "libmdmp_io_handle.h"
27
#include "libmdmp_libcerror.h"
28
#include "libmdmp_libfdata.h"
29
#include "libmdmp_stream_descriptor.h"
30
31
/* Creates a stream descriptor
32
 * Make sure the value stream_descriptor is referencing, is set to NULL
33
 * Returns 1 if successful or -1 on error
34
 */
35
int libmdmp_stream_descriptor_initialize(
36
     libmdmp_stream_descriptor_t **stream_descriptor,
37
     libcerror_error_t **error )
38
430k
{
39
430k
  static char *function = "libmdmp_stream_descriptor_initialize";
40
41
430k
  if( stream_descriptor == NULL )
42
0
  {
43
0
    libcerror_error_set(
44
0
     error,
45
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
46
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
47
0
     "%s: invalid stream descriptor.",
48
0
     function );
49
50
0
    return( -1 );
51
0
  }
52
430k
  if( *stream_descriptor != NULL )
53
0
  {
54
0
    libcerror_error_set(
55
0
     error,
56
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
57
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
58
0
     "%s: invalid stream descriptor value already set.",
59
0
     function );
60
61
0
    return( -1 );
62
0
  }
63
430k
  *stream_descriptor = memory_allocate_structure(
64
430k
                        libmdmp_stream_descriptor_t );
65
66
430k
  if( *stream_descriptor == NULL )
67
0
  {
68
0
    libcerror_error_set(
69
0
     error,
70
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
71
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
72
0
     "%s: unable to create stream descriptor.",
73
0
     function );
74
75
0
    goto on_error;
76
0
  }
77
430k
  if( memory_set(
78
430k
       *stream_descriptor,
79
430k
       0,
80
430k
       sizeof( libmdmp_stream_descriptor_t ) ) == NULL )
81
0
  {
82
0
    libcerror_error_set(
83
0
     error,
84
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
85
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
86
0
     "%s: unable to clear stream descriptor.",
87
0
     function );
88
89
0
    memory_free(
90
0
     *stream_descriptor );
91
92
0
    *stream_descriptor = NULL;
93
94
0
    return( -1 );
95
0
  }
96
430k
  if( libfdata_stream_initialize(
97
430k
       &( ( *stream_descriptor )->data_stream ),
98
430k
       NULL,
99
430k
       NULL,
100
430k
       NULL,
101
430k
       NULL,
102
430k
       (ssize_t (*)(intptr_t *, intptr_t *, int, int, uint8_t *, size_t, uint32_t, uint8_t, libcerror_error_t **)) &libmdmp_io_handle_read_segment_data,
103
430k
       NULL,
104
430k
       (off64_t (*)(intptr_t *, intptr_t *, int, int, off64_t, libcerror_error_t **)) &libmdmp_io_handle_seek_segment_offset,
105
430k
       0,
106
430k
       error ) != 1 )
107
0
  {
108
0
    libcerror_error_set(
109
0
     error,
110
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
111
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
112
0
     "%s: unable to create data stream.",
113
0
     function );
114
115
0
    goto on_error;
116
0
  }
117
430k
  if( libfdata_stream_resize(
118
430k
       ( *stream_descriptor )->data_stream,
119
430k
       1,
120
430k
       error ) != 1 )
121
0
  {
122
0
    libcerror_error_set(
123
0
     error,
124
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
125
0
     LIBCERROR_RUNTIME_ERROR_RESIZE_FAILED,
126
0
     "%s: unable to resize data stream.",
127
0
     function );
128
129
0
    goto on_error;
130
0
  }
131
430k
  return( 1 );
132
133
0
on_error:
134
0
  if( *stream_descriptor != NULL )
135
0
  {
136
0
    if( ( *stream_descriptor )->data_stream != NULL )
137
0
    {
138
0
      libfdata_stream_free(
139
0
       &( ( *stream_descriptor )->data_stream ),
140
0
       NULL );
141
0
    }
142
0
    memory_free(
143
0
     *stream_descriptor );
144
145
0
    *stream_descriptor = NULL;
146
0
  }
147
0
  return( -1 );
148
430k
}
149
150
/* Frees a stream descriptor
151
 * Returns 1 if successful or -1 on error
152
 */
153
int libmdmp_stream_descriptor_free(
154
     libmdmp_stream_descriptor_t **stream_descriptor,
155
     libcerror_error_t **error )
156
430k
{
157
430k
  static char *function = "libmdmp_stream_descriptor_free";
158
430k
  int result            = 1;
159
160
430k
  if( stream_descriptor == NULL )
161
0
  {
162
0
    libcerror_error_set(
163
0
     error,
164
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
165
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
166
0
     "%s: invalid stream descriptor.",
167
0
     function );
168
169
0
    return( -1 );
170
0
  }
171
430k
  if( *stream_descriptor != NULL )
172
430k
  {
173
430k
    if( libfdata_stream_free(
174
430k
         &( ( *stream_descriptor )->data_stream ),
175
430k
         error ) != 1 )
176
0
    {
177
0
      libcerror_error_set(
178
0
       error,
179
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
180
0
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
181
0
       "%s: unable to free data stream.",
182
0
       function );
183
184
0
      result = -1;
185
0
    }
186
430k
    memory_free(
187
430k
     *stream_descriptor );
188
189
430k
    *stream_descriptor = NULL;
190
430k
  }
191
430k
  return( result );
192
430k
}
193
194
/* Sets the data range
195
 * Returns 1 if successful or -1 on error
196
 */
197
int libmdmp_stream_descriptor_set_data_range(
198
     libmdmp_stream_descriptor_t *stream_descriptor,
199
     off64_t data_offset,
200
     size64_t data_size,
201
     libcerror_error_t **error )
202
430k
{
203
430k
  static char *function = "libmdmp_stream_descriptor_set_data_range";
204
205
430k
  if( stream_descriptor == NULL )
206
0
  {
207
0
    libcerror_error_set(
208
0
     error,
209
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
210
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
211
0
     "%s: invalid stream descriptor.",
212
0
     function );
213
214
0
    return( -1 );
215
0
  }
216
430k
  if( libfdata_stream_set_segment_by_index(
217
430k
       stream_descriptor->data_stream,
218
430k
       0,
219
430k
       0,
220
430k
       data_offset,
221
430k
       data_size,
222
430k
       0,
223
430k
       error ) != 1 )
224
0
  {
225
0
    libcerror_error_set(
226
0
     error,
227
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
228
0
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
229
0
     "%s: unable to set segment in data stream.",
230
0
     function );
231
232
0
    return( -1 );
233
0
  }
234
430k
  return( 1 );
235
430k
}
236