MCPcopy Create free account
hub / github.com/Kitware/CMake / cmListFileLexer_ReadBOM

Function cmListFileLexer_ReadBOM

Source/LexerParser/cmListFileLexer.c:2740–2801  ·  view source on GitHub ↗

--------------------------------------------------------------------------*/

Source from the content-addressed store, hash-verified

2738
2739/*--------------------------------------------------------------------------*/
2740static cmListFileLexer_BOM cmListFileLexer_ReadBOM(FILE* f,
2741 unsigned char readBuffer[4],
2742 size_t* readSize)
2743{
2744 /* Read the up to four bytes that might correspond to a BOM. In case these
2745 bytes turn out not to represent a BOM, save them for later consumption in
2746 order to avoid seeking the file (which might not be seekable, e.g., if
2747 it's a pipe). */
2748 unsigned char* b = readBuffer;
2749
2750 size_t n = fread(b, 1, 2, f);
2751 *readSize = n; /* Initialize first and then accumulate */
2752
2753 if (n == 2) {
2754 if (b[0] == 0xEF && b[1] == 0xBB) {
2755 n = fread(b + 2, 1, 1, f);
2756 *readSize += n;
2757
2758 if (n == 1) {
2759 if (b[2] == 0xBF) {
2760 *readSize = 0; /* We consumed the BOM: discard it */
2761 return cmListFileLexer_BOM_UTF8;
2762 }
2763 }
2764 } else if (b[0] == 0xFE && b[1] == 0xFF) {
2765 *readSize = 0; /* We consumed the BOM: discard it */
2766 /* UTF-16 BE */
2767 return cmListFileLexer_BOM_UTF16BE;
2768 } else if (b[0] == 0 && b[1] == 0) {
2769 n = fread(b + 2, 1, 2, f);
2770 *readSize += n;
2771
2772 if (n == 2) {
2773 if (b[2] == 0xFE && b[3] == 0xFF) {
2774 *readSize = 0; /* We consumed the BOM: discard it */
2775 return cmListFileLexer_BOM_UTF32BE;
2776 }
2777 }
2778 } else if (b[0] == 0xFF && b[1] == 0xFE) {
2779 n = fread(b + 2, 1, 2, f);
2780 *readSize += n;
2781
2782 if (n == 2 && b[2] == 0 && b[3] == 0) {
2783 *readSize = 0; /* We consumed the BOM: discard it */
2784 return cmListFileLexer_BOM_UTF32LE;
2785 }
2786
2787 /* In case we were able to subsequently read only a single byte out of two
2788 (i.e., three in total), the file must be corrupt and the BOM cannot
2789 represent a UTF-16-LE BOM since each code unit must consist of two
2790 bytes. This avoids incorrectly detecting an incomplete UTF-32-LE BOM as
2791 UTF-16-LE input. */
2792 if (n % 2 == 0) {
2793 *readSize = n; /* We consumed the read bytes as BOM only partially */
2794 memmove(b, b + 2, n);
2795 return cmListFileLexer_BOM_UTF16LE;
2796 }
2797 }

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…