// File: /workspace/github_c_makefile_star2_O2/ajpaulson[P]srogue11/xcrypt.c
// Function: xcrypt
char *xcrypt(const char *key,const char *setting)
{
	int		i;
	unsigned int	count, salt, l, r0, r1, keybuf[2];
	unsigned char		*p, *q;
	static unsigned char	output[21];

	if (!des_initialised)
		des_init();

	/*
	 * Copy the key, shifting each character up by one bit
	 * and padding with zeros.
	 */
	q = (unsigned char *) keybuf;
	while ((q - (unsigned char *) keybuf) < sizeof(keybuf)) {
		if ((*q++ = *key << 1))
			key++;
	}
	if (des_setkey((const char *) keybuf))
		return(NULL);

	if (*setting == _PASSWORD_EFMT1) {
		/*
		 * "new"-style:
		 *	setting - underscore, 4 bytes of count, 4 bytes of salt
		 *	key - unlimited characters
		 */
		for (i = 1, count = 0; i < 5; i++)
			count |= ascii_to_bin(setting[i]) << (i - 1) * 6;

		for (i = 5, salt = 0; i < 9; i++)
			salt |= ascii_to_bin(setting[i]) << (i - 5) * 6;

		while (*key) {
			/*
			 * Encrypt the key with itself.
			 */
			if (des_cipher((const char*)keybuf, (char*)keybuf, 0, 1))
				return(NULL);
			/*
			 * And XOR with the next 8 characters of the key.
			 */
			q = (unsigned char *) keybuf;
			while (((q - (unsigned char *) keybuf) < sizeof(keybuf)) &&
					*key)
				*q++ ^= *key++ << 1;

			if (des_setkey((const char *) keybuf))
				return(NULL);
		}
		strncpy((char *)output, setting, 9);

		/*
		 * Double check that we weren't given a short setting.
		 * If we were, the above code will probably have created
		 * wierd values for count and salt, but we don't really care.
		 * Just make sure the output string doesn't have an extra
		 * NUL in it.
		 */
		output[9] = '\0';
		p = output + strlen((const char *)output);
	} else {
		/*
		 * "old"-style:
		 *	setting - 2 bytes of salt
		 *	key - up to 8 characters
		 */
		count = 25;

		salt = (ascii_to_bin(setting[1]) << 6)
		     |  ascii_to_bin(setting[0]);

		output[0] = setting[0];
		/*
		 * If the encrypted password that the salt was extracted from
		 * is only 1 character long, the salt will be corrupted.  We
		 * need to ensure that the output string doesn't have an extra
		 * NUL in it!
		 */
		output[1] = setting[1] ? setting[1] : output[0];

		p = output + 2;
	}
	setup_salt(salt);
	/*
	 * Do it.
	 */
	if (do_des(0, 0, &r0, &r1, count))
		return(NULL);
	/*
	 * Now encode the result...
	 */
	l = (r0 >> 8);
	*p++ = ascii64[(l >> 18) & 0x3f];
	*p++ = ascii64[(l >> 12) & 0x3f];
	*p++ = ascii64[(l >> 6) & 0x3f];
	*p++ = ascii64[l & 0x3f];

	l = (r0 << 16) | ((r1 >> 16) & 0xffff);
	*p++ = ascii64[(l >> 18) & 0x3f];
	*p++ = ascii64[(l >> 12) & 0x3f];
	*p++ = ascii64[(l >> 6) & 0x3f];
	*p++ = ascii64[l & 0x3f];

	l = r1 << 2;
	*p++ = ascii64[(l >> 12) & 0x3f];
	*p++ = ascii64[(l >> 6) & 0x3f];
	*p++ = ascii64[l & 0x3f];
	*p = 0;

	return((char *)output);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %rbx
movq %rdi, %r14
cmpb $0x0, 0x17979(%rip)     # 0x33548
jne 0x1bbd6
callq 0x1c442
xorl %eax, %eax
cmpq $0x7, %rax
ja 0x1bbf2
movb (%r14), %cl
addb %cl, %cl
movb %cl, (%rsp,%rax)
cmpb $0x1, %cl
sbbq $-0x1, %r14
incq %rax
jmp 0x1bbd8
movq %rsp, %rdi
callq 0x1be45
movb (%rbx), %r12b
cmpb $0x5f, %r12b
jne 0x1bc2e
movq %rbx, 0x10(%rsp)
leaq 0x1(%rbx), %r15
xorl %ebx, %ebx
xorl %ebp, %ebp
cmpq $0x18, %rbx
je 0x1bc72
movsbl (%r15), %edi
callq 0x1c0c0
movl %ebx, %ecx
shll %cl, %eax
orl %eax, %ebp
addq $0x6, %rbx
incq %r15
jmp 0x1bc10
movsbl 0x1(%rbx), %ebx
movl %ebx, %edi
callq 0x1c0c0
movl %eax, %ebp
shll $0x6, %ebp
movsbl %r12b, %edi
callq 0x1c0c0
movl %eax, %r15d
orl %ebp, %r15d
testl %ebx, %ebx
movzbl %r12b, %eax
cmovnel %ebx, %eax
movb %r12b, 0x178d3(%rip)    # 0x33530
movb %al, 0x178ce(%rip)      # 0x33531
leaq 0x178c8(%rip), %rbx     # 0x33532
pushq $0x19
popq %rbp
jmp 0x1bd4f
movq 0x10(%rsp), %rax
leaq 0x5(%rax), %r12
xorl %ebx, %ebx
xorl %r15d, %r15d
cmpq $0x18, %rbx
je 0x1bca0
movsbl (%r12), %edi
callq 0x1c0c0
movl %ebx, %ecx
shll %cl, %eax
orl %eax, %r15d
addq $0x6, %rbx
incq %r12
jmp 0x1bc80
leaq 0x8(%rsp), %rbx
pushq $0x1
popq %r12
cmpb $0x0, (%r14)
je 0x1bd23
cmpb $0x0, 0x17892(%rip)     # 0x33548
jne 0x1bcbd
callq 0x1c442
xorl %edi, %edi
callq 0x1c0f5
movl (%rsp), %edi
movl 0x4(%rsp), %esi
bswapl %edi
bswapl %esi
leaq 0xc(%rsp), %rdx
movq %rbx, %rcx
movl %r12d, %r8d
callq 0x1c12f
movl 0xc(%rsp), %ecx
bswapl %ecx
movl 0x8(%rsp), %edx
bswapl %edx
movl %ecx, (%rsp)
movl %edx, 0x4(%rsp)
testl %eax, %eax
jne 0x1be30
xorl %r13d, %r13d
cmpq $0x7, %r13
ja 0x1bd16
movb (%r14,%r13), %al
testb %al, %al
je 0x1bd16
addb %al, %al
xorb %al, (%rsp,%r13)
incq %r13
jmp 0x1bcfd
movq %rsp, %rdi
callq 0x1be45
addq %r13, %r14
jmp 0x1bca9
leaq 0x17806(%rip), %r14     # 0x33530
pushq $0x9
popq %rdx
movq %r14, %rdi
movq 0x10(%rsp), %rsi
callq 0x60b0
movb $0x0, 0x177f8(%rip)     # 0x33539
movq %r14, %rdi
callq 0x6170
movq %rax, %rbx
addq %r14, %rbx
movl %r15d, %edi
callq 0x1c0f5
xorl %r14d, %r14d
leaq 0xc(%rsp), %rdx
leaq 0x8(%rsp), %rcx
xorl %edi, %edi
xorl %esi, %esi
movl %ebp, %r8d
callq 0x1c12f
testl %eax, %eax
jne 0x1be33
movl 0xc(%rsp), %ecx
movl %ecx, %edx
shrl $0x8, %edx
movl %ecx, %esi
shrl $0x1a, %esi
leaq 0x5c93(%rip), %rax      # 0x21a20
movb (%rsi,%rax), %sil
movb %sil, (%rbx)
movl %ecx, %esi
shrl $0x14, %esi
andl $0x3f, %esi
movb (%rsi,%rax), %sil
movb %sil, 0x1(%rbx)
movl %ecx, %esi
shrl $0xe, %esi
andl $0x3f, %esi
movb (%rsi,%rax), %sil
movb %sil, 0x2(%rbx)
andl $0x3f, %edx
movb (%rdx,%rax), %dl
movb %dl, 0x3(%rbx)
movl 0x8(%rsp), %edx
shldl $0x10, %edx, %ecx
movl %ecx, %esi
shrl $0x12, %esi
andl $0x3f, %esi
movb (%rsi,%rax), %sil
movb %sil, 0x4(%rbx)
movl %ecx, %esi
shrl $0xc, %esi
andl $0x3f, %esi
movb (%rsi,%rax), %sil
movb %sil, 0x5(%rbx)
movl %ecx, %esi
shrl $0x6, %esi
andl $0x3f, %esi
movb (%rsi,%rax), %sil
movb %sil, 0x6(%rbx)
andl $0x3f, %ecx
movb (%rcx,%rax), %cl
movb %cl, 0x7(%rbx)
movl %edx, %ecx
shrl $0xa, %ecx
andl $0x3f, %ecx
movb (%rcx,%rax), %cl
movb %cl, 0x8(%rbx)
movl %edx, %ecx
shrl $0x4, %ecx
andl $0x3f, %ecx
movb (%rcx,%rax), %cl
movb %cl, 0x9(%rbx)
andl $0xf, %edx
movb (%rax,%rdx,4), %al
movb %al, 0xa(%rbx)
movb $0x0, 0xb(%rbx)
leaq 0x17702(%rip), %r14     # 0x33530
jmp 0x1be33
xorl %r14d, %r14d
movq %r14, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq