/src/poppler/poppler/Error.cc
Line | Count | Source (jump to first uncovered line) |
1 | | //======================================================================== |
2 | | // |
3 | | // Error.cc |
4 | | // |
5 | | // Copyright 1996-2003 Glyph & Cog, LLC |
6 | | // |
7 | | //======================================================================== |
8 | | |
9 | | //======================================================================== |
10 | | // |
11 | | // Modified under the Poppler project - http://poppler.freedesktop.org |
12 | | // |
13 | | // All changes made under the Poppler project to this file are licensed |
14 | | // under GPL version 2 or later |
15 | | // |
16 | | // Copyright (C) 2005, 2007 Jeff Muizelaar <jeff@infidigm.net> |
17 | | // Copyright (C) 2005, 2018, 2022 Albert Astals Cid <aacid@kde.org> |
18 | | // Copyright (C) 2007 Krzysztof Kowalczyk <kkowalczyk@gmail.com> |
19 | | // Copyright (C) 2012 Marek Kasik <mkasik@redhat.com> |
20 | | // Copyright (C) 2013, 2017 Adrian Johnson <ajohnson@redneon.com> |
21 | | // Copyright (C) 2020 Adam Reichold <adam.reichold@t-online.de> |
22 | | // Copyright (C) 2024 Oliver Sander <oliver.sander@tu-dresden.de> |
23 | | // Copyright (C) 2024 Vincent Lefevre <vincent@vinc17.net> |
24 | | // Copyright (C) 2024 g10 Code GmbH, Author: Sune Stolborg Vuorela <sune@vuorela.dk> |
25 | | // |
26 | | // To see a description of the changes please see the Changelog file that |
27 | | // came with your tarball or type make ChangeLog if you are building from git |
28 | | // |
29 | | //======================================================================== |
30 | | |
31 | | #include <config.h> |
32 | | #include <poppler-config.h> |
33 | | |
34 | | #include <cstdio> |
35 | | #include <cstddef> |
36 | | #include <cstdarg> |
37 | | #include "goo/GooString.h" |
38 | | #include "GlobalParams.h" |
39 | | #include "Error.h" |
40 | | |
41 | | static const char *errorCategoryNames[] = { "Syntax Warning", "Syntax Error", "Config Error", "Command Line Error", "I/O Error", "Permission Error", "Unimplemented Feature", "Internal Error" }; |
42 | | |
43 | | static ErrorCallback errorCbk = nullptr; |
44 | | |
45 | | void setErrorCallback(ErrorCallback cbk) |
46 | 121k | { |
47 | 121k | errorCbk = cbk; |
48 | 121k | } |
49 | | |
50 | | void CDECL error(ErrorCategory category, Goffset pos, const char *msg, ...) |
51 | 546M | { |
52 | 546M | va_list args; |
53 | | |
54 | | // NB: this can be called before the globalParams object is created |
55 | 546M | if (!errorCbk && globalParams && globalParams->getErrQuiet()) { |
56 | 0 | return; |
57 | 0 | } |
58 | 546M | va_start(args, msg); |
59 | 546M | const std::string s = GooString::formatv(msg, args); |
60 | 546M | va_end(args); |
61 | | |
62 | 546M | GooString sanitized; |
63 | 19.2G | for (const char c : s) { |
64 | 19.2G | if (c < (char)0x20 || c >= (char)0x7f) { |
65 | 25.7M | sanitized.appendf("<{0:02x}>", c & 0xff); |
66 | 19.1G | } else { |
67 | 19.1G | sanitized.append(c); |
68 | 19.1G | } |
69 | 19.2G | } |
70 | | |
71 | 546M | if (errorCbk) { |
72 | 546M | (*errorCbk)(category, pos, sanitized.c_str()); |
73 | 546M | } else { |
74 | 0 | if (pos >= 0) { |
75 | 0 | fprintf(stderr, "%s (%lld): %s\n", errorCategoryNames[category], (long long)pos, sanitized.c_str()); |
76 | 0 | } else { |
77 | 0 | fprintf(stderr, "%s: %s\n", errorCategoryNames[category], sanitized.c_str()); |
78 | 0 | } |
79 | 0 | fflush(stderr); |
80 | 0 | } |
81 | 546M | } |