ksgi

Форк
0
/
compats.c 
4184 строки · 113.8 Кб
1
#include "config.h"
2
#if !HAVE_ERR
3
/*
4
 * Copyright (c) 1993
5
 *      The Regents of the University of California.  All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 * 1. Redistributions of source code must retain the above copyright
11
 *    notice, this list of conditions and the following disclaimer.
12
 * 2. Redistributions in binary form must reproduce the above copyright
13
 *    notice, this list of conditions and the following disclaimer in the
14
 *    documentation and/or other materials provided with the distribution.
15
 * 3. Neither the name of the University nor the names of its contributors
16
 *    may be used to endorse or promote products derived from this software
17
 *    without specific prior written permission.
18
 *
19
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29
 * SUCH DAMAGE.
30
 */
31

32
#include <errno.h>
33
#include <stdarg.h>
34
#include <stdio.h>
35
#include <stdlib.h>
36
#include <string.h>
37

38
void
39
vwarnx(const char *fmt, va_list ap)
40
{
41
	fprintf(stderr, "%s: ", getprogname());
42
	if (fmt != NULL)
43
		vfprintf(stderr, fmt, ap);
44
	fprintf(stderr, "\n");
45
}
46

47
void
48
vwarnc(int code, const char *fmt, va_list ap)
49
{
50
	fprintf(stderr, "%s: ", getprogname());
51
	if (fmt != NULL) {
52
		vfprintf(stderr, fmt, ap);
53
		fprintf(stderr, ": ");
54
	}
55
	fprintf(stderr, "%s\n", strerror(code));
56
}
57

58
void
59
vwarn(const char *fmt, va_list ap)
60
{
61
	int sverrno;
62

63
	sverrno = errno;
64
	fprintf(stderr, "%s: ", getprogname());
65
	if (fmt != NULL) {
66
		vfprintf(stderr, fmt, ap);
67
		fprintf(stderr, ": ");
68
	}
69
	fprintf(stderr, "%s\n", strerror(sverrno));
70
}
71

72
void
73
verrc(int eval, int code, const char *fmt, va_list ap)
74
{
75
	fprintf(stderr, "%s: ", getprogname());
76
	if (fmt != NULL) {
77
		vfprintf(stderr, fmt, ap);
78
		fprintf(stderr, ": ");
79
	}
80
	fprintf(stderr, "%s\n", strerror(code));
81
	exit(eval);
82
}
83

84
void
85
verrx(int eval, const char *fmt, va_list ap)
86
{
87
	fprintf(stderr, "%s: ", getprogname());
88
	if (fmt != NULL)
89
		vfprintf(stderr, fmt, ap);
90
	fprintf(stderr, "\n");
91
	exit(eval);
92
}
93

94
void
95
verr(int eval, const char *fmt, va_list ap)
96
{
97
	int sverrno;
98

99
	sverrno = errno;
100
	fprintf(stderr, "%s: ", getprogname());
101
	if (fmt != NULL) {
102
		vfprintf(stderr, fmt, ap);
103
		fprintf(stderr, ": ");
104
	}
105
	fprintf(stderr, "%s\n", strerror(sverrno));
106
	exit(eval);
107
}
108

109
void
110
err(int eval, const char *fmt, ...)
111
{
112
	va_list ap;
113

114
	va_start(ap, fmt);
115
	verr(eval, fmt, ap);
116
	va_end(ap);
117
}
118

119
void
120
errc(int eval, int code, const char *fmt, ...)
121
{
122
	va_list ap;
123

124
	va_start(ap, fmt);
125
	verrc(eval, code, fmt, ap);
126
	va_end(ap);
127
}
128

129
void
130
errx(int eval, const char *fmt, ...)
131
{
132
	va_list ap;
133

134
	va_start(ap, fmt);
135
	verrx(eval, fmt, ap);
136
	va_end(ap);
137
}
138

139
void
140
warn(const char *fmt, ...)
141
{
142
	va_list ap;
143

144
	va_start(ap, fmt);
145
	vwarn(fmt, ap);
146
	va_end(ap);
147
}
148

149
void
150
warnc(int code, const char *fmt, ...)
151
{
152
	va_list ap;
153

154
	va_start(ap, fmt);
155
	vwarnc(code, fmt, ap);
156
	va_end(ap);
157
}
158

159
void
160
warnx(const char *fmt, ...)
161
{
162
	va_list ap;
163

164
	va_start(ap, fmt);
165
	vwarnx(fmt, ap);
166
	va_end(ap);
167
}
168
#endif /* !HAVE_ERR */
169
#if !HAVE_B64_NTOP
170
/*	$OpenBSD$	*/
171

172
/*
173
 * Copyright (c) 1996 by Internet Software Consortium.
174
 *
175
 * Permission to use, copy, modify, and distribute this software for any
176
 * purpose with or without fee is hereby granted, provided that the above
177
 * copyright notice and this permission notice appear in all copies.
178
 *
179
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
180
 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
181
 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
182
 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
183
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
184
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
185
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
186
 * SOFTWARE.
187
 */
188

189
/*
190
 * Portions Copyright (c) 1995 by International Business Machines, Inc.
191
 *
192
 * International Business Machines, Inc. (hereinafter called IBM) grants
193
 * permission under its copyrights to use, copy, modify, and distribute this
194
 * Software with or without fee, provided that the above copyright notice and
195
 * all paragraphs of this notice appear in all copies, and that the name of IBM
196
 * not be used in connection with the marketing of any product incorporating
197
 * the Software or modifications thereof, without specific, written prior
198
 * permission.
199
 *
200
 * To the extent it has a right to do so, IBM grants an immunity from suit
201
 * under its patents, if any, for the use, sale or manufacture of products to
202
 * the extent that such products are used for performing Domain Name System
203
 * dynamic updates in TCP/IP networks by means of the Software.  No immunity is
204
 * granted for any product per se or for any other function of any product.
205
 *
206
 * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
207
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
208
 * PARTICULAR PURPOSE.  IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
209
 * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
210
 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
211
 * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
212
 */
213

214
#include <sys/types.h>
215
#include <sys/socket.h>
216
#include <netinet/in.h>
217
#include <arpa/inet.h>
218
#include <arpa/nameser.h>
219

220
#include <ctype.h>
221
#include <resolv.h>
222
#include <stdio.h>
223

224
#include <stdlib.h>
225
#include <string.h>
226

227
static const char b64_Base64[] =
228
	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
229
static const char b64_Pad64 = '=';
230

231
/* (From RFC1521 and draft-ietf-dnssec-secext-03.txt)
232
   The following encoding technique is taken from RFC 1521 by Borenstein
233
   and Freed.  It is reproduced here in a slightly edited form for
234
   convenience.
235

236
   A 65-character subset of US-ASCII is used, enabling 6 bits to be
237
   represented per printable character. (The extra 65th character, "=",
238
   is used to signify a special processing function.)
239

240
   The encoding process represents 24-bit groups of input bits as output
241
   strings of 4 encoded characters. Proceeding from left to right, a
242
   24-bit input group is formed by concatenating 3 8-bit input groups.
243
   These 24 bits are then treated as 4 concatenated 6-bit groups, each
244
   of which is translated into a single digit in the base64 alphabet.
245

246
   Each 6-bit group is used as an index into an array of 64 printable
247
   characters. The character referenced by the index is placed in the
248
   output string.
249

250
                         Table 1: The Base64 Alphabet
251

252
      Value Encoding  Value Encoding  Value Encoding  Value Encoding
253
          0 A            17 R            34 i            51 z
254
          1 B            18 S            35 j            52 0
255
          2 C            19 T            36 k            53 1
256
          3 D            20 U            37 l            54 2
257
          4 E            21 V            38 m            55 3
258
          5 F            22 W            39 n            56 4
259
          6 G            23 X            40 o            57 5
260
          7 H            24 Y            41 p            58 6
261
          8 I            25 Z            42 q            59 7
262
          9 J            26 a            43 r            60 8
263
         10 K            27 b            44 s            61 9
264
         11 L            28 c            45 t            62 +
265
         12 M            29 d            46 u            63 /
266
         13 N            30 e            47 v
267
         14 O            31 f            48 w         (pad) =
268
         15 P            32 g            49 x
269
         16 Q            33 h            50 y
270

271
   Special processing is performed if fewer than 24 bits are available
272
   at the end of the data being encoded.  A full encoding quantum is
273
   always completed at the end of a quantity.  When fewer than 24 input
274
   bits are available in an input group, zero bits are added (on the
275
   right) to form an integral number of 6-bit groups.  Padding at the
276
   end of the data is performed using the '=' character.
277

278
   Since all base64 input is an integral number of octets, only the
279
         -------------------------------------------------                       
280
   following cases can arise:
281
   
282
       (1) the final quantum of encoding input is an integral
283
           multiple of 24 bits; here, the final unit of encoded
284
	   output will be an integral multiple of 4 characters
285
	   with no "=" padding,
286
       (2) the final quantum of encoding input is exactly 8 bits;
287
           here, the final unit of encoded output will be two
288
	   characters followed by two "=" padding characters, or
289
       (3) the final quantum of encoding input is exactly 16 bits;
290
           here, the final unit of encoded output will be three
291
	   characters followed by one "=" padding character.
292
   */
293

294
int
295
b64_ntop(u_char const *src, size_t srclength, char *target, size_t targsize)
296
{
297
	size_t datalength = 0;
298
	u_char input[3];
299
	u_char output[4];
300
	size_t i;
301

302
	while (2 < srclength) {
303
		input[0] = *src++;
304
		input[1] = *src++;
305
		input[2] = *src++;
306
		srclength -= 3;
307

308
		output[0] = input[0] >> 2;
309
		output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);
310
		output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);
311
		output[3] = input[2] & 0x3f;
312

313
		if (datalength + 4 > targsize)
314
			return (-1);
315
		target[datalength++] = b64_Base64[output[0]];
316
		target[datalength++] = b64_Base64[output[1]];
317
		target[datalength++] = b64_Base64[output[2]];
318
		target[datalength++] = b64_Base64[output[3]];
319
	}
320
    
321
	/* Now we worry about padding. */
322
	if (0 != srclength) {
323
		/* Get what's left. */
324
		input[0] = input[1] = input[2] = '\0';
325
		for (i = 0; i < srclength; i++)
326
			input[i] = *src++;
327
	
328
		output[0] = input[0] >> 2;
329
		output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);
330
		output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);
331

332
		if (datalength + 4 > targsize)
333
			return (-1);
334
		target[datalength++] = b64_Base64[output[0]];
335
		target[datalength++] = b64_Base64[output[1]];
336
		if (srclength == 1)
337
			target[datalength++] = b64_Pad64;
338
		else
339
			target[datalength++] = b64_Base64[output[2]];
340
		target[datalength++] = b64_Pad64;
341
	}
342
	if (datalength >= targsize)
343
		return (-1);
344
	target[datalength] = '\0';	/* Returned value doesn't count \0. */
345
	return (datalength);
346
}
347

348
/* skips all whitespace anywhere.
349
   converts characters, four at a time, starting at (or after)
350
   src from base - 64 numbers into three 8 bit bytes in the target area.
351
   it returns the number of data bytes stored at the target, or -1 on error.
352
 */
353

354
int
355
b64_pton(char const *src, u_char *target, size_t targsize)
356
{
357
	int state, ch;
358
	size_t tarindex;
359
	u_char nextbyte;
360
	char *pos;
361

362
	state = 0;
363
	tarindex = 0;
364

365
	while ((ch = (unsigned char)*src++) != '\0') {
366
		if (isspace(ch))	/* Skip whitespace anywhere. */
367
			continue;
368

369
		if (ch == b64_Pad64)
370
			break;
371

372
		pos = strchr(b64_Base64, ch);
373
		if (pos == 0) 		/* A non-base64 character. */
374
			return (-1);
375

376
		switch (state) {
377
		case 0:
378
			if (target) {
379
				if (tarindex >= targsize)
380
					return (-1);
381
				target[tarindex] = (pos - b64_Base64) << 2;
382
			}
383
			state = 1;
384
			break;
385
		case 1:
386
			if (target) {
387
				if (tarindex >= targsize)
388
					return (-1);
389
				target[tarindex]   |=  (pos - b64_Base64) >> 4;
390
				nextbyte = ((pos - b64_Base64) & 0x0f) << 4;
391
				if (tarindex + 1 < targsize)
392
					target[tarindex+1] = nextbyte;
393
				else if (nextbyte)
394
					return (-1);
395
			}
396
			tarindex++;
397
			state = 2;
398
			break;
399
		case 2:
400
			if (target) {
401
				if (tarindex >= targsize)
402
					return (-1);
403
				target[tarindex]   |=  (pos - b64_Base64) >> 2;
404
				nextbyte = ((pos - b64_Base64) & 0x03) << 6;
405
				if (tarindex + 1 < targsize)
406
					target[tarindex+1] = nextbyte;
407
				else if (nextbyte)
408
					return (-1);
409
			}
410
			tarindex++;
411
			state = 3;
412
			break;
413
		case 3:
414
			if (target) {
415
				if (tarindex >= targsize)
416
					return (-1);
417
				target[tarindex] |= (pos - b64_Base64);
418
			}
419
			tarindex++;
420
			state = 0;
421
			break;
422
		}
423
	}
424

425
	/*
426
	 * We are done decoding Base-64 chars.  Let's see if we ended
427
	 * on a byte boundary, and/or with erroneous trailing characters.
428
	 */
429

430
	if (ch == b64_Pad64) {			/* We got a pad char. */
431
		ch = (unsigned char)*src++;	/* Skip it, get next. */
432
		switch (state) {
433
		case 0:		/* Invalid = in first position */
434
		case 1:		/* Invalid = in second position */
435
			return (-1);
436

437
		case 2:		/* Valid, means one byte of info */
438
			/* Skip any number of spaces. */
439
			for (; ch != '\0'; ch = (unsigned char)*src++)
440
				if (!isspace(ch))
441
					break;
442
			/* Make sure there is another trailing = sign. */
443
			if (ch != b64_Pad64)
444
				return (-1);
445
			ch = (unsigned char)*src++;		/* Skip the = */
446
			/* Fall through to "single trailing =" case. */
447
			/* FALLTHROUGH */
448

449
		case 3:		/* Valid, means two bytes of info */
450
			/*
451
			 * We know this char is an =.  Is there anything but
452
			 * whitespace after it?
453
			 */
454
			for (; ch != '\0'; ch = (unsigned char)*src++)
455
				if (!isspace(ch))
456
					return (-1);
457

458
			/*
459
			 * Now make sure for cases 2 and 3 that the "extra"
460
			 * bits that slopped past the last full byte were
461
			 * zeros.  If we don't check them, they become a
462
			 * subliminal channel.
463
			 */
464
			if (target && tarindex < targsize &&
465
			    target[tarindex] != 0)
466
				return (-1);
467
		}
468
	} else {
469
		/*
470
		 * We ended by seeing the end of the string.  Make sure we
471
		 * have no partial bytes lying around.
472
		 */
473
		if (state != 0)
474
			return (-1);
475
	}
476

477
	return (tarindex);
478
}
479
#endif /* !HAVE_B64_NTOP */
480
#if !HAVE_EXPLICIT_BZERO
481
/* OPENBSD ORIGINAL: lib/libc/string/explicit_bzero.c */
482
/*
483
 * Public domain.
484
 * Written by Ted Unangst
485
 */
486

487
#include <string.h>
488

489
/*
490
 * explicit_bzero - don't let the compiler optimize away bzero
491
 */
492

493
#if HAVE_MEMSET_S
494

495
void
496
explicit_bzero(void *p, size_t n)
497
{
498
	if (n == 0)
499
		return;
500
	(void)memset_s(p, n, 0, n);
501
}
502

503
#else /* HAVE_MEMSET_S */
504

505
#include <strings.h>
506

507
/*
508
 * Indirect memset through a volatile pointer to hopefully avoid
509
 * dead-store optimisation eliminating the call.
510
 */
511
static void (* volatile ssh_memset)(void *, int, size_t) = memset;
512

513
void
514
explicit_bzero(void *p, size_t n)
515
{
516
	if (n == 0)
517
		return;
518
	/*
519
	 * clang -fsanitize=memory needs to intercept memset-like functions
520
	 * to correctly detect memory initialisation. Make sure one is called
521
	 * directly since our indirection trick above sucessfully confuses it.
522
	 */
523
#if defined(__has_feature)
524
# if __has_feature(memory_sanitizer)
525
	memset(p, 0, n);
526
# endif
527
#endif
528

529
	ssh_memset(p, 0, n);
530
}
531

532
#endif /* HAVE_MEMSET_S */
533
#endif /* !HAVE_EXPLICIT_BZERO */
534
#if !HAVE_FTS
535
/*	$OpenBSD$	*/
536

537
/*-
538
 * Copyright (c) 1990, 1993, 1994
539
 *	The Regents of the University of California.  All rights reserved.
540
 *
541
 * Redistribution and use in source and binary forms, with or without
542
 * modification, are permitted provided that the following conditions
543
 * are met:
544
 * 1. Redistributions of source code must retain the above copyright
545
 *    notice, this list of conditions and the following disclaimer.
546
 * 2. Redistributions in binary form must reproduce the above copyright
547
 *    notice, this list of conditions and the following disclaimer in the
548
 *    documentation and/or other materials provided with the distribution.
549
 * 3. Neither the name of the University nor the names of its contributors
550
 *    may be used to endorse or promote products derived from this software
551
 *    without specific prior written permission.
552
 *
553
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
554
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
555
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
556
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
557
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
558
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
559
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
560
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
561
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
562
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
563
 * SUCH DAMAGE.
564
 */
565

566
#include <sys/stat.h>
567
#include <sys/types.h>
568

569
#include <dirent.h>
570
#include <errno.h>
571
#include <fcntl.h>
572
#include <limits.h>
573
#include <stdlib.h>
574
#include <string.h>
575
#include <unistd.h>
576

577
/* 
578
 * oconfigure: Adapted from sys/_types.h.
579
 * oconfigure: Be conservative with ALIGNBYTES.
580
 */
581
#define	FTS_ALIGNBYTES		(sizeof(long) - 1)
582
#define	FTS_ALIGN(p)		(((unsigned long)(p) + FTS_ALIGNBYTES) &~ FTS_ALIGNBYTES)
583

584
static FTSENT	*fts_alloc(FTS *, char *, size_t);
585
static FTSENT	*fts_build(FTS *, int);
586
static void	 fts_lfree(FTSENT *);
587
static void	 fts_load(FTS *, FTSENT *);
588
static size_t	 fts_maxarglen(char * const *);
589
static void	 fts_padjust(FTS *, FTSENT *);
590
static int	 fts_palloc(FTS *, size_t);
591
static FTSENT	*fts_sort(FTS *, FTSENT *, int);
592
static u_short	 fts_stat(FTS *, FTSENT *, int, int);
593
static int	 fts_safe_changedir(FTS *, FTSENT *, int, char *);
594

595
/* oconfigure: Prefix with FTS_. */
596

597
#define FTS_MAX(a, b)	(((a) > (b)) ? (a) : (b))
598
#define	FTS_ISDOT(a)		(a[0] == '.' && (!a[1] || (a[1] == '.' && !a[2])))
599
#define	FTS_CLR(opt)		(sp->fts_options &= ~(opt))
600
#define	FTS_ISSET(opt)		(sp->fts_options & (opt))
601
#define	FTS_SET(opt)		(sp->fts_options |= (opt))
602
#define	FTS_FCHDIR(sp, fd)	(!FTS_ISSET(FTS_NOCHDIR) && fchdir(fd))
603
/* fts_build flags */
604
#define	FTS_BCHILD		1 /* fts_children */
605
#define	FTS_BNAMES		2 /* fts_children, names only */
606
#define	FTS_BREAD		3 /* fts_read */
607

