Coverage Report

Created: 2025-06-13 07:22

/src/libfsntfs/libfsntfs/libfsntfs_cluster_block_data.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Cluster block data functions
3
 *
4
 * Copyright (C) 2010-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 <memory.h>
24
#include <types.h>
25
26
#include "libfsntfs_cluster_block_data.h"
27
#include "libfsntfs_libbfio.h"
28
#include "libfsntfs_libcerror.h"
29
#include "libfsntfs_libfdata.h"
30
#include "libfsntfs_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 libfsntfs_cluster_block_data_read_segment_data(
37
         intptr_t *data_handle LIBFSNTFS_ATTRIBUTE_UNUSED,
38
         libbfio_handle_t *file_io_handle,
39
         int segment_index LIBFSNTFS_ATTRIBUTE_UNUSED,
40
         int segment_file_index LIBFSNTFS_ATTRIBUTE_UNUSED,
41
         uint8_t *segment_data,
42
         size_t segment_data_size,
43
         uint32_t segment_flags,
44
         uint8_t read_flags LIBFSNTFS_ATTRIBUTE_UNUSED,
45
         libcerror_error_t **error )
46
15.1k
{
47
15.1k
  static char *function = "libfsntfs_cluster_block_data_read_segment_data";
48
15.1k
  ssize_t read_count    = 0;
49
50
15.1k
  LIBFSNTFS_UNREFERENCED_PARAMETER( data_handle )
51
15.1k
  LIBFSNTFS_UNREFERENCED_PARAMETER( segment_index )
52
15.1k
  LIBFSNTFS_UNREFERENCED_PARAMETER( segment_file_index )
53
15.1k
  LIBFSNTFS_UNREFERENCED_PARAMETER( read_flags )
54
55
15.1k
  if( segment_data == NULL )
56
0
  {
57
0
    libcerror_error_set(
58
0
     error,
59
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
60
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
61
0
     "%s: invalid segment data.",
62
0
     function );
63
64
0
    return( -1 );
65
0
  }
66
15.1k
  if( segment_data_size > (size_t) SSIZE_MAX )
67
0
  {
68
0
    libcerror_error_set(
69
0
     error,
70
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
71
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
72
0
     "%s: invalid segment data size value exceeds maximum.",
73
0
     function );
74
75
0
    return( -1 );
76
0
  }
77
15.1k
  if( ( segment_flags & LIBFDATA_RANGE_FLAG_IS_SPARSE ) != 0 )
78
847
  {
79
847
    if( memory_set(
80
847
         segment_data,
81
847
         0,
82
847
         segment_data_size ) == 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 segment data.",
89
0
       function );
90
91
0
      return( -1 );
92
0
    }
93
847
    read_count = (ssize_t) segment_data_size;
94
847
  }
95
14.2k
  else
96
14.2k
  {
97
14.2k
    read_count = libbfio_handle_read_buffer(
98
14.2k
                  file_io_handle,
99
14.2k
                  segment_data,
100
14.2k
                  segment_data_size,
101
14.2k
                  error );
102
103
14.2k
    if( read_count < 0 )
104
197
    {
105
197
      libcerror_error_set(
106
197
       error,
107
197
       LIBCERROR_ERROR_DOMAIN_IO,
108
197
       LIBCERROR_IO_ERROR_READ_FAILED,
109
197
       "%s: unable to read segment data.",
110
197
       function );
111
112
197
      return( -1 );
113
197
    }
114
14.2k
  }
115
14.9k
  return( read_count );
116
15.1k
}
117
118
/* Seeks a certain offset of the data
119
 * Callback for the cluster block data stream
120
 * Returns the offset if seek is successful or -1 on error
121
 */
122
off64_t libfsntfs_cluster_block_data_seek_segment_offset(
123
         intptr_t *data_handle LIBFSNTFS_ATTRIBUTE_UNUSED,
124
         libbfio_handle_t *file_io_handle,
125
         int segment_index LIBFSNTFS_ATTRIBUTE_UNUSED,
126
         int segment_file_index LIBFSNTFS_ATTRIBUTE_UNUSED,
127
         off64_t segment_offset,
128
         libcerror_error_t **error )
129
15.4k
{
130
15.4k
  static char *function = "libfsntfs_cluster_block_data_seek_segment_offset";
131
132
15.4k
  LIBFSNTFS_UNREFERENCED_PARAMETER( data_handle )
133
15.4k
  LIBFSNTFS_UNREFERENCED_PARAMETER( segment_index )
134
15.4k
  LIBFSNTFS_UNREFERENCED_PARAMETER( segment_file_index )
135
136
15.4k
  if( segment_offset < 0 )
137
0
  {
138
0
    libcerror_error_set(
139
0
     error,
140
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
141
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
142
0
     "%s: invalid segment offset value out of bounds.",
143
0
     function );
144
145
0
    return( -1 );
146
0
  }
147
15.4k
  if( libbfio_handle_seek_offset(
148
15.4k
       file_io_handle,
149
15.4k
       segment_offset,
150
15.4k
       SEEK_SET,
151
15.4k
       error ) == -1 )
152
0
  {
153
0
    libcerror_error_set(
154
0
     error,
155
0
     LIBCERROR_ERROR_DOMAIN_IO,
156
0
     LIBCERROR_IO_ERROR_SEEK_FAILED,
157
0
     "%s: unable to seek segment offset: %" PRIi64 " (0x%08" PRIx64 ").",
158
0
     function,
159
0
     segment_offset,
160
0
     segment_offset );
161
162
0
    return( -1 );
163
0
  }
164
15.4k
  return( segment_offset );
165
15.4k
}
166