Permalink
Browse files

protect ftgl. it might crash on broken utf8

  • Loading branch information...
1 parent 8258300 commit 42fcb288586ce36f44c9376b5443d1f7f75a7a86 @dividuum committed Apr 29, 2012
Showing with 66 additions and 0 deletions.
  1. +8 −0 LICENSE.txt
  2. +5 −0 font.c
  3. +42 −0 misc.c
  4. +11 −0 misc.h
View
@@ -38,6 +38,14 @@ files in included in their header.
Includes code based on lua-users wiki:
http://lua-users.org/wiki/UserDataWithPointerExample
+ Includes code based on
+ http://bjoern.hoehrmann.de/utf-8/decoder/dfa/
+
+ misc.c
+
+ Includes code based on
+ http://bjoern.hoehrmann.de/utf-8/decoder/dfa/
+
userlib.lua
Includes code based on lua-users wiki:
http://lua-users.org/wiki/TableSerialization
View
5 font.c
@@ -26,6 +26,11 @@ static int font_write(lua_State *L) {
GLfloat x = luaL_checknumber(L, 2);
GLfloat y = luaL_checknumber(L, 3);
const char *text = luaL_checkstring(L, 4);
+
+ // Protect FTGL
+ if (!check_utf8(text))
+ luaL_error(L, "invalid utf8");
+
GLfloat size = luaL_checknumber(L, 5) / 1000.0;
int type = lua_type(L, 6);
View
42 misc.c
@@ -4,6 +4,9 @@
#include <stdlib.h>
#include <stdarg.h>
#include <sys/time.h>
+#include <stdint.h>
+
+#include "misc.h"
void die(const char *fmt, ...) {
va_list ap;
@@ -33,3 +36,42 @@ void *xmalloc(size_t size) {
return ptr;
}
+// Copyright (c) 2008-2009 Bjoern Hoehrmann <bjoern@hoehrmann.de>
+// See http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ for details.
+//
+// Modified for info-beamer
+
+static const uint8_t utf8d[] = {
+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 00..1f
+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 20..3f
+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 40..5f
+ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 60..7f
+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, // 80..9f
+ 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, // a0..bf
+ 8,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, // c0..df
+ 0xa,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x4,0x3,0x3, // e0..ef
+ 0xb,0x6,0x6,0x6,0x5,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8, // f0..ff
+ 0x0,0x1,0x2,0x3,0x5,0x8,0x7,0x1,0x1,0x1,0x4,0x6,0x1,0x1,0x1,0x1, // s0..s0
+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,0,1,0,1,1,1,1,1,1, // s1..s2
+ 1,2,1,1,1,1,1,2,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1, // s3..s4
+ 1,2,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,3,1,3,1,1,1,1,1,1, // s5..s6
+ 1,3,1,1,1,1,1,3,1,3,1,1,1,1,1,1,1,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // s7..s8
+};
+
+uint32_t decode_utf8(uint32_t* state, uint32_t* codep, uint32_t byte) {
+ uint32_t type = utf8d[byte];
+
+ *codep = (*state != UTF8_ACCEPT) ?
+ (byte & 0x3fu) | (*codep << 6) :
+ (0xff >> type) & (byte);
+
+ *state = utf8d[256 + *state*16 + type];
+ return *state;
+}
+
+int check_utf8(const char* s) {
+ uint32_t codepoint, state = 0;
+ while (*s)
+ decode_utf8(&state, &codepoint, *(uint8_t*)s++);
+ return state == UTF8_ACCEPT;
+}
View
11 misc.h
@@ -4,6 +4,7 @@
#define MISC_H
#include <sys/time.h>
+#include <stdint.h>
#include <GL/gl.h>
#include <lualib.h>
@@ -89,4 +90,14 @@ int type##_register(lua_State *L) { \
return 1; \
}
+// Copyright (c) 2008-2009 Bjoern Hoehrmann <bjoern@hoehrmann.de>
+// See http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ for details.
+//
+// Modified for info-beamer
+
+#define UTF8_ACCEPT 0
+#define UTF8_REJECT 1
+
+int check_utf8(const char* s);
+
#endif

0 comments on commit 42fcb28

Please sign in to comment.