1 /* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2    file Copyright.txt or https://cmake.org/licensing#kwsys for details.  */
3 #include "kwsysPrivate.h"
4 #include KWSYS_HEADER(FStream.hxx)
5 
6 // Work-around CMake dependency scanning limitation.  This must
7 // duplicate the above list of headers.
8 #if 0
9 #include "FStream.hxx.in"
10 #endif
11 
12 namespace KWSYS_NAMESPACE {
13 namespace FStream {
14 
ReadBOM(std::istream & in)15 BOM ReadBOM(std::istream& in)
16 {
17   if (!in.good()) {
18     return BOM_None;
19   }
20   unsigned long orig = in.tellg();
21   unsigned char bom[4];
22   in.read(reinterpret_cast<char*>(bom), 2);
23   if (!in.good()) {
24     in.clear();
25     in.seekg(orig);
26     return BOM_None;
27   }
28   if (bom[0] == 0xEF && bom[1] == 0xBB) {
29     in.read(reinterpret_cast<char*>(bom + 2), 1);
30     if (in.good() && bom[2] == 0xBF) {
31       return BOM_UTF8;
32     }
33   } else if (bom[0] == 0xFE && bom[1] == 0xFF) {
34     return BOM_UTF16BE;
35   } else if (bom[0] == 0x00 && bom[1] == 0x00) {
36     in.read(reinterpret_cast<char*>(bom + 2), 2);
37     if (in.good() && bom[2] == 0xFE && bom[3] == 0xFF) {
38       return BOM_UTF32BE;
39     }
40   } else if (bom[0] == 0xFF && bom[1] == 0xFE) {
41     unsigned long p = in.tellg();
42     in.read(reinterpret_cast<char*>(bom + 2), 2);
43     if (in.good() && bom[2] == 0x00 && bom[3] == 0x00) {
44       return BOM_UTF32LE;
45     }
46     in.seekg(p);
47     return BOM_UTF16LE;
48   }
49   in.clear();
50   in.seekg(orig);
51   return BOM_None;
52 }
53 
54 } // FStream namespace
55 } // KWSYS_NAMESPACE
56