Make loading/saving dollar gesture templates endian clean (thanks, Martin!).
authorRyan C. Gordon <icculus@icculus.org>
Sun, 05 Apr 2015 18:59:52 -0400
changeset 94579f2beba999b8
parent 9456 2d6ec08030a0
child 9458 543298b36b28
Make loading/saving dollar gesture templates endian clean (thanks, Martin!).

Fixes Bugzilla #2674.
src/events/SDL_gesture.c
     1.1 --- a/src/events/SDL_gesture.c	Sun Apr 05 18:44:24 2015 -0400
     1.2 +++ b/src/events/SDL_gesture.c	Sun Apr 05 18:59:52 2015 -0400
     1.3 @@ -24,6 +24,7 @@
     1.4  /* General mouse handling code for SDL */
     1.5  
     1.6  #include "SDL_events.h"
     1.7 +#include "SDL_endian.h"
     1.8  #include "SDL_events_c.h"
     1.9  #include "SDL_gesture_c.h"
    1.10  
    1.11 @@ -114,14 +115,34 @@
    1.12  
    1.13  static int SaveTemplate(SDL_DollarTemplate *templ, SDL_RWops *dst)
    1.14  {
    1.15 -    if (dst == NULL) return 0;
    1.16 +    if (dst == NULL) {
    1.17 +        return 0;
    1.18 +    }
    1.19  
    1.20      /* No Longer storing the Hash, rehash on load */
    1.21      /* if (SDL_RWops.write(dst, &(templ->hash), sizeof(templ->hash), 1) != 1) return 0; */
    1.22  
    1.23 +#if SDL_BYTEORDER == SDL_LIL_ENDIAN
    1.24      if (SDL_RWwrite(dst, templ->path,
    1.25 -                    sizeof(templ->path[0]),DOLLARNPOINTS) != DOLLARNPOINTS)
    1.26 +                    sizeof(templ->path[0]),DOLLARNPOINTS) != DOLLARNPOINTS) {
    1.27          return 0;
    1.28 +    }
    1.29 +#else
    1.30 +    {
    1.31 +        SDL_DollarTemplate copy = *templ;
    1.32 +        SDL_FloatPoint *p = copy.path;
    1.33 +        int i;
    1.34 +        for (i = 0; i < DOLLARNPOINTS; i++, p++) {
    1.35 +            p->x = SDL_SwapFloatLE(p->x);
    1.36 +            p->y = SDL_SwapFloatLE(p->y);
    1.37 +        }
    1.38 +
    1.39 +        if (SDL_RWwrite(dst, copy.path,
    1.40 +                        sizeof(copy.path[0]),DOLLARNPOINTS) != DOLLARNPOINTS) {
    1.41 +            return 0;
    1.42 +        }
    1.43 +    }
    1.44 +#endif
    1.45  
    1.46      return 1;
    1.47  }
    1.48 @@ -203,17 +224,33 @@
    1.49      SDL_GestureTouch *touch = NULL;
    1.50      if (src == NULL) return 0;
    1.51      if (touchId >= 0) {
    1.52 -        for (i = 0; i < SDL_numGestureTouches; i++)
    1.53 -            if (SDL_gestureTouch[i].id == touchId)
    1.54 +        for (i = 0; i < SDL_numGestureTouches; i++) {
    1.55 +            if (SDL_gestureTouch[i].id == touchId) {
    1.56                  touch = &SDL_gestureTouch[i];
    1.57 -        if (touch == NULL) return SDL_SetError("given touch id not found");
    1.58 +            }
    1.59 +        }
    1.60 +        if (touch == NULL) {
    1.61 +            return SDL_SetError("given touch id not found");
    1.62 +        }
    1.63      }
    1.64  
    1.65      while (1) {
    1.66          SDL_DollarTemplate templ;
    1.67  
    1.68 -        if (SDL_RWread(src,templ.path,sizeof(templ.path[0]),DOLLARNPOINTS) <
    1.69 -           DOLLARNPOINTS) break;
    1.70 +        if (SDL_RWread(src,templ.path,sizeof(templ.path[0]),DOLLARNPOINTS) < DOLLARNPOINTS) {
    1.71 +            if (loaded == 0) {
    1.72 +                return SDL_SetError("could not read any dollar gesture from rwops");
    1.73 +            }
    1.74 +            break;
    1.75 +        }
    1.76 +
    1.77 +#if SDL_BYTEORDER != SDL_LIL_ENDIAN
    1.78 +        for (i = 0; i < DOLLARNPOINTS; i++) {
    1.79 +            SDL_FloatPoint *p = &templ.path[i];
    1.80 +            p->x = SDL_SwapFloatLE(p->x);
    1.81 +            p->y = SDL_SwapFloatLE(p->y);
    1.82 +        }
    1.83 +#endif
    1.84  
    1.85          if (touchId >= 0) {
    1.86              /* printf("Adding loaded gesture to 1 touch\n"); */