Debian Codegraph @

gcc/ada/libgnat/i-cstrin.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- I N T E R F A C E S . C . S T R I N G S --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2025, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
17 -- --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
21 -- --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
26 -- --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
29 -- --
30 ------------------------------------------------------------------------------
31
32 with System; use System;
33 with System.Storage_Elements; use System.Storage_Elements;
34
35 with Ada.Unchecked_Conversion;
36
37 package body Interfaces.C.Strings with
38 SPARK_Mode => Off
39 is
40
41 -- Note that the type chars_ptr has a pragma No_Strict_Aliasing in the
42 -- spec, to prevent any assumptions about aliasing for values of this type,
43 -- since arbitrary addresses can be converted, and it is quite likely that
44 -- this type will in fact be used for aliasing values of other types.
45
46 -- Convert between chars_ptr and a C pointer
47 function To_chars_ptr is
48 new Ada.Unchecked_Conversion (System.Parameters.C_Address, chars_ptr);
49
50 function To_Address is
51 new Ada.Unchecked_Conversion (chars_ptr, System.Parameters.C_Address);
52
53 -----------------------
54 -- Local Subprograms --
55 -----------------------
56
57 function Peek (From : chars_ptr) return char;
58 pragma Inline (Peek);
59 -- Given a chars_ptr value, obtain referenced character
60
61 procedure Poke (Value : char; Into : chars_ptr);
62 pragma Inline (Poke);
63 -- Given a chars_ptr, modify referenced Character value
64
65 function "+" (Left : chars_ptr; Right : size_t) return chars_ptr;
66 pragma Inline ("+");
67 -- Address arithmetic on chars_ptr value
68
69 function Position_Of_Nul (Into : char_array) return size_t;
70 -- Returns position of the first Nul in Into or Into'Last + 1 if none
71
72 -- We can't use directly System.Memory because the categorization is not
73 -- compatible, so we directly import here the malloc and free routines.
74
75 function Memory_Alloc (Size : size_t) return chars_ptr;
76 pragma Import (C, Memory_Alloc, System.Parameters.C_Malloc_Linkname);
77 -- Allocate a chunk of memory on the heap
78
79 procedure Memory_Free (Address : chars_ptr);
80 pragma Import (C, Memory_Free, "__gnat_free");
81 -- Deallocate a previously allocated chunk of memory from the heap. On
82 -- runtimes that do not allow deallocation this is a no-op.
83
84 ---------
85 -- "+" --
86 ---------
87
88 function "+" (Left : chars_ptr; Right : size_t) return chars_ptr is
89 begin
90 return To_chars_ptr (To_Address (Left) + Storage_Offset (Right));
91 end "+";
92
93 ----------
94 -- Free --
95 ----------
96
97 procedure Free (Item : in out chars_ptr) is
98 begin
99 if Item /= Null_Ptr then
100 Memory_Free (Item);
101 Item := Null_Ptr;
102 end if;
103 end Free;
104
105 --------------------
106 -- New_Char_Array --
107 --------------------
108
109 function New_Char_Array (Chars : char_array) return chars_ptr is
110 Index : size_t;
111 Pointer : chars_ptr;
112
113 begin
114 -- Get index of position of null. If Index > Chars'Last,
115 -- nul is absent and must be added explicitly.
116
117 Index := Position_Of_Nul (Into => Chars);
118 Pointer := Memory_Alloc ((Index - Chars'First + 1));
119
120 -- If nul is present, transfer string up to and including nul
121
122 if Index <= Chars'Last then
123 Update (Item => Pointer,
124 Offset => 0,
125 Chars => Chars (Chars'First .. Index),
126 Check => False);
127 else
128 -- If original string has no nul, transfer whole string and add
129 -- terminator explicitly.
130
131 Update (Item => Pointer,
132 Offset => 0,
133 Chars => Chars,
134 Check => False);
135 Poke (nul, Into => Pointer + size_t'(Chars'Length));
136 end if;
137
138 return Pointer;
139 end New_Char_Array;
140
141 ----------------
142 -- New_String --
143 ----------------
144
145 function New_String (Str : String) return chars_ptr is
146
147 -- It's important that this subprogram uses the heap directly to compute
148 -- the result, and doesn't copy the string on the stack, otherwise its
149 -- use is limited when used from tasks on large strings.
150
151 Result : constant chars_ptr := Memory_Alloc (Str'Length + 1);
152
153 Result_Array : char_array (1 .. Str'Length + 1);
154 for Result_Array'Address use To_Address (Result);
155 pragma Import (Ada, Result_Array);
156
157 Count : size_t;
158
159 begin
160 To_C
161 (Item => Str,
162 Target => Result_Array,
163 Count => Count,
164 Append_Nul => True);
165 return Result;
166 end New_String;
167
168 ----------
169 -- Peek --
170 ----------
171
172 function Peek (From : chars_ptr) return char is
173 begin
174 return char (From.all);
175 end Peek;
176
177 ----------
178 -- Poke --
179 ----------
180
181 procedure Poke (Value : char; Into : chars_ptr) is
182 begin
183 Into.all := Character (Value);
184 end Poke;
185
186 ---------------------
187 -- Position_Of_Nul --
188 ---------------------
189
190 function Position_Of_Nul (Into : char_array) return size_t is
191 begin
192 pragma Annotate (Gnatcheck, Exempt_On, "Improper_Returns",
193 "early returns for performance");
194 for J in Into'Range loop
195 if Into (J) = nul then
196 return J;
197 end if;
198 end loop;
199
200 return Into'Last + 1;
201
202 pragma Annotate (Gnatcheck, Exempt_Off, "Improper_Returns");
203 end Position_Of_Nul;
204
205 ------------
206 -- Strlen --
207 ------------
208
209 function Strlen (Item : chars_ptr) return size_t is
210 Item_Index : size_t := 0;
211
212 begin
213 if Item = Null_Ptr then
214 raise Dereference_Error;
215 end if;
216
217 loop
218 if Peek (Item + Item_Index) = nul then
219 return Item_Index;
220 end if;
221
222 Item_Index := Item_Index + 1;
223 end loop;
224 end Strlen;
225
226 ------------------
227 -- To_Chars_Ptr --
228 ------------------
229
230 function To_Chars_Ptr
231 (Item : char_array_access;
232 Nul_Check : Boolean := False) return chars_ptr
233 is
234 begin
235 pragma Annotate (Gnatcheck, Exempt_On, "Improper_Returns",
236 "early returns for performance");
237 if Item = null then
238 return Null_Ptr;
239 elsif Nul_Check
240 and then Position_Of_Nul (Into => Item.all) > Item'Last
241 then
242 raise Terminator_Error;
243 else
244 return To_chars_ptr (Item (Item'First)'Address);
245 end if;
246
247 pragma Annotate (Gnatcheck, Exempt_Off, "Improper_Returns");
248 end To_Chars_Ptr;
249
250 ------------
251 -- Update --
252 ------------
253
254 procedure Update
255 (Item : chars_ptr;
256 Offset : size_t;
257 Chars : char_array;
258 Check : Boolean := True)
259 is
260 Index : chars_ptr := Item + Offset;
261
262 begin
263 if Check and then Offset + Chars'Length > Strlen (Item) then
264 raise Update_Error;
265 end if;
266
267 for J in Chars'Range loop
268 Poke (Chars (J), Into => Index);
269 Index := Index + size_t'(1);
270 end loop;
271 end Update;
272
273 procedure Update
274 (Item : chars_ptr;
275 Offset : size_t;
276 Str : String;
277 Check : Boolean := True)
278 is
279 begin
280 -- Note: in RM 95, the Append_Nul => False parameter is omitted. But
281 -- this has the unintended consequence of truncating the string after
282 -- an update. As discussed in Ada 2005 AI-242, this was unintended,
283 -- and should be corrected. Since this is a clear error, it seems
284 -- appropriate to apply the correction in Ada 95 mode as well.
285
286 Update (Item, Offset, To_C (Str, Append_Nul => False), Check);
287 end Update;
288
289 -----------
290 -- Value --
291 -----------
292
293 function Value (Item : chars_ptr) return char_array is
294 Result : char_array (0 .. Strlen (Item));
295
296 begin
297 if Item = Null_Ptr then
298 raise Dereference_Error;
299 end if;
300
301 -- Note that the following loop will also copy the terminating Nul
302
303 for J in Result'Range loop
304 Result (J) := Peek (Item + J);
305 end loop;
306
307 return Result;
308 end Value;
309
310 function Value
311 (Item : chars_ptr;
312 Length : size_t) return char_array
313 is
314 begin
315 pragma Annotate (Gnatcheck, Exempt_On, "Improper_Returns",
316 "early returns for performance");
317 if Item = Null_Ptr then
318 raise Dereference_Error;
319 end if;
320
321 -- ACATS cxb3010 checks that Constraint_Error gets raised when Length
322 -- is 0. Seems better to check that Length is not null before declaring
323 -- an array with size_t bounds of 0 .. Length - 1 anyway.
324
325 if Length = 0 then
326 raise Constraint_Error;
327 end if;
328
329 declare
330 Result : char_array (0 .. Length - 1);
331
332 begin
333 for J in Result'Range loop
334 Result (J) := Peek (Item + J);
335
336 if Result (J) = nul then
337 return Result (0 .. J);
338 end if;
339 end loop;
340
341 return Result;
342 end;
343
344 pragma Annotate (Gnatcheck, Exempt_Off, "Improper_Returns");
345 end Value;
346
347 function Value (Item : chars_ptr) return String is
348 begin
349 return To_Ada (Value (Item));
350 end Value;
351
352 function Value (Item : chars_ptr; Length : size_t) return String is
353 Result : String (1 .. Natural (Length));
354 C : char;
355
356 begin
357 pragma Annotate (Gnatcheck, Exempt_On, "Improper_Returns",
358 "early returns for performance");
359
360 -- As per AI-00177, this is equivalent to:
361
362 -- To_Ada (Value (Item, Length) & nul);
363
364 if Item = Null_Ptr then
365 raise Dereference_Error;
366 end if;
367
368 for J in Result'Range loop
369 C := Peek (Item + size_t (J - 1));
370
371 if C = nul then
372 return Result (1 .. J - 1);
373 else
374 Result (J) := To_Ada (C);
375 end if;
376 end loop;
377
378 return Result;
379
380 pragma Annotate (Gnatcheck, Exempt_Off, "Improper_Returns");
381 end Value;
382
383 end Interfaces.C.Strings;