608
FTS *
609
fts_open(char * const *argv, int options,
610
    int (*compar)(const FTSENT **, const FTSENT **))
611
{
612
	FTS *sp;
613
	FTSENT *p, *root;
614
	int nitems;
615
	FTSENT *parent, *prev;
616
	char empty[1] = { '\0' };
617

618
	/* Options check. */
619
	if (options & ~FTS_OPTIONMASK) {
620
		errno = EINVAL;
621
		return (NULL);
622
	}
623

624
	/* At least one path must be specified. */
625
	if (*argv == NULL) {
626
		errno = EINVAL;
627
		return (NULL);
628
	}
629

630
	/* Allocate/initialize the stream */
631
	if ((sp = calloc(1, sizeof(FTS))) == NULL)
632
		return (NULL);
633
	sp->fts_compar = compar;
634
	sp->fts_options = options;
635

636
	/* Logical walks turn on NOCHDIR; symbolic links are too hard. */
637
	if (FTS_ISSET(FTS_LOGICAL))
638
		FTS_SET(FTS_NOCHDIR);
639

640
	/*
641
	 * Start out with 1K of path space, and enough, in any case,
642
	 * to hold the user's paths.
643
	 */
644
	if (fts_palloc(sp, FTS_MAX(fts_maxarglen(argv), PATH_MAX)))
645
		goto mem1;
646

647
	/* Allocate/initialize root's parent. */
648
	if ((parent = fts_alloc(sp, empty, 0)) == NULL)
649
		goto mem2;
650
	parent->fts_level = FTS_ROOTPARENTLEVEL;
651

652
	/* Allocate/initialize root(s). */
653
	for (root = prev = NULL, nitems = 0; *argv; ++argv, ++nitems) {
654
		if ((p = fts_alloc(sp, *argv, strlen(*argv))) == NULL)
655
			goto mem3;
656
		p->fts_level = FTS_ROOTLEVEL;
657
		p->fts_parent = parent;
658
		p->fts_accpath = p->fts_name;
659
		p->fts_info = fts_stat(sp, p, FTS_ISSET(FTS_COMFOLLOW), -1);
660

661
		/* Command-line "." and ".." are real directories. */
662
		if (p->fts_info == FTS_DOT)
663
			p->fts_info = FTS_D;
664

665
		/*
666
		 * If comparison routine supplied, traverse in sorted
667
		 * order; otherwise traverse in the order specified.
668
		 */
669
		if (compar) {
670
			p->fts_link = root;
671
			root = p;
672
		} else {
673
			p->fts_link = NULL;
674
			if (root == NULL)
675
				root = p;
676
			else
677
				prev->fts_link = p;
678
			prev = p;
679
		}
680
	}
681
	if (compar && nitems > 1)
682
		root = fts_sort(sp, root, nitems);
683

684
	/*
685
	 * Allocate a dummy pointer and make fts_read think that we've just
686
	 * finished the node before the root(s); set p->fts_info to FTS_INIT
687
	 * so that everything about the "current" node is ignored.
688
	 */
689
	if ((sp->fts_cur = fts_alloc(sp, empty, 0)) == NULL)
690
		goto mem3;
691
	sp->fts_cur->fts_link = root;
692
	sp->fts_cur->fts_info = FTS_INIT;
693

694
	/*
695
	 * If using chdir(2), grab a file descriptor pointing to dot to ensure
696
	 * that we can get back here; this could be avoided for some paths,
697
	 * but almost certainly not worth the effort.  Slashes, symbolic links,
698
	 * and ".." are all fairly nasty problems.  Note, if we can't get the
699
	 * descriptor we run anyway, just more slowly.
700
	 */
701
	if (!FTS_ISSET(FTS_NOCHDIR) &&
702
	    (sp->fts_rfd = open(".", O_RDONLY | O_CLOEXEC)) == -1)
703
		FTS_SET(FTS_NOCHDIR);
704

705
	if (nitems == 0)
706
		free(parent);
707

708
	return (sp);
709

710
mem3:	fts_lfree(root);
711
	free(parent);
712
mem2:	free(sp->fts_path);
713
mem1:	free(sp);
714
	return (NULL);
715
}
716

717
static void
718
fts_load(FTS *sp, FTSENT *p)
719
{
720
	size_t len;
721
	char *cp;
722

723
	/*
724
	 * Load the stream structure for the next traversal.  Since we don't
725
	 * actually enter the directory until after the preorder visit, set
726
	 * the fts_accpath field specially so the chdir gets done to the right
727
	 * place and the user can access the first node.  From fts_open it's
728
	 * known that the path will fit.
729
	 */
730
	len = p->fts_pathlen = p->fts_namelen;
731
	memmove(sp->fts_path, p->fts_name, len + 1);
732
	if ((cp = strrchr(p->fts_name, '/')) && (cp != p->fts_name || cp[1])) {
733
		len = strlen(++cp);
734
		memmove(p->fts_name, cp, len + 1);
735
		p->fts_namelen = len;
736
	}
737
	p->fts_accpath = p->fts_path = sp->fts_path;
738
	sp->fts_dev = p->fts_dev;
739
}
740

741
int
742
fts_close(FTS *sp)
743
{
744
	FTSENT *freep, *p;
745
	int rfd, error = 0;
746

747
	/*
748
	 * This still works if we haven't read anything -- the dummy structure
749
	 * points to the root list, so we step through to the end of the root
750
	 * list which has a valid parent pointer.
751
	 */
752
	if (sp->fts_cur) {
753
		for (p = sp->fts_cur; p->fts_level >= FTS_ROOTLEVEL;) {
754
			freep = p;
755
			p = p->fts_link ? p->fts_link : p->fts_parent;
756
			free(freep);
757
		}
758
		free(p);
759
	}
760

761
	/* Stash the original directory fd if needed. */
762
	rfd = FTS_ISSET(FTS_NOCHDIR) ? -1 : sp->fts_rfd;
763

764
	/* Free up child linked list, sort array, path buffer, stream ptr.*/
765
	if (sp->fts_child)
766
		fts_lfree(sp->fts_child);
767
	free(sp->fts_array);
768
	free(sp->fts_path);
769
	free(sp);
770

771
	/* Return to original directory, checking for error. */
772
	if (rfd != -1) {
773
		int saved_errno;
774
		error = fchdir(rfd);
775
		saved_errno = errno;
776
		(void)close(rfd);
777
		errno = saved_errno;
778
	}
779

780
	return (error);
781
}
782

783
/*
784
 * Special case of "/" at the end of the path so that slashes aren't
785
 * appended which would cause paths to be written as "....//foo".
786
 */
787
#define	NAPPEND(p)							\
788
	(p->fts_path[p->fts_pathlen - 1] == '/'				\
789
	    ? p->fts_pathlen - 1 : p->fts_pathlen)
790

