---
 src/stkutil.c |  219 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/stkutil.h |    2 +
 2 files changed, 221 insertions(+), 0 deletions(-)

diff --git a/src/stkutil.c b/src/stkutil.c
index 6f072e7..5da356c 100644
--- a/src/stkutil.c
+++ b/src/stkutil.c
@@ -5819,3 +5819,222 @@ const unsigned char *stk_pdu_from_envelope(const struct 
stk_envelope *envelope,
 
        return pdu;
 }
+
+static const char *html_colors[] = {
+       "#000000", /* Black */
+       "#808080", /* Dark Grey */
+       "#C11B17", /* Dark Red */
+       "#FBB117", /* Dark Yellow */
+       "#347235", /* Dark Green */
+       "#307D7E", /* Dark Cyan */
+       "#0000A0", /* Dark Blue */
+       "#C031C7", /* Dark Magenta */
+       "#C0C0C0", /* Grey */
+       "#FFFFFF", /* White */
+       "#FF0000", /* Bright Red */
+       "#FFFF00", /* Bright Yellow */
+       "#00FF00", /* Bright Green */
+       "#00FFFF", /* Bright Cyan */
+       "#0000FF", /* Bright Blue */
+       "#FF00FF", /* Bright Magenta */
+};
+
+#define STK_TEXT_FORMAT_ALIGN_MASK 0x03
+#define STK_TEXT_FORMAT_FONT_MASK 0x0C
+#define STK_TEXT_FORMAT_STYLE_MASK 0xF0
+#define STK_DEFAULT_TEXT_ALIGNMENT 0x00
+#define STK_TEXT_FORMAT_INIT -1
+
+/* Defined in ETSI 123 40 9.2.3.24.10.1.1 */
+enum stk_text_format_code {
+       STK_TEXT_FORMAT_LEFT_ALIGN = 0x00,
+       STK_TEXT_FORMAT_CENTER_ALIGN = 0x01,
+       STK_TEXT_FORMAT_RIGHT_ALIGN = 0x02,
+       STK_TEXT_FORMAT_NO_ALIGN = 0x03,
+       STK_TEXT_FORMAT_FONT_SIZE_LARGE = 0x04,
+       STK_TEXT_FORMAT_FONT_SIZE_SMALL = 0x08,
+       STK_TEXT_FORMAT_FONT_SIZE_RESERVED = 0x0c,
+       STK_TEXT_FORMAT_STYLE_BOLD = 0x10,
+       STK_TEXT_FORMAT_STYLE_ITALIC = 0x20,
+       STK_TEXT_FORMAT_STYLE_UNDERLINED = 0x40,
+       STK_TEXT_FORMAT_STYLE_STRIKETHROUGH = 0x80,
+};
+
+
+static void end_format(GString *string, guint8 code, guint8 color)
+{
+       if ((code & ~STK_TEXT_FORMAT_ALIGN_MASK) || color)
+               g_string_append(string, "</span>");
+
+       if ((code & STK_TEXT_FORMAT_ALIGN_MASK) != STK_TEXT_FORMAT_NO_ALIGN)
+               g_string_append(string, "</div>");
+}
+
+static void start_format(GString *string, guint8 code, guint8 color)
+{
+       guint8 align = code & STK_TEXT_FORMAT_ALIGN_MASK;
+       guint8 font = code & STK_TEXT_FORMAT_FONT_MASK;
+       guint8 style = code & STK_TEXT_FORMAT_STYLE_MASK;
+       int fg = color & 0x0f;
+       int bg = (color >> 4) & 0x0f;
+
+       /* align formatting applies to a block of test */
+       if (align != STK_TEXT_FORMAT_NO_ALIGN)
+               g_string_append(string, "<div style=\"");
+
+       switch (align) {
+       case STK_TEXT_FORMAT_RIGHT_ALIGN:
+               g_string_append(string, "text-align: right;\">");
+               break;
+       case STK_TEXT_FORMAT_CENTER_ALIGN:
+               g_string_append(string, "text-align: center;\">");
+               break;
+       case STK_TEXT_FORMAT_LEFT_ALIGN:
+               g_string_append(string, "text-align: left;\">");
+               break;
+       }
+
+       if (((code & ~STK_TEXT_FORMAT_ALIGN_MASK) == 0) && (color == 0))
+               return;
+
+       /* font, style, and color are inline */
+       g_string_append(string, "<span style=\"");
+
+       switch (font) {
+       case STK_TEXT_FORMAT_FONT_SIZE_LARGE:
+               g_string_append(string, "font-size: big;");
+               break;
+       case STK_TEXT_FORMAT_FONT_SIZE_SMALL:
+               g_string_append(string, "font-size: small;");
+               break;
+       }
+
+       switch (style) {
+       case STK_TEXT_FORMAT_STYLE_BOLD:
+               g_string_append(string, "font-weight: bold;");
+               break;
+       case STK_TEXT_FORMAT_STYLE_ITALIC:
+               g_string_append(string, "font-style: italic;");
+               break;
+       case STK_TEXT_FORMAT_STYLE_UNDERLINED:
+               g_string_append(string, "text-decoration: underline;");
+               break;
+       case STK_TEXT_FORMAT_STYLE_STRIKETHROUGH:
+               g_string_append(string, "text-decoration: line-through;");
+               break;
+       }
+
+       /* add any color */
+       if (fg)
+               g_string_append_printf(string, "color: %s;", html_colors[fg]);
+       if (bg)
+               g_string_append_printf(string, "background-color: %s;",
+                                               html_colors[bg]);
+       g_string_append(string, "\">");
+}
+
+char *stk_text_to_html(const char *text, int text_len,
+                               const unsigned char *attrs, int attrs_len)
+{
+       GString *string = g_string_sized_new(text_len + 1);
+       int *formats;
+       int pos, i, j, attr, prev_attr;
+       guint8 start, end, code, color, len, align;
+
+       formats = g_try_malloc0(sizeof(int) * (text_len + 1));
+       if (formats == NULL)
+               return NULL;
+
+       /* we will need formatting at the position beyond the last char */
+       for (i = 0; i <= text_len; i++)
+               formats[i] = STK_TEXT_FORMAT_INIT;
+
+       for (i = 0; i+3 < attrs_len; i += 4) {
+               start = attrs[i];
+               len = attrs[i + 1];
+               code = attrs[i + 2];
+
+               if (i + 3 < attrs_len)
+                       color = attrs[i + 3];
+               else
+                       color = 0;
+
+               if (len == 0)
+                       end = text_len;
+               else
+                       end = start + len;
+
+               /* sanity check values */
+               if (start > end || end > text_len)
+                       continue;
+
+               /*
+                * if the alignment is the same as either the default
+                * or the last alignment used, don't set any alignment
+                * value.
+                */
+               if (start == 0)
+                       align = STK_DEFAULT_TEXT_ALIGNMENT;
+               else {
+                       align = (formats[start - 1] & 0xFF) &
+                                       STK_TEXT_FORMAT_ALIGN_MASK;
+                       if (align == STK_TEXT_FORMAT_NO_ALIGN)
+                               align = STK_DEFAULT_TEXT_ALIGNMENT;
+               }
+
+               if ((code & STK_TEXT_FORMAT_ALIGN_MASK) == align)
+                       code |= STK_TEXT_FORMAT_NO_ALIGN;
+
+               attr = code | (color << 8);
+
+               for (j = start; j < end; j++)
+                       formats[j] = attr;
+       }
+
+       prev_attr = STK_TEXT_FORMAT_INIT;
+
+       for (pos = 0; pos <= text_len; pos++) {
+               attr = formats[pos];
+               if (attr != prev_attr) {
+                       if (prev_attr != STK_TEXT_FORMAT_INIT)
+                               end_format(string, prev_attr & 0xFF,
+                                                       (attr >> 8) & 0xFF);
+
+                       if (attr != STK_TEXT_FORMAT_INIT)
+                               start_format(string, attr & 0xFF,
+                                                       (attr >> 8) & 0xFF);
+
+                       prev_attr = attr;
+               }
+
+               if (pos == text_len)
+                       break;
+
+               switch (text[pos]) {
+               case '\n':
+                       g_string_append(string, "<br/>");
+                       break;
+               case '\r':
+                       g_string_append(string, "<br/>");
+                       if ((pos + 1 < text_len) && (text[pos + 1] == '\n'))
+                               pos++;
+                       break;
+               case '<':
+                       g_string_append(string, "&lt;");
+                       break;
+               case '>':
+                       g_string_append(string, "&gt;");
+                       break;
+               case '&':
+                       g_string_append(string, "&amp;");
+                       break;
+               default:
+                       g_string_append_c(string, text[pos]);
+               }
+       }
+
+       g_free(formats);
+
+       /* return characters from string. Caller must free char data */
+       return g_string_free(string, FALSE);
+}
diff --git a/src/stkutil.h b/src/stkutil.h
index ca4817e..2fbcd7d 100644
--- a/src/stkutil.h
+++ b/src/stkutil.h
@@ -1643,3 +1643,5 @@ const unsigned char *stk_pdu_from_response(const struct 
stk_response *response,
                                                unsigned int *out_length);
 const unsigned char *stk_pdu_from_envelope(const struct stk_envelope *envelope,
                                                unsigned int *out_length);
+char *stk_text_to_html(const char *text, int text_len,
+                               const unsigned char *attrs, int attrs_len);
-- 
1.6.6.1

_______________________________________________
ofono mailing list
ofono@ofono.org
http://lists.ofono.org/listinfo/ofono

Reply via email to