2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2012 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
31 #include "conn-encoding.h"
35 char Encoding_Buffer[COMMAND_LEN];
37 char *Convert_Message PARAMS((iconv_t Handle, char *Message));
40 * Set client character encoding on a connection.
42 * @param Conn Connection identifier.
43 * @param ClientEnc Client encoding (for example "ASCII", "MacRoman", ...).
44 * @return true on success, false otherwise.
47 Conn_SetEncoding(CONN_ID Conn, const char *ClientEnc)
49 char client_enc[25], server_enc[25];
52 assert(ClientEnc != NULL);
54 Conn_UnsetEncoding(Conn);
56 /* Is the client character set identical to server character set? */
57 if (strcasecmp(ClientEnc, "UTF-8") == 0)
60 snprintf(client_enc, sizeof(client_enc), "%s//TRANSLIT", ClientEnc);
61 snprintf(server_enc, sizeof(server_enc), "%s//TRANSLIT", "UTF-8");
63 My_Connections[Conn].iconv_from = iconv_open(server_enc, client_enc);
64 if (My_Connections[Conn].iconv_from == (iconv_t)(-1)) {
65 Conn_UnsetEncoding(Conn);
68 My_Connections[Conn].iconv_to = iconv_open(client_enc, server_enc);
69 if (My_Connections[Conn].iconv_to == (iconv_t)(-1)) {
70 Conn_UnsetEncoding(Conn);
74 LogDebug("Set client character set of connection \"%d\" to \"%s\".",
80 * Remove client character encoding conversion on a connection.
82 * @param Conn Connection identifier.
85 Conn_UnsetEncoding(CONN_ID Conn)
89 if (My_Connections[Conn].iconv_from != (iconv_t)(-1))
90 iconv_close(My_Connections[Conn].iconv_from);
91 if (My_Connections[Conn].iconv_to != (iconv_t)(-1))
92 iconv_close(My_Connections[Conn].iconv_to);
94 My_Connections[Conn].iconv_from = (iconv_t)(-1);
95 My_Connections[Conn].iconv_to = (iconv_t)(-1);
97 LogDebug("Unset character conversion of connection %d.", Conn);
101 * Convert the encoding of a given message.
103 * This function uses a static buffer for the result of the encoding
104 * conversion which is overwritten by subsequent calls to this function!
106 * @param Handle libiconv handle.
107 * @param Message The message to convert.
108 * @return Pointer to the result.
111 Convert_Message(iconv_t Handle, char *Message)
113 size_t in_left, out_left;
114 char *out = Encoding_Buffer;
116 assert (Handle != (iconv_t)(-1));
117 assert (Message != NULL);
119 in_left = strlen(Message);
120 out_left = sizeof(Encoding_Buffer) - 1;
122 if (iconv(Handle, &Message, &in_left, &out, &out_left) == (size_t)(-1)) {
123 /* An error occurred! */
124 LogDebug("Error converting message encoding!");
125 strlcpy(out, Message, sizeof(Encoding_Buffer));
126 iconv(Handle, NULL, NULL, NULL, NULL);
130 return Encoding_Buffer;
136 * Convert encoding of a message received from a connection.
138 * Note 1: If no conversion is required, this function returns the original
139 * pointer to the message.
141 * Note 2: This function uses Convert_Message(), so subsequent calls to this
142 * function will overwrite the earlier results.
144 * @param Conn Connection identifier.
145 * @param Message The message to convert.
146 * @return Pointer to the result.
147 * @see Convert_Message
150 Conn_EncodingFrom(UNUSED CONN_ID Conn, char *Message)
153 assert (Message != NULL);
156 if (My_Connections[Conn].iconv_from != (iconv_t)(-1))
157 return Convert_Message(My_Connections[Conn].iconv_from, Message);
163 * Convert encoding of a message for sending on a connection.
165 * Note 1: If no conversion is required, this function returns the original
166 * pointer to the message.
168 * Note 2: This function uses Convert_Message(), so subsequent calls to this
169 * function will overwrite the earlier results.
171 * @param Conn Connection identifier.
172 * @param Message The message to convert.
173 * @return Pointer to the result.
174 * @see Convert_Message
177 Conn_EncodingTo(UNUSED CONN_ID Conn, char *Message)
180 assert (Message != NULL);
183 if (My_Connections[Conn].iconv_to != (iconv_t)(-1))
184 return Convert_Message(My_Connections[Conn].iconv_to, Message);