Skip to content

Commit

Permalink
ALAC/ : Replace all un-aligned accesses with safe alternatives.
Browse files Browse the repository at this point in the history
  • Loading branch information
erikd committed Feb 13, 2013
1 parent 3c812eb commit 34f1fe1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 24 deletions.
5 changes: 4 additions & 1 deletion ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
Add tests for psf_put_be32() and psf_put_be64().

* src/sfendian.h src/test_endswap.(def|tpl)
Add functions psf_get_be32() and psf_gett_be64() with tests.
Add functions psf_get_be(16|32|64) with tests.
These are needed for platforms where un-aligned accesses cause bus faults.

* src/ALAC/ag_enc.c src/ALAC/alac_decoder.c
Replace all un-aligned accesses with safe alternatives.

2013-02-12 Erik de Castro Lopo <erikd AT mega-nerd DOT com>

* src/sfendian.h
Expand Down
18 changes: 6 additions & 12 deletions src/ALAC/ag_enc.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,16 +180,13 @@ static inline int32_t dyn_code_32bit(int32_t maxbits, uint32_t m, uint32_t k, ui

static inline void ALWAYS_INLINE dyn_jam_noDeref(unsigned char *out, uint32_t bitPos, uint32_t numBits, uint32_t value)
{
/* FIXME: Cast-align warning when compiling on armhf. */
uint32_t *i = (uint32_t *)(out + (bitPos >> 3));
uint32_t mask;
uint32_t curr;
uint32_t shift;

//Assert( numBits <= 32 );

curr = *i;
curr = Swap32NtoB( curr );
curr = psf_get_be32 (out, bitPos >> 3);

shift = 32 - (bitPos & 7) - numBits;

Expand All @@ -199,23 +196,20 @@ static inline void ALWAYS_INLINE dyn_jam_noDeref(unsigned char *out, uint32_t bi
value = (value << shift) & mask;
value |= curr & ~mask;

*i = Swap32BtoN( value );
psf_put_be32 (out, bitPos >> 3, value) ;
}


static inline void ALWAYS_INLINE dyn_jam_noDeref_large(unsigned char *out, uint32_t bitPos, uint32_t numBits, uint32_t value)
{
/* FIXME: Cast-align warning when compiling on armhf. */
uint32_t * i = (uint32_t *)(out + (bitPos>>3));
uint32_t w;
uint32_t curr;
uint32_t mask;
int32_t shiftvalue = (32 - (bitPos&7) - numBits);
int32_t shiftvalue = (32 - (bitPos&7) - numBits);

//Assert(numBits <= 32);

curr = *i;
curr = Swap32NtoB( curr );
curr = psf_get_be32 (out, bitPos >> 3);

if (shiftvalue < 0)
{
Expand All @@ -226,7 +220,7 @@ static inline void ALWAYS_INLINE dyn_jam_noDeref_large(unsigned char *out, uint3
mask = ~0u >> -shiftvalue;
w |= (curr & ~mask);

tailptr = ((uint8_t *)i) + 4;
tailptr = out + (bitPos>>3) + 4;
tailbyte = (value << ((8+shiftvalue))) & 0xff;
*tailptr = (uint8_t)tailbyte;
}
Expand All @@ -239,7 +233,7 @@ static inline void ALWAYS_INLINE dyn_jam_noDeref_large(unsigned char *out, uint3
w |= curr & ~mask;
}

*i = Swap32BtoN( w );
psf_put_be32 (out, bitPos >> 3, w) ;
}


Expand Down
23 changes: 12 additions & 11 deletions src/ALAC/alac_decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/

#include <stdlib.h>
#include <stddef.h>
#include <string.h>

#include "alac_codec.h"
Expand Down Expand Up @@ -87,21 +88,21 @@ alac_decoder_init (ALAC_DECODER *p, void * inMagicCookie, uint32_t inMagicCookie
if (theCookieBytesRemaining >= sizeof(ALACSpecificConfig))
{
/* FIXME: Cast-align warning when compiling on armhf. */
theConfig.frameLength = Swap32BtoN(((ALACSpecificConfig *)theActualCookie)->frameLength);
theConfig.frameLength = psf_get_be32 (theActualCookie, offsetof (ALACSpecificConfig, frameLength)) ;

if (theConfig.frameLength > ALAC_FRAME_LENGTH)
return fALAC_FrameLengthError ;

theConfig.compatibleVersion = ((ALACSpecificConfig *)theActualCookie)->compatibleVersion;
theConfig.bitDepth = ((ALACSpecificConfig *)theActualCookie)->bitDepth;
theConfig.pb = ((ALACSpecificConfig *)theActualCookie)->pb;
theConfig.mb = ((ALACSpecificConfig *)theActualCookie)->mb;
theConfig.kb = ((ALACSpecificConfig *)theActualCookie)->kb;
theConfig.numChannels = ((ALACSpecificConfig *)theActualCookie)->numChannels;
theConfig.maxRun = Swap16BtoN(((ALACSpecificConfig *)theActualCookie)->maxRun);
theConfig.maxFrameBytes = Swap32BtoN(((ALACSpecificConfig *)theActualCookie)->maxFrameBytes);
theConfig.avgBitRate = Swap32BtoN(((ALACSpecificConfig *)theActualCookie)->avgBitRate);
theConfig.sampleRate = Swap32BtoN(((ALACSpecificConfig *)theActualCookie)->sampleRate);
theConfig.compatibleVersion = theActualCookie [offsetof (ALACSpecificConfig, compatibleVersion)] ;
theConfig.bitDepth = theActualCookie [offsetof (ALACSpecificConfig, bitDepth)] ;
theConfig.pb = theActualCookie [offsetof (ALACSpecificConfig, pb)] ;
theConfig.mb = theActualCookie [offsetof (ALACSpecificConfig, mb)] ;
theConfig.kb = theActualCookie [offsetof (ALACSpecificConfig, kb)] ;
theConfig.numChannels = theActualCookie [offsetof (ALACSpecificConfig, numChannels)] ;
theConfig.maxRun = psf_get_be16 (theActualCookie, offsetof (ALACSpecificConfig, maxRun)) ;
theConfig.maxFrameBytes = psf_get_be32 (theActualCookie, offsetof (ALACSpecificConfig, maxFrameBytes)) ;
theConfig.avgBitRate = psf_get_be32 (theActualCookie, offsetof (ALACSpecificConfig, avgBitRate)) ;
theConfig.sampleRate = psf_get_be32 (theActualCookie, offsetof (ALACSpecificConfig, sampleRate)) ;

p->mConfig = theConfig;

Expand Down

0 comments on commit 34f1fe1

Please sign in to comment.