Coverage Report

Created: 2023-06-07 06:53

/src/libbde/libfvalue/libfvalue_utf16_string.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * UTF-16 string value functions
3
 *
4
 * Copyright (C) 2010-2022, 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 <types.h>
24
25
#include "libfvalue_libcerror.h"
26
#include "libfvalue_split_utf16_string.h"
27
#include "libfvalue_types.h"
28
29
/* Splits an UTF-16 string
30
 * Returns 1 if successful or -1 on error
31
 */
32
int libfvalue_utf16_string_split(
33
     const uint16_t *utf16_string,
34
     size_t utf16_string_size,
35
     uint16_t delimiter,
36
     libfvalue_split_utf16_string_t **split_string,
37
     libcerror_error_t **error )
38
0
{
39
0
  uint16_t *segment_start = NULL;
40
0
  uint16_t *segment_end   = NULL;
41
0
  uint16_t *string_end    = NULL;
42
0
  static char *function   = "libfvalue_utf16_string_split";
43
0
  size_t string_size      = 0;
44
0
  ssize_t segment_length  = 0;
45
0
  int number_of_segments  = 0;
46
0
  int segment_index       = 0;
47
48
0
  if( utf16_string == NULL )
49
0
  {
50
0
    libcerror_error_set(
51
0
     error,
52
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
53
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
54
0
     "%s: invalid UTF-16 string.",
55
0
     function );
56
57
0
    return( -1 );
58
0
  }
59
0
  if( utf16_string_size > (size_t) SSIZE_MAX )
60
0
  {
61
0
    libcerror_error_set(
62
0
     error,
63
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
64
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
65
0
     "%s: invalid UTF-16 string size value exceeds maximum.",
66
0
     function );
67
68
0
    return( -1 );
69
0
  }
70
0
  if( split_string == NULL )
71
0
  {
72
0
    libcerror_error_set(
73
0
     error,
74
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
75
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
76
0
     "%s: invalid split string.",
77
0
     function );
78
79
0
    return( -1 );
80
0
  }
81
0
  if( *split_string != NULL )
82
0
  {
83
0
    libcerror_error_set(
84
0
     error,
85
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
86
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
87
0
     "%s: invalid split string already set.",
88
0
     function );
89
90
0
    return( -1 );
91
0
  }
92
  /* An empty string has no segments
93
   */
94
0
  if( ( utf16_string_size == 0 )
95
0
   || ( utf16_string[ 0 ] == 0 ) )
96
0
  {
97
0
    return( 1 );
98
0
  }
99
  /* Determine the number of segments
100
   */
101
0
  segment_start = (uint16_t *) utf16_string;
102
0
  string_end    = (uint16_t *) &( utf16_string[ utf16_string_size - 1 ] );
103
104
0
  do
105
0
  {
106
0
    segment_end = segment_start;
107
108
0
    while( segment_end <= string_end )
109
0
    {
110
0
      if( ( segment_end == string_end )
111
0
       || ( *segment_end == 0 ) )
112
0
      {
113
0
        segment_end = NULL;
114
115
0
        break;
116
0
      }
117
0
      else if( *segment_end == delimiter )
118
0
      {
119
0
        break;
120
0
      }
121
0
      segment_end++;
122
0
    }
123
0
    if( segment_end > string_end )
124
0
    {
125
0
      break;
126
0
    }
127
0
    segment_index++;
128
129
0
    if( segment_end == NULL )
130
0
    {
131
0
      break;
132
0
    }
133
0
    if( segment_end == segment_start )
134
0
    {
135
0
      segment_start++;
136
0
    }
137
0
    else if( segment_end != utf16_string )
138
0
    {
139
0
      segment_start = segment_end + 1;
140
0
    }
141
0
  }
142
0
  while( segment_end != NULL );
143
144
0
  number_of_segments = segment_index;
145
146
0
  if( libfvalue_split_utf16_string_initialize(
147
0
       split_string,
148
0
       utf16_string,
149
0
       utf16_string_size,
150
0
       number_of_segments,
151
0
       error ) != 1 )
152
0
  {
153
0
    libcerror_error_set(
154
0
     error,
155
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
156
0
     LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
157
0
     "%s: unable to initialize split string.",
158
0
     function );
159
160
0
    goto on_error;
161
0
  }
162
0
  if( *split_string == NULL )
163
0
  {
164
0
    libcerror_error_set(
165
0
     error,
166
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
167
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
168
0
     "%s: missing split string.",
169
0
     function );
170
171
0
    goto on_error;
172
0
  }
173
  /* Do not bother splitting empty strings
174
   */
175
0
  if( number_of_segments == 0 )
176
0
  {
177
0
    return( 1 );
178
0
  }
179
  /* Determine the segments
180
   * empty segments are stored as strings only containing the end of character
181
   */
182
0
  if( libfvalue_split_utf16_string_get_string(
183
0
       *split_string,
184
0
       &segment_start,
185
0
       &string_size,
186
0
       error ) != 1 )
187
0
  {
188
0
    libcerror_error_set(
189
0
     error,
190
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
191
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
192
0
     "%s: unable to retrieve split UTF-16 string.",
193
0
     function );
194
195
0
    goto on_error;
196
0
  }
197
0
  if( segment_start == NULL )
198
0
  {
199
0
    libcerror_error_set(
200
0
     error,
201
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
202
0
     LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
203
0
     "%s: missing segment start.",
204
0
     function );
205
206
0
    goto on_error;
207
0
  }
208
0
  if( string_size < 1 )
209
0
  {
210
0
    libcerror_error_set(
211
0
     error,
212
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
213
0
     LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
214
0
     "%s: invalid string size value out of bounds.",
215
0
     function );
216
217
0
    goto on_error;
218
0
  }
219
0
  string_end = &( segment_start[ string_size - 1 ] );
220
221
0
  for( segment_index = 0;
222
0
       segment_index < number_of_segments;
223
0
       segment_index++ )
224
0
  {
225
0
    segment_end = segment_start;
226
227
0
    while( segment_end <= string_end )
228
0
    {
229
0
      if( ( segment_end == string_end )
230
0
       || ( *segment_end == 0 ) )
231
0
      {
232
0
        segment_end = NULL;
233
234
0
        break;
235
0
      }
236
0
      else if( *segment_end == delimiter )
237
0
      {
238
0
        break;
239
0
      }
240
0
      segment_end++;
241
0
    }
242
0
    if( segment_end == NULL )
243
0
    {
244
0
      segment_length = (ssize_t) ( string_end - segment_start );
245
0
    }
246
0
    else
247
0
    {
248
0
      segment_length = (ssize_t) ( segment_end - segment_start );
249
0
    }
250
0
    if( segment_length >= 0 )
251
0
    {
252
0
      segment_start[ segment_length ] = 0;
253
254
0
      if( libfvalue_split_utf16_string_set_segment_by_index(
255
0
           *split_string,
256
0
           segment_index,
257
0
           segment_start,
258
0
           segment_length + 1,
259
0
           error ) != 1 )
260
0
      {
261
0
        libcerror_error_set(
262
0
         error,
263
0
         LIBCERROR_ERROR_DOMAIN_RUNTIME,
264
0
         LIBCERROR_RUNTIME_ERROR_SET_FAILED,
265
0
         "%s: unable to set split UTF-16 string segment: %d.",
266
0
         function,
267
0
         segment_index );
268
269
0
        goto on_error;
270
0
      }
271
0
    }
272
0
    if( segment_end == NULL )
273
0
    {
274
0
      break;
275
0
    }
276
0
    if( segment_end == string_end )
277
0
    {
278
0
      segment_start++;
279
0
    }
280
0
    if( segment_end != string_end )
281
0
    {
282
0
      segment_start = segment_end + 1;
283
0
    }
284
0
  }
285
0
  return( 1 );
286
287
0
on_error:
288
0
  if( *split_string != NULL )
289
0
  {
290
0
    libfvalue_split_utf16_string_free(
291
0
     split_string,
292
0
     NULL );
293
0
  }
294
0
  return( -1 );
295
0
}
296