Adapt migration for files
[pidgin-git.git] / libpurple / plugins / caesarcipher.c
blob0c79589b5aaf0fb4db876f5bf5f43d1b9651dbee
1 /*
2 * An example plugin that demonstrates exporting of a cipher object
3 * type to be used in another plugin.
5 * This plugin only provides the CaesarCipher type. See caesarcipher_consumer
6 * plugin for its use.
8 * Copyright (C) 2013, Ankit Vani <a@nevitus.org>
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of the
13 * License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 * 02111-1301, USA.
26 /* When writing a third-party plugin, do not include libpurple's internal.h
27 * included below. This file is for internal libpurple use only. We're including
28 * it here for our own convenience. */
29 #include "internal.h"
31 /* This file defines PURPLE_PLUGINS and includes all the libpurple headers */
32 #include <purple.h>
34 #include "caesarcipher.h"
36 #define CAESAR_CIPHER_GET_PRIVATE(obj) \
37 (G_TYPE_INSTANCE_GET_PRIVATE((obj), CAESAR_TYPE_CIPHER, CaesarCipherPrivate))
39 typedef struct {
40 gint8 offset;
41 } CaesarCipherPrivate;
43 enum {
44 PROP_NONE,
45 PROP_OFFSET,
46 PROP_LAST,
49 /******************************************************************************
50 * Cipher stuff
51 *****************************************************************************/
52 static void
53 caesar_shift(const guchar input[], size_t in_len, guchar output[], gint8 offset)
55 size_t i;
57 for (i = 0; i < in_len; ++i) {
58 if (input[i] >= 'a' && input[i] <= 'z')
59 output[i] = (((input[i] - 'a') + offset + 26) % 26) + 'a';
60 else if (input[i] >= 'A' && input[i] <= 'Z')
61 output[i] = (((input[i] - 'A') + offset + 26) % 26) + 'A';
62 else
63 output[i] = input[i];
66 output[i] = '\0';
69 static void
70 caesar_cipher_set_offset(PurpleCipher *cipher, gint8 offset)
72 CaesarCipherPrivate *priv = CAESAR_CIPHER_GET_PRIVATE(cipher);
74 priv->offset = offset % 26;
77 static ssize_t
78 caesar_cipher_encrypt(PurpleCipher *cipher, const guchar input[], size_t in_len,
79 guchar output[], size_t out_size)
81 CaesarCipherPrivate *priv = CAESAR_CIPHER_GET_PRIVATE(cipher);
83 g_return_val_if_fail(out_size > in_len, -1);
85 caesar_shift(input, in_len, output, priv->offset);
87 return in_len;
90 static ssize_t
91 caesar_cipher_decrypt(PurpleCipher *cipher, const guchar input[], size_t in_len,
92 guchar output[], size_t out_size)
94 CaesarCipherPrivate *priv = CAESAR_CIPHER_GET_PRIVATE(cipher);
96 g_return_val_if_fail(out_size > in_len, -1);
98 caesar_shift(input, in_len, output, -priv->offset);
100 return in_len;
103 static void
104 caesar_cipher_set_key(PurpleCipher *cipher, const guchar *key, size_t len)
106 caesar_cipher_set_offset(cipher, len);
109 /******************************************************************************
110 * Object stuff
111 *****************************************************************************/
112 static void
113 caesar_cipher_set_property(GObject *obj, guint param_id, const GValue *value,
114 GParamSpec *pspec)
116 PurpleCipher *cipher = PURPLE_CIPHER(obj);
118 switch(param_id) {
119 case PROP_OFFSET:
120 caesar_cipher_set_offset(cipher, g_value_get_schar(value));
121 break;
122 default:
123 G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
124 break;
128 /* initialize the cipher object. used in PURPLE_DEFINE_TYPE. */
129 static void
130 caesar_cipher_init(CaesarCipher *cipher)
132 /* classic caesar cipher uses a shift of 3 */
133 CAESAR_CIPHER_GET_PRIVATE(cipher)->offset = 3;
136 /* initialize the cipher class. used in PURPLE_DEFINE_TYPE. */
137 static void
138 caesar_cipher_class_init(PurpleCipherClass *klass)
140 GObjectClass *obj_class = G_OBJECT_CLASS(klass);
142 obj_class->set_property = caesar_cipher_set_property;
143 g_type_class_add_private(obj_class, sizeof(CaesarCipherPrivate));
145 klass->encrypt = caesar_cipher_encrypt;
146 klass->decrypt = caesar_cipher_decrypt;
147 klass->set_key = caesar_cipher_set_key;
149 g_object_class_install_property(obj_class, PROP_OFFSET,
150 g_param_spec_char("offset", "offset",
151 "The offset by which to shift alphabets",
152 -25, 25, 3,
153 G_PARAM_WRITABLE)
158 * define the CaesarCipher type. this function defines
159 * caesar_cipher_get_type() and caesar_cipher_register_type().
161 PURPLE_DEFINE_TYPE(CaesarCipher, caesar_cipher, PURPLE_TYPE_CIPHER);
163 G_MODULE_EXPORT PurpleCipher *
164 caesar_cipher_new(void)
166 return g_object_new(CAESAR_TYPE_CIPHER, NULL);
169 /******************************************************************************
170 * Plugin stuff
171 *****************************************************************************/
172 static PurplePluginInfo *
173 plugin_query(GError **error)
175 const gchar * const authors[] = {
176 "Ankit Vani <a@nevitus.org>",
177 NULL
180 return purple_plugin_info_new(
181 "id", "core-caesarcipher",
182 "name", N_("Caesar Cipher Provider Example"),
183 "version", DISPLAY_VERSION,
184 "category", N_("Example"),
185 "summary", N_("An example plugin that demonstrates exporting of a "
186 "cipher type."),
187 "description", N_("An example plugin that demonstrates exporting of "
188 "a cipher object type to be used in another "
189 "plugin."),
190 "authors", authors,
191 "website", PURPLE_WEBSITE,
192 "abi-version", PURPLE_ABI_VERSION,
193 NULL
197 static gboolean
198 plugin_load(PurplePlugin *plugin, GError **error)
200 /* register the CaesarCipher type in the type system */
201 caesar_cipher_register_type(plugin);
203 return TRUE;
206 static gboolean
207 plugin_unload(PurplePlugin *plugin, GError **error)
209 return TRUE;
212 PURPLE_PLUGIN_INIT(caesarcipher, plugin_query, plugin_load, plugin_unload);