Changeset 68 for trunk/src/sh_utils.c
- Timestamp:
- Oct 30, 2006, 12:03:44 AM (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/sh_utils.c
r34 r68 196 196 197 197 SL_RETURN(i, _("sh_util_hidesetup")); 198 } 199 200 char * sh_util_acl_compact(char * buf, ssize_t len) 201 { 202 unsigned char * p = (unsigned char *) buf; 203 int state = 0; 204 ssize_t rem = 0; 205 char * out; 206 207 SH_VALIDATE_NE(buf, NULL); 208 SH_VALIDATE_GE(len, 0); 209 210 out = SH_ALLOC(len + 1); 211 212 while (*p != '\0') { 213 214 /* -- not at start or after newline 215 */ 216 if (state == 1) { 217 if (*p == '\n' || *p == ' ' || *p == '\t' || *p == '#') { 218 while (*p != '\n') { 219 ++p; 220 if (*p == '\0') { 221 goto exit_it; 222 } 223 } 224 out[rem] = ','; ++rem; 225 while (p[1] == '\n') ++p; /* scan over consecutive newlines */ 226 state = 0; 227 if (p[1] == '\0') { 228 if (rem > 0) out[rem-1] = '\0'; 229 break; 230 } 231 } 232 else { 233 if (*p <= 0x7F && isgraph((int) *p)) { 234 out[rem] = (char) *p; ++rem; 235 } 236 } 237 } 238 239 /* -- at start or after newline 240 */ 241 else /* if (state == 0) */ { 242 if (0 == strncmp((char *) p, "user", 4)) { 243 out[rem] = 'u'; ++rem; 244 p += 3; state = 1; 245 } else if (0 == strncmp((char *) p, "group", 5)) { 246 out[rem] = 'g'; ++rem; 247 p += 4; state = 1; 248 } else if (0 == strncmp((char *) p, "mask", 4)) { 249 out[rem] = 'm'; ++rem; 250 p += 3; state = 1; 251 } else if (0 == strncmp((char *) p, "other", 5)) { 252 out[rem] = 'o'; 253 p += 4; state = 1; ++rem; 254 } else if (*p == '\0') { 255 if (rem > 0) { out[rem-1] = '\0'; } 256 break; 257 } else { 258 if (*p <= 0x7F && isprint((int) *p)) { 259 out[rem] = (char) *p; ++rem; 260 } 261 } 262 state = 1; 263 } 264 ++p; 265 } 266 exit_it: 267 out[rem] = '\0'; 268 return out; 198 269 } 199 270 … … 1343 1414 static unsigned char sh_obscure_index[256]; 1344 1415 1416 int sh_util_valid_utf8 (const unsigned char * str) 1417 { 1418 size_t len = strlen((char *)str); 1419 size_t l = 0; 1420 unsigned char c; 1421 1422 #define SH_VAL_UTF8_1 ((c != '\0') && ((c & 0x80) == 0x00)) 1423 #define SH_VAL_UTF8_2 ((c != '\0') && ((c & 0xE0) == 0xC0)) /* 110x xxxx */ 1424 #define SH_VAL_UTF8_3 ((c != '\0') && ((c & 0xF0) == 0xE0)) /* 1110 xxxx */ 1425 #define SH_VAL_UTF8_4 ((c != '\0') && ((c & 0xF8) == 0xF0)) /* 1111 0xxx */ 1426 #define SH_VAL_UTF8_N ((c != '\0') && ((c & 0xC0) == 0x80)) /* 10xx xxxx */ 1427 #define SH_VAL_BAD ((c == '"') || (c == '\t') || (c == '\b') || (c == '\f') || (c == '\n') || \ 1428 (c == '\r') || (c == '\v') || iscntrl((int) c) || \ 1429 (c != ' ' && !isgraph ((int) c))) 1430 1431 while(l < len) 1432 { 1433 c = str[l]; 1434 1435 if (SH_VAL_UTF8_1) 1436 { 1437 if (SH_VAL_BAD && (sh_obscure_index[c] != 1)) return S_FALSE; 1438 ++l; continue; /* ASCII character */ 1439 } 1440 else if (SH_VAL_UTF8_2) 1441 { 1442 if ((c & 0x3e) == 0x00) 1443 return S_FALSE; /* overlong 2-byte seq. */ 1444 ++l; if (l == len) return S_FALSE; c = str[l]; 1445 if(!SH_VAL_UTF8_N) return S_FALSE; 1446 ++l; continue; 1447 1448 } 1449 else if (SH_VAL_UTF8_3) 1450 { 1451 ++l; if (l == len) return S_FALSE; c = str[l]; 1452 if(!SH_VAL_UTF8_N) return S_FALSE; 1453 if (((str[l-1] & 0x1F) == 0x00) && ((c & 0x60) == 0x00)) 1454 return S_FALSE; /* overlong 3-byte seq. */ 1455 ++l; if (l == len) return S_FALSE; c = str[l]; 1456 if(!SH_VAL_UTF8_N) return S_FALSE; 1457 ++l; continue; 1458 } 1459 else if (SH_VAL_UTF8_4) 1460 { 1461 ++l; if (l == len) return S_FALSE; c = str[l]; 1462 if(!SH_VAL_UTF8_N) return S_FALSE; 1463 if (((str[l-1] & 0x0F) == 0x00) && ((c & 0x70) == 0x00)) 1464 return S_FALSE; /* overlong 4-byte seq. */ 1465 ++l; if (l == len) return S_FALSE; c = str[l]; 1466 if(!SH_VAL_UTF8_N) return S_FALSE; 1467 ++l; if (l == len) return S_FALSE; c = str[l]; 1468 if(!SH_VAL_UTF8_N) return S_FALSE; 1469 ++l; continue; 1470 } 1471 return S_FALSE; 1472 } 1473 return S_TRUE; 1474 } 1475 1476 1345 1477 int sh_util_obscure_ok (const char * str) 1346 1478 { … … 1387 1519 } 1388 1520 1521 static int sh_obscure_check_utf8 = S_FALSE; 1522 1523 int sh_util_obscure_utf8 (const char * c) 1524 { 1525 int i; 1526 SL_ENTER(_("sh_util_obscure_utf8")); 1527 i = sh_util_flagval(c, &(sh_obscure_check_utf8)); 1528 1529 SL_RETURN(i, _("sh_util_obscure_utf8")); 1530 } 1531 1532 1389 1533 int sh_util_obscurename (ShErrLevel level, char * name_orig, int flag) 1390 1534 { … … 1396 1540 1397 1541 ASSERT_RET((name != NULL), _("name != NULL"), (0)) 1542 1543 if (sh_obscure_check_utf8 == S_TRUE) 1544 { 1545 if (S_FALSE == sh_util_valid_utf8(name)) 1546 { 1547 goto err; 1548 } 1549 SL_RETURN((0),_("sh_util_obscurename")); 1550 } 1398 1551 1399 1552 /* -- Check name. -- … … 1410 1563 if (sh_obscure_index[i] != (unsigned char)1) 1411 1564 { 1412 if (flag == S_TRUE) 1413 { 1414 safe = sh_util_safe_name (name_orig); 1415 sh_error_handle (level, FIL__, __LINE__, 0, MSG_FI_OBSC, 1416 safe); 1417 SH_FREE(safe); 1418 } 1419 SL_RETURN((-1),_("sh_util_obscurename")); 1565 goto err; 1420 1566 } 1421 1567 } … … 1424 1570 1425 1571 SL_RETURN((0),_("sh_util_obscurename")); 1426 } 1572 1573 err: 1574 1575 if (flag == S_TRUE) 1576 { 1577 safe = sh_util_safe_name (name_orig); 1578 sh_error_handle (level, FIL__, __LINE__, 0, MSG_FI_OBSC, 1579 safe); 1580 SH_FREE(safe); 1581 } 1582 SL_RETURN((-1),_("sh_util_obscurename")); 1583 } 1584 1427 1585 #endif 1428 1586
Note:
See TracChangeset
for help on using the changeset viewer.