791
FTSENT *
792
fts_read(FTS *sp)
793
{
794
	FTSENT *p, *tmp;
795
	int instr;
796
	char *t;
797
	char up[3] = { '.', '.', '\0' };
798
	int saved_errno;
799

800
	/* If finished or unrecoverable error, return NULL. */
801
	if (sp->fts_cur == NULL || FTS_ISSET(FTS_STOP))
802
		return (NULL);
803

804
	/* Set current node pointer. */
805
	p = sp->fts_cur;
806

807
	/* Save and zero out user instructions. */
808
	instr = p->fts_instr;
809
	p->fts_instr = FTS_NOINSTR;
810

811
	/* Any type of file may be re-visited; re-stat and re-turn. */
812
	if (instr == FTS_AGAIN) {
813
		p->fts_info = fts_stat(sp, p, 0, -1);
814
		return (p);
815
	}
816

817
	/*
818
	 * Following a symlink -- SLNONE test allows application to see
819
	 * SLNONE and recover.  If indirecting through a symlink, have
820
	 * keep a pointer to current location.  If unable to get that
821
	 * pointer, follow fails.
822
	 */
823
	if (instr == FTS_FOLLOW &&
824
	    (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) {
825
		p->fts_info = fts_stat(sp, p, 1, -1);
826
		if (p->fts_info == FTS_D && !FTS_ISSET(FTS_NOCHDIR)) {
827
			if ((p->fts_symfd =
828
			    open(".", O_RDONLY | O_CLOEXEC)) == -1) {
829
				p->fts_errno = errno;
830
				p->fts_info = FTS_ERR;
831
			} else
832
				p->fts_flags |= FTS_SYMFOLLOW;
833
		}
834
		return (p);
835
	}
836

837
	/* Directory in pre-order. */
838
	if (p->fts_info == FTS_D) {
839
		/* If skipped or crossed mount point, do post-order visit. */
840
		if (instr == FTS_SKIP ||
841
		    (FTS_ISSET(FTS_XDEV) && p->fts_dev != sp->fts_dev)) {
842
			if (p->fts_flags & FTS_SYMFOLLOW)
843
				(void)close(p->fts_symfd);
844
			if (sp->fts_child) {
845
				fts_lfree(sp->fts_child);
846
				sp->fts_child = NULL;
847
			}
848
			p->fts_info = FTS_DP;
849
			return (p);
850
		}
851

852
		/* Rebuild if only read the names and now traversing. */
853
		if (sp->fts_child && FTS_ISSET(FTS_NAMEONLY)) {
854
			FTS_CLR(FTS_NAMEONLY);
855
			fts_lfree(sp->fts_child);
856
			sp->fts_child = NULL;
857
		}
858

859
		/*
860
		 * Cd to the subdirectory.
861
		 *
862
		 * If have already read and now fail to chdir, whack the list
863
		 * to make the names come out right, and set the parent errno
864
		 * so the application will eventually get an error condition.
865
		 * Set the FTS_DONTCHDIR flag so that when we logically change
866
		 * directories back to the parent we don't do a chdir.
867
		 *
868
		 * If haven't read do so.  If the read fails, fts_build sets
869
		 * FTS_STOP or the fts_info field of the node.
870
		 */
871
		if (sp->fts_child) {
872
			if (fts_safe_changedir(sp, p, -1, p->fts_accpath)) {
873
				p->fts_errno = errno;
874
				p->fts_flags |= FTS_DONTCHDIR;
875
				for (p = sp->fts_child; p; p = p->fts_link)
876
					p->fts_accpath =
877
					    p->fts_parent->fts_accpath;
878
			}
879
		} else if ((sp->fts_child = fts_build(sp, FTS_BREAD)) == NULL) {
880
			if (FTS_ISSET(FTS_STOP))
881
				return (NULL);
882
			return (p);
883
		}
884
		p = sp->fts_child;
885
		sp->fts_child = NULL;
886
		goto name;
887
	}
888

889
	/* Move to the next node on this level. */
890
next:	tmp = p;
891
	if ((p = p->fts_link)) {
892
		free(tmp);
893

894
		/*
895
		 * If reached the top, return to the original directory (or
896
		 * the root of the tree), and load the paths for the next root.
897
		 */
898
		if (p->fts_level == FTS_ROOTLEVEL) {
899
			if (FTS_FCHDIR(sp, sp->fts_rfd)) {
900
				FTS_SET(FTS_STOP);
901
				return (NULL);
902
			}
903
			fts_load(sp, p);
904
			return (sp->fts_cur = p);
905
		}
906

907
		/*
908
		 * User may have called fts_set on the node.  If skipped,
909
		 * ignore.  If followed, get a file descriptor so we can
910
		 * get back if necessary.
911
		 */
912
		if (p->fts_instr == FTS_SKIP)
913
			goto next;
914
		if (p->fts_instr == FTS_FOLLOW) {
915
			p->fts_info = fts_stat(sp, p, 1, -1);
916
			if (p->fts_info == FTS_D && !FTS_ISSET(FTS_NOCHDIR)) {
917
				if ((p->fts_symfd =
918
				    open(".", O_RDONLY | O_CLOEXEC)) == -1) {
919
					p->fts_errno = errno;
920
					p->fts_info = FTS_ERR;
921
				} else
922
					p->fts_flags |= FTS_SYMFOLLOW;
923
			}
924
			p->fts_instr = FTS_NOINSTR;
925
		}
926

927
name:		t = sp->fts_path + NAPPEND(p->fts_parent);
928
		*t++ = '/';
929
		memmove(t, p->fts_name, p->fts_namelen + 1);
930
		return (sp->fts_cur = p);
931
	}
932

933
	/* Move up to the parent node. */
934
	p = tmp->fts_parent;
935
	free(tmp);
936

937
	if (p->fts_level == FTS_ROOTPARENTLEVEL) {
938
		/*
939
		 * Done; free everything up and set errno to 0 so the user
940
		 * can distinguish between error and EOF.
941
		 */
942
		free(p);
943
		errno = 0;
944
		return (sp->fts_cur = NULL);
945
	}
946

947
	/* NUL terminate the pathname. */
948
	sp->fts_path[p->fts_pathlen] = '\0';
949

950
	/*
951
	 * Return to the parent directory.  If at a root node or came through
952
	 * a symlink, go back through the file descriptor.  Otherwise, cd up
953
	 * one directory.
954
	 */
955
	if (p->fts_level == FTS_ROOTLEVEL) {
956
		if (FTS_FCHDIR(sp, sp->fts_rfd)) {
957
			FTS_SET(FTS_STOP);
958
			sp->fts_cur = p;
959
			return (NULL);
960
		}
961
	} else if (p->fts_flags & FTS_SYMFOLLOW) {
962
		if (FTS_FCHDIR(sp, p->fts_symfd)) {
963
			saved_errno = errno;
964
			(void)close(p->fts_symfd);
965
			errno = saved_errno;
966
			FTS_SET(FTS_STOP);
967
			sp->fts_cur = p;
968
			return (NULL);
969
		}
970
		(void)close(p->fts_symfd);
971
	} else if (!(p->fts_flags & FTS_DONTCHDIR) &&
972
	    fts_safe_changedir(sp, p->fts_parent, -1, up)) {
973
		FTS_SET(FTS_STOP);
974
		sp->fts_cur = p;
975
		return (NULL);
976
	}
977
	p->fts_info = p->fts_errno ? FTS_ERR : FTS_DP;
978
	return (sp->fts_cur = p);
979
}
980

981
/*
982
 * Fts_set takes the stream as an argument although it's not used in this
983
 * implementation; it would be necessary if anyone wanted to add global
984
 * semantics to fts using fts_set.  An error return is allowed for similar
985
 * reasons.
986
 */
987
int
988
fts_set(FTS *sp, FTSENT *p, int instr)
989
{
990
	if (instr && instr != FTS_AGAIN && instr != FTS_FOLLOW &&
991
	    instr != FTS_NOINSTR && instr != FTS_SKIP) {
992
		errno = EINVAL;
993
		return (1);
994
	}
995
	p->fts_instr = instr;
996
	return (0);
997
}
998

999
FTSENT *
1000
fts_children(FTS *sp, int instr)
1001
{
1002
	FTSENT *p;
1003
	int fd;
1004

1005
	if (instr && instr != FTS_NAMEONLY) {
1006
		errno = EINVAL;
1007
		return (NULL);
1008
	}
1009

1010
	/* Set current node pointer. */
1011
	p = sp->fts_cur;
1012

1013
	/*
1014
	 * Errno set to 0 so user can distinguish empty directory from
1015
	 * an error.
1016
	 */
1017
	errno = 0;
1018

1019
	/* Fatal errors stop here. */
1020
	if (FTS_ISSET(FTS_STOP))
1021
		return (NULL);
1022

1023
	/* Return logical hierarchy of user's arguments. */
1024
	if (p->fts_info == FTS_INIT)
1025
		return (p->fts_link);
1026

1027
	/*
1028
	 * If not a directory being visited in pre-order, stop here.  Could
1029
	 * allow FTS_DNR, assuming the user has fixed the problem, but the
1030
	 * same effect is available with FTS_AGAIN.
1031
	 */
1032
	if (p->fts_info != FTS_D /* && p->fts_info != FTS_DNR */)
1033
		return (NULL);
1034

1035
	/* Free up any previous child list. */
1036
	if (sp->fts_child)
1037
		fts_lfree(sp->fts_child);
1038

1039
	if (instr == FTS_NAMEONLY) {
1040
		FTS_SET(FTS_NAMEONLY);
1041
		instr = FTS_BNAMES;
1042
	} else
1043
		instr = FTS_BCHILD;
1044

1045
	/*
1046
	 * If using chdir on a relative path and called BEFORE fts_read does
1047
	 * its chdir to the root of a traversal, we can lose -- we need to
1048
	 * chdir into the subdirectory, and we don't know where the current
1049
	 * directory is, so we can't get back so that the upcoming chdir by
1050
	 * fts_read will work.
1051
	 */
1052
	if (p->fts_level != FTS_ROOTLEVEL || p->fts_accpath[0] == '/' ||
1053
	    FTS_ISSET(FTS_NOCHDIR))
1054
		return (sp->fts_child = fts_build(sp, instr));
1055

1056
	if ((fd = open(".", O_RDONLY | O_CLOEXEC)) == -1)
1057
		return (NULL);
1058
	sp->fts_child = fts_build(sp, instr);
1059
	if (fchdir(fd)) {
1060
		(void)close(fd);
1061
		return (NULL);
1062
	}
1063
	(void)close(fd);
1064
	return (sp->fts_child);
1065
}
1066

1067
/*
1068
 * This is the tricky part -- do not casually change *anything* in here.  The
1069
 * idea is to build the linked list of entries that are used by fts_children
1070
 * and fts_read.  There are lots of special cases.
1071
 *
1072
 * The real slowdown in walking the tree is the stat calls.  If FTS_NOSTAT is
1073
 * set and it's a physical walk (so that symbolic links can't be directories),
1074
 * we can do things quickly.  First, if it's a 4.4BSD file system, the type
1075
 * of the file is in the directory entry.  Otherwise, we assume that the number
1076
 * of subdirectories in a node is equal to the number of links to the parent.
1077
 * The former skips all stat calls.  The latter skips stat calls in any leaf
1078
 * directories and for any files after the subdirectories in the directory have
1079
 * been found, cutting the stat calls by about 2/3.
1080
 */
1081
static FTSENT *
1082
fts_build(FTS *sp, int type)
1083
{
1084
	struct dirent *dp;
1085
	FTSENT *p, *head;
1086
	FTSENT *cur, *tail;
1087
	DIR *dirp;
1088
	void *oldaddr;
1089
	size_t len, maxlen, namlen;
1090
	int nitems, cderrno, descend, level, nlinks, nostat, doadjust;
1091
	int saved_errno;
1092
	char *cp;
1093
	char up[3] = { '.', '.', '\0' };
1094

1095
	/* Set current node pointer. */
1096
	cur = sp->fts_cur;
1097

1098
	/*
1099
	 * Open the directory for reading.  If this fails, we're done.
1100
	 * If being called from fts_read, set the fts_info field.
1101
	 */
1102
	if ((dirp = opendir(cur->fts_accpath)) == NULL) {
1103
		if (type == FTS_BREAD) {
1104
			cur->fts_info = FTS_DNR;
1105
			cur->fts_errno = errno;
1106
		}
1107
		return (NULL);
1108
	}
1109

1110
	/*
1111
	 * Nlinks is the number of possible entries of type directory in the
1112
	 * directory if we're cheating on stat calls, 0 if we're not doing
1113
	 * any stat calls at all, -1 if we're doing stats on everything.
1114
	 */
1115
	if (type == FTS_BNAMES)
1116
		nlinks = 0;
1117
	else if (FTS_ISSET(FTS_NOSTAT) && FTS_ISSET(FTS_PHYSICAL)) {
1118
		nlinks = cur->fts_nlink - (FTS_ISSET(FTS_SEEDOT) ? 0 : 2);
1119
		nostat = 1;
1120
	} else {
1121
		nlinks = -1;
1122
		nostat = 0;
1123
	}
1124

1125
#ifdef notdef
1126
	(void)printf("nlinks == %d (cur: %u)\n", nlinks, cur->fts_nlink);
1127
	(void)printf("NOSTAT %d PHYSICAL %d SEEDOT %d\n",
1128
	    FTS_ISSET(FTS_NOSTAT), FTS_ISSET(FTS_PHYSICAL), FTS_ISSET(FTS_SEEDOT));
1129
#endif
1130
	/*
1131
	 * If we're going to need to stat anything or we want to descend
1132
	 * and stay in the directory, chdir.  If this fails we keep going,
1133
	 * but set a flag so we don't chdir after the post-order visit.
1134
	 * We won't be able to stat anything, but we can still return the
1135
	 * names themselves.  Note, that since fts_read won't be able to
1136
	 * chdir into the directory, it will have to return different path
1137
	 * names than before, i.e. "a/b" instead of "b".  Since the node
1138
	 * has already been visited in pre-order, have to wait until the
1139
	 * post-order visit to return the error.  There is a special case
1140
	 * here, if there was nothing to stat then it's not an error to
1141
	 * not be able to stat.  This is all fairly nasty.  If a program
1142
	 * needed sorted entries or stat information, they had better be
1143
	 * checking FTS_NS on the returned nodes.
1144
	 */
1145
	cderrno = 0;
1146
	if (nlinks || type == FTS_BREAD) {
1147
		if (fts_safe_changedir(sp, cur, dirfd(dirp), NULL)) {
1148
			if (nlinks && type == FTS_BREAD)
1149
				cur->fts_errno = errno;
1150
			cur->fts_flags |= FTS_DONTCHDIR;
1151
			descend = 0;
1152
			cderrno = errno;
1153
			(void)closedir(dirp);
1154
			dirp = NULL;
1155
		} else
1156
			descend = 1;
1157
	} else
1158
		descend = 0;
1159

1160
	/*
1161
	 * Figure out the max file name length that can be stored in the
1162
	 * current path -- the inner loop allocates more path as necessary.
1163
	 * We really wouldn't have to do the maxlen calculations here, we
1164
	 * could do them in fts_read before returning the path, but it's a
1165
	 * lot easier here since the length is part of the dirent structure.
1166
	 *
1167
	 * If not changing directories set a pointer so that can just append
1168
	 * each new name into the path.
1169
	 */
1170
	len = NAPPEND(cur);
1171
	if (FTS_ISSET(FTS_NOCHDIR)) {
1172
		cp = sp->fts_path + len;
1173
		*cp++ = '/';
1174
	}
1175
	len++;
1176
	maxlen = sp->fts_pathlen - len;
1177

1178
	/*
1179
	 * fts_level is signed so we must prevent it from wrapping
1180
	 * around to FTS_ROOTLEVEL and FTS_ROOTPARENTLEVEL.
1181
	 */
1182
	level = cur->fts_level;
1183
	if (level < FTS_MAXLEVEL)
1184
	    level++;
1185

1186
	/* Read the directory, attaching each entry to the `link' pointer. */
1187
	doadjust = 0;
1188
	for (head = tail = NULL, nitems = 0; dirp && (dp = readdir(dirp));) {
1189
		if (!FTS_ISSET(FTS_SEEDOT) && FTS_ISDOT(dp->d_name))
1190
			continue;
1191

1192
		namlen = strlen(dp->d_name);
1193

1194
		if (!(p = fts_alloc(sp, dp->d_name, namlen)))
1195
			goto mem1;
1196
		if (namlen >= maxlen) {	/* include space for NUL */
1197
			oldaddr = sp->fts_path;
1198
			if (fts_palloc(sp, namlen +len + 1)) {
1199
				/*
1200
				 * No more memory for path or structures.  Save
1201
				 * errno, free up the current structure and the
1202
				 * structures already allocated.
1203
				 */
1204
mem1:				saved_errno = errno;
1205
				free(p);
1206
				fts_lfree(head);
1207
				(void)closedir(dirp);
1208
				cur->fts_info = FTS_ERR;
1209
				FTS_SET(FTS_STOP);
1210
				errno = saved_errno;
1211
				return (NULL);
1212
			}
1213
			/* Did realloc() change the pointer? */
1214
			if (oldaddr != sp->fts_path) {
1215
				doadjust = 1;
1216
				if (FTS_ISSET(FTS_NOCHDIR))
1217
					cp = sp->fts_path + len;
1218
			}
1219
			maxlen = sp->fts_pathlen - len;
1220
		}
1221

1222
		p->fts_level = level;
1223
		p->fts_parent = sp->fts_cur;
1224
		p->fts_pathlen = len + namlen;
1225
		if (p->fts_pathlen < len) {
1226
			/*
1227
			 * If we wrap, free up the current structure and
1228
			 * the structures already allocated, then error
1229
			 * out with ENAMETOOLONG.
1230
			 */
1231
			free(p);
1232
			fts_lfree(head);
1233
			(void)closedir(dirp);
1234
			cur->fts_info = FTS_ERR;
1235
			FTS_SET(FTS_STOP);
1236
			errno = ENAMETOOLONG;
1237
			return (NULL);
1238
		}
1239

1240
		if (cderrno) {
1241
			if (nlinks) {
1242
				p->fts_info = FTS_NS;
1243
				p->fts_errno = cderrno;
1244
			} else
1245
				p->fts_info = FTS_NSOK;
1246
			p->fts_accpath = cur->fts_accpath;
1247
		} else if (nlinks == 0
1248
#ifdef DT_DIR
1249
		    || (nostat &&
1250
		    dp->d_type != DT_DIR && dp->d_type != DT_UNKNOWN)
1251
#endif
1252
		    ) {
1253
			p->fts_accpath =
1254
			    FTS_ISSET(FTS_NOCHDIR) ? p->fts_path : p->fts_name;
1255
			p->fts_info = FTS_NSOK;
1256
		} else {
1257
			/* Build a file name for fts_stat to stat. */
1258
			if (FTS_ISSET(FTS_NOCHDIR)) {
1259
				p->fts_accpath = p->fts_path;
1260
				memmove(cp, p->fts_name, p->fts_namelen + 1);
1261
				p->fts_info = fts_stat(sp, p, 0, dirfd(dirp));
1262
			} else {
1263
				p->fts_accpath = p->fts_name;
1264
				p->fts_info = fts_stat(sp, p, 0, -1);
1265
			}
1266

1267
			/* Decrement link count if applicable. */
1268
			if (nlinks > 0 && (p->fts_info == FTS_D ||
1269
			    p->fts_info == FTS_DC || p->fts_info == FTS_DOT))
1270
				--nlinks;
1271
		}
1272

1273
		/* We walk in directory order so "ls -f" doesn't get upset. */
1274
		p->fts_link = NULL;
1275
		if (head == NULL)
1276
			head = tail = p;
1277
		else {
1278
			tail->fts_link = p;
1279
			tail = p;
1280
		}
1281
		++nitems;
1282
	}
1283
	if (dirp)
1284
		(void)closedir(dirp);
1285

1286
	/*
1287
	 * If realloc() changed the address of the path, adjust the
1288
	 * addresses for the rest of the tree and the dir list.
1289
	 */
1290
	if (doadjust)
1291
		fts_padjust(sp, head);
1292

1293
	/*
1294
	 * If not changing directories, reset the path back to original
1295
	 * state.
1296
	 */
1297
	if (FTS_ISSET(FTS_NOCHDIR)) {
1298
		if (len == sp->fts_pathlen || nitems == 0)
1299
			--cp;
1300
		*cp = '\0';
1301
	}
1302

1303
	/*
1304
	 * If descended after called from fts_children or after called from
1305
	 * fts_read and nothing found, get back.  At the root level we use
1306
	 * the saved fd; if one of fts_open()'s arguments is a relative path
1307
	 * to an empty directory, we wind up here with no other way back.  If
1308
	 * can't get back, we're done.
1309
	 */
1310
	if (descend && (type == FTS_BCHILD || !nitems) &&
1311
	    (cur->fts_level == FTS_ROOTLEVEL ? FTS_FCHDIR(sp, sp->fts_rfd) :
1312
	    fts_safe_changedir(sp, cur->fts_parent, -1, up))) {
1313
		cur->fts_info = FTS_ERR;
1314
		FTS_SET(FTS_STOP);
1315
		return (NULL);
1316
	}
1317

1318
	/* If didn't find anything, return NULL. */
1319
	if (!nitems) {
1320
		if (type == FTS_BREAD)
1321
			cur->fts_info = FTS_DP;
1322
		return (NULL);
1323
	}
1324

1325
	/* Sort the entries. */
1326
	if (sp->fts_compar && nitems > 1)
1327
		head = fts_sort(sp, head, nitems);
1328
	return (head);
1329
}
1330

1331
static u_short
1332
fts_stat(FTS *sp, FTSENT *p, int follow, int dfd)
1333
{
1334
	FTSENT *t;
1335
	dev_t dev;
1336
	ino_t ino;
1337
	struct stat *sbp, sb;
1338
	int saved_errno;
1339
	const char *path;
1340

1341
	if (dfd == -1) {
1342
		path = p->fts_accpath;
1343
		dfd = AT_FDCWD;
1344
	} else
1345
		path = p->fts_name;
1346

1347
	/* If user needs stat info, stat buffer already allocated. */
1348
	sbp = FTS_ISSET(FTS_NOSTAT) ? &sb : p->fts_statp;
1349

1350
	/*
1351
	 * If doing a logical walk, or application requested FTS_FOLLOW, do
1352
	 * a stat(2).  If that fails, check for a non-existent symlink.  If
1353
	 * fail, set the errno from the stat call.
1354
	 */
1355
	if (FTS_ISSET(FTS_LOGICAL) || follow) {
1356
		if (fstatat(dfd, path, sbp, 0)) {
1357
			saved_errno = errno;
1358
			if (!fstatat(dfd, path, sbp, AT_SYMLINK_NOFOLLOW)) {
1359
				errno = 0;
1360
				return (FTS_SLNONE);
1361
			}
1362
			p->fts_errno = saved_errno;
1363
			goto err;
1364
		}
1365
	} else if (fstatat(dfd, path, sbp, AT_SYMLINK_NOFOLLOW)) {
1366
		p->fts_errno = errno;
1367
err:		memset(sbp, 0, sizeof(struct stat));
1368
		return (FTS_NS);
1369
	}
1370

1371
	if (S_ISDIR(sbp->st_mode)) {
1372
		/*
1373
		 * Set the device/inode.  Used to find cycles and check for
1374
		 * crossing mount points.  Also remember the link count, used
1375
		 * in fts_build to limit the number of stat calls.  It is
1376
		 * understood that these fields are only referenced if fts_info
1377
		 * is set to FTS_D.
1378
		 */
1379
		dev = p->fts_dev = sbp->st_dev;
1380
		ino = p->fts_ino = sbp->st_ino;
1381
		p->fts_nlink = sbp->st_nlink;
1382

1383
		if (FTS_ISDOT(p->fts_name))
1384
			return (FTS_DOT);
1385

1386
		/*
1387
		 * Cycle detection is done by brute force when the directory
1388
		 * is first encountered.  If the tree gets deep enough or the
1389
		 * number of symbolic links to directories is high enough,
1390
		 * something faster might be worthwhile.
1391
		 */
1392
		for (t = p->fts_parent;
1393
		    t->fts_level >= FTS_ROOTLEVEL; t = t->fts_parent)
1394
			if (ino == t->fts_ino && dev == t->fts_dev) {
1395
				p->fts_cycle = t;
1396
				return (FTS_DC);
1397
			}
1398
		return (FTS_D);
1399
	}
1400
	if (S_ISLNK(sbp->st_mode))
1401
		return (FTS_SL);
1402
	if (S_ISREG(sbp->st_mode))
1403
		return (FTS_F);
1404
	return (FTS_DEFAULT);
1405
}
1406

1407
static FTSENT *
1408
fts_sort(FTS *sp, FTSENT *head, int nitems)
1409
{
1410
	FTSENT **ap, *p;
1411

1412
	/*
1413
	 * Construct an array of pointers to the structures and call qsort(3).
1414
	 * Reassemble the array in the order returned by qsort.  If unable to
1415
	 * sort for memory reasons, return the directory entries in their
1416
	 * current order.  Allocate enough space for the current needs plus
1417
	 * 40 so don't realloc one entry at a time.
1418
	 */
1419
	if (nitems > sp->fts_nitems) {
1420
		struct _ftsent **a;
1421

1422
		if ((a = reallocarray(sp->fts_array,
1423
		    nitems + 40, sizeof(FTSENT *))) == NULL) {
1424
			free(sp->fts_array);
1425
			sp->fts_array = NULL;
1426
			sp->fts_nitems = 0;
1427
			return (head);
1428
		}
1429
		sp->fts_nitems = nitems + 40;
1430
		sp->fts_array = a;
1431
	}
1432
	for (ap = sp->fts_array, p = head; p; p = p->fts_link)
1433
		*ap++ = p;
1434
	qsort(sp->fts_array, nitems, sizeof(FTSENT *),
1435
		(int(*)(const void *, const void *))sp->fts_compar);
1436
	for (head = *(ap = sp->fts_array); --nitems; ++ap)
1437
		ap[0]->fts_link = ap[1];
1438
	ap[0]->fts_link = NULL;
1439
	return (head);
1440
}
1441

1442
static FTSENT *
1443
fts_alloc(FTS *sp, char *name, size_t namelen)
1444
{
1445
	FTSENT *p;
1446
	size_t len;
1447

1448
	/*
1449
	 * The file name is a variable length array and no stat structure is
1450
	 * necessary if the user has set the nostat bit.  Allocate the FTSENT
1451
	 * structure, the file name and the stat structure in one chunk, but
1452
	 * be careful that the stat structure is reasonably aligned.  Since the
1453
	 * fts_name field is declared to be of size 1, the fts_name pointer is
1454
	 * namelen + 2 before the first possible address of the stat structure.
1455
	 */
1456
	len = sizeof(FTSENT) + namelen;
1457
	if (!FTS_ISSET(FTS_NOSTAT))
1458
		len += sizeof(struct stat) + FTS_ALIGNBYTES;
1459
	if ((p = calloc(1, len)) == NULL)
1460
		return (NULL);
1461

1462
	p->fts_path = sp->fts_path;
1463
	p->fts_namelen = namelen;
1464
	p->fts_instr = FTS_NOINSTR;
1465
	if (!FTS_ISSET(FTS_NOSTAT))
1466
		p->fts_statp = (struct stat *)FTS_ALIGN(p->fts_name + namelen + 2);
1467
	memcpy(p->fts_name, name, namelen);
1468

1469
	return (p);
1470
}
1471

1472
static void
1473
fts_lfree(FTSENT *head)
1474
{
1475
	FTSENT *p;
1476

1477
	/* Free a linked list of structures. */
1478
	while ((p = head)) {
1479
		head = head->fts_link;
1480
		free(p);
1481
	}
1482
}
1483

1484
/*
1485
 * Allow essentially unlimited paths; find, rm, ls should all work on any tree.
1486
 * Most systems will allow creation of paths much longer than PATH_MAX, even
1487
 * though the kernel won't resolve them.  Add the size (not just what's needed)
1488
 * plus 256 bytes so don't realloc the path 2 bytes at a time.
1489
 */
1490
static int
1491
fts_palloc(FTS *sp, size_t more)
1492
{
1493
	char *p;
1494

1495
	/*
1496
	 * Check for possible wraparound.
1497
	 */
1498
	more += 256;
1499
	if (sp->fts_pathlen + more < sp->fts_pathlen) {
1500
		free(sp->fts_path);
1501
		sp->fts_path = NULL;
1502
		errno = ENAMETOOLONG;
1503
		return (1);
1504
	}
1505
	p = recallocarray(sp->fts_path, sp->fts_pathlen,
1506
	    sp->fts_pathlen + more, 1);
1507
	if (p == NULL) {
1508
		free(sp->fts_path);
1509
		sp->fts_path = NULL;
1510
		return (1);
1511
	}
1512
	sp->fts_pathlen += more;
1513
	sp->fts_path = p;
1514
	return (0);
1515
}
1516

1517
/*
1518
 * When the path is realloc'd, have to fix all of the pointers in structures
1519
 * already returned.
1520
 */
1521
static void
1522
fts_padjust(FTS *sp, FTSENT *head)
1523
{
1524
	FTSENT *p;
1525
	char *addr = sp->fts_path;
1526

1527
#define	ADJUST(p) {							\
1528
	if ((p)->fts_accpath != (p)->fts_name) {			\
1529
		(p)->fts_accpath =					\
1530
		    (char *)addr + ((p)->fts_accpath - (p)->fts_path);	\
1531
	}								\
1532
	(p)->fts_path = addr;						\
1533
}
1534
	/* Adjust the current set of children. */
1535
	for (p = sp->fts_child; p; p = p->fts_link)
1536
		ADJUST(p);
1537

1538
	/* Adjust the rest of the tree, including the current level. */
1539
	for (p = head; p->fts_level >= FTS_ROOTLEVEL;) {
1540
		ADJUST(p);
1541
		p = p->fts_link ? p->fts_link : p->fts_parent;
1542
	}
1543
}
1544

1545
static size_t
1546
fts_maxarglen(char * const *argv)
1547
{
1548
	size_t len, max;
1549

1550
	for (max = 0; *argv; ++argv)
1551
		if ((len = strlen(*argv)) > max)
1552
			max = len;
1553
	return (max + 1);
1554
}
1555

1556
/*
1557
 * Change to dir specified by fd or p->fts_accpath without getting
1558
 * tricked by someone changing the world out from underneath us.
1559
 * Assumes p->fts_dev and p->fts_ino are filled in.
1560
 */
1561
static int
1562
fts_safe_changedir(FTS *sp, FTSENT *p, int fd, char *path)
1563
{
1564
	int ret, oerrno, newfd;
1565
	struct stat sb;
1566

1567
	newfd = fd;
1568
	if (FTS_ISSET(FTS_NOCHDIR))
1569
		return (0);
1570
	if (fd == -1 && (newfd = open(path, O_RDONLY|O_DIRECTORY|O_CLOEXEC)) == -1)
1571
		return (-1);
1572
	if (fstat(newfd, &sb) == -1) {
1573
		ret = -1;
1574
		goto bail;
1575
	}
1576
	if (p->fts_dev != sb.st_dev || p->fts_ino != sb.st_ino) {
1577
		errno = ENOENT;		/* disinformation */
1578
		ret = -1;
1579
		goto bail;
1580
	}
1581
	ret = fchdir(newfd);
1582
bail:
1583
	oerrno = errno;
1584
	if (fd == -1)
1585
		(void)close(newfd);
1586
	errno = oerrno;
1587
	return (ret);
1588
}
1589
#endif /* !HAVE_FTS */
1590
#if !HAVE_GETPROGNAME
1591
/*
1592
 * Copyright (c) 2016 Nicholas Marriott <nicholas.marriott@gmail.com>
1593
 * Copyright (c) 2017 Kristaps Dzonsons <kristaps@bsd.lv>
1594
 * Copyright (c) 2020 Stephen Gregoratto <dev@sgregoratto.me>
1595
 *
1596
 * Permission to use, copy, modify, and distribute this software for any
1597
 * purpose with or without fee is hereby granted, provided that the above
1598
 * copyright notice and this permission notice appear in all copies.
1599
 *
1600
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1601
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1602
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1603
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1604
 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
1605
 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
1606
 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1607
 */
