Coverage Report

Created: 2023-06-07 06:53

/src/libwrc/libwrc/libwrc_manifest_values.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Manifest values functions
3
 *
4
 * Copyright (C) 2011-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 "libwrc_data_descriptor.h"
27
#include "libwrc_definitions.h"
28
#include "libwrc_io_handle.h"
29
#include "libwrc_language_entry.h"
30
#include "libwrc_libbfio.h"
31
#include "libwrc_libcerror.h"
32
#include "libwrc_libcnotify.h"
33
#include "libwrc_libfvalue.h"
34
#include "libwrc_manifest_values.h"
35
36
/* Reads manifest values
37
 * Returns 1 if successful or -1 on error
38
 */
39
int libwrc_manifest_values_read(
40
     libwrc_language_entry_t *language_entry,
41
     libwrc_io_handle_t *io_handle,
42
     libbfio_handle_t *file_io_handle,
43
     libwrc_data_descriptor_t *data_descriptor,
44
     libcerror_error_t **error )
45
0
{
46
0
  libfvalue_value_t *manifest_value = NULL;
47
0
  uint8_t *resource_data            = NULL;
48
0
  static char *function             = "libwrc_manifest_values_read";
49
0
  off64_t file_offset               = 0;
50
0
  size_t resource_data_size         = 0;
51
0
  ssize_t read_count                = 0;
52
0
  int value_index                   = 0;
53
54
0
  if( language_entry == NULL )
55
0
  {
56
0
    libcerror_error_set(
57
0
     error,
58
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
59
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
60
0
     "%s: invalid language entry.",
61
0
     function );
62
63
0
    return( -1 );
64
0
  }
65
0
  if( io_handle == NULL )
66
0
  {
67
0
    libcerror_error_set(
68
0
     error,
69
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
70
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
71
0
     "%s: invalid IO handle.",
72
0
     function );
73
74
0
    return( -1 );
75
0
  }
76
0
  if( data_descriptor == NULL )
77
0
  {
78
0
    libcerror_error_set(
79
0
     error,
80
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
81
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
82
0
     "%s: invalid data descriptor.",
83
0
     function );
84
85
0
    return( -1 );
86
0
  }
87
0
  resource_data_size = (size_t) data_descriptor->size;
88
89
0
  if( ( resource_data_size == 0 )
90
0
   || ( resource_data_size > (size_t) MEMORY_MAXIMUM_ALLOCATION_SIZE ) )
91
0
  {
92
0
    libcerror_error_set(
93
0
     error,
94
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
95
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
96
0
     "%s: invalid resource data size value out of bounds.",
97
0
     function );
98
99
0
    goto on_error;
100
0
  }
101
0
  resource_data = (uint8_t *) memory_allocate(
102
0
                               sizeof( uint8_t ) * resource_data_size );
103
104
0
  if( resource_data == NULL )
105
0
  {
106
0
    libcerror_error_set(
107
0
     error,
108
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
109
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
110
0
     "%s: unable to create resource data.",
111
0
     function );
112
113
0
    goto on_error;
114
0
  }
115
0
  file_offset = data_descriptor->virtual_address - io_handle->virtual_address;
116
117
0
  read_count = libbfio_handle_read_buffer_at_offset(
118
0
                file_io_handle,
119
0
                resource_data,
120
0
                resource_data_size,
121
0
                file_offset,
122
0
                error );
123
124
0
  if( read_count != (ssize_t) resource_data_size )
125
0
  {
126
0
    libcerror_error_set(
127
0
     error,
128
0
     LIBCERROR_ERROR_DOMAIN_IO,
129
0
     LIBCERROR_IO_ERROR_READ_FAILED,
130
0
     "%s: unable to read resource data at offset: %" PRIi64 " (0x%08" PRIx64 ").",
131
0
     function,
132
0
     file_offset,
133
0
     file_offset );
134
135
0
    goto on_error;
136
0
  }
137
#if defined( HAVE_DEBUG_OUTPUT )
138
  if( libcnotify_verbose != 0 )
139
  {
140
    libcnotify_printf(
141
     "%s: resource data:\n",
142
     function );
143
    libcnotify_print_data(
144
     resource_data,
145
     resource_data_size,
146
     0 );
147
  }
148
#endif
149
0
  if( libfvalue_value_type_initialize(
150
0
       &manifest_value,
151
0
       LIBFVALUE_VALUE_TYPE_STRING_UTF8,
152
0
       error ) != 1 )
153
0
  {
154
0
    libcerror_error_set(
155
0
     error,
156
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
157
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
158
0
     "%s: unable to create manifest value.",
159
0
     function );
160
161
0
    goto on_error;
162
0
  }
163
0
  if( libfvalue_value_set_data(
164
0
       manifest_value,
165
0
       resource_data,
166
0
       resource_data_size,
167
0
       LIBFVALUE_CODEPAGE_UTF8,
168
0
       LIBFVALUE_VALUE_DATA_FLAG_MANAGED,
169
0
       error ) != 1 )
170
0
  {
171
0
    libcerror_error_set(
172
0
     error,
173
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
174
0
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
175
0
     "%s: unable to set data of manifest value.",
176
0
     function );
177
178
0
    goto on_error;
179
0
  }
180
#if defined( HAVE_DEBUG_OUTPUT )
181
  if( libcnotify_verbose != 0 )
182
  {
183
    libcnotify_printf(
184
     "%s: manifest string:\n",
185
     function );
186
187
    if( libfvalue_value_print(
188
         manifest_value,
189
         0,
190
         0,
191
         error ) != 1 )
192
    {
193
      libcerror_error_set(
194
       error,
195
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
196
       LIBCERROR_RUNTIME_ERROR_PRINT_FAILED,
197
       "%s: unable to print manifest value.",
198
       function );
199
200
      goto on_error;
201
    }
202
    libcnotify_printf(
203
     "\n\n" );
204
  }
205
#endif
206
0
  if( libwrc_language_entry_append_value(
207
0
       language_entry,
208
0
       &value_index,
209
0
       (intptr_t *) manifest_value,
210
0
       error ) != 1 )
211
0
  {
212
0
    libcerror_error_set(
213
0
     error,
214
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
215
0
     LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
216
0
     "%s: unable to append manifest value.",
217
0
     function );
218
219
0
    goto on_error;
220
0
  }
221
0
  manifest_value = NULL;
222
223
0
  memory_free(
224
0
   resource_data );
225
226
0
  return( 1 );
227
228
0
on_error:
229
0
  if( manifest_value != NULL )
230
0
  {
231
0
    libfvalue_value_free(
232
0
     &manifest_value,
233
0
     NULL );
234
0
  }
235
0
  if( resource_data != NULL )
236
0
  {
237
0
    memory_free(
238
0
     resource_data );
239
0
  }
240
0
  return( -1 );
241
0
}
242