Skip to content

Commit

Permalink
Swig api refactor to use byte_vector
Browse files Browse the repository at this point in the history
  • Loading branch information
aldenml committed Feb 4, 2016
1 parent 3f49c32 commit afb3b48
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 68 deletions.
13 changes: 8 additions & 5 deletions src/main/java/com/frostwire/jlibtorrent/DHT.java
Expand Up @@ -154,7 +154,7 @@ private void toggleDHT(boolean on) {
* @return
*/
public static Sha1Hash itemTargetId(Entry e) {
return new Sha1Hash(dht_item.item_target_id(e.getSwig().bencode()));
return null;//new Sha1Hash(dht_item.item_target_id(e.getSwig().bencode()));
}

/**
Expand Down Expand Up @@ -185,6 +185,7 @@ public static Sha1Hash itemTargetId(String salt, byte[] pk) {
* @param sig
*/
public static void signMutableItem(Entry e, String salt, int seq, byte[] pk, byte[] sk, byte[] sig) {
/*
if (sig == null || sig.length != Ed25519.SIGNATURE_SIZE) {
throw new IllegalArgumentException("The signature array must be a valid one with length " + Ed25519.SIGNATURE_SIZE);
}
Expand All @@ -195,16 +196,17 @@ public static void signMutableItem(Entry e, String salt, int seq, byte[] pk, byt
Vectors.bytes2char_vector(pk), Vectors.bytes2char_vector(sk), sig_v);
Vectors.char_vector2bytes(sig_v, sig);
*/
}

public static boolean verifyMutableItem(Entry e, String salt, int seq, byte[] pk, byte[] sig) {
return dht_item.verify_mutable_item(e.getSwig().bencode(), salt, seq,
return false;/*dht_item.verify_mutable_item(e.getSwig().bencode(), salt, seq,
Vectors.bytes2char_vector(pk),
Vectors.bytes2char_vector(sig));
Vectors.bytes2char_vector(sig));*/
}

public static int canonicalString(Entry e, int seq, String salt, byte[] out) {
if (out == null || out.length != 1200) {
/* if (out == null || out.length != 1200) {
throw new IllegalArgumentException("The out array must be a valid one with length 1200");
}
Expand All @@ -214,6 +216,7 @@ public static int canonicalString(Entry e, int seq, String salt, byte[] out) {
Vectors.char_vector2bytes(out_v, out);
return r;
return r;*/
return 0;
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/frostwire/jlibtorrent/Entry.java
Expand Up @@ -38,7 +38,7 @@ public entry getSwig() {
}

public byte[] bencode() {
return Vectors.char_vector2bytes(e.bencode());
return Vectors.byte_vector2bytes(e.bencode());
}

public String string() {
Expand Down Expand Up @@ -84,7 +84,7 @@ public String toString() {
}

public static Entry bdecode(byte[] data) {
return new Entry(entry.bdecode(Vectors.bytes2char_vector(data)));
return new Entry(entry.bdecode(Vectors.bytes2byte_vector(data)));
}

public static Entry bdecode(File file) throws IOException {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/frostwire/jlibtorrent/Session.java
Expand Up @@ -403,7 +403,7 @@ public boolean isListening() {
public byte[] saveState() {
entry e = new entry();
s.save_state(e);
return Vectors.char_vector2bytes(e.bencode());
return Vectors.byte_vector2bytes(e.bencode());
}

/**
Expand All @@ -422,7 +422,7 @@ public byte[] saveState() {
* @param data
*/
public void loadState(byte[] data) {
char_vector buffer = Vectors.bytes2char_vector(data);
byte_vector buffer = Vectors.bytes2byte_vector(data);
bdecode_node n = new bdecode_node();
error_code ec = new error_code();
int ret = bdecode_node.bdecode(buffer, n, ec);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/frostwire/jlibtorrent/TorrentInfo.java
Expand Up @@ -456,7 +456,7 @@ private static torrent_info bdecode0(File file) {
private static torrent_info bdecode0(byte[] data) {
bdecode_node n = new bdecode_node();
error_code ec = new error_code();
int ret = bdecode_node.bdecode(Vectors.bytes2char_vector(data), n, ec);
int ret = bdecode_node.bdecode(Vectors.bytes2byte_vector(data), n, ec);

if (ret == 0) {
ec.clear();
Expand Down
17 changes: 9 additions & 8 deletions src/main/java/com/frostwire/jlibtorrent/Vectors.java
Expand Up @@ -22,30 +22,31 @@ public static byte[] char_vector2bytes(char_vector v) {
return arr;
}

public static void byte_vector2bytes(byte_vector v, byte[] arr) {
public static byte[] byte_vector2bytes(byte_vector v) {
int size = (int) v.size();
byte[] arr = new byte[size];

for (int i = 0; i < size; i++) {
arr[i] = v.get(i);
}

return arr;
}

public static void char_vector2bytes(char_vector v, byte[] arr) {
public static void byte_vector2bytes(byte_vector v, byte[] arr) {
int size = (int) v.size();

for (int i = 0; i < size; i++) {
arr[i] = (byte) v.get(i);
arr[i] = v.get(i);
}
}

public static String char_vector2string(char_vector v, int size) {
StringBuilder sb = new StringBuilder(size);
public static void char_vector2bytes(char_vector v, byte[] arr) {
int size = (int) v.size();

for (int i = 0; i < size; i++) {
sb.append(v.get(i));
arr[i] = (byte) v.get(i);
}

return sb.toString();
}

public static char_vector string2char_vector(String s) {
Expand Down
Expand Up @@ -135,8 +135,8 @@ public static String to_string(bdecode_node e, boolean single_line, int indent)
return libtorrent_jni.bdecode_node_to_string(bdecode_node.getCPtr(e), e, single_line, indent);
}

public static int bdecode(char_vector buffer, bdecode_node ret, error_code ec) {
return libtorrent_jni.bdecode_node_bdecode(char_vector.getCPtr(buffer), buffer, bdecode_node.getCPtr(ret), ret, error_code.getCPtr(ec), ec);
public static int bdecode(byte_vector buffer, bdecode_node ret, error_code ec) {
return libtorrent_jni.bdecode_node_bdecode(byte_vector.getCPtr(buffer), buffer, bdecode_node.getCPtr(ret), ret, error_code.getCPtr(ec), ec);
}

public final static class type_t {
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/frostwire/jlibtorrent/swig/entry.java
Expand Up @@ -116,12 +116,12 @@ public short getM_type_queried() {
return libtorrent_jni.entry_m_type_queried_get(swigCPtr, this);
}

public char_vector bencode() {
return new char_vector(libtorrent_jni.entry_bencode(swigCPtr, this), true);
public byte_vector bencode() {
return new byte_vector(libtorrent_jni.entry_bencode(swigCPtr, this), true);
}

public static entry bdecode(char_vector buffer) {
return new entry(libtorrent_jni.entry_bdecode(char_vector.getCPtr(buffer), buffer), true);
public static entry bdecode(byte_vector buffer) {
return new entry(libtorrent_jni.entry_bdecode(byte_vector.getCPtr(buffer), buffer), true);
}

public final static class data_type {
Expand Down
Expand Up @@ -666,7 +666,7 @@ public class libtorrent_jni {
public final static native void entry_m_type_queried_set(long jarg1, entry jarg1_, short jarg2);
public final static native short entry_m_type_queried_get(long jarg1, entry jarg1_);
public final static native long entry_bencode(long jarg1, entry jarg1_);
public final static native long entry_bdecode(long jarg1, char_vector jarg1_);
public final static native long entry_bdecode(long jarg1, byte_vector jarg1_);
public final static native int sha1_hash_size_get();
public final static native long new_sha1_hash__SWIG_0();
public final static native long sha1_hash_max();
Expand Down Expand Up @@ -2715,7 +2715,7 @@ public class libtorrent_jni {
public final static native String bdecode_node_string_value(long jarg1, bdecode_node jarg1_);
public final static native int bdecode_node_string_length(long jarg1, bdecode_node jarg1_);
public final static native String bdecode_node_to_string(long jarg1, bdecode_node jarg1_, boolean jarg2, int jarg3);
public final static native int bdecode_node_bdecode(long jarg1, char_vector jarg1_, long jarg2, bdecode_node jarg2_, long jarg3, error_code jarg3_);
public final static native int bdecode_node_bdecode(long jarg1, byte_vector jarg1_, long jarg2, bdecode_node jarg2_, long jarg3, error_code jarg3_);
public final static native void delete_bdecode_node(long jarg1);
public final static native String make_magnet_uri__SWIG_0(long jarg1, torrent_handle jarg1_);
public final static native String make_magnet_uri__SWIG_1(long jarg1, torrent_info jarg1_);
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/frostwire/jlibtorrent/demo/LazyRead.java
Expand Up @@ -24,7 +24,7 @@ public static void main(String[] args) throws Throwable {

byte[] data = Utils.readFileToByteArray(torrentFile);

char_vector buffer = Vectors.bytes2char_vector(data);
byte_vector buffer = Vectors.bytes2byte_vector(data);
bdecode_node e = new bdecode_node();
error_code ec = new error_code();
int ret = bdecode_node.bdecode(buffer, e, ec);
Expand Down
Expand Up @@ -25,7 +25,7 @@ public static void main(String[] args) throws Throwable {
System.out.println("Using libtorrent version: " + LibTorrent.version());

byte[] data = Utils.readFileToByteArray(torrentFile);
entry e = entry.bdecode(Vectors.bytes2char_vector(data));
entry e = entry.bdecode(Vectors.bytes2byte_vector(data));

entry_vector files = e.find_key("info").find_key("files").list().to_vector();

Expand Down
12 changes: 6 additions & 6 deletions swig/libtorrent.i
Expand Up @@ -966,14 +966,14 @@ namespace libtorrent {
};

%extend entry {
std::vector<char> bencode() {
std::vector<char> buffer;
std::vector<int8_t> bencode() {
std::vector<int8_t> buffer;
libtorrent::bencode(std::back_inserter(buffer), *$self);
return buffer;
}

static entry bdecode(std::vector<char>& buffer) {
return bdecode(buffer.begin(), buffer.end());
static entry bdecode(std::vector<int8_t>& buffer) {
return libtorrent::bdecode(buffer.begin(), buffer.end());
}
};

Expand All @@ -982,8 +982,8 @@ namespace libtorrent {
return libtorrent::print_entry(e, single_line, indent);
}

static int bdecode(std::vector<char>& buffer, bdecode_node& ret, error_code& ec) {
return libtorrent::bdecode(&buffer[0], &buffer[0] + buffer.size(), ret, ec);
static int bdecode(std::vector<int8_t>& buffer, bdecode_node& ret, error_code& ec) {
return libtorrent::bdecode((char const*)&buffer[0], (char const*)&buffer[0] + buffer.size(), ret, ec);
}
};

Expand Down
30 changes: 15 additions & 15 deletions swig/libtorrent_jni.cpp
Expand Up @@ -1334,13 +1334,13 @@ SWIGINTERN void std_vector_Sl_std_pair_Sl_std_string_Sc_dht_extension_handler_li
else
throw std::out_of_range("vector index out of range");
}
SWIGINTERN std::vector< char > libtorrent_entry_bencode(libtorrent::entry *self){
std::vector<char> buffer;
SWIGINTERN std::vector< int8_t > libtorrent_entry_bencode(libtorrent::entry *self){
std::vector<int8_t> buffer;
libtorrent::bencode(std::back_inserter(buffer), *self);
return buffer;
}
SWIGINTERN libtorrent::entry libtorrent_entry_bdecode(std::vector< char > &buffer){
return bdecode(buffer.begin(), buffer.end());
SWIGINTERN libtorrent::entry libtorrent_entry_bdecode(std::vector< int8_t > &buffer){
return libtorrent::bdecode(buffer.begin(), buffer.end());
}
SWIGINTERN std::string libtorrent_sha1_hash_to_hex(libtorrent::sha1_hash *self){
return libtorrent::to_hex(self->to_string());
Expand Down Expand Up @@ -1537,8 +1537,8 @@ SWIGINTERN void libtorrent_session_handle_add_swig_extension(libtorrent::session
SWIGINTERN std::string libtorrent_bdecode_node_to_string(libtorrent::bdecode_node const &e,bool single_line,int indent){
return libtorrent::print_entry(e, single_line, indent);
}
SWIGINTERN int libtorrent_bdecode_node_bdecode(std::vector< char > &buffer,libtorrent::bdecode_node &ret,libtorrent::error_code &ec){
return libtorrent::bdecode(&buffer[0], &buffer[0] + buffer.size(), ret, ec);
SWIGINTERN int libtorrent_bdecode_node_bdecode(std::vector< int8_t > &buffer,libtorrent::bdecode_node &ret,libtorrent::error_code &ec){
return libtorrent::bdecode((char const*)&buffer[0], (char const*)&buffer[0] + buffer.size(), ret, ec);
}
SWIGINTERN std::string tcp_endpoint_address(tcp::endpoint *self){
return self->address().to_string();
Expand Down Expand Up @@ -18539,7 +18539,7 @@ SWIGEXPORT jshort JNICALL Java_com_frostwire_jlibtorrent_swig_libtorrent_1jni_en
SWIGEXPORT jlong JNICALL Java_com_frostwire_jlibtorrent_swig_libtorrent_1jni_entry_1bencode(JNIEnv *jenv, jclass jcls, jlong jarg1, jobject jarg1_) {
jlong jresult = 0 ;
libtorrent::entry *arg1 = (libtorrent::entry *) 0 ;
std::vector< char > result;
std::vector< int8_t > result;

(void)jenv;
(void)jcls;
Expand All @@ -18554,22 +18554,22 @@ SWIGEXPORT jlong JNICALL Java_com_frostwire_jlibtorrent_swig_libtorrent_1jni_ent
SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, "Unknown exception type");
}
}
*(std::vector< char > **)&jresult = new std::vector< char >((const std::vector< char > &)result);
*(std::vector< int8_t > **)&jresult = new std::vector< int8_t >((const std::vector< int8_t > &)result);
return jresult;
}


SWIGEXPORT jlong JNICALL Java_com_frostwire_jlibtorrent_swig_libtorrent_1jni_entry_1bdecode(JNIEnv *jenv, jclass jcls, jlong jarg1, jobject jarg1_) {
jlong jresult = 0 ;
std::vector< char > *arg1 = 0 ;
std::vector< int8_t > *arg1 = 0 ;
libtorrent::entry result;

(void)jenv;
(void)jcls;
(void)jarg1_;
arg1 = *(std::vector< char > **)&jarg1;
arg1 = *(std::vector< int8_t > **)&jarg1;
if (!arg1) {
SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "std::vector< char > & reference is null");
SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "std::vector< int8_t > & reference is null");
return 0;
}
{
Expand Down Expand Up @@ -57440,7 +57440,7 @@ SWIGEXPORT jstring JNICALL Java_com_frostwire_jlibtorrent_swig_libtorrent_1jni_b

SWIGEXPORT jint JNICALL Java_com_frostwire_jlibtorrent_swig_libtorrent_1jni_bdecode_1node_1bdecode(JNIEnv *jenv, jclass jcls, jlong jarg1, jobject jarg1_, jlong jarg2, jobject jarg2_, jlong jarg3, jobject jarg3_) {
jint jresult = 0 ;
std::vector< char > *arg1 = 0 ;
std::vector< int8_t > *arg1 = 0 ;
libtorrent::bdecode_node *arg2 = 0 ;
libtorrent::error_code *arg3 = 0 ;
int result;
Expand All @@ -57450,9 +57450,9 @@ SWIGEXPORT jint JNICALL Java_com_frostwire_jlibtorrent_swig_libtorrent_1jni_bdec
(void)jarg1_;
(void)jarg2_;
(void)jarg3_;
arg1 = *(std::vector< char > **)&jarg1;
arg1 = *(std::vector< int8_t > **)&jarg1;
if (!arg1) {
SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "std::vector< char > & reference is null");
SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "std::vector< int8_t > & reference is null");
return 0;
}
arg2 = *(libtorrent::bdecode_node **)&jarg2;
Expand Down Expand Up @@ -63916,7 +63916,7 @@ SWIGEXPORT jstring JNICALL Java_com_frostwire_jlibtorrent_swig_libtorrent_1jni_J

(void)jenv;
(void)jcls;
result = (char *)("21fdcd5374f98fec45e5697fc70af14fa57dec4e");
result = (char *)("3f49c322e209c6cbf14886e79edd2d43820b93e0");
if (result) jresult = jenv->NewStringUTF((const char *)result);
return jresult;
}
Expand Down

0 comments on commit afb3b48

Please sign in to comment.