Patrick Williams | b48b7b4 | 2016-08-17 15:04:38 -0500 | [diff] [blame^] | 1 | From e58cb210a7c15352040a411d11a8383eac0defda Mon Sep 17 00:00:00 2001 |
| 2 | From: Jianchuan Wang <jianchuan.wang@windriver.com> |
| 3 | Date: Tue, 30 Sep 2014 12:16:17 +0800 |
| 4 | Subject: [PATCH] xfsprogs: generate crctable which is moved into runtime from |
| 5 | compile |
| 6 | |
| 7 | After upgraded, There is a compile error except x86, |
| 8 | Because crc32.c need two arraies crc32table_le and crc32ctable_le from crc32table.h, |
| 9 | which are generated by gen_crc32table.c relative to different platforms. |
| 10 | For this, move the function implementation from gen_crc32table.c to crc.c |
| 11 | |
| 12 | Upstream-Status: Pending |
| 13 | |
| 14 | Signed-off-by: Jianchuan Wang <jianchuan.wang@windriver.com> |
| 15 | --- |
| 16 | libxfs/Makefile | 23 ++---------------- |
| 17 | libxfs/crc32.c | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- |
| 18 | 2 files changed, 75 insertions(+), 23 deletions(-) |
| 19 | |
| 20 | diff --git a/libxfs/Makefile b/libxfs/Makefile |
| 21 | index ae15a5d..7670159 100644 |
| 22 | --- a/libxfs/Makefile |
| 23 | +++ b/libxfs/Makefile |
| 24 | @@ -10,7 +10,7 @@ LT_CURRENT = 0 |
| 25 | LT_REVISION = 0 |
| 26 | LT_AGE = 0 |
| 27 | |
| 28 | -HFILES = xfs.h init.h xfs_dir2_priv.h crc32defs.h crc32table.h |
| 29 | +HFILES = xfs.h init.h xfs_dir2_priv.h crc32defs.h |
| 30 | CFILES = cache.c \ |
| 31 | crc32.c \ |
| 32 | init.c kmem.c logitem.c radix-tree.c rdwr.c trans.c util.c \ |
| 33 | @@ -43,7 +43,6 @@ CFILES = cache.c \ |
| 34 | CFILES += $(PKG_PLATFORM).c |
| 35 | PCFILES = darwin.c freebsd.c irix.c linux.c |
| 36 | LSRCFILES = $(shell echo $(PCFILES) | sed -e "s/$(PKG_PLATFORM).c//g") |
| 37 | -LSRCFILES += gen_crc32table.c |
| 38 | |
| 39 | # |
| 40 | # Tracing flags: |
| 41 | @@ -61,25 +60,7 @@ LTLIBS = $(LIBPTHREAD) $(LIBRT) |
| 42 | # don't try linking xfs_repair with a debug libxfs. |
| 43 | DEBUG = -DNDEBUG |
| 44 | |
| 45 | -LDIRT = gen_crc32table crc32table.h crc32selftest |
| 46 | - |
| 47 | -default: crc32selftest ltdepend $(LTLIBRARY) |
| 48 | - |
| 49 | -crc32table.h: gen_crc32table.c |
| 50 | - @echo " [CC] gen_crc32table" |
| 51 | - $(Q) $(CC) $(CFLAGS) -o gen_crc32table $< |
| 52 | - @echo " [GENERATE] $@" |
| 53 | - $(Q) ./gen_crc32table > crc32table.h |
| 54 | - |
| 55 | -# The selftest binary will return an error if it fails. This is made a |
| 56 | -# dependency of the build process so that we refuse to build the tools on broken |
| 57 | -# systems/architectures. Hence we make sure that xfsprogs will never use a |
| 58 | -# busted CRC calculation at build time and hence avoid putting bad CRCs down on |
| 59 | -# disk. |
| 60 | -crc32selftest: gen_crc32table.c crc32table.h crc32.c |
| 61 | - @echo " [TEST] CRC32" |
| 62 | - $(Q) $(CC) $(CFLAGS) -D CRC32_SELFTEST=1 crc32.c -o $@ |
| 63 | - $(Q) ./$@ |
| 64 | +default: ltdepend $(LTLIBRARY) |
| 65 | |
| 66 | include $(BUILDRULES) |
| 67 | |
| 68 | diff --git a/libxfs/crc32.c b/libxfs/crc32.c |
| 69 | index 0f847d2..be5fbc3 100644 |
| 70 | --- a/libxfs/crc32.c |
| 71 | +++ b/libxfs/crc32.c |
| 72 | @@ -55,8 +55,6 @@ typedef __u32 u64; |
| 73 | # define tobe(x) (x) |
| 74 | #endif |
| 75 | |
| 76 | -#include "crc32table.h" |
| 77 | - |
| 78 | #if CRC_LE_BITS > 8 || CRC_BE_BITS > 8 |
| 79 | |
| 80 | /* implements slicing-by-4 or slicing-by-8 algorithm */ |
| 81 | @@ -183,13 +181,86 @@ u32 __pure crc32c_le(u32 crc, unsigned char const *p, size_t len) |
| 82 | return crc32_le_generic(crc, p, len, NULL, CRC32C_POLY_LE); |
| 83 | } |
| 84 | #else |
| 85 | + |
| 86 | +#include <stdio.h> |
| 87 | +#include "crc32defs.h" |
| 88 | +#include <inttypes.h> |
| 89 | + |
| 90 | +#define ENTRIES_PER_LINE 4 |
| 91 | + |
| 92 | +#if CRC_LE_BITS > 8 |
| 93 | +# define LE_TABLE_ROWS (CRC_LE_BITS/8) |
| 94 | +# define LE_TABLE_SIZE 256 |
| 95 | +#else |
| 96 | +# define LE_TABLE_ROWS 1 |
| 97 | +# define LE_TABLE_SIZE (1 << CRC_LE_BITS) |
| 98 | +#endif |
| 99 | + |
| 100 | +#if CRC_BE_BITS > 8 |
| 101 | +# define BE_TABLE_ROWS (CRC_BE_BITS/8) |
| 102 | +# define BE_TABLE_SIZE 256 |
| 103 | +#else |
| 104 | +# define BE_TABLE_ROWS 1 |
| 105 | +# define BE_TABLE_SIZE (1 << CRC_BE_BITS) |
| 106 | +#endif |
| 107 | + |
| 108 | +static uint32_t crc32table_le[LE_TABLE_ROWS][256]; |
| 109 | +static uint32_t crc32ctable_le[LE_TABLE_ROWS][256]; |
| 110 | + |
| 111 | +static uint32_t crc32table_le_init = 0; |
| 112 | +static uint32_t crc32ctable_le_init = 0; |
| 113 | + |
| 114 | +/* |
| 115 | + * big endian ordered CRC not used by XFS. |
| 116 | +static uint32_t crc32table_be[BE_TABLE_ROWS][256]; |
| 117 | + */ |
| 118 | + |
| 119 | +/** |
| 120 | + * crc32init_le() - allocate and initialize LE table data |
| 121 | + * |
| 122 | + * crc is the crc of the byte i; other entries are filled in based on the |
| 123 | + * fact that crctable[i^j] = crctable[i] ^ crctable[j]. |
| 124 | + * |
| 125 | + */ |
| 126 | +static void crc32init_le_generic(const uint32_t polynomial, |
| 127 | + uint32_t (*tab)[256]) |
| 128 | +{ |
| 129 | + unsigned i, j; |
| 130 | + uint32_t crc = 1; |
| 131 | + |
| 132 | + tab[0][0] = 0; |
| 133 | + |
| 134 | + for (i = LE_TABLE_SIZE >> 1; i; i >>= 1) { |
| 135 | + crc = (crc >> 1) ^ ((crc & 1) ? polynomial : 0); |
| 136 | + for (j = 0; j < LE_TABLE_SIZE; j += 2 * i) |
| 137 | + tab[0][i + j] = crc ^ tab[0][j]; |
| 138 | + } |
| 139 | + for (i = 0; i < LE_TABLE_SIZE; i++) { |
| 140 | + crc = tab[0][i]; |
| 141 | + for (j = 1; j < LE_TABLE_ROWS; j++) { |
| 142 | + crc = tab[0][crc & 0xff] ^ (crc >> 8); |
| 143 | + tab[j][i] = crc; |
| 144 | + } |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | u32 __pure crc32_le(u32 crc, unsigned char const *p, size_t len) |
| 149 | { |
| 150 | + if (crc32table_le_init == 0) { |
| 151 | + crc32init_le_generic(CRCPOLY_LE, crc32table_le); |
| 152 | + crc32table_le_init == 1; |
| 153 | + } |
| 154 | + |
| 155 | return crc32_le_generic(crc, p, len, |
| 156 | (const u32 (*)[256])crc32table_le, CRCPOLY_LE); |
| 157 | } |
| 158 | u32 __pure crc32c_le(u32 crc, unsigned char const *p, size_t len) |
| 159 | { |
| 160 | + if (crc32ctable_le_init == 0) { |
| 161 | + crc32init_le_generic(CRC32C_POLY_LE, crc32ctable_le); |
| 162 | + crc32ctable_le_init == 1; |
| 163 | + } |
| 164 | + |
| 165 | return crc32_le_generic(crc, p, len, |
| 166 | (const u32 (*)[256])crc32ctable_le, CRC32C_POLY_LE); |
| 167 | } |
| 168 | -- |
| 169 | 1.9.1 |
| 170 | |