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