blob: 7935a3a00368e52a6a45b89d3594b2fac852bd7c [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
19/*
20 * The following applies to stcasecmp and strncasecmp implementations:
21 *
22 * Copyright (c) 1987 Regents of the University of California.
23 * All rights reserved.
24 *
25 * Redistribution and use in source and binary forms are permitted
26 * provided that this notice is preserved and that due credit is given
27 * to the University of California at Berkeley. The name of the University
28 * may not be used to endorse or promote products derived from this
29 * software without specific written prior permission. This software
30 * is provided ``as is'' without express or implied warranty.
31 */
32
Adam Tkac3a549222008-10-08 13:40:28 +000033#ifdef HAVE_COMMON_CONFIG_H
Adam Tkacad1cbd92008-10-06 14:08:00 +000034#include <common-config.h>
35#endif
36
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000037#include <rfb/util.h>
Adam Tkac97abe8a2009-09-04 10:16:58 +000038#include <errno.h>
39#include <stdio.h>
40#include <stdlib.h>
41
42char* safe_strdup(const char* s) {
43 char *tmp;
44
45 tmp = strdup(s);
46 if (tmp == NULL) {
47 perror("safe_strdup failed");
48 exit(1);
49 }
50};
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000051
52// Provide strcasecmp() and/or strncasecmp() if absent on this system.
53
54#ifndef WIN32
55#if !defined(HAVE_STRCASECMP) || !defined(HAVE_STRNCASECMP)
56
57extern "C" {
58
59/*
60 * This array is designed for mapping upper and lower case letter
61 * together for a case independent comparison. The mappings are
62 * based upon ascii character sequences.
63 */
64static unsigned char s_charmap[] = {
65 '\000', '\001', '\002', '\003', '\004', '\005', '\006', '\007',
66 '\010', '\011', '\012', '\013', '\014', '\015', '\016', '\017',
67 '\020', '\021', '\022', '\023', '\024', '\025', '\026', '\027',
68 '\030', '\031', '\032', '\033', '\034', '\035', '\036', '\037',
69 '\040', '\041', '\042', '\043', '\044', '\045', '\046', '\047',
70 '\050', '\051', '\052', '\053', '\054', '\055', '\056', '\057',
71 '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
72 '\070', '\071', '\072', '\073', '\074', '\075', '\076', '\077',
73 '\100', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
74 '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
75 '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
76 '\170', '\171', '\172', '\133', '\134', '\135', '\136', '\137',
77 '\140', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
78 '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
79 '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
80 '\170', '\171', '\172', '\173', '\174', '\175', '\176', '\177',
81 '\200', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
82 '\210', '\211', '\212', '\213', '\214', '\215', '\216', '\217',
83 '\220', '\221', '\222', '\223', '\224', '\225', '\226', '\227',
84 '\230', '\231', '\232', '\233', '\234', '\235', '\236', '\237',
85 '\240', '\241', '\242', '\243', '\244', '\245', '\246', '\247',
86 '\250', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
87 '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
88 '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
89 '\300', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
90 '\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
91 '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
92 '\370', '\371', '\372', '\333', '\334', '\335', '\336', '\337',
93 '\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
94 '\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
95 '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
96 '\370', '\371', '\372', '\373', '\374', '\375', '\376', '\377',
97};
98
99#ifndef HAVE_STRCASECMP
100int
101strcasecmp(const char *s1, const char *s2)
102{
103 unsigned char u1, u2;
104
105 for (;;) {
106 u1 = (unsigned char) *s1++;
107 u2 = (unsigned char) *s2++;
108 if (s_charmap[u1] != s_charmap[u2]) {
109 return s_charmap[u1] - s_charmap[u2];
110 }
111 if (u1 == '\0') {
112 return 0;
113 }
114 }
115}
116#endif // !defined(HAVE_STRCASECMP)
117
118#ifndef HAVE_STRNCASECMP
119int
120strncasecmp(const char *s1, const char *s2, size_t n)
121{
122 unsigned char u1, u2;
123
124 for (; n != 0; --n) {
125 u1 = (unsigned char) *s1++;
126 u2 = (unsigned char) *s2++;
127 if (s_charmap[u1] != s_charmap[u2]) {
128 return s_charmap[u1] - s_charmap[u2];
129 }
130 if (u1 == '\0') {
131 return 0;
132 }
133 }
134 return 0;
135}
136#endif // !defined(HAVE_STRNCASECMP)
137
138} // extern "C"
139
140#endif // !defined(HAVE_STRCASECMP) || !defined(HAVE_STRNCASECMP)
141#endif // defined(WIN32)
142
143namespace rfb {
144
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000145 bool strSplit(const char* src, const char limiter, char** out1, char** out2, bool fromEnd) {
146 CharArray out1old, out2old;
147 if (out1) out1old.buf = *out1;
148 if (out2) out2old.buf = *out2;
149 int len = strlen(src);
150 int i=0, increment=1, limit=len;
151 if (fromEnd) {
152 i=len-1; increment = -1; limit = -1;
153 }
154 while (i!=limit) {
155 if (src[i] == limiter) {
156 if (out1) {
157 *out1 = new char[i+1];
158 if (i) memcpy(*out1, src, i);
159 (*out1)[i] = 0;
160 }
161 if (out2) {
162 *out2 = new char[len-i];
163 if (len-i-1) memcpy(*out2, &src[i+1], len-i-1);
164 (*out2)[len-i-1] = 0;
165 }
166 return true;
167 }
168 i+=increment;
169 }
Adam Tkac97abe8a2009-09-04 10:16:58 +0000170 if (out1) *out1 = safe_strdup(src);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000171 if (out2) *out2 = 0;
172 return false;
173 }
174
175 bool strContains(const char* src, char c) {
176 int l=strlen(src);
177 for (int i=0; i<l; i++)
178 if (src[i] == c) return true;
179 return false;
180 }
181
182 void strCopy(char* dest, const char* src, int destlen) {
183 if (src)
184 strncpy(dest, src, destlen-1);
185 dest[src ? destlen-1 : 0] = 0;
186 }
187
188};