get_best_eci: check UTF-8 before returning 26; move is_valid_utf8 to common

This commit is contained in:
gitlost 2021-03-21 17:35:52 +00:00
parent 7f74414666
commit 2d962c6321
7 changed files with 104 additions and 64 deletions

View file

@ -335,6 +335,20 @@ INTERNAL unsigned int decode_utf8(unsigned int *state, unsigned int *codep, cons
return *state;
}
/* Is string valid UTF-8? */
INTERNAL int is_valid_utf8(const unsigned char source[], const int length) {
int i;
unsigned int codepoint, state = 0;
for (i = 0; i < length; i++) {
if (decode_utf8(&state, &codepoint, source[i]) == 12) {
return 0;
}
}
return state == 0;
}
/* Convert UTF-8 to Unicode. If `disallow_4byte` unset, allow all values (UTF-32). If `disallow_4byte` set,
* only allow codepoints <= U+FFFF (ie four-byte sequences not allowed) (UTF-16, no surrogates) */
INTERNAL int utf8_to_unicode(struct zint_symbol *symbol, const unsigned char source[], unsigned int vals[],