Whamcloud - gitweb
libext2fs: Fix namespace leakage of crc16 functions
authorTheodore Ts'o <tytso@mit.edu>
Mon, 25 Aug 2008 02:44:33 +0000 (22:44 -0400)
committerTheodore Ts'o <tytso@mit.edu>
Mon, 25 Aug 2008 02:44:33 +0000 (22:44 -0400)
Rename crc16 to ext2fs_crc16, and make crc16_table static, since
there's not reason it should be exposed at all.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
lib/ext2fs/crc16.c
lib/ext2fs/crc16.h
lib/ext2fs/csum.c
lib/ext2fs/tst_csum.c

index 1b2b8a1..86091a4 100644 (file)
@@ -8,7 +8,7 @@
 #include "crc16.h"
 
 /** CRC table for the CRC-16. The poly is 0x8005 (x16 + x15 + x2 + 1) */
-__u16 const crc16_table[256] = {
+static __u16 const crc16_table[256] = {
        0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241,
        0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440,
        0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40,
@@ -51,11 +51,18 @@ __u16 const crc16_table[256] = {
  * @param len     number of bytes in the buffer
  * @return        the updated CRC value
  */
-crc16_t crc16(crc16_t crc, const void *buffer, unsigned int len)
+crc16_t ext2fs_crc16(crc16_t crc, const void *buffer, unsigned int len)
 {
        const unsigned char *cp = buffer;
 
        while (len--)
-               crc = crc16_byte(crc, *cp++);
+               /*
+                * for an unknown reason, PPC treats __u16 as signed
+                * and keeps doing sign extension on the value.
+                * Instead, use only the low 16 bits of an unsigned
+                * int for holding the CRC value to avoid this.
+                */
+               crc = (((crc >> 8) & 0xffU) ^
+                      crc16_table[(crc ^ *cp++) & 0xffU]) & 0x0000ffffU;
        return crc;
 }
index ab3f094..e3d8b4a 100644 (file)
 
 #include <ext2fs/ext2_types.h>
 
-extern __u16 const crc16_table[256];
-
-#ifdef WORDS_BIGENDIAN
 /* for an unknown reason, PPC treats __u16 as signed and keeps doing sign
  * extension on the value.  Instead, use only the low 16 bits of an
  * unsigned int for holding the CRC value to avoid this.
  */
-typedef unsigned crc16_t;
-
-static inline crc16_t crc16_byte(crc16_t crc, const unsigned char data)
-{
-       return (((crc >> 8) & 0xffU) ^ crc16_table[(crc ^ data) & 0xffU]) &
-               0x0000ffffU;
-}
-#else
-typedef __u16 crc16_t;
-
-static inline crc16_t crc16_byte(crc16_t crc, const unsigned char data)
-{
-       return (crc >> 8) ^ crc16_table[(crc ^ data) & 0xff];
-}
-#endif
+typedef unsigned int crc16_t;
 
-extern crc16_t crc16(crc16_t crc, const void *buffer, unsigned int len);
+extern crc16_t ext2fs_crc16(crc16_t crc, const void *buffer, unsigned int len);
 
 #endif /* __CRC16_H */
index 6a49d8f..257d713 100644 (file)
@@ -43,14 +43,15 @@ STATIC __u16 ext2fs_group_desc_csum(ext2_filsys fs, dgrp_t group)
 
                group = ext2fs_swab32(group);
 #endif
-               crc = crc16(~0, fs->super->s_uuid, sizeof(fs->super->s_uuid));
-               crc = crc16(crc, &group, sizeof(group));
-               crc = crc16(crc, desc, offset);
+               crc = ext2fs_crc16(~0, fs->super->s_uuid,
+                                  sizeof(fs->super->s_uuid));
+               crc = ext2fs_crc16(crc, &group, sizeof(group));
+               crc = ext2fs_crc16(crc, desc, offset);
                offset += sizeof(desc->bg_checksum); /* skip checksum */
                assert(offset == sizeof(*desc));
                /* for checksum of struct ext4_group_desc do the rest...*/
                if (offset < fs->super->s_desc_size) {
-                       crc = crc16(crc, (char *)desc + offset,
+                       crc = ext2fs_crc16(crc, (char *)desc + offset,
                                    fs->super->s_desc_size - offset);
                }
        }
index 17ea105..7a91b19 100644 (file)
@@ -36,9 +36,10 @@ void print_csum(const char *msg, ext2_filsys fs, dgrp_t group)
        swabgroup = group;
 #endif
 
-       crc1 = crc16(~0, sb->s_uuid, sizeof(fs->super->s_uuid));
-       crc2 = crc16(crc1, &swabgroup, sizeof(swabgroup));
-       crc3 = crc16(crc2, desc, offsetof(struct ext2_group_desc, bg_checksum));
+       crc1 = ext2fs_crc16(~0, sb->s_uuid, sizeof(fs->super->s_uuid));
+       crc2 = ext2fs_crc16(crc1, &swabgroup, sizeof(swabgroup));
+       crc3 = ext2fs_crc16(crc2, desc,
+                           offsetof(struct ext2_group_desc, bg_checksum));
        printf("%s: UUID %016Lx%016Lx(%04x), grp %u(%04x): %04x=%04x\n",
               msg, *(long long *)&sb->s_uuid, *(long long *)&sb->s_uuid[8],
               crc1, group, crc2, crc3, ext2fs_group_desc_csum(fs, group));