1608

1609
#include <sys/types.h>
1610

1611
#include <errno.h>
1612

1613
#if HAVE_GETEXECNAME
1614
#include <stdlib.h>
1615
const char *
1616
getprogname(void)
1617
{
1618
	return getexecname();
1619
}
1620
#elif HAVE_PROGRAM_INVOCATION_SHORT_NAME
1621
const char *
1622
getprogname(void)
1623
{
1624
	return (program_invocation_short_name);
1625
}
1626
#elif HAVE___PROGNAME
1627
const char *
1628
getprogname(void)
1629
{
1630
	extern char	*__progname;
1631

1632
	return (__progname);
1633
}
1634
#else
1635
#error No getprogname available.
1636
#endif
1637
#endif /* !HAVE_GETPROGNAME */
1638
#if !HAVE_MD5
1639
/*
1640
 * This code implements the MD5 message-digest algorithm.
1641
 * The algorithm is due to Ron Rivest.	This code was
1642
 * written by Colin Plumb in 1993, no copyright is claimed.
1643
 * This code is in the public domain; do with it what you wish.
1644
 *
1645
 * Equivalent code is available from RSA Data Security, Inc.
1646
 * This code has been tested against that, and is equivalent,
1647
 * except that you don't need to include two pages of legalese
1648
 * with every copy.
1649
 *
1650
 * To compute the message digest of a chunk of bytes, declare an
1651
 * MD5Context structure, pass it to MD5Init, call MD5Update as
1652
 * needed on buffers full of bytes, and then call MD5Final, which
1653
 * will fill a supplied 16-byte array with the digest.
1654
 */
1655

1656
#include <sys/types.h>
1657
#include <stdlib.h>
1658
#include <string.h>
1659

1660
#ifndef BYTE_ORDER
1661
# if defined(LITTLE_ENDIAN) || defined(BIG_ENDIAN)
1662
#  error Confusion in endian macros.
1663
# endif
1664
# if !defined(__BYTE_ORDER__)
1665
#  error Byte order macro not found.
1666
# endif
1667
# if !defined(__ORDER_LITTLE_ENDIAN__) || !defined(__ORDER_BIG_ENDIAN__)
1668
#  error Little/big endian macros not found.
1669
# endif
1670
# define BYTE_ORDER __BYTE_ORDER__
1671
# define LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__
1672
# define BIG_ENDIAN __ORDER_BIG_ENDIAN__
1673
#endif /*!BYTE_ORDER*/
1674

1675
#define PUT_64BIT_LE(cp, value) do {					\
1676
	(cp)[7] = (value) >> 56;					\
1677
	(cp)[6] = (value) >> 48;					\
1678
	(cp)[5] = (value) >> 40;					\
1679
	(cp)[4] = (value) >> 32;					\
1680
	(cp)[3] = (value) >> 24;					\
1681
	(cp)[2] = (value) >> 16;					\
1682
	(cp)[1] = (value) >> 8;						\
1683
	(cp)[0] = (value); } while (0)
1684

1685
#define PUT_32BIT_LE(cp, value) do {					\
1686
	(cp)[3] = (value) >> 24;					\
1687
	(cp)[2] = (value) >> 16;					\
1688
	(cp)[1] = (value) >> 8;						\
1689
	(cp)[0] = (value); } while (0)
1690

1691
static uint8_t PADDING[MD5_BLOCK_LENGTH] = {
1692
	0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1693
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1694
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
1695
};
1696

1697
/*
1698
 * Start MD5 accumulation.  Set bit count to 0 and buffer to mysterious
1699
 * initialization constants.
1700
 */
1701
void
1702
MD5Init(MD5_CTX *ctx)
1703
{
1704
	ctx->count = 0;
1705
	ctx->state[0] = 0x67452301;
1706
	ctx->state[1] = 0xefcdab89;
1707
	ctx->state[2] = 0x98badcfe;
1708
	ctx->state[3] = 0x10325476;
1709
}
1710

1711
/*
1712
 * Update context to reflect the concatenation of another buffer full
1713
 * of bytes.
1714
 */
1715
void
1716
MD5Update(MD5_CTX *ctx, const unsigned char *input, size_t len)
1717
{
1718
	size_t have, need;
1719

1720
	/* Check how many bytes we already have and how many more we need. */
1721
	have = (size_t)((ctx->count >> 3) & (MD5_BLOCK_LENGTH - 1));
1722
	need = MD5_BLOCK_LENGTH - have;
1723

1724
	/* Update bitcount */
1725
	ctx->count += (uint64_t)len << 3;
1726

1727
	if (len >= need) {
1728
		if (have != 0) {
1729
			memcpy(ctx->buffer + have, input, need);
1730
			MD5Transform(ctx->state, ctx->buffer);
1731
			input += need;
1732
			len -= need;
1733
			have = 0;
1734
		}
1735

1736
		/* Process data in MD5_BLOCK_LENGTH-byte chunks. */
1737
		while (len >= MD5_BLOCK_LENGTH) {
1738
			MD5Transform(ctx->state, input);
1739
			input += MD5_BLOCK_LENGTH;
1740
			len -= MD5_BLOCK_LENGTH;
1741
		}
1742
	}
1743

1744
	/* Handle any remaining bytes of data. */
1745
	if (len != 0)
1746
		memcpy(ctx->buffer + have, input, len);
1747
}
1748

1749
/*
1750
 * Pad pad to 64-byte boundary with the bit pattern
1751
 * 1 0* (64-bit count of bits processed, MSB-first)
1752
 */
1753
void
1754
MD5Pad(MD5_CTX *ctx)
1755
{
1756
	uint8_t count[8];
1757
	size_t padlen;
1758

1759
	/* Convert count to 8 bytes in little endian order. */
1760
	PUT_64BIT_LE(count, ctx->count);
1761

1762
	/* Pad out to 56 mod 64. */
1763
	padlen = MD5_BLOCK_LENGTH -
1764
	    ((ctx->count >> 3) & (MD5_BLOCK_LENGTH - 1));
1765
	if (padlen < 1 + 8)
1766
		padlen += MD5_BLOCK_LENGTH;
1767
	MD5Update(ctx, PADDING, padlen - 8);		/* padlen - 8 <= 64 */
1768
	MD5Update(ctx, count, 8);
1769
}
1770

1771
/*
1772
 * Final wrapup--call MD5Pad, fill in digest and zero out ctx.
1773
 */
1774
void
1775
MD5Final(unsigned char digest[MD5_DIGEST_LENGTH], MD5_CTX *ctx)
1776
{
1777
	int i;
1778

1779
	MD5Pad(ctx);
1780
	for (i = 0; i < 4; i++)
1781
		PUT_32BIT_LE(digest + i * 4, ctx->state[i]);
1782
	memset(ctx, 0, sizeof(*ctx));
1783
}
1784

1785

1786
/* The four core functions - F1 is optimized somewhat */
1787

1788
/* #define F1(x, y, z) (x & y | ~x & z) */
1789
#define F1(x, y, z) (z ^ (x & (y ^ z)))
1790
#define F2(x, y, z) F1(z, x, y)
1791
#define F3(x, y, z) (x ^ y ^ z)
1792
#define F4(x, y, z) (y ^ (x | ~z))
1793

1794
/* This is the central step in the MD5 algorithm. */
1795
#define MD5STEP(f, w, x, y, z, data, s) \
1796
	( w += f(x, y, z) + data,  w = w<<s | w>>(32-s),  w += x )
1797

1798
/*
1799
 * The core of the MD5 algorithm, this alters an existing MD5 hash to
1800
 * reflect the addition of 16 longwords of new data.  MD5Update blocks
1801
 * the data and converts bytes into longwords for this routine.
1802
 */
1803
void
1804
MD5Transform(uint32_t state[4], const uint8_t block[MD5_BLOCK_LENGTH])
1805
{
1806
	uint32_t a, b, c, d, in[MD5_BLOCK_LENGTH / 4];
1807

1808
#if BYTE_ORDER == LITTLE_ENDIAN
1809
	memcpy(in, block, sizeof(in));
1810
#else
1811
	for (a = 0; a < MD5_BLOCK_LENGTH / 4; a++) {
1812
		in[a] = (uint32_t)(
1813
		    (uint32_t)(block[a * 4 + 0]) |
1814
		    (uint32_t)(block[a * 4 + 1]) <<  8 |
1815
		    (uint32_t)(block[a * 4 + 2]) << 16 |
1816
		    (uint32_t)(block[a * 4 + 3]) << 24);
1817
	}
1818
#endif
1819

1820
	a = state[0];
1821
	b = state[1];
1822
	c = state[2];
1823
	d = state[3];
1824

1825
	MD5STEP(F1, a, b, c, d, in[ 0] + 0xd76aa478,  7);
1826
	MD5STEP(F1, d, a, b, c, in[ 1] + 0xe8c7b756, 12);
1827
	MD5STEP(F1, c, d, a, b, in[ 2] + 0x242070db, 17);
1828
	MD5STEP(F1, b, c, d, a, in[ 3] + 0xc1bdceee, 22);
1829
	MD5STEP(F1, a, b, c, d, in[ 4] + 0xf57c0faf,  7);
1830
	MD5STEP(F1, d, a, b, c, in[ 5] + 0x4787c62a, 12);
1831
	MD5STEP(F1, c, d, a, b, in[ 6] + 0xa8304613, 17);
1832
	MD5STEP(F1, b, c, d, a, in[ 7] + 0xfd469501, 22);
1833
	MD5STEP(F1, a, b, c, d, in[ 8] + 0x698098d8,  7);
1834
	MD5STEP(F1, d, a, b, c, in[ 9] + 0x8b44f7af, 12);
1835
	MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
1836
	MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
1837
	MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122,  7);
1838
	MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
1839
	MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
1840
	MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
1841

1842
	MD5STEP(F2, a, b, c, d, in[ 1] + 0xf61e2562,  5);
1843
	MD5STEP(F2, d, a, b, c, in[ 6] + 0xc040b340,  9);
1844
	MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
1845
	MD5STEP(F2, b, c, d, a, in[ 0] + 0xe9b6c7aa, 20);
1846
	MD5STEP(F2, a, b, c, d, in[ 5] + 0xd62f105d,  5);
1847
	MD5STEP(F2, d, a, b, c, in[10] + 0x02441453,  9);
1848
	MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
1849
	MD5STEP(F2, b, c, d, a, in[ 4] + 0xe7d3fbc8, 20);
1850
	MD5STEP(F2, a, b, c, d, in[ 9] + 0x21e1cde6,  5);
1851
	MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6,  9);
1852
	MD5STEP(F2, c, d, a, b, in[ 3] + 0xf4d50d87, 14);
1853
	MD5STEP(F2, b, c, d, a, in[ 8] + 0x455a14ed, 20);
1854
	MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905,  5);
1855
	MD5STEP(F2, d, a, b, c, in[ 2] + 0xfcefa3f8,  9);
1856
	MD5STEP(F2, c, d, a, b, in[ 7] + 0x676f02d9, 14);
1857
	MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
1858

1859
	MD5STEP(F3, a, b, c, d, in[ 5] + 0xfffa3942,  4);
1860
	MD5STEP(F3, d, a, b, c, in[ 8] + 0x8771f681, 11);
1861
	MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
1862
	MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
1863
	MD5STEP(F3, a, b, c, d, in[ 1] + 0xa4beea44,  4);
1864
	MD5STEP(F3, d, a, b, c, in[ 4] + 0x4bdecfa9, 11);
1865
	MD5STEP(F3, c, d, a, b, in[ 7] + 0xf6bb4b60, 16);
1866
	MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
1867
	MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6,  4);
1868
	MD5STEP(F3, d, a, b, c, in[ 0] + 0xeaa127fa, 11);
1869
	MD5STEP(F3, c, d, a, b, in[ 3] + 0xd4ef3085, 16);
1870
	MD5STEP(F3, b, c, d, a, in[ 6] + 0x04881d05, 23);
1871
	MD5STEP(F3, a, b, c, d, in[ 9] + 0xd9d4d039,  4);
1872
	MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
1873
	MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
1874
	MD5STEP(F3, b, c, d, a, in[2 ] + 0xc4ac5665, 23);
1875

1876
	MD5STEP(F4, a, b, c, d, in[ 0] + 0xf4292244,  6);
1877
	MD5STEP(F4, d, a, b, c, in[7 ] + 0x432aff97, 10);
1878
	MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
1879
	MD5STEP(F4, b, c, d, a, in[5 ] + 0xfc93a039, 21);
1880
	MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3,  6);
1881
	MD5STEP(F4, d, a, b, c, in[3 ] + 0x8f0ccc92, 10);
1882
	MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
1883
	MD5STEP(F4, b, c, d, a, in[1 ] + 0x85845dd1, 21);
1884
	MD5STEP(F4, a, b, c, d, in[8 ] + 0x6fa87e4f,  6);
1885
	MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
1886
	MD5STEP(F4, c, d, a, b, in[6 ] + 0xa3014314, 15);
1887
	MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
1888
	MD5STEP(F4, a, b, c, d, in[4 ] + 0xf7537e82,  6);
1889
	MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
1890
	MD5STEP(F4, c, d, a, b, in[2 ] + 0x2ad7d2bb, 15);
1891
	MD5STEP(F4, b, c, d, a, in[9 ] + 0xeb86d391, 21);
1892

1893
	state[0] += a;
1894
	state[1] += b;
1895
	state[2] += c;
1896
	state[3] += d;
1897
}
1898

1899
char *
1900
MD5End(MD5_CTX *ctx, char *buf)
1901
{
1902
	int i;
1903
	unsigned char digest[MD5_DIGEST_LENGTH];
1904
	static const char hex[]="0123456789abcdef";
1905

1906
	if (!buf)
1907
		buf = malloc(2*MD5_DIGEST_LENGTH + 1);
1908
	if (!buf)
1909
		return 0;
1910
	MD5Final(digest, ctx);
1911
	for (i = 0; i < MD5_DIGEST_LENGTH; i++) {
1912
		buf[i+i] = hex[digest[i] >> 4];
1913
		buf[i+i+1] = hex[digest[i] & 0x0f];
1914
	}
1915
	buf[i+i] = '\0';
1916
	return buf;
1917
}
1918
#endif /* !HAVE_MD5 */
1919
#if !HAVE_MEMMEM
1920
/*-
1921
 * Copyright (c) 2005 Pascal Gloor <pascal.gloor@spale.com>
1922
 *
1923
 * Redistribution and use in source and binary forms, with or without
1924
 * modification, are permitted provided that the following conditions
1925
 * are met:
1926
 * 1. Redistributions of source code must retain the above copyright
1927
 *    notice, this list of conditions and the following disclaimer.
1928
 * 2. Redistributions in binary form must reproduce the above copyright
1929
 *    notice, this list of conditions and the following disclaimer in
1930
 *    the documentation and/or other materials provided with the
1931
 *    distribution.
1932
 * 3. The name of the author may not be used to endorse or promote
1933
 *    products derived from this software without specific prior written
1934
 *    permission.
1935
 *
1936
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
1937
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
1938
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
1939
 * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR
1940
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
1941
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
1942
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
1943
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
1944
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1945
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
1946
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1947
 */
1948
/*
1949
 * Find the first occurrence of the byte string s in byte string l.
1950
 */
1951
void *
1952
memmem(const void *l, size_t l_len, const void *s, size_t s_len)
1953
{
1954
	const char *cur, *last;
1955
	const char *cl = l;
1956
	const char *cs = s;
1957

1958
	/* a zero length needle should just return the haystack */
1959
	if (l_len == 0)
1960
		return (void *)cl;
1961

1962
	/* "s" must be smaller or equal to "l" */
1963
	if (l_len < s_len)
1964
		return NULL;
1965

1966
	/* special case where s_len == 1 */
1967
	if (s_len == 1)
1968
		return memchr(l, *cs, l_len);
1969

1970
	/* the last position where its possible to find "s" in "l" */
1971
	last = cl + l_len - s_len;
1972

1973
	for (cur = cl; cur <= last; cur++)
1974
		if (cur[0] == cs[0] && memcmp(cur, cs, s_len) == 0)
1975
			return (void *)cur;
1976

1977
	return NULL;
1978
}
1979
#endif /* !HAVE_MEMMEM */
1980
#if !HAVE_MEMRCHR
1981
/*
1982
 * Copyright (c) 2007 Todd C. Miller <Todd.Miller@courtesan.com>
1983
 *
1984
 * Permission to use, copy, modify, and distribute this software for any
1985
 * purpose with or without fee is hereby granted, provided that the above
1986
 * copyright notice and this permission notice appear in all copies.
1987
 *
1988
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1989
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1990
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1991
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1992
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1993
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1994
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1995
 *
1996
 */
1997

1998
#include <string.h>
1999

2000
/*
2001
 * Reverse memchr()
2002
 * Find the last occurrence of 'c' in the buffer 's' of size 'n'.
2003
 */
2004
void *
2005
memrchr(const void *s, int c, size_t n)
2006
{
2007
    const unsigned char *cp;
2008

2009
    if (n != 0) {
2010
        cp = (unsigned char *)s + n;
2011
        do {
2012
            if (*(--cp) == (unsigned char)c)
2013
                return((void *)cp);
2014
        } while (--n != 0);
2015
    }
2016
    return(NULL);
2017
}
2018
#endif /* !HAVE_MEMRCHR */
2019
#if !HAVE_MKFIFOAT
2020
#include <sys/stat.h>
2021

2022
#include <errno.h>
2023
#include <fcntl.h>
2024
#include <unistd.h>
2025

2026
int
2027
mkfifoat(int fd, const char *path, mode_t mode)
2028
{
2029
	int	er, curfd = -1, newfd = -1;
2030

2031
	/* Get our current directory then switch to the given one. */
2032

2033
	if (fd != AT_FDCWD) {
2034
		if ((curfd = open(".", O_RDONLY | O_DIRECTORY, 0)) == -1)
2035
			return -1;
2036
		if (fchdir(fd) == -1)
2037
			goto out;
2038
	}
2039

2040
	if ((newfd = mkfifo(path, mode)) == -1)
2041
		goto out;
2042

2043
	/* This leaves the fifo if it fails. */
2044

2045
	if (curfd != -1 && fchdir(curfd) == -1)
2046
		goto out;
2047
	if (curfd != -1)
2048
		close(curfd);
2049

2050
	return newfd;
2051
out:
2052
	/* Ignore errors in close(2). */
2053

2054
	er = errno;
2055
	if (curfd != -1)
2056
		fchdir(curfd);
2057
	if (curfd != -1)
2058
		close(curfd);
2059
	if (newfd != -1)
2060
		close(newfd);
2061
	errno = er;
2062
	return -1;
2063
}
2064
#endif /* !HAVE_MKFIFOAT */
2065
#if !HAVE_MKNODAT
2066
#include <sys/stat.h>
2067

2068
#include <errno.h>
2069
#include <fcntl.h>
2070
#include <unistd.h>
2071

