Skip to content

Commit

Permalink
fix group filenames and refactor peer name on sync
Browse files Browse the repository at this point in the history
  • Loading branch information
zoff99 committed Nov 22, 2024
1 parent 336e18d commit 453275b
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
import static com.zoffcc.applications.trifa.TRIFAGlobals.MESSAGE_V2_MSG_SENT_OK;
import static com.zoffcc.applications.trifa.TRIFAGlobals.NOTIFICATION_EDIT_ACTION.NOTIFICATION_EDIT_ACTION_ADD;
import static com.zoffcc.applications.trifa.TRIFAGlobals.SECONDS_TO_STAY_ONLINE_IN_BATTERY_SAVINGS_MODE;
import static com.zoffcc.applications.trifa.TRIFAGlobals.TOX_NGC_HISTORY_SYNC_MAX_PEERNAME_BYTES;
import static com.zoffcc.applications.trifa.TRIFAGlobals.TRIFA_FT_DIRECTION.TRIFA_FT_DIRECTION_OUTGOING;
import static com.zoffcc.applications.trifa.TRIFAGlobals.TRIFA_MSG_TYPE.TRIFA_MSG_TYPE_TEXT;
import static com.zoffcc.applications.trifa.TRIFAGlobals.TRIFA_SYNC_TYPE.TRIFA_SYNC_TYPE_NGC_PEERS;
Expand Down Expand Up @@ -1689,6 +1690,53 @@ else if ('a' <= b2 && b2 <= 'f')
return res;
}

public static String utf8_string_from_bytes_with_padding(final ByteBuffer buf, final int max_bytes_output,
final String default_str)
{
String ret = default_str;
try
{
byte[] byte_buf = new byte[max_bytes_output];
Arrays.fill(byte_buf, (byte)0x0);
buf.rewind();
buf.get(byte_buf);

int start_index = 0;
int end_index = max_bytes_output - 1;
for(int j=0;j<max_bytes_output;j++)
{
if (byte_buf[j] == 0)
{
start_index = j+1;
}
else
{
break;
}
}

for(int j=(max_bytes_output-1);j>=0;j--)
{
if (byte_buf[j] == 0)
{
end_index = j;
}
else
{
break;
}
}

byte[] byte_buf_stripped = Arrays.copyOfRange(byte_buf, start_index,end_index);
ret = new String(byte_buf_stripped, StandardCharsets.UTF_8);
}
catch(Exception e)
{
e.printStackTrace();
}
return ret;
}

public static String fourbytes_of_long_to_hex(final long in)
{
return String.format("%08x", in);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import static com.zoffcc.applications.trifa.HelperGeneric.fourbytes_of_long_to_hex;
import static com.zoffcc.applications.trifa.HelperGeneric.io_file_copy;
import static com.zoffcc.applications.trifa.HelperGeneric.update_savedata_file_wrapper;
import static com.zoffcc.applications.trifa.HelperGeneric.utf8_string_from_bytes_with_padding;
import static com.zoffcc.applications.trifa.HelperMsgNotification.change_msg_notification;
import static com.zoffcc.applications.trifa.MainActivity.PREF__conference_show_system_messages;
import static com.zoffcc.applications.trifa.MainActivity.android_tox_callback_conference_title_cb_method;
Expand All @@ -86,6 +87,7 @@
import static com.zoffcc.applications.trifa.TRIFAGlobals.MESSAGE_GROUP_HISTORY_SYNC_DOUBLE_INTERVAL_SECS;
import static com.zoffcc.applications.trifa.TRIFAGlobals.MESSAGE_SYNC_DOUBLE_INTERVAL_SECS;
import static com.zoffcc.applications.trifa.TRIFAGlobals.NOTIFICATION_EDIT_ACTION.NOTIFICATION_EDIT_ACTION_ADD;
import static com.zoffcc.applications.trifa.TRIFAGlobals.TOX_NGC_HISTORY_SYNC_MAX_FILENAME_BYTES;
import static com.zoffcc.applications.trifa.TRIFAGlobals.TOX_NGC_HISTORY_SYNC_MAX_PEERNAME_BYTES;
import static com.zoffcc.applications.trifa.TRIFAGlobals.TOX_NGC_HISTORY_SYNC_MAX_SECONDS_BACK;
import static com.zoffcc.applications.trifa.TRIFAGlobals.TRIFA_FT_DIRECTION.TRIFA_FT_DIRECTION_INCOMING;
Expand Down Expand Up @@ -1240,6 +1242,10 @@ static void send_group_image(final GroupMessage g)
// fill with null bytes up to 255 for the filename
data_buf.put((byte) 0x0);
}




// -- now fill the data from file --
java.io.File img_file = new java.io.File(g.filename_fullpath);

