Browse Source

move encoding/decoding UTF-8/UTF-16 functions to new "static" class Utils.java

JNI-106
heck 5 years ago
parent
commit
555cb10070
  1. 122
      src/foundation/pEp/jniadapter/AbstractEngine.java
  2. 6
      src/foundation/pEp/jniadapter/Blob.java
  3. 28
      src/foundation/pEp/jniadapter/Engine.java.target
  4. 10
      src/foundation/pEp/jniadapter/Identity.java
  5. 218
      src/foundation/pEp/jniadapter/Message.java.target
  6. 115
      src/foundation/pEp/jniadapter/Utils.java
  7. 6
      src/foundation/pEp/jniadapter/_Blob.java
  8. 10
      src/foundation/pEp/jniadapter/_Identity.java
  9. 10
      src/gen_java_Engine.ysl2
  10. 28
      src/gen_java_Message.ysl2

122
src/foundation/pEp/jniadapter/AbstractEngine.java

@ -29,7 +29,7 @@ abstract class AbstractEngine extends UniquelyIdentifiable implements AbstractEn
}
final public void close() {
synchronized (AbstractEngine.class){
synchronized (AbstractEngine.class) {
release();
}
}
@ -75,110 +75,10 @@ abstract class AbstractEngine extends UniquelyIdentifiable implements AbstractEn
private native boolean _isSyncRunning();
public static byte[] toUTF8(String str) {
if (str == null)
return null;
try {
String _str = Normalizer.normalize(str, Normalizer.Form.NFC);
byte _buf[] = _str.getBytes("UTF-8");
byte _cpy[] = new byte[_buf.length];
System.arraycopy(_buf,0,_cpy,0,_buf.length);
return _cpy;
}
catch (UnsupportedEncodingException e) {
assert false;
return new byte[0];
}
}
public static Vector<byte[]> toUTF8(Vector<String> list) {
if (list == null)
return null;
Vector<byte[]> result = new Vector<byte[]>(list.size());
for (int i=0; i<list.size(); i++)
result.add(toUTF8(list.get(i)));
return result;
}
public static Pair<byte[], byte[]> toUTF8(Pair<String, String> pair) {
if (pair == null)
return null;
Pair<byte[], byte[]> result = new Pair<byte[], byte[]>();
result.first = toUTF8(pair.first);
result.second = toUTF8(pair.second);
return result;
}
public static ArrayList<Pair<byte[], byte[]>> toUTF8(ArrayList<Pair<String, String>> list) {
if (list == null)
return null;
ArrayList<Pair<byte[], byte[]>> result = new ArrayList<Pair<byte[], byte[]>>(list.size());
for (int i=0; i<list.size(); i++)
result.set(i, toUTF8(list.get(i)));
return result;
}
public static String toUTF16(byte[] utf8) {
if (utf8 == null)
return null;
try {
byte newUtf8[] = new byte[utf8.length];
System.arraycopy(utf8,0,newUtf8,0,utf8.length);
return new String(newUtf8, "UTF-8");
}
catch (UnsupportedEncodingException e) {
assert false;
return new String();
}
public boolean isSyncRunning() {
return _isSyncRunning();
}
public static Vector<String> toUTF16(Vector<byte[]> list) {
if (list == null)
return null;
Vector<String> result = new Vector<String>(list.size());
for (int i=0; i<list.size(); i++)
result.add(toUTF16(list.get(i)));
return result;
}
public static Pair<String, String> toUTF16(Pair<byte[], byte[]> pair) {
if (pair == null)
return null;
Pair<String, String> result = new Pair<String,String>();
result.first = toUTF16(pair.first);
result.second = toUTF16(pair.second);
return result;
}
public static ArrayList<Pair<String, String>> toUTF16(ArrayList<Pair<byte[], byte[]>> list) {
if (list == null)
return null;
ArrayList<Pair<String, String>> result = new ArrayList<Pair<String, String>>(list.size());
for (int i=0; i<list.size(); i++)
result.set(i, toUTF16(list.get(i)));
return result;
}
public void setMessageToSendCallback(Sync.MessageToSendCallback messageToSendCallback) {
this.messageToSendCallback = messageToSendCallback;
@ -204,7 +104,7 @@ abstract class AbstractEngine extends UniquelyIdentifiable implements AbstractEn
public int notifyHandshakeCallFromC(_Identity _myself, _Identity _partner, SyncHandshakeSignal _signal) {
Identity myself = new Identity(_myself);
Identity partner = new Identity(_partner);
System.out.println("pEpSync" +"notifyHandshakeCallFromC: " + notifyHandshakeCallback);
System.out.println("pEpSync" + "notifyHandshakeCallFromC: " + notifyHandshakeCallback);
if (notifyHandshakeCallback != null) {
notifyHandshakeCallback.notifyHandshake(myself, partner, _signal);
} else {
@ -213,8 +113,8 @@ abstract class AbstractEngine extends UniquelyIdentifiable implements AbstractEn
return 0;
}
public int messageToSendCallFromC (Message message) {
System.out.println("pEpSync" + "messageToSendCallFromC: " + messageToSendCallback );
public int messageToSendCallFromC(Message message) {
System.out.println("pEpSync" + "messageToSendCallFromC: " + messageToSendCallback);
if (messageToSendCallback != null) {
messageToSendCallback.messageToSend(message);
} else {
@ -232,11 +132,11 @@ abstract class AbstractEngine extends UniquelyIdentifiable implements AbstractEn
ArrayList<Pair<String, String>> opts = new ArrayList<>();
Pair<String, String> xpEp = new Pair<>();
xpEp.first = "X-pEp-Version";
xpEp.second = this.getProtocolVersion();;
xpEp.second = this.getProtocolVersion();
opts.add(xpEp);
msg.setOptFields(opts);
if(encFormat == Message.EncFormat.PEP) {
if (encFormat == Message.EncFormat.PEP) {
// For EncFormat.PEP
// The pgpText goes into the attachment index 1
msg.setShortmsg("p≡p");
@ -257,14 +157,12 @@ abstract class AbstractEngine extends UniquelyIdentifiable implements AbstractEn
attachments.add(att0);
attachments.add(att1);
msg.setAttachments(attachments);
}
else if (encFormat == Message.EncFormat.PEPEncInlineEA) {
} else if (encFormat == Message.EncFormat.PEPEncInlineEA) {
// For EncFormat.PEPEncInlineEA
// The pgpText goes into the longMessage
msg.setShortmsg("");
msg.setLongmsg(pgpText);
}
else {
} else {
throw new pEpCannotEncode("Message.Encformat not supported: " + encFormat.toString());
}

6
src/foundation/pEp/jniadapter/Blob.java

@ -22,15 +22,15 @@ public class Blob implements BlobInterface, Serializable {
String ret = "";
ret += "mime_type: \"" + mime_type + "\"\n";
ret += "filename: \"" + filename + "\"\n";
ret += "data plain: \"" + AbstractEngine.toUTF16(data) + "\"\n";
ret += "data plain: \"" + Utils.toUTF16(data) + "\"\n";
ret += "data decoded: \"" + _b.dataToXER() + "\"\n";
return ret;
}
Blob(_Blob b) {
data = b.data;
mime_type = AbstractEngine.toUTF16(b.mime_type);
filename = AbstractEngine.toUTF16(b.filename);
mime_type = Utils.toUTF16(b.mime_type);
filename = Utils.toUTF16(b.filename);
}
}

28
src/foundation/pEp/jniadapter/Engine.java.target

@ -1,28 +0,0 @@
package foundation.pEp.jniadapter;
import java.util.ArrayList;
import java.util.Vector;
final public class Engine extends AbstractEngine {
public Engine() throws pEpException { }
public native Message encrypt_message(
Message src,
ArrayList<String> extra,
Message.EncFormat enc_format
) throws pEpException;
public class DecryptResult {
public Message dst;
public Color color;
}
public native DecryptResult decrypt_message(
Message src,
ArrayList<String> keylist
) throws pEpException;
public native Color outgoing_message_color(
Message msg
) throws pEpException;
}

10
src/foundation/pEp/jniadapter/Identity.java

@ -23,12 +23,12 @@ public class Identity implements IdentityInterface, Serializable {
}
public Identity(_Identity i) {
address = AbstractEngine.toUTF16(i.address);
fpr = AbstractEngine.toUTF16(i.fpr);
user_id = AbstractEngine.toUTF16(i.user_id);
username = AbstractEngine.toUTF16(i.username);
address = Utils.toUTF16(i.address);
fpr = Utils.toUTF16(i.fpr);
user_id = Utils.toUTF16(i.user_id);
username = Utils.toUTF16(i.username);
comm_type = CommType.Management.tag.get(i.comm_type);
lang = AbstractEngine.toUTF16(i.lang);
lang = Utils.toUTF16(i.lang);
me = i.me;
flags = i.flags;
}

218
src/foundation/pEp/jniadapter/Message.java.target

@ -1,218 +0,0 @@
package foundation.pEp.jniadapter;
import java.util.ArrayList;
import java.util.Vector;
import java.util.Date;
import java.util.HashMap;
public class Message implements AutoCloseable {
private final long handle;
native long init();
native void release(long handle);
public Message() {
handle = init();
}
public final void close() {
release(handle);
}
public enum TextFormat {
Plain (0),
Html (1),
Other (255)
;
static class Management {
public static final HashMap<Integer, TextFormat> tag =
new HashMap<Integer, TextFormat>();
}
public final int value;
TextFormat(int value) {
this.value = value;
fill(value);
}
private void fill(int value) {
Management.tag.put(value, this);
}
}
public enum Direction {
Incoming (0),
Outgoing (1)
;
static class Management {
public static final HashMap<Integer, Direction> tag =
new HashMap<Integer, Direction>();
}
public final int value;
Direction(int value) {
this.value = value;
fill(value);
}
private void fill(int value) {
Management.tag.put(value, this);
}
}
public enum EncFormat {
None (0),
Pieces (1),
SMIME (2),
PGPMIME (3),
PEP (4)
;
static class Management {
public static final HashMap<Integer, EncFormat> tag =
new HashMap<Integer, EncFormat>();
}
public final int value;
EncFormat(int value) {
this.value = value;
fill(value);
}
private void fill(int value) {
Management.tag.put(value, this);
}
}
private native int _getDir();
private native void _setDir(int value);
public Direction getDir() {
return Direction.tag.get(_getDir());
}
public void setDir(Direction value) {
_setDir(value.value);
}
private native byte[] _getId();
private native void _setId(byte[] value);
public String getId() {
return AbstractEngine.toUTF16(_getId());
}
public void setId(String value) {
_setId(AbstractEngine.toUTF8(value));
}
private native byte[] _getShortmsg();
private native void _setShortmsg(byte[] value);
public String getShortmsg() {
return AbstractEngine.toUTF16(_getShortmsg());
}
public void setShortmsg(String value) {
_setShortmsg(AbstractEngine.toUTF8(value));
}
private native byte[] _getLongmsg();
private native void _setLongmsg(byte[] value);
public String getLongmsg() {
return AbstractEngine.toUTF16(_getLongmsg());
}
public void setLongmsg(String value) {
_setLongmsg(AbstractEngine.toUTF8(value));
}
private native byte[] _getLongmsgFormatted();
private native void _setLongmsgFormatted(byte[] value);
public String getLongmsgFormatted() {
return AbstractEngine.toUTF16(_getLongmsgFormatted());
}
public void setLongmsgFormatted(String value) {
_setLongmsgFormatted(AbstractEngine.toUTF8(value));
}
public native ArrayList<Blob> getAttachments();
public native void setAttachments(ArrayList<Blob> value);
public native Date getSent();
public native void setSent(Date value);
public native Date getRecv();
public native void setRecv(Date value);
private native AbstractEngine._Identity _getFrom();
private native void _setFrom(AbstractEngine._Identity value);
public Identity getFrom() {
return new _getFrom().getIdentity();
}
public void setFrom(Identity value) {
_setFrom(new AbstractEngine._Identity(value));
}
public native ArrayList<Identity> getTo();
public native void setTo(ArrayList<Identity> value);
public native Identity getRecvBy();
public native void setRecvBy(Identity value);
public native ArrayList<Identity> getCc();
public native void setCc(ArrayList<Identity> value);
public native ArrayList<Identity> getBcc();
public native void setBcc(ArrayList<Identity> value);
public native ArrayList<Identity> getReplyTo();
public native void setReplyTo(ArrayList<Identity> value);
private native ArrayList<byte[]> _getInReplyTo();
private native void _setInReplyTo(ArrayList<byte[]> value);
public ArrayList<String> getInReplyTo() {
return AbstractEngine.toUTF16(_getInReplyTo());
}
public void setInReplyTo(ArrayList<String> value) {
_setInReplyTo(AbstractEngine.toUTF8(value));
}
private native ArrayList<byte[]> _getReferences();
private native void _setReferences(ArrayList<byte[]> value);
public ArrayList<String> getReferences() {
return AbstractEngine.toUTF16(_getReferences());
}
public void setReferences(ArrayList<String> value) {
_setReferences(AbstractEngine.toUTF8(value));
}
private native ArrayList<byte[]> _getKeywords();
private native void _setKeywords(ArrayList<byte[]> value);
public ArrayList<String> getKeywords() {
return AbstractEngine.toUTF16(_getKeywords());
}
public void setKeywords(ArrayList<String> value) {
_setKeywords(AbstractEngine.toUTF8(value));
}
private native byte[] _getComments();
private native void _setComments(byte[] value);
public String getComments() {
return AbstractEngine.toUTF16(_getComments());
}
public void setComments(String value) {
_setComments(AbstractEngine.toUTF8(value));
}
private native Vector<Pair<byte[], byte[]>> _getOptFields();
private native void _setOptFields(Vector<Pair<byte[], byte[]>> value);
public Vector<Pair<String, String>> getOptFields() {
return AbstractEngine.toUTF16(_getOptFields());
}
public void setOptFields(Vector<Pair<String, String>> value) {
_setOptFields(AbstractEngine.toUTF8(value));
}
public native Message.EncFormat getEncFormat();
public native void setEncFormat(Message.EncFormat value);
}

115
src/foundation/pEp/jniadapter/Utils.java

@ -0,0 +1,115 @@
package foundation.pEp.jniadapter;
import java.io.UnsupportedEncodingException;
import java.text.Normalizer;
import java.util.ArrayList;
import java.util.Vector;
public class Utils {
private Utils() { }
public static byte[] toUTF8(String str) {
if (str == null)
return null;
try {
String _str = Normalizer.normalize(str, Normalizer.Form.NFC);
byte _buf[] = _str.getBytes("UTF-8");
byte _cpy[] = new byte[_buf.length];
System.arraycopy(_buf,0,_cpy,0,_buf.length);
return _cpy;
}
catch (UnsupportedEncodingException e) {
assert false;
return new byte[0];
}
}
public static Vector<byte[]> toUTF8(Vector<String> list) {
if (list == null)
return null;
Vector<byte[]> result = new Vector<byte[]>(list.size());
for (int i=0; i<list.size(); i++)
result.add(toUTF8(list.get(i)));
return result;
}
public static Pair<byte[], byte[]> toUTF8(Pair<String, String> pair) {
if (pair == null)
return null;
Pair<byte[], byte[]> result = new Pair<byte[], byte[]>();
result.first = toUTF8(pair.first);
result.second = toUTF8(pair.second);
return result;
}
public static ArrayList<Pair<byte[], byte[]>> toUTF8(ArrayList<Pair<String, String>> list) {
if (list == null)
return null;
ArrayList<Pair<byte[], byte[]>> result = new ArrayList<Pair<byte[], byte[]>>(list.size());
for (int i=0; i<list.size(); i++)
result.set(i, toUTF8(list.get(i)));
return result;
}
public static String toUTF16(byte[] utf8) {
if (utf8 == null)
return null;
try {
byte newUtf8[] = new byte[utf8.length];
System.arraycopy(utf8,0,newUtf8,0,utf8.length);
return new String(newUtf8, "UTF-8");
}
catch (UnsupportedEncodingException e) {
assert false;
return new String();
}
}
public static Vector<String> toUTF16(Vector<byte[]> list) {
if (list == null)
return null;
Vector<String> result = new Vector<String>(list.size());
for (int i=0; i<list.size(); i++)
result.add(toUTF16(list.get(i)));
return result;
}
public static Pair<String, String> toUTF16(Pair<byte[], byte[]> pair) {
if (pair == null)
return null;
Pair<String, String> result = new Pair<String,String>();
result.first = toUTF16(pair.first);
result.second = toUTF16(pair.second);
return result;
}
public static ArrayList<Pair<String, String>> toUTF16(ArrayList<Pair<byte[], byte[]>> list) {
if (list == null)
return null;
ArrayList<Pair<String, String>> result = new ArrayList<Pair<String, String>>(list.size());
for (int i=0; i<list.size(); i++)
result.set(i, toUTF16(list.get(i)));
return result;
}
}

6
src/foundation/pEp/jniadapter/_Blob.java

@ -9,13 +9,13 @@ public class _Blob {
private native byte[] _dataToXER() throws pEpException;;
public String dataToXER() {
return AbstractEngine.toUTF16(_dataToXER());
return Utils.toUTF16(_dataToXER());
}
_Blob(Blob b) {
data = b.data;
mime_type = AbstractEngine.toUTF8(b.mime_type);
filename = AbstractEngine.toUTF8(b.filename);
mime_type = Utils.toUTF8(b.mime_type);
filename = Utils.toUTF8(b.filename);
}
}

10
src/foundation/pEp/jniadapter/_Identity.java

@ -19,12 +19,12 @@ public class _Identity {
}
public _Identity(Identity i) {
address = AbstractEngine.toUTF8(i.address);
fpr = AbstractEngine.toUTF8(i.fpr);
user_id = AbstractEngine.toUTF8(i.user_id);
username = AbstractEngine.toUTF8(i.username);
address = Utils.toUTF8(i.address);
fpr = Utils.toUTF8(i.fpr);
user_id = Utils.toUTF8(i.user_id);
username = Utils.toUTF8(i.username);
comm_type = i.comm_type.value;
lang = AbstractEngine.toUTF8(i.lang);
lang = Utils.toUTF8(i.lang);
me = i.me;
flags = i.flags;
}

10
src/gen_java_Engine.ysl2

@ -50,7 +50,7 @@ tstylesheet {
when "@type = 'bool'"
|> return new Boolean(_«@name»(`apply "parm/*", mode=basic_parm_name`));
otherwise
|> return AbstractEngine.toUTF16(_«@name»(`apply "parm/*", mode=basic_parm_name`));
|> return Utils.toUTF16(_«@name»(`apply "parm/*", mode=basic_parm_name`));
}
||
}
@ -191,14 +191,14 @@ tstylesheet {
// TODO factorize type conversion with multireturn and gen_java_message.java
when "count(parm[returns][stringlist|string|sstring]) > 0"
|> return AbstractEngine.toUTF16(_«@name»(`apply "parm[in]", mode=call;`));
|> return Utils.toUTF16(_«@name»(`apply "parm[in]", mode=call;`));
when "count(parm[returns]/stringpairlist) > 0"
||
ArrayList<Pair<byte[], byte[]>> glist = _«@name»(`apply "parm[in]", mode=call;`);
if(glist != null){
ArrayList<Pair<String, String>> list = new ArrayList<Pair<String, String>>();
for (Pair<byte[],byte[]> i : glist)
list.add(new Pair<String, String>(AbstractEngine.toUTF16(i.first), AbstractEngine.toUTF16(i.second)));
list.add(new Pair<String, String>(Utils.toUTF16(i.first), Utils.toUTF16(i.second)));
return list;
}
return null;
@ -287,7 +287,7 @@ tstylesheet {
if "$dir = 'in' or $dir = 'inout'" choose {
when "$type = 'string' or $type = 'stringlist'"
| ¡$type _«$name» = AbstractEngine.toUTF8(«$name»);
| ¡$type _«$name» = Utils.toUTF8(«$name»);
when "$type = 'identity'"
||
_Identity _«$name» = null;
@ -311,7 +311,7 @@ tstylesheet {
if "$dir != 'in'" choose {
when "$type = 'string' or $type = 'stringlist'"
| result.«$name» = AbstractEngine.toUTF16(_result.«$name»);
| result.«$name» = Utils.toUTF16(_result.«$name»);
when "$type = 'identity'"
| result.«$name» = Identity(«$name»);
otherwise

28
src/gen_java_Message.ysl2

@ -34,14 +34,14 @@ tstylesheet {
) throws pEpException;
public «$cname»(String mime_text) {
byte[] _mime_text = AbstractEngine.toUTF8(mime_text);
byte[] _mime_text = Utils.toUTF8(mime_text);
handle = _«$cname»(_mime_text);
}
public native byte[] _encodeMIME() throws pEpException;
public String encodeMIME() {
return AbstractEngine.toUTF16(_encodeMIME());
return Utils.toUTF16(_encodeMIME());
}
private «$cname»(long h) {
@ -157,13 +157,13 @@ tstylesheet {
otherwise > _«$ename»
}
const "convget" choose {
when "$ctype = 'stringlist'" > AbstractEngine.toUTF16(i)
when "$ctype = 'stringpairlist'" > new Pair<String, String>(AbstractEngine.toUTF16(i.first), AbstractEngine.toUTF16(i.second))
when "$ctype = 'stringlist'" > Utils.toUTF16(i)
when "$ctype = 'stringpairlist'" > new Pair<String, String>(Utils.toUTF16(i.first), Utils.toUTF16(i.second))
otherwise > new «$ename»(i)
}
const "convset" choose {
when "$ctype = 'stringlist'" > AbstractEngine.toUTF8(i)
when "$ctype = 'stringpairlist'" > new Pair<byte[],byte[]>(AbstractEngine.toUTF8(i.first), AbstractEngine.toUTF8(i.second))
when "$ctype = 'stringlist'" > Utils.toUTF8(i)
when "$ctype = 'stringpairlist'" > new Pair<byte[],byte[]>(Utils.toUTF8(i.first), Utils.toUTF8(i.second))
otherwise > new _«$ename»(i)
}
||
@ -200,13 +200,13 @@ tstylesheet {
public «$type» get«$name»() {
«$itype» res = _get«$name»();
if(res != null)
return AbstractEngine.toUTF16(res);
return Utils.toUTF16(res);
else
return null;
}
public void set«$name»(«$type» value) {
if(value != null)
_set«$name»(AbstractEngine.toUTF8(value));
_set«$name»(Utils.toUTF8(value));
else
_set«$name»(new byte[0]);
}
@ -220,13 +220,13 @@ tstylesheet {
public «$type» get«$name»() {
«$itype» res = _get«$name»();
if(res != null)
return AbstractEngine.toUTF16(res);
return Utils.toUTF16(res);
else
return null;
}
public void set«$name»(«$type» value) {
if(value != null)
_set«$name»(AbstractEngine.toUTF8(value));
_set«$name»(Utils.toUTF8(value));
else
_set«$name»(null);
}
@ -280,13 +280,13 @@ tstylesheet {
otherwise > _«$ename»
}
const "convget" choose {
when "$ctype = 'stringlist'" > AbstractEngine.toUTF16(i)
when "$ctype = 'stringpairlist'" > new Pair<String, String>(AbstractEngine.toUTF16(i.first), AbstractEngine.toUTF16(i.second))
when "$ctype = 'stringlist'" > Utils.toUTF16(i)
when "$ctype = 'stringpairlist'" > new Pair<String, String>(Utils.toUTF16(i.first), Utils.toUTF16(i.second))
otherwise > new «$ename»(i)
}
const "convset" choose {
when "$ctype = 'stringlist'" > AbstractEngine.toUTF8(i)
when "$ctype = 'stringpairlist'" > new Pair<byte[],byte[]>(AbstractEngine.toUTF8(i.first), AbstractEngine.toUTF8(i.second))
when "$ctype = 'stringlist'" > Utils.toUTF8(i)
when "$ctype = 'stringpairlist'" > new Pair<byte[],byte[]>(Utils.toUTF8(i.first), Utils.toUTF8(i.second))
otherwise > new _«$ename»(i)
}
||

Loading…
Cancel
Save