Make loading/saving dollar gesture templates endian clean (thanks, Martin!). Fixes Bugzilla #2674.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
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"); */