Skip to content

Commit 453275b

Browse files
committed
fix group filenames and refactor peer name on sync
1 parent 336e18d commit 453275b

File tree

3 files changed

+93
-114
lines changed

3 files changed

+93
-114
lines changed

android-refimpl-app/app/src/main/java/com/zoffcc/applications/trifa/HelperGeneric.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@
126126
import static com.zoffcc.applications.trifa.TRIFAGlobals.MESSAGE_V2_MSG_SENT_OK;
127127
import static com.zoffcc.applications.trifa.TRIFAGlobals.NOTIFICATION_EDIT_ACTION.NOTIFICATION_EDIT_ACTION_ADD;
128128
import static com.zoffcc.applications.trifa.TRIFAGlobals.SECONDS_TO_STAY_ONLINE_IN_BATTERY_SAVINGS_MODE;
129+
import static com.zoffcc.applications.trifa.TRIFAGlobals.TOX_NGC_HISTORY_SYNC_MAX_PEERNAME_BYTES;
129130
import static com.zoffcc.applications.trifa.TRIFAGlobals.TRIFA_FT_DIRECTION.TRIFA_FT_DIRECTION_OUTGOING;
130131
import static com.zoffcc.applications.trifa.TRIFAGlobals.TRIFA_MSG_TYPE.TRIFA_MSG_TYPE_TEXT;
131132
import static com.zoffcc.applications.trifa.TRIFAGlobals.TRIFA_SYNC_TYPE.TRIFA_SYNC_TYPE_NGC_PEERS;
@@ -1689,6 +1690,53 @@ else if ('a' <= b2 && b2 <= 'f')
16891690
return res;
16901691
}
16911692

1693+
public static String utf8_string_from_bytes_with_padding(final ByteBuffer buf, final int max_bytes_output,
1694+
final String default_str)
1695+
{
1696+
String ret = default_str;
1697+
try
1698+
{
1699+
byte[] byte_buf = new byte[max_bytes_output];
1700+
Arrays.fill(byte_buf, (byte)0x0);
1701+
buf.rewind();
1702+
buf.get(byte_buf);
1703+
1704+
int start_index = 0;
1705+
int end_index = max_bytes_output - 1;
1706+
for(int j=0;j<max_bytes_output;j++)
1707+
{
1708+
if (byte_buf[j] == 0)
1709+
{
1710+
start_index = j+1;
1711+
}
1712+
else
1713+
{
1714+
break;
1715+
}
1716+
}
1717+
1718+
for(int j=(max_bytes_output-1);j>=0;j--)
1719+
{
1720+
if (byte_buf[j] == 0)
1721+
{
1722+
end_index = j;
1723+
}
1724+
else
1725+
{
1726+
break;
1727+
}
1728+
}
1729+
1730+
byte[] byte_buf_stripped = Arrays.copyOfRange(byte_buf, start_index,end_index);
1731+
ret = new String(byte_buf_stripped, StandardCharsets.UTF_8);
1732+
}
1733+
catch(Exception e)
1734+
{
1735+
e.printStackTrace();
1736+
}
1737+
return ret;
1738+
}
1739+
16921740
public static String fourbytes_of_long_to_hex(final long in)
16931741
{
16941742
return String.format("%08x", in);

android-refimpl-app/app/src/main/java/com/zoffcc/applications/trifa/HelperGroup.java

Lines changed: 44 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import static com.zoffcc.applications.trifa.HelperGeneric.fourbytes_of_long_to_hex;
6262
import static com.zoffcc.applications.trifa.HelperGeneric.io_file_copy;
6363
import static com.zoffcc.applications.trifa.HelperGeneric.update_savedata_file_wrapper;
64+
import static com.zoffcc.applications.trifa.HelperGeneric.utf8_string_from_bytes_with_padding;
6465
import static com.zoffcc.applications.trifa.HelperMsgNotification.change_msg_notification;
6566
import static com.zoffcc.applications.trifa.MainActivity.PREF__conference_show_system_messages;
6667
import static com.zoffcc.applications.trifa.MainActivity.android_tox_callback_conference_title_cb_method;
@@ -86,6 +87,7 @@
8687
import static com.zoffcc.applications.trifa.TRIFAGlobals.MESSAGE_GROUP_HISTORY_SYNC_DOUBLE_INTERVAL_SECS;
8788
import static com.zoffcc.applications.trifa.TRIFAGlobals.MESSAGE_SYNC_DOUBLE_INTERVAL_SECS;
8889
import static com.zoffcc.applications.trifa.TRIFAGlobals.NOTIFICATION_EDIT_ACTION.NOTIFICATION_EDIT_ACTION_ADD;
90+
import static com.zoffcc.applications.trifa.TRIFAGlobals.TOX_NGC_HISTORY_SYNC_MAX_FILENAME_BYTES;
8991
import static com.zoffcc.applications.trifa.TRIFAGlobals.TOX_NGC_HISTORY_SYNC_MAX_PEERNAME_BYTES;
9092
import static com.zoffcc.applications.trifa.TRIFAGlobals.TOX_NGC_HISTORY_SYNC_MAX_SECONDS_BACK;
9193
import static com.zoffcc.applications.trifa.TRIFAGlobals.TRIFA_FT_DIRECTION.TRIFA_FT_DIRECTION_INCOMING;
@@ -1240,6 +1242,10 @@ static void send_group_image(final GroupMessage g)
12401242
// fill with null bytes up to 255 for the filename
12411243
data_buf.put((byte) 0x0);
12421244
}
1245+
1246+
1247+
1248+
12431249
// -- now fill the data from file --
12441250
java.io.File img_file = new java.io.File(g.filename_fullpath);
12451251

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

