Coverage Report

Created: 2025-06-24 07:14

/src/libluksde/libluksde/libluksde_io_handle.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Input/Output (IO) handle functions
3
 *
4
 * Copyright (C) 2013-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 "libluksde_io_handle.h"
27
#include "libluksde_libcerror.h"
28
29
const uint8_t luksde_signature[ 6 ] = { 'L', 'U', 'K', 'S', 0xba, 0xbe };
30
31
/* Creates an IO handle
32
 * Make sure the value io_handle is referencing, is set to NULL
33
 * Returns 1 if successful or -1 on error
34
 */
35
int libluksde_io_handle_initialize(
36
     libluksde_io_handle_t **io_handle,
37
     libcerror_error_t **error )
38
949
{
39
949
  static char *function = "libluksde_io_handle_initialize";
40
41
949
  if( io_handle == 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 IO handle.",
48
0
     function );
49
50
0
    return( -1 );
51
0
  }
52
949
  if( *io_handle != 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 IO handle value already set.",
59
0
     function );
60
61
0
    return( -1 );
62
0
  }
63
949
  *io_handle = memory_allocate_structure(
64
949
                libluksde_io_handle_t );
65
66
949
  if( *io_handle == 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 IO handle.",
73
0
     function );
74
75
0
    goto on_error;
76
0
  }
77
949
  if( memory_set(
78
949
       *io_handle,
79
949
       0,
80
949
       sizeof( libluksde_io_handle_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 IO handle.",
87
0
     function );
88
89
0
    goto on_error;
90
0
  }
91
949
  ( *io_handle )->bytes_per_sector = 512;
92
93
949
  return( 1 );
94
95
0
on_error:
96
0
  if( *io_handle != NULL )
97
0
  {
98
0
    memory_free(
99
0
     *io_handle );
100
101
0
    *io_handle = NULL;
102
0
  }
103
0
  return( -1 );
104
949
}
105
106
/* Frees an IO handle
107
 * Returns 1 if successful or -1 on error
108
 */
109
int libluksde_io_handle_free(
110
     libluksde_io_handle_t **io_handle,
111
     libcerror_error_t **error )
112
949
{
113
949
  static char *function = "libluksde_io_handle_free";
114
949
  int result            = 1;
115
116
949
  if( io_handle == NULL )
117
0
  {
118
0
    libcerror_error_set(
119
0
     error,
120
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
121
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
122
0
     "%s: invalid IO handle.",
123
0
     function );
124
125
0
    return( -1 );
126
0
  }
127
949
  if( *io_handle != NULL )
128
949
  {
129
949
    if( libluksde_io_handle_clear(
130
949
         *io_handle,
131
949
         error ) != 1 )
132
0
    {
133
0
      libcerror_error_set(
134
0
       error,
135
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
136
0
       LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
137
0
       "%s: unable to clear IO handle.",
138
0
       function );
139
140
0
      result = -1;
141
0
    }
142
949
    memory_free(
143
949
     *io_handle );
144
145
949
    *io_handle = NULL;
146
949
  }
147
949
  return( result );
148
949
}
149
150
/* Clears the IO handle
151
 * Returns 1 if successful or -1 on error
152
 */
153
int libluksde_io_handle_clear(
154
     libluksde_io_handle_t *io_handle,
155
     libcerror_error_t **error )
156
1.00k
{
157
1.00k
  static char *function = "libluksde_io_handle_clear";
158
159
1.00k
  if( io_handle == NULL )
160
0
  {
161
0
    libcerror_error_set(
162
0
     error,
163
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
164
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
165
0
     "%s: invalid IO handle.",
166
0
     function );
167
168
0
    return( -1 );
169
0
  }
170
1.00k
  io_handle->bytes_per_sector = 512;
171
172
1.00k
  return( 1 );
173
1.00k
}
174