OrcaSlicer latest
OrcaSlicer
Loading...
Searching...
No Matches
encoding-check.cpp File Reference
#include <iosfwd>
#include <vector>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
Include dependency graph for encoding-check.cpp:

Go to the source code of this file.

Functions

unsigned char * utf8_check (unsigned char *s)
bool check_file (const char *target, const char *filename)
int main (int argc, char const *argv[])

Function Documentation

◆ check_file()

bool check_file ( const char * target,
const char * filename )
74{
75 std::ifstream file(filename, std::ios::binary | std::ios::ate);
76 if (!file.is_open()) {
77 std::cerr << "\n\tError: Could not open file: " << filename
78 << "\n\tTarget: " << target << "\n" << std::endl;
79 return false;
80 }
81
82 const std::streampos pos = file.tellg();
83
84 // Fix: tellg() returns -1 (cast to streampos) on failure.
85 // Also guard against implausibly large files before casting to size_t.
86 if (pos < 0) {
87 std::cerr << "\n\tError: Could not determine file size: " << filename
88 << "\n\tTarget: " << target << "\n" << std::endl;
89 return false;
90 }
91
92 const auto size = static_cast<std::size_t>(pos);
93
94 if (size == 0)
95 return true;
96
97 file.seekg(0, std::ios::beg);
98 std::vector<char> buffer(size);
99
100 // Fix: cast size to streamsize explicitly — streampos and streamsize are
101 // distinct types and passing streampos where streamsize is expected is
102 // implementation-defined behaviour on some platforms.
103 if (!file.read(buffer.data(), static_cast<std::streamsize>(size))) {
104 std::cerr << "\n\tError: Could not read source file: " << filename
105 << "\n\tTarget: " << target << "\n" << std::endl;
106 return false;
107 }
108
109 buffer.push_back('\0');
110
111 if (utf8_check(reinterpret_cast<unsigned char*>(buffer.data())) != nullptr) {
112 std::cerr << "\n\tError: Source file does not contain (valid) UTF-8: " << filename
113 << "\n\tTarget: " << target << "\n" << std::endl;
114 return false;
115 }
116
117 if (buffer.size() >= 3
118 && buffer[0] == '\xef'
119 && buffer[1] == '\xbb'
120 && buffer[2] == '\xbf') {
121 std::cerr << "\n\tError: Source file is valid UTF-8 but contains a BOM mark: " << filename
122 << "\n\tTarget: " << target << "\n" << std::endl;
123 return false;
124 }
125
126 return true;
127}
unsigned char * utf8_check(unsigned char *s)
Definition encoding-check.cpp:27
Vec3d pos(const Pt &p)
Definition ReprojectPointsOnMesh.hpp:16

References utf8_check().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( int argc,
char const * argv[] )
130{
131 if (argc < 3) {
132 std::cerr << "Usage: " << argv[0] << " <target> <file|@responsefile> [...]" << std::endl;
133 return -1;
134 }
135
136 const char* target = argv[1];
137
138 // Collect all files — support @responsefile syntax
139 std::vector<std::string> files;
140 for (int i = 2; i < argc; i++) {
141 if (argv[i][0] == '@') {
142 // Response file — read paths from it, one per line
143 std::ifstream rsp(argv[i] + 1);
144 if (!rsp.is_open()) {
145 std::cerr << "Could not open response file: " << (argv[i]+1) << std::endl;
146 return -1;
147 }
148 std::string line;
149 while (std::getline(rsp, line)) {
150 if (!line.empty())
151 files.push_back(line);
152 }
153 } else {
154 files.push_back(argv[i]);
155 }
156 }
157
158 bool all_ok = true;
159 for (const auto& f : files) {
160 if (!check_file(target, f.c_str()))
161 all_ok = false;
162 }
163
164 return all_ok ? 0 : -2;
165}
bool check_file(const char *target, const char *filename)
Definition encoding-check.cpp:73

References check_file().

Referenced by DropDown::messureSize().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ utf8_check()

unsigned char * utf8_check ( unsigned char * s)
28{
29 while (*s) {
30 if (*s < 0x80) {
31 // 0xxxxxxx
32 s++;
33 } else if ((s[0] & 0xe0) == 0xc0) {
34 // 110xxxxx 10xxxxxx
35 if ((s[1] & 0xc0) != 0x80 ||
36 (s[0] & 0xfe) == 0xc0) { // overlong?
37 return s;
38 } else {
39 s += 2;
40 }
41 } else if ((s[0] & 0xf0) == 0xe0) {
42 // 1110xxxx 10xxxxxx 10xxxxxx
43 if ((s[1] & 0xc0) != 0x80 ||
44 (s[2] & 0xc0) != 0x80 ||
45 (s[0] == 0xe0 && (s[1] & 0xe0) == 0x80) || // overlong?
46 (s[0] == 0xed && (s[1] & 0xe0) == 0xa0) || // surrogate?
47 (s[0] == 0xef && s[1] == 0xbf &&
48 (s[2] & 0xfe) == 0xbe)) { // U+FFFE or U+FFFF?
49 return s;
50 } else {
51 s += 3;
52 }
53 } else if ((s[0] & 0xf8) == 0xf0) {
54 // 11110xxX 10xxxxxx 10xxxxxx 10xxxxxx
55 if ((s[1] & 0xc0) != 0x80 ||
56 (s[2] & 0xc0) != 0x80 ||
57 (s[3] & 0xc0) != 0x80 ||
58 (s[0] == 0xf0 && (s[1] & 0xf0) == 0x80) || // overlong?
59 (s[0] == 0xf4 && s[1] > 0x8f) || s[0] > 0xf4) { // > U+10FFFF?
60 return s;
61 } else {
62 s += 4;
63 }
64 } else {
65 return s;
66 }
67 }
68
69 return NULL;
70}

Referenced by check_file().

Here is the caller graph for this function: