summaryrefslogtreecommitdiff
path: root/payloads/bayou/util/pbuilder/create.c
blob: c866079ebfed4bda95b5f6710b96c82df62aa627 (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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
 * This file is part of the bayou project.
 *
 * Copyright (C) 2008 Advanced Micro Devices, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * 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, write to the Free Software
 * Foundation, Inc.
 */

#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <libgen.h>
#include "liblar.h"
#include "pbuilder.h"

void do_lzma_compress(char *in, int in_len, char *out, int *out_len);

int add_bpt_to_lar(struct LAR *lar, struct config *config)
{
	char *buffer;
	int ret, i, len = sizeof(struct bpt_config);
	struct bpt_config *cfg;
	struct LARAttr attr;
	char *ptr;

	for (i = 0; i < config->n_entries; i++) {
		len += sizeof(struct bpt_pentry);

		if (config->entries[i]->type != BPT_TYPE_CHAIN)
			len += ((strlen(config->entries[i]->larname)
				+ 15) & ~0x0F);
	}

	buffer = calloc(len, 1);

	if (buffer == NULL)
		return -1;

	cfg = (struct bpt_config *)buffer;

	cfg->id = BPT_ID;
	cfg->timeout = config->timeout;
	cfg->entries = config->n_entries;

	ptr = buffer + sizeof(struct bpt_config);

	for (i = 0; i < config->n_entries; i++) {
		int nlen = 0;
		struct bpt_pentry *pentry = (struct bpt_pentry *)ptr;

		pentry->index = config->entries[i]->index;
		pentry->parent = config->entries[i]->parent;
		pentry->type = config->entries[i]->type;
		pentry->flags = config->entries[i]->flags;

		strncpy((char *)pentry->title,
			(char *)config->entries[i]->title,
			sizeof(pentry->title));

		if (config->entries[i]->type != BPT_TYPE_CHAIN) {
			nlen = strlen(config->entries[i]->larname);
			nlen = (nlen + 15) & ~0x0F;

			strcpy((char *)(ptr + sizeof(struct bpt_pentry)),
			       config->entries[i]->larname);

			pentry->nlen = nlen;
		}

		ptr += sizeof(struct bpt_pentry);

		if (config->entries[i]->type != BPT_TYPE_CHAIN)
			ptr += nlen;
	}

	LAR_SetAttrs(&attr, "bayou_payload_table", ALGO_NONE);

	ret = LAR_AppendBuffer(lar, (unsigned char *)buffer, len, &attr);
	free(buffer);
	return ret;
}

struct lfile {
	char *file;
	char *larname;
};

int n_lfiles;

int create_lar_from_config(const char *input, const char *output)
{
	struct config config;
	FILE *stream;
	struct LAR *lar;
	struct LARAttr attr;
	int i, j, ret = -1;
	struct lfile *lfiles;

	stream = fopen(input, "r");

	if (stream == NULL) {
		warn("E:  Couldn't open %s for reading\n", input);
		return -1;
	}

	memset(&config, 0, sizeof(config));

	parseconfig(stream, &config);
	fclose(stream);

	lar = LAR_Create(output);

	if (lar == NULL) {
		warn("E:  Couldn't create a new lar file\n");
		return -1;
	}

	LAR_SetCompressionFuncs(lar, ALGO_LZMA, do_lzma_compress, NULL);

	lfiles = calloc(sizeof(struct lfile), config.n_entries);

	if (lfiles == NULL) {
		warn("E: Couldn't allocate memory: %m\n");
		return -1;
	}

	for (i = 0; i < config.n_entries; i++) {
		/* Master chain entries don't have files associated with them. */
		if (config.entries[i]->type == BPT_TYPE_CHAIN)
			continue;

		if (access(config.entries[i]->file, R_OK)) {
			warn("E:  Could not find file %s\n",
			     config.entries[i]->file);

			goto err;
		}

		if (config.entries[i]->larname == NULL) {
			config.entries[i]->larname =
			    strdup(basename(config.entries[i]->file));

			if (config.entries[i]->larname == NULL) {
				warn("E: Could not allocate memory for the default name\n");
				goto err;
			}
		}

		/*
		 * Add the file to the list of files to add to the LAR - skip
		 * any duplicates, but be on the lookout for the same LAR name
		 * attached to a different file.
		 */
		for (j = 0; j < n_lfiles; j++) {
			if (!strcmp(lfiles[j].larname,
				    config.entries[i]->larname)) {
				if (strcmp(lfiles[j].file,
					   config.entries[i]->file)) {
					warn("E:  LAR name '%s' has already been used\n", config.entries[i]->larname);
					goto err;
				}
				break;
			}
		}

		if (j == n_lfiles) {
			lfiles[n_lfiles].file = config.entries[i]->file;
			lfiles[n_lfiles++].larname = config.entries[i]->larname;
		}
	}

	/* Add all the files to the LAR. */
	for (i = 0; i < n_lfiles; i++) {
		LAR_SetAttrs(&attr, lfiles[i].larname, ALGO_LZMA);

		if (LAR_AppendFile(lar, lfiles[i].file, &attr)) {
			warn("E: Could not add %s to the LAR\n",
			     lfiles[i].file);
			goto err;
		}
	}

	ret = add_bpt_to_lar(lar, &config);

err:
	LAR_Close(lar);
	return ret;
}