Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cb70bdbe5a7abf8eef204f6329ef241aac24957e (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2021 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.osgi.internal.signedcontent;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;
import org.eclipse.osgi.storage.bundlefile.BundleEntry;
import org.eclipse.osgi.storage.bundlefile.BundleFile;

/*
 * Converts a BundleFile into an input stream that is appropriate for
 * creating a JarInputStream.
 */
public final class BundleToJarInputStream extends InputStream {
	private final BundleFile bundlefile;
	private final Iterator<String> entryPaths;
	private final JarOutputStream jarOutput;
	private final ByteArrayOutputStream nextEntryOutput = new ByteArrayOutputStream();

	private ByteArrayInputStream nextEntryInput = null;

	public BundleToJarInputStream(BundleFile bundleFile) throws IOException {
		this.bundlefile = bundleFile;
		List<String> entries = new ArrayList<>();
		int signatureFileCnt = 0;
		for (Enumeration<String> ePaths = bundleFile.getEntryPaths("", true); ePaths.hasMoreElements();) { //$NON-NLS-1$
			String entry = ePaths.nextElement();
			if (entry.equals(JarFile.MANIFEST_NAME)) {
				// this is always read into the stream first and entries follow
				entries.add(0, entry);
				signatureFileCnt++;

			} else if (isSignatureFile(entry)) {
				// Add signature files directly after manifest.
				entries.add(signatureFileCnt++, entry);
			} else {
				// everything else is just at the end in order of the enumeration
				entries.add(entry);
			}
		}

		entryPaths = entries.iterator();

		jarOutput = new JarOutputStream(nextEntryOutput);
	}

	private boolean isSignatureFile(String entry) {
		entry = entry.toUpperCase();
		if (entry.startsWith("META-INF/") && entry.indexOf('/', "META-INF/".length()) == -1) { //$NON-NLS-1$ //$NON-NLS-2$
			return entry.endsWith(".SF") //$NON-NLS-1$
					|| entry.endsWith(".DSA") //$NON-NLS-1$
					|| entry.endsWith(".RSA") //$NON-NLS-1$
					|| entry.endsWith(".EC"); //$NON-NLS-1$
		}
		return false;
	}

	public int read() throws IOException {
		if (nextEntryInput != null) {
			int result = nextEntryInput.read();
			if (result != -1) {
				return result;
			}

			// this entry is done force a new one to be read if there is a next
			nextEntryInput = null;
			return read();

		}

		if (entryPaths.hasNext()) {
			readNext(entryPaths.next());

			if (!entryPaths.hasNext()) {
				jarOutput.close();
			}
		} else {
			jarOutput.close();
			return -1;
		}

		return read();
	}

	private void readNext(String path) throws IOException {
		BundleEntry found = bundlefile.getEntry(path);
		if (found == null) {
			throw new IOException("No entry found: " + path); //$NON-NLS-1$
		}

		nextEntryOutput.reset();
		JarEntry entry = new JarEntry(path);
		jarOutput.putNextEntry(entry);
		if (!entry.isDirectory()) {
			try (InputStream source = found.getInputStream()) {
				byte[] buf = new byte[8192];
				int length;
				while ((length = source.read(buf)) > 0) {
					jarOutput.write(buf, 0, length);
				}
			}
		}


		jarOutput.closeEntry();
		jarOutput.flush();

		// now save off the entry we just wrote
		nextEntryInput = new ByteArrayInputStream(nextEntryOutput.toByteArray());
	}

}

Back to the top