]> wimlib.net Git - wimlib/commitdiff
to_leXX() -> cpu_to_leXX(), leXX_to_cpu()
authorEric Biggers <ebiggers3@gmail.com>
Tue, 2 Oct 2012 16:56:46 +0000 (11:56 -0500)
committerEric Biggers <ebiggers3@gmail.com>
Tue, 2 Oct 2012 16:59:32 +0000 (11:59 -0500)
Split the macros to_leXX() into separate macros for converting *from* in-memory
little endian *to* the correct in-CPU value, and for converting *from* a correct
in-CPU value *to* in-memory little-endian.  In reality these macros are defined
the same, but it makes sense to distinguish the cases (as is done in other
projects such as the Linux kernel).

By the way, wimlib still has NOT actually been tested on big-endian systems.

src/comp.c
src/comp.h
src/decomp.h
src/endianness.h
src/header.c
src/io.h
src/lzx-comp.c
src/lzx-decomp.c
src/resource.c
src/security.c
src/symlink.c

index 506a07818aefe9feab77f4bca008924403a2f926..7837a4614629f8b33fcde74051f676b76e7d220a 100644 (file)
@@ -29,7 +29,7 @@
 
 static inline void flush_bits(struct output_bitstream *ostream)
 {
-       *(u16*)ostream->bit_output = to_le16(ostream->bitbuf);
+       *(u16*)ostream->bit_output = cpu_to_le16(ostream->bitbuf);
        ostream->bit_output = ostream->next_bit_output;
        ostream->next_bit_output = ostream->output;
        ostream->output += 2;
index c60fd886e3ea17d112c499cce015b13555971444..7b5f6c6bfd94e5f41f9e6da30f35a654f4671f85 100644 (file)
@@ -46,11 +46,11 @@ static inline int bitstream_put_byte(struct output_bitstream *ostream,
 }
 
 static inline int bitstream_put_two_bytes(struct output_bitstream *ostream,
-                                          u16 n)
+                                         u16 n)
 {
        if (ostream->num_bytes_remaining < 2)
                return 1;
-       *(u16*)ostream->output = to_le16(n);
+       *(u16*)ostream->output = cpu_to_le16(n);
        ostream->output += 2;
        ostream->num_bytes_remaining -= 2;
        return 0;
index c039582c79e8aa2228eeb0ace2ea322acc3659cb..72cf165764f57fa4e92082f209a9471b832315cb 100644 (file)
@@ -64,7 +64,7 @@ static inline int bitstream_ensure_bits(struct input_bitstream *istream,
 
                uint shift = sizeof(input_bitbuf_t) * 8 - 16 - 
                             istream->bitsleft;
-               istream->bitbuf |= (input_bitbuf_t)to_le16(
+               istream->bitbuf |= (input_bitbuf_t)le16_to_cpu(
                                        *(u16*)istream->data) << shift;
                istream->data += 2;
                istream->bitsleft += 16;
index e5f8009c08dbfb76f4e90738a3c66ea206ff1594..cba645bf30554359eab58152b2f9f14150c13b80 100644 (file)
@@ -5,14 +5,14 @@
 #include "config.h"
 #include <inttypes.h>
 
-#ifdef WORDS_BIGENDIAN
+/* Watch out for conflicts with ntfs-3g headers... */
 
 #ifndef bswap16
 static inline uint16_t bswap16(uint16_t n)
 {
        return (n << 8) | (n >> 8);
 }
-#endif
+#endif /* ifndef bswap16 */
 
 #ifndef bswap32
 static inline uint32_t bswap32(uint32_t n)
@@ -24,8 +24,7 @@ static inline uint32_t bswap32(uint32_t n)
                                                        (n >> 24);
 #endif
 }
-#endif
-
+#endif /* ifndef bswap32 */
 
 #ifndef bswap64
 static inline uint64_t bswap64(uint64_t n)
@@ -39,65 +38,49 @@ static inline uint64_t bswap64(uint64_t n)
                        ((n & 0xff000000000000) >> 40) | (n >> 56);
 #endif
 }
-#endif
+#endif /* ifndef bswap64 */
 
-/* Not in place */
-#define to_le16(n) bswap16(n)
-#define to_le32(n) bswap32(n)
-#define to_le64(n) bswap64(n)
 
 #ifndef _NTFS_ENDIANS_H
-#define le16_to_cpu(n) bswap16(n)
-#define le32_to_cpu(n) bswap32(n)
-#define le64_to_cpu(n) bswap64(n)
+#      ifdef WORDS_BIGENDIAN
+#              define le16_to_cpu(n) bswap16(n)
+#              define le32_to_cpu(n) bswap32(n)
+#              define le64_to_cpu(n) bswap64(n)
+#              define cpu_to_le16(n) bswap16(n)
+#              define cpu_to_le32(n) bswap32(n)
+#              define cpu_to_le64(n) bswap64(n)
+#      else
+#              define cpu_to_le16(n) (n)
+#              define cpu_to_le32(n) (n)
+#              define cpu_to_le64(n) (n)
+#              define le16_to_cpu(n) (n)
+#              define le32_to_cpu(n) (n)
+#              define le64_to_cpu(n) (n)
+#      endif
 #endif
 
-/* In place */
-#define TO_LE16(n) ((n) = to_le16(n))
-#define TO_LE32(n) ((n) = to_le32(n))
-#define TO_LE64(n) ((n) = to_le64(n))
-
-static inline void array_to_le16(uint16_t *p, uint64_t n)
+static inline void array_cpu_to_le32(uint32_t *p, uint64_t n)
 {
        while (n--)
-               *p++ = to_le16(*p);
+               *p++ = cpu_to_le32(*p);
 }
-static inline void array_to_le32(uint32_t *p, uint64_t n)
+
+static inline void array_le32_to_cpu(uint32_t *p, uint64_t n)
 {
        while (n--)
-               *p++ = to_le32(*p);
+               *p++ = le32_to_cpu(*p);
 }
-static inline void array_to_le64(uint64_t *p, uint64_t n)
+
+static inline void array_cpu_to_le64(uint64_t *p, uint64_t n)
 {
        while (n--)
-               *p++ = to_le64(*p);
+               *p++ = cpu_to_le64(*p);
 }
 
-#else
-
-/* Little endian. */
-
-/* Not in place */
-#define to_le16(n) (n)
-#define to_le32(n) (n)
-#define to_le64(n) (n)
-
-#ifndef _NTFS_ENDIANS_H
-#define le16_to_cpu(n) (n)
-#define le32_to_cpu(n) (n)
-#define le64_to_cpu(n) (n)
-#endif
-
-/* In place */
-#define TO_LE16(n)
-#define TO_LE32(n)
-#define TO_LE64(n)
-
-#define array_to_le16(p, n)
-#define array_to_le32(p, n)
-#define array_to_le64(p, n)
-
-#endif
-
+static inline void array_le64_to_cpu(uint64_t *p, uint64_t n)
+{
+       while (n--)
+               *p++ = le64_to_cpu(*p);
+}
 
 #endif /* _WIMLIB_ENDIANNESS_H */
index e7dd900904a93b0a883725799a85df1bd922b5d5..68ea052628b5a6a1fa7ceb25a8478ebbfcd2ac4d 100644 (file)
@@ -61,7 +61,7 @@ int read_header(FILE *fp, struct wim_header *hdr, int split_ok)
        if (bytes_read != sizeof(u32))
                goto err;
 
-       TO_LE32(hdr_size);
+       hdr_size = le32_to_cpu(hdr_size);
 
        /* Byte 12 */
 
index dc49f5c08e92887f422e3fce1134dc16d5c4317d..1a617e261d28bee11c6607e22ce278ceb641625b 100644 (file)
--- a/src/io.h
+++ b/src/io.h
@@ -29,7 +29,7 @@ static inline const u8 *get_u8(const u8 *p, u8 *res)
 
 static inline const u8 *get_u16(const u8 *p, u16 *res)
 {
-       *res = to_le16(*(u16*)p);
+       *res = le16_to_cpu(*(u16*)p);
        return p + 2;
 }
 
@@ -37,21 +37,21 @@ static inline const u8 *get_u16(const u8 *p, u16 *res)
 
 static inline const u8 *get_u32(const u8 *p, u32 *res)
 {
-       *res = to_le32(*(u32*)p);
+       *res = le32_to_cpu(*(u32*)p);
        return p + 4;
 }
 
 
 static inline const u8 *get_u56(const u8 *p, u64 *res)
 {
-       *res = to_le64(*(u64*)p) & 0x00ffffffffffffff;
+       *res = le64_to_cpu(*(u64*)p) & 0x00ffffffffffffff;
        return p + 7;
 }
 
 
 static inline const u8 *get_u64(const u8 *p, u64 *res)
 {
-       *res = to_le64(*(u64*)p);
+       *res = le64_to_cpu(*(u64*)p);
        return p + 8;
 }
 
@@ -69,13 +69,13 @@ static inline u8 *put_u8(u8 *res, u8 val)
 
 static inline u8 *put_u16(u8 *res, u16 val)
 {
-       *(uint16_t*)res = to_le16(val);
+       *(uint16_t*)res = cpu_to_le16(val);
        return res + 2;
 }
 
 static inline u8 *put_u32(u8 *res, u32 val)
 {
-       *(uint32_t*)res = to_le32(val);
+       *(uint32_t*)res = cpu_to_le32(val);
        return res + 4;
 }
 
@@ -98,7 +98,7 @@ static inline u8 *put_u56(u8 *res, u64 val)
 
 static inline u8 *put_u64(u8 *res, u64 val)
 {
-       *(u64*)res = to_le64(val);
+       *(u64*)res = cpu_to_le64(val);
        return res + 8;
 }
 
index 8267a2eaece40d28187adee2c114a953f2ebe825..328627de3f842f0dc0471294fa8cfcb42101931a 100644 (file)
@@ -581,7 +581,7 @@ static void do_call_insn_preprocessing(u8 uncompressed_data[],
                        i++;
                        continue;
                }
-               rel_offset = to_le32(*(int32_t*)(uncompressed_data + i + 1));
+               rel_offset = le32_to_cpu(*(int32_t*)(uncompressed_data + i + 1));
 
                if (rel_offset >= -i && rel_offset < file_size) {
                        if (rel_offset < file_size - i) {
@@ -591,7 +591,7 @@ static void do_call_insn_preprocessing(u8 uncompressed_data[],
                                /* "compensating translation" */
                                abs_offset = rel_offset - file_size;
                        }
-                       *(int32_t*)(uncompressed_data + i + 1) = to_le32(abs_offset);
+                       *(int32_t*)(uncompressed_data + i + 1) = cpu_to_le32(abs_offset);
                }
                i += 5;
        }
index d598d941c1d49bb0c2fef2c6c4b44ba26de4636f..5fa38049e2afede25267d3a69de2cdba45b0acd8 100644 (file)
@@ -342,7 +342,7 @@ static int lzx_read_block_header(struct input_bitstream *istream,
                ret = bitstream_read_bits(istream, 16, &block_size);
                if (ret != 0)
                        return ret;
-               block_size = to_le16(block_size);
+               block_size = le16_to_cpu(block_size);
        }
 
        switch (block_type) {
@@ -450,10 +450,9 @@ static int lzx_read_block_header(struct input_bitstream *istream,
                ret = bitstream_read_bytes(istream, sizeof(R), R);
                if (ret != 0)
                        return ret;
-               array_to_le32(R, ARRAY_LEN(R));
-               queue->R0 = R[0];
-               queue->R1 = R[1];
-               queue->R2 = R[2];
+               queue->R0 = le32_to_cpu(R[0]);
+               queue->R1 = le32_to_cpu(R[1]);
+               queue->R2 = le32_to_cpu(R[2]);
                break;
        default:
                LZX_DEBUG("Found invalid block.");
@@ -669,7 +668,7 @@ static void undo_call_insn_preprocessing(u8 uncompressed_data[],
                        i++;
                        continue;
                }
-               abs_offset = to_le32(*(int32_t*)(uncompressed_data + i + 1));
+               abs_offset = le32_to_cpu(*(int32_t*)(uncompressed_data + i + 1));
 
                if (abs_offset >= -i && abs_offset < file_size) {
                        if (abs_offset >= 0) {
@@ -680,7 +679,7 @@ static void undo_call_insn_preprocessing(u8 uncompressed_data[],
                                rel_offset = abs_offset + file_size;
                        }
                        *(int32_t*)(uncompressed_data + i + 1) = 
-                                               to_le32(rel_offset);
+                                               cpu_to_le32(rel_offset);
                }
                i += 5;
        }
index f7a7916e5f0cc0ed94bcae2b30673b500f80db4b..cd098f92ad5e6edd5990aa68971e5351b1007237 100644 (file)
@@ -191,11 +191,11 @@ static int read_compressed_resource(FILE *fp, u64 resource_compressed_size,
        if (chunk_entry_size == 4) {
                u32 *entries = (u32*)chunk_tab_buf;
                while (num_needed_chunk_entries--)
-                       *chunk_tab_p++ = to_le32(*entries++);
+                       *chunk_tab_p++ = le32_to_cpu(*entries++);
        } else {
                u64 *entries = (u64*)chunk_tab_buf;
                while (num_needed_chunk_entries--)
-                       *chunk_tab_p++ = to_le64(*entries++);
+                       *chunk_tab_p++ = le64_to_cpu(*entries++);
        }
 
        /* Done with the chunk table now.  We must now seek to the first chunk
@@ -693,11 +693,11 @@ finish_wim_resource_chunk_tab(struct chunk_table *chunk_tab,
        }
 
        if (chunk_tab->bytes_per_chunk_entry == 8) {
-               array_to_le64(chunk_tab->offsets, chunk_tab->num_chunks);
+               array_cpu_to_le64(chunk_tab->offsets, chunk_tab->num_chunks);
        } else {
                for (u64 i = 0; i < chunk_tab->num_chunks; i++)
                        ((u32*)chunk_tab->offsets)[i] =
-                               to_le32(chunk_tab->offsets[i]);
+                               cpu_to_le32(chunk_tab->offsets[i]);
        }
        bytes_written = fwrite((u8*)chunk_tab->offsets +
                                        chunk_tab->bytes_per_chunk_entry,
index ca75ac0bf442ffb490ad8c9912a131e7fcf088d2..63ab5ba090984a0cc8f0656bc7d27d44cdce1972 100644 (file)
@@ -46,7 +46,7 @@ static void empty_sacl_fixup(char *descr, u64 *size_p)
                SecurityDescriptor *sd = (SecurityDescriptor*)descr;
                u32 sacl_offset = le32_to_cpu(sd->sacl_offset);
                if (sacl_offset == *size_p - sizeof(ACL)) {
-                       sd->sacl_offset = to_le32(0);
+                       sd->sacl_offset = cpu_to_le32(0);
                        *size_p -= sizeof(ACL);
                }
        }
@@ -135,7 +135,7 @@ int read_security_data(const u8 metadata_resource[], u64 metadata_resource_len,
 
        /* Copy the sizes array in from the file data. */
        p = get_bytes(p, sizes_size, sd->sizes);
-       array_to_le64(sd->sizes, sd->num_entries);
+       array_le64_to_cpu(sd->sizes, sd->num_entries);
 
        /* Allocate the array of pointers to descriptors, and read them in. */
        sd->descriptors = CALLOC(sd->num_entries, sizeof(u8*));
@@ -216,8 +216,8 @@ static void print_acl(const u8 *p, const char *type)
 {
        const ACL *acl = (const ACL*)p;
        u8 revision = acl->revision;
-       u16 acl_size = to_le16(acl->acl_size);
-       u16 ace_count = to_le16(acl->ace_count);
+       u16 acl_size = le16_to_cpu(acl->acl_size);
+       u16 ace_count = le16_to_cpu(acl->ace_count);
        printf("    [%s ACL]\n", type);
        printf("    Revision = %u\n", revision);
        printf("    ACL Size = %u\n", acl_size);
@@ -231,8 +231,8 @@ static void print_acl(const u8 *p, const char *type)
                printf("        ACE flags = 0x%x\n", hdr->flags);
                printf("        ACE size  = %u\n", hdr->size);
                const AccessAllowedACE *aaa = (const AccessAllowedACE*)hdr;
-               printf("        ACE mask = %x\n", to_le32(aaa->mask));
-               printf("        SID start = %u\n", to_le32(aaa->sid_start));
+               printf("        ACE mask = %x\n", le32_to_cpu(aaa->mask));
+               printf("        SID start = %u\n", le32_to_cpu(aaa->sid_start));
                p += hdr->size;
        }
        putchar('\n');
@@ -245,10 +245,12 @@ static void print_sid(const u8 *p, const char *type)
        printf("    Revision = %u\n", sid->revision);
        printf("    Subauthority count = %u\n", sid->sub_authority_count);
        printf("    Identifier authority = ");
-       print_byte_field(sid->identifier_authority, sizeof(sid->identifier_authority));
+       print_byte_field(sid->identifier_authority,
+                        sizeof(sid->identifier_authority));
        putchar('\n');
        for (uint i = 0; i < sid->sub_authority_count; i++)
-               printf("    Subauthority %u = %u\n", i, to_le32(sid->sub_authority[i]));
+               printf("    Subauthority %u = %u\n",
+                      i, le32_to_cpu(sid->sub_authority[i]));
        putchar('\n');
 }
 
@@ -256,11 +258,11 @@ static void print_security_descriptor(const u8 *p, u64 size)
 {
        const SecurityDescriptor *sd = (const SecurityDescriptor*)p;
        u8 revision      = sd->revision;
-       u16 control      = to_le16(sd->security_descriptor_control);
-       u32 owner_offset = to_le32(sd->owner_offset);
-       u32 group_offset = to_le32(sd->group_offset);
-       u32 sacl_offset  = to_le32(sd->sacl_offset);
-       u32 dacl_offset  = to_le32(sd->dacl_offset);
+       u16 control      = le16_to_cpu(sd->security_descriptor_control);
+       u32 owner_offset = le32_to_cpu(sd->owner_offset);
+       u32 group_offset = le32_to_cpu(sd->group_offset);
+       u32 sacl_offset  = le32_to_cpu(sd->sacl_offset);
+       u32 dacl_offset  = le32_to_cpu(sd->dacl_offset);
        printf("Revision = %u\n", revision);
        printf("Security Descriptor Control = %#x\n", control);
        printf("Owner offset = %u\n", owner_offset);
index 59072ac0dc56da425c71ba50e66f7977fcb863b3..5b8fe6f3ae7460ae437144f648edb54ceec067f7 100644 (file)
@@ -133,8 +133,8 @@ void *make_symlink_reparse_data_buf(const char *symlink_target, size_t *len_ret)
                return NULL;
        /*DEBUG("utf16_len = %zu", utf16_len);*/
        for (size_t i = 0; i < utf16_len / 2; i++)
-               if (((u16*)name_utf16)[i] == to_le16('/'))
-                       ((u16*)name_utf16)[i] = to_le16('\\');
+               if (((u16*)name_utf16)[i] == cpu_to_le16('/'))
+                       ((u16*)name_utf16)[i] = cpu_to_le16('\\');
        size_t len = 12 + utf16_len * 2 + 4;
        void *buf = MALLOC(len);
        if (!buf)