xref: /aosp_15_r20/external/libxkbcommon/src/utils.c (revision 2b949d0487e80d67f1fda82db69e101e761f8064)
1*2b949d04SAndroid Build Coastguard Worker /*
2*2b949d04SAndroid Build Coastguard Worker  * Copyright © 2013 Ran Benita <[email protected]>
3*2b949d04SAndroid Build Coastguard Worker  *
4*2b949d04SAndroid Build Coastguard Worker  * Permission is hereby granted, free of charge, to any person obtaining a
5*2b949d04SAndroid Build Coastguard Worker  * copy of this software and associated documentation files (the "Software"),
6*2b949d04SAndroid Build Coastguard Worker  * to deal in the Software without restriction, including without limitation
7*2b949d04SAndroid Build Coastguard Worker  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*2b949d04SAndroid Build Coastguard Worker  * and/or sell copies of the Software, and to permit persons to whom the
9*2b949d04SAndroid Build Coastguard Worker  * Software is furnished to do so, subject to the following conditions:
10*2b949d04SAndroid Build Coastguard Worker  *
11*2b949d04SAndroid Build Coastguard Worker  * The above copyright notice and this permission notice (including the next
12*2b949d04SAndroid Build Coastguard Worker  * paragraph) shall be included in all copies or substantial portions of the
13*2b949d04SAndroid Build Coastguard Worker  * Software.
14*2b949d04SAndroid Build Coastguard Worker  *
15*2b949d04SAndroid Build Coastguard Worker  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16*2b949d04SAndroid Build Coastguard Worker  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17*2b949d04SAndroid Build Coastguard Worker  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18*2b949d04SAndroid Build Coastguard Worker  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19*2b949d04SAndroid Build Coastguard Worker  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20*2b949d04SAndroid Build Coastguard Worker  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21*2b949d04SAndroid Build Coastguard Worker  * DEALINGS IN THE SOFTWARE.
22*2b949d04SAndroid Build Coastguard Worker  */
23*2b949d04SAndroid Build Coastguard Worker 
24*2b949d04SAndroid Build Coastguard Worker #include "config.h"
25*2b949d04SAndroid Build Coastguard Worker 
26*2b949d04SAndroid Build Coastguard Worker #include "utils.h"
27*2b949d04SAndroid Build Coastguard Worker 
28*2b949d04SAndroid Build Coastguard Worker #ifdef HAVE_MMAP
29*2b949d04SAndroid Build Coastguard Worker 
30*2b949d04SAndroid Build Coastguard Worker #include <fcntl.h>
31*2b949d04SAndroid Build Coastguard Worker #include <unistd.h>
32*2b949d04SAndroid Build Coastguard Worker #include <sys/mman.h>
33*2b949d04SAndroid Build Coastguard Worker #include <sys/stat.h>
34*2b949d04SAndroid Build Coastguard Worker #include <sys/types.h>
35*2b949d04SAndroid Build Coastguard Worker 
36*2b949d04SAndroid Build Coastguard Worker bool
map_file(FILE * file,char ** string_out,size_t * size_out)37*2b949d04SAndroid Build Coastguard Worker map_file(FILE *file, char **string_out, size_t *size_out)
38*2b949d04SAndroid Build Coastguard Worker {
39*2b949d04SAndroid Build Coastguard Worker     struct stat stat_buf;
40*2b949d04SAndroid Build Coastguard Worker     int fd;
41*2b949d04SAndroid Build Coastguard Worker     char *string;
42*2b949d04SAndroid Build Coastguard Worker 
43*2b949d04SAndroid Build Coastguard Worker     /* Make sure to keep the errno on failure! */
44*2b949d04SAndroid Build Coastguard Worker     fd = fileno(file);
45*2b949d04SAndroid Build Coastguard Worker     if (fd < 0)
46*2b949d04SAndroid Build Coastguard Worker         return false;
47*2b949d04SAndroid Build Coastguard Worker 
48*2b949d04SAndroid Build Coastguard Worker     if (fstat(fd, &stat_buf) != 0)
49*2b949d04SAndroid Build Coastguard Worker         return false;
50*2b949d04SAndroid Build Coastguard Worker 
51*2b949d04SAndroid Build Coastguard Worker     string = mmap(NULL, stat_buf.st_size, PROT_READ, MAP_SHARED, fd, 0);
52*2b949d04SAndroid Build Coastguard Worker     if (string == MAP_FAILED)
53*2b949d04SAndroid Build Coastguard Worker         return false;
54*2b949d04SAndroid Build Coastguard Worker 
55*2b949d04SAndroid Build Coastguard Worker     *string_out = string;
56*2b949d04SAndroid Build Coastguard Worker     *size_out = stat_buf.st_size;
57*2b949d04SAndroid Build Coastguard Worker     return true;
58*2b949d04SAndroid Build Coastguard Worker }
59*2b949d04SAndroid Build Coastguard Worker 
60*2b949d04SAndroid Build Coastguard Worker void
unmap_file(char * str,size_t size)61*2b949d04SAndroid Build Coastguard Worker unmap_file(char *str, size_t size)
62*2b949d04SAndroid Build Coastguard Worker {
63*2b949d04SAndroid Build Coastguard Worker     munmap(str, size);
64*2b949d04SAndroid Build Coastguard Worker }
65*2b949d04SAndroid Build Coastguard Worker 
66*2b949d04SAndroid Build Coastguard Worker #else
67*2b949d04SAndroid Build Coastguard Worker 
68*2b949d04SAndroid Build Coastguard Worker bool
map_file(FILE * file,char ** string_out,size_t * size_out)69*2b949d04SAndroid Build Coastguard Worker map_file(FILE *file, char **string_out, size_t *size_out)
70*2b949d04SAndroid Build Coastguard Worker {
71*2b949d04SAndroid Build Coastguard Worker     long ret;
72*2b949d04SAndroid Build Coastguard Worker     size_t ret_s;
73*2b949d04SAndroid Build Coastguard Worker     char *string;
74*2b949d04SAndroid Build Coastguard Worker     size_t size;
75*2b949d04SAndroid Build Coastguard Worker 
76*2b949d04SAndroid Build Coastguard Worker     /* Make sure to keep the errno on failure! */
77*2b949d04SAndroid Build Coastguard Worker 
78*2b949d04SAndroid Build Coastguard Worker     ret = fseek(file, 0, SEEK_END);
79*2b949d04SAndroid Build Coastguard Worker     if (ret != 0)
80*2b949d04SAndroid Build Coastguard Worker         return false;
81*2b949d04SAndroid Build Coastguard Worker 
82*2b949d04SAndroid Build Coastguard Worker     ret = ftell(file);
83*2b949d04SAndroid Build Coastguard Worker     if (ret < 0)
84*2b949d04SAndroid Build Coastguard Worker         return false;
85*2b949d04SAndroid Build Coastguard Worker     size = (size_t) ret;
86*2b949d04SAndroid Build Coastguard Worker 
87*2b949d04SAndroid Build Coastguard Worker     ret = fseek(file, 0, SEEK_SET);
88*2b949d04SAndroid Build Coastguard Worker     if (ret < 0)
89*2b949d04SAndroid Build Coastguard Worker         return false;
90*2b949d04SAndroid Build Coastguard Worker 
91*2b949d04SAndroid Build Coastguard Worker     string = malloc(size);
92*2b949d04SAndroid Build Coastguard Worker     if (!string)
93*2b949d04SAndroid Build Coastguard Worker         return false;
94*2b949d04SAndroid Build Coastguard Worker 
95*2b949d04SAndroid Build Coastguard Worker     ret_s = fread(string, 1, size, file);
96*2b949d04SAndroid Build Coastguard Worker     if (ret_s < size) {
97*2b949d04SAndroid Build Coastguard Worker         free(string);
98*2b949d04SAndroid Build Coastguard Worker         return false;
99*2b949d04SAndroid Build Coastguard Worker     }
100*2b949d04SAndroid Build Coastguard Worker 
101*2b949d04SAndroid Build Coastguard Worker     *string_out = string;
102*2b949d04SAndroid Build Coastguard Worker     *size_out = size;
103*2b949d04SAndroid Build Coastguard Worker     return true;
104*2b949d04SAndroid Build Coastguard Worker }
105*2b949d04SAndroid Build Coastguard Worker 
106*2b949d04SAndroid Build Coastguard Worker void
unmap_file(char * str,size_t size)107*2b949d04SAndroid Build Coastguard Worker unmap_file(char *str, size_t size)
108*2b949d04SAndroid Build Coastguard Worker {
109*2b949d04SAndroid Build Coastguard Worker     free(str);
110*2b949d04SAndroid Build Coastguard Worker }
111*2b949d04SAndroid Build Coastguard Worker 
112*2b949d04SAndroid Build Coastguard Worker #endif
113*2b949d04SAndroid Build Coastguard Worker 
114*2b949d04SAndroid Build Coastguard Worker // ASCII lower-case map.
115*2b949d04SAndroid Build Coastguard Worker static const unsigned char lower_map[] = {
116*2b949d04SAndroid Build Coastguard Worker     0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
117*2b949d04SAndroid Build Coastguard Worker     21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
118*2b949d04SAndroid Build Coastguard Worker     40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58,
119*2b949d04SAndroid Build Coastguard Worker     59, 60, 61, 62, 63, 64, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
120*2b949d04SAndroid Build Coastguard Worker     108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122,
121*2b949d04SAndroid Build Coastguard Worker     91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
122*2b949d04SAndroid Build Coastguard Worker     108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122,
123*2b949d04SAndroid Build Coastguard Worker     123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137,
124*2b949d04SAndroid Build Coastguard Worker     138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152,
125*2b949d04SAndroid Build Coastguard Worker     153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167,
126*2b949d04SAndroid Build Coastguard Worker     168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182,
127*2b949d04SAndroid Build Coastguard Worker     183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197,
128*2b949d04SAndroid Build Coastguard Worker     198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212,
129*2b949d04SAndroid Build Coastguard Worker     213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227,
130*2b949d04SAndroid Build Coastguard Worker     228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242,
131*2b949d04SAndroid Build Coastguard Worker     243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255
132*2b949d04SAndroid Build Coastguard Worker };
133*2b949d04SAndroid Build Coastguard Worker 
134*2b949d04SAndroid Build Coastguard Worker // ASCII tolower (to avoid locale issues).
135*2b949d04SAndroid Build Coastguard Worker char
to_lower(char c)136*2b949d04SAndroid Build Coastguard Worker to_lower(char c)
137*2b949d04SAndroid Build Coastguard Worker {
138*2b949d04SAndroid Build Coastguard Worker     return (char) lower_map[(unsigned char) c];
139*2b949d04SAndroid Build Coastguard Worker }
140*2b949d04SAndroid Build Coastguard Worker 
141*2b949d04SAndroid Build Coastguard Worker // ASCII strcasecmp (to avoid locale issues).
142*2b949d04SAndroid Build Coastguard Worker int
istrcmp(const char * a,const char * b)143*2b949d04SAndroid Build Coastguard Worker istrcmp(const char *a, const char *b)
144*2b949d04SAndroid Build Coastguard Worker {
145*2b949d04SAndroid Build Coastguard Worker     for (size_t i = 0; ; i++) {
146*2b949d04SAndroid Build Coastguard Worker         if (to_lower(a[i]) != to_lower(b[i]))
147*2b949d04SAndroid Build Coastguard Worker             return (int) to_lower(a[i]) - (int) to_lower(b[i]);
148*2b949d04SAndroid Build Coastguard Worker         if (!a[i])
149*2b949d04SAndroid Build Coastguard Worker             break;
150*2b949d04SAndroid Build Coastguard Worker     }
151*2b949d04SAndroid Build Coastguard Worker     return 0;
152*2b949d04SAndroid Build Coastguard Worker }
153*2b949d04SAndroid Build Coastguard Worker 
154*2b949d04SAndroid Build Coastguard Worker // ASCII strncasecmp (to avoid locale issues).
155*2b949d04SAndroid Build Coastguard Worker int
istrncmp(const char * a,const char * b,size_t n)156*2b949d04SAndroid Build Coastguard Worker istrncmp(const char *a, const char *b, size_t n)
157*2b949d04SAndroid Build Coastguard Worker {
158*2b949d04SAndroid Build Coastguard Worker     for (size_t i = 0; i < n; i++) {
159*2b949d04SAndroid Build Coastguard Worker         if (to_lower(a[i]) != to_lower(b[i]))
160*2b949d04SAndroid Build Coastguard Worker             return (int) to_lower(a[i]) - (int) to_lower(b[i]);
161*2b949d04SAndroid Build Coastguard Worker         if (!a[i])
162*2b949d04SAndroid Build Coastguard Worker             break;
163*2b949d04SAndroid Build Coastguard Worker     }
164*2b949d04SAndroid Build Coastguard Worker     return 0;
165*2b949d04SAndroid Build Coastguard Worker }
166*2b949d04SAndroid Build Coastguard Worker 
167*2b949d04SAndroid Build Coastguard Worker #if !(defined(HAVE_ASPRINTF) && HAVE_ASPRINTF)
168*2b949d04SAndroid Build Coastguard Worker int
asprintf(char ** strp,const char * fmt,...)169*2b949d04SAndroid Build Coastguard Worker asprintf(char **strp, const char *fmt, ...)
170*2b949d04SAndroid Build Coastguard Worker {
171*2b949d04SAndroid Build Coastguard Worker     int ret;
172*2b949d04SAndroid Build Coastguard Worker     va_list ap;
173*2b949d04SAndroid Build Coastguard Worker     va_start(ap, fmt);
174*2b949d04SAndroid Build Coastguard Worker     ret = vasprintf(strp, fmt, ap);
175*2b949d04SAndroid Build Coastguard Worker     va_end(ap);
176*2b949d04SAndroid Build Coastguard Worker     return ret;
177*2b949d04SAndroid Build Coastguard Worker }
178*2b949d04SAndroid Build Coastguard Worker 
179*2b949d04SAndroid Build Coastguard Worker # if !(defined(HAVE_VASPRINTF) && HAVE_VASPRINTF)
180*2b949d04SAndroid Build Coastguard Worker int
vasprintf(char ** strp,const char * fmt,va_list ap)181*2b949d04SAndroid Build Coastguard Worker vasprintf(char **strp, const char *fmt, va_list ap)
182*2b949d04SAndroid Build Coastguard Worker {
183*2b949d04SAndroid Build Coastguard Worker     int ret;
184*2b949d04SAndroid Build Coastguard Worker     char *buf;
185*2b949d04SAndroid Build Coastguard Worker     va_list ap_copy;
186*2b949d04SAndroid Build Coastguard Worker 
187*2b949d04SAndroid Build Coastguard Worker     /*
188*2b949d04SAndroid Build Coastguard Worker      * The value of the va_list parameter is undefined after the call to
189*2b949d04SAndroid Build Coastguard Worker      * vsnprintf() returns: pass a copy to make sure "ap" remains valid.
190*2b949d04SAndroid Build Coastguard Worker      */
191*2b949d04SAndroid Build Coastguard Worker     va_copy(ap_copy, ap);
192*2b949d04SAndroid Build Coastguard Worker     ret = vsnprintf(NULL, 0, fmt, ap_copy);
193*2b949d04SAndroid Build Coastguard Worker     va_end(ap_copy);
194*2b949d04SAndroid Build Coastguard Worker 
195*2b949d04SAndroid Build Coastguard Worker     if (ret < 0)
196*2b949d04SAndroid Build Coastguard Worker         return ret;
197*2b949d04SAndroid Build Coastguard Worker 
198*2b949d04SAndroid Build Coastguard Worker     if (!(buf = malloc(ret + 1)))
199*2b949d04SAndroid Build Coastguard Worker         return -1;
200*2b949d04SAndroid Build Coastguard Worker 
201*2b949d04SAndroid Build Coastguard Worker     if ((ret = vsnprintf(buf, ret + 1, fmt, ap)) < 0) {
202*2b949d04SAndroid Build Coastguard Worker         free(buf);
203*2b949d04SAndroid Build Coastguard Worker         return ret;
204*2b949d04SAndroid Build Coastguard Worker     }
205*2b949d04SAndroid Build Coastguard Worker 
206*2b949d04SAndroid Build Coastguard Worker     *strp = buf;
207*2b949d04SAndroid Build Coastguard Worker     return ret;
208*2b949d04SAndroid Build Coastguard Worker }
209*2b949d04SAndroid Build Coastguard Worker # endif /* !HAVE_VASPRINTF */
210*2b949d04SAndroid Build Coastguard Worker #endif /* !HAVE_ASPRINTF */
211