Coverage Report

Created: 2026-08-31 07:43

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