aboutsummaryrefslogtreecommitdiffstats
path: root/tools/firmware-utils/src/nec-enc.c
blob: 3c4e38721e2bc0b260393e78b89d2ccc92439b85 (plain)
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
pre { line-height: 125%; margin: 0; }
td.linenos pre { color: #000000; background-color: #f0f0f0; padding: 0 5px 0 5px; }
span.linenos { color: #000000; background-color: #f0f0f0; padding: 0 5px 0 5px; }
td.linenos pre.special { color: #000000; background-color: #ffffc0; padding: 0 5px 0 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding: 0 5px 0 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight { background: #ffffff; }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /*
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * nec-enc.c - encode/decode nec firmware with key
 *
 * based on xorimage.c in OpenWrt
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>

#define KEY_LEN     16
#define PATTERN_LEN 251

static int
xor_pattern(uint8_t *data, size_t len, const char *key, int k_len, int k_off)
{
	int offset = k_off;

	while (len--) {
		*data ^= key[offset];
		data++;
		offset = (offset + 1) % k_len;
	}

	return offset;
}

static void xor_data(uint8_t *data, size_t len, const uint8_t *pattern)
{
	for (int i = 0; i < len; i++) {
		*data ^= pattern[i];
		data++;
	}
}

static void __attribute__((noreturn)) usage(void)
{
	fprintf(stderr, "Usage: nec-enc -i infile -o outfile -k <key>\n");
	exit(EXIT_FAILURE);
}

static unsigned char buf_pattern[4096], buf[4096];

int main(int argc, char **argv)
{
	int k_off = 0, ptn = 0, c, ret = EXIT_SUCCESS;
	char *ifn = NULL, *ofn = NULL, *key = NULL;
	size_t n, k_len;
	FILE *out, *in;

	while ((c = getopt(argc, argv, "i:o:k:h")) != -1) {
		switch (c) {
		case 'i':
			ifn = optarg;
			break;
		case 'o':
			ofn = optarg;
			break;
		case 'k':
			key = optarg;
			break;
		case 'h':
		default:
			usage();
		}
	}

	if (optind != argc || optind == 1) {
		fprintf(stderr, "illegal arg \"%s\"\n", argv[optind]);
		usage();
	}

	in = fopen(ifn, "r");
	if (!in) {
		perror("can not open input file");
		usage();
	}

	out = fopen(ofn, "w");
	if (!out) {
		perror("can not open output file");
		usage();
	}

	if (!key) {
		fprintf(stderr, "key is not specified\n");
		usage();
	}

	k_len = strnlen(key, KEY_LEN + 1);
	if (k_len == 0 || k_len > KEY_LEN) {
		fprintf(stderr, "key length is not in range (0,%d)\n", KEY_LEN);
		usage();
	}

	while ((n = fread(buf, 1, sizeof(buf), in)) > 0) {
		for (int i = 0; i < n; i++) {
			buf_pattern[i] = ptn + 1;
			ptn++;

			if (ptn > 250)
				ptn = 0;
		}

		k_off = xor_pattern(buf_pattern, n, key, k_len, k_off);
		xor_data(buf, n, buf_pattern);

		if (fwrite(buf, 1, n, out) != n) {
			perror("failed to write");
			ret = EXIT_FAILURE;
			goto out;
		}
	}

	if (ferror(in)) {
		perror("failed to read");
		ret = EXIT_FAILURE;
		goto out;
	}

out:
	fclose(in);
	fclose(out);
	return ret;
}