diff --git a/homoglyph.c b/homoglyph.c
index f335131..acd9ac1 100644
--- a/homoglyph.c
+++ b/homoglyph.c
@@ -24,7 +24,7 @@ const char* homoglyphs[] = {
"\u200A\u200A\u200A\u200A"
};
-static int find_map_index(const char *map[], char c)
+static int find_map_index(const char* map[], char c)
{
int homoglyphs_count = sizeof(homoglyphs) / sizeof(homoglyphs[0]);
for (int i = 0; i < sizeof(ascii_table) / sizeof(ascii_table[0]); i++)
@@ -39,7 +39,7 @@ static int find_map_index(const char *map[], char c)
return -1;
}
-static void hexdump(const char *buf, size_t len)
+static void hexdump(const char* buf, size_t len)
{
for (size_t i = 0; i < len; i++)
{
@@ -48,7 +48,7 @@ static void hexdump(const char *buf, size_t len)
fprintf(stderr, "\n");
}
-static void safe_print(const char *buf, size_t len)
+static void safe_print(const char* buf, size_t len)
{
for (size_t i = 0; i < len; i++)
{
@@ -62,10 +62,9 @@ static void safe_print(const char *buf, size_t len)
}
}
fprintf(stderr, "\n");
-
}
-char *do_homoglyphs(char* str)
+char* do_homoglyphs(char* str)
{
// walk string backwards, substituting one char with multi-char
@@ -94,22 +93,36 @@ char *do_homoglyphs(char* str)
// }
// }
+
+ // find longest item in homoglyphs
+ unsigned long max_len_homoglyph = 0;
+ int homoglyphIndex = 0;
+ while (homoglyphs[homoglyphIndex] != NULL)
+ {
+ unsigned long len = strlen(homoglyphs[homoglyphIndex]);
+ if (len > max_len_homoglyph) max_len_homoglyph = len;
+ homoglyphIndex++;
+ }
+
unsigned long len = strlen(str);
- char* new_str = xmalloc(len * 4);
+ unsigned long alloc_sz = len * max_len_homoglyph;
+ char* new_str = xmalloc(alloc_sz);
+
+ // fprintf(stderr, "len=%lu max_len_homoglyph=%lu alloc=%lu\n", len, max_len_homoglyph, alloc_sz);
+
+ int targetIdx = 0;
for (unsigned long i = 0; i < len; i++)
{
+ // if (str[i] != '\n')
+ // fprintf(stderr, "%lu/%lu %02x=%c\n", i, strlen(new_str), str[i], str[i]);
unsigned long index = find_map_index(ascii_table, str[i]);
- if (index != -1)
+
+ char* text = index == -1 ? &str[i] : homoglyphs[index];
+ for (int idx = 0; idx < strlen(text); ++idx)
{
- char *homoglyph = (char*) homoglyphs[index];
- snprintf(new_str, strlen(new_str) + strlen(homoglyph), "%s%s", new_str, homoglyph);
+ new_str[targetIdx + idx] = text[idx];
}
- else snprintf(new_str, strlen(new_str) + 1, "%s%c", new_str, str[i]);
- // else printf("%c", str[i]);
-
- // fflush(stdout);
-
- // sleep_millisec(100);
+ targetIdx += strlen(text);
}
return new_str;
@@ -123,7 +136,7 @@ void print_homoglyphs(char* str)
unsigned long index = find_map_index(ascii_table, str[i]);
if (index != -1)
{
- char *homoglyph = (char*) homoglyphs[index];
+ char* homoglyph = (char*)homoglyphs[index];
printf("%s", homoglyph);
}
else printf("%c", str[i]);
@@ -132,4 +145,4 @@ void print_homoglyphs(char* str)
sleep_millisec(100);
}
-}
\ No newline at end of file
+}
|