Coverage Report

Created: 2023-06-07 06:53

/src/libfsfat/libfsfat/libfsfat_cluster_block_data.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Cluster block data functions
3
 *
4
 * Copyright (C) 2021-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 "libfsfat_cluster_block_data.h"
27
#include "libfsfat_libbfio.h"
28
#include "libfsfat_libcerror.h"
29
#include "libfsfat_libfdata.h"
30
#include "libfsfat_unused.h"
31
32
/* Reads data from the current offset into a buffer
33
 * Callback for the cluster block data stream
34
 * Returns the number of bytes read or -1 on error
35
 */
36
ssize_t libfsfat_cluster_block_data_read_segment_data(
37
         intptr_t *data_handle LIBFSFAT_ATTRIBUTE_UNUSED,
38
         libbfio_handle_t *file_io_handle,
39
         int segment_index LIBFSFAT_ATTRIBUTE_UNUSED,
40
         int segment_file_index LIBFSFAT_ATTRIBUTE_UNUSED,
41
         uint8_t *segment_data,
42
         size_t segment_data_size,
43
         uint32_t segment_flags LIBFSFAT_ATTRIBUTE_UNUSED,
44
         uint8_t read_flags LIBFSFAT_ATTRIBUTE_UNUSED,
45
         libcerror_error_t **error )
46
0
{
47
0
  static char *function = "libfsfat_cluster_block_data_read_segment_data";
48
0
  ssize_t read_count    = 0;
49
50
0
  LIBFSFAT_UNREFERENCED_PARAMETER( data_handle )
51
0
  LIBFSFAT_UNREFERENCED_PARAMETER( segment_index )
52
0
  LIBFSFAT_UNREFERENCED_PARAMETER( segment_file_index )
53
0
  LIBFSFAT_UNREFERENCED_PARAMETER( segment_flags )
54
0
  LIBFSFAT_UNREFERENCED_PARAMETER( read_flags )
55
56
0
  if( segment_data == NULL )
57
0
  {
58
0
    libcerror_error_set(
59
0
     error,
60
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
61
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
62
0
     "%s: invalid segment data.",
63
0
     function );
64
65
0
    return( -1 );
66
0
  }
67
0
  if( segment_data_size > (size_t) SSIZE_MAX )
68
0
  {
69
0
    libcerror_error_set(
70
0
     error,
71
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
72
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
73
0
     "%s: invalid segment data size value exceeds maximum.",
74
0
     function );
75
76
0
    return( -1 );
77
0
  }
78
0
  read_count = libbfio_handle_read_buffer(
79
0
                file_io_handle,
80
0
                segment_data,
81
0
                segment_data_size,
82
0
                error );
83
84
0
  if( read_count < 0 )
85
0
  {
86
0
    libcerror_error_set(
87
0
     error,
88
0
     LIBCERROR_ERROR_DOMAIN_IO,
89
0
     LIBCERROR_IO_ERROR_READ_FAILED,
90
0
     "%s: unable to read segment data.",
91
0
     function );
92
93
0
    return( -1 );
94
0
  }
95
0
  return( read_count );
96
0
}
97
98
/* Seeks a certain offset of the data
99
 * Callback for the cluster block data stream
100
 * Returns the offset if seek is successful or -1 on error
101
 */
102
off64_t libfsfat_cluster_block_data_seek_segment_offset(
103
         intptr_t *data_handle LIBFSFAT_ATTRIBUTE_UNUSED,
104
         libbfio_handle_t *file_io_handle,
105
         int segment_index LIBFSFAT_ATTRIBUTE_UNUSED,
106
         int segment_file_index LIBFSFAT_ATTRIBUTE_UNUSED,
107
         off64_t segment_offset,
108
         libcerror_error_t **error )
109
0
{
110
0
  static char *function = "libfsfat_cluster_block_data_seek_segment_offset";
111
112
0
  LIBFSFAT_UNREFERENCED_PARAMETER( data_handle )
113
0
  LIBFSFAT_UNREFERENCED_PARAMETER( segment_index )
114
0
  LIBFSFAT_UNREFERENCED_PARAMETER( segment_file_index )
115
116
0
  if( segment_offset < 0 )
117
0
  {
118
0
    libcerror_error_set(
119
0
     error,
120
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
121
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
122
0
     "%s: invalid segment offset value out of bounds.",
123
0
     function );
124
125
0
    return( -1 );
126
0
  }
127
0
  if( libbfio_handle_seek_offset(
128
0
       file_io_handle,
129
0
       segment_offset,
130
0
       SEEK_SET,
131
0
       error ) == -1 )
132
0
  {
133
0
    libcerror_error_set(
134
0
     error,
135
0
     LIBCERROR_ERROR_DOMAIN_IO,
136
0
     LIBCERROR_IO_ERROR_SEEK_FAILED,
137
0
     "%s: unable to seek segment offset: %" PRIi64 " (0x%08" PRIx64 ").",
138
0
     function,
139
0
     segment_offset,
140
0
     segment_offset );
141
142
0
    return( -1 );
143
0
  }
144
0
  return( segment_offset );
145
0
}
146