2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2014 Alexander Barton (alex@barton.de) and Contributors.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * Please read the file COPYING, README and AUTHORS for more information.
12 #define __conn_encoding_c__
20 * Functions to deal with character encodings and conversions
33 #include "conn-encoding.h"
35 char Encoding_Buffer[COMMAND_LEN];
36 char *Convert_Message PARAMS((iconv_t Handle, char *Message));
39 * Set client character encoding on a connection.
41 * @param Conn Connection identifier.
42 * @param ClientEnc Client encoding (for example "ASCII", "MacRoman", ...).
43 * @return true on success, false otherwise.
46 Conn_SetEncoding(CONN_ID Conn, const char *ClientEnc)
48 char client_enc[25], server_enc[25];
51 assert(ClientEnc != NULL);
53 Conn_UnsetEncoding(Conn);
55 /* Is the client character set identical to server character set? */
56 if (strcasecmp(ClientEnc, "UTF-8") == 0)
59 snprintf(client_enc, sizeof(client_enc), "%s//TRANSLIT", ClientEnc);
60 snprintf(server_enc, sizeof(server_enc), "%s//TRANSLIT", "UTF-8");
62 My_Connections[Conn].iconv_from = iconv_open(server_enc, client_enc);
63 if (My_Connections[Conn].iconv_from == (iconv_t)(-1)) {
64 Conn_UnsetEncoding(Conn);
67 My_Connections[Conn].iconv_to = iconv_open(client_enc, server_enc);
68 if (My_Connections[Conn].iconv_to == (iconv_t)(-1)) {
69 Conn_UnsetEncoding(Conn);
73 LogDebug("Set client character set of connection \"%d\" to \"%s\".",
79 * Remove client character encoding conversion on a connection.
81 * @param Conn Connection identifier.
84 Conn_UnsetEncoding(CONN_ID Conn)
88 if (My_Connections[Conn].iconv_from != (iconv_t)(-1))
89 iconv_close(My_Connections[Conn].iconv_from);
90 if (My_Connections[Conn].iconv_to != (iconv_t)(-1))
91 iconv_close(My_Connections[Conn].iconv_to);
93 My_Connections[Conn].iconv_from = (iconv_t)(-1);
94 My_Connections[Conn].iconv_to = (iconv_t)(-1);
96 LogDebug("Unset character conversion of connection %d.", Conn);
100 * Convert the encoding of a given message.
102 * This function uses a static buffer for the result of the encoding
103 * conversion which is overwritten by subsequent calls to this function!
105 * @param Handle libiconv handle.
106 * @param Message The message to convert.
107 * @return Pointer to the result.
110 Convert_Message(iconv_t Handle, char *Message)
112 size_t in_left, out_left;
113 char *out = Encoding_Buffer;
115 assert (Handle != (iconv_t)(-1));
116 assert (Message != NULL);
118 in_left = strlen(Message);
119 out_left = sizeof(Encoding_Buffer) - 1;
121 if (iconv(Handle, &Message, &in_left, &out, &out_left) == (size_t)(-1)) {
122 /* An error occurred! */
123 LogDebug("Error converting message encoding!");
124 strlcpy(out, Message, sizeof(Encoding_Buffer));
125 iconv(Handle, NULL, NULL, NULL, NULL);
129 return Encoding_Buffer;
135 * Convert encoding of a message received from a connection.
137 * Note 1: If no conversion is required, this function returns the original
138 * pointer to the message.
140 * Note 2: This function uses Convert_Message(), so subsequent calls to this
141 * function will overwrite the earlier results.
143 * @param Conn Connection identifier.
144 * @param Message The message to convert.
145 * @return Pointer to the result.
146 * @see Convert_Message
149 Conn_EncodingFrom(UNUSED CONN_ID Conn, char *Message)
152 assert (Message != NULL);
155 if (My_Connections[Conn].iconv_from != (iconv_t)(-1))
156 return Convert_Message(My_Connections[Conn].iconv_from, Message);
162 * Convert encoding of a message for sending on a connection.
164 * Note 1: If no conversion is required, this function returns the original
165 * pointer to the message.
167 * Note 2: This function uses Convert_Message(), so subsequent calls to this
168 * function will overwrite the earlier results.
170 * @param Conn Connection identifier.
171 * @param Message The message to convert.
172 * @return Pointer to the result.
173 * @see Convert_Message
176 Conn_EncodingTo(UNUSED CONN_ID Conn, char *Message)
179 assert (Message != NULL);
182 if (My_Connections[Conn].iconv_to != (iconv_t)(-1))
183 return Convert_Message(My_Connections[Conn].iconv_to, Message);