[gimp/gimp-2-6] Additional fixes for handling UTF-8 coded strings (Bugs 572865 & 628893)



commit d2986674793f39d3582a5f983c7a1b719db5edad
Author: Kevin Cozens <kcozens cvs gnome org>
Date:   Mon Sep 20 23:47:55 2010 -0400

    Additional fixes for handling UTF-8 coded strings (Bugs 572865 & 628893)
    
    plug-ins/script-fu/scheme-wrapper.c:
    - Fixed set and get of UTF-8 coded strings for parasites using slightly
      modified version of a patch from Massimo.
    
    plug-ins/script-fu/tinyscheme/scheme.c:
    - Additional fix to string-append and fixed substring. The mk_empty_string
      routine is not the way to allocate space for UTF-8 coded strings.
    - Added some comments.
    (Cherry picked of commit b0d8ba5ffddd46798a7480d75f5de3c8d808b9ad)

 plug-ins/script-fu/scheme-wrapper.c    |   10 +++++---
 plug-ins/script-fu/tinyscheme/scheme.c |   35 +++++++++++++++++++++++++------
 2 files changed, 34 insertions(+), 11 deletions(-)
---
diff --git a/plug-ins/script-fu/scheme-wrapper.c b/plug-ins/script-fu/scheme-wrapper.c
index d3deb5e..8ea6220 100644
--- a/plug-ins/script-fu/scheme-wrapper.c
+++ b/plug-ins/script-fu/scheme-wrapper.c
@@ -1251,10 +1251,9 @@ script_fu_marshal_procedure_call (scheme  *sc,
                   break;
                 }
 
-              args[i].data.d_parasite.size =
-                sc->vptr->ivalue (sc->vptr->pair_car (temp_val));
               args[i].data.d_parasite.data =
                 sc->vptr->string_value (sc->vptr->pair_car (temp_val));
+              args[i].data.d_parasite.size = strlen (args[i].data.d_parasite.data);
 
 #if DEBUG_MARSHALL
               g_printerr (", size %d\n", args[i].data.d_parasite.size);
@@ -1596,6 +1595,8 @@ script_fu_marshal_procedure_call (scheme  *sc,
                 else
                   {
                     GimpParasite *p = &values[i + 1].data.d_parasite;
+                    gchar        *data = g_strndup (p->data, p->size);
+                    gint          char_cnt = g_utf8_strlen (data, p->size);
                     pointer       temp_val;
 
                     /* don't move the mk_foo() calls outside this function call,
@@ -1607,12 +1608,13 @@ script_fu_marshal_procedure_call (scheme  *sc,
                                    sc->vptr->mk_integer (sc, p->flags),
                                    sc->vptr->cons (sc,
                                      sc->vptr->mk_counted_string (sc,
-                                                                  p->data,
-                                                                  p->size),
+                                                                  data,
+                                                                  char_cnt),
                                      sc->NIL)));
                     return_val = sc->vptr->cons (sc,
                                                  temp_val,
                                                  return_val);
+                    g_free (data);
 
 #if DEBUG_MARSHALL
                     g_printerr ("      name '%s'\n", p->name);
diff --git a/plug-ins/script-fu/tinyscheme/scheme.c b/plug-ins/script-fu/tinyscheme/scheme.c
index da7b484..60440fc 100644
--- a/plug-ins/script-fu/tinyscheme/scheme.c
+++ b/plug-ins/script-fu/tinyscheme/scheme.c
@@ -1065,6 +1065,7 @@ INTERFACE pointer mk_string(scheme *sc, const char *str) {
      return mk_counted_string(sc,str,g_utf8_strlen(str, -1));
 }
 
+/* str points to a NUL terminated string. */
 /* len is the length of str in characters */
 INTERFACE pointer mk_counted_string(scheme *sc, const char *str, int len) {
      pointer x = get_cell(sc, sc->NIL, sc->NIL);
@@ -1075,6 +1076,7 @@ INTERFACE pointer mk_counted_string(scheme *sc, const char *str, int len) {
      return (x);
 }
 
+/* len is the length for the empty string in characters */
 INTERFACE pointer mk_empty_string(scheme *sc, int len, gunichar fill) {
      pointer x = get_cell(sc, sc->NIL, sc->NIL);
 
@@ -3436,8 +3438,8 @@ static pointer opexe_2(scheme *sc, enum scheme_opcodes op) {
      case OP_STRAPPEND: { /* string-append */
        /* in 1.29 string-append was in Scheme in init.scm but was too slow */
        int len = 0;
-       pointer newstr;
        pointer car_x;
+       char *newstr;
        char *pos;
        char *end;
 
@@ -3447,10 +3449,15 @@ static pointer opexe_2(scheme *sc, enum scheme_opcodes op) {
           end = g_utf8_offset_to_pointer(strvalue(car_x), (long)strlength(car_x));
           len += end - strvalue(car_x);
        }
-       newstr = mk_empty_string(sc, len, ' ');
+
+       newstr = (char *)sc->malloc(len+1);
+       if (newstr == NULL) {
+          sc->no_memory=1;
+          Error_1(sc,"string-set!: No memory to append strings:",car(sc->args));
+       }
 
        /* store the contents of the argument strings into the new string */
-       pos = strvalue(newstr);
+       pos = newstr;
        for (x = sc->args; x != sc->NIL; x = cdr(x)) {
            car_x = car(x);
            end = g_utf8_offset_to_pointer(strvalue(car_x), (long)strlength(car_x));
@@ -3459,7 +3466,11 @@ static pointer opexe_2(scheme *sc, enum scheme_opcodes op) {
            pos += len;
        }
        *pos = '\0';
-       s_return(sc, newstr);
+
+       car_x = mk_string(sc, newstr);
+       g_free(newstr);
+
+       s_return(sc, car_x);
      }
 
      case OP_SUBSTR: { /* substring */
@@ -3488,12 +3499,22 @@ static pointer opexe_2(scheme *sc, enum scheme_opcodes op) {
                index1=g_utf8_strlen(str, -1);
           }
 
+          /* store the contents of the argument strings into the new string */
           beg = g_utf8_offset_to_pointer(str, (long)index0);
           end = g_utf8_offset_to_pointer(str, (long)index1);
           len=end-beg;
-          x=mk_empty_string(sc,len,' ');
-          memcpy(strvalue(x),beg,len);
-          strvalue(x)[len] = '\0';
+
+          str = (char *)sc->malloc(len+1);
+          if (str == NULL) {
+             sc->no_memory=1;
+             Error_1(sc,"string-set!: No memory to extract substring:",car(sc->args));
+          }
+
+          memcpy(str, beg, len);
+          str[len] = '\0';
+
+          x = mk_string(sc, str);
+          g_free(str);
 
           s_return(sc,x);
      }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]