2 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
7 * This program is copyright Alec Muffett 1993. The author disclaims all
8 * responsibility or liability with respect to it's usage or its effect
9 * upon hardware or computer systems, and maintains copyright as set out
10 * in the "LICENCE" document which accompanies distributions of Crack v4.0
21 (void) snprintf(fname
, sizeof (fname
), "%s/%s", path
,
24 (void) snprintf(fname
, sizeof (fname
), "%s/%s", path
,
27 (void) snprintf(fname
, sizeof (fname
), "%s/%s", path
,
33 PWOpen(char *path
, char *mode
)
46 if ((pdesc
= calloc(1, sizeof (PWDICT
))) == NULL
)
47 return ((PWDICT
*) 0);
49 if (pdesc
->header
.pih_magic
== PIH_MAGIC
) {
50 return ((PWDICT
*) 0);
52 (void) memset(pdesc
, '\0', sizeof (pdesc
));
54 (void) snprintf(iname
, sizeof (iname
), "%s/%s", path
,
56 (void) snprintf(dname
, sizeof (dname
), "%s/%s", path
,
58 (void) snprintf(wname
, sizeof (wname
), "%s/%s", path
,
61 if ((fd_d
= open(dname
, O_RDWR
|O_CREAT
, 0600)) == -1)
62 syslog(LOG_ERR
, "PWopen: can't open %s: %s", dname
,
64 if ((fd_i
= open(iname
, O_RDWR
|O_CREAT
, 0600)) == -1)
65 syslog(LOG_ERR
, "PWopen: can't open %s: %s", iname
,
67 if ((fd_w
= open(wname
, O_RDWR
|O_CREAT
, 0600)) == -1)
68 syslog(LOG_ERR
, "PWopen: can't open %s: %s", wname
,
71 if (!(pdesc
->dfp
= fdopen(fd_d
, mode
))) {
72 return ((PWDICT
*) 0);
75 if (!(pdesc
->ifp
= fdopen(fd_i
, mode
))) {
76 (void) fclose(pdesc
->dfp
);
77 return ((PWDICT
*) 0);
80 if (pdesc
->wfp
= fdopen(fd_w
, mode
)) {
81 pdesc
->flags
|= PFOR_USEHWMS
;
89 pdesc
->flags
|= PFOR_WRITE
;
90 pdesc
->header
.pih_magic
= PIH_MAGIC
;
91 pdesc
->header
.pih_blocklen
= NUMWORDS
;
92 pdesc
->header
.pih_numwords
= 0;
94 (void) fwrite((char *)&(pdesc
->header
), sizeof (pdesc
->header
),
97 pdesc
->flags
&= ~PFOR_WRITE
;
99 if (!fread((char *)&(pdesc
->header
), sizeof (pdesc
->header
),
101 pdesc
->header
.pih_magic
= 0;
104 return ((PWDICT
*) 0);
107 if (pdesc
->header
.pih_magic
!= PIH_MAGIC
) {
108 pdesc
->header
.pih_magic
= 0;
111 return ((PWDICT
*) 0);
114 if (pdesc
->header
.pih_blocklen
!= NUMWORDS
) {
115 pdesc
->header
.pih_magic
= 0;
118 return ((PWDICT
*) 0);
121 if (pdesc
->flags
& PFOR_USEHWMS
) {
122 if (fread(pdesc
->hwms
, 1, sizeof (pdesc
->hwms
), wfp
) !=
123 sizeof (pdesc
->hwms
)) {
124 pdesc
->flags
&= ~PFOR_USEHWMS
;
134 if (pwp
->header
.pih_magic
!= PIH_MAGIC
) {
138 if (pwp
->flags
& PFOR_WRITE
) {
139 pwp
->flags
|= PFOR_FLUSH
;
140 (void) PutPW(pwp
, (char *)0); /* flush last index if necess */
142 if (fseek(pwp
->ifp
, 0L, 0)) {
146 if (!fwrite((char *)&pwp
->header
, sizeof (pwp
->header
),
151 if (pwp
->flags
& PFOR_USEHWMS
) {
153 for (i
= 1; i
<= 0xff; i
++) {
155 pwp
->hwms
[i
] = pwp
->hwms
[i
-1];
158 (void) fwrite(pwp
->hwms
, 1, sizeof (pwp
->hwms
),
163 (void) fclose(pwp
->ifp
);
164 (void) fclose(pwp
->dfp
);
165 (void) fclose(pwp
->wfp
);
167 pwp
->header
.pih_magic
= 0;
175 PutPW(PWDICT
*pwp
, char *string
)
177 if (!(pwp
->flags
& PFOR_WRITE
)) {
182 (void) strncpy(pwp
->data
[pwp
->count
], string
, MAXWORDLEN
);
183 pwp
->data
[pwp
->count
][MAXWORDLEN
- 1] = '\0';
185 pwp
->hwms
[string
[0] & 0xff] = pwp
->header
.pih_numwords
;
188 ++(pwp
->header
.pih_numwords
);
190 } else if (!(pwp
->flags
& PFOR_FLUSH
)) {
194 if ((pwp
->flags
& PFOR_FLUSH
) || !(pwp
->count
% NUMWORDS
)) {
199 datum
= (uint32_t)ftell(pwp
->dfp
);
201 (void) fwrite((char *)&datum
, sizeof (datum
), 1, pwp
->ifp
);
203 (void) fputs(pwp
->data
[0], pwp
->dfp
);
204 (void) putc(0, pwp
->dfp
);
208 for (i
= 1; i
< NUMWORDS
; i
++) {
215 for (j
= 0; ostr
[j
] && nstr
[j
] &&
216 (ostr
[j
] == nstr
[j
]); j
++)
218 (void) putc(j
& 0xff, pwp
->dfp
);
219 (void) fputs(nstr
+ j
, pwp
->dfp
);
221 (void) putc(0, pwp
->dfp
);
226 (void) memset(pwp
->data
, '\0', sizeof (pwp
->data
));
233 GetPW(PWDICT
*pwp
, uint32_t number
)
240 char buffer
[NUMWORDS
* MAXWORDLEN
];
241 static char data
[NUMWORDS
][MAXWORDLEN
];
242 static uint32_t prevblock
= 0xffffffff;
245 thisblock
= number
/ NUMWORDS
;
247 if (prevblock
== thisblock
) {
248 return (data
[number
% NUMWORDS
]);
251 if (fseek(pwp
->ifp
, sizeof (struct pi_header
) +
252 (thisblock
* sizeof (uint32_t)), 0)) {
256 if (!fread((char *)&datum
, sizeof (datum
), 1, pwp
->ifp
)) {
260 if (fseek(pwp
->dfp
, datum
, 0)) {
264 if (!fread(buffer
, 1, sizeof (buffer
), pwp
->dfp
)) {
268 prevblock
= thisblock
;
272 for (ostr
= data
[0]; *(ostr
++) = *(bptr
++); /* nothing */)
277 for (i
= 1; i
< NUMWORDS
; i
++) {
279 (void) strcpy(nstr
, ostr
);
280 ostr
= nstr
+ *(bptr
++);
281 while (*(ostr
++) = *(bptr
++))
287 return (data
[number
% NUMWORDS
]);
291 FindPW(PWDICT
*pwp
, char *string
)
298 return (PW_WORDS(pwp
));
300 if (pwp
->flags
& PFOR_USEHWMS
) {
301 idx
= string
[0] & 0xff;
302 lwm
= idx
? pwp
->hwms
[idx
- 1] : 0;
303 hwm
= pwp
->hwms
[idx
];
306 hwm
= PW_WORDS(pwp
) - 1;
314 pivot
= lwm
+ ((hwm
+1)-lwm
)/2;
316 if (feof(pwp
->ifp
) && feof(pwp
->dfp
) && feof(pwp
->wfp
))
319 if ((this = GetPW(pwp
, pivot
)) == NULL
)
322 cmp
= strcmp(string
, this); /* INLINE ? */
331 if (lwm
> hwm
) /* searched all; not found */
336 return (PW_WORDS(pwp
));