2072
int
2073
mknodat(int fd, const char *path, mode_t mode, dev_t dev)
2074
{
2075
	int	er, curfd = -1, newfd = -1;
2076

2077
	/* Get our current directory then switch to the given one. */
2078

2079
	if (fd != AT_FDCWD) {
2080
		if ((curfd = open(".", O_RDONLY | O_DIRECTORY, 0)) == -1)
2081
			return -1;
2082
		if (fchdir(fd) == -1)
2083
			goto out;
2084
	}
2085

2086
	if ((newfd = mknod(path, mode, dev)) == -1)
2087
		goto out;
2088

2089
	/* This leaves the node if it fails. */
2090

2091
	if (curfd != -1 && fchdir(curfd) == -1)
2092
		goto out;
2093
	if (curfd != -1)
2094
		close(curfd);
2095

2096
	return newfd;
2097
out:
2098
	
2099
	/* Ignore errors in close(2). */
2100

2101
	er = errno;
2102
	if (curfd != -1)
2103
		fchdir(curfd);
2104
	if (curfd != -1)
2105
		close(curfd);
2106
	if (newfd != -1)
2107
		close(newfd);
2108
	errno = er;
2109
	return -1;
2110
}
2111
#endif /* !HAVE_MKNODAT */
2112
#if !HAVE_READPASSPHRASE
2113
/* 
2114
 * Original: readpassphrase.c in OpenSSH portable
2115
 */
2116
/*
2117
 * Copyright (c) 2000-2002, 2007, 2010
2118
 *	Todd C. Miller <millert@openbsd.org>
2119
 *
2120
 * Permission to use, copy, modify, and distribute this software for any
2121
 * purpose with or without fee is hereby granted, provided that the above
2122
 * copyright notice and this permission notice appear in all copies.
2123
 *
2124
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
2125
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
2126
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
2127
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
2128
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
2129
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
2130
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2131
 *
2132
 * Sponsored in part by the Defense Advanced Research Projects
2133
 * Agency (DARPA) and Air Force Research Laboratory, Air Force
2134
 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
2135
 */
2136

2137
#include <ctype.h>
2138
#include <errno.h>
2139
#include <fcntl.h>
2140
#include <paths.h>
2141
#include <pwd.h>
2142
#include <signal.h>
2143
#include <string.h>
2144
#include <termios.h>
2145
#include <unistd.h>
2146

2147
#if !defined(_NSIG) && defined(NSIG)
2148
# define _NSIG NSIG
2149
#endif
2150

2151
static volatile sig_atomic_t readpassphrase_signo[_NSIG];
2152

2153
static void
2154
readpassphrase_handler(int s)
2155
{
2156

2157
	readpassphrase_signo[s] = 1;
2158
}
2159

2160
char *
2161
readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags)
2162
{
2163
	ssize_t nr;
2164
	int input, output, save_errno, i, need_restart;
2165
	char ch, *p, *end;
2166
	struct termios term, oterm;
2167
	struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm;
2168
	struct sigaction savetstp, savettin, savettou, savepipe;
2169
/* If we don't have TCSASOFT define it so that ORing it it below is a no-op. */
2170
#ifndef TCSASOFT
2171
	const int tcasoft = 0;
2172
#else
2173
	const int tcasoft = TCSASOFT;
2174
#endif
2175

2176
	/* I suppose we could alloc on demand in this case (XXX). */
2177
	if (bufsiz == 0) {
2178
		errno = EINVAL;
2179
		return(NULL);
2180
	}
2181

2182
restart:
2183
	for (i = 0; i < _NSIG; i++)
2184
		readpassphrase_signo[i] = 0;
2185
	nr = -1;
2186
	save_errno = 0;
2187
	need_restart = 0;
2188
	/*
2189
	 * Read and write to /dev/tty if available.  If not, read from
2190
	 * stdin and write to stderr unless a tty is required.
2191
	 */
2192
	if ((flags & RPP_STDIN) ||
2193
	    (input = output = open(_PATH_TTY, O_RDWR)) == -1) {
2194
		if (flags & RPP_REQUIRE_TTY) {
2195
			errno = ENOTTY;
2196
			return(NULL);
2197
		}
2198
		input = STDIN_FILENO;
2199
		output = STDERR_FILENO;
2200
	}
2201

2202
	/*
2203
	 * Turn off echo if possible.
2204
	 * If we are using a tty but are not the foreground pgrp this will
2205
	 * generate SIGTTOU, so do it *before* installing the signal handlers.
2206
	 */
2207
	if (input != STDIN_FILENO && tcgetattr(input, &oterm) == 0) {
2208
		memcpy(&term, &oterm, sizeof(term));
2209
		if (!(flags & RPP_ECHO_ON))
2210
			term.c_lflag &= ~(ECHO | ECHONL);
2211
#ifdef VSTATUS
2212
		if (term.c_cc[VSTATUS] != _POSIX_VDISABLE)
2213
			term.c_cc[VSTATUS] = _POSIX_VDISABLE;
2214
#endif
2215
		(void)tcsetattr(input, TCSAFLUSH|tcasoft, &term);
2216
	} else {
2217
		memset(&term, 0, sizeof(term));
2218
		term.c_lflag |= ECHO;
2219
		memset(&oterm, 0, sizeof(oterm));
2220
		oterm.c_lflag |= ECHO;
2221
	}
2222

2223
	/*
2224
	 * Catch signals that would otherwise cause the user to end
2225
	 * up with echo turned off in the shell.  Don't worry about
2226
	 * things like SIGXCPU and SIGVTALRM for now.
2227
	 */
2228
	sigemptyset(&sa.sa_mask);
2229
	sa.sa_flags = 0;		/* don't restart system calls */
2230
	sa.sa_handler = readpassphrase_handler;
2231
	(void)sigaction(SIGALRM, &sa, &savealrm);
2232
	(void)sigaction(SIGHUP, &sa, &savehup);
2233
	(void)sigaction(SIGINT, &sa, &saveint);
2234
	(void)sigaction(SIGPIPE, &sa, &savepipe);
2235
	(void)sigaction(SIGQUIT, &sa, &savequit);
2236
	(void)sigaction(SIGTERM, &sa, &saveterm);
2237
	(void)sigaction(SIGTSTP, &sa, &savetstp);
2238
	(void)sigaction(SIGTTIN, &sa, &savettin);
2239
	(void)sigaction(SIGTTOU, &sa, &savettou);
2240

2241
	if (!(flags & RPP_STDIN))
2242
		(void)write(output, prompt, strlen(prompt));
2243
	end = buf + bufsiz - 1;
2244
	p = buf;
2245
	while ((nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r') {
2246
		if (p < end) {
2247
			if ((flags & RPP_SEVENBIT))
2248
				ch &= 0x7f;
2249
			if (isalpha((unsigned char)ch)) {
2250
				if ((flags & RPP_FORCELOWER))
2251
					ch = (char)tolower((unsigned char)ch);
2252
				if ((flags & RPP_FORCEUPPER))
2253
					ch = (char)toupper((unsigned char)ch);
2254
			}
2255
			*p++ = ch;
2256
		}
2257
	}
2258
	*p = '\0';
2259
	save_errno = errno;
2260
	if (!(term.c_lflag & ECHO))
2261
		(void)write(output, "\n", 1);
2262

2263
	/* Restore old terminal settings and signals. */
2264
	if (memcmp(&term, &oterm, sizeof(term)) != 0) {
2265
		const int sigttou = readpassphrase_signo[SIGTTOU];
2266

2267
		/* Ignore SIGTTOU generated when we are not the fg pgrp. */
2268
		while (tcsetattr(input, TCSAFLUSH|tcasoft, &oterm) == -1 &&
2269
		    errno == EINTR && !readpassphrase_signo[SIGTTOU])
2270
			continue;
2271
		readpassphrase_signo[SIGTTOU] = sigttou;
2272
	}
2273
	(void)sigaction(SIGALRM, &savealrm, NULL);
2274
	(void)sigaction(SIGHUP, &savehup, NULL);
2275
	(void)sigaction(SIGINT, &saveint, NULL);
2276
	(void)sigaction(SIGQUIT, &savequit, NULL);
2277
	(void)sigaction(SIGPIPE, &savepipe, NULL);
2278
	(void)sigaction(SIGTERM, &saveterm, NULL);
2279
	(void)sigaction(SIGTSTP, &savetstp, NULL);
2280
	(void)sigaction(SIGTTIN, &savettin, NULL);
2281
	(void)sigaction(SIGTTOU, &savettou, NULL);
2282
	if (input != STDIN_FILENO)
2283
		(void)close(input);
2284

2285
	/*
2286
	 * If we were interrupted by a signal, resend it to ourselves
2287
	 * now that we have restored the signal handlers.
2288
	 */
2289
	for (i = 0; i < _NSIG; i++) {
2290
		if (readpassphrase_signo[i]) {
2291
			kill(getpid(), i);
2292
			switch (i) {
2293
			case SIGTSTP:
2294
			case SIGTTIN:
2295
			case SIGTTOU:
2296
				need_restart = 1;
2297
			}
2298
		}
2299
	}
2300
	if (need_restart)
2301
		goto restart;
2302

2303
	if (save_errno)
2304
		errno = save_errno;
2305
	return(nr == -1 ? NULL : buf);
2306
}
2307
#endif /* !HAVE_READPASSPHRASE */
2308
#if !HAVE_REALLOCARRAY
2309
/*
2310
 * Copyright (c) 2008 Otto Moerbeek <otto@drijf.net>
2311
 *
2312
 * Permission to use, copy, modify, and distribute this software for any
2313
 * purpose with or without fee is hereby granted, provided that the above
2314
 * copyright notice and this permission notice appear in all copies.
2315
 *
2316
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
2317
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
2318
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
2319
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
2320
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
2321
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
2322
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2323
 */
2324

2325
#include <sys/types.h>
2326
#include <errno.h>
2327
#include <stdint.h>
2328
#include <stdlib.h>
2329

2330
/*
2331
 * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
2332
 * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
2333
 */
2334
#define MUL_NO_OVERFLOW	((size_t)1 << (sizeof(size_t) * 4))
2335

2336
void *
2337
reallocarray(void *optr, size_t nmemb, size_t size)
2338
{
2339
	if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
2340
	    nmemb > 0 && SIZE_MAX / nmemb < size) {
2341
		errno = ENOMEM;
2342
		return NULL;
2343
	}
2344
	return realloc(optr, size * nmemb);
2345
}
2346
#endif /* !HAVE_REALLOCARRAY */
2347
#if !HAVE_RECALLOCARRAY
2348
/*
2349
 * Copyright (c) 2008, 2017 Otto Moerbeek <otto@drijf.net>
2350
 *
2351
 * Permission to use, copy, modify, and distribute this software for any
2352
 * purpose with or without fee is hereby granted, provided that the above
2353
 * copyright notice and this permission notice appear in all copies.
2354
 *
2355
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
2356
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
2357
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
2358
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
2359
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
2360
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
2361
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2362
 */
2363

2364
/* OPENBSD ORIGINAL: lib/libc/stdlib/recallocarray.c */
2365

2366
#include <errno.h>
2367
#include <stdlib.h>
2368
#include <stdint.h>
2369
#include <string.h>
2370
#include <unistd.h>
2371

2372
/*
2373
 * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
2374
 * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
2375
 */
2376
#define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4))
2377

