int encode_devnode_name(const char *str, char *str_enc, size_t len) { size_t i, j; if (!str || !str_enc) return -EINVAL; for (i = 0, j = 0; str[i] != '\0'; i++) { int seqlen; seqlen = utf8_encoded_valid_unichar(&str[i]); if (seqlen > 1) { if (len-j < (size_t)seqlen) return -EINVAL; memcpy(&str_enc[j], &str[i], seqlen); j += seqlen; i += (seqlen-1); } else if (str[i] == '\\' || !whitelisted_char_for_devnode(str[i], NULL)) { if (len-j < 4) return -EINVAL; sprintf(&str_enc[j], "\\x%02x", (unsigned char) str[i]); j += 4; } else { if (len-j < 1) return -EINVAL; str_enc[j] = str[i]; j++; } } if (len-j < 1) return -EINVAL; str_enc[j] = '\0'; return 0; }
/* allow chars in whitelist, plain ascii, hex-escaping and valid utf8 */ size_t util_replace_chars(char *str, const char *white) { size_t i = 0, replaced = 0; assert(str); while (str[i] != '\0') { int len; if (whitelisted_char_for_devnode(str[i], white)) { i++; continue; } /* accept hex encoding */ if (str[i] == '\\' && str[i+1] == 'x') { i += 2; continue; } /* accept valid utf8 */ len = utf8_encoded_valid_unichar(str + i, (size_t) -1); if (len > 1) { i += len; continue; } /* if space is allowed, replace whitespace with ordinary space */ if (isspace(str[i]) && white && strchr(white, ' ')) { str[i] = ' '; i++; replaced++; continue; } /* everything else is replaced with '_' */ str[i] = '_'; i++; replaced++; } return replaced; }