conn-encoding.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2014 Alexander Barton (alex@barton.de) and Contributors.
  4. *
  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.
  10. */
  11. #define __conn_encoding_c__
  12. #define CONN_MODULE
  13. #include "portab.h"
  14. /**
  15. * @file
  16. * Functions to deal with character encodings and conversions
  17. */
  18. #include <assert.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <strings.h>
  22. #include "conn.h"
  23. #ifdef ICONV
  24. #include "log.h"
  25. #include "conn-encoding.h"
  26. char Encoding_Buffer[COMMAND_LEN];
  27. char *Convert_Message PARAMS((iconv_t Handle, char *Message));
  28. /**
  29. * Set client character encoding on a connection.
  30. *
  31. * @param Conn Connection identifier.
  32. * @param ClientEnc Client encoding (for example "ASCII", "MacRoman", ...).
  33. * @return true on success, false otherwise.
  34. */
  35. GLOBAL bool
  36. Conn_SetEncoding(CONN_ID Conn, const char *ClientEnc)
  37. {
  38. char client_enc[25], server_enc[25];
  39. assert(Conn > NONE);
  40. assert(ClientEnc != NULL);
  41. Conn_UnsetEncoding(Conn);
  42. /* Is the client character set identical to server character set? */
  43. if (strcasecmp(ClientEnc, "UTF-8") == 0)
  44. return true;
  45. snprintf(client_enc, sizeof(client_enc), "%s//TRANSLIT", ClientEnc);
  46. snprintf(server_enc, sizeof(server_enc), "%s//TRANSLIT", "UTF-8");
  47. My_Connections[Conn].iconv_from = iconv_open(server_enc, client_enc);
  48. if (My_Connections[Conn].iconv_from == (iconv_t)(-1)) {
  49. Conn_UnsetEncoding(Conn);
  50. return false;
  51. }
  52. My_Connections[Conn].iconv_to = iconv_open(client_enc, server_enc);
  53. if (My_Connections[Conn].iconv_to == (iconv_t)(-1)) {
  54. Conn_UnsetEncoding(Conn);
  55. return false;
  56. }
  57. LogDebug("Set client character set of connection \"%d\" to \"%s\".",
  58. Conn, client_enc);
  59. return true;
  60. }
  61. /**
  62. * Remove client character encoding conversion on a connection.
  63. *
  64. * @param Conn Connection identifier.
  65. */
  66. GLOBAL void
  67. Conn_UnsetEncoding(CONN_ID Conn)
  68. {
  69. assert(Conn > NONE);
  70. if (My_Connections[Conn].iconv_from != (iconv_t)(-1))
  71. iconv_close(My_Connections[Conn].iconv_from);
  72. if (My_Connections[Conn].iconv_to != (iconv_t)(-1))
  73. iconv_close(My_Connections[Conn].iconv_to);
  74. My_Connections[Conn].iconv_from = (iconv_t)(-1);
  75. My_Connections[Conn].iconv_to = (iconv_t)(-1);
  76. LogDebug("Unset character conversion of connection %d.", Conn);
  77. }
  78. /**
  79. * Convert the encoding of a given message.
  80. *
  81. * This function uses a static buffer for the result of the encoding
  82. * conversion which is overwritten by subsequent calls to this function!
  83. *
  84. * @param Handle libiconv handle.
  85. * @param Message The message to convert.
  86. * @return Pointer to the result.
  87. */
  88. char *
  89. Convert_Message(iconv_t Handle, char *Message)
  90. {
  91. size_t in_left, out_left;
  92. char *out = Encoding_Buffer;
  93. assert (Handle != (iconv_t)(-1));
  94. assert (Message != NULL);
  95. in_left = strlen(Message);
  96. out_left = sizeof(Encoding_Buffer) - 1;
  97. if (iconv(Handle, &Message, &in_left, &out, &out_left) == (size_t)(-1)) {
  98. /* An error occurred! */
  99. LogDebug("Error converting message encoding!");
  100. strlcpy(out, Message, sizeof(Encoding_Buffer));
  101. iconv(Handle, NULL, NULL, NULL, NULL);
  102. } else
  103. *out = '\0';
  104. return Encoding_Buffer;
  105. }
  106. #endif /* ICONV */
  107. /**
  108. * Convert encoding of a message received from a connection.
  109. *
  110. * Note 1: If no conversion is required, this function returns the original
  111. * pointer to the message.
  112. *
  113. * Note 2: This function uses Convert_Message(), so subsequent calls to this
  114. * function will overwrite the earlier results.
  115. *
  116. * @param Conn Connection identifier.
  117. * @param Message The message to convert.
  118. * @return Pointer to the result.
  119. * @see Convert_Message
  120. */
  121. GLOBAL char *
  122. Conn_EncodingFrom(UNUSED CONN_ID Conn, char *Message)
  123. {
  124. assert(Conn > NONE);
  125. assert (Message != NULL);
  126. #ifdef ICONV
  127. if (My_Connections[Conn].iconv_from != (iconv_t)(-1))
  128. return Convert_Message(My_Connections[Conn].iconv_from, Message);
  129. #endif
  130. return Message;
  131. }
  132. /**
  133. * Convert encoding of a message for sending on a connection.
  134. *
  135. * Note 1: If no conversion is required, this function returns the original
  136. * pointer to the message.
  137. *
  138. * Note 2: This function uses Convert_Message(), so subsequent calls to this
  139. * function will overwrite the earlier results.
  140. *
  141. * @param Conn Connection identifier.
  142. * @param Message The message to convert.
  143. * @return Pointer to the result.
  144. * @see Convert_Message
  145. */
  146. GLOBAL char *
  147. Conn_EncodingTo(UNUSED CONN_ID Conn, char *Message)
  148. {
  149. assert(Conn > NONE);
  150. assert (Message != NULL);
  151. #ifdef ICONV
  152. if (My_Connections[Conn].iconv_to != (iconv_t)(-1))
  153. return Convert_Message(My_Connections[Conn].iconv_to, Message);
  154. #endif
  155. return Message;
  156. }
  157. /* -eof- */