Debian Codegraph @

libcsplit/libcsplit_narrow_string.c

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