udev_utils_string.c revision 01618658fd82dbc5e6315b639f00e87c6fee3c54
/*
* Copyright (C) 2004-2005 Kay Sievers <kay.sievers@vrfy.org>
*
* under the terms of the GNU General Public License as published by the
* Free Software Foundation version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <ctype.h>
#include <dirent.h>
#include <syslog.h>
#include "udev.h"
int string_is_true(const char *str)
{
return 1;
return 1;
return 1;
return 0;
}
void remove_trailing_chars(char *path, char c)
{
}
{
size_t i, j;
t[0] = '\0';
for (i = 0, j = 0; s[i] != '\0'; i++) {
if (s[i] == '/') {
j += 4;
} else if (s[i] == '\\') {
j += 4;
} else {
t[j] = s[i];
j++;
}
}
t[j] = '\0';
return j;
}
size_t path_decode(char *s)
{
size_t i, j;
for (i = 0, j = 0; s[i] != '\0'; j++) {
s[j] = '/';
i += 4;
s[j] = '\\';
i += 4;
} else {
s[j] = s[i];
i++;
}
}
s[j] = '\0';
return j;
}
/* count of characters used to encode one unicode char */
static int utf8_encoded_expected_len(const char *str)
{
unsigned char c = (unsigned char)str[0];
if (c < 0x80)
return 1;
if ((c & 0xe0) == 0xc0)
return 2;
if ((c & 0xf0) == 0xe0)
return 3;
if ((c & 0xf8) == 0xf0)
return 4;
if ((c & 0xfc) == 0xf8)
return 5;
if ((c & 0xfe) == 0xfc)
return 6;
return 0;
}
/* decode one unicode char */
static int utf8_encoded_to_unichar(const char *str)
{
int unichar;
int len;
int i;
switch (len) {
case 1:
return (int)str[0];
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
default:
return -1;
}
for (i = 1; i < len; i++) {
return -1;
unichar <<= 6;
}
return unichar;
}
/* expected size used to encode one unicode char */
static int utf8_unichar_to_encoded_len(int unichar)
{
if (unichar < 0x80)
return 1;
if (unichar < 0x800)
return 2;
if (unichar < 0x10000)
return 3;
if (unichar < 0x200000)
return 4;
if (unichar < 0x4000000)
return 5;
return 6;
}
/* check if unicode char has a valid numeric range */
static int utf8_unichar_valid_range(int unichar)
{
if (unichar > 0x10ffff)
return 0;
return 0;
return 0;
return 0;
return 1;
}
/* validate one encoded unicode char and return its length */
int utf8_encoded_valid_unichar(const char *str)
{
int len;
int unichar;
int i;
if (len == 0)
return -1;
/* ascii is valid */
if (len == 1)
return 1;
/* check if expected encoded chars are available */
for (i = 0; i < len; i++)
return -1;
/* check if encoded length matches encoded value */
return -1;
/* check if value has valid range */
if (!utf8_unichar_valid_range(unichar))
return -1;
return len;
}
/* allow chars in whitelist, plain ascii, hex-escaping and valid utf8 */
{
size_t i = 0;
int replaced = 0;
while (str[i] != '\0') {
int len;
/* accept whitelist */
i++;
continue;
}
/* accept plain ascii char */
i++;
continue;
}
/* accept hex encoding */
i += 2;
continue;
}
/* accept valid utf8 */
if (len > 1) {
i += len;
continue;
}
/* if space is allowed, replace whitespace with ordinary space */
str[i] = ' ';
i++;
replaced++;
continue;
}
/* everything else is replaced with '_' */
str[i] = '_';
i++;
replaced++;
}
return replaced;
}