2 * This program is copyright Alec Muffett 1993. The author disclaims all
3 * responsibility or liability with respect to it's usage or its effect
4 * upon hardware or computer systems, and maintains copyright as set out
5 * in the "LICENCE" document which accompanies distributions of Crack v4.0
11 static char vers_id[] = "packlib.c : v2.3p2 Alec Muffett 18 May 1993";
20 char iname[STRINGSIZE];
21 char dname[STRINGSIZE];
22 char wname[STRINGSIZE];
23 char buffer[STRINGSIZE];
28 if (pdesc.header.pih_magic == PIH_MAGIC)
30 fprintf(stderr, "%s: another dictionary already open\n", prefix);
31 return ((PWDICT *) 0);
34 memset(&pdesc, '\0', sizeof(pdesc));
36 sprintf(iname, "%s.pwi", prefix);
37 sprintf(dname, "%s.pwd", prefix);
38 sprintf(wname, "%s.hwm", prefix);
40 if (!(pdesc.dfp = fopen(dname, mode)))
43 return ((PWDICT *) 0);
46 if (!(pdesc.ifp = fopen(iname, mode)))
50 return ((PWDICT *) 0);
53 if (pdesc.wfp = fopen(wname, mode))
55 pdesc.flags |= PFOR_USEHWMS;
64 pdesc.flags |= PFOR_WRITE;
65 pdesc.header.pih_magic = PIH_MAGIC;
66 pdesc.header.pih_blocklen = NUMWORDS;
67 pdesc.header.pih_numwords = 0;
69 fwrite((char *) &pdesc.header, sizeof(pdesc.header), 1, ifp);
72 pdesc.flags &= ~PFOR_WRITE;
74 if (!fread((char *) &pdesc.header, sizeof(pdesc.header), 1, ifp))
76 fprintf(stderr, "%s: error reading header\n", prefix);
78 pdesc.header.pih_magic = 0;
81 return ((PWDICT *) 0);
84 if (pdesc.header.pih_magic != PIH_MAGIC)
86 fprintf(stderr, "%s: magic mismatch\n", prefix);
88 pdesc.header.pih_magic = 0;
91 return ((PWDICT *) 0);
94 if (pdesc.header.pih_blocklen != NUMWORDS)
96 fprintf(stderr, "%s: size mismatch\n", prefix);
98 pdesc.header.pih_magic = 0;
101 return ((PWDICT *) 0);
104 if (pdesc.flags & PFOR_USEHWMS)
106 if (fread(pdesc.hwms, 1, sizeof(pdesc.hwms), wfp) != sizeof(pdesc.hwms))
108 pdesc.flags &= ~PFOR_USEHWMS;
120 if (pwp->header.pih_magic != PIH_MAGIC)
122 fprintf(stderr, "PWClose: close magic mismatch\n");
126 if (pwp->flags & PFOR_WRITE)
128 pwp->flags |= PFOR_FLUSH;
129 PutPW(pwp, (char *) 0); /* flush last index if necess */
131 if (fseek(pwp->ifp, 0L, 0))
133 fprintf(stderr, "index magic fseek failed\n");
137 if (!fwrite((char *) &pwp->header, sizeof(pwp->header), 1, pwp->ifp))
139 fprintf(stderr, "index magic fwrite failed\n");
143 if (pwp->flags & PFOR_USEHWMS)
146 for (i=1; i<=0xff; i++)
150 pwp->hwms[i] = pwp->hwms[i-1];
153 printf("hwm[%02x] = %d\n", i, pwp->hwms[i]);
156 fwrite(pwp->hwms, 1, sizeof(pwp->hwms), pwp->wfp);
163 pwp->header.pih_magic = 0;
173 if (!(pwp->flags & PFOR_WRITE))
180 strncpy(pwp->data[pwp->count], string, MAXWORDLEN);
181 pwp->data[pwp->count][MAXWORDLEN - 1] = '\0';
183 pwp->hwms[string[0] & 0xff]= pwp->header.pih_numwords;
186 ++(pwp->header.pih_numwords);
188 } else if (!(pwp->flags & PFOR_FLUSH))
193 if ((pwp->flags & PFOR_FLUSH) || !(pwp->count % NUMWORDS))
199 datum = (int32) ftell(pwp->dfp);
201 fwrite((char *) &datum, sizeof(datum), 1, pwp->ifp);
203 fputs(pwp->data[0], pwp->dfp);
208 for (i = 1; i < NUMWORDS; i++)
216 for (j = 0; ostr[j] && nstr[j] && (ostr[j] == nstr[j]); j++);
217 putc(j & 0xff, pwp->dfp);
218 fputs(nstr + j, pwp->dfp);
225 memset(pwp->data, '\0', sizeof(pwp->data));
241 char buffer[NUMWORDS * MAXWORDLEN];
242 static char data[NUMWORDS][MAXWORDLEN];
243 static int32 prevblock = 0xffffffff;
246 thisblock = number / NUMWORDS;
248 if (prevblock == thisblock)
250 return (data[number % NUMWORDS]);
253 if (fseek(pwp->ifp, sizeof(struct pi_header) + (thisblock * sizeof(int32)), 0))
255 perror("(index fseek failed)");
259 if (!fread((char *) &datum, sizeof(datum), 1, pwp->ifp))
261 perror("(index fread failed)");
265 if (fseek(pwp->dfp, datum, 0))
267 perror("(data fseek failed)");
271 if (!fread(buffer, 1, sizeof(buffer), pwp->dfp))
273 perror("(data fread failed)");
277 prevblock = thisblock;
281 for (ostr = data[0]; *(ostr++) = *(bptr++); /* nothing */ );
285 for (i = 1; i < NUMWORDS; i++)
290 ostr = nstr + *(bptr++);
291 while (*(ostr++) = *(bptr++));
296 return (data[number % NUMWORDS]);
306 register int32 middle;
310 if (pwp->flags & PFOR_USEHWMS)
312 idx = string[0] & 0xff;
313 lwm = idx ? pwp->hwms[idx - 1] : 0;
314 hwm = pwp->hwms[idx];
318 hwm = PW_WORDS(pwp) - 1;
322 printf("---- %lu, %lu ----\n", lwm, hwm);
330 printf("%lu, %lu\n", lwm, hwm);
333 middle = lwm + ((hwm - lwm + 1) / 2);
340 this = GetPW(pwp, middle);
341 cmp = strcmp(string, this); /* INLINE ? */
355 return (PW_WORDS(pwp));