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