Expand Down Expand Up @@ -1563,46 +1569,15 @@ static void handle_incoming_group_file(long group_number, long peer_id, byte[] d
// TODO: fix me!
long timestamp = ((byte)data[8+32]<<3) + ((byte)data[8+32+1]<<2) + ((byte)data[8+32+2]<<1) + (byte)data[8+32+3];

ByteBuffer filename_bytes = ByteBuffer.allocateDirect(TOX_MAX_FILENAME_LENGTH);
filename_bytes.put(data, 8 + 32 + 4, 255);
filename_bytes.rewind();
String filename = "image.jpg";
try
{
byte[] filename_byte_buf = new byte[255];
Arrays.fill(filename_byte_buf, (byte)0x0);
filename_bytes.rewind();
filename_bytes.get(filename_byte_buf);

int start_index = 0;
int end_index = 254;
for(int j=0;j<255;j++)
{
if (filename_byte_buf[j] == 0)
{
start_index = j+1;
}
else
{
break;
}
}

for(int j=254;j>=0;j--)
{
if (filename_byte_buf[j] == 0)
{
end_index = j;
}
else
{
break;
}
}

byte[] filename_byte_buf_stripped = Arrays.copyOfRange(filename_byte_buf,start_index,end_index);
filename = new String(filename_byte_buf_stripped, StandardCharsets.UTF_8);
//Log.i(TAG,"group_custom_packet_cb:filename str=" + filename);
ByteBuffer filename_bytes = ByteBuffer.allocateDirect(TOX_MAX_FILENAME_LENGTH);
filename_bytes.put(data, 8 + 32 + 4, TOX_MAX_FILENAME_LENGTH);
filename = utf8_string_from_bytes_with_padding(filename_bytes,
TOX_MAX_FILENAME_LENGTH,
"image.jpg");
Log.i(TAG,"group_custom_packet_cb:filename str=" + filename);

//Log.i(TAG, "group_custom_packet_cb:filename:"+filename_bytes.arrayOffset()+" "
//+filename_bytes.limit()+" "+filename_bytes.array().length);
Expand Down Expand Up @@ -2103,7 +2078,19 @@ private static void send_ngch_syncfile(final String group_identifier, final Stri
}
catch(Exception e)
{
e.printStackTrace();
}

try
{
Log.i(TAG,"send_ngch_syncmsg:send_ts_bytes:filename_bytes=" +
HelperGeneric.bytesToHex(filename_bytes, 0, filename_bytes.length));
}
catch(Exception e)
{
e.printStackTrace();
}

data_buf.put(filename_bytes);
for (int k=0;k<(TOX_MAX_FILENAME_LENGTH - filename_bytes.length);k++)
{
Expand Down Expand Up @@ -2229,46 +2216,17 @@ else if (timestamp < ((System.currentTimeMillis() / 1000) - (60 * 200)))
// Log.i(TAG, "handle_incoming_sync_group_message:message_id_tox hex=" + message_id_tox);
//
//
ByteBuffer name_buffer = ByteBuffer.allocateDirect(TOX_NGC_HISTORY_SYNC_MAX_PEERNAME_BYTES);
name_buffer.put(data, 8 + 4 + 32 + 4, TOX_NGC_HISTORY_SYNC_MAX_PEERNAME_BYTES);
name_buffer.rewind();
String peer_name = "peer";

try
{
byte[] name_byte_buf = new byte[TOX_NGC_HISTORY_SYNC_MAX_PEERNAME_BYTES];
Arrays.fill(name_byte_buf, (byte)0x0);
name_buffer.rewind();
name_buffer.get(name_byte_buf);
ByteBuffer name_buffer = ByteBuffer.allocateDirect(TOX_NGC_HISTORY_SYNC_MAX_PEERNAME_BYTES);
name_buffer.put(data, 8 + 4 + 32 + 4, TOX_NGC_HISTORY_SYNC_MAX_PEERNAME_BYTES);
String peer_name = utf8_string_from_bytes_with_padding(name_buffer,
TOX_NGC_HISTORY_SYNC_MAX_PEERNAME_BYTES,
"peer");
Log.i(TAG,"handle_incoming_sync_group_message:peer_name str=" + peer_name);

int start_index = 0;
int end_index = TOX_NGC_HISTORY_SYNC_MAX_PEERNAME_BYTES - 1;
for(int j=0;j<TOX_NGC_HISTORY_SYNC_MAX_PEERNAME_BYTES;j++)
{
if (name_byte_buf[j] == 0)
{
start_index = j+1;
}
else
{
break;
}
}

for(int j=(TOX_NGC_HISTORY_SYNC_MAX_PEERNAME_BYTES-1);j>=0;j--)
{
if (name_byte_buf[j] == 0)
{
end_index = j;
}
else
{
break;
}
}

byte[] peername_byte_buf_stripped = Arrays.copyOfRange(name_byte_buf, start_index,end_index);
peer_name = new String(peername_byte_buf_stripped, StandardCharsets.UTF_8);
// Log.i(TAG,"handle_incoming_sync_group_message:peer_name str=" + peer_name);
//
final int header = 6+1+1+4+32+4+25; // 73 bytes
long text_size = length - header;
Expand Down Expand Up @@ -2471,59 +2429,31 @@ else if (timestamp < ((System.currentTimeMillis() / 1000) - (60 * 200)))
// Log.i(TAG, "handle_incoming_sync_group_file:message_id_hash hex=" + message_id_hash);
//
//
ByteBuffer name_buffer = ByteBuffer.allocateDirect(TOX_NGC_HISTORY_SYNC_MAX_PEERNAME_BYTES);
name_buffer.put(data, 8 + 32 + 32 + 4, TOX_NGC_HISTORY_SYNC_MAX_PEERNAME_BYTES);
name_buffer.rewind();
String peer_name = "peer";
try
{
byte[] name_byte_buf = new byte[TOX_NGC_HISTORY_SYNC_MAX_PEERNAME_BYTES];
Arrays.fill(name_byte_buf, (byte)0x0);
name_buffer.rewind();
name_buffer.get(name_byte_buf);

int start_index = 0;
int end_index = TOX_NGC_HISTORY_SYNC_MAX_PEERNAME_BYTES - 1;
for(int j=0;j<TOX_NGC_HISTORY_SYNC_MAX_PEERNAME_BYTES;j++)
{
if (name_byte_buf[j] == 0)
{
start_index = j+1;
}
else
{
break;
}
}

for(int j=(TOX_NGC_HISTORY_SYNC_MAX_PEERNAME_BYTES-1);j>=0;j--)
{
if (name_byte_buf[j] == 0)
{
end_index = j;
}
else
{
break;
}
}

byte[] peername_byte_buf_stripped = Arrays.copyOfRange(name_byte_buf, start_index,end_index);
peer_name = new String(peername_byte_buf_stripped, StandardCharsets.UTF_8);
// Log.i(TAG,"handle_incoming_sync_group_file:peer_name str=" + peer_name);
ByteBuffer name_buffer = ByteBuffer.allocateDirect(TOX_NGC_HISTORY_SYNC_MAX_PEERNAME_BYTES);
name_buffer.put(data, 8 + 32 + 32 + 4, TOX_NGC_HISTORY_SYNC_MAX_PEERNAME_BYTES);
final String peer_name = utf8_string_from_bytes_with_padding(name_buffer,
TOX_NGC_HISTORY_SYNC_MAX_PEERNAME_BYTES,
"peer");
Log.i(TAG,"handle_incoming_sync_group_file:peer_name str=" + peer_name);
//
//
//
// TODO: fixme, get real filename
final String filename = "image.jpg";
ByteBuffer filename_buffer = ByteBuffer.allocateDirect(TOX_NGC_HISTORY_SYNC_MAX_FILENAME_BYTES);
filename_buffer.put(data, 6 + 1 + 1 + 32 + 32 + 4 + 25, TOX_NGC_HISTORY_SYNC_MAX_FILENAME_BYTES);
final String filename = utf8_string_from_bytes_with_padding(filename_buffer,
TOX_NGC_HISTORY_SYNC_MAX_FILENAME_BYTES,
"image.jpg");
Log.i(TAG, "handle_incoming_sync_group_file:filename=" + filename);
//
//
//
final int header = 6+1+1+32+32+4+25+255;
long filedata_size = length - header;
if ((filedata_size < 1) || (filedata_size > 37000))
{
// Log.i(TAG, "handle_incoming_sync_group_file: file size less than 1 byte or larger than 37000 bytes");
Log.i(TAG, "handle_incoming_sync_group_file: file size less than 1 byte or larger than 37000 bytes");
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public class TRIFAGlobals
public static final int MAX_TEXTMSG_RESEND_COUNT_OLDMSG_VERSION = 4;

public static final int TOX_NGC_HISTORY_SYNC_MAX_PEERNAME_BYTES = 25;
public static final int TOX_NGC_HISTORY_SYNC_MAX_FILENAME_BYTES = 255;
public static final int TOX_NGC_HISTORY_SYNC_MAX_SECONDS_BACK = 130 * 60; // 130 minutes

public static final long NGC_NEW_PEERS_TIMEDELTA_IN_MS = (2 * 3600) * 1000; // 2hrs in millis
Expand Down

0 comments on commit 453275b

Please sign in to comment.