Deleted Added
full compact
kgzcmp.c (112089) kgzcmp.c (130927)
1/*
2 * Copyright (c) 1999 Global Technology Associates, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 9 unchanged lines hidden (view full) ---

18 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
19 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
20 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
21 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
23 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
24 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
1/*
2 * Copyright (c) 1999 Global Technology Associates, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 9 unchanged lines hidden (view full) ---

18 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
19 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
20 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
21 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
23 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
24 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 * $FreeBSD: head/usr.sbin/kgzip/kgzcmp.c 112089 2003-03-11 11:45:43Z ru $
26 * $FreeBSD: head/usr.sbin/kgzip/kgzcmp.c 130927 2004-06-22 17:05:39Z obrien $
27 */
28
27 */
28
29#define _KERNEL
30#include <sys/param.h>
31#undef _KERNEL
29#include <sys/types.h>
30#include <sys/stat.h>
31#include <sys/wait.h>
32
33#include <err.h>
34#include <fcntl.h>
35#include <stdio.h>
36#include <stdlib.h>
37#include <unistd.h>
38
32#include <sys/types.h>
33#include <sys/stat.h>
34#include <sys/wait.h>
35
36#include <err.h>
37#include <fcntl.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <unistd.h>
41
39#include "i386_a.out.h"
42#include <a.out.h>
40
41#include "aouthdr.h"
42#include "elfhdr.h"
43
44#include "aouthdr.h"
45#include "elfhdr.h"
43#include "endian.h"
44#include "kgzip.h"
45
46static void mk_data(const struct iodesc *i, const struct iodesc *,
47 struct kgz_hdr *, size_t);
48static int ld_elf(const struct iodesc *, const struct iodesc *,
49 struct kgz_hdr *, const Elf32_Ehdr *);
50static int ld_aout(const struct iodesc *, const struct iodesc *,
46#include "kgzip.h"
47
48static void mk_data(const struct iodesc *i, const struct iodesc *,
49 struct kgz_hdr *, size_t);
50static int ld_elf(const struct iodesc *, const struct iodesc *,
51 struct kgz_hdr *, const Elf32_Ehdr *);
52static int ld_aout(const struct iodesc *, const struct iodesc *,
51 struct kgz_hdr *, const struct i386_exec *);
53 struct kgz_hdr *, const struct exec *);
52
53/*
54 * Compress executable and output it in relocatable object format.
55 */
56void
57kgzcmp(struct kgz_hdr *kh, const char *f1, const char *f2)
58{
59 struct iodesc idi, ido;

--- 23 unchanged lines hidden (view full) ---

83 xzero(&ido, x);
84 }
85 xwrite(&ido, &ahdr1, sizeof(ahdr1));
86 ahdr0.a.a_data += kh->nsize + x;
87 xseek(&ido, 0);
88 xwrite(&ido, &ahdr0, sizeof(ahdr0));
89 } else {
90 struct kgz_elfhdr ehdr = elfhdr;
54
55/*
56 * Compress executable and output it in relocatable object format.
57 */
58void
59kgzcmp(struct kgz_hdr *kh, const char *f1, const char *f2)
60{
61 struct iodesc idi, ido;

--- 23 unchanged lines hidden (view full) ---

85 xzero(&ido, x);
86 }
87 xwrite(&ido, &ahdr1, sizeof(ahdr1));
88 ahdr0.a.a_data += kh->nsize + x;
89 xseek(&ido, 0);
90 xwrite(&ido, &ahdr0, sizeof(ahdr0));
91 } else {
92 struct kgz_elfhdr ehdr = elfhdr;
91 ehdr.st[KGZ_ST_KGZ_NDATA].st_size = HTOLE32(kh->nsize);
93 ehdr.st[KGZ_ST_KGZ_NDATA].st_size = htole32(kh->nsize);
92 ehdr.sh[KGZ_SH_DATA].sh_size =
94 ehdr.sh[KGZ_SH_DATA].sh_size =
93 HTOLE32(LE32TOH(ehdr.sh[KGZ_SH_DATA].sh_size) + kh->nsize);
95 htole32(le32toh(ehdr.sh[KGZ_SH_DATA].sh_size) + kh->nsize);
94 xseek(&ido, 0);
95 xwrite(&ido, &ehdr, sizeof(ehdr));
96 }
97 khle = *kh;
96 xseek(&ido, 0);
97 xwrite(&ido, &ehdr, sizeof(ehdr));
98 }
99 khle = *kh;
98 khle.dload = HTOLE32(khle.dload);
99 khle.dsize = HTOLE32(khle.dsize);
100 khle.isize = HTOLE32(khle.isize);
101 khle.entry = HTOLE32(khle.entry);
102 khle.nsize = HTOLE32(khle.nsize);
100 khle.dload = htole32(khle.dload);
101 khle.dsize = htole32(khle.dsize);
102 khle.isize = htole32(khle.isize);
103 khle.entry = htole32(khle.entry);
104 khle.nsize = htole32(khle.nsize);
103 xwrite(&ido, &khle, sizeof(khle));
104 xclose(&ido);
105 xclose(&idi);
106}
107
108/*
109 * Make encoded (compressed) data.
110 */
111static void
112mk_data(const struct iodesc * idi, const struct iodesc * ido,
113 struct kgz_hdr * kh, size_t off)
114{
115 union {
105 xwrite(&ido, &khle, sizeof(khle));
106 xclose(&ido);
107 xclose(&idi);
108}
109
110/*
111 * Make encoded (compressed) data.
112 */
113static void
114mk_data(const struct iodesc * idi, const struct iodesc * ido,
115 struct kgz_hdr * kh, size_t off)
116{
117 union {
116 struct i386_exec ex;
118 struct exec ex;
117 Elf32_Ehdr ee;
118 } hdr;
119 struct stat sb;
120 struct iodesc idp;
121 int fd[2];
122 pid_t pid;
123 size_t n;
124 int fmt, status, e;
125
126 n = xread(idi, &hdr, sizeof(hdr), 0);
127 fmt = 0;
128 if (n >= sizeof(hdr.ee) && IS_ELF(hdr.ee))
129 fmt = F_ELF;
119 Elf32_Ehdr ee;
120 } hdr;
121 struct stat sb;
122 struct iodesc idp;
123 int fd[2];
124 pid_t pid;
125 size_t n;
126 int fmt, status, e;
127
128 n = xread(idi, &hdr, sizeof(hdr), 0);
129 fmt = 0;
130 if (n >= sizeof(hdr.ee) && IS_ELF(hdr.ee))
131 fmt = F_ELF;
130 else if (n >= sizeof(hdr.ex) && I386_N_GETMAGIC(hdr.ex) == ZMAGIC)
132 else if (n >= sizeof(hdr.ex) && N_GETMAGIC(hdr.ex) == ZMAGIC)
131 fmt = F_AOUT;
132 if (!fmt)
133 errx(1, "%s: Format not supported", idi->fname);
134 xseek(ido, off);
135 if (pipe(fd))
136 err(1, NULL);
137 switch (pid = fork()) {
138 case -1:

--- 72 unchanged lines hidden (view full) ---

211 return 0;
212}
213
214/*
215 * "Load" an a.out-format executable.
216 */
217static int
218ld_aout(const struct iodesc * idi, const struct iodesc * ido,
133 fmt = F_AOUT;
134 if (!fmt)
135 errx(1, "%s: Format not supported", idi->fname);
136 xseek(ido, off);
137 if (pipe(fd))
138 err(1, NULL);
139 switch (pid = fork()) {
140 case -1:

--- 72 unchanged lines hidden (view full) ---

213 return 0;
214}
215
216/*
217 * "Load" an a.out-format executable.
218 */
219static int
220ld_aout(const struct iodesc * idi, const struct iodesc * ido,
219 struct kgz_hdr * kh, const struct i386_exec * a)
221 struct kgz_hdr * kh, const struct exec * a)
220{
221 size_t load, addr;
222
222{
223 size_t load, addr;
224
223 load = addr = I386_N_TXTADDR(*a);
224 xcopy(idi, ido, LE32TOH(a->a_text), I386_N_TXTOFF(*a));
225 addr += LE32TOH(a->a_text);
226 if (I386_N_DATADDR(*a) != addr)
225 load = addr = N_TXTADDR(*a);
226 xcopy(idi, ido, le32toh(a->a_text), N_TXTOFF(*a));
227 addr += le32toh(a->a_text);
228 if (N_DATADDR(*a) != addr)
227 return -1;
229 return -1;
228 xcopy(idi, ido, LE32TOH(a->a_data), I386_N_DATOFF(*a));
229 addr += LE32TOH(a->a_data);
230 xcopy(idi, ido, le32toh(a->a_data), N_DATOFF(*a));
231 addr += le32toh(a->a_data);
230 kh->dload = load;
231 kh->dsize = addr - load;
232 kh->dload = load;
233 kh->dsize = addr - load;
232 kh->isize = kh->dsize + LE32TOH(a->a_bss);
233 kh->entry = LE32TOH(a->a_entry);
234 kh->isize = kh->dsize + le32toh(a->a_bss);
235 kh->entry = le32toh(a->a_entry);
234 return 0;
235}
236 return 0;
237}