1566-
ByteBuffer filename_bytes = ByteBuffer.allocateDirect(TOX_MAX_FILENAME_LENGTH);
1567-
filename_bytes.put(data, 8 + 32 + 4, 255);
1568-
filename_bytes.rewind();
15691572
String filename = "image.jpg";
15701573
try
15711574
{
1572-
byte[] filename_byte_buf = new byte[255];
1573-
Arrays.fill(filename_byte_buf, (byte)0x0);
1574-
filename_bytes.rewind();
1575-
filename_bytes.get(filename_byte_buf);
1576-
1577-
int start_index = 0;
1578-
int end_index = 254;
1579-
for(int j=0;j<255;j++)
1580-
{
1581-
if (filename_byte_buf[j] == 0)
1582-
{
1583-
start_index = j+1;
1584-
}
1585-
else
1586-
{
1587-
break;
1588-
}
1589-
}
1590-
1591-
for(int j=254;j>=0;j--)
1592-
{
1593-
if (filename_byte_buf[j] == 0)
1594-
{
1595-
end_index = j;
1596-
}
1597-
else
1598-
{
1599-
break;
1600-
}
1601-
}
1602-
1603-
byte[] filename_byte_buf_stripped = Arrays.copyOfRange(filename_byte_buf,start_index,end_index);
1604-
filename = new String(filename_byte_buf_stripped, StandardCharsets.UTF_8);
1605-
//Log.i(TAG,"group_custom_packet_cb:filename str=" + filename);
1575+
ByteBuffer filename_bytes = ByteBuffer.allocateDirect(TOX_MAX_FILENAME_LENGTH);
1576+
filename_bytes.put(data, 8 + 32 + 4, TOX_MAX_FILENAME_LENGTH);
1577+
filename = utf8_string_from_bytes_with_padding(filename_bytes,
1578+
TOX_MAX_FILENAME_LENGTH,
1579+
"image.jpg");
1580+
Log.i(TAG,"group_custom_packet_cb:filename str=" + filename);
16061581

16071582
//Log.i(TAG, "group_custom_packet_cb:filename:"+filename_bytes.arrayOffset()+" "
16081583
//+filename_bytes.limit()+" "+filename_bytes.array().length);
@@ -2103,7 +2078,19 @@ private static void send_ngch_syncfile(final String group_identifier, final Stri
21032078
}
21042079
catch(Exception e)
21052080
{
2081+
e.printStackTrace();
2082+
}
2083+
2084+
try
2085+
{
2086+
Log.i(TAG,"send_ngch_syncmsg:send_ts_bytes:filename_bytes=" +
2087+
HelperGeneric.bytesToHex(filename_bytes, 0, filename_bytes.length));
2088+
}
2089+
catch(Exception e)
2090+
{
2091+
e.printStackTrace();
21062092
}
2093+
21072094
data_buf.put(filename_bytes);
21082095
for (int k=0;k<(TOX_MAX_FILENAME_LENGTH - filename_bytes.length);k++)
21092096
{
@@ -2229,46 +2216,17 @@ else if (timestamp < ((System.currentTimeMillis() / 1000) - (60 * 200)))
22292216
// Log.i(TAG, "handle_incoming_sync_group_message:message_id_tox hex=" + message_id_tox);
22302217
//
22312218
//
2232-
ByteBuffer name_buffer = ByteBuffer.allocateDirect(TOX_NGC_HISTORY_SYNC_MAX_PEERNAME_BYTES);
2233-
name_buffer.put(data, 8 + 4 + 32 + 4, TOX_NGC_HISTORY_SYNC_MAX_PEERNAME_BYTES);
2234-
name_buffer.rewind();
2235-
String peer_name = "peer";
2219+
22362220
try
22372221
{
2238-
byte[] name_byte_buf = new byte[TOX_NGC_HISTORY_SYNC_MAX_PEERNAME_BYTES];
2239-
Arrays.fill(name_byte_buf, (byte)0x0);
2240-
name_buffer.rewind();
2241-
name_buffer.get(name_byte_buf);
2222+
ByteBuffer name_buffer = ByteBuffer.allocateDirect(TOX_NGC_HISTORY_SYNC_MAX_PEERNAME_BYTES);
2223+
name_buffer.put(data, 8 + 4 + 32 + 4, TOX_NGC_HISTORY_SYNC_MAX_PEERNAME_BYTES);
2224+
String peer_name = utf8_string_from_bytes_with_padding(name_buffer,
2225+
TOX_NGC_HISTORY_SYNC_MAX_PEERNAME_BYTES,
2226+
"peer");
2227+
Log.i(TAG,"handle_incoming_sync_group_message:peer_name str=" + peer_name);
22422228

2243-
int start_index = 0;
2244-
int end_index = TOX_NGC_HISTORY_SYNC_MAX_PEERNAME_BYTES - 1;
2245-
for(int j=0;j<TOX_NGC_HISTORY_SYNC_MAX_PEERNAME_BYTES;j++)
2246-
{
2247-
if (name_byte_buf[j] == 0)
2248-
{
2249-
start_index = j+1;
2250-
}
2251-
else
2252-
{
2253-
break;
2254-
}
2255-
}
22562229

2257-
for(int j=(TOX_NGC_HISTORY_SYNC_MAX_PEERNAME_BYTES-1);j>=0;j--)
2258-
{
2259-
if (name_byte_buf[j] == 0)
2260-
{
2261-
end_index = j;
2262-
}
2263-
else
2264-
{
2265-
break;
2266-
}
2267-
}
2268-
2269-
byte[] peername_byte_buf_stripped = Arrays.copyOfRange(name_byte_buf, start_index,end_index);
2270-
peer_name = new String(peername_byte_buf_stripped, StandardCharsets.UTF_8);
2271-
// Log.i(TAG,"handle_incoming_sync_group_message:peer_name str=" + peer_name);
22722230
//
22732231
final int header = 6+1+1+4+32+4+25; // 73 bytes
22742232
long text_size = length - header;
@@ -2471,59 +2429,31 @@ else if (timestamp < ((System.currentTimeMillis() / 1000) - (60 * 200)))
24712429
// Log.i(TAG, "handle_incoming_sync_group_file:message_id_hash hex=" + message_id_hash);
24722430
//
24732431
//
2474-
ByteBuffer name_buffer = ByteBuffer.allocateDirect(TOX_NGC_HISTORY_SYNC_MAX_PEERNAME_BYTES);
2475-
name_buffer.put(data, 8 + 32 + 32 + 4, TOX_NGC_HISTORY_SYNC_MAX_PEERNAME_BYTES);
2476-
name_buffer.rewind();
2477-
String peer_name = "peer";
24782432
try
24792433
{
2480-
byte[] name_byte_buf = new byte[TOX_NGC_HISTORY_SYNC_MAX_PEERNAME_BYTES];
2481-
Arrays.fill(name_byte_buf, (byte)0x0);
2482-
name_buffer.rewind();
2483-
name_buffer.get(name_byte_buf);
2484-
2485-
int start_index = 0;
2486-
int end_index = TOX_NGC_HISTORY_SYNC_MAX_PEERNAME_BYTES - 1;
2487-
for(int j=0;j<TOX_NGC_HISTORY_SYNC_MAX_PEERNAME_BYTES;j++)
2488-
{
2489-
if (name_byte_buf[j] == 0)
2490-
{
2491-
start_index = j+1;
2492-
}
2493-
else
2494-
{
2495-
break;
2496-
}
2497-
}
2498-
2499-
for(int j=(TOX_NGC_HISTORY_SYNC_MAX_PEERNAME_BYTES-1);j>=0;j--)
2500-
{
2501-
if (name_byte_buf[j] == 0)
2502-
{
2503-
end_index = j;
2504-
}
2505-
else
2506-
{
2507-
break;
2508-
}
2509-
}
2510-
2511-
byte[] peername_byte_buf_stripped = Arrays.copyOfRange(name_byte_buf, start_index,end_index);
2512-
peer_name = new String(peername_byte_buf_stripped, StandardCharsets.UTF_8);
2513-
// Log.i(TAG,"handle_incoming_sync_group_file:peer_name str=" + peer_name);
2434+
ByteBuffer name_buffer = ByteBuffer.allocateDirect(TOX_NGC_HISTORY_SYNC_MAX_PEERNAME_BYTES);
2435+
name_buffer.put(data, 8 + 32 + 32 + 4, TOX_NGC_HISTORY_SYNC_MAX_PEERNAME_BYTES);
2436+
final String peer_name = utf8_string_from_bytes_with_padding(name_buffer,
2437+
TOX_NGC_HISTORY_SYNC_MAX_PEERNAME_BYTES,
2438+
"peer");
2439+
Log.i(TAG,"handle_incoming_sync_group_file:peer_name str=" + peer_name);
25142440
//
25152441
//
25162442
//
2517-
// TODO: fixme, get real filename
2518-
final String filename = "image.jpg";
2443+
ByteBuffer filename_buffer = ByteBuffer.allocateDirect(TOX_NGC_HISTORY_SYNC_MAX_FILENAME_BYTES);
2444+
filename_buffer.put(data, 6 + 1 + 1 + 32 + 32 + 4 + 25, TOX_NGC_HISTORY_SYNC_MAX_FILENAME_BYTES);
2445+
final String filename = utf8_string_from_bytes_with_padding(filename_buffer,
2446+
TOX_NGC_HISTORY_SYNC_MAX_FILENAME_BYTES,
2447+
"image.jpg");
2448+
Log.i(TAG, "handle_incoming_sync_group_file:filename=" + filename);
25192449
//
25202450
//
25212451
//
25222452
final int header = 6+1+1+32+32+4+25+255;
25232453
long filedata_size = length - header;
25242454
if ((filedata_size < 1) || (filedata_size > 37000))
25252455
{
2526-
// Log.i(TAG, "handle_incoming_sync_group_file: file size less than 1 byte or larger than 37000 bytes");
2456+
Log.i(TAG, "handle_incoming_sync_group_file: file size less than 1 byte or larger than 37000 bytes");
25272457
return;
25282458
}
25292459

android-refimpl-app/app/src/main/java/com/zoffcc/applications/trifa/TRIFAGlobals.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public class TRIFAGlobals
7979
public static final int MAX_TEXTMSG_RESEND_COUNT_OLDMSG_VERSION = 4;
8080

8181
public static final int TOX_NGC_HISTORY_SYNC_MAX_PEERNAME_BYTES = 25;
82+
public static final int TOX_NGC_HISTORY_SYNC_MAX_FILENAME_BYTES = 255;
8283
public static final int TOX_NGC_HISTORY_SYNC_MAX_SECONDS_BACK = 130 * 60; // 130 minutes
8384

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

0 commit comments

Comments
 (0)