1 /*
2  * ProFTPD - FTP server daemon
3  * Copyright (c) 2008-2020 The ProFTPD Project team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program 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
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18  *
19  * As a special exemption, Public Flood Software/MacGyver aka Habeeb J. Dihu
20  * and other respective copyright holders give permission to link this program
21  * with OpenSSL, and distribute the resulting executable, without including
22  * the source code for OpenSSL in the source distribution.
23  */
24 
25 /* String manipulation functions */
26 
27 #ifndef PR_STR_H
28 #define PR_STR_H
29 
30 /* Default maximum number of replacements that will do in a given string. */
31 #define PR_STR_MAX_REPLACEMENTS                 8
32 
33 /* Per RFC959, directory responses for MKD and PWD should be "dir_name" (with
34  * quotes).  For directories that CONTAIN quotes, the additional quotes must
35  * be duplicated.
36  */
37 const char *quote_dir(pool *p, char *dir);
38 
39 char *sstrcat(char *, const char *, size_t);
40 const char *sreplace(pool *, const char *, ...);
41 
42 char *pdircat(pool *, ...)
43 #ifdef __GNUC__
44       __attribute__ ((sentinel));
45 #else
46       ;
47 #endif
48 char *pstrcat(pool *, ...)
49 #ifdef __GNUC__
50       __attribute__ ((sentinel));
51 #else
52       ;
53 #endif
54 char *pstrdup(pool *, const char *);
55 char *pstrndup(pool *, const char *, size_t);
56 
57 /* Returns TRUE if the string `s' ends with given `suffix', FALSE if the string
58  * does not end with the given suffix, and -1 if there was an error (errno
59  * will be set appropriately).
60  *
61  * The `flags' value consisted of OR'ing the following:
62  *
63  *  PR_STR_FL_IGNORE_CASE
64  *    Request a case-insensitive comparison
65  */
66 int pr_strnrstr(const char *s, size_t slen, const char *suffix,
67   size_t suffixlen, int flags);
68 
69 /* Returns a quoted version of the given string. */
70 const char *pr_str_quote(pool *p, const char *str);
71 
72 /* Newer version of sreplace(), with more control and better error reporting. */
73 const char *pr_str_replace(pool *, unsigned int, const char *, ...);
74 const char *pr_str_strip(pool *, const char *);
75 char *pr_str_strip_end(char *, const char *);
76 int pr_str_get_nbytes(const char *, const char *, off_t *);
77 char *pr_str_get_word(char **, int);
78 
79 /* Parses a "time string" into its duration, in seconds.  Returns the number
80  * of seconds obtained via the `duration' pointer, or -1 (with errno) if
81  * there was a problem parsing the provided string.
82  *
83  * A "time string" is formatted as "hh:mm:ss".
84  */
85 int pr_str_get_duration(const char *str, int *duration);
86 
87 /* Encode the given buffer of binary data as a hex string.  The flags indicate
88  * whether to use uppercase or lowercase hex values; the default is to use
89  * lowercase values.
90  *
91  * Returns NULL on error, or the successfully encoded string, allocated out of
92  * the given pool, on success.
93  */
94 char *pr_str_bin2hex(pool *p, const unsigned char *buf, size_t len, int flags);
95 #define PR_STR_FL_HEX_USE_UC			0x0001
96 #define PR_STR_FL_HEX_USE_LC			0x0002
97 
98 /* Decodes the given buffer of hex-encoded data into binary data. */
99 unsigned char *pr_str_hex2bin(pool *p, const unsigned char *hex, size_t hex_len,
100   size_t *len);
101 
102 /* Obtain the Levenshtein distance between the two strings.  The various
103  * operations (swap, substitution, insertion, deletion) can be weighted.
104  */
105 int pr_str_levenshtein(pool *p, const char *a, const char *b,
106   int swap_cost, int subst_cost, int insert_cost, int del_cost, int flags);
107 
108 /* Given a string and a list of possibly similar candidates, return an
109  * array of the candidates, sorted in order of Levenshtein distance (ascending).
110  * A maximum edit distance can be used to return the most relevant subset of
111  * the candidates; if a max distance of zero is used, the default max distance
112  * value will be used.
113  */
114 array_header *pr_str_get_similars(pool *p, const char *s,
115   array_header *candidates, int max_distance, int flags);
116 #define PR_STR_DEFAULT_MAX_EDIT_DISTANCE		7
117 
118 /* Given a string delimited by a character (such as comma or pipe), return
119  * an array of each item.
120  */
121 array_header *pr_str_text_to_array(pool *p, const char *text, char delimiter);
122 
123 /* Converts a string to a uid_t/gid_t, respectively. */
124 int pr_str2uid(const char *, uid_t *);
125 int pr_str2gid(const char *, gid_t *);
126 
127 /* Converts a uid_t/gid_t to a string, respectively */
128 const char *pr_uid2str(pool *, uid_t);
129 const char *pr_gid2str(pool *, gid_t);
130 
131 #define PR_STR_FL_PRESERVE_COMMENTS		0x0001
132 #define PR_STR_FL_PRESERVE_WHITESPACE		0x0002
133 #define PR_STR_FL_IGNORE_CASE			0x0004
134 
135 char *pr_str_get_token(char **, char *);
136 char *pr_str_get_token2(char **, char *, size_t *);
137 
138 /* Returns TRUE if the given string is "on", "yes", "true", or "1"; returns
139  * FALSE if the string is "off", "false", "no", or "0".  Otherwise, -1
140  * is returned, with errno set to EINVAL.
141  */
142 int pr_str_is_boolean(const char *);
143 
144 int pr_str_is_fnmatch(const char *);
145 
146 #define CHOP(s)		pr_str_strip_end((s), "\r\n")
147 
148 #endif /* PR_STR_H */
149