ffi-tox 0.1.1 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/ProjectTox-Core/AUTHORS +0 -0
- data/ProjectTox-Core/ChangeLog +0 -0
- data/ProjectTox-Core/INSTALL +370 -0
- data/ProjectTox-Core/INSTALL.md +455 -56
- data/ProjectTox-Core/Makefile.am +35 -0
- data/ProjectTox-Core/NEWS +0 -0
- data/ProjectTox-Core/README +43 -0
- data/ProjectTox-Core/README.md +34 -44
- data/ProjectTox-Core/auto_tests/Makefile.inc +110 -0
- data/ProjectTox-Core/auto_tests/TCP_test.c +519 -0
- data/ProjectTox-Core/auto_tests/assoc_test.c +160 -0
- data/ProjectTox-Core/auto_tests/crypto_test.c +302 -0
- data/ProjectTox-Core/auto_tests/dht_test.c +362 -0
- data/ProjectTox-Core/auto_tests/encryptsave_test.c +104 -0
- data/ProjectTox-Core/auto_tests/friends_test.c +238 -0
- data/ProjectTox-Core/auto_tests/helpers.h +15 -0
- data/ProjectTox-Core/auto_tests/messenger_test.c +365 -0
- data/ProjectTox-Core/auto_tests/network_test.c +171 -0
- data/ProjectTox-Core/auto_tests/onion_test.c +363 -0
- data/ProjectTox-Core/auto_tests/skeleton_test.c +49 -0
- data/ProjectTox-Core/auto_tests/tox_test.c +454 -0
- data/ProjectTox-Core/auto_tests/toxav_basic_test.c +597 -0
- data/ProjectTox-Core/auto_tests/toxav_many_test.c +402 -0
- data/ProjectTox-Core/autogen.sh +6 -0
- data/ProjectTox-Core/build/Makefile.am +14 -0
- data/ProjectTox-Core/configure.ac +694 -0
- data/ProjectTox-Core/dist-build/android-arm.sh +3 -0
- data/ProjectTox-Core/dist-build/android-armv7.sh +3 -0
- data/ProjectTox-Core/dist-build/android-build.sh +59 -0
- data/ProjectTox-Core/dist-build/android-mips.sh +3 -0
- data/ProjectTox-Core/dist-build/android-x86.sh +3 -0
- data/ProjectTox-Core/docs/Group-Chats.md +71 -0
- data/ProjectTox-Core/docs/Hardening.txt +60 -0
- data/ProjectTox-Core/docs/Hardening_docs.txt +30 -0
- data/ProjectTox-Core/docs/Prevent_Tracking.txt +160 -0
- data/ProjectTox-Core/docs/TCP_Network.txt +154 -0
- data/ProjectTox-Core/docs/TODO +62 -0
- data/ProjectTox-Core/docs/Tox_middle_level_network_protocol.txt +120 -0
- data/ProjectTox-Core/docs/av_api.md +194 -0
- data/ProjectTox-Core/libtoxav.pc.in +11 -0
- data/ProjectTox-Core/libtoxcore.pc.in +11 -0
- data/ProjectTox-Core/m4/ax_have_epoll.m4 +104 -0
- data/ProjectTox-Core/m4/ax_pthread.m4 +317 -0
- data/ProjectTox-Core/m4/pkg.m4 +199 -0
- data/ProjectTox-Core/other/DHT_bootstrap.c +121 -58
- data/ProjectTox-Core/other/DHTnodes +3 -0
- data/ProjectTox-Core/other/Makefile.inc +20 -0
- data/ProjectTox-Core/other/bootstrap_node_packets.c +65 -0
- data/ProjectTox-Core/other/tox.png +0 -0
- data/ProjectTox-Core/testing/DHT_test.c +170 -98
- data/ProjectTox-Core/testing/Makefile.inc +112 -0
- data/ProjectTox-Core/testing/Messenger_test.c +133 -69
- data/ProjectTox-Core/testing/dns3_test.c +115 -0
- data/ProjectTox-Core/testing/misc_tools.c +59 -13
- data/ProjectTox-Core/testing/nTox.c +1127 -264
- data/ProjectTox-Core/testing/nTox.h +10 -19
- data/ProjectTox-Core/testing/tox_shell.c +159 -0
- data/ProjectTox-Core/testing/tox_sync.c +299 -0
- data/ProjectTox-Core/tools/README +11 -0
- data/ProjectTox-Core/tools/astylerc +11 -0
- data/ProjectTox-Core/tools/pre-commit +17 -0
- data/ProjectTox-Core/toxav/Makefile.inc +36 -0
- data/ProjectTox-Core/toxav/codec.c +357 -0
- data/ProjectTox-Core/toxav/codec.h +116 -0
- data/ProjectTox-Core/toxav/msi.c +1949 -0
- data/ProjectTox-Core/toxav/msi.h +267 -0
- data/ProjectTox-Core/toxav/rtp.c +600 -0
- data/ProjectTox-Core/toxav/rtp.h +196 -0
- data/ProjectTox-Core/toxav/toxav.c +1148 -0
- data/ProjectTox-Core/toxav/toxav.h +389 -0
- data/ProjectTox-Core/toxcore/DHT.c +2521 -0
- data/ProjectTox-Core/toxcore/DHT.h +412 -0
- data/ProjectTox-Core/toxcore/LAN_discovery.c +322 -0
- data/ProjectTox-Core/{core → toxcore}/LAN_discovery.h +17 -12
- data/ProjectTox-Core/toxcore/Makefile.inc +67 -0
- data/ProjectTox-Core/toxcore/Messenger.c +3006 -0
- data/ProjectTox-Core/toxcore/Messenger.h +818 -0
- data/ProjectTox-Core/toxcore/TCP_client.c +858 -0
- data/ProjectTox-Core/toxcore/TCP_client.h +156 -0
- data/ProjectTox-Core/toxcore/TCP_server.c +1332 -0
- data/ProjectTox-Core/toxcore/TCP_server.h +181 -0
- data/ProjectTox-Core/toxcore/assoc.c +1033 -0
- data/ProjectTox-Core/toxcore/assoc.h +104 -0
- data/ProjectTox-Core/toxcore/crypto_core.c +278 -0
- data/ProjectTox-Core/toxcore/crypto_core.h +151 -0
- data/ProjectTox-Core/toxcore/friend_requests.c +175 -0
- data/ProjectTox-Core/toxcore/friend_requests.h +83 -0
- data/ProjectTox-Core/toxcore/group_chats.c +837 -0
- data/ProjectTox-Core/toxcore/group_chats.h +199 -0
- data/ProjectTox-Core/toxcore/list.c +256 -0
- data/ProjectTox-Core/toxcore/list.h +85 -0
- data/ProjectTox-Core/toxcore/logger.c +153 -0
- data/ProjectTox-Core/toxcore/logger.h +84 -0
- data/ProjectTox-Core/toxcore/misc_tools.h +70 -0
- data/ProjectTox-Core/toxcore/net_crypto.c +2753 -0
- data/ProjectTox-Core/toxcore/net_crypto.h +410 -0
- data/ProjectTox-Core/toxcore/network.c +979 -0
- data/ProjectTox-Core/toxcore/network.h +367 -0
- data/ProjectTox-Core/toxcore/onion.c +540 -0
- data/ProjectTox-Core/toxcore/onion.h +150 -0
- data/ProjectTox-Core/toxcore/onion_announce.c +433 -0
- data/ProjectTox-Core/toxcore/onion_announce.h +139 -0
- data/ProjectTox-Core/toxcore/onion_client.c +1347 -0
- data/ProjectTox-Core/toxcore/onion_client.h +253 -0
- data/ProjectTox-Core/toxcore/ping.c +346 -0
- data/ProjectTox-Core/toxcore/ping.h +47 -0
- data/ProjectTox-Core/toxcore/ping_array.c +162 -0
- data/ProjectTox-Core/toxcore/ping_array.h +75 -0
- data/ProjectTox-Core/toxcore/tox.c +940 -0
- data/ProjectTox-Core/toxcore/tox.h +734 -0
- data/ProjectTox-Core/toxcore/util.c +193 -0
- data/ProjectTox-Core/toxcore/util.h +63 -0
- data/ProjectTox-Core/toxdns/Makefile.inc +29 -0
- data/ProjectTox-Core/toxdns/toxdns.c +238 -0
- data/ProjectTox-Core/toxdns/toxdns.h +88 -0
- data/ProjectTox-Core/toxencryptsave/Makefile.inc +45 -0
- data/ProjectTox-Core/toxencryptsave/toxencryptsave.c +179 -0
- data/ProjectTox-Core/toxencryptsave/toxencryptsave.h +74 -0
- data/interfaces/libtox.i +2 -6
- data/lib/ffi-tox/libtox.rb +406 -28
- metadata +124 -46
- data/ProjectTox-Core/CMakeLists.txt +0 -50
- data/ProjectTox-Core/cmake/FindLIBCONFIG.cmake +0 -15
- data/ProjectTox-Core/cmake/FindNaCl.cmake +0 -17
- data/ProjectTox-Core/cmake/FindSODIUM.cmake +0 -15
- data/ProjectTox-Core/core/CMakeLists.txt +0 -19
- data/ProjectTox-Core/core/DHT.c +0 -1104
- data/ProjectTox-Core/core/DHT.h +0 -111
- data/ProjectTox-Core/core/LAN_discovery.c +0 -79
- data/ProjectTox-Core/core/Lossless_UDP.c +0 -755
- data/ProjectTox-Core/core/Lossless_UDP.h +0 -106
- data/ProjectTox-Core/core/Messenger.c +0 -596
- data/ProjectTox-Core/core/Messenger.h +0 -165
- data/ProjectTox-Core/core/friend_requests.c +0 -131
- data/ProjectTox-Core/core/friend_requests.h +0 -51
- data/ProjectTox-Core/core/net_crypto.c +0 -575
- data/ProjectTox-Core/core/net_crypto.h +0 -134
- data/ProjectTox-Core/core/network.c +0 -205
- data/ProjectTox-Core/core/network.h +0 -134
- data/ProjectTox-Core/docs/commands.md +0 -25
- data/ProjectTox-Core/docs/start_guide.de.md +0 -40
- data/ProjectTox-Core/docs/start_guide.md +0 -38
- data/ProjectTox-Core/other/CMakeLists.txt +0 -9
- data/ProjectTox-Core/testing/CMakeLists.txt +0 -18
- data/ProjectTox-Core/testing/DHT_cryptosendfiletest.c +0 -228
- data/ProjectTox-Core/testing/DHT_sendfiletest.c +0 -176
- data/ProjectTox-Core/testing/Lossless_UDP_testclient.c +0 -214
- data/ProjectTox-Core/testing/Lossless_UDP_testserver.c +0 -201
- data/ProjectTox-Core/testing/misc_tools.h +0 -29
- data/ProjectTox-Core/testing/nTox_win32.c +0 -387
- data/ProjectTox-Core/testing/nTox_win32.h +0 -40
- data/ProjectTox-Core/testing/rect.py +0 -45
@@ -0,0 +1,199 @@
|
|
1
|
+
/* group_chats.h
|
2
|
+
*
|
3
|
+
* An implementation of massive text only group chats.
|
4
|
+
*
|
5
|
+
*
|
6
|
+
* Copyright (C) 2013 Tox project All Rights Reserved.
|
7
|
+
*
|
8
|
+
* This file is part of Tox.
|
9
|
+
*
|
10
|
+
* Tox is free software: you can redistribute it and/or modify
|
11
|
+
* it under the terms of the GNU General Public License as published by
|
12
|
+
* the Free Software Foundation, either version 3 of the License, or
|
13
|
+
* (at your option) any later version.
|
14
|
+
*
|
15
|
+
* Tox is distributed in the hope that it will be useful,
|
16
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18
|
+
* GNU General Public License for more details.
|
19
|
+
*
|
20
|
+
* You should have received a copy of the GNU General Public License
|
21
|
+
* along with Tox. If not, see <http://www.gnu.org/licenses/>.
|
22
|
+
*
|
23
|
+
*/
|
24
|
+
|
25
|
+
#ifndef GROUP_CHATS_H
|
26
|
+
#define GROUP_CHATS_H
|
27
|
+
|
28
|
+
#define MAX_NICK_BYTES 128
|
29
|
+
|
30
|
+
typedef struct {
|
31
|
+
uint8_t client_id[crypto_box_PUBLICKEYBYTES];
|
32
|
+
uint64_t pingid;
|
33
|
+
uint64_t last_pinged;
|
34
|
+
IP_Port ping_via;
|
35
|
+
|
36
|
+
uint64_t last_recv;
|
37
|
+
uint64_t last_recv_msgping;
|
38
|
+
uint32_t last_message_number;
|
39
|
+
|
40
|
+
uint8_t nick[MAX_NICK_BYTES];
|
41
|
+
uint16_t nick_len;
|
42
|
+
|
43
|
+
uint8_t deleted;
|
44
|
+
uint64_t deleted_time;
|
45
|
+
} Group_Peer;
|
46
|
+
|
47
|
+
typedef struct {
|
48
|
+
uint8_t client_id[crypto_box_PUBLICKEYBYTES];
|
49
|
+
IP_Port ip_port;
|
50
|
+
uint64_t last_recv;
|
51
|
+
} Group_Close;
|
52
|
+
|
53
|
+
#define GROUP_CLOSE_CONNECTIONS 6
|
54
|
+
|
55
|
+
typedef struct Group_Chat {
|
56
|
+
Networking_Core *net;
|
57
|
+
uint8_t self_public_key[crypto_box_PUBLICKEYBYTES];
|
58
|
+
uint8_t self_secret_key[crypto_box_SECRETKEYBYTES];
|
59
|
+
|
60
|
+
Group_Peer *group;
|
61
|
+
Group_Close close[GROUP_CLOSE_CONNECTIONS];
|
62
|
+
uint32_t numpeers;
|
63
|
+
|
64
|
+
uint32_t message_number;
|
65
|
+
void (*group_message)(struct Group_Chat *m, int, const uint8_t *, uint16_t, void *);
|
66
|
+
void *group_message_userdata;
|
67
|
+
void (*group_action)(struct Group_Chat *m, int, const uint8_t *, uint16_t, void *);
|
68
|
+
void *group_action_userdata;
|
69
|
+
void (*peer_namelistchange)(struct Group_Chat *m, int peer, uint8_t change, void *);
|
70
|
+
void *group_namelistchange_userdata;
|
71
|
+
|
72
|
+
uint64_t last_sent_ping;
|
73
|
+
|
74
|
+
uint8_t nick[MAX_NICK_BYTES];
|
75
|
+
uint16_t nick_len;
|
76
|
+
uint64_t last_sent_nick;
|
77
|
+
|
78
|
+
struct Assoc *assoc;
|
79
|
+
} Group_Chat;
|
80
|
+
|
81
|
+
#define GROUP_CHAT_PING 0
|
82
|
+
#define GROUP_CHAT_NEW_PEER 16
|
83
|
+
#define GROUP_CHAT_QUIT 24
|
84
|
+
#define GROUP_CHAT_PEER_NICK 48
|
85
|
+
#define GROUP_CHAT_CHAT_MESSAGE 64
|
86
|
+
#define GROUP_CHAT_ACTION 63
|
87
|
+
|
88
|
+
/* Copy the name of peernum to name.
|
89
|
+
* name must be at least MAX_NICK_BYTES long.
|
90
|
+
*
|
91
|
+
* return length of name if success
|
92
|
+
* return -1 if failure
|
93
|
+
*/
|
94
|
+
int group_peername(const Group_Chat *chat, int peernum, uint8_t *name);
|
95
|
+
|
96
|
+
/*
|
97
|
+
* Set callback function for chat messages.
|
98
|
+
*
|
99
|
+
* format of function is: function(Group_Chat *chat, peer number, message, message length, userdata)
|
100
|
+
*/
|
101
|
+
void callback_groupmessage(Group_Chat *chat, void (*function)(Group_Chat *chat, int, const uint8_t *, uint16_t, void *),
|
102
|
+
void *userdata);
|
103
|
+
|
104
|
+
/*
|
105
|
+
* Set callback function for actions.
|
106
|
+
*
|
107
|
+
* format of function is: function(Group_Chat *chat, peer number, action, action length, userdata)
|
108
|
+
*/
|
109
|
+
void callback_groupaction(Group_Chat *chat, void (*function)(Group_Chat *chat, int, const uint8_t *, uint16_t, void *),
|
110
|
+
void *userdata);
|
111
|
+
|
112
|
+
/*
|
113
|
+
* Set callback function for peer name list changes.
|
114
|
+
*
|
115
|
+
* It gets called every time the name list changes(new peer/name, deleted peer)
|
116
|
+
*
|
117
|
+
* format of function is: function(Group_Chat *chat, userdata)
|
118
|
+
*/
|
119
|
+
typedef enum {
|
120
|
+
CHAT_CHANGE_PEER_ADD,
|
121
|
+
CHAT_CHANGE_PEER_DEL,
|
122
|
+
CHAT_CHANGE_PEER_NAME,
|
123
|
+
} CHAT_CHANGE;
|
124
|
+
|
125
|
+
void callback_namelistchange(Group_Chat *chat, void (*function)(Group_Chat *chat, int peer, uint8_t change, void *),
|
126
|
+
void *userdata);
|
127
|
+
|
128
|
+
/*
|
129
|
+
* Send a message to the group.
|
130
|
+
*
|
131
|
+
* returns the number of peers it has sent it to.
|
132
|
+
*/
|
133
|
+
uint32_t group_sendmessage(Group_Chat *chat, const uint8_t *message, uint32_t length);
|
134
|
+
|
135
|
+
/*
|
136
|
+
* Send an action to the group.
|
137
|
+
*
|
138
|
+
* returns the number of peers it has sent it to.
|
139
|
+
*/
|
140
|
+
uint32_t group_sendaction(Group_Chat *chat, const uint8_t *action, uint32_t length);
|
141
|
+
|
142
|
+
/*
|
143
|
+
* Set our nick for this group.
|
144
|
+
*
|
145
|
+
* returns -1 on failure, 0 on success.
|
146
|
+
*/
|
147
|
+
int set_nick(Group_Chat *chat, const uint8_t *nick, uint16_t nick_len);
|
148
|
+
|
149
|
+
/*
|
150
|
+
* Tell everyone about a new peer (a person we are inviting for example.)
|
151
|
+
*
|
152
|
+
*/
|
153
|
+
uint32_t group_newpeer(Group_Chat *chat, const uint8_t *client_id);
|
154
|
+
|
155
|
+
|
156
|
+
/* Create a new group chat.
|
157
|
+
*
|
158
|
+
* Returns a new group chat instance if success.
|
159
|
+
*
|
160
|
+
* Returns a NULL pointer if fail.
|
161
|
+
*/
|
162
|
+
Group_Chat *new_groupchat(Networking_Core *net);
|
163
|
+
|
164
|
+
|
165
|
+
/* Return the number of peers in the group chat.
|
166
|
+
*/
|
167
|
+
uint32_t group_numpeers(const Group_Chat *chat);
|
168
|
+
|
169
|
+
/* List all the peers in the group chat.
|
170
|
+
*
|
171
|
+
* Copies the names of the peers to the name[length][MAX_NICK_BYTES] array.
|
172
|
+
*
|
173
|
+
* returns the number of peers.
|
174
|
+
*/
|
175
|
+
uint32_t group_client_names(const Group_Chat *chat, uint8_t names[][MAX_NICK_BYTES], uint16_t lengths[],
|
176
|
+
uint16_t length);
|
177
|
+
|
178
|
+
/* Kill a group chat
|
179
|
+
*
|
180
|
+
* Frees the memory and everything.
|
181
|
+
*/
|
182
|
+
void kill_groupchat(Group_Chat *chat);
|
183
|
+
|
184
|
+
/*
|
185
|
+
* This is the main loop.
|
186
|
+
*/
|
187
|
+
void do_groupchat(Group_Chat *chat);
|
188
|
+
|
189
|
+
/* if we receive a group chat packet we call this function so it can be handled.
|
190
|
+
return 0 if packet is handled correctly.
|
191
|
+
return 1 if it didn't handle the packet or if the packet was shit. */
|
192
|
+
int handle_groupchatpacket(Group_Chat *chat, IP_Port source, const uint8_t *packet, uint32_t length);
|
193
|
+
|
194
|
+
|
195
|
+
void chat_bootstrap(Group_Chat *chat, IP_Port ip_port, const uint8_t *client_id);
|
196
|
+
void chat_bootstrap_nonlazy(Group_Chat *chat, IP_Port ip_port, const uint8_t *client_id);
|
197
|
+
|
198
|
+
|
199
|
+
#endif
|
@@ -0,0 +1,256 @@
|
|
1
|
+
/* list.h
|
2
|
+
*
|
3
|
+
* Simple struct with functions to create a list which associates ids with data
|
4
|
+
* -Allows for finding ids associated with data such as IPs or public keys in a short time
|
5
|
+
* -Should only be used if there are relatively few add/remove calls to the list
|
6
|
+
*
|
7
|
+
* Copyright (C) 2014 Tox project All Rights Reserved.
|
8
|
+
*
|
9
|
+
* This file is part of Tox.
|
10
|
+
*
|
11
|
+
* Tox is free software: you can redistribute it and/or modify
|
12
|
+
* it under the terms of the GNU General Public License as published by
|
13
|
+
* the Free Software Foundation, either version 3 of the License, or
|
14
|
+
* (at your option) any later version.
|
15
|
+
*
|
16
|
+
* Tox is distributed in the hope that it will be useful,
|
17
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
18
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
19
|
+
* GNU General Public License for more details.
|
20
|
+
*
|
21
|
+
* You should have received a copy of the GNU General Public License
|
22
|
+
* along with Tox. If not, see <http://www.gnu.org/licenses/>.
|
23
|
+
*
|
24
|
+
*/
|
25
|
+
|
26
|
+
#include "list.h"
|
27
|
+
|
28
|
+
/* Basically, the elements in the list are placed in order so that they can be searched for easily
|
29
|
+
* -each element is seen as a big-endian integer when ordering them
|
30
|
+
* -the ids array is maintained so that each id always matches
|
31
|
+
* -the search algorithm cuts down the time to find the id associated with a piece of data
|
32
|
+
* at the cost of slow add/remove functions for large lists
|
33
|
+
* -Starts at 1/2 of the array, compares the element in the array with the data,
|
34
|
+
* then moves +/- 1/4 of the array depending on whether the value is greater or lower,
|
35
|
+
* then +- 1/8, etc, until the value is matched or its position where it should be in the array is found
|
36
|
+
* -some considerations since the array size is never perfect
|
37
|
+
*/
|
38
|
+
|
39
|
+
#define INDEX(i) (~i)
|
40
|
+
|
41
|
+
/* Find data in list
|
42
|
+
*
|
43
|
+
* return value:
|
44
|
+
* >= 0 : index of data in array
|
45
|
+
* < 0 : no match, returns index (return value is INDEX(index)) where
|
46
|
+
* the data should be inserted
|
47
|
+
*/
|
48
|
+
static int find(const BS_LIST *list, const void *data)
|
49
|
+
{
|
50
|
+
//should work well, but could be improved
|
51
|
+
if (list->n == 0) {
|
52
|
+
return INDEX(0);
|
53
|
+
}
|
54
|
+
|
55
|
+
uint32_t i = list->n / 2; //current position in the array
|
56
|
+
uint32_t delta = i / 2; //how much we move in the array
|
57
|
+
|
58
|
+
if (!delta) {
|
59
|
+
delta = 1;
|
60
|
+
}
|
61
|
+
|
62
|
+
int d = -1; //used to determine if closest match is found
|
63
|
+
//closest match is found if we move back to where we have already been
|
64
|
+
|
65
|
+
while (1) {
|
66
|
+
int r = memcmp(data, list->data + list->element_size * i, list->element_size);
|
67
|
+
|
68
|
+
if (r == 0) {
|
69
|
+
return i;
|
70
|
+
}
|
71
|
+
|
72
|
+
if (r > 0) {
|
73
|
+
//data is greater
|
74
|
+
//move down
|
75
|
+
i += delta;
|
76
|
+
|
77
|
+
if (d == 0 || i == list->n) {
|
78
|
+
//reached bottom of list, or closest match
|
79
|
+
return INDEX(i);
|
80
|
+
}
|
81
|
+
|
82
|
+
delta = (delta) / 2;
|
83
|
+
|
84
|
+
if (delta == 0) {
|
85
|
+
delta = 1;
|
86
|
+
d = 1;
|
87
|
+
}
|
88
|
+
} else {
|
89
|
+
//data is smaller
|
90
|
+
if (d == 1 || i == 0) {
|
91
|
+
//reached top or list or closest match
|
92
|
+
return INDEX(i);
|
93
|
+
}
|
94
|
+
|
95
|
+
//move up
|
96
|
+
i -= delta;
|
97
|
+
|
98
|
+
delta = (delta) / 2;
|
99
|
+
|
100
|
+
if (delta == 0) {
|
101
|
+
delta = 1;
|
102
|
+
d = 0;
|
103
|
+
}
|
104
|
+
}
|
105
|
+
}
|
106
|
+
}
|
107
|
+
|
108
|
+
/* Resized the list list
|
109
|
+
*
|
110
|
+
* return value:
|
111
|
+
* 1 : success
|
112
|
+
* 0 : failure
|
113
|
+
*/
|
114
|
+
static int resize(BS_LIST *list, uint32_t new_size)
|
115
|
+
{
|
116
|
+
void *p;
|
117
|
+
|
118
|
+
p = realloc(list->data, list->element_size * new_size);
|
119
|
+
|
120
|
+
if (!p) {
|
121
|
+
return 0;
|
122
|
+
} else {
|
123
|
+
list->data = p;
|
124
|
+
}
|
125
|
+
|
126
|
+
p = realloc(list->ids, sizeof(int) * new_size);
|
127
|
+
|
128
|
+
if (!p) {
|
129
|
+
return 0;
|
130
|
+
} else {
|
131
|
+
list->ids = p;
|
132
|
+
}
|
133
|
+
|
134
|
+
return 1;
|
135
|
+
}
|
136
|
+
|
137
|
+
|
138
|
+
int bs_list_init(BS_LIST *list, uint32_t element_size, uint32_t initial_capacity)
|
139
|
+
{
|
140
|
+
//set initial values
|
141
|
+
list->n = 0;
|
142
|
+
list->element_size = element_size;
|
143
|
+
list->capacity = 0;
|
144
|
+
list->data = NULL;
|
145
|
+
list->ids = NULL;
|
146
|
+
|
147
|
+
if (initial_capacity != 0) {
|
148
|
+
if (!resize(list, initial_capacity)) {
|
149
|
+
return 0;
|
150
|
+
}
|
151
|
+
}
|
152
|
+
|
153
|
+
list->capacity = initial_capacity;
|
154
|
+
|
155
|
+
return 1;
|
156
|
+
}
|
157
|
+
|
158
|
+
void bs_list_free(BS_LIST *list)
|
159
|
+
{
|
160
|
+
//free both arrays
|
161
|
+
free(list->data);
|
162
|
+
free(list->ids);
|
163
|
+
}
|
164
|
+
|
165
|
+
int bs_list_find(const BS_LIST *list, const void *data)
|
166
|
+
{
|
167
|
+
int r = find(list, data);
|
168
|
+
|
169
|
+
//return only -1 and positive values
|
170
|
+
if (r < 0) {
|
171
|
+
return -1;
|
172
|
+
}
|
173
|
+
|
174
|
+
return list->ids[r];
|
175
|
+
}
|
176
|
+
|
177
|
+
int bs_list_add(BS_LIST *list, const void *data, int id)
|
178
|
+
{
|
179
|
+
//find where the new element should be inserted
|
180
|
+
//see: return value of find()
|
181
|
+
int i = find(list, data);
|
182
|
+
|
183
|
+
if (i >= 0) {
|
184
|
+
//already in list
|
185
|
+
return 0;
|
186
|
+
}
|
187
|
+
|
188
|
+
i = ~i;
|
189
|
+
|
190
|
+
//increase the size of the arrays if needed
|
191
|
+
if (list->n == list->capacity) {
|
192
|
+
// 1.5 * n + 1
|
193
|
+
const uint32_t new_capacity = list->n + list->n / 2 + 1;
|
194
|
+
|
195
|
+
if (!resize(list, new_capacity)) {
|
196
|
+
return 0;
|
197
|
+
}
|
198
|
+
|
199
|
+
list->capacity = new_capacity;
|
200
|
+
}
|
201
|
+
|
202
|
+
//insert data to element array
|
203
|
+
memmove(list->data + (i + 1) * list->element_size, list->data + i * list->element_size,
|
204
|
+
(list->n - i) * list->element_size);
|
205
|
+
memcpy(list->data + i * list->element_size, data, list->element_size);
|
206
|
+
|
207
|
+
//insert id to id array
|
208
|
+
memmove(&list->ids[i + 1], &list->ids[i], (list->n - i) * sizeof(int));
|
209
|
+
list->ids[i] = id;
|
210
|
+
|
211
|
+
//increase n
|
212
|
+
list->n++;
|
213
|
+
|
214
|
+
return 1;
|
215
|
+
}
|
216
|
+
|
217
|
+
int bs_list_remove(BS_LIST *list, const void *data, int id)
|
218
|
+
{
|
219
|
+
int i = find(list, data);
|
220
|
+
|
221
|
+
if (i < 0) {
|
222
|
+
return 0;
|
223
|
+
}
|
224
|
+
|
225
|
+
if (list->ids[i] != id) {
|
226
|
+
//this should never happen
|
227
|
+
return 0;
|
228
|
+
}
|
229
|
+
|
230
|
+
//decrease the size of the arrays if needed
|
231
|
+
if (list->n < list->capacity / 2) {
|
232
|
+
const uint32_t new_capacity = list->capacity / 2;
|
233
|
+
|
234
|
+
if (resize(list, new_capacity)) {
|
235
|
+
list->capacity = new_capacity;
|
236
|
+
}
|
237
|
+
}
|
238
|
+
|
239
|
+
list->n--;
|
240
|
+
|
241
|
+
memmove(list->data + i * list->element_size, list->data + (i + 1) * list->element_size,
|
242
|
+
(list->n - i) * list->element_size);
|
243
|
+
memmove(&list->ids[i], &list->ids[i + 1], (list->n - i) * sizeof(int));
|
244
|
+
|
245
|
+
return 1;
|
246
|
+
}
|
247
|
+
|
248
|
+
int bs_list_trim(BS_LIST *list)
|
249
|
+
{
|
250
|
+
if (!resize(list, list->n)) {
|
251
|
+
return 0;
|
252
|
+
}
|
253
|
+
|
254
|
+
list->capacity = list->n;
|
255
|
+
return 1;
|
256
|
+
}
|