File: common.c

package info (click to toggle)
libzc 0.4.3-1
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 872 kB
  • sloc: ansic: 7,407; sh: 327; python: 171; makefile: 99
file content (121 lines) | stat: -rw-r--r-- 2,778 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 *  zc - zip crack library
 *  Copyright (C) 2012-2018 Marc Ferland
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <unistd.h>

#include "libzc_private.h"
#include "decrypt_byte.h"

int fill_header(struct zc_ctx *ctx, const char *filename,
		struct zc_header *h,
		size_t len)
{
	struct zc_file *file;
	int err;

	err = zc_file_new_from_filename(ctx, filename, &file);
	if (err)
		return -1;

	err = zc_file_open(file);
	if (err) {
		zc_file_unref(file);
		return -1;
	}

	int size = read_zc_header(file, h, len);

	zc_file_close(file);
	zc_file_unref(file);

	return size;
}

int fill_test_cipher(struct zc_ctx *ctx, const char *filename,
		     unsigned char **buf, size_t *len,
		     uint32_t *original_crc, bool *is_deflated)
{
	struct zc_file *file;
	int err;

	err = zc_file_new_from_filename(ctx, filename, &file);
	if (err)
		goto err1;

	err = zc_file_open(file);
	if (err)
		goto err2;

	err = read_crypt_data(file, buf, len, original_crc, is_deflated);
	zc_file_close(file);
	zc_file_unref(file);

	return err ? -1 : 0;

err2:
	zc_file_unref(file);
err1:
	return -1;
}

void decrypt(const unsigned char *in, unsigned char *out, size_t len,
	     const struct zc_key *key)
{
	struct zc_key k = *key;

	for (size_t i = 0; i < len - 1; ++i) {
		out[i] = in[i] ^ decrypt_byte_lookup(k.key2);
		update_keys(out[i], &k, &k);
	}

	out[len - 1] = in[len - 1] ^ decrypt_byte_lookup(k.key2);
}

uint8_t decrypt_header(const uint8_t *buf, struct zc_key *k, uint8_t magic)
{
	for (size_t i = 0; i < ENC_HEADER_LEN - 1; ++i) {
		uint8_t c = buf[i] ^ decrypt_byte_lookup(k->key2);
		update_keys(c, k, k);
	}

	/* Returns the last byte of the decrypted header */
	return buf[ENC_HEADER_LEN - 1] ^ decrypt_byte_lookup(k->key2) ^ magic;
}

bool decrypt_headers(const struct zc_key *k, const struct zc_header *h, size_t len)
{
	struct zc_key tmp;

	for (size_t i = 0; i < len; ++i) {
		reset_encryption_keys(k, &tmp);
		if (decrypt_header(h[i].buf, &tmp, h[i].magic))
			return false;
	}

	return true;
}

long threads_to_create(long forced)
{
	if (forced > 0)
		return forced;
	long n = sysconf(_SC_NPROCESSORS_ONLN);
	if (n < 1)
		return 1;
	return n;
}