/src/libnk2/libnk2/libnk2_io_handle.c
Line | Count | Source |
1 | | /* |
2 | | * Input/Output (IO) handle functions |
3 | | * |
4 | | * Copyright (C) 2009-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 <byte_stream.h> |
24 | | #include <memory.h> |
25 | | #include <types.h> |
26 | | |
27 | | #include "libnk2_codepage.h" |
28 | | #include "libnk2_io_handle.h" |
29 | | #include "libnk2_libcerror.h" |
30 | | |
31 | | const uint8_t nk2_file_signature[ 4 ] = { 0x0d, 0xf0, 0xad, 0xba }; |
32 | | |
33 | | /* Creates an IO handle |
34 | | * Make sure the value io_handle is referencing, is set to NULL |
35 | | * Returns 1 if successful or -1 on error |
36 | | */ |
37 | | int libnk2_io_handle_initialize( |
38 | | libnk2_io_handle_t **io_handle, |
39 | | libcerror_error_t **error ) |
40 | 563 | { |
41 | 563 | static char *function = "libnk2_io_handle_initialize"; |
42 | | |
43 | 563 | if( io_handle == NULL ) |
44 | 0 | { |
45 | 0 | libcerror_error_set( |
46 | 0 | error, |
47 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
48 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
49 | 0 | "%s: invalid IO handle.", |
50 | 0 | function ); |
51 | |
|
52 | 0 | return( -1 ); |
53 | 0 | } |
54 | 563 | if( *io_handle != NULL ) |
55 | 0 | { |
56 | 0 | libcerror_error_set( |
57 | 0 | error, |
58 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
59 | 0 | LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET, |
60 | 0 | "%s: invalid IO handle value already set.", |
61 | 0 | function ); |
62 | |
|
63 | 0 | return( -1 ); |
64 | 0 | } |
65 | 563 | *io_handle = memory_allocate_structure( |
66 | 563 | libnk2_io_handle_t ); |
67 | | |
68 | 563 | if( *io_handle == NULL ) |
69 | 0 | { |
70 | 0 | libcerror_error_set( |
71 | 0 | error, |
72 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
73 | 0 | LIBCERROR_MEMORY_ERROR_INSUFFICIENT, |
74 | 0 | "%s: unable to create IO handle.", |
75 | 0 | function ); |
76 | |
|
77 | 0 | goto on_error; |
78 | 0 | } |
79 | 563 | if( memory_set( |
80 | 563 | *io_handle, |
81 | 563 | 0, |
82 | 563 | sizeof( libnk2_io_handle_t ) ) == NULL ) |
83 | 0 | { |
84 | 0 | libcerror_error_set( |
85 | 0 | error, |
86 | 0 | LIBCERROR_ERROR_DOMAIN_MEMORY, |
87 | 0 | LIBCERROR_MEMORY_ERROR_SET_FAILED, |
88 | 0 | "%s: unable to clear file.", |
89 | 0 | function ); |
90 | |
|
91 | 0 | goto on_error; |
92 | 0 | } |
93 | 563 | ( *io_handle )->ascii_codepage = LIBNK2_CODEPAGE_WINDOWS_1252; |
94 | | |
95 | 563 | return( 1 ); |
96 | | |
97 | 0 | on_error: |
98 | 0 | if( *io_handle != NULL ) |
99 | 0 | { |
100 | 0 | memory_free( |
101 | 0 | *io_handle ); |
102 | |
|
103 | 0 | *io_handle = NULL; |
104 | 0 | } |
105 | 0 | return( -1 ); |
106 | 563 | } |
107 | | |
108 | | /* Frees an IO handle |
109 | | * Returns 1 if successful or -1 on error |
110 | | */ |
111 | | int libnk2_io_handle_free( |
112 | | libnk2_io_handle_t **io_handle, |
113 | | libcerror_error_t **error ) |
114 | 563 | { |
115 | 563 | static char *function = "libnk2_io_handle_free"; |
116 | | |
117 | 563 | 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 | 563 | if( *io_handle != NULL ) |
129 | 563 | { |
130 | 563 | memory_free( |
131 | 563 | *io_handle ); |
132 | | |
133 | 563 | *io_handle = NULL; |
134 | 563 | } |
135 | 563 | return( 1 ); |
136 | 563 | } |
137 | | |
138 | | /* Clears the IO handle |
139 | | * Returns 1 if successful or -1 on error |
140 | | */ |
141 | | int libnk2_io_handle_clear( |
142 | | libnk2_io_handle_t *io_handle, |
143 | | libcerror_error_t **error ) |
144 | 4 | { |
145 | 4 | static char *function = "libnk2_io_handle_clear"; |
146 | | |
147 | 4 | 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 | 4 | if( memory_set( |
159 | 4 | io_handle, |
160 | 4 | 0, |
161 | 4 | sizeof( libnk2_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 | 4 | io_handle->ascii_codepage = LIBNK2_CODEPAGE_WINDOWS_1252; |
173 | | |
174 | 4 | return( 1 ); |
175 | 4 | } |
176 | | |