| 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 |
-- S p e c -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 1993-2025, Free Software Foundation, Inc. -- |
| 10 |
-- -- |
| 11 |
-- This specification is derived from the Ada Reference Manual for use with -- |
| 12 |
-- GNAT. The copyright notice above, and the license provisions that follow -- |
| 13 |
-- apply solely to the contents of the part following the private keyword. -- |
| 14 |
-- -- |
| 15 |
-- GNAT is free software; you can redistribute it and/or modify it under -- |
| 16 |
-- terms of the GNU General Public License as published by the Free Soft- -- |
| 17 |
-- ware Foundation; either version 3, or (at your option) any later ver- -- |
| 18 |
-- sion. GNAT is distributed in the hope that it will be useful, but WITH- -- |
| 19 |
-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -- |
| 20 |
-- or FITNESS FOR A PARTICULAR PURPOSE. -- |
| 21 |
-- -- |
| 22 |
-- As a special exception under Section 7 of GPL version 3, you are granted -- |
| 23 |
-- additional permissions described in the GCC Runtime Library Exception, -- |
| 24 |
-- version 3.1, as published by the Free Software Foundation. -- |
| 25 |
-- -- |
| 26 |
-- You should have received a copy of the GNU General Public License and -- |
| 27 |
-- a copy of the GCC Runtime Library Exception along with this program; -- |
| 28 |
-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see -- |
| 29 |
-- <http://www.gnu.org/licenses/>. -- |
| 30 |
-- -- |
| 31 |
-- GNAT was originally developed by the GNAT team at New York University. -- |
| 32 |
-- Extensive contributions were provided by Ada Core Technologies Inc. -- |
| 33 |
-- -- |
| 34 |
------------------------------------------------------------------------------ |
| 35 |
|
| 36 |
-- This package declares types and subprograms that allow the allocation, |
| 37 |
-- reference, update and deallocation of C-style strings, as defined by |
| 38 |
-- ARM B.3.1. |
| 39 |
|
| 40 |
-- Preconditions in this unit are meant for analysis only, not for run-time |
| 41 |
-- checking, so that the expected exceptions are raised. This is enforced by |
| 42 |
-- setting the corresponding assertion policy to Ignore. These preconditions |
| 43 |
-- protect from Constraint_Error, Dereference_Error and Update_Error, but not |
| 44 |
-- from Storage_Error. |
| 45 |
|
| 46 |
pragma Assertion_Policy (Pre => Ignore); |
| 47 |
|
| 48 |
package Interfaces.C.Strings with |
| 49 |
SPARK_Mode => On, |
| 50 |
Abstract_State => (C_Memory), |
| 51 |
Initializes => (C_Memory), |
| 52 |
Always_Terminates |
| 53 |
is |
| 54 |
pragma Preelaborate; |
| 55 |
|
| 56 |
-- Definitions for C character arrays |
| 57 |
type char_array_access is access all char_array; |
| 58 |
for char_array_access'Size use System.Parameters.ptr_bits; |
| 59 |
|
| 60 |
pragma No_Strict_Aliasing (char_array_access); |
| 61 |
-- Since this type is used for external interfacing, with the pointer |
| 62 |
-- coming from who knows where, it seems a good idea to turn off any |
| 63 |
-- strict aliasing assumptions for this type. |
| 64 |
|
| 65 |
type chars_ptr is private; |
| 66 |
pragma Preelaborable_Initialization (chars_ptr); |
| 67 |
|
| 68 |
type chars_ptr_array is array (size_t range <>) of aliased chars_ptr; |
| 69 |
|
| 70 |
Null_Ptr : constant chars_ptr; |
| 71 |
-- Null value for private type chars_ptr |
| 72 |
|
| 73 |
function To_Chars_Ptr |
| 74 |
(Item : char_array_access; |
| 75 |
Nul_Check : Boolean := False) return chars_ptr |
| 76 |
with |
| 77 |
SPARK_Mode => Off; -- To_Chars_Ptr'Result is aliased with Item |
| 78 |
-- Extract raw chars_ptr from char_array access type |
| 79 |
|
| 80 |
function New_Char_Array (Chars : char_array) return chars_ptr with |
| 81 |
Volatile_Function, |
| 82 |
Post => New_Char_Array'Result /= Null_Ptr, |
| 83 |
Global => (Input => C_Memory); |
| 84 |
-- Copy the contents of Chars into a newly allocated chars_ptr |
| 85 |
|
| 86 |
function New_String (Str : String) return chars_ptr with |
| 87 |
Volatile_Function, |
| 88 |
Post => New_String'Result /= Null_Ptr, |
| 89 |
Global => (Input => C_Memory); |
| 90 |
-- Copy the contents of Str into a newly allocated chars_ptr |
| 91 |
|
| 92 |
procedure Free (Item : in out chars_ptr) with |
| 93 |
SPARK_Mode => Off; |
| 94 |
-- When deallocation is prohibited (eg: cert runtimes) this routine |
| 95 |
-- will raise Program_Error. |
| 96 |
|
| 97 |
Dereference_Error : exception; |
| 98 |
-- This exception is raised when a subprogram of this unit tries to |
| 99 |
-- dereference a chars_ptr with the value Null_Ptr. |
| 100 |
|
| 101 |
-- The Value functions copy the contents of a chars_ptr object |
| 102 |
-- into a char_array/String. |
| 103 |
function Value (Item : chars_ptr) return char_array with |
| 104 |
Pre => Item /= Null_Ptr, |
| 105 |
Global => (Input => C_Memory); |
| 106 |
|
| 107 |
function Value |
| 108 |
(Item : chars_ptr; |
| 109 |
Length : size_t) return char_array |
| 110 |
with |
| 111 |
Pre => Item /= Null_Ptr and then Length /= 0, |
| 112 |
Global => (Input => C_Memory); |
| 113 |
|
| 114 |
function Value (Item : chars_ptr) return String with |
| 115 |
Pre => Item /= Null_Ptr, |
| 116 |
Global => (Input => C_Memory); |
| 117 |
|
| 118 |
function Value |
| 119 |
(Item : chars_ptr; |
| 120 |
Length : size_t) return String |
| 121 |
with |
| 122 |
Pre => Item /= Null_Ptr and then Length /= 0, |
| 123 |
Global => (Input => C_Memory); |
| 124 |
|
| 125 |
function Strlen (Item : chars_ptr) return size_t with |
| 126 |
Pre => Item /= Null_Ptr, |
| 127 |
Global => (Input => C_Memory); |
| 128 |
-- Return the length of a string contained in a chars_ptr |
| 129 |
|
| 130 |
-- Update the contents of a chars_ptr with a char_array/String. If the |
| 131 |
-- update exceeds the original length of the chars_ptr the Update_Error |
| 132 |
-- exception is raised. |
| 133 |
procedure Update |
| 134 |
(Item : chars_ptr; |
| 135 |
Offset : size_t; |
| 136 |
Chars : char_array; |
| 137 |
Check : Boolean := True) |
| 138 |
with |
| 139 |
Pre => |
| 140 |
Item /= Null_Ptr |
| 141 |
and then (Chars'First /= 0 or else Chars'Last /= size_t'Last) |
| 142 |
and then Chars'Length <= size_t'Last - Offset |
| 143 |
and then Chars'Length + Offset <= Strlen (Item), |
| 144 |
Global => (In_Out => C_Memory); |
| 145 |
|
| 146 |
procedure Update |
| 147 |
(Item : chars_ptr; |
| 148 |
Offset : size_t; |
| 149 |
Str : String; |
| 150 |
Check : Boolean := True) |
| 151 |
with |
| 152 |
Pre => |
| 153 |
Item /= Null_Ptr |
| 154 |
and then Str'Length <= size_t'Last - Offset |
| 155 |
and then Str'Length + Offset <= Strlen (Item), |
| 156 |
Global => (In_Out => C_Memory); |
| 157 |
|
| 158 |
Update_Error : exception; |
| 159 |
|
| 160 |
private |
| 161 |
pragma SPARK_Mode (Off); |
| 162 |
type chars_ptr is access all Character; |
| 163 |
for chars_ptr'Size use System.Parameters.ptr_bits; |
| 164 |
|
| 165 |
pragma No_Strict_Aliasing (chars_ptr); |
| 166 |
-- Since this type is used for external interfacing, with the pointer |
| 167 |
-- coming from who knows where, it seems a good idea to turn off any |
| 168 |
-- strict aliasing assumptions for this type. |
| 169 |
|
| 170 |
Null_Ptr : constant chars_ptr := null; |
| 171 |
end Interfaces.C.Strings; |