Coverage Report

Created: 2025-06-24 07:14

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