Skip to content

Commit

Permalink
Fixed reading descriptor with empty value
Browse files Browse the repository at this point in the history
  • Loading branch information
woesss committed Dec 29, 2023
1 parent bd3e07c commit 1b5cfae
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 87 deletions.
22 changes: 6 additions & 16 deletions app/src/main/java/ru/playsoftware/j2meloader/util/IOUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright 2020 Nikita Shakarun
* Copyright 2023 Yury Kharchenko
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,30 +17,19 @@

package ru.playsoftware.j2meloader.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class IOUtils {

private static final int BUFFER_SIZE = 16384;
import kotlin.io.ByteStreamsKt;
import kotlin.io.ConstantsKt;

public class IOUtils {
public static byte[] toByteArray(InputStream stream) throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buf = new byte[BUFFER_SIZE];
int len;
while ((len = stream.read(buf)) != -1) {
outputStream.write(buf, 0, len);
}
return outputStream.toByteArray();
return ByteStreamsKt.readBytes(stream);
}

public static void copy(InputStream input, OutputStream output) throws IOException {
byte[] buf = new byte[BUFFER_SIZE];
int len;
while ((len = input.read(buf)) != -1) {
output.write(buf, 0, len);
}
ByteStreamsKt.copyTo(input, output, ConstantsKt.DEFAULT_BUFFER_SIZE);
}
}
13 changes: 4 additions & 9 deletions app/src/main/java/ru/woesss/j2me/installer/AppInstaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@

import com.android.dx.command.dexer.Main;

import ru.woesss.util.zip.ZipFile;
import net.lingala.zip4j.io.inputstream.ZipInputStream;
import net.lingala.zip4j.model.FileHeader;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
Expand All @@ -48,8 +46,10 @@
import ru.playsoftware.j2meloader.config.Config;
import ru.playsoftware.j2meloader.util.ConverterException;
import ru.playsoftware.j2meloader.util.FileUtils;
import ru.playsoftware.j2meloader.util.IOUtils;
import ru.playsoftware.j2meloader.util.ZipUtils;
import ru.woesss.j2me.jar.Descriptor;
import ru.woesss.util.zip.ZipFile;

public class AppInstaller {
private static final String TAG = AppInstaller.class.getSimpleName();
Expand Down Expand Up @@ -340,13 +340,8 @@ private Descriptor loadManifest(File jar) throws IOException {
throw new IOException("JAR not have " + JarFile.MANIFEST_NAME);
}
try (ZipInputStream is = zip.getInputStream(manifest)) {
ByteArrayOutputStream baos = new ByteArrayOutputStream(20480);
byte[] buf = new byte[4096];
int read;
while ((read = is.read(buf)) != -1) {
baos.write(buf, 0, read);
}
return new Descriptor(baos.toString(), false);
String text = new String(IOUtils.toByteArray(is));
return new Descriptor(text, false);
}
}
}
Expand Down
90 changes: 28 additions & 62 deletions app/src/main/java/ru/woesss/j2me/jar/Descriptor.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 Yury Kharchenko
* Copyright 2020-2023 Yury Kharchenko
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,27 +17,22 @@
package ru.woesss.j2me.jar;

import android.content.Context;
import android.net.Uri;
import android.text.SpannableStringBuilder;
import android.util.Log;

import java.io.EOFException;
import androidx.annotation.Nullable;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

import androidx.annotation.Nullable;
import ru.playsoftware.j2meloader.R;
import ru.playsoftware.j2meloader.util.FileUtils;

public class Descriptor {
private static final String TAG = Descriptor.class.getName();

private static final char UNICODE_BOM = '\uFEFF';
private static final String MANIFEST_VERSION = "Manifest-Version";
// required in JAD and Manifest
Expand Down Expand Up @@ -74,7 +69,11 @@ public class Descriptor {

public Descriptor(String source, boolean isJad) throws IOException {
this.isJad = isJad;
init(source);
try {
parse(source);
} catch (Exception e) {
throw new DescriptorException("Bad descriptor: \n" + source, e);
}
if (isJad) {
verifyJadAttrs();
}
Expand All @@ -83,26 +82,7 @@ public Descriptor(String source, boolean isJad) throws IOException {
}

public Descriptor(File file, boolean isJad) throws IOException {
this.isJad = isJad;
byte[] buf;
try (InputStream inputStream = new FileInputStream(file)) {
int count = inputStream.available();
buf = new byte[count];
int n = 0;
while (n < buf.length) {
int read = inputStream.read(buf, n, buf.length - n);
if (read < 0) {
throw new EOFException();
}
n += read;
}
}
init(new String(buf));
if (isJad) {
verifyJadAttrs();
}
verify();

this(FileUtils.getText(file.getPath()), isJad);
}

public int compareVersion(String version) {
Expand Down Expand Up @@ -170,55 +150,41 @@ public final Map<String, String> getAttrs() {
return attributes;
}

private void init(String source) throws DescriptorException {
try {
parse(source);
} catch (Exception e) {
Log.e(TAG, source);
throw new DescriptorException("Bad descriptor", e);
}
}

private void parse(String source) {
String[] lines = source.split("[\\n\\r]+");
int length = lines.length;
if (length == 0) {
throw new IllegalArgumentException("Descriptor source is empty");
}
String line0 = lines[0];
if (line0.charAt(0) == UNICODE_BOM)
if (line0.length() > 0 && line0.charAt(0) == UNICODE_BOM)
lines[0] = line0.substring(1);
Map<String, String> attrs = attributes;
final StringBuilder sb = new StringBuilder("1.0");
String key = MANIFEST_VERSION;
for (String line : lines) {
if (line.trim().isEmpty()) {
continue;
}
int colon = line.indexOf(':');
if (colon == -1) {
if (line.charAt(0) == ' ') sb.append(line, 1, line.length());
else sb.append(line);
if (line.isEmpty()) {
continue;
}
if (line.charAt(0) == ' ') {
sb.append(line, 1, line.length());
} else {
sb.append(line);
}
} else {
attrs.put(key, sb.toString().trim());
sb.setLength(0);
key = line.substring(0, colon++).trim();
if (line.charAt(colon) == ' ')
colon++;
sb.append(line, colon, line.length());
key = line.substring(0, colon).trim();
if (line.length() > ++colon) {
sb.append(line, colon, line.length());
}
}
}
attrs.put(key, sb.toString().trim());
}

private String getFileLocation(String jarURL) {
Uri jarUri = Uri.parse(jarURL);
if ("http".equalsIgnoreCase(jarUri.getScheme()) || "https".equalsIgnoreCase(jarUri.getScheme())) {
return "Интернет";
}
return "Файл";
}

public String getName() {
return attributes.get(MIDLET_NAME);
}
Expand Down Expand Up @@ -279,12 +245,12 @@ public String getJarUrl() {

@Override
public boolean equals(@Nullable Object obj) {
if (this == obj)
if (this == obj) {
return true;
if (!(obj instanceof Descriptor))
return false;
Descriptor o = (Descriptor) obj;
return getName().equals(o.getName()) && getVendor().equals(o.getVendor());
} else if (obj instanceof Descriptor o) {
return getName().equals(o.getName()) && getVendor().equals(o.getVendor());
}
return false;
}

public SpannableStringBuilder getInfo(Context c) {
Expand Down

0 comments on commit 1b5cfae

Please sign in to comment.