Coverage Report

Created: 2024-02-25 07:20

/src/libfsfat/libfsfat/libfsfat_io_handle.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Input/Output (IO) handle functions
3
 *
4
 * Copyright (C) 2021-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 <byte_stream.h>
24
#include <memory.h>
25
#include <system_string.h>
26
#include <types.h>
27
28
#include "libfsfat_debug.h"
29
#include "libfsfat_io_handle.h"
30
#include "libfsfat_libbfio.h"
31
#include "libfsfat_libcerror.h"
32
#include "libfsfat_libcnotify.h"
33
34
/* Creates an IO handle
35
 * Make sure the value io_handle is referencing, is set to NULL
36
 * Returns 1 if successful or -1 on error
37
 */
38
int libfsfat_io_handle_initialize(
39
     libfsfat_io_handle_t **io_handle,
40
     libcerror_error_t **error )
41
2.28k
{
42
2.28k
  static char *function = "libfsfat_io_handle_initialize";
43
44
2.28k
  if( io_handle == NULL )
45
0
  {
46
0
    libcerror_error_set(
47
0
     error,
48
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
49
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
50
0
     "%s: invalid IO handle.",
51
0
     function );
52
53
0
    return( -1 );
54
0
  }
55
2.28k
  if( *io_handle != NULL )
56
0
  {
57
0
    libcerror_error_set(
58
0
     error,
59
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
60
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
61
0
     "%s: invalid IO handle value already set.",
62
0
     function );
63
64
0
    return( -1 );
65
0
  }
66
2.28k
  *io_handle = memory_allocate_structure(
67
2.28k
                libfsfat_io_handle_t );
68
69
2.28k
  if( *io_handle == NULL )
70
0
  {
71
0
    libcerror_error_set(
72
0
     error,
73
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
74
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
75
0
     "%s: unable to create IO handle.",
76
0
     function );
77
78
0
    goto on_error;
79
0
  }
80
2.28k
  if( memory_set(
81
2.28k
       *io_handle,
82
2.28k
       0,
83
2.28k
       sizeof( libfsfat_io_handle_t ) ) == NULL )
84
0
  {
85
0
    libcerror_error_set(
86
0
     error,
87
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
88
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
89
0
     "%s: unable to clear IO handle.",
90
0
     function );
91
92
0
    goto on_error;
93
0
  }
94
2.28k
  return( 1 );
95
96
0
on_error:
97
0
  if( *io_handle != NULL )
98
0
  {
99
0
    memory_free(
100
0
     *io_handle );
101
102
0
    *io_handle = NULL;
103
0
  }
104
0
  return( -1 );
105
2.28k
}
106
107
/* Frees an IO handle
108
 * Returns 1 if successful or -1 on error
109
 */
110
int libfsfat_io_handle_free(
111
     libfsfat_io_handle_t **io_handle,
112
     libcerror_error_t **error )
113
2.28k
{
114
2.28k
  static char *function = "libfsfat_io_handle_free";
115
2.28k
  int result            = 1;
116
117
2.28k
  if( io_handle == NULL )
118
0
  {
119
0
    libcerror_error_set(
120
0
     error,
121
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
122
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
123
0
     "%s: invalid IO handle.",
124
0
     function );
125
126
0
    return( -1 );
127
0
  }
128
2.28k
  if( *io_handle != NULL )
129
2.28k
  {
130
2.28k
    memory_free(
131
2.28k
     *io_handle );
132
133
2.28k
    *io_handle = NULL;
134
2.28k
  }
135
2.28k
  return( result );
136
2.28k
}
137
138
/* Clears the IO handle
139
 * Returns 1 if successful or -1 on error
140
 */
141
int libfsfat_io_handle_clear(
142
     libfsfat_io_handle_t *io_handle,
143
     libcerror_error_t **error )
144
259
{
145
259
  static char *function = "libfsfat_io_handle_clear";
146
147
259
  if( io_handle == NULL )
148
0
  {
149
0
    libcerror_error_set(
150
0
     error,
151
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
152
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
153
0
     "%s: invalid IO handle.",
154
0
     function );
155
156
0
    return( -1 );
157
0
  }
158
259
  if( memory_set(
159
259
       io_handle,
160
259
       0,
161
259
       sizeof( libfsfat_io_handle_t ) ) == NULL )
162
0
  {
163
0
    libcerror_error_set(
164
0
     error,
165
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
166
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
167
0
     "%s: unable to clear IO handle.",
168
0
     function );
169
170
0
    return( -1 );
171
0
  }
172
259
  return( 1 );
173
259
}
174