| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- G N A T . B Y T E _ O R D E R _ M A R K -- |
| 6 |
-- -- |
| 7 |
-- S p e c -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 2006-2025, AdaCore -- |
| 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 |
-- This package provides a procedure for reading and interpreting the BOM |
| 33 |
-- (byte order mark) used to publish the encoding method for a string (for |
| 34 |
-- example, a UTF-8 encoded file in windows will start with the appropriate |
| 35 |
-- BOM sequence to signal UTF-8 encoding). |
| 36 |
|
| 37 |
-- There are two cases |
| 38 |
|
| 39 |
-- Case 1. UTF encodings for Unicode files |
| 40 |
|
| 41 |
-- Here the convention is to have the first character of the file be a |
| 42 |
-- non-breaking zero width space character (16#0000_FEFF#). For the UTF |
| 43 |
-- encodings, the representation of this character can be used to uniquely |
| 44 |
-- determine the encoding. Furthermore, the possibility of any confusion |
| 45 |
-- with unencoded files is minimal, since for example the UTF-8 encoding |
| 46 |
-- of this character looks like the sequence: |
| 47 |
|
| 48 |
-- LC_I_Diaeresis |
| 49 |
-- Right_Angle_Quotation |
| 50 |
-- Fraction_One_Half |
| 51 |
|
| 52 |
-- which is so unlikely to occur legitimately in normal use that it can |
| 53 |
-- safely be ignored in most cases (for example, no legitimate Ada source |
| 54 |
-- file could start with this sequence of characters). |
| 55 |
|
| 56 |
-- Case 2. Specialized XML encodings |
| 57 |
|
| 58 |
-- The XML standard defines a number of other possible encodings and also |
| 59 |
-- defines standardized sequences for marking these encodings. This package |
| 60 |
-- can also optionally handle these XML defined BOM sequences. These XML |
| 61 |
-- cases depend on the first character of the XML file being < so that the |
| 62 |
-- encoding of this character can be recognized. |
| 63 |
|
| 64 |
package GNAT.Byte_Order_Mark is |
| 65 |
|
| 66 |
type BOM_Kind is |
| 67 |
(UTF8_All, -- UTF8-encoding |
| 68 |
UTF16_LE, -- UTF16 little-endian encoding |
| 69 |
UTF16_BE, -- UTF16 big-endian encoding |
| 70 |
UTF32_LE, -- UTF32 little-endian encoding |
| 71 |
UTF32_BE, -- UTF32 big-endian encoding |
| 72 |
|
| 73 |
-- The following cases are for XML only |
| 74 |
|
| 75 |
UCS4_BE, -- UCS-4, big endian machine (1234 order) |
| 76 |
UCS4_LE, -- UCS-4, little endian machine (4321 order) |
| 77 |
UCS4_2143, -- UCS-4, unusual byte order (2143 order) |
| 78 |
UCS4_3412, -- UCS-4, unusual byte order (3412 order) |
| 79 |
|
| 80 |
-- Value returned if no BOM recognized |
| 81 |
|
| 82 |
Unknown); -- Unknown, assumed to be ASCII compatible |
| 83 |
|
| 84 |
procedure Read_BOM |
| 85 |
(Str : String; |
| 86 |
Len : out Natural; |
| 87 |
BOM : out BOM_Kind; |
| 88 |
XML_Support : Boolean := False); |
| 89 |
-- This is the routine to read the BOM from the start of the given string |
| 90 |
-- Str. On return BOM is set to the appropriate BOM_Kind and Len is set to |
| 91 |
-- its length. The caller will typically skip the first Len characters in |
| 92 |
-- the string to ignore the BOM sequence. The special XML possibilities are |
| 93 |
-- recognized only if flag XML_Support is set to True. Note that for the |
| 94 |
-- XML cases, Len is always set to zero on return (not to the length of the |
| 95 |
-- relevant sequence) since in the XML cases, the sequence recognized is |
| 96 |
-- for the first real character in the file (<) which is not to be skipped. |
| 97 |
|
| 98 |
end GNAT.Byte_Order_Mark; |