| 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 |
-- B o d y -- |
| 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 |
package body GNAT.Byte_Order_Mark is |
| 33 |
|
| 34 |
-------------- |
| 35 |
-- Read_BOM -- |
| 36 |
-------------- |
| 37 |
|
| 38 |
procedure Read_BOM |
| 39 |
(Str : String; |
| 40 |
Len : out Natural; |
| 41 |
BOM : out BOM_Kind; |
| 42 |
XML_Support : Boolean := False) |
| 43 |
is |
| 44 |
begin |
| 45 |
-- Note: the order of these tests is important, because in some cases |
| 46 |
-- one sequence is a prefix of a longer sequence, and we must test for |
| 47 |
-- the longer sequence first |
| 48 |
|
| 49 |
-- UTF-32 (big-endian) |
| 50 |
|
| 51 |
if Str'Length >= 4 |
| 52 |
and then Str (Str'First) = Character'Val (16#00#) |
| 53 |
and then Str (Str'First + 1) = Character'Val (16#00#) |
| 54 |
and then Str (Str'First + 2) = Character'Val (16#FE#) |
| 55 |
and then Str (Str'First + 3) = Character'Val (16#FF#) |
| 56 |
then |
| 57 |
Len := 4; |
| 58 |
BOM := UTF32_BE; |
| 59 |
|
| 60 |
-- UTF-32 (little-endian) |
| 61 |
|
| 62 |
elsif Str'Length >= 4 |
| 63 |
and then Str (Str'First) = Character'Val (16#FF#) |
| 64 |
and then Str (Str'First + 1) = Character'Val (16#FE#) |
| 65 |
and then Str (Str'First + 2) = Character'Val (16#00#) |
| 66 |
and then Str (Str'First + 3) = Character'Val (16#00#) |
| 67 |
then |
| 68 |
Len := 4; |
| 69 |
BOM := UTF32_LE; |
| 70 |
|
| 71 |
-- UTF-16 (big-endian) |
| 72 |
|
| 73 |
elsif Str'Length >= 2 |
| 74 |
and then Str (Str'First) = Character'Val (16#FE#) |
| 75 |
and then Str (Str'First + 1) = Character'Val (16#FF#) |
| 76 |
then |
| 77 |
Len := 2; |
| 78 |
BOM := UTF16_BE; |
| 79 |
|
| 80 |
-- UTF-16 (little-endian) |
| 81 |
|
| 82 |
elsif Str'Length >= 2 |
| 83 |
and then Str (Str'First) = Character'Val (16#FF#) |
| 84 |
and then Str (Str'First + 1) = Character'Val (16#FE#) |
| 85 |
then |
| 86 |
Len := 2; |
| 87 |
BOM := UTF16_LE; |
| 88 |
|
| 89 |
-- UTF-8 (endian-independent) |
| 90 |
|
| 91 |
elsif Str'Length >= 3 |
| 92 |
and then Str (Str'First) = Character'Val (16#EF#) |
| 93 |
and then Str (Str'First + 1) = Character'Val (16#BB#) |
| 94 |
and then Str (Str'First + 2) = Character'Val (16#BF#) |
| 95 |
then |
| 96 |
Len := 3; |
| 97 |
BOM := UTF8_All; |
| 98 |
|
| 99 |
-- UCS-4 (big-endian) XML only |
| 100 |
|
| 101 |
elsif XML_Support |
| 102 |
and then Str'Length >= 4 |
| 103 |
and then Str (Str'First) = Character'Val (16#00#) |
| 104 |
and then Str (Str'First + 1) = Character'Val (16#00#) |
| 105 |
and then Str (Str'First + 2) = Character'Val (16#00#) |
| 106 |
and then Str (Str'First + 3) = Character'Val (16#3C#) |
| 107 |
then |
| 108 |
Len := 0; |
| 109 |
BOM := UCS4_BE; |
| 110 |
|
| 111 |
-- UCS-4 (little-endian) XML case |
| 112 |
|
| 113 |
elsif XML_Support |
| 114 |
and then Str'Length >= 4 |
| 115 |
and then Str (Str'First) = Character'Val (16#3C#) |
| 116 |
and then Str (Str'First + 1) = Character'Val (16#00#) |
| 117 |
and then Str (Str'First + 2) = Character'Val (16#00#) |
| 118 |
and then Str (Str'First + 3) = Character'Val (16#00#) |
| 119 |
then |
| 120 |
Len := 0; |
| 121 |
BOM := UCS4_LE; |
| 122 |
|
| 123 |
-- UCS-4 (unusual byte order 2143) XML case |
| 124 |
|
| 125 |
elsif XML_Support |
| 126 |
and then Str'Length >= 4 |
| 127 |
and then Str (Str'First) = Character'Val (16#00#) |
| 128 |
and then Str (Str'First + 1) = Character'Val (16#00#) |
| 129 |
and then Str (Str'First + 2) = Character'Val (16#3C#) |
| 130 |
and then Str (Str'First + 3) = Character'Val (16#00#) |
| 131 |
then |
| 132 |
Len := 0; |
| 133 |
BOM := UCS4_2143; |
| 134 |
|
| 135 |
-- UCS-4 (unusual byte order 3412) XML case |
| 136 |
|
| 137 |
elsif XML_Support |
| 138 |
and then Str'Length >= 4 |
| 139 |
and then Str (Str'First) = Character'Val (16#00#) |
| 140 |
and then Str (Str'First + 1) = Character'Val (16#3C#) |
| 141 |
and then Str (Str'First + 2) = Character'Val (16#00#) |
| 142 |
and then Str (Str'First + 3) = Character'Val (16#00#) |
| 143 |
then |
| 144 |
Len := 0; |
| 145 |
BOM := UCS4_3412; |
| 146 |
|
| 147 |
-- UTF-16 (big-endian) XML case |
| 148 |
|
| 149 |
elsif XML_Support |
| 150 |
and then Str'Length >= 4 |
| 151 |
and then Str (Str'First) = Character'Val (16#00#) |
| 152 |
and then Str (Str'First + 1) = Character'Val (16#3C#) |
| 153 |
and then Str (Str'First + 2) = Character'Val (16#00#) |
| 154 |
and then Str (Str'First + 3) = Character'Val (16#3F#) |
| 155 |
then |
| 156 |
Len := 0; |
| 157 |
BOM := UTF16_BE; |
| 158 |
|
| 159 |
-- UTF-32 (little-endian) XML case |
| 160 |
|
| 161 |
elsif XML_Support |
| 162 |
and then Str'Length >= 4 |
| 163 |
and then Str (Str'First) = Character'Val (16#3C#) |
| 164 |
and then Str (Str'First + 1) = Character'Val (16#00#) |
| 165 |
and then Str (Str'First + 2) = Character'Val (16#3F#) |
| 166 |
and then Str (Str'First + 3) = Character'Val (16#00#) |
| 167 |
then |
| 168 |
Len := 0; |
| 169 |
BOM := UTF16_LE; |
| 170 |
|
| 171 |
-- Unrecognized special encodings XML only |
| 172 |
|
| 173 |
elsif XML_Support |
| 174 |
and then Str'Length >= 4 |
| 175 |
and then Str (Str'First) = Character'Val (16#3C#) |
| 176 |
and then Str (Str'First + 1) = Character'Val (16#3F#) |
| 177 |
and then Str (Str'First + 2) = Character'Val (16#78#) |
| 178 |
and then Str (Str'First + 3) = Character'Val (16#6D#) |
| 179 |
then |
| 180 |
-- UTF-8, ASCII, some part of ISO8859, Shift-JIS, EUC,... |
| 181 |
|
| 182 |
Len := 0; |
| 183 |
BOM := Unknown; |
| 184 |
|
| 185 |
-- No BOM recognized |
| 186 |
|
| 187 |
else |
| 188 |
Len := 0; |
| 189 |
BOM := Unknown; |
| 190 |
end if; |
| 191 |
end Read_BOM; |
| 192 |
|
| 193 |
end GNAT.Byte_Order_Mark; |