2378
void *
2379
recallocarray(void *ptr, size_t oldnmemb, size_t newnmemb, size_t size)
2380
{
2381
	size_t oldsize, newsize;
2382
	void *newptr;
2383

2384
	if (ptr == NULL)
2385
		return calloc(newnmemb, size);
2386

2387
	if ((newnmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
2388
	    newnmemb > 0 && SIZE_MAX / newnmemb < size) {
2389
		errno = ENOMEM;
2390
		return NULL;
2391
	}
2392
	newsize = newnmemb * size;
2393

2394
	if ((oldnmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
2395
	    oldnmemb > 0 && SIZE_MAX / oldnmemb < size) {
2396
		errno = EINVAL;
2397
		return NULL;
2398
	}
2399
	oldsize = oldnmemb * size;
2400
	
2401
	/*
2402
	 * Don't bother too much if we're shrinking just a bit,
2403
	 * we do not shrink for series of small steps, oh well.
2404
	 */
2405
	if (newsize <= oldsize) {
2406
		size_t d = oldsize - newsize;
2407

2408
		if (d < oldsize / 2 && d < (size_t)getpagesize()) {
2409
			memset((char *)ptr + newsize, 0, d);
2410
			return ptr;
2411
		}
2412
	}
2413

2414
	newptr = malloc(newsize);
2415
	if (newptr == NULL)
2416
		return NULL;
2417

2418
	if (newsize > oldsize) {
2419
		memcpy(newptr, ptr, oldsize);
2420
		memset((char *)newptr + oldsize, 0, newsize - oldsize);
2421
	} else
2422
		memcpy(newptr, ptr, newsize);
2423

2424
	explicit_bzero(ptr, oldsize);
2425
	free(ptr);
2426

2427
	return newptr;
2428
}
2429
#endif /* !HAVE_RECALLOCARRAY */
2430
#if !HAVE_SCAN_SCALED
2431
/*	$OpenBSD: fmt_scaled.c,v 1.22 2022/03/11 09:04:59 dtucker Exp $	*/
2432

2433
/*
2434
 * Copyright (c) 2001, 2002, 2003 Ian F. Darwin.  All rights reserved.
2435
 *
2436
 * Redistribution and use in source and binary forms, with or without
2437
 * modification, are permitted provided that the following conditions
2438
 * are met:
2439
 * 1. Redistributions of source code must retain the above copyright
2440
 *    notice, this list of conditions and the following disclaimer.
2441
 * 2. Redistributions in binary form must reproduce the above copyright
2442
 *    notice, this list of conditions and the following disclaimer in the
2443
 *    documentation and/or other materials provided with the distribution.
2444
 * 3. The name of the author may not be used to endorse or promote products
2445
 *    derived from this software without specific prior written permission.
2446
 *
2447
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2448
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2449
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2450
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2451
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2452
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2453
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2454
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2455
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2456
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2457
 */
2458

2459
/*
2460
 * fmt_scaled: Format numbers scaled for human comprehension
2461
 * scan_scaled: Scan numbers in this format.
2462
 *
2463
 * "Human-readable" output uses 4 digits max, and puts a unit suffix at
2464
 * the end.  Makes output compact and easy-to-read esp. on huge disks.
2465
 * Formatting code was originally in OpenBSD "df", converted to library routine.
2466
 * Scanning code written for OpenBSD libutil.
2467
 */
2468

2469
#include <stdio.h>
2470
#include <stdlib.h>
2471
#include <errno.h>
2472
#include <string.h>
2473
#include <ctype.h>
2474
#include <limits.h>
2475

2476
typedef enum {
2477
	NONE = 0, KILO = 1, MEGA = 2, GIGA = 3, TERA = 4, PETA = 5, EXA = 6
2478
} unit_type;
2479

2480
/* These three arrays MUST be in sync!  XXX make a struct */
2481
static const unit_type units[] = { NONE, KILO, MEGA, GIGA, TERA, PETA, EXA };
2482
static const char scale_chars[] = "BKMGTPE";
2483
static const long long scale_factors[] = {
2484
	1LL,
2485
	1024LL,
2486
	1024LL*1024,
2487
	1024LL*1024*1024,
2488
	1024LL*1024*1024*1024,
2489
	1024LL*1024*1024*1024*1024,
2490
	1024LL*1024*1024*1024*1024*1024,
2491
};
2492
#define	SCALE_LENGTH (sizeof(units)/sizeof(units[0]))
2493

2494
#define MAX_DIGITS (SCALE_LENGTH * 3)	/* XXX strlen(sprintf("%lld", -1)? */
2495

2496
/* Convert the given input string "scaled" into numeric in "result".
2497
 * Return 0 on success, -1 and errno set on error.
2498
 */
2499
int
2500
scan_scaled(char *scaled, long long *result)
2501
{
2502
	char *p = scaled;
2503
	int sign = 0;
2504
	unsigned int i, ndigits = 0, fract_digits = 0;
2505
	long long scale_fact = 1, whole = 0, fpart = 0;
2506

2507
	/* Skip leading whitespace */
2508
	while (isascii((unsigned char)*p) && isspace((unsigned char)*p))
2509
		++p;
2510

2511
	/* Then at most one leading + or - */
2512
	while (*p == '-' || *p == '+') {
2513
		if (*p == '-') {
2514
			if (sign) {
2515
				errno = EINVAL;
2516
				return -1;
2517
			}
2518
			sign = -1;
2519
			++p;
2520
		} else if (*p == '+') {
2521
			if (sign) {
2522
				errno = EINVAL;
2523
				return -1;
2524
			}
2525
			sign = +1;
2526
			++p;
2527
		}
2528
	}
2529

2530
	/* Main loop: Scan digits, find decimal point, if present.
2531
	 * We don't allow exponentials, so no scientific notation
2532
	 * (but note that E for Exa might look like e to some!).
2533
	 * Advance 'p' to end, to get scale factor.
2534
	 */
2535
	for (; isascii((unsigned char)*p) &&
2536
	    (isdigit((unsigned char)*p) || *p=='.'); ++p) {
2537
		if (*p == '.') {
2538
			if (fract_digits > 0) {	/* oops, more than one '.' */
2539
				errno = EINVAL;
2540
				return -1;
2541
			}
2542
			fract_digits = 1;
2543
			continue;
2544
		}
2545

2546
		i = (*p) - '0';			/* whew! finally a digit we can use */
2547
		if (fract_digits > 0) {
2548
			if (fract_digits >= MAX_DIGITS-1)
2549
				/* ignore extra fractional digits */
2550
				continue;
2551
			fract_digits++;		/* for later scaling */
2552
			if (fpart > LLONG_MAX / 10) {
2553
				errno = ERANGE;
2554
				return -1;
2555
			}
2556
			fpart *= 10;
2557
			if (i > LLONG_MAX - fpart) {
2558
				errno = ERANGE;
2559
				return -1;
2560
			}
2561
			fpart += i;
2562
		} else {				/* normal digit */
2563
			if (++ndigits >= MAX_DIGITS) {
2564
				errno = ERANGE;
2565
				return -1;
2566
			}
2567
			if (whole > LLONG_MAX / 10) {
2568
				errno = ERANGE;
2569
				return -1;
2570
			}
2571
			whole *= 10;
2572
			if (i > LLONG_MAX - whole) {
2573
				errno = ERANGE;
2574
				return -1;
2575
			}
2576
			whole += i;
2577
		}
2578
	}
2579

2580
	if (sign)
2581
		whole *= sign;
2582

2583
	/* If no scale factor given, we're done. fraction is discarded. */
2584
	if (!*p) {
2585
		*result = whole;
2586
		return 0;
2587
	}
2588

2589
	/* Validate scale factor, and scale whole and fraction by it. */
2590
	for (i = 0; i < SCALE_LENGTH; i++) {
2591

2592
		/* Are we there yet? */
2593
		if (*p == scale_chars[i] ||
2594
			*p == tolower((unsigned char)scale_chars[i])) {
2595

2596
			/* If it ends with alphanumerics after the scale char, bad. */
2597
			if (isalnum((unsigned char)*(p+1))) {
2598
				errno = EINVAL;
2599
				return -1;
2600
			}
2601
			scale_fact = scale_factors[i];
2602

2603
			/* check for overflow and underflow after scaling */
2604
			if (whole > LLONG_MAX / scale_fact ||
2605
			    whole < LLONG_MIN / scale_fact) {
2606
				errno = ERANGE;
2607
				return -1;
2608
			}
2609

2610
			/* scale whole part */
2611
			whole *= scale_fact;
2612

2613
			/* truncate fpart so it does't overflow.
2614
			 * then scale fractional part.
2615
			 */
2616
			while (fpart >= LLONG_MAX / scale_fact) {
2617
				fpart /= 10;
2618
				fract_digits--;
2619
			}
2620
			fpart *= scale_fact;
2621
			if (fract_digits > 0) {
2622
				for (i = 0; i < fract_digits -1; i++)
2623
					fpart /= 10;
2624
			}
2625
			if (sign == -1)
2626
				whole -= fpart;
2627
			else
2628
				whole += fpart;
2629
			*result = whole;
2630
			return 0;
2631
		}
2632
	}
2633

2634
	/* Invalid unit or character */
2635
	errno = EINVAL;
2636
	return -1;
2637
}
2638

2639
/* Format the given "number" into human-readable form in "result".
2640
 * Result must point to an allocated buffer of length FMT_SCALED_STRSIZE.
2641
 * Return 0 on success, -1 and errno set if error.
2642
 */
2643
int
2644
fmt_scaled(long long number, char *result)
2645
{
2646
	long long abval, fract = 0;
2647
	unsigned int i;
2648
	unit_type unit = NONE;
2649

2650
	/* Not every negative long long has a positive representation. */
2651
	if (number == LLONG_MIN) {
2652
		errno = ERANGE;
2653
		return -1;
2654
	}
2655

2656
	abval = llabs(number);
2657

2658
	/* Also check for numbers that are just too darned big to format. */
2659
	if (abval / 1024 >= scale_factors[SCALE_LENGTH-1]) {
2660
		errno = ERANGE;
2661
		return -1;
2662
	}
2663

2664
	/* scale whole part; get unscaled fraction */
2665
	for (i = 0; i < SCALE_LENGTH; i++) {
2666
		if (abval/1024 < scale_factors[i]) {
2667
			unit = units[i];
2668
			fract = (i == 0) ? 0 : abval % scale_factors[i];
2669
			number /= scale_factors[i];
2670
			if (i > 0)
2671
				fract /= scale_factors[i - 1];
2672
			break;
2673
		}
2674
	}
2675

2676
	fract = (10 * fract + 512) / 1024;
2677
	/* if the result would be >= 10, round main number */
2678
	if (fract >= 10) {
2679
		if (number >= 0)
2680
			number++;
2681
		else
2682
			number--;
2683
		fract = 0;
2684
	} else if (fract < 0) {
2685
		/* shouldn't happen */
2686
		fract = 0;
2687
	}
2688

2689
	if (number == 0)
2690
		strlcpy(result, "0B", FMT_SCALED_STRSIZE);
2691
	else if (unit == NONE || number >= 100 || number <= -100) {
2692
		if (fract >= 5) {
2693
			if (number >= 0)
2694
				number++;
2695
			else
2696
				number--;
2697
		}
2698
		(void)snprintf(result, FMT_SCALED_STRSIZE, "%lld%c",
2699
			number, scale_chars[unit]);
2700
	} else
2701
		(void)snprintf(result, FMT_SCALED_STRSIZE, "%lld.%1lld%c",
2702
			number, fract, scale_chars[unit]);
2703

2704
	return 0;
2705
}
2706

2707
#endif /* !HAVE_SCAN_SCALED */
2708
#if !HAVE_SETRESGID
2709
/*
2710
 * Copyright (c) 2004, 2005 Darren Tucker (dtucker at zip com au).
2711
 *
2712
 * Permission to use, copy, modify, and distribute this software for any
2713
 * purpose with or without fee is hereby granted, provided that the above
2714
 * copyright notice and this permission notice appear in all copies.
2715
 *
2716
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
2717
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
2718
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
2719
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
2720
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
2721
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
2722
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2723
 */
2724

2725
#include <sys/types.h>
2726
#include <unistd.h>
2727

2728
int
2729
setresgid(gid_t rgid, gid_t egid, gid_t sgid)
2730
{
2731
	/* this is the only configuration tested */
2732

2733
	if (rgid != egid || egid != sgid)
2734
		return -1;
2735

2736
	if (setregid(rgid, egid) == -1)
2737
		return -1;
2738

2739
	return 0;
2740
}
2741
#endif /* !HAVE_SETRESGID */
2742
#if !HAVE_SETRESUID
2743
/*
2744
 * Copyright (c) 2004, 2005 Darren Tucker (dtucker at zip com au).
2745
 *
2746
 * Permission to use, copy, modify, and distribute this software for any
2747
 * purpose with or without fee is hereby granted, provided that the above
2748
 * copyright notice and this permission notice appear in all copies.
2749
 *
2750
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
2751
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
2752
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
2753
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
2754
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
2755
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
2756
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2757
 */
2758

2759
#include <sys/types.h>
2760

2761
#include <errno.h>
2762
#include <unistd.h>
2763

2764
int
2765
setresuid(uid_t ruid, uid_t euid, uid_t suid)
2766
{
2767
	uid_t ouid;
2768
	int ret = -1;
2769

2770
	/* Allow only the tested configuration. */
2771

2772
	if (ruid != euid || euid != suid) {
2773
		errno = ENOSYS;
2774
		return -1;
2775
	}
2776
	ouid = getuid();
2777

2778
	if ((ret = setreuid(euid, euid)) == -1)
2779
		return -1;
2780

2781
	/*
2782
	 * When real, effective and saved uids are the same and we have
2783
	 * changed uids, sanity check that we cannot restore the old uid.
2784
	 */
2785

2786
	if (ruid == euid && euid == suid && ouid != ruid &&
2787
	    setuid(ouid) != -1 && seteuid(ouid) != -1) {
2788
		errno = EINVAL;
2789
		return -1;
2790
	}
2791

2792
	/*
2793
	 * Finally, check that the real and effective uids are what we
2794
	 * expect.
2795
	 */
2796
	if (getuid() != ruid || geteuid() != euid) {
2797
		errno = EACCES;
2798
		return -1;
2799
	}
2800

2801
	return ret;
2802
}
2803
#endif /* !HAVE_SETRESUID */
2804
#if !HAVE_SHA2
2805
/*	$OpenBSD$	*/
2806

2807
/*
2808
 * FILE:	sha2.c
2809
 * AUTHOR:	Aaron D. Gifford <me@aarongifford.com>
2810
 * 
2811
 * Copyright (c) 2000-2001, Aaron D. Gifford
2812
 * All rights reserved.
2813
 *
2814
 * Redistribution and use in source and binary forms, with or without
2815
 * modification, are permitted provided that the following conditions
2816
 * are met:
2817
 * 1. Redistributions of source code must retain the above copyright
2818
 *    notice, this list of conditions and the following disclaimer.
2819
 * 2. Redistributions in binary form must reproduce the above copyright
2820
 *    notice, this list of conditions and the following disclaimer in the
2821
 *    documentation and/or other materials provided with the distribution.
2822
 * 3. Neither the name of the copyright holder nor the names of contributors
2823
 *    may be used to endorse or promote products derived from this software
2824
 *    without specific prior written permission.
2825
 * 
2826
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
2827
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2828
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2829
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
2830
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2831
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2832
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2833
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2834
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2835
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2836
 * SUCH DAMAGE.
2837
 *
2838
 * $From: sha2.c,v 1.1 2001/11/08 00:01:51 adg Exp adg $
2839
 */
2840

2841
/* OPENBSD ORIGINAL: lib/libc/hash/sha2.c */
2842

2843
/* no-op out, similar to DEF_WEAK but only needed here */
2844
#define MAKE_CLONE(x, y)	void __ssh_compat_make_clone_##x_##y(void)
2845

2846
#include <sys/types.h>
2847
#include <sys/stat.h>
2848

2849
#include <errno.h>
2850
#include <fcntl.h>
2851
#include <stdlib.h>
2852
#include <stdio.h>
2853
#include <string.h>
2854
#include <unistd.h>
2855

2856
#ifndef MINIMUM
2857
# define MINIMUM(a, b)	(((a) < (b)) ? (a) : (b))
2858
#endif
2859

2860
#ifndef BYTE_ORDER
2861
# if defined(LITTLE_ENDIAN) || defined(BIG_ENDIAN)
2862
#  error Confusion in endian macros.
2863
# endif
2864
# if !defined(__BYTE_ORDER__)
2865
#  error Byte order macro not found.
2866
# endif
2867
# if !defined(__ORDER_LITTLE_ENDIAN__) || !defined(__ORDER_BIG_ENDIAN__)
2868
#  error Little/big endian macros not found.
2869
# endif
2870
# define BYTE_ORDER __BYTE_ORDER__
2871
# define LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__
2872
# define BIG_ENDIAN __ORDER_BIG_ENDIAN__
2873
#endif /*!BYTE_ORDER*/
2874

2875
/*
2876
 * UNROLLED TRANSFORM LOOP NOTE:
2877
 * You can define SHA2_UNROLL_TRANSFORM to use the unrolled transform
2878
 * loop version for the hash transform rounds (defined using macros
2879
 * later in this file).  Either define on the command line, for example:
2880
 *
2881
 *   cc -DSHA2_UNROLL_TRANSFORM -o sha2 sha2.c sha2prog.c
2882
 *
2883
 * or define below:
2884
 *
2885
 *   #define SHA2_UNROLL_TRANSFORM
2886
 *
2887
 */
2888
#if defined(__amd64__) || defined(__i386__)
2889
#define SHA2_UNROLL_TRANSFORM
2890
#endif
2891

2892
/*** SHA-224/256/384/512 Machine Architecture Definitions *****************/
2893
/*
2894
 * BYTE_ORDER NOTE:
2895
 *
2896
 * Please make sure that your system defines BYTE_ORDER.  If your
2897
 * architecture is little-endian, make sure it also defines
2898
 * LITTLE_ENDIAN and that the two (BYTE_ORDER and LITTLE_ENDIAN) are
2899
 * equivalent.
2900
 *
2901
 * If your system does not define the above, then you can do so by
2902
 * hand like this:
2903
 *
2904
 *   #define LITTLE_ENDIAN 1234
2905
 *   #define BIG_ENDIAN    4321
2906
 *
2907
 * And for little-endian machines, add:
2908
 *
2909
 *   #define BYTE_ORDER LITTLE_ENDIAN 
2910
 *
2911
 * Or for big-endian machines:
2912
 *
2913
 *   #define BYTE_ORDER BIG_ENDIAN
2914
 *
2915
 * The FreeBSD machine this was written on defines BYTE_ORDER
2916
 * appropriately by including <sys/types.h> (which in turn includes
2917
 * <machine/endian.h> where the appropriate definitions are actually
2918
 * made).
2919
 */
2920
#if !defined(BYTE_ORDER) || (BYTE_ORDER != LITTLE_ENDIAN && BYTE_ORDER != BIG_ENDIAN)
2921
#error Define BYTE_ORDER to be equal to either LITTLE_ENDIAN or BIG_ENDIAN
2922
#endif
2923

2924

2925
/*** SHA-224/256/384/512 Various Length Definitions ***********************/
2926
/* NOTE: Most of these are in sha2.h */
2927
#define SHA224_SHORT_BLOCK_LENGTH	(SHA224_BLOCK_LENGTH - 8)
2928
#define SHA256_SHORT_BLOCK_LENGTH	(SHA256_BLOCK_LENGTH - 8)
2929
#define SHA384_SHORT_BLOCK_LENGTH	(SHA384_BLOCK_LENGTH - 16)
2930
#define SHA512_SHORT_BLOCK_LENGTH	(SHA512_BLOCK_LENGTH - 16)
2931

2932
/*** ENDIAN SPECIFIC COPY MACROS **************************************/
2933
#define BE_8_TO_32(dst, cp) do {					\
2934
	(dst) = (uint32_t)(cp)[3] | ((uint32_t)(cp)[2] << 8) |	\
2935
	    ((uint32_t)(cp)[1] << 16) | ((uint32_t)(cp)[0] << 24);	\
2936
} while(0)
2937

2938
#define BE_8_TO_64(dst, cp) do {					\
2939
	(dst) = (uint64_t)(cp)[7] | ((uint64_t)(cp)[6] << 8) |	\
2940
	    ((uint64_t)(cp)[5] << 16) | ((uint64_t)(cp)[4] << 24) |	\
2941
	    ((uint64_t)(cp)[3] << 32) | ((uint64_t)(cp)[2] << 40) |	\
2942
	    ((uint64_t)(cp)[1] << 48) | ((uint64_t)(cp)[0] << 56);	\
2943
} while (0)
2944

2945
#define BE_64_TO_8(cp, src) do {					\
2946
	(cp)[0] = (src) >> 56;						\
2947
        (cp)[1] = (src) >> 48;						\
2948
	(cp)[2] = (src) >> 40;						\
2949
	(cp)[3] = (src) >> 32;						\
2950
	(cp)[4] = (src) >> 24;						\
2951
	(cp)[5] = (src) >> 16;						\
2952
	(cp)[6] = (src) >> 8;						\
2953
	(cp)[7] = (src);						\
2954
} while (0)
2955

2956
#define BE_32_TO_8(cp, src) do {					\
2957
	(cp)[0] = (src) >> 24;						\
2958
	(cp)[1] = (src) >> 16;						\
2959
	(cp)[2] = (src) >> 8;						\
2960
	(cp)[3] = (src);						\
2961
} while (0)
2962

2963
/*
2964
 * Macro for incrementally adding the unsigned 64-bit integer n to the
2965
 * unsigned 128-bit integer (represented using a two-element array of
2966
 * 64-bit words):
2967
 */
2968
#define ADDINC128(w,n) do {						\
2969
	(w)[0] += (uint64_t)(n);					\
2970
	if ((w)[0] < (n)) {						\
2971
		(w)[1]++;						\
2972
	}								\
2973
} while (0)
2974

2975
/*** THE SIX LOGICAL FUNCTIONS ****************************************/
2976
/*
2977
 * Bit shifting and rotation (used by the six SHA-XYZ logical functions:
2978
 *
2979
 *   NOTE:  The naming of R and S appears backwards here (R is a SHIFT and
2980
 *   S is a ROTATION) because the SHA-224/256/384/512 description document
2981
 *   (see http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf) uses this
2982
 *   same "backwards" definition.
2983
 */
2984
/* Shift-right (used in SHA-224, SHA-256, SHA-384, and SHA-512): */
2985
#define R(b,x) 		((x) >> (b))
2986
/* 32-bit Rotate-right (used in SHA-224 and SHA-256): */
2987
#define S32(b,x)	(((x) >> (b)) | ((x) << (32 - (b))))
2988
/* 64-bit Rotate-right (used in SHA-384 and SHA-512): */
2989
#define S64(b,x)	(((x) >> (b)) | ((x) << (64 - (b))))
2990

2991
/* Two of six logical functions used in SHA-224, SHA-256, SHA-384, and SHA-512: */
2992
#define Ch(x,y,z)	(((x) & (y)) ^ ((~(x)) & (z)))
2993
#define Maj(x,y,z)	(((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
2994

2995
/* Four of six logical functions used in SHA-224 and SHA-256: */
2996
#define Sigma0_256(x)	(S32(2,  (x)) ^ S32(13, (x)) ^ S32(22, (x)))
2997
#define Sigma1_256(x)	(S32(6,  (x)) ^ S32(11, (x)) ^ S32(25, (x)))
2998
#define sigma0_256(x)	(S32(7,  (x)) ^ S32(18, (x)) ^ R(3 ,   (x)))
2999
#define sigma1_256(x)	(S32(17, (x)) ^ S32(19, (x)) ^ R(10,   (x)))
3000

3001
/* Four of six logical functions used in SHA-384 and SHA-512: */
3002
#define Sigma0_512(x)	(S64(28, (x)) ^ S64(34, (x)) ^ S64(39, (x)))
3003
#define Sigma1_512(x)	(S64(14, (x)) ^ S64(18, (x)) ^ S64(41, (x)))
3004
#define sigma0_512(x)	(S64( 1, (x)) ^ S64( 8, (x)) ^ R( 7,   (x)))
3005
#define sigma1_512(x)	(S64(19, (x)) ^ S64(61, (x)) ^ R( 6,   (x)))
3006

3007

3008
/*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/
3009
/* Hash constant words K for SHA-224 and SHA-256: */
3010
static const uint32_t K256[64] = {
3011
	0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL,
3012
	0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL,
3013
	0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL,
3014
	0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL,
3015
	0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
3016
	0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL,
3017
	0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL,
3018
	0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL,
3019
	0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL,
3020
	0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
3021
	0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL,
3022
	0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL,
3023
	0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL,
3024
	0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL,
3025
	0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
3026
	0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL
3027
};
3028

3029
/* Initial hash value H for SHA-256: */
3030
static const uint32_t sha256_initial_hash_value[8] = {
3031
	0x6a09e667UL,
3032
	0xbb67ae85UL,
3033
	0x3c6ef372UL,
3034
	0xa54ff53aUL,
3035
	0x510e527fUL,
3036
	0x9b05688cUL,
3037
	0x1f83d9abUL,
3038
	0x5be0cd19UL
3039
};
3040

3041
/* Hash constant words K for SHA-384 and SHA-512: */
3042
static const uint64_t K512[80] = {
3043
	0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL,
3044
	0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
3045
	0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,
3046
	0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL,
3047
	0xd807aa98a3030242ULL, 0x12835b0145706fbeULL,
3048
	0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
3049
	0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL,
3050
	0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL,
3051
	0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL,
3052
	0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL,
3053
	0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL,
3054
	0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
3055
	0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL,
3056
	0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL,
3057
	0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL,
3058
	0x06ca6351e003826fULL, 0x142929670a0e6e70ULL,
3059
	0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL,
3060
	0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
3061
	0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL,
3062
	0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
3063
	0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL,
3064
	0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL,
3065
	0xd192e819d6ef5218ULL, 0xd69906245565a910ULL,
3066
	0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
3067
	0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL,
3068
	0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL,
3069
	0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL,
3070
	0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL,
3071
	0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL,
3072
	0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
3073
	0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL,
3074
	0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,
3075
	0xca273eceea26619cULL, 0xd186b8c721c0c207ULL,
3076
	0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL,
3077
	0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL,
3078
	0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
3079
	0x28db77f523047d84ULL, 0x32caab7b40c72493ULL,
3080
	0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL,
3081
	0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL,
3082
	0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL
3083
};
3084

3085
/* Initial hash value H for SHA-512 */
3086
static const uint64_t sha512_initial_hash_value[8] = {
3087
	0x6a09e667f3bcc908ULL,
3088
	0xbb67ae8584caa73bULL,
3089
	0x3c6ef372fe94f82bULL,
3090
	0xa54ff53a5f1d36f1ULL,
3091
	0x510e527fade682d1ULL,
3092
	0x9b05688c2b3e6c1fULL,
3093
	0x1f83d9abfb41bd6bULL,
3094
	0x5be0cd19137e2179ULL
3095
};
3096

3097
/* Initial hash value H for SHA-384 */
3098
static const uint64_t sha384_initial_hash_value[8] = {
3099
	0xcbbb9d5dc1059ed8ULL,
3100
	0x629a292a367cd507ULL,
3101
	0x9159015a3070dd17ULL,
3102
	0x152fecd8f70e5939ULL,
3103
	0x67332667ffc00b31ULL,
3104
	0x8eb44a8768581511ULL,
3105
	0xdb0c2e0d64f98fa7ULL,
3106
	0x47b5481dbefa4fa4ULL
3107
};
3108

3109
/*** SHA-256: *********************************************************/
3110
void
3111
SHA256Init(SHA2_CTX *context)
3112
{
3113
	memcpy(context->state.st32, sha256_initial_hash_value,
3114
	    sizeof(sha256_initial_hash_value));
3115
	memset(context->buffer, 0, sizeof(context->buffer));
3116
	context->bitcount[0] = 0;
3117
}
3118

3119
#ifdef SHA2_UNROLL_TRANSFORM
3120

3121
/* Unrolled SHA-256 round macros: */
3122

3123
#define ROUND256_0_TO_15(a,b,c,d,e,f,g,h) do {				    \
3124
	BE_8_TO_32(W256[j], data);					    \
3125
	data += 4;							    \
3126
	T1 = (h) + Sigma1_256((e)) + Ch((e), (f), (g)) + K256[j] + W256[j]; \
3127
	(d) += T1;							    \
3128
	(h) = T1 + Sigma0_256((a)) + Maj((a), (b), (c));		    \
3129
	j++;								    \
3130
} while(0)
3131

3132
#define ROUND256(a,b,c,d,e,f,g,h) do {					    \
3133
	s0 = W256[(j+1)&0x0f];						    \
3134
	s0 = sigma0_256(s0);						    \
3135
	s1 = W256[(j+14)&0x0f];						    \
3136
	s1 = sigma1_256(s1);						    \
3137
	T1 = (h) + Sigma1_256((e)) + Ch((e), (f), (g)) + K256[j] +	    \
3138
	     (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0);		    \
3139
	(d) += T1;							    \
3140
	(h) = T1 + Sigma0_256((a)) + Maj((a), (b), (c));		    \
3141
	j++;								    \
3142
} while(0)
3143

3144
void
3145
SHA256Transform(uint32_t state[8], const uint8_t data[SHA256_BLOCK_LENGTH])
3146
{
3147
	uint32_t	a, b, c, d, e, f, g, h, s0, s1;
3148
	uint32_t	T1, W256[16];
3149
	int		j;
3150

3151
	/* Initialize registers with the prev. intermediate value */
3152
	a = state[0];
3153
	b = state[1];
3154
	c = state[2];
3155
	d = state[3];
3156
	e = state[4];
3157
	f = state[5];
3158
	g = state[6];
3159
	h = state[7];
3160

3161
	j = 0;
3162
	do {
3163
		/* Rounds 0 to 15 (unrolled): */
3164
		ROUND256_0_TO_15(a,b,c,d,e,f,g,h);
3165
		ROUND256_0_TO_15(h,a,b,c,d,e,f,g);
3166
		ROUND256_0_TO_15(g,h,a,b,c,d,e,f);
3167
		ROUND256_0_TO_15(f,g,h,a,b,c,d,e);
3168
		ROUND256_0_TO_15(e,f,g,h,a,b,c,d);
3169
		ROUND256_0_TO_15(d,e,f,g,h,a,b,c);
3170
		ROUND256_0_TO_15(c,d,e,f,g,h,a,b);
3171
		ROUND256_0_TO_15(b,c,d,e,f,g,h,a);
3172
	} while (j < 16);
3173

3174
	/* Now for the remaining rounds up to 63: */
3175
	do {
3176
		ROUND256(a,b,c,d,e,f,g,h);
3177
		ROUND256(h,a,b,c,d,e,f,g);
3178
		ROUND256(g,h,a,b,c,d,e,f);
3179
		ROUND256(f,g,h,a,b,c,d,e);
3180
		ROUND256(e,f,g,h,a,b,c,d);
3181
		ROUND256(d,e,f,g,h,a,b,c);
3182
		ROUND256(c,d,e,f,g,h,a,b);
3183
		ROUND256(b,c,d,e,f,g,h,a);
3184
	} while (j < 64);
3185

3186
	/* Compute the current intermediate hash value */
3187
	state[0] += a;
3188
	state[1] += b;
3189
	state[2] += c;
3190
	state[3] += d;
3191
	state[4] += e;
3192
	state[5] += f;
3193
	state[6] += g;
3194
	state[7] += h;
3195

3196
	/* Clean up */
3197
	a = b = c = d = e = f = g = h = T1 = 0;
3198
}
3199

3200
#else /* SHA2_UNROLL_TRANSFORM */
3201

3202
void
3203
SHA256Transform(uint32_t state[8], const uint8_t data[SHA256_BLOCK_LENGTH])
3204
{
3205
	uint32_t	a, b, c, d, e, f, g, h, s0, s1;
3206
	uint32_t	T1, T2, W256[16];
3207
	int		j;
3208

3209
	/* Initialize registers with the prev. intermediate value */
3210
	a = state[0];
3211
	b = state[1];
3212
	c = state[2];
3213
	d = state[3];
3214
	e = state[4];
3215
	f = state[5];
3216
	g = state[6];
3217
	h = state[7];
3218

3219
	j = 0;
3220
	do {
3221
		BE_8_TO_32(W256[j], data);
3222
		data += 4;
3223
		/* Apply the SHA-256 compression function to update a..h */
3224
		T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + W256[j];
3225
		T2 = Sigma0_256(a) + Maj(a, b, c);
3226
		h = g;
3227
		g = f;
3228
		f = e;
3229
		e = d + T1;
3230
		d = c;
3231
		c = b;
3232
		b = a;
3233
		a = T1 + T2;
3234

3235
		j++;
3236
	} while (j < 16);
3237

3238
	do {
3239
		/* Part of the message block expansion: */
3240
		s0 = W256[(j+1)&0x0f];
3241
		s0 = sigma0_256(s0);
3242
		s1 = W256[(j+14)&0x0f];	
3243
		s1 = sigma1_256(s1);
3244

3245
		/* Apply the SHA-256 compression function to update a..h */
3246
		T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + 
3247
		     (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0);
3248
		T2 = Sigma0_256(a) + Maj(a, b, c);
3249
		h = g;
3250
		g = f;
3251
		f = e;
3252
		e = d + T1;
3253
		d = c;
3254
		c = b;
3255
		b = a;
3256
		a = T1 + T2;
3257

3258
		j++;
3259
	} while (j < 64);
3260

3261
	/* Compute the current intermediate hash value */
3262
	state[0] += a;
3263
	state[1] += b;
3264
	state[2] += c;
3265
	state[3] += d;
3266
	state[4] += e;
3267
	state[5] += f;
3268
	state[6] += g;
3269
	state[7] += h;
3270

3271
	/* Clean up */
3272
	a = b = c = d = e = f = g = h = T1 = T2 = 0;
3273
}
3274

3275
#endif /* SHA2_UNROLL_TRANSFORM */
3276

3277
void
3278
SHA256Update(SHA2_CTX *context, const uint8_t *data, size_t len)
3279
{
3280
	uint64_t	freespace, usedspace;
3281

3282
	/* Calling with no data is valid (we do nothing) */
3283
	if (len == 0)
3284
		return;
3285

3286
	usedspace = (context->bitcount[0] >> 3) % SHA256_BLOCK_LENGTH;
3287
	if (usedspace > 0) {
3288
		/* Calculate how much free space is available in the buffer */
3289
		freespace = SHA256_BLOCK_LENGTH - usedspace;
3290

3291
		if (len >= freespace) {
3292
			/* Fill the buffer completely and process it */
3293
			memcpy(&context->buffer[usedspace], data, freespace);
3294
			context->bitcount[0] += freespace << 3;
3295
			len -= freespace;
3296
			data += freespace;
3297
			SHA256Transform(context->state.st32, context->buffer);
3298
		} else {
3299
			/* The buffer is not yet full */
3300
			memcpy(&context->buffer[usedspace], data, len);
3301
			context->bitcount[0] += (uint64_t)len << 3;
3302
			/* Clean up: */
3303
			usedspace = freespace = 0;
3304
			return;
3305
		}
3306
	}
3307
	while (len >= SHA256_BLOCK_LENGTH) {
3308
		/* Process as many complete blocks as we can */
3309
		SHA256Transform(context->state.st32, data);
3310
		context->bitcount[0] += SHA256_BLOCK_LENGTH << 3;
3311
		len -= SHA256_BLOCK_LENGTH;
3312
		data += SHA256_BLOCK_LENGTH;
3313
	}
3314
	if (len > 0) {
3315
		/* There's left-overs, so save 'em */
3316
		memcpy(context->buffer, data, len);
3317
		context->bitcount[0] += len << 3;
3318
	}
3319
	/* Clean up: */
3320
	usedspace = freespace = 0;
3321
}
3322

3323
void
3324
SHA256Pad(SHA2_CTX *context)
3325
{
3326
	unsigned int	usedspace;
3327

3328
	usedspace = (context->bitcount[0] >> 3) % SHA256_BLOCK_LENGTH;
3329
	if (usedspace > 0) {
3330
		/* Begin padding with a 1 bit: */
3331
		context->buffer[usedspace++] = 0x80;
3332

3333
		if (usedspace <= SHA256_SHORT_BLOCK_LENGTH) {
3334
			/* Set-up for the last transform: */
3335
			memset(&context->buffer[usedspace], 0,
3336
			    SHA256_SHORT_BLOCK_LENGTH - usedspace);
3337
		} else {
3338
			if (usedspace < SHA256_BLOCK_LENGTH) {
3339
				memset(&context->buffer[usedspace], 0,
3340
				    SHA256_BLOCK_LENGTH - usedspace);
3341
			}
3342
			/* Do second-to-last transform: */
3343
			SHA256Transform(context->state.st32, context->buffer);
3344

3345
			/* Prepare for last transform: */
3346
			memset(context->buffer, 0, SHA256_SHORT_BLOCK_LENGTH);
3347
		}
3348
	} else {
3349
		/* Set-up for the last transform: */
3350
		memset(context->buffer, 0, SHA256_SHORT_BLOCK_LENGTH);
3351

3352
		/* Begin padding with a 1 bit: */
3353
		*context->buffer = 0x80;
3354
	}
3355
	/* Store the length of input data (in bits) in big endian format: */
3356
	BE_64_TO_8(&context->buffer[SHA256_SHORT_BLOCK_LENGTH],
3357
	    context->bitcount[0]);
3358

3359
	/* Final transform: */
3360
	SHA256Transform(context->state.st32, context->buffer);
3361

3362
	/* Clean up: */
3363
	usedspace = 0;
3364
}
3365

3366
void
3367
SHA256Final(uint8_t digest[SHA256_DIGEST_LENGTH], SHA2_CTX *context)
3368
{
3369
	SHA256Pad(context);
3370

3371
#if BYTE_ORDER == LITTLE_ENDIAN
3372
	int	i;
3373

3374
	/* Convert TO host byte order */
3375
	for (i = 0; i < 8; i++)
3376
		BE_32_TO_8(digest + i * 4, context->state.st32[i]);
3377
#else
3378
	memcpy(digest, context->state.st32, SHA256_DIGEST_LENGTH);
3379
#endif
3380
	explicit_bzero(context, sizeof(*context));
3381
}
3382

3383

3384
/*** SHA-512: *********************************************************/
3385
void
3386
SHA512Init(SHA2_CTX *context)
3387
{
3388
	memcpy(context->state.st64, sha512_initial_hash_value,
3389
	    sizeof(sha512_initial_hash_value));
3390
	memset(context->buffer, 0, sizeof(context->buffer));
3391
	context->bitcount[0] = context->bitcount[1] =  0;
3392
}
3393

3394
#ifdef SHA2_UNROLL_TRANSFORM
3395

3396
/* Unrolled SHA-512 round macros: */
3397

3398
#define ROUND512_0_TO_15(a,b,c,d,e,f,g,h) do {				    \
3399
	BE_8_TO_64(W512[j], data);					    \
3400
	data += 8;							    \
3401
	T1 = (h) + Sigma1_512((e)) + Ch((e), (f), (g)) + K512[j] + W512[j]; \
3402
	(d) += T1;							    \
3403
	(h) = T1 + Sigma0_512((a)) + Maj((a), (b), (c));		    \
3404
	j++;								    \
3405
} while(0)
3406

3407

3408
#define ROUND512(a,b,c,d,e,f,g,h) do {					    \
3409
	s0 = W512[(j+1)&0x0f];						    \
3410
	s0 = sigma0_512(s0);						    \
3411
	s1 = W512[(j+14)&0x0f];						    \
3412
	s1 = sigma1_512(s1);						    \
3413
	T1 = (h) + Sigma1_512((e)) + Ch((e), (f), (g)) + K512[j] +	    \
3414
             (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0);		    \
3415
	(d) += T1;							    \
3416
	(h) = T1 + Sigma0_512((a)) + Maj((a), (b), (c));		    \
3417
	j++;								    \
3418
} while(0)
3419

3420
void
3421
SHA512Transform(uint64_t state[8], const uint8_t data[SHA512_BLOCK_LENGTH])
3422
{
3423
	uint64_t	a, b, c, d, e, f, g, h, s0, s1;
3424
	uint64_t	T1, W512[16];
3425
	int		j;
3426

3427
	/* Initialize registers with the prev. intermediate value */
3428
	a = state[0];
3429
	b = state[1];
3430
	c = state[2];
3431
	d = state[3];
3432
	e = state[4];
3433
	f = state[5];
3434
	g = state[6];
3435
	h = state[7];
3436

3437
	j = 0;
3438
	do {
3439
		/* Rounds 0 to 15 (unrolled): */
3440
		ROUND512_0_TO_15(a,b,c,d,e,f,g,h);
3441
		ROUND512_0_TO_15(h,a,b,c,d,e,f,g);
3442
		ROUND512_0_TO_15(g,h,a,b,c,d,e,f);
3443
		ROUND512_0_TO_15(f,g,h,a,b,c,d,e);
3444
		ROUND512_0_TO_15(e,f,g,h,a,b,c,d);
3445
		ROUND512_0_TO_15(d,e,f,g,h,a,b,c);
3446
		ROUND512_0_TO_15(c,d,e,f,g,h,a,b);
3447
		ROUND512_0_TO_15(b,c,d,e,f,g,h,a);
3448
	} while (j < 16);
3449

3450
	/* Now for the remaining rounds up to 79: */
3451
	do {
3452
		ROUND512(a,b,c,d,e,f,g,h);
3453
		ROUND512(h,a,b,c,d,e,f,g);
3454
		ROUND512(g,h,a,b,c,d,e,f);
3455
		ROUND512(f,g,h,a,b,c,d,e);
3456
		ROUND512(e,f,g,h,a,b,c,d);
3457
		ROUND512(d,e,f,g,h,a,b,c);
3458
		ROUND512(c,d,e,f,g,h,a,b);
3459
		ROUND512(b,c,d,e,f,g,h,a);
3460
	} while (j < 80);
3461

3462
	/* Compute the current intermediate hash value */
3463
	state[0] += a;
3464
	state[1] += b;
3465
	state[2] += c;
3466
	state[3] += d;
3467
	state[4] += e;
3468
	state[5] += f;
3469
	state[6] += g;
3470
	state[7] += h;
3471

3472
	/* Clean up */
3473
	a = b = c = d = e = f = g = h = T1 = 0;
3474
}
3475

3476
#else /* SHA2_UNROLL_TRANSFORM */
3477

3478
void
3479
SHA512Transform(uint64_t state[8], const uint8_t data[SHA512_BLOCK_LENGTH])
3480
{
3481
	uint64_t	a, b, c, d, e, f, g, h, s0, s1;
3482
	uint64_t	T1, T2, W512[16];
3483
	int		j;
3484

3485
	/* Initialize registers with the prev. intermediate value */
3486
	a = state[0];
3487
	b = state[1];
3488
	c = state[2];
3489
	d = state[3];
3490
	e = state[4];
3491
	f = state[5];
3492
	g = state[6];
3493
	h = state[7];
3494

3495
	j = 0;
3496
	do {
3497
		BE_8_TO_64(W512[j], data);
3498
		data += 8;
3499
		/* Apply the SHA-512 compression function to update a..h */
3500
		T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + W512[j];
3501
		T2 = Sigma0_512(a) + Maj(a, b, c);
3502
		h = g;
3503
		g = f;
3504
		f = e;
3505
		e = d + T1;
3506
		d = c;
3507
		c = b;
3508
		b = a;
3509
		a = T1 + T2;
3510

3511
		j++;
3512
	} while (j < 16);
3513

3514
	do {
3515
		/* Part of the message block expansion: */
3516
		s0 = W512[(j+1)&0x0f];
3517
		s0 = sigma0_512(s0);
3518
		s1 = W512[(j+14)&0x0f];
3519
		s1 =  sigma1_512(s1);
3520

3521
		/* Apply the SHA-512 compression function to update a..h */
3522
		T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] +
3523
		     (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0);
3524
		T2 = Sigma0_512(a) + Maj(a, b, c);
3525
		h = g;
3526
		g = f;
3527
		f = e;
3528
		e = d + T1;
3529
		d = c;
3530
		c = b;
3531
		b = a;
3532
		a = T1 + T2;
3533

3534
		j++;
3535
	} while (j < 80);
3536

3537
	/* Compute the current intermediate hash value */
3538
	state[0] += a;
3539
	state[1] += b;
3540
	state[2] += c;
3541
	state[3] += d;
3542
	state[4] += e;
3543
	state[5] += f;
3544
	state[6] += g;
3545
	state[7] += h;
3546

3547
	/* Clean up */
3548
	a = b = c = d = e = f = g = h = T1 = T2 = 0;
3549
}
3550

3551
#endif /* SHA2_UNROLL_TRANSFORM */
3552

3553
void
3554
SHA512Update(SHA2_CTX *context, const uint8_t *data, size_t len)
3555
{
3556
	size_t	freespace, usedspace;
3557

3558
	/* Calling with no data is valid (we do nothing) */
3559
	if (len == 0)
3560
		return;
3561

3562
	usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
3563
	if (usedspace > 0) {
3564
		/* Calculate how much free space is available in the buffer */
3565
		freespace = SHA512_BLOCK_LENGTH - usedspace;
3566

3567
		if (len >= freespace) {
3568
			/* Fill the buffer completely and process it */
3569
			memcpy(&context->buffer[usedspace], data, freespace);
3570
			ADDINC128(context->bitcount, freespace << 3);
3571
			len -= freespace;
3572
			data += freespace;
3573
			SHA512Transform(context->state.st64, context->buffer);
3574
		} else {
3575
			/* The buffer is not yet full */
3576
			memcpy(&context->buffer[usedspace], data, len);
3577
			ADDINC128(context->bitcount, len << 3);
3578
			/* Clean up: */
3579
			usedspace = freespace = 0;
3580
			return;
3581
		}
3582
	}
3583
	while (len >= SHA512_BLOCK_LENGTH) {
3584
		/* Process as many complete blocks as we can */
3585
		SHA512Transform(context->state.st64, data);
3586
		ADDINC128(context->bitcount, SHA512_BLOCK_LENGTH << 3);
3587
		len -= SHA512_BLOCK_LENGTH;
3588
		data += SHA512_BLOCK_LENGTH;
3589
	}
3590
	if (len > 0) {
3591
		/* There's left-overs, so save 'em */
3592
		memcpy(context->buffer, data, len);
3593
		ADDINC128(context->bitcount, len << 3);
3594
	}
3595
	/* Clean up: */
3596
	usedspace = freespace = 0;
3597
}
3598

3599
void
3600
SHA512Pad(SHA2_CTX *context)
3601
{
3602
	unsigned int	usedspace;
3603

3604
	usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
3605
	if (usedspace > 0) {
3606
		/* Begin padding with a 1 bit: */
3607
		context->buffer[usedspace++] = 0x80;
3608

3609
		if (usedspace <= SHA512_SHORT_BLOCK_LENGTH) {
3610
			/* Set-up for the last transform: */
3611
			memset(&context->buffer[usedspace], 0, SHA512_SHORT_BLOCK_LENGTH - usedspace);
3612
		} else {
3613
			if (usedspace < SHA512_BLOCK_LENGTH) {
3614
				memset(&context->buffer[usedspace], 0, SHA512_BLOCK_LENGTH - usedspace);
3615
			}
3616
			/* Do second-to-last transform: */
3617
			SHA512Transform(context->state.st64, context->buffer);
3618

3619
			/* And set-up for the last transform: */
3620
			memset(context->buffer, 0, SHA512_BLOCK_LENGTH - 2);
3621
		}
3622
	} else {
3623
		/* Prepare for final transform: */
3624
		memset(context->buffer, 0, SHA512_SHORT_BLOCK_LENGTH);
3625

3626
		/* Begin padding with a 1 bit: */
3627
		*context->buffer = 0x80;
3628
	}
3629
	/* Store the length of input data (in bits) in big endian format: */
3630
	BE_64_TO_8(&context->buffer[SHA512_SHORT_BLOCK_LENGTH],
3631
	    context->bitcount[1]);
3632
	BE_64_TO_8(&context->buffer[SHA512_SHORT_BLOCK_LENGTH + 8],
3633
	    context->bitcount[0]);
3634

3635
	/* Final transform: */
3636
	SHA512Transform(context->state.st64, context->buffer);
3637

3638
	/* Clean up: */
3639
	usedspace = 0;
3640
}
3641

3642
void
3643
SHA512Final(uint8_t digest[SHA512_DIGEST_LENGTH], SHA2_CTX *context)
3644
{
3645
	SHA512Pad(context);
3646

3647
#if BYTE_ORDER == LITTLE_ENDIAN
3648
	int	i;
3649

3650
	/* Convert TO host byte order */
3651
	for (i = 0; i < 8; i++)
3652
		BE_64_TO_8(digest + i * 8, context->state.st64[i]);
3653
#else
3654
	memcpy(digest, context->state.st64, SHA512_DIGEST_LENGTH);
3655
#endif
3656
	explicit_bzero(context, sizeof(*context));
3657
}
3658

3659
/*** SHA-384: *********************************************************/
3660
void
3661
SHA384Init(SHA2_CTX *context)
3662
{
3663
	memcpy(context->state.st64, sha384_initial_hash_value,
3664
	    sizeof(sha384_initial_hash_value));
3665
	memset(context->buffer, 0, sizeof(context->buffer));
3666
	context->bitcount[0] = context->bitcount[1] = 0;
3667
}
3668

3669
MAKE_CLONE(SHA384Transform, SHA512Transform);
3670
MAKE_CLONE(SHA384Update, SHA512Update);
3671
MAKE_CLONE(SHA384Pad, SHA512Pad);
3672

3673
/* Equivalent of MAKE_CLONE (which is a no-op) for SHA384 funcs */
3674
void
3675
SHA384Transform(uint64_t state[8], const uint8_t data[SHA512_BLOCK_LENGTH])
3676
{
3677
	SHA512Transform(state, data);
3678
}
3679

3680
void
3681
SHA384Update(SHA2_CTX *context, const uint8_t *data, size_t len)
3682
{
3683
	SHA512Update(context, data, len);
3684
}
3685

3686
void
3687
SHA384Pad(SHA2_CTX *context)
3688
{
3689
	SHA512Pad(context);
3690
}
3691

3692
void
3693
SHA384Final(uint8_t digest[SHA384_DIGEST_LENGTH], SHA2_CTX *context)
3694
{
3695
	SHA384Pad(context);
3696

3697
#if BYTE_ORDER == LITTLE_ENDIAN
3698
	int	i;
3699

3700
	/* Convert TO host byte order */
3701
	for (i = 0; i < 6; i++)
3702
		BE_64_TO_8(digest + i * 8, context->state.st64[i]);
3703
#else
3704
	memcpy(digest, context->state.st64, SHA384_DIGEST_LENGTH);
3705
#endif
3706
	/* Zero out state data */
3707
	explicit_bzero(context, sizeof(*context));
3708
}
3709

3710
char *
3711
SHA256End(SHA2_CTX *ctx, char *buf)
3712
{
3713
	int i;
3714
	uint8_t digest[SHA256_DIGEST_LENGTH];
3715
	static const char hex[] = "0123456789abcdef";
3716

3717
	if (buf == NULL && (buf = malloc(SHA256_DIGEST_STRING_LENGTH)) == NULL)
3718
		return (NULL);
3719

3720
	SHA256Final(digest, ctx);
3721
	for (i = 0; i < SHA256_DIGEST_LENGTH; i++) {
3722
		buf[i + i] = hex[digest[i] >> 4];
3723
		buf[i + i + 1] = hex[digest[i] & 0x0f];
3724
	}
3725
	buf[i + i] = '\0';
3726
	explicit_bzero(digest, sizeof(digest));
3727
	return (buf);
3728
}
3729

3730
char *
3731
SHA384End(SHA2_CTX *ctx, char *buf)
3732
{
3733
	int i;
3734
	uint8_t digest[SHA384_DIGEST_LENGTH];
3735
	static const char hex[] = "0123456789abcdef";
3736

3737
	if (buf == NULL && (buf = malloc(SHA384_DIGEST_STRING_LENGTH)) == NULL)
3738
		return (NULL);
3739

3740
	SHA384Final(digest, ctx);
3741
	for (i = 0; i < SHA384_DIGEST_LENGTH; i++) {
3742
		buf[i + i] = hex[digest[i] >> 4];
3743
		buf[i + i + 1] = hex[digest[i] & 0x0f];
3744
	}
3745
	buf[i + i] = '\0';
3746
	explicit_bzero(digest, sizeof(digest));
3747
	return (buf);
3748
}
3749

3750
char *
3751
SHA512End(SHA2_CTX *ctx, char *buf)
3752
{
3753
	int i;
3754
	uint8_t digest[SHA512_DIGEST_LENGTH];
3755
	static const char hex[] = "0123456789abcdef";
3756

3757
	if (buf == NULL && (buf = malloc(SHA512_DIGEST_STRING_LENGTH)) == NULL)
3758
		return (NULL);
3759

3760
	SHA512Final(digest, ctx);
3761
	for (i = 0; i < SHA512_DIGEST_LENGTH; i++) {
3762
		buf[i + i] = hex[digest[i] >> 4];
3763
		buf[i + i + 1] = hex[digest[i] & 0x0f];
3764
	}
3765
	buf[i + i] = '\0';
3766
	explicit_bzero(digest, sizeof(digest));
3767
	return (buf);
3768
}
3769

3770
char *
3771
SHA256FileChunk(const char *filename, char *buf, off_t off, off_t len)
3772
{
3773
	struct stat sb;
3774
	u_char buffer[BUFSIZ];
3775
	SHA2_CTX ctx;
3776
	int fd, save_errno;
3777
	ssize_t nr;
3778

3779
	SHA256Init(&ctx);
3780

3781
	if ((fd = open(filename, O_RDONLY)) == -1)
3782
		return (NULL);
3783
	if (len == 0) {
3784
		if (fstat(fd, &sb) == -1) {
3785
			save_errno = errno;
3786
			close(fd);
3787
			errno = save_errno;
3788
			return (NULL);
3789
		}
3790
		len = sb.st_size;
3791
	}
3792
	if (off > 0 && lseek(fd, off, SEEK_SET) == -1) {
3793
		save_errno = errno;
3794
		close(fd);
3795
		errno = save_errno;
3796
		return (NULL);
3797
	}
3798

3799
	while ((nr = read(fd, buffer, MINIMUM(sizeof(buffer), (size_t)len))) > 0) {
3800
		SHA256Update(&ctx, buffer, nr);
3801
		if (len > 0 && (len -= nr) == 0)
3802
			break;
3803
	}
3804

3805
	save_errno = errno;
3806
	close(fd);
3807
	errno = save_errno;
3808
	return (nr == -1 ? NULL : SHA256End(&ctx, buf));
3809
}
3810

3811
char *
3812
SHA256File(const char *filename, char *buf)
3813
{
3814
	return (SHA256FileChunk(filename, buf, 0, 0));
3815
}
3816

3817
char *
3818
SHA384FileChunk(const char *filename, char *buf, off_t off, off_t len)
3819
{
3820
	struct stat sb;
3821
	u_char buffer[BUFSIZ];
3822
	SHA2_CTX ctx;
3823
	int fd, save_errno;
3824
	ssize_t nr;
3825

3826
	SHA384Init(&ctx);
3827

3828
	if ((fd = open(filename, O_RDONLY)) == -1)
3829
		return (NULL);
3830
	if (len == 0) {
3831
		if (fstat(fd, &sb) == -1) {
3832
			save_errno = errno;
3833
			close(fd);
3834
			errno = save_errno;
3835
			return (NULL);
3836
		}
3837
		len = sb.st_size;
3838
	}
3839
	if (off > 0 && lseek(fd, off, SEEK_SET) == -1) {
3840
		save_errno = errno;
3841
		close(fd);
3842
		errno = save_errno;
3843
		return (NULL);
3844
	}
3845

3846
	while ((nr = read(fd, buffer, MINIMUM(sizeof(buffer), (size_t)len))) > 0) {
3847
		SHA384Update(&ctx, buffer, nr);
3848
		if (len > 0 && (len -= nr) == 0)
3849
			break;
3850
	}
3851

3852
	save_errno = errno;
3853
	close(fd);
3854
	errno = save_errno;
3855
	return (nr == -1 ? NULL : SHA384End(&ctx, buf));
3856
}
3857

3858
char *
3859
SHA384File(const char *filename, char *buf)
3860
{
3861
	return (SHA384FileChunk(filename, buf, 0, 0));
3862
}
3863

3864
char *
3865
SHA512FileChunk(const char *filename, char *buf, off_t off, off_t len)
3866
{
3867
	struct stat sb;
3868
	u_char buffer[BUFSIZ];
3869
	SHA2_CTX ctx;
3870
	int fd, save_errno;
3871
	ssize_t nr;
3872

3873
	SHA512Init(&ctx);
3874

3875
	if ((fd = open(filename, O_RDONLY)) == -1)
3876
		return (NULL);
3877
	if (len == 0) {
3878
		if (fstat(fd, &sb) == -1) {
3879
			save_errno = errno;
3880
			close(fd);
3881
			errno = save_errno;
3882
			return (NULL);
3883
		}
3884
		len = sb.st_size;
3885
	}
3886
	if (off > 0 && lseek(fd, off, SEEK_SET) == -1) {
3887
		save_errno = errno;
3888
		close(fd);
3889
		errno = save_errno;
3890
		return (NULL);
3891
	}
3892

3893
	while ((nr = read(fd, buffer, MINIMUM(sizeof(buffer), (size_t)len))) > 0) {
3894
		SHA512Update(&ctx, buffer, nr);
3895
		if (len > 0 && (len -= nr) == 0)
3896
			break;
3897
	}
3898

3899
	save_errno = errno;
3900
	close(fd);
3901
	errno = save_errno;
3902
	return (nr == -1 ? NULL : SHA512End(&ctx, buf));
3903
}
3904

3905
char *
3906
SHA512File(const char *filename, char *buf)
3907
{
3908
	return (SHA512FileChunk(filename, buf, 0, 0));
3909
}
3910

3911
char *
3912
SHA256Data(const u_char *data, size_t len, char *buf)
3913
{
3914
	SHA2_CTX ctx;
3915

3916
	SHA256Init(&ctx);
3917
	SHA256Update(&ctx, data, len);
3918
	return (SHA256End(&ctx, buf));
3919
}
3920

3921
char *
3922
SHA384Data(const u_char *data, size_t len, char *buf)
3923
{
3924
	SHA2_CTX ctx;
3925

3926
	SHA384Init(&ctx);
3927
	SHA384Update(&ctx, data, len);
3928
	return (SHA384End(&ctx, buf));
3929
}
3930

3931
char *
3932
SHA512Data(const u_char *data, size_t len, char *buf)
3933
{
3934
	SHA2_CTX ctx;
3935

3936
	SHA512Init(&ctx);
3937
	SHA512Update(&ctx, data, len);
3938
	return (SHA512End(&ctx, buf));
3939
}
3940
#endif /* !HAVE_SHA2 */
3941
#if !HAVE_STRLCAT
3942
/*
3943
 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
3944
 *
3945
 * Permission to use, copy, modify, and distribute this software for any
3946
 * purpose with or without fee is hereby granted, provided that the above
3947
 * copyright notice and this permission notice appear in all copies.
3948
 *
3949
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
3950
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
3951
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
3952
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
3953
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
3954
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
3955
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
3956
 */
3957

3958
#include <sys/types.h>
3959
#include <string.h>
3960

3961
/*
3962
 * Appends src to string dst of size siz (unlike strncat, siz is the
3963
 * full size of dst, not space left).  At most siz-1 characters
3964
 * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
3965
 * Returns strlen(src) + MIN(siz, strlen(initial dst)).
3966
 * If retval >= siz, truncation occurred.
3967
 */
3968
size_t
3969
strlcat(char *dst, const char *src, size_t siz)
3970
{
3971
	char *d = dst;
3972
	const char *s = src;
3973
	size_t n = siz;
3974
	size_t dlen;
3975

3976
	/* Find the end of dst and adjust bytes left but don't go past end */
3977
	while (n-- != 0 && *d != '\0')
3978
		d++;
3979
	dlen = d - dst;
3980
	n = siz - dlen;
3981

3982
	if (n == 0)
3983
		return(dlen + strlen(s));
3984
	while (*s != '\0') {
3985
		if (n != 1) {
3986
			*d++ = *s;
3987
			n--;
3988
		}
3989
		s++;
3990
	}
3991
	*d = '\0';
3992

3993
	return(dlen + (s - src));	/* count does not include NUL */
3994
}
3995
#endif /* !HAVE_STRLCAT */
3996
#if !HAVE_STRLCPY
3997
/*
3998
 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
3999
 *
4000
 * Permission to use, copy, modify, and distribute this software for any
4001
 * purpose with or without fee is hereby granted, provided that the above
4002
 * copyright notice and this permission notice appear in all copies.
4003
 *
4004
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
4005
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
4006
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
4007
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
4008
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
4009
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
4010
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
4011
 */
4012

4013
#include <sys/types.h>
4014
#include <string.h>
4015

4016
/*
4017
 * Copy src to string dst of size siz.  At most siz-1 characters
4018
 * will be copied.  Always NUL terminates (unless siz == 0).
4019
 * Returns strlen(src); if retval >= siz, truncation occurred.
4020
 */
4021
size_t
4022
strlcpy(char *dst, const char *src, size_t siz)
4023
{
4024
	char *d = dst;
4025
	const char *s = src;
4026
	size_t n = siz;
4027

4028
	/* Copy as many bytes as will fit */
4029
	if (n != 0) {
4030
		while (--n != 0) {
4031
			if ((*d++ = *s++) == '\0')
4032
				break;
4033
		}
4034
	}
4035

4036
	/* Not enough room in dst, add NUL and traverse rest of src */
4037
	if (n == 0) {
4038
		if (siz != 0)
4039
			*d = '\0';		/* NUL-terminate dst */
4040
		while (*s++)
4041
			;
4042
	}
4043

4044
	return(s - src - 1);	/* count does not include NUL */
4045
}
4046
#endif /* !HAVE_STRLCPY */
4047
#if !HAVE_STRNDUP
4048
/*	$OpenBSD$	*/
4049
/*
4050
 * Copyright (c) 2010 Todd C. Miller <Todd.Miller@courtesan.com>
4051
 *
4052
 * Permission to use, copy, modify, and distribute this software for any
4053
 * purpose with or without fee is hereby granted, provided that the above
4054
 * copyright notice and this permission notice appear in all copies.
4055
 *
4056
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
4057
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
4058
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
4059
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
4060
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
4061
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
4062
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
4063
 */
4064

4065
#include <sys/types.h>
4066

4067
#include <stddef.h>
4068
#include <stdlib.h>
4069
#include <string.h>
4070

4071
char *
4072
strndup(const char *str, size_t maxlen)
4073
{
4074
	char *copy;
4075
	size_t len;
4076

4077
	len = strnlen(str, maxlen);
4078
	copy = malloc(len + 1);
4079
	if (copy != NULL) {
4080
		(void)memcpy(copy, str, len);
4081
		copy[len] = '\0';
4082
	}
4083

4084
	return copy;
4085
}
4086
#endif /* !HAVE_STRNDUP */
4087
#if !HAVE_STRNLEN
4088
/*	$OpenBSD$	*/
4089

4090
/*
4091
 * Copyright (c) 2010 Todd C. Miller <Todd.Miller@courtesan.com>
4092
 *
4093
 * Permission to use, copy, modify, and distribute this software for any
4094
 * purpose with or without fee is hereby granted, provided that the above
4095
 * copyright notice and this permission notice appear in all copies.
4096
 *
4097
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
4098
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
4099
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
4100
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
4101
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
4102
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
4103
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
4104
 */
4105

4106
#include <sys/types.h>
4107
#include <string.h>
4108

4109
size_t
4110
strnlen(const char *str, size_t maxlen)
4111
{
4112
	const char *cp;
4113

4114
	for (cp = str; maxlen != 0 && *cp != '\0'; cp++, maxlen--)
4115
		;
4116

4117
	return (size_t)(cp - str);
4118
}
4119
#endif /* !HAVE_STRNLEN */
4120
#if !HAVE_STRTONUM
4121
/*
4122
 * Copyright (c) 2004 Ted Unangst and Todd Miller
4123
 * All rights reserved.
4124
 *
4125
 * Permission to use, copy, modify, and distribute this software for any
4126
 * purpose with or without fee is hereby granted, provided that the above
4127
 * copyright notice and this permission notice appear in all copies.
4128
 *
4129
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
4130
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
4131
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
4132
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
4133
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
4134
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
4135
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
4136
 */
4137

4138
#include <errno.h>
4139
#include <limits.h>
4140
#include <stdlib.h>
4141

4142
#define	INVALID		1
4143
#define	TOOSMALL	2
4144
#define	TOOLARGE	3
4145

4146
long long
4147
strtonum(const char *numstr, long long minval, long long maxval,
4148
    const char **errstrp)
4149
{
4150
	long long ll = 0;
4151
	int error = 0;
4152
	char *ep;
4153
	struct errval {
4154
		const char *errstr;
4155
		int err;
4156
	} ev[4] = {
4157
		{ NULL,		0 },
4158
		{ "invalid",	EINVAL },
4159
		{ "too small",	ERANGE },
4160
		{ "too large",	ERANGE },
4161
	};
4162

4163
	ev[0].err = errno;
4164
	errno = 0;
4165
	if (minval > maxval) {
4166
		error = INVALID;
4167
	} else {
4168
		ll = strtoll(numstr, &ep, 10);
4169
		if (numstr == ep || *ep != '\0')
4170
			error = INVALID;
4171
		else if ((ll == LLONG_MIN && errno == ERANGE) || ll < minval)
4172
			error = TOOSMALL;
4173
		else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval)
4174
			error = TOOLARGE;
4175
	}
4176
	if (errstrp != NULL)
4177
		*errstrp = ev[error].errstr;
4178
	errno = ev[error].err;
4179
	if (error)
4180
		ll = 0;
4181

4182
	return (ll);
4183
}
4184
#endif /* !HAVE_STRTONUM */
4185

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.