Skip to content

Commit

Permalink
utils: refactor variable names
Browse files Browse the repository at this point in the history
Signed-off-by: Matthew Fala <[email protected]>
  • Loading branch information
matthewfala committed Nov 16, 2021
1 parent ff950f1 commit a006b7b
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions src/flb_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,8 @@ int flb_utils_write_str(char *buf, int *off, size_t size,
int len;
int hex_bytes;
int is_valid;
int utf_sequence_number;
int utf_sequence_length;
uint32_t codepoint;
uint32_t state = 0;
char tmp[16];
Expand Down Expand Up @@ -741,59 +743,61 @@ int flb_utils_write_str(char *buf, int *off, size_t size,
i += (hex_bytes - 1);
}
else if (c > 0xFFFF) {
hex_bytes = flb_utf8_len(str + i);
utf_sequence_length = flb_utf8_len(str + i);
if (available - written < 6) {
return FLB_FALSE;
}

if (i + hex_bytes > str_len) {
if (i + utf_sequence_length > str_len) {
break; /* skip truncated UTF-8 */
}

is_valid = FLB_TRUE;
for (b = 0; b < hex_bytes; b++) {
for (utf_sequence_number = 0; utf_sequence_number < utf_sequence_length;
utf_sequence_number++) {
/* Leading characters must start with bits 11 */
if (b == 0 && ((str[i] & 0xC0) != 0xC0)) {
if (utf_sequence_number == 0 && ((str[i] & 0xC0) != 0xC0)) {
/* Invalid unicode character. replace */
flb_debug("[pack] unexpected UTF-8 leading byte, "
"substituting character with replacement character");
tmp[b] = str[i];
tmp[utf_sequence_number] = str[i];
++i; /* Consume invalid leading byte */
hex_bytes = b + 1;
utf_sequence_length = utf_sequence_number + 1;
is_valid = FLB_FALSE;
break;
}
/* Trailing characters must start with bits 10 */
else if (b > 0 && ((str[i] & 0xC0) != 0x80)) {
else if (utf_sequence_number > 0 && ((str[i] & 0xC0) != 0x80)) {
/* Invalid unicode character. replace */
flb_debug("[pack] unexpected UTF-8 continuation byte, "
"substituting character with replacement character");
/* This byte, i, is the start of the next unicode character */
hex_bytes = b;
utf_sequence_length = utf_sequence_number;
is_valid = FLB_FALSE;
break;
}

tmp[b] = str[i];
tmp[utf_sequence_number] = str[i];
++i;
}
--i;

if (is_valid) {
encoded_to_buf(p, tmp, hex_bytes);
p += hex_bytes;
encoded_to_buf(p, tmp, utf_sequence_length);
p += utf_sequence_length;
}
else {
encoded_to_buf(p, flb_utils_replace_fragment_start,
FLB_UTILS_REPLACE_FRAGMENT_START_LEN);
p += FLB_UTILS_REPLACE_FRAGMENT_START_LEN;

for (b = 0; b < hex_bytes; ++b) {
if (b > 0) {
for (utf_sequence_number = 0; utf_sequence_number < utf_sequence_length;
++utf_sequence_number) {
if (utf_sequence_number > 0) {
*p++ = ' ';
}
*p++ = int2hex[(tmp[b] & 0xff) / 0x10];
*p++ = int2hex[(tmp[b] & 0xff) % 0x10];
*p++ = int2hex[(tmp[utf_sequence_number] & 0xff) / 0x10];
*p++ = int2hex[(tmp[utf_sequence_number] & 0xff) % 0x10];
}

encoded_to_buf(p, flb_utils_replace_fragment_end,
Expand Down

0 comments on commit a006b7b

Please sign in to comment.