Coverage Report

Created: 2025-06-22 07:35

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