1 /*
2  * Copyright (c) 2012 Dave Vasilevsky <dave@vasilevsky.ca>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 #include "swap.h"
26 
27 #define SWAP(BITS) \
28 	void sqfs_swapin##BITS(uint##BITS##_t *v) { \
29 		int i; \
30 		uint8_t *c = (uint8_t*)v; \
31 		uint##BITS##_t r = 0; \
32 		for (i = sizeof(*v) - 1; i >= 0; --i) { \
33 			r <<= 8; \
34 			r += c[i]; \
35 		} \
36 		*v = r; \
37 	}
38 
39 SWAP(16)
40 SWAP(32)
41 SWAP(64)
42 #undef SWAP
43 
sqfs_swapin16_internal(__le16 * v)44 void sqfs_swapin16_internal(__le16 *v) { sqfs_swapin16((uint16_t*)v); }
sqfs_swapin32_internal(__le32 * v)45 void sqfs_swapin32_internal(__le32 *v) { sqfs_swapin32((uint32_t*)v); }
sqfs_swapin64_internal(__le64 * v)46 void sqfs_swapin64_internal(__le64 *v) { sqfs_swapin64((uint64_t*)v); }
47 
sqfs_swap16(uint16_t * n)48 void sqfs_swap16(uint16_t *n) {
49 	*n = (*n >> 8) + (*n << 8);
50 }
51 
52 #include "squashfs_fs.h"
53 #include "swap.c.inc"
54