php_xxtea.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /***********************************************************************
  2. Copyright 2006-2007 Ma Bingyao
  3. These sources is free software. Redistributions of source code must
  4. retain the above copyright notice. Redistributions in binary form
  5. must reproduce the above copyright notice. You can redistribute it
  6. freely. You can use it with any free or commercial software.
  7. These sources is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY. Without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. You may contact the author by:
  11. e-mail: andot@coolcode.cn
  12. *************************************************************************/
  13. #ifdef HAVE_CONFIG_H
  14. #include "config.h"
  15. #endif
  16. #include "php.h"
  17. #if HAVE_XXTEA
  18. #include "php_xxtea.h"
  19. #include "ext/standard/info.h" /* for phpinfo() functions */
  20. #include "xxtea.h"
  21. /* compiled function list so Zend knows what's in this module */
  22. zend_function_entry xxtea_functions[] =
  23. {
  24. ZEND_FE(xxtea_encrypt, NULL)
  25. ZEND_FE(xxtea_decrypt, NULL)
  26. ZEND_FE(xxtea_info, NULL)
  27. {NULL, NULL, NULL}
  28. };
  29. /* compiled module information */
  30. zend_module_entry xxtea_module_entry =
  31. {
  32. STANDARD_MODULE_HEADER,
  33. XXTEA_MODULE_NAME,
  34. xxtea_functions,
  35. ZEND_MINIT(xxtea),
  36. ZEND_MSHUTDOWN(xxtea),
  37. NULL,
  38. NULL,
  39. ZEND_MINFO(xxtea),
  40. XXTEA_VERSION,
  41. STANDARD_MODULE_PROPERTIES
  42. };
  43. /* implement standard "stub" routine to introduce ourselves to Zend */
  44. #if defined(COMPILE_DL_XXTEA)
  45. ZEND_GET_MODULE(xxtea)
  46. #endif
  47. static xxtea_long *xxtea_to_long_array(unsigned char *data, xxtea_long len, int include_length, xxtea_long *ret_len) {
  48. xxtea_long i, n, *result;
  49. n = len >> 2;
  50. n = (((len & 3) == 0) ? n : n + 1);
  51. if (include_length) {
  52. result = (xxtea_long *)emalloc((n + 1) << 2);
  53. result[n] = len;
  54. *ret_len = n + 1;
  55. } else {
  56. result = (xxtea_long *)emalloc(n << 2);
  57. *ret_len = n;
  58. }
  59. memset(result, 0, n << 2);
  60. for (i = 0; i < len; i++) {
  61. result[i >> 2] |= (xxtea_long)data[i] << ((i & 3) << 3);
  62. }
  63. return result;
  64. }
  65. static unsigned char *xxtea_to_byte_array(xxtea_long *data, xxtea_long len, int include_length, xxtea_long *ret_len) {
  66. xxtea_long i, n, m;
  67. unsigned char *result;
  68. n = len << 2;
  69. if (include_length) {
  70. m = data[len - 1];
  71. if ((m < n - 7) || (m > n - 4)) return NULL;
  72. n = m;
  73. }
  74. result = (unsigned char *)emalloc(n + 1);
  75. for (i = 0; i < n; i++) {
  76. result[i] = (unsigned char)((data[i >> 2] >> ((i & 3) << 3)) & 0xff);
  77. }
  78. result[n] = '\0';
  79. *ret_len = n;
  80. return result;
  81. }
  82. static unsigned char *php_xxtea_encrypt(unsigned char *data, xxtea_long len, unsigned char *key, xxtea_long *ret_len) {
  83. unsigned char *result;
  84. xxtea_long *v, *k, v_len, k_len;
  85. v = xxtea_to_long_array(data, len, 1, &v_len);
  86. k = xxtea_to_long_array(key, 16, 0, &k_len);
  87. xxtea_long_encrypt(v, v_len, k);
  88. result = xxtea_to_byte_array(v, v_len, 0, ret_len);
  89. efree(v);
  90. efree(k);
  91. return result;
  92. }
  93. static unsigned char *php_xxtea_decrypt(unsigned char *data, xxtea_long len, unsigned char *key, xxtea_long *ret_len) {
  94. unsigned char *result;
  95. xxtea_long *v, *k, v_len, k_len;
  96. v = xxtea_to_long_array(data, len, 0, &v_len);
  97. k = xxtea_to_long_array(key, 16, 0, &k_len);
  98. xxtea_long_decrypt(v, v_len, k);
  99. result = xxtea_to_byte_array(v, v_len, 1, ret_len);
  100. efree(v);
  101. efree(k);
  102. return result;
  103. }
  104. /* {{{ proto string xxtea_encrypt(string data, string key)
  105. Encrypt string using XXTEA algorithm */
  106. ZEND_FUNCTION(xxtea_encrypt)
  107. {
  108. unsigned char *data, *key;
  109. unsigned char *result;
  110. xxtea_long data_len, key_len, ret_length;
  111. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &data, &data_len, &key, &key_len) == FAILURE) {
  112. return;
  113. }
  114. if (data_len == 0) RETVAL_STRINGL(NULL, 0, 0);
  115. if (key_len != 16) RETURN_FALSE;
  116. result = php_xxtea_encrypt(data, data_len, key, &ret_length);
  117. if (result != NULL) {
  118. RETVAL_STRINGL((char *)result, ret_length, 0);
  119. } else {
  120. RETURN_FALSE;
  121. }
  122. }
  123. /* }}} */
  124. /* {{{ proto string xxtea_decrypt(string data, string key)
  125. Decrypt string using XXTEA algorithm */
  126. ZEND_FUNCTION(xxtea_decrypt)
  127. {
  128. unsigned char *data, *key;
  129. unsigned char *result;
  130. xxtea_long data_len, key_len, ret_length;
  131. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &data, &data_len, &key, &key_len) == FAILURE) {
  132. return;
  133. }
  134. if (data_len == 0) RETVAL_STRINGL(NULL, 0, 0);
  135. if (key_len != 16) RETURN_FALSE;
  136. result = php_xxtea_decrypt(data, data_len, key, &ret_length);
  137. if (result != NULL) {
  138. RETVAL_STRINGL((char *)result, ret_length, 0);
  139. } else {
  140. RETURN_FALSE;
  141. }
  142. }
  143. /* }}} */
  144. ZEND_MINIT_FUNCTION(xxtea)
  145. {
  146. return SUCCESS;
  147. }
  148. ZEND_MSHUTDOWN_FUNCTION(xxtea)
  149. {
  150. return SUCCESS;
  151. }
  152. ZEND_MINFO_FUNCTION(xxtea)
  153. {
  154. php_info_print_table_start();
  155. php_info_print_table_row(2, "xxtea support", "enabled");
  156. php_info_print_table_row(2, "xxtea module version", XXTEA_VERSION);
  157. php_info_print_table_row(2, "xxtea author", XXTEA_AUTHOR);
  158. php_info_print_table_row(2, "xxtea homepage", XXTEA_HOMEPAGE);
  159. php_info_print_table_end();
  160. }
  161. ZEND_FUNCTION(xxtea_info)
  162. {
  163. array_init(return_value);
  164. add_assoc_string(return_value, "ext_version", XXTEA_VERSION, 1);
  165. add_assoc_string(return_value, "ext_build_date", XXTEA_BUILD_DATE, 1);
  166. add_assoc_string(return_value, "ext_author", XXTEA_AUTHOR, 1);
  167. add_assoc_string(return_value, "ext_homepage", XXTEA_HOMEPAGE, 1);
  168. }
  169. #endif /* if HAVE_XXTEA */