Coverage Report

Created: 2023-06-07 06:53

/src/libvsgpt/libvsgpt/libvsgpt_io_handle.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * The IO handle functions
3
 *
4
 * Copyright (C) 2019-2023, 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 "libvsgpt_io_handle.h"
27
#include "libvsgpt_libcerror.h"
28
29
/* Creates an IO handle
30
 * Make sure the value io_handle is referencing, is set to NULL
31
 * Returns 1 if successful or -1 on error
32
 */
33
int libvsgpt_io_handle_initialize(
34
     libvsgpt_io_handle_t **io_handle,
35
     libcerror_error_t **error )
36
2.24k
{
37
2.24k
  static char *function = "libvsgpt_io_handle_initialize";
38
39
2.24k
  if( io_handle == 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 IO handle.",
46
0
     function );
47
48
0
    return( -1 );
49
0
  }
50
2.24k
  if( *io_handle != 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 IO handle value already set.",
57
0
     function );
58
59
0
    return( -1 );
60
0
  }
61
2.24k
  *io_handle = memory_allocate_structure(
62
2.24k
                libvsgpt_io_handle_t );
63
64
2.24k
  if( *io_handle == 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 IO handle.",
71
0
     function );
72
73
0
    goto on_error;
74
0
  }
75
2.24k
  if( memory_set(
76
2.24k
       *io_handle,
77
2.24k
       0,
78
2.24k
       sizeof( libvsgpt_io_handle_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 IO handle.",
85
0
     function );
86
87
0
    goto on_error;
88
0
  }
89
2.24k
  ( *io_handle )->bytes_per_sector = 512;
90
91
2.24k
  return( 1 );
92
93
0
on_error:
94
0
  if( *io_handle != NULL )
95
0
  {
96
0
    memory_free(
97
0
     *io_handle );
98
99
0
    *io_handle = NULL;
100
0
  }
101
0
  return( -1 );
102
2.24k
}
103
104
/* Frees an IO handle
105
 * Returns 1 if successful or -1 on error
106
 */
107
int libvsgpt_io_handle_free(
108
     libvsgpt_io_handle_t **io_handle,
109
     libcerror_error_t **error )
110
2.24k
{
111
2.24k
  static char *function = "libvsgpt_io_handle_free";
112
113
2.24k
  if( io_handle == NULL )
114
0
  {
115
0
    libcerror_error_set(
116
0
     error,
117
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
118
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
119
0
     "%s: invalid IO handle.",
120
0
     function );
121
122
0
    return( -1 );
123
0
  }
124
2.24k
  if( *io_handle != NULL )
125
2.24k
  {
126
2.24k
    memory_free(
127
2.24k
     *io_handle );
128
129
2.24k
    *io_handle = NULL;
130
2.24k
  }
131
2.24k
  return( 1 );
132
2.24k
}
133
134
/* Clears the IO handle
135
 * Returns 1 if successful or -1 on error
136
 */
137
int libvsgpt_io_handle_clear(
138
     libvsgpt_io_handle_t *io_handle,
139
     libcerror_error_t **error )
140
113
{
141
113
  static char *function = "libvsgpt_io_handle_clear";
142
143
113
  if( io_handle == 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 IO handle.",
150
0
     function );
151
152
0
    return( -1 );
153
0
  }
154
113
  if( memory_set(
155
113
       io_handle,
156
113
       0,
157
113
       sizeof( libvsgpt_io_handle_t ) ) == NULL )
158
0
  {
159
0
    libcerror_error_set(
160
0
     error,
161
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
162
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
163
0
     "%s: unable to clear IO handle.",
164
0
     function );
165
166
0
    return( -1 );
167
0
  }
168
113
  io_handle->bytes_per_sector = 512;
169
170
113
  return( 1 );
171
113
}
172