utf8tocp1251.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. 3APA3A simpliest proxy server
  3. (c) 2007-2008 by ZARAZA <3APA3A@security.nnov.ru>
  4. please read License Agreement
  5. $Id: utf8tocp1251.c,v 1.9 2009/09/03 05:32:56 v.dubrovin Exp $
  6. */
  7. #include "../../structures.h"
  8. #include <string.h>
  9. #include <ctype.h>
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. __declspec(dllexport) int utf8tocp1251(struct pluginlink * pluginlink,
  14. int argc, char** argv);
  15. static struct auth alwaysauth;
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. unsigned char * conv_utf8_to_cp1251(unsigned char *s){
  20. int i, j=0, n=(int)strlen(s);
  21. int byte2 = 0;
  22. int c1, new_c1, new_c2, new_i;
  23. for(i = 0; i < n; i++){
  24. if(byte2 && s[i]>=128 && s[i]<=192){
  25. new_c2=(c1&3)*64+(s[i]&63);
  26. new_c1=(c1>>2)&5;
  27. new_i=(new_c1*256)+new_c2;
  28. if(new_i == 1025) s[j++] = 168;
  29. else if (new_i==1105) s[j++] = 184;
  30. else if (new_i < (192 + 848) || new_i > (255 + 848)){
  31. return s;
  32. }
  33. else s[j++] = new_i - 848;
  34. byte2 = 0;
  35. }
  36. else if(byte2){
  37. return s;
  38. }
  39. else if((s[i]>>5)==6){
  40. c1 = s[i];
  41. byte2 = 1;
  42. }
  43. else if(s[i] < 128) s[j++] = s[i];
  44. else return s;
  45. }
  46. s[j] = 0;
  47. return s;
  48. }
  49. static int aufunc(struct clientparam *param){
  50. if(!param->username || !param->password || param->pwtype != 0) return 4;
  51. conv_utf8_to_cp1251(param->username);
  52. conv_utf8_to_cp1251(param->password);
  53. return 4;
  54. }
  55. #ifdef __cplusplus
  56. }
  57. #endif
  58. int utf8tocp1251(struct pluginlink * pluginlink, int argc, char** argv){
  59. static int loaded = 0;
  60. if(!loaded){
  61. alwaysauth.authenticate = aufunc;
  62. alwaysauth.authorize = pluginlink->checkACL;
  63. alwaysauth.desc = "utf8tocp1251";
  64. alwaysauth.next = pluginlink->authfuncs->next;
  65. pluginlink->authfuncs->next = &alwaysauth;
  66. loaded = 1;
  67. }
  68. return 0;
  69. }