Coverage Report

Created: 2025-08-28 07:10

/src/libewf/libewf/libewf_data_stream.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Data stream 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 <types.h>
24
25
#include "libewf_buffer_data_handle.h"
26
#include "libewf_data_stream.h"
27
#include "libewf_libcerror.h"
28
#include "libewf_libfdata.h"
29
#include "libewf_section_data_handle.h"
30
31
/* Creates data stream from a buffer of data
32
 * Make sure the value data_stream is referencing, is set to NULL
33
 * Returns 1 if successful or -1 on error
34
 */
35
int libewf_data_stream_initialize_from_buffer(
36
     libfdata_stream_t **data_stream,
37
     const uint8_t *buffer,
38
     size_t buffer_size,
39
     libcerror_error_t **error )
40
0
{
41
0
  libewf_buffer_data_handle_t *data_handle = NULL;
42
0
  libfdata_stream_t *safe_data_stream      = NULL;
43
0
  static char *function                    = "libewf_data_stream_initialize_from_buffer";
44
0
  int segment_index                        = 0;
45
46
0
  if( data_stream == NULL )
47
0
  {
48
0
    libcerror_error_set(
49
0
     error,
50
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
51
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
52
0
     "%s: invalid data stream.",
53
0
     function );
54
55
0
    return( -1 );
56
0
  }
57
0
  if( libewf_buffer_data_handle_initialize(
58
0
       &data_handle,
59
0
       buffer,
60
0
       buffer_size,
61
0
       error ) != 1 )
62
0
  {
63
0
    libcerror_error_set(
64
0
     error,
65
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
66
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
67
0
     "%s: unable to create buffer data handle.",
68
0
     function );
69
70
0
    goto on_error;
71
0
  }
72
0
  if( libfdata_stream_initialize(
73
0
       &safe_data_stream,
74
0
       (intptr_t *) data_handle,
75
0
       (int (*)(intptr_t **, libcerror_error_t **)) &libewf_buffer_data_handle_free,
76
0
       NULL,
77
0
       NULL,
78
0
       (ssize_t (*)(intptr_t *, intptr_t *, int, int, uint8_t *, size_t, uint32_t, uint8_t, libcerror_error_t **)) &libewf_buffer_data_handle_read_segment_data,
79
0
       NULL,
80
0
       (off64_t (*)(intptr_t *, intptr_t *, int, int, off64_t, libcerror_error_t **)) &libewf_buffer_data_handle_seek_segment_offset,
81
0
       LIBFDATA_DATA_HANDLE_FLAG_MANAGED,
82
0
       error ) != 1 )
83
0
  {
84
0
    libcerror_error_set(
85
0
     error,
86
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
87
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
88
0
     "%s: unable to create data stream.",
89
0
     function );
90
91
0
    goto on_error;
92
0
  }
93
0
  data_handle = NULL;
94
95
0
  if( libfdata_stream_append_segment(
96
0
       safe_data_stream,
97
0
       &segment_index,
98
0
       0,
99
0
       0,
100
0
       (size64_t) buffer_size,
101
0
       0,
102
0
       error ) != 1 )
103
0
  {
104
0
    libcerror_error_set(
105
0
     error,
106
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
107
0
     LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
108
0
     "%s: unable to append data stream segment.",
109
0
     function );
110
111
0
    goto on_error;
112
0
  }
113
0
  *data_stream = safe_data_stream;
114
115
0
  return( 1 );
116
117
0
on_error:
118
0
  if( safe_data_stream != NULL )
119
0
  {
120
0
    libfdata_stream_free(
121
0
     &safe_data_stream,
122
0
     NULL );
123
0
  }
124
0
  if( data_handle != NULL )
125
0
  {
126
0
    libewf_buffer_data_handle_free(
127
0
     &data_handle,
128
0
     NULL );
129
0
  }
130
0
  return( -1 );
131
0
}
132
133
/* Creates data stream from a section
134
 * Make sure the value data_stream is referencing, is set to NULL
135
 * Returns 1 if successful or -1 on error
136
 */
137
int libewf_data_stream_initialize_from_section(
138
     libfdata_stream_t **data_stream,
139
     int file_io_pool_entry,
140
     off64_t data_offset,
141
     size64_t data_size,
142
     libcerror_error_t **error )
143
90
{
144
90
  libewf_section_data_handle_t *data_handle = NULL;
145
90
  libfdata_stream_t *safe_data_stream       = NULL;
146
90
  static char *function                     = "libewf_data_stream_initialize_from_section";
147
90
  int segment_index                         = 0;
148
149
90
  if( data_stream == NULL )
150
0
  {
151
0
    libcerror_error_set(
152
0
     error,
153
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
154
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
155
0
     "%s: invalid data stream.",
156
0
     function );
157
158
0
    return( -1 );
159
0
  }
160
90
  if( libewf_section_data_handle_initialize(
161
90
       &data_handle,
162
90
       data_offset,
163
90
       data_size,
164
90
       error ) != 1 )
165
0
  {
166
0
    libcerror_error_set(
167
0
     error,
168
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
169
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
170
0
     "%s: unable to create section data handle.",
171
0
     function );
172
173
0
    goto on_error;
174
0
  }
175
90
  if( libfdata_stream_initialize(
176
90
       &safe_data_stream,
177
90
       (intptr_t *) data_handle,
178
90
       (int (*)(intptr_t **, libcerror_error_t **)) &libewf_section_data_handle_free,
179
90
       NULL,
180
90
       NULL,
181
90
       (ssize_t (*)(intptr_t *, intptr_t *, int, int, uint8_t *, size_t, uint32_t, uint8_t, libcerror_error_t **)) &libewf_section_data_handle_read_segment_data,
182
90
       NULL,
183
90
       (off64_t (*)(intptr_t *, intptr_t *, int, int, off64_t, libcerror_error_t **)) &libewf_section_data_handle_seek_segment_offset,
184
90
       LIBFDATA_DATA_HANDLE_FLAG_MANAGED,
185
90
       error ) != 1 )
186
0
  {
187
0
    libcerror_error_set(
188
0
     error,
189
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
190
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
191
0
     "%s: unable to create data stream.",
192
0
     function );
193
194
0
    goto on_error;
195
0
  }
196
90
  data_handle = NULL;
197
198
90
  if( libfdata_stream_append_segment(
199
90
       safe_data_stream,
200
90
       &segment_index,
201
90
       file_io_pool_entry,
202
90
       0,
203
90
       data_size,
204
90
       0,
205
90
       error ) != 1 )
206
0
  {
207
0
    libcerror_error_set(
208
0
     error,
209
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
210
0
     LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
211
0
     "%s: unable to append data stream segment.",
212
0
     function );
213
214
0
    goto on_error;
215
0
  }
216
90
  *data_stream = safe_data_stream;
217
218
90
  return( 1 );
219
220
0
on_error:
221
0
  if( safe_data_stream != NULL )
222
0
  {
223
0
    libfdata_stream_free(
224
0
     &safe_data_stream,
225
0
     NULL );
226
0
  }
227
0
  if( data_handle != NULL )
228
0
  {
229
0
    libewf_section_data_handle_free(
230
0
     &data_handle,
231
0
     NULL );
232
0
  }
233
0
  return( -1 );
234
90
}
235