88 * that.
99 */
1010#include <unistd.h>
11- #include <byteswap.h>
1211#include <errno.h>
1312#include <string.h>
1413#include <arpa/inet.h>
15- #include <asm/types .h>
14+ #include <endian .h>
1615#include "cow.h"
1716#include "cow_sys.h"
1817
@@ -214,8 +213,8 @@ int write_cow_header(char *cow_file, int fd, char *backing_file,
214213 "header\n" );
215214 goto out ;
216215 }
217- header -> magic = htonl (COW_MAGIC );
218- header -> version = htonl (COW_VERSION );
216+ header -> magic = htobe32 (COW_MAGIC );
217+ header -> version = htobe32 (COW_VERSION );
219218
220219 err = - EINVAL ;
221220 if (strlen (backing_file ) > sizeof (header -> backing_file ) - 1 ) {
@@ -246,10 +245,10 @@ int write_cow_header(char *cow_file, int fd, char *backing_file,
246245 goto out_free ;
247246 }
248247
249- header -> mtime = htonl (modtime );
250- header -> size = htonll (* size );
251- header -> sectorsize = htonl (sectorsize );
252- header -> alignment = htonl (alignment );
248+ header -> mtime = htobe32 (modtime );
249+ header -> size = htobe64 (* size );
250+ header -> sectorsize = htobe32 (sectorsize );
251+ header -> alignment = htobe32 (alignment );
253252 header -> cow_format = COW_BITMAP ;
254253
255254 err = cow_write_file (fd , header , sizeof (* header ));
@@ -301,8 +300,8 @@ int read_cow_header(int (*reader)(__u64, char *, int, void *), void *arg,
301300 magic = header -> v1 .magic ;
302301 if (magic == COW_MAGIC )
303302 version = header -> v1 .version ;
304- else if (magic == ntohl (COW_MAGIC ))
305- version = ntohl (header -> v1 .version );
303+ else if (magic == be32toh (COW_MAGIC ))
304+ version = be32toh (header -> v1 .version );
306305 /* No error printed because the non-COW case comes through here */
307306 else goto out ;
308307
@@ -327,9 +326,9 @@ int read_cow_header(int (*reader)(__u64, char *, int, void *), void *arg,
327326 "header\n" );
328327 goto out ;
329328 }
330- * mtime_out = ntohl (header -> v2 .mtime );
331- * size_out = ntohll (header -> v2 .size );
332- * sectorsize_out = ntohl (header -> v2 .sectorsize );
329+ * mtime_out = be32toh (header -> v2 .mtime );
330+ * size_out = be64toh (header -> v2 .size );
331+ * sectorsize_out = be32toh (header -> v2 .sectorsize );
333332 * bitmap_offset_out = sizeof (header -> v2 );
334333 * align_out = * sectorsize_out ;
335334 file = header -> v2 .backing_file ;
@@ -341,10 +340,10 @@ int read_cow_header(int (*reader)(__u64, char *, int, void *), void *arg,
341340 "header\n" );
342341 goto out ;
343342 }
344- * mtime_out = ntohl (header -> v3 .mtime );
345- * size_out = ntohll (header -> v3 .size );
346- * sectorsize_out = ntohl (header -> v3 .sectorsize );
347- * align_out = ntohl (header -> v3 .alignment );
343+ * mtime_out = be32toh (header -> v3 .mtime );
344+ * size_out = be64toh (header -> v3 .size );
345+ * sectorsize_out = be32toh (header -> v3 .sectorsize );
346+ * align_out = be32toh (header -> v3 .alignment );
348347 if (* align_out == 0 ) {
349348 cow_printf ("read_cow_header - invalid COW header, "
350349 "align == 0\n" );
@@ -366,16 +365,16 @@ int read_cow_header(int (*reader)(__u64, char *, int, void *), void *arg,
366365 * this was used until Dec2005 - 64bits are needed to represent
367366 * 2038+. I.e. we can safely do this truncating cast.
368367 *
369- * Additionally, we must use ntohl () instead of ntohll (), since
368+ * Additionally, we must use be32toh () instead of be64toh (), since
370369 * the program used to use the former (tested - I got mtime
371370 * mismatch "0 vs whatever").
372371 *
373372 * Ever heard about bug-to-bug-compatibility ? ;-) */
374- * mtime_out = (time32_t ) ntohl (header -> v3_b .mtime );
373+ * mtime_out = (time32_t ) be32toh (header -> v3_b .mtime );
375374
376- * size_out = ntohll (header -> v3_b .size );
377- * sectorsize_out = ntohl (header -> v3_b .sectorsize );
378- * align_out = ntohl (header -> v3_b .alignment );
375+ * size_out = be64toh (header -> v3_b .size );
376+ * sectorsize_out = be32toh (header -> v3_b .sectorsize );
377+ * align_out = be32toh (header -> v3_b .alignment );
379378 if (* align_out == 0 ) {
380379 cow_printf ("read_cow_header - invalid COW header, "
381380 "align == 0\n" );
0 commit comments