libpurple/protocols/gg/keymapper.c

Tue, 19 May 2020 10:29:23 +0000

author
Gary Kramlich <grim@reaperworld.com>
date
Tue, 19 May 2020 10:29:23 +0000
changeset 40417
48c621e76141
parent 35605
8a0ae6cc73fc
child 41084
72d6941bfa2e
permissions
-rw-r--r--

Merged in default (pull request #697)

simple: Fix search for message headers

Approved-by: Gary Kramlich

35358
7beae478c905 Style: fix gg (partially)
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35144
diff changeset
1 /* purple
7beae478c905 Style: fix gg (partially)
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35144
diff changeset
2 *
7beae478c905 Style: fix gg (partially)
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35144
diff changeset
3 * Purple is the legal property of its developers, whose names are too numerous
7beae478c905 Style: fix gg (partially)
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35144
diff changeset
4 * to list here. Please refer to the COPYRIGHT file distributed with this
7beae478c905 Style: fix gg (partially)
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35144
diff changeset
5 * source distribution.
7beae478c905 Style: fix gg (partially)
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35144
diff changeset
6 *
7beae478c905 Style: fix gg (partially)
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35144
diff changeset
7 * Component written by Tomek Wasilczyk (http://www.wasilczyk.pl).
7beae478c905 Style: fix gg (partially)
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35144
diff changeset
8 *
7beae478c905 Style: fix gg (partially)
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35144
diff changeset
9 * This file is dual-licensed under the GPL2+ and the X11 (MIT) licences.
7beae478c905 Style: fix gg (partially)
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35144
diff changeset
10 * As a recipient of this file you may choose, which license to receive the
7beae478c905 Style: fix gg (partially)
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35144
diff changeset
11 * code under. As a contributor, you have to ensure the new code is
7beae478c905 Style: fix gg (partially)
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35144
diff changeset
12 * compatible with both.
7beae478c905 Style: fix gg (partially)
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35144
diff changeset
13 *
7beae478c905 Style: fix gg (partially)
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35144
diff changeset
14 * This program is free software; you can redistribute it and/or modify
7beae478c905 Style: fix gg (partially)
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35144
diff changeset
15 * it under the terms of the GNU General Public License as published by
7beae478c905 Style: fix gg (partially)
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35144
diff changeset
16 * the Free Software Foundation; either version 2 of the License, or
7beae478c905 Style: fix gg (partially)
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35144
diff changeset
17 * (at your option) any later version.
7beae478c905 Style: fix gg (partially)
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35144
diff changeset
18 *
7beae478c905 Style: fix gg (partially)
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35144
diff changeset
19 * This program is distributed in the hope that it will be useful,
7beae478c905 Style: fix gg (partially)
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35144
diff changeset
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7beae478c905 Style: fix gg (partially)
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35144
diff changeset
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7beae478c905 Style: fix gg (partially)
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35144
diff changeset
22 * GNU General Public License for more details.
7beae478c905 Style: fix gg (partially)
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35144
diff changeset
23 *
7beae478c905 Style: fix gg (partially)
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35144
diff changeset
24 * You should have received a copy of the GNU General Public License
7beae478c905 Style: fix gg (partially)
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35144
diff changeset
25 * along with this program; if not, write to the Free Software
7beae478c905 Style: fix gg (partially)
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35144
diff changeset
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
7beae478c905 Style: fix gg (partially)
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35144
diff changeset
27 */
35144
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
28 #include "keymapper.h"
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
29
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
30 /* The problem: we want to convert 64-bit unique integers into unique gpointer
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
31 * keys (that may be 32-bit or 64-bit, or whatever else). We also want to
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
32 * convert it back.
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
33 *
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
34 * The idea: let's store every value in our internal memory. Then, its address
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
35 * can be also an unique key. We also need a map, to quickly figure out the
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
36 * address for any previously stored value.
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
37 *
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
38 * The naming problem: values becames the keys and vice-versa.
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
39 */
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
40
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
41 /* TODO
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
42 * For a 64-bit gpointer, keymapper could just do nothing and return the value
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
43 * as a key. But it have to be figured out at a compile time.
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
44 */
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
45
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
46 struct _ggp_keymapper
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
47 {
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
48 /* Table keys: pointers to 64-bit mapped *values*.
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
49 * Table values: keys (gpointers) corresponding to mapped values.
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
50 *
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
51 * Ultimately, both keys and values are the same pointers.
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
52 *
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
53 * Yes, it's hard to comment it well enough.
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
54 */
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
55 GHashTable *val_to_key;
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
56 };
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
57
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
58 ggp_keymapper *
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
59 ggp_keymapper_new(void)
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
60 {
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
61 ggp_keymapper *km;
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
62
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
63 km = g_new0(ggp_keymapper, 1);
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
64 km->val_to_key = g_hash_table_new_full(g_int64_hash, g_int64_equal,
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
65 g_free, NULL);
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
66
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
67 return km;
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
68 }
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
69
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
70 void
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
71 ggp_keymapper_free(ggp_keymapper *km)
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
72 {
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
73 if (km == NULL)
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
74 return;
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
75
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
76 g_hash_table_destroy(km->val_to_key);
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
77 g_free(km);
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
78 }
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
79
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
80 gpointer
35605
8a0ae6cc73fc Fix win32 build
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35358
diff changeset
81 ggp_keymapper_to_key(ggp_keymapper *km, guint64 val)
35144
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
82 {
35605
8a0ae6cc73fc Fix win32 build
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35358
diff changeset
83 guint64 *key;
35144
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
84
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
85 g_return_val_if_fail(km != NULL, NULL);
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
86
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
87 key = g_hash_table_lookup(km->val_to_key, &val);
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
88 if (key)
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
89 return key;
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
90
35605
8a0ae6cc73fc Fix win32 build
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35358
diff changeset
91 key = g_new(guint64, 1);
35144
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
92 *key = val;
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
93
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
94 g_hash_table_insert(km->val_to_key, key, key);
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
95
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
96 return key;
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
97 }
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
98
35605
8a0ae6cc73fc Fix win32 build
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35358
diff changeset
99 guint64
35144
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
100 ggp_keymapper_from_key(ggp_keymapper *km, gpointer key)
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
101 {
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
102 g_return_val_if_fail(km != NULL, 0);
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
103 g_return_val_if_fail(key != NULL, 0);
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
104
35605
8a0ae6cc73fc Fix win32 build
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35358
diff changeset
105 return *((guint64*)key);
35144
3590ceb242b5 Gadu-Gadu: multilogon / other sessions dialog implementation. Refs #14366
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
106 }

mercurial