static int fd_read(struct current *current)
{
#ifdef USE_UTF8
- char buf[4];
+ char buf[MAX_UTF8_LEN];
int n;
int i;
int c;
return -1;
}
n = utf8_charlen(buf[0]);
- if (n < 1 || n > 3) {
+ if (n < 1) {
return -1;
}
for (i = 1; i < n; i++) {
return -1;
}
}
- buf[n] = 0;
/* decode and return the character */
utf8_tounicode(buf, &c);
return c;
}
#endif
+#ifndef utf8_getchars
static int utf8_getchars(char *buf, int c)
{
#ifdef USE_UTF8
return 1;
#endif
}
+#endif
/**
* Returns the unicode character at the given offset,
*/
static int insert_char(struct current *current, int pos, int ch)
{
- char buf[3];
+ char buf[MAX_UTF8_LEN];
int n = utf8_getchars(buf, ch);
if (has_room(current, n) && pos >= 0 && pos <= current->chars) {
skipsame = 1;
}
else if (c >= ' ') {
- if (rlen >= (int)sizeof(rbuf) + 3) {
+ /* >= here to allow for null terminator */
+ if (rlen >= (int)sizeof(rbuf) - MAX_UTF8_LEN) {
continue;
}
}
break;
case ctrl('V'): /* ctrl-v */
- if (has_room(current, 3)) {
+ if (has_room(current, MAX_UTF8_LEN)) {
/* Insert the ^V first */
if (insert_char(current, current->pos, c)) {
refreshLine(current->prompt, current);
if (bytelen < 0) {
bytelen = strlen(str);
}
- while (bytelen) {
+ while (bytelen > 0) {
int c;
int l = utf8_tounicode(str, &c);
charlen++;
if (s[0] < 0xe0) {
if ((s[1] & 0xc0) == 0x80) {
*uc = ((s[0] & ~0xc0) << 6) | (s[1] & ~0x80);
- return 2;
+ if (*uc >= 0x80) {
+ return 2;
+ }
+ /* Otherwise this is an invalid sequence */
}
}
else if (s[0] < 0xf0) {
if (((str[1] & 0xc0) == 0x80) && ((str[2] & 0xc0) == 0x80)) {
*uc = ((s[0] & ~0xe0) << 12) | ((s[1] & ~0x80) << 6) | (s[2] & ~0x80);
- return 3;
+ if (*uc >= 0x800) {
+ return 3;
+ }
+ /* Otherwise this is an invalid sequence */
}
}
else if (s[0] < 0xf8) {
if (((str[1] & 0xc0) == 0x80) && ((str[2] & 0xc0) == 0x80) && ((str[3] & 0xc0) == 0x80)) {
*uc = ((s[0] & ~0xf0) << 18) | ((s[1] & ~0x80) << 12) | ((s[2] & ~0x80) << 6) | (s[3] & ~0x80);
- return 4;
+ if (*uc >= 0x10000) {
+ return 4;
+ }
+ /* Otherwise this is an invalid sequence */
}
}
#ifndef USE_UTF8
#include <ctype.h>
+#define MAX_UTF8_LEN 1
+
/* No utf-8 support. 1 byte = 1 char */
#define utf8_strlen(S, B) ((B) < 0 ? (int)strlen(S) : (B))
#define utf8_strwidth(S, B) utf8_strlen((S), (B))
#define utf8_width(C) 1
#else
+
+#define MAX_UTF8_LEN 4
+
/**
* Converts the given unicode codepoint (0 - 0x1fffff) to utf-8
* and stores the result at 'p'.