/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8   -*-â
âvi: set et ft=c ts=8 tw=8 fenc=utf-8                                       :viâ
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â                                                                              â
â  Musl Libc                                                                   â
â  Copyright Â© 2005-2014 Rich Felker, et al.                                   â
â                                                                              â
â  Permission is hereby granted, free of charge, to any person obtaining       â
â  a copy of this software and associated documentation files (the             â
â  "Software"), to deal in the Software without restriction, including         â
â  without limitation the rights to use, copy, modify, merge, publish,         â
â  distribute, sublicense, and/or sell copies of the Software, and to          â
â  permit persons to whom the Software is furnished to do so, subject to       â
â  the following conditions:                                                   â
â                                                                              â
â  The above copyright notice and this permission notice shall be              â
â  included in all copies or substantial portions of the Software.             â
â                                                                              â
â  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,             â
â  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF          â
â  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.      â
â  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY        â
â  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,        â
â  TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE           â
â  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                      â
â                                                                              â
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ*/
#include "libc/mem/alg.h"
#include "libc/calls/calls.h"
#include "libc/calls/weirdtypes.h"
#include "libc/fmt/conv.h"
#include "libc/limits.h"
#include "libc/literal.h"
#include "libc/mem/mem.h"
#include "libc/stdio/rand.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/temp.h"
#include "libc/sysv/consts/exit.h"
#include "libc/sysv/consts/fileno.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/ok.h"
#include "third_party/gdtoa/gdtoa.h"
#include "third_party/getopt/getopt.h"
#include "third_party/musl/crypt_des.internal.h"

asm(".ident\t\"\\n\\n\
Musl libc (MIT License)\\n\
Copyright 2005-2014 Rich Felker, et. al.\"");
asm(".include \"libc/disclaimer.inc\"");
// clang-format off

static struct expanded_key __encrypt_key;

void setkey(const char *key)
{
	unsigned char bkey[8];
	int i, j;

	for (i = 0; i < 8; i++) {
		bkey[i] = 0;
		for (j = 7; j >= 0; j--, key++)
			bkey[i] |= (uint32_t)(*key & 1) << j;
	}

	__des_setkey(bkey, &__encrypt_key);
}

void encrypt(char *block, int edflag)
{
	struct expanded_key decrypt_key, *key;
	uint32_t b[2];
	int i, j;
	char *p;

	p = block;
	for (i = 0; i < 2; i++) {
		b[i] = 0;
		for (j = 31; j >= 0; j--, p++)
			b[i] |= (uint32_t)(*p & 1) << j;
	}

	key = &__encrypt_key;
	if (edflag) {
		key = &decrypt_key;
		for (i = 0; i < 16; i++) {
			decrypt_key.l[i] = __encrypt_key.l[15-i];
			decrypt_key.r[i] = __encrypt_key.r[15-i];
		}
	}

	__do_des(b[0], b[1], b, b + 1, 1, 0, key);

	p = block;
	for (i = 0; i < 2; i++)
		for (j = 31; j >= 0; j--)
			*p++ = b[i]>>j & 1;
}
