xref: /freebsd/crypto/krb5/src/include/k5-regex.h (revision f1c4c3daccbaf3820f0e2224de53df12fc952fcc)
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* include/k5-regex.h - Compatibility glue for std::regex on Windows */
3 
4 /*
5  * Copyright (C) 2024 United States Government as represented by the
6  * Secretary of the Navy.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  *   notice, this list of conditions and the following disclaimer.
15  *
16  * * Redistributions in binary form must reproduce the above copyright
17  *   notice, this list of conditions and the following disclaimer in
18  *   the documentation and/or other materials provided with the
19  *   distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
26  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
32  * OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /*
36  * On POSIX platforms we can use the standardized regcomp()/regexec() function
37  * calls.  Windows does not provide POSIX regex functions, but does provide a
38  * C++ interface (std::regex) that has the same functionality.
39  */
40 
41 #ifndef _K5_REGEX_H_
42 #define _K5_REGEX_H_
43 
44 #ifndef _WIN32
45 
46 /* On POSIX platforms, just include regex.h. */
47 #include <regex.h>
48 
49 #else /* _WIN32 */
50 
51 #ifdef __cplusplus
52 extern "C" {
53 #endif /* __cplusplus */
54 
55 /* On Windows, emulate the POSIX interface using functions from
56  * libkrb5support. */
57 
58 typedef struct {
59     size_t re_nsub;             /* Number of subexpressions */
60     void *regex;                /* Pointer to std::basic_regex */
61     char errmsg[128];           /* Regular expression error message */
62 } regex_t;
63 
64 typedef ssize_t regoff_t;
65 
66 typedef struct {
67     regoff_t rm_so;
68     regoff_t rm_eo;
69 } regmatch_t;
70 
71 /*
72  * Flags to k5_regcomp()
73  */
74 
75 #define REG_BASIC       0x00    /* Basic regular expressions */
76 #define REG_EXTENDED    0x01    /* Extended regular expressions */
77 #define REG_ICASE       0x02    /* Case-insensitive match */
78 #define REG_NOSUB       0x04    /* Do not do submatching */
79 
80 /*
81  * Flags to k5_regexec()
82  */
83 
84 #define REG_NOTBOL      0x01    /* First character not at beginning of line */
85 #define REG_NOTEOL      0x02    /* Last character not at end of line */
86 
87 /*
88  * Error return codes for k5_regcomp()/k5_regexec()
89  *
90  * We only define REG_NOMATCH and REG_BADPAT, since no Kerberos code looks
91  * for anything other than success and REG_NOMATCH.
92  */
93 
94 #define REG_NOMATCH     1
95 #define REG_BADPAT      2
96 
97 /*
98  * Note that we don't follow the POSIX API exactly because k5_regexec()
99  * doesn't declare regex_t as const; that's so we can store an error
100  * string.
101  */
102 int k5_regcomp(regex_t *preg, const char *pattern, int flags);
103 int k5_regexec(regex_t *preg, const char *string, size_t,
104                regmatch_t pmatch[], int flags);
105 size_t k5_regerror(int code, const regex_t *preg, char *errmsg,
106                    size_t errmsg_size);
107 void k5_regfree(regex_t *preg);
108 
109 #define regcomp k5_regcomp
110 #define regexec k5_regexec
111 #define regerror k5_regerror
112 #define regfree k5_regfree
113 
114 #ifdef __cplusplus
115 }
116 #endif /* __cplusplus */
117 #endif /* _WIN32 */
118 #endif /* _K5_REGEX_H_ */
119