1 /*
2 * Private string definitions for CUPS.
3 *
4 * Copyright © 2020-2024 by OpenPrinting.
5 * Copyright © 2007-2018 by Apple Inc.
6 * Copyright © 1997-2006 by Easy Software Products.
7 *
8 * Licensed under Apache License v2.0. See the file "LICENSE" for more
9 * information.
10 */
11
12 #ifndef _CUPS_STRING_PRIVATE_H_
13 # define _CUPS_STRING_PRIVATE_H_
14
15 /*
16 * Include necessary headers...
17 */
18
19 # include "config.h"
20 # include <stdio.h>
21 # include <stdlib.h>
22 # include <stdarg.h>
23 # include <string.h>
24 # include <ctype.h>
25 # include <errno.h>
26 # include <locale.h>
27 # include <time.h>
28
29 # include <cups/versioning.h>
30
31
32 # if defined(_WIN32) && !defined(__CUPS_SSIZE_T_DEFINED)
33 # define __CUPS_SSIZE_T_DEFINED
34 # include <stddef.h>
35 /* Windows does not support the ssize_t type, so map it to __int64... */
36 typedef __int64 ssize_t; /* @private@ */
37 # endif /* _WIN32 && !__CUPS_SSIZE_T_DEFINED */
38
39
40 /*
41 * C++ magic...
42 */
43
44 # ifdef __cplusplus
45 extern "C" {
46 # endif /* __cplusplus */
47
48
49 /*
50 * String pool structures...
51 */
52
53 # define _CUPS_STR_GUARD 0x12344321
54
55 typedef struct _cups_sp_item_s /**** String Pool Item ****/
56 {
57 # ifdef DEBUG_GUARDS
58 unsigned int guard; /* Guard word */
59 # endif /* DEBUG_GUARDS */
60 unsigned int ref_count; /* Reference count */
61 char str[1]; /* String */
62 } _cups_sp_item_t;
63
64
65 /*
66 * Replacements for the ctype macros that are not affected by locale, since we
67 * really only care about testing for ASCII characters when parsing files, etc.
68 *
69 * The _CUPS_INLINE definition controls whether we get an inline function body,
70 * and external function body, or an external definition.
71 */
72
73 # if defined(__GNUC__) || __STDC_VERSION__ >= 199901L
74 # define _CUPS_INLINE static inline
75 # elif defined(_MSC_VER)
76 # define _CUPS_INLINE static __inline
77 # elif defined(_CUPS_STRING_C_)
78 # define _CUPS_INLINE
79 # endif /* __GNUC__ || __STDC_VERSION__ */
80
81 # ifdef _CUPS_INLINE
82 _CUPS_INLINE int /* O - 1 on match, 0 otherwise */
_cups_isalnum(int ch)83 _cups_isalnum(int ch) /* I - Character to test */
84 {
85 return ((ch >= '0' && ch <= '9') ||
86 (ch >= 'A' && ch <= 'Z') ||
87 (ch >= 'a' && ch <= 'z'));
88 }
89
90 _CUPS_INLINE int /* O - 1 on match, 0 otherwise */
_cups_isalpha(int ch)91 _cups_isalpha(int ch) /* I - Character to test */
92 {
93 return ((ch >= 'A' && ch <= 'Z') ||
94 (ch >= 'a' && ch <= 'z'));
95 }
96
97 _CUPS_INLINE int /* O - 1 on match, 0 otherwise */
_cups_islower(int ch)98 _cups_islower(int ch) /* I - Character to test */
99 {
100 return (ch >= 'a' && ch <= 'z');
101 }
102
103 _CUPS_INLINE int /* O - 1 on match, 0 otherwise */
_cups_isspace(int ch)104 _cups_isspace(int ch) /* I - Character to test */
105 {
106 return (ch == ' ' || ch == '\f' || ch == '\n' || ch == '\r' || ch == '\t' ||
107 ch == '\v');
108 }
109
110 _CUPS_INLINE int /* O - 1 on match, 0 otherwise */
_cups_isupper(int ch)111 _cups_isupper(int ch) /* I - Character to test */
112 {
113 return (ch >= 'A' && ch <= 'Z');
114 }
115
116 _CUPS_INLINE int /* O - Converted character */
_cups_tolower(int ch)117 _cups_tolower(int ch) /* I - Character to convert */
118 {
119 return (_cups_isupper(ch) ? ch - 'A' + 'a' : ch);
120 }
121
122 _CUPS_INLINE int /* O - Converted character */
_cups_toupper(int ch)123 _cups_toupper(int ch) /* I - Character to convert */
124 {
125 return (_cups_islower(ch) ? ch - 'a' + 'A' : ch);
126 }
127 # else
128 extern int _cups_isalnum(int ch);
129 extern int _cups_isalpha(int ch);
130 extern int _cups_islower(int ch);
131 extern int _cups_isspace(int ch);
132 extern int _cups_isupper(int ch);
133 extern int _cups_tolower(int ch);
134 extern int _cups_toupper(int ch);
135 # endif /* _CUPS_INLINE */
136
137
138 /*
139 * Prototypes...
140 */
141
142 extern ssize_t _cups_safe_vsnprintf(char *buffer, size_t bufsize, const char *format, va_list args) _CUPS_PRIVATE;
143 extern void _cups_strcpy(char *dst, const char *src) _CUPS_PRIVATE;
144
145 # ifndef HAVE_STRDUP
146 extern char *_cups_strdup(const char *) _CUPS_PRIVATE;
147 # define strdup _cups_strdup
148 # endif /* !HAVE_STRDUP */
149
150 extern int _cups_strcasecmp(const char *, const char *) _CUPS_PRIVATE;
151
152 extern int _cups_strncasecmp(const char *, const char *, size_t n) _CUPS_PRIVATE;
153
154 # ifndef HAVE_STRLCAT
155 extern size_t _cups_strlcat(char *, const char *, size_t) _CUPS_PRIVATE;
156 # define strlcat _cups_strlcat
157 # endif /* !HAVE_STRLCAT */
158
159 # ifndef HAVE_STRLCPY
160 extern size_t _cups_strlcpy(char *, const char *, size_t) _CUPS_PRIVATE;
161 # define strlcpy _cups_strlcpy
162 # endif /* !HAVE_STRLCPY */
163
164 # ifndef HAVE_SNPRINTF
165 extern int _cups_snprintf(char *, size_t, const char *, ...) _CUPS_FORMAT(3, 4) _CUPS_PRIVATE;
166 # define snprintf _cups_snprintf
167 # endif /* !HAVE_SNPRINTF */
168
169 # ifndef HAVE_VSNPRINTF
170 extern int _cups_vsnprintf(char *, size_t, const char *, va_list) _CUPS_PRIVATE;
171 # define vsnprintf _cups_vsnprintf
172 # endif /* !HAVE_VSNPRINTF */
173
174 /*
175 * String pool functions...
176 */
177
178 extern char *_cupsStrAlloc(const char *s) _CUPS_PRIVATE;
179 extern void _cupsStrFlush(void) _CUPS_PRIVATE;
180 extern void _cupsStrFree(const char *s) _CUPS_PRIVATE;
181 extern char *_cupsStrRetain(const char *s) _CUPS_PRIVATE;
182 extern size_t _cupsStrStatistics(size_t *alloc_bytes, size_t *total_bytes) _CUPS_PRIVATE;
183
184
185 /*
186 * Floating point number functions...
187 */
188
189 extern char *_cupsStrFormatd(char *buf, char *bufend, double number,
190 struct lconv *loc) _CUPS_PRIVATE;
191 extern double _cupsStrScand(const char *buf, char **bufptr,
192 struct lconv *loc) _CUPS_PRIVATE;
193
194
195 /*
196 * Date function...
197 */
198
199 extern char *_cupsStrDate(char *buf, size_t bufsize, time_t timeval) _CUPS_PRIVATE;
200
201
202 /*
203 * C++ magic...
204 */
205
206 # ifdef __cplusplus
207 }
208 # endif /* __cplusplus */
209
210 #endif /* !_CUPS_STRING_H_ */
211