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
33 #include "conn-encoding.h"
37 char Encoding_Buffer[COMMAND_LEN];
39 char *Convert_Message PARAMS((iconv_t Handle, char *Message));
43 * Set client character encoding on a connection.
45 * @param Conn Connection identifier.
46 * @param ClientEnc Client encoding (for example "ASCII", "MacRoman", ...).
47 * @return true on success, false otherwise.
50 Conn_SetEncoding(CONN_ID Conn, const char *ClientEnc)
52 char client_enc[25], server_enc[25];
55 assert(ClientEnc != NULL);
57 Conn_UnsetEncoding(Conn);
59 /* Is the client character set identical to server character set? */
60 if (strcasecmp(ClientEnc, "UTF-8") == 0)
63 snprintf(client_enc, sizeof(client_enc), "%s//TRANSLIT", ClientEnc);
64 snprintf(server_enc, sizeof(server_enc), "%s//TRANSLIT", "UTF-8");
66 My_Connections[Conn].iconv_from = iconv_open(server_enc, client_enc);
67 if (My_Connections[Conn].iconv_from == (iconv_t)(-1)) {
68 Conn_UnsetEncoding(Conn);
71 My_Connections[Conn].iconv_to = iconv_open(client_enc, server_enc);
72 if (My_Connections[Conn].iconv_to == (iconv_t)(-1)) {
73 Conn_UnsetEncoding(Conn);
77 LogDebug("Set client character set of connection \"%d\" to \"%s\".",
83 * Remove client character encoding conversion on a connection.
85 * @param Conn Connection identifier.
88 Conn_UnsetEncoding(CONN_ID Conn)
92 if (My_Connections[Conn].iconv_from != (iconv_t)(-1))
93 iconv_close(My_Connections[Conn].iconv_from);
94 if (My_Connections[Conn].iconv_to != (iconv_t)(-1))
95 iconv_close(My_Connections[Conn].iconv_to);
97 My_Connections[Conn].iconv_from = (iconv_t)(-1);
98 My_Connections[Conn].iconv_to = (iconv_t)(-1);
100 LogDebug("Unset character conversion of connection %d.", Conn);
104 * Convert the encoding of a given message.
106 * This function uses a static buffer for the result of the encoding
107 * conversion which is overwritten by subsequent calls to this function!
109 * @param Handle libiconv handle.
110 * @param Message The message to convert.
111 * @return Pointer to the result.
114 Convert_Message(iconv_t Handle, char *Message)
116 size_t in_left, out_left;
117 char *out = Encoding_Buffer;
119 assert (Handle != (iconv_t)(-1));
120 assert (Message != NULL);
122 in_left = strlen(Message);
123 out_left = sizeof(Encoding_Buffer) - 1;
125 if (iconv(Handle, &Message, &in_left, &out, &out_left) == (size_t)(-1)) {
126 /* An error occured! */
127 LogDebug("Error converting message encoding!");
128 strlcpy(Encoding_Buffer, Message, sizeof(Encoding_Buffer));
129 iconv(Handle, NULL, NULL, NULL, NULL);
133 return Encoding_Buffer;
139 * Convert encoding of a message received from a connection.
141 * Note 1: If no conversion is required, this function returns the original
142 * pointer to the message.
144 * Note 2: This function uses Convert_Message(), so subsequent calls to this
145 * function will overwrite the earlier results.
147 * @param Conn Connection identifier.
148 * @param Message The message to convert.
149 * @return Pointer to the result.
150 * @see Convert_Message
153 Conn_EncodingFrom(UNUSED CONN_ID Conn, char *Message)
156 assert (Message != NULL);
159 if (My_Connections[Conn].iconv_from != (iconv_t)(-1))
160 return Convert_Message(My_Connections[Conn].iconv_from, Message);
166 * Convert encoding of a message for sending on a connection.
168 * Note 1: If no conversion is required, this function returns the original
169 * pointer to the message.
171 * Note 2: This function uses Convert_Message(), so subsequent calls to this
172 * function will overwrite the earlier results.
174 * @param Conn Connection identifier.
175 * @param Message The message to convert.
176 * @return Pointer to the result.
177 * @see Convert_Message
180 Conn_EncodingTo(UNUSED CONN_ID Conn, char *Message)
183 assert (Message != NULL);
186 if (My_Connections[Conn].iconv_to != (iconv_t)(-1))
187 return Convert_Message(My_Connections[Conn].iconv_to, Message);