]> code.ossystems Code Review - openembedded-core.git/blob
8e0f40642affcb7d076e0c9697cb6ef662643ff5
[openembedded-core.git] /
1 From dae29a98c066bc67bb5ba12219d5fd68a8675514 Mon Sep 17 00:00:00 2001
2 From: Hongxu Jia <hongxu.jia@windriver.com>
3 Date: Fri, 26 Apr 2013 20:44:10 +0800
4 Subject: [PATCH] packlib.c: support dictionary byte-order dependent
5
6 The previous dict files are NOT byte-order independent, in fact they are
7 probably ARCHITECTURE SPECIFIC.
8 Create the dict files in big endian, and convert to host endian while
9 load them. This could fix the endian issue on multiple platform.
10
11 Signed-off-by: Hongxu Jia <hongxu.jia@windriver.com>
12 Upstream-Status: Pending
13
14 We can't use the endian.h, htobe* and be*toh functions because they are
15 not available on older versions of glibc, such as that found in RHEL 
16 5.9.
17
18 Change to checking endian and directly calling bswap_* as defined in
19 byteswap.h.
20
21 Signed-off-by: Mark Hatle <mark.hatle@windriver.com>
22
23 ---
24  lib/packlib.c |  208 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--
25  1 file changed, 204 insertions(+), 4 deletions(-)
26
27 Index: cracklib-2.8.22/lib/packlib.c
28 ===================================================================
29 --- cracklib-2.8.22.orig/lib/packlib.c
30 +++ cracklib-2.8.22/lib/packlib.c
31 @@ -16,6 +16,12 @@
32  #ifdef HAVE_STDINT_H
33  #include <stdint.h>
34  #endif
35 +
36 +#ifndef _BSD_SOURCE
37 +#define _BSD_SOURCE             /* See feature_test_macros(7) */
38 +#endif
39 +#include <endian.h>
40 +#include <byteswap.h>
41  #include "packer.h"
42  
43  static const char vers_id[] = "packlib.c : v2.3p2 Alec Muffett 18 May 1993";
44 @@ -45,6 +51,182 @@ typedef struct
45      char data_get[NUMWORDS][MAXWORDLEN];
46  } PWDICT64;
47  
48 +enum{
49 +    en_is32,
50 +    en_is64
51 +};
52 +
53 +static int
54 +IheaderHostToBigEndian(char *pHeader, int nBitType)
55 +{
56 +    if (nBitType == en_is64 && __BYTE_ORDER == __LITTLE_ENDIAN)
57 +    {
58 +        struct pi_header64 *pHeader64 = (struct pi_header64*)pHeader;
59 +
60 +        pHeader64->pih_magic = bswap_64(pHeader64->pih_magic);
61 +        pHeader64->pih_numwords = bswap_64(pHeader64->pih_numwords);
62 +        pHeader64->pih_blocklen = bswap_16(pHeader64->pih_blocklen);
63 +        pHeader64->pih_pad = bswap_16(pHeader64->pih_pad);
64 +
65 +#if DEBUG
66 +        printf("Header64: magic %x, numwords %x, blocklen %x, pad %x\n",
67 +          pHeader64->pih_magic, pHeader64->pih_numwords,
68 +          pHeader64->pih_blocklen, pHeader64->pih_pad);
69 +#endif
70 +    }
71 +    else if (nBitType == en_is32 && __BYTE_ORDER == __LITTLE_ENDIAN)
72 +    {
73 +        struct pi_header *pHeader32 = (struct pi_header*)pHeader;
74 +
75 +        pHeader32->pih_magic = bswap_32(pHeader32->pih_magic);
76 +        pHeader32->pih_numwords = bswap_32(pHeader32->pih_numwords);
77 +        pHeader32->pih_blocklen = bswap_16(pHeader32->pih_blocklen);
78 +        pHeader32->pih_pad = bswap_16(pHeader32->pih_pad);
79 +
80 +#if DEBUG
81 +        printf("Header32: magic %x, numwords %x, blocklen %x, pad %x\n",
82 +          pHeader32->pih_magic, pHeader32->pih_numwords,
83 +          pHeader32->pih_blocklen, pHeader32->pih_pad);
84 +#endif
85 +    }
86 +    else if (__BYTE_ORDER == __LITTLE_ENDIAN)
87 +    {
88 +        fprintf(stderr, "Neither 32 or 64: %d\n", nBitType);
89 +        return (-1);
90 +    }
91 +
92 +    return 0;
93 +}
94 +
95 +static int
96 +IheaderBigEndianToHost(char *pHeader, int nBitType)
97 +{
98 +    if (nBitType == en_is64 && __BYTE_ORDER == __LITTLE_ENDIAN)
99 +    {
100 +        struct pi_header64 *pHeader64 = (struct pi_header64*)pHeader;
101 +
102 +        pHeader64->pih_magic = bswap_64(pHeader64->pih_magic);
103 +        pHeader64->pih_numwords = bswap_64(pHeader64->pih_numwords);
104 +        pHeader64->pih_blocklen = bswap_16(pHeader64->pih_blocklen);
105 +        pHeader64->pih_pad = bswap_16(pHeader64->pih_pad);
106 +
107 +#if DEBUG
108 +        printf("Header64: magic %x, numwords %x, blocklen %x, pad %x\n",
109 +          pHeader64->pih_magic, pHeader64->pih_numwords,
110 +          pHeader64->pih_blocklen, pHeader64->pih_pad);
111 +#endif
112 +    }
113 +    else if (nBitType == en_is32 && __BYTE_ORDER == __LITTLE_ENDIAN)
114 +    {
115 +        struct pi_header *pHeader32 = (struct pi_header*)pHeader;
116 +
117 +        pHeader32->pih_magic = bswap_32(pHeader32->pih_magic);
118 +        pHeader32->pih_numwords = bswap_32(pHeader32->pih_numwords);
119 +        pHeader32->pih_blocklen = bswap_16(pHeader32->pih_blocklen);
120 +        pHeader32->pih_pad = bswap_16(pHeader32->pih_pad);
121 +
122 +#if DEBUG
123 +        printf("Header32: magic %x, numwords %x, blocklen %x, pad %x\n",
124 +            pHeader32->pih_magic, pHeader32->pih_numwords,
125 +            pHeader32->pih_blocklen, pHeader32->pih_pad);
126 +#endif
127 +    }
128 +    else if (__BYTE_ORDER == __LITTLE_ENDIAN)
129 +    {
130 +        fprintf(stderr, "Neither 32 or 64: %d\n", nBitType);
131 +        return (-1);
132 +    }
133 +
134 +    return 0;
135 +}
136 +
137 +static int
138 +HwmsHostToBigEndian(char *pHwms, int nLen,int nBitType)
139 +{
140 +    int i = 0;
141 +
142 +    if (nBitType == en_is64 && __BYTE_ORDER == __LITTLE_ENDIAN)
143 +    {
144 +        uint64_t *pHwms64 = (uint64_t*)pHwms;
145 +
146 +        for (i = 0; i < nLen / sizeof(uint64_t); i++)
147 +        {
148 +            *pHwms64++ = bswap_64(*pHwms64);
149 +        }
150 +
151 +    }
152 +    else if (nBitType == en_is32 && __BYTE_ORDER == __LITTLE_ENDIAN)
153 +    {
154 +        uint32_t *pHwms32 = (uint32_t*)pHwms;
155 +
156 +        for (i = 0; i < nLen / sizeof(uint32_t); i++)
157 +        {
158 +            *pHwms32++ = bswap_32(*pHwms32);
159 +        }
160 +
161 +    }
162 +    else if (__BYTE_ORDER == __LITTLE_ENDIAN)
163 +    {
164 +        fprintf(stderr, "Neither 32 or 64: %d\n", nBitType);
165 +        return (-1);
166 +    }
167 +
168 +#if DEBUG
169 +    for (i = 0; i < nLen; i+=8)
170 +    {
171 +        printf("hwms%s: %02X %02X %02X %02X %02X %02X %02X %02X\n",
172 +            nBitType==en_is64?"64":"32", pHwms[i+0]&0xFF, pHwms[i+1]&0xFF,
173 +            pHwms[i+2]&0xFF, pHwms[i+3]&0xFF, pHwms[i+4]&0xFF,
174 +            pHwms[i+5]&0xFF, pHwms[i+6]&0xFF, pHwms[i+7]&0xFF);
175 +    }
176 +#endif
177 +
178 +    return 0;
179 +}
180 +
181 +static int
182 +HwmsBigEndianToHost(char *pHwms, int nLen, int nBitType)
183 +{
184 +    int i = 0;
185 +
186 +    if (nBitType == en_is64 && __BYTE_ORDER == __LITTLE_ENDIAN)
187 +    {
188 +        uint64_t *pHwms64 = (uint64_t*)pHwms;
189 +
190 +        for (i = 0; i < nLen / sizeof(uint64_t); i++)
191 +        {
192 +            *pHwms64++ = bswap_64(*pHwms64);
193 +        }
194 +
195 +    }
196 +    else if (nBitType == en_is32 && __BYTE_ORDER == __LITTLE_ENDIAN)
197 +    {
198 +        uint32_t *pHwms32 = (uint32_t*)pHwms;
199 +
200 +        for (i = 0; i < nLen / sizeof(uint32_t); i++)
201 +        {
202 +            *pHwms32++ = bswap_32(*pHwms32);
203 +        }
204 +
205 +    }
206 +    else if (__BYTE_ORDER == __LITTLE_ENDIAN)
207 +    {
208 +        fprintf(stderr, "Neither 32 or 64: %d\n", nBitType);
209 +        return (-1);
210 +    }
211 +
212 +#if DEBUG
213 +    for (i = 0; i < nLen; i+=8)
214 +    {
215 +        printf("hwms%s: %02X %02X %02X %02X %02X %02X %02X %02X\n",
216 +            nBitType==en_is64?"64":"32", pHwms[i+0]&0xFF, pHwms[i+1]&0xFF,
217 +            pHwms[i+2]&0xFF, pHwms[i+3]&0xFF, pHwms[i+4]&0xFF,
218 +            pHwms[i+5]&0xFF, pHwms[i+6]&0xFF, pHwms[i+7]&0xFF);
219 +    }
220 +#endif
221 +
222 +    return 0;
223 +}
224  
225  static int
226  _PWIsBroken64(FILE *ifp)
227 @@ -57,6 +239,7 @@ _PWIsBroken64(FILE *ifp)
228         return 0;
229      }
230  
231 +    IheaderBigEndianToHost((char *) &pdesc64.header, en_is64);
232      return (pdesc64.header.pih_magic == PIH_MAGIC);
233  }
234  
235 @@ -149,7 +332,11 @@ PWOpen(prefix, mode)
236         pdesc.header.pih_blocklen = NUMWORDS;
237         pdesc.header.pih_numwords = 0;
238  
239 -       fwrite((char *) &pdesc.header, sizeof(pdesc.header), 1, ifp);
240 +       struct pi_header tmpheader32;
241 +
242 +       memcpy(&tmpheader32,  &pdesc.header, sizeof(pdesc.header));
243 +       IheaderHostToBigEndian((char *) &tmpheader32, en_is32);
244 +       fwrite((char *) &tmpheader32, sizeof(tmpheader32), 1, ifp);
245      } else
246      {
247         pdesc.flags &= ~PFOR_WRITE;
248 @@ -173,6 +360,7 @@ PWOpen(prefix, mode)
249             return ((PWDICT *) 0);
250         }
251  
252 +        IheaderBigEndianToHost((char *) &pdesc.header, en_is32);
253          if ((pdesc.header.pih_magic == 0) || (pdesc.header.pih_numwords == 0))
254          {
255              /* uh-oh. either a broken "64-bit" file or a garbage file. */
256 @@ -195,6 +383,7 @@ PWOpen(prefix, mode)
257                 }
258                  return ((PWDICT *) 0);
259              }
260 +            IheaderBigEndianToHost((char *) &pdesc64.header, en_is64);
261              if (pdesc64.header.pih_magic != PIH_MAGIC)
262              {
263                  /* nope, not "64-bit" after all */
264 @@ -290,6 +479,7 @@ PWOpen(prefix, mode)
265                  {
266                      pdesc.flags &= ~PFOR_USEHWMS;
267                  }
268 +                HwmsBigEndianToHost((char*)pdesc64.hwms, sizeof(pdesc64.hwms), en_is64);
269                  for (i = 0; i < sizeof(pdesc.hwms) / sizeof(pdesc.hwms[0]); i++)
270                  {
271                      pdesc.hwms[i] = pdesc64.hwms[i];
272 @@ -299,6 +489,7 @@ PWOpen(prefix, mode)
273             {
274                 pdesc.flags &= ~PFOR_USEHWMS;
275             }
276 +           HwmsBigEndianToHost((char*)pdesc.hwms, sizeof(pdesc.hwms), en_is32);
277  #if DEBUG
278              for (i=1; i<=0xff; i++)
279              {
280 @@ -332,7 +523,11 @@ PWClose(pwp)
281             return (-1);
282         }
283  
284 -       if (!fwrite((char *) &pwp->header, sizeof(pwp->header), 1, pwp->ifp))
285 +       struct pi_header tmpheader32;
286 +
287 +       memcpy(&tmpheader32,  &pwp->header, sizeof(pwp->header));
288 +       IheaderHostToBigEndian((char *) &tmpheader32, en_is32);
289 +       if (!fwrite((char *) &tmpheader32, sizeof(tmpheader32), 1, pwp->ifp))
290         {
291             fprintf(stderr, "index magic fwrite failed\n");
292             return (-1);
293 @@ -351,7 +546,12 @@ PWClose(pwp)
294                 printf("hwm[%02x] = %d\n", i, pwp->hwms[i]);
295  #endif
296             }
297 -           fwrite(pwp->hwms, 1, sizeof(pwp->hwms), pwp->wfp);
298 +
299 +           PWDICT tmp_pwp;
300 +
301 +           memcpy(&tmp_pwp, pwp, sizeof(PWDICT));
302 +           HwmsHostToBigEndian(tmp_pwp.hwms, sizeof(tmp_pwp.hwms), en_is32);
303 +           fwrite(tmp_pwp.hwms, 1, sizeof(tmp_pwp.hwms), pwp->wfp);
304         }
305      }
306  
307 @@ -405,7 +605,8 @@ PutPW(pwp, string)
308  
309         datum = (uint32_t) ftell(pwp->dfp);
310  
311 -       fwrite((char *) &datum, sizeof(datum), 1, pwp->ifp);
312 +       uint32_t tmpdatum = (__BYTE_ORDER == __LITTLE_ENDIAN) ? bswap_32(datum) : datum;
313 +       fwrite((char *) &tmpdatum, sizeof(tmpdatum), 1, pwp->ifp);
314  
315         fputs(pwp->data_put[0], pwp->dfp);
316         putc(0, pwp->dfp);
317 @@ -473,6 +674,7 @@ GetPW(pwp, number)
318             perror("(index fread failed)");
319             return ((char *) 0);
320         }
321 +       datum64 = (__BYTE_ORDER == __LITTLE_ENDIAN) ? bswap_64(datum64) : datum64;
322         datum = datum64;
323      } else {
324         if (fseek(pwp->ifp, sizeof(struct pi_header) + (thisblock * sizeof(uint32_t)), 0))
325 @@ -486,6 +688,7 @@ GetPW(pwp, number)
326             perror("(index fread failed)");
327             return ((char *) 0);
328         }
329 +       datum = (__BYTE_ORDER == __LITTLE_ENDIAN) ? bswap_32(datum) : datum;
330      }
331  
332         int r = 1;