| 1 | /* |
| 2 | * Copyright 2023 Red Hat Inc., Durham, North Carolina. |
| 3 | * All Rights Reserved. |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2.1 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | * |
| 19 | * Authors: |
| 20 | * Evgenii Kolesnikov <[email protected]> |
| 21 | */ |
| 22 | |
| 23 | #ifndef OSCAP_PCRE_ |
| 24 | #define OSCAP_PCRE_ |
| 25 | |
| 26 | typedef struct oscap_pcre oscap_pcre_t; |
| 27 | |
| 28 | typedef enum { |
| 29 | OSCAP_PCRE_OPTS_NONE = 0x0001, |
| 30 | OSCAP_PCRE_OPTS_UTF8 = 0x0002, |
| 31 | OSCAP_PCRE_OPTS_MULTILINE = 0x0004, |
| 32 | OSCAP_PCRE_OPTS_DOTALL = 0x0008, |
| 33 | OSCAP_PCRE_OPTS_CASELESS = 0x0010, |
| 34 | OSCAP_PCRE_OPTS_NO_UTF8_CHECK = 0x0020, |
| 35 | OSCAP_PCRE_OPTS_PARTIAL = 0x0040, |
| 36 | } oscap_pcre_options_t; |
| 37 | |
| 38 | typedef enum { |
| 39 | OSCAP_PCRE_ERR_NOMATCH = -1, |
| 40 | OSCAP_PCRE_ERR_PARTIAL = -2, |
| 41 | OSCAP_PCRE_ERR_BADPARTIAL = -3, |
| 42 | OSCAP_PCRE_ERR_BADUTF8 = -10, |
| 43 | OSCAP_PCRE_ERR_RECURSIONLIMIT = -21, |
| 44 | OSCAP_PCRE_ERR_UNKNOWN = -100, |
| 45 | } oscap_pcre_error_t; |
| 46 | |
| 47 | |
| 48 | /** |
| 49 | * Compile a regular expression string into PCRE object and returns it. |
| 50 | * Caller is responsible for freeing the returned object or the error message |
| 51 | * if the result is NULL (USE oscap_pcre_err_free()!). |
| 52 | * @param pattern expresstion string |
| 53 | * @param options compile options |
| 54 | * @param errptr a return value for a string representation of error |
| 55 | * @param erroffset the offset in the expression where the problem was detected |
| 56 | * @return a PCRE object |
| 57 | * NULL on failure |
| 58 | */ |
| 59 | oscap_pcre_t* oscap_pcre_compile(const char *pattern, oscap_pcre_options_t options, |
| 60 | char **errptr, int *erroffset); |
| 61 | |
| 62 | /** |
| 63 | * Execute the compiled regular expression against a string subject and returns |
| 64 | * matches count (or a negative error code). |
| 65 | * @param opcre the oscap_pcre_t object |
| 66 | * @param subject target string |
| 67 | * @param length target string length |
| 68 | * @param startoffset the offset for the target string |
| 69 | * @param options match options |
| 70 | * @param ovector the output vector for match offset pairs, see pcre_exec for details |
| 71 | * @param ovecsize the size of ovector, see pcre_exec for details |
| 72 | * @return matches count |
| 73 | * negative error code on failure |
| 74 | */ |
| 75 | int oscap_pcre_exec(const oscap_pcre_t *opcre, const char *subject, |
| 76 | int length, int startoffset, oscap_pcre_options_t options, |
| 77 | int *ovector, int ovecsize); |
| 78 | |
| 79 | /** |
| 80 | * Free the compiled regular expression object. |
| 81 | * @param opcre the oscap_pcre_t object |
| 82 | */ |
| 83 | void oscap_pcre_free(oscap_pcre_t *opcre); |
| 84 | |
| 85 | /** |
| 86 | * Limit the compiled regular expression object's recursion depth for future |
| 87 | * matches. |
| 88 | * @param opcre the oscap_pcre_t object |
| 89 | * @param limit maximum depth |
| 90 | */ |
| 91 | void oscap_pcre_set_match_limit_recursion(oscap_pcre_t *opcre, unsigned long limit); |
| 92 | |
| 93 | /** |
| 94 | * Match a regular expression and return substrings. |
| 95 | * Caller is responsible for freeing the returned array. |
| 96 | * @param str subject string |
| 97 | * @param ofs starting offset in str |
| 98 | * @param re compiled regular expression |
| 99 | * @param want_substrs if non-zero, substrings will be returned |
| 100 | * @param substrings contains returned substrings |
| 101 | * @return count of matched substrings, 0 if no match |
| 102 | * negative value on failure |
| 103 | */ |
| 104 | int oscap_pcre_get_substrings(char *str, int *ofs, oscap_pcre_t *re, int want_substrs, char ***substrings); |
| 105 | |
| 106 | /** |
| 107 | * Free the error message returned by oscap_pcre_compile. DON'T USE REGULAR free()! |
| 108 | * @param err the message |
| 109 | */ |
| 110 | void oscap_pcre_err_free(char *err); |
| 111 | |
| 112 | #endif |