|
| 1 | +/* Internal helpers related to entities |
| 2 | + __ __ _ |
| 3 | + ___\ \/ /_ __ __ _| |_ |
| 4 | + / _ \\ /| '_ \ / _` | __| |
| 5 | + | __// \| |_) | (_| | |_ |
| 6 | + \___/_/\_\ .__/ \__,_|\__| |
| 7 | + |_| XML parser |
| 8 | +
|
| 9 | + Copyright (c) 2026 Sebastian Pipping <sebastian@pipping.org> |
| 10 | + Licensed under the MIT license: |
| 11 | +
|
| 12 | + Permission is hereby granted, free of charge, to any person obtaining |
| 13 | + a copy of this software and associated documentation files (the |
| 14 | + "Software"), to deal in the Software without restriction, including |
| 15 | + without limitation the rights to use, copy, modify, merge, publish, |
| 16 | + distribute, sublicense, and/or sell copies of the Software, and to permit |
| 17 | + persons to whom the Software is furnished to do so, subject to the |
| 18 | + following conditions: |
| 19 | +
|
| 20 | + The above copyright notice and this permission notice shall be included |
| 21 | + in all copies or substantial portions of the Software. |
| 22 | +
|
| 23 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 24 | + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 25 | + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN |
| 26 | + NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 27 | + DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 28 | + OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 29 | + USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 30 | +
|
| 31 | + SPDX-License-Identifier: MIT |
| 32 | +*/ |
| 33 | + |
| 34 | +#if ! defined(EXPAT_ENTITIES_H) |
| 35 | +# define EXPAT_ENTITIES_H 1 |
| 36 | + |
| 37 | +# include "expat_config.h" // for XML_GE |
| 38 | +# include "expat_external.h" // for XML_Char |
| 39 | +# include "internal.h" // for XML_NONTESTING_STATIC |
| 40 | +# include <stdbool.h> // for size_t |
| 41 | +# include <stddef.h> // for size_t |
| 42 | + |
| 43 | +typedef struct { |
| 44 | + const XML_Char *name; |
| 45 | + const XML_Char *textPtr; |
| 46 | + size_t textLen; /* length in XML_Chars */ |
| 47 | + size_t processed; /* # of processed bytes - when suspended */ |
| 48 | + const XML_Char *systemId; |
| 49 | + const XML_Char *base; |
| 50 | + const XML_Char *publicId; |
| 51 | + const XML_Char *notation; |
| 52 | + bool open; |
| 53 | + bool hasMore; /* true if entity has not been completely processed */ |
| 54 | + /* An entity can be open while being already completely processed (!hasMore). |
| 55 | + The reason is the delayed closing of entities until their inner |
| 56 | + entities are processed and closed */ |
| 57 | + bool is_param; |
| 58 | + bool is_internal; /* true if declared in internal subset outside PE */ |
| 59 | +# if XML_GE == 1 |
| 60 | + /* Indirect accounting bytes charged when this entity is expanded: 1 per text |
| 61 | + character, plus each reference's "&name;"/"%name;" token length and its own |
| 62 | + expansion. Matches XML_ACCOUNT_ENTITY_EXPANSION, so it is not the |
| 63 | + replacement-text size. 0 means "not computed yet" (an empty entity is 0 |
| 64 | + too, told apart by textLen == 0). */ |
| 65 | + XmlBigCount expandedSize; |
| 66 | + bool beingAnalyzed; /* on the resolveEntityClosureExpandedSize depth-first |
| 67 | + search stack; used for cycle detection */ |
| 68 | + const XML_Char |
| 69 | + *scanPtr; /* cursor position into the entity's replacement text; NULL when |
| 70 | + `textPtr` is NULL, else a pointer into the entity value, |
| 71 | + ranging from `.textPtr` to `.textPtr + .textLen`. */ |
| 72 | +# endif /* XML_GE == 1 */ |
| 73 | +} ENTITY; |
| 74 | + |
| 75 | +# if XML_GE == 1 |
| 76 | +/* How a scanEntityText call ended. */ |
| 77 | +enum EntityScanResult { |
| 78 | + ENTITY_SCAN_COMPLETE, /* reached end of text; *expandedSize is final */ |
| 79 | + ENTITY_SCAN_BLOCKED, /* stopped at a defined entity whose expanded |
| 80 | + size has not been computed yet; *blockerOut |
| 81 | + names the entity whose size to compute |
| 82 | + first */ |
| 83 | + ENTITY_SCAN_OVERFLOW /* the expanded size would exceed XmlBigCount; |
| 84 | + callers must abort parsing */ |
| 85 | +}; |
| 86 | + |
| 87 | +XML_NONTESTING_STATIC int isCDataCommentOrPIStart(const XML_Char *pos, |
| 88 | + size_t maxLen, |
| 89 | + size_t *openTextLen); |
| 90 | + |
| 91 | +XML_NONTESTING_STATIC int isPredefinedEntityName(const XML_Char *name, |
| 92 | + size_t nameLen); |
| 93 | + |
| 94 | +XML_NONTESTING_STATIC enum EntityScanResult |
| 95 | +scanEntityText(XML_Parser parser, ENTITY *entity, const XML_Char **posPtr, |
| 96 | + XmlBigCount *expandedSize, ENTITY **blockerOut); |
| 97 | +# endif /* XML_GE == 1 */ |
| 98 | + |
| 99 | +#endif // ! defined(EXPAT_ENTITIES_H) |
0 commit comments