@rjd324
We cannot be 100% sure what the original purpose was as the code is illegal. Not just because of the casting/++ abuse but also because it guarantees misaligned data accesses which may be a problem depending on the target CPU.
Anyway, apparently the intention is to grab and store chunks of data of different types which are linearly tightly packed into some structure.
And because of lazyness the original coder tried to use the same UBYTE pointers for all different access variants.
It's of course dirty crap. However the old compiler obviously allowed to increase the pointers depending on the type they were casted to, so if it was casted to a temporary UWORD* the ++ operator would increase the original UBYTE* by sizeof(UWORD).
Your
val=*(UWORD*)(chunkbuf++);
has the same semantics as
val=*(UWORD*)(chunkbuf);
++chunkbuf;
and defeats that purpose as it grabs a UWORD but then increases chunkbuf by sizeof(UBYTE) instead of sizeof(UWORD). So the next access would grab parts of the previously read value...
I strongly doubt that this was the original intention.
@kas1e
Of course my union solution doesn't fix potentially misaligned data access.