utf8tocp1251.c 1.6 KB

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