Commit 2f3f3287149cbc7b738feab90882735e720816c6

Ryan C. Gordon 2015-04-05T18:59:52

Make loading/saving dollar gesture templates endian clean (thanks, Martin!). Fixes Bugzilla #2674.

diff --git a/src/events/SDL_gesture.c b/src/events/SDL_gesture.c
index 28b85ad..746d116 100644
--- a/src/events/SDL_gesture.c
+++ b/src/events/SDL_gesture.c
@@ -24,6 +24,7 @@
 /* General mouse handling code for SDL */
 
 #include "SDL_events.h"
+#include "SDL_endian.h"
 #include "SDL_events_c.h"
 #include "SDL_gesture_c.h"
 
@@ -114,14 +115,34 @@ static unsigned long SDL_HashDollar(SDL_FloatPoint* points)
 
 static int SaveTemplate(SDL_DollarTemplate *templ, SDL_RWops *dst)
 {
-    if (dst == NULL) return 0;
+    if (dst == NULL) {
+        return 0;
+    }
 
     /* No Longer storing the Hash, rehash on load */
     /* if (SDL_RWops.write(dst, &(templ->hash), sizeof(templ->hash), 1) != 1) return 0; */
 
+#if SDL_BYTEORDER == SDL_LIL_ENDIAN
     if (SDL_RWwrite(dst, templ->path,
-                    sizeof(templ->path[0]),DOLLARNPOINTS) != DOLLARNPOINTS)
+                    sizeof(templ->path[0]),DOLLARNPOINTS) != DOLLARNPOINTS) {
         return 0;
+    }
+#else
+    {
+        SDL_DollarTemplate copy = *templ;
+        SDL_FloatPoint *p = copy.path;
+        int i;
+        for (i = 0; i < DOLLARNPOINTS; i++, p++) {
+            p->x = SDL_SwapFloatLE(p->x);
+            p->y = SDL_SwapFloatLE(p->y);
+        }
+
+        if (SDL_RWwrite(dst, copy.path,
+                        sizeof(copy.path[0]),DOLLARNPOINTS) != DOLLARNPOINTS) {
+            return 0;
+        }
+    }
+#endif
 
     return 1;
 }
@@ -203,17 +224,33 @@ int SDL_LoadDollarTemplates(SDL_TouchID touchId, SDL_RWops *src)
     SDL_GestureTouch *touch = NULL;
     if (src == NULL) return 0;
     if (touchId >= 0) {
-        for (i = 0; i < SDL_numGestureTouches; i++)
-            if (SDL_gestureTouch[i].id == touchId)
+        for (i = 0; i < SDL_numGestureTouches; i++) {
+            if (SDL_gestureTouch[i].id == touchId) {
                 touch = &SDL_gestureTouch[i];
-        if (touch == NULL) return SDL_SetError("given touch id not found");
+            }
+        }
+        if (touch == NULL) {
+            return SDL_SetError("given touch id not found");
+        }
     }
 
     while (1) {
         SDL_DollarTemplate templ;
 
-        if (SDL_RWread(src,templ.path,sizeof(templ.path[0]),DOLLARNPOINTS) <
-           DOLLARNPOINTS) break;
+        if (SDL_RWread(src,templ.path,sizeof(templ.path[0]),DOLLARNPOINTS) < DOLLARNPOINTS) {
+            if (loaded == 0) {
+                return SDL_SetError("could not read any dollar gesture from rwops");
+            }
+            break;
+        }
+
+#if SDL_BYTEORDER != SDL_LIL_ENDIAN
+        for (i = 0; i < DOLLARNPOINTS; i++) {
+            SDL_FloatPoint *p = &templ.path[i];
+            p->x = SDL_SwapFloatLE(p->x);
+            p->y = SDL_SwapFloatLE(p->y);
+        }
+#endif
 
         if (touchId >= 0) {
             /* printf("Adding loaded gesture to 1 touch\n"); */