Re:[PATCH] Octstr support for converting to and from HTML 4 entities.
"Rene Kluwen" <[email protected]>
| Newsgroups | gmane.comp.mobile.kannel.devel |
|---|---|
| Message-ID | <[email protected]> |
1. Don't forget to include the octstr_insert_char patch either. This code won't work without it (the patch). I do not believe any code that uses it works as it is now. (a quick grep yielded that indeed there is no code, using it atm). 2. test_octstr_format: Where can I find this function? ----- Original Message ----- Date: Mon September 23, 2002 04:55 PM From: Oded Arbel <[email protected]> To: Kannel-devel (E-mail) <[email protected]> Subject: [PATCH] Octstr support for converting to and from HTML 4 entities. Hi list. Here's the full HTML entity patch based on the work of Rene Kluwen (Thanks !) I only tested it to work as expected (both 'to' and 'from') and that no memory leaks are present - have not benchmarked it, but I think that the code by Rene should prove much faster then my original hack. though this patch does not change any behaviour and is used currently by no part of the publicly available Kannel, I'd like to have a vote on its usablity and cleanliness for inclusion in the CVS. the main changes from the patch submitted by Rene is reogranization of the two last if statements in find_entity() into an if..else statement in order to make gcc happy, and a change to html-entities.def to include the '<','>' and '"' entities with decimal values. P.S. the test_octstr_format detects two memory leaks - from first look I didn't understand what is causing them. could someone familiar with code please take a look ? -- Oded Arbel m-Wise mobile solutions On Monday 23 September 2002 04:13, Rene Kluwen / Chimit Software Solutions wrote: > Hello list, > > So I took my own challenge (I was bored ;]). > (Sorry, I will try to stick with only one patch for the same thing, next > time I post it.) > > Advantages of this patch over the previous one: > - It is faster, because it uses a binary search (didn't really bench > mark it though). > - It is less lines of code (so better to read and less chance on bugs) > - It doesn't rely on c-strings, except for the oct_str functions. > - The oct_str functions are also used for comparing, hencing porting > to other locales does not impose a problem. > > In principle, octstr_convert_to_html_entities could be made as fast by > binary searching the table as well. But it requires sorting the table. > If the compiler optimizes well enough (which it does), generating the > entities-table does not require cpu seconds at run-time. > > -- Rene...
octstr.patch
(text/x-diff, 3.7 KB)
--- gwlib/octstr.h 2002-09-04 20:42:57.000000000 +0300
+++ gwlib/octstr.h 2002-09-22 20:44:43.000000000 +0300
@@ -582,4 +582,17 @@
*/
int octstr_recode (Octstr *tocode, Octstr *fromcode, Octstr *orig);
+/*
+ * make data HTML safe by converting appropriate characters to HTML entities.
+ * conversion is done in place
+ */
+void octstr_convert_to_html_entities(Octstr* input);
+
+/*
+ * convert HTML safe data back to binary data by replacing HTML entities with their
+ * respective character values.
+ * conversion is done in place
+ */
+void octstr_convert_from_html_entities(Octstr* input);
+
#endif
--- gwlib/octstr.c 2002-09-23 16:13:13.000000000 +0300
+++ gwlib/octstr.c 2002-09-23 16:37:40.000000000 +0300
@@ -2181,3 +2182,95 @@
return resultcode;
}
+
+/*
+ * function octstr_convert_to_html_entities()
+ * make data HTML safe by converting appropriate characters to HTML entities
+ * Input: data to be inserted in HTML
+ **/
+void octstr_convert_to_html_entities(Octstr* input)
+{
+ int i;
+
+ for (i = 0; i < octstr_len(input); ++i) {
+ switch (octstr_get_char(input, i)) {
+#define ENTITY(a,b) \
+ case a: \
+ octstr_delete(input, i, 1); \
+ octstr_insert(input, octstr_imm("&" b ";"), i); \
+ i += sizeof(b); break;
+#include "gwlib/html-entities.def"
+#undef ENTITY
+ }
+ }
+}
+
+/*
+ * This function is meant to find html entities in an octstr.
+ * The html-entities.def file must be sorted alphabetically for
+ * this function to work (according to current Locale in use).
+*/
+static int octstr_find_entity(Octstr* input, int startfind, int endfind)
+{
+#define ENTITY(a,b) { a, b },
+ struct entity_struct {
+ int entity;
+ char *entity_str;
+ };
+ const struct entity_struct entities[] = {
+#include "html-entities.def"
+ { -1, "" } /* pivot */
+ };
+#undef ENTITY
+ int center; /* position in table that we are about to compare */
+ int matchresult; /* result of match agains found entity name. indicates less, equal or greater */
+
+ if (endfind == 0) {
+ /* when calling this function we do not (nor even want to) know the
+ * sizeof(entities). Hence this check. */
+ endfind = (sizeof(entities) / sizeof(struct entity_struct)) - 1;
+ }
+ center = startfind + ((endfind - startfind) / 2);
+ matchresult = octstr_str_compare(input, entities[center].entity_str);
+ if (matchresult == 0) {
+ return entities[center].entity;
+ }
+ if (endfind - startfind <= 1) {
+ /* we are at the end of our results */
+ return -1;
+ }
+ if (matchresult < 0) {
+ /* keep searching in first part of the table */
+ return octstr_find_entity(input, startfind, center);
+ } else {
+ /* keep searching in last part of the table */
+ return octstr_find_entity(input, center, endfind);
+ }
+}
+
+/*
+ * function octstr_convert_from_html_entities()
+ * convert HTML safe data back to binary data by replacing HTML entities with their
+ * respective character values
+ * Input: data to be inserted in HTML
+ **/
+void octstr_convert_from_html_entities(Octstr* input)
+{
+ int startpos = 0, endpos;
+ int entity;
+ Octstr *match;
+
+ while ((startpos = octstr_search_char(input, '&', startpos)) != -1) {
+ endpos = octstr_search_char(input, ';', startpos + 1);
+ if (endpos >= 0) {
+ match = octstr_copy(input, startpos + 1, endpos - startpos - 1);
+ entity = octstr_find_entity(match, 0, 0);
+ if (entity >= 0) {
+ octstr_delete(input, startpos, endpos - startpos + 1);
+ octstr_insert_char(input, startpos, entity);
+ }
+ octstr_destroy(match);
+ }
+ startpos++;
+ }
+}
html-entities.def
(text/x-csrc, 2 KB)
/* Please keep this file sorted alphabetically (according to current * Locale in use) */ ENTITY(198,"AElig") ENTITY(193,"Aacute") ENTITY(194,"Acirc") ENTITY(192,"Agrave") ENTITY(197,"Aring") ENTITY(195,"Atilde") ENTITY(196,"Auml") ENTITY(199,"Ccedil") ENTITY(208,"ETH") ENTITY(201,"Eacute") ENTITY(202,"Ecirc") ENTITY(200,"Egrave") ENTITY(203,"Euml") ENTITY(205,"Iacute") ENTITY(206,"Icirc") ENTITY(204,"Igrave") ENTITY(207,"Iuml") ENTITY(209,"Ntilde") ENTITY(211,"Oacute") ENTITY(212,"Ocirc") ENTITY(210,"Ograve") ENTITY(216,"Oslash") ENTITY(213,"Otilde") ENTITY(214,"Ouml") ENTITY(222,"THORN") ENTITY(218,"Uacute") ENTITY(219,"Ucirc") ENTITY(217,"Ugrave") ENTITY(220,"Uuml") ENTITY(221,"Yacute") ENTITY(225,"aacute") ENTITY(226,"acirc") ENTITY(180,"acute") ENTITY(230,"aelig") ENTITY(224,"agrave") ENTITY(38, "amp") ENTITY(229,"aring") ENTITY(227,"atilde") ENTITY(228,"auml") ENTITY(166,"brvbar") ENTITY(231,"ccedil") ENTITY(184,"cedil") ENTITY(162,"cent") ENTITY(169,"copy") ENTITY(164,"curren") ENTITY(176,"deg") ENTITY(247,"divide") ENTITY(233,"eacute") ENTITY(234,"ecirc") ENTITY(232,"egrave") ENTITY(240,"eth") ENTITY(235,"euml") ENTITY(189,"frac12") ENTITY(188,"frac14") ENTITY(190,"frac34") ENTITY(62, "gt") ENTITY(237,"iacute") ENTITY(238,"icirc") ENTITY(161,"iexcl") ENTITY(236,"igrave") ENTITY(191,"iquest") ENTITY(239,"iuml") ENTITY(171,"laquo") ENTITY(60, "lt") ENTITY(175,"macr") ENTITY(181,"micro") ENTITY(183,"middot") ENTITY(160,"nbsp") ENTITY(172,"not") ENTITY(241,"ntilde") ENTITY(243,"oacute") ENTITY(244,"ocirc") ENTITY(242,"ograve") ENTITY(170,"ordf") ENTITY(186,"ordm") ENTITY(248,"oslash") ENTITY(245,"otilde") ENTITY(246,"ouml") ENTITY(182,"para") ENTITY(177,"plusmn") ENTITY(163,"pound") ENTITY(34, "quot") ENTITY(187,"raquo") ENTITY(174,"reg") ENTITY(167,"sect") ENTITY(173,"shy") ENTITY(185,"sup1") ENTITY(178,"sup2") ENTITY(179,"sup3") ENTITY(223,"szlig") ENTITY(254,"thorn") ENTITY(215,"times") ENTITY(250,"uacute") ENTITY(251,"ucirc") ENTITY(249,"ugrave") ENTITY(168,"uml") ENTITY(252,"uuml") ENTITY(253,"yacute") ENTITY(165,"yen")