summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAmitesh Singh <amitesh.sh@samsung.com>2017-11-24 14:56:10 +0900
committerAmitesh Singh <amitesh.sh@samsung.com>2017-11-24 14:58:15 +0900
commit475ae7ef1bf6b44a5d4c3abba21a4a2fa2d565f5 (patch)
tree7e529567309524d6ea9288c5f8c0ddbba37db1f0
parente9d7436fd22208d4595f28b9ef2f437f01c02311 (diff)
Efl.Ui.Format: add string format checking for user input
-rw-r--r--src/lib/efl/interfaces/efl_ui_format.c76
1 files changed, 76 insertions, 0 deletions
diff --git a/src/lib/efl/interfaces/efl_ui_format.c b/src/lib/efl/interfaces/efl_ui_format.c
index b55cda0..390a451 100644
--- a/src/lib/efl/interfaces/efl_ui_format.c
+++ b/src/lib/efl/interfaces/efl_ui_format.c
@@ -1,16 +1,92 @@
1#include "config.h" 1#include "config.h"
2#include "Efl.h" 2#include "Efl.h"
3 3
4#define ERR(...) EINA_LOG_DOM_ERR(EINA_LOG_DOMAIN_DEFAULT, __VA_ARGS__)
5
4typedef struct 6typedef struct
5{ 7{
6 const char *template; 8 const char *template;
7} Efl_Ui_Format_Data; 9} Efl_Ui_Format_Data;
8 10
11static Eina_Bool
12_is_valid_digit(char x)
13{
14 return ((x >= '0' && x <= '9') || (x == '.')) ? EINA_TRUE : EINA_FALSE;
15}
16
17static int
18_format_string_check(const char *fmt)
19{
20 const char *itr = NULL;
21 const char *start = NULL;
22 Eina_Bool found = EINA_FALSE;
23 int ret_type = 0;
24
25 start = strchr(fmt, '%');
26 if (!start) return 0;
27
28 while (start)
29 {
30 if (found && start[1] != '%')
31 {
32 return 0;
33 }
34
35 if (start[1] != '%' && !found)
36 {
37 found = EINA_TRUE;
38 for (itr = start + 1; *itr != '\0'; itr++)
39 {
40 if ((*itr == 'd') || (*itr == 'u') || (*itr == 'i') ||
41 (*itr == 'o') || (*itr == 'x') || (*itr == 'X'))
42 {
43 ret_type = 1; //int
44 break;
45 }
46 else if ((*itr == 'f') || (*itr == 'F'))
47 {
48 ret_type = 2; //double
49 break;
50 }
51 else if (_is_valid_digit(*itr))
52 {
53 continue;
54 }
55 else
56 {
57 return 0;
58 }
59 }
60 }
61 start = strchr(start + 2, '%');
62 }
63
64 return ret_type;
65}
66
9static void 67static void
10_default_format_cb(void *data, Eina_Strbuf *str, const Eina_Value value) 68_default_format_cb(void *data, Eina_Strbuf *str, const Eina_Value value)
11{ 69{
12 const Eina_Value_Type *type = eina_value_type_get(&value); 70 const Eina_Value_Type *type = eina_value_type_get(&value);
13 Efl_Ui_Format_Data *sd = data; 71 Efl_Ui_Format_Data *sd = data;
72 int format_check_result;
73
74 if (type == EINA_VALUE_TYPE_TM)
75 {
76 struct tm v;
77 eina_value_get(&value, &v);
78 eina_strbuf_append_strftime(str, sd->template, &v);
79
80 return;
81 }
82
83 format_check_result = _format_string_check(sd->template);
84
85 if (format_check_result == 0)
86 {
87 ERR("Wrong String Format: %s\n", sd->template);
88 return;
89 }
14 90
15 if (type == EINA_VALUE_TYPE_DOUBLE) 91 if (type == EINA_VALUE_TYPE_DOUBLE)
16 { 92 {