zend_system_id.c 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Authors: Sammy Kaye Powers <sammyk@php.net> |
  14. | Dmitry Stogov <dmitry@php.net> |
  15. +----------------------------------------------------------------------+
  16. */
  17. #include "php.h"
  18. #include "zend_system_id.h"
  19. #include "zend_extensions.h"
  20. #include "ext/standard/md5.h"
  21. #include "ext/hash/php_hash.h"
  22. ZEND_API char zend_system_id[32];
  23. static PHP_MD5_CTX context;
  24. static int finalized = 0;
  25. ZEND_API ZEND_RESULT_CODE zend_add_system_entropy(const char *module_name, const char *hook_name, const void *data, size_t size)
  26. {
  27. if (finalized == 0) {
  28. PHP_MD5Update(&context, module_name, strlen(module_name));
  29. PHP_MD5Update(&context, hook_name, strlen(hook_name));
  30. if (size) {
  31. PHP_MD5Update(&context, data, size);
  32. }
  33. return SUCCESS;
  34. }
  35. return FAILURE;
  36. }
  37. #define ZEND_BIN_ID "BIN_" ZEND_TOSTR(SIZEOF_INT) ZEND_TOSTR(SIZEOF_LONG) ZEND_TOSTR(SIZEOF_SIZE_T) ZEND_TOSTR(SIZEOF_ZEND_LONG) ZEND_TOSTR(ZEND_MM_ALIGNMENT)
  38. void zend_startup_system_id(void)
  39. {
  40. PHP_MD5Init(&context);
  41. PHP_MD5Update(&context, PHP_VERSION, sizeof(PHP_VERSION)-1);
  42. PHP_MD5Update(&context, ZEND_EXTENSION_BUILD_ID, sizeof(ZEND_EXTENSION_BUILD_ID)-1);
  43. PHP_MD5Update(&context, ZEND_BIN_ID, sizeof(ZEND_BIN_ID)-1);
  44. if (strstr(PHP_VERSION, "-dev") != 0) {
  45. /* Development versions may be changed from build to build */
  46. PHP_MD5Update(&context, __DATE__, sizeof(__DATE__)-1);
  47. PHP_MD5Update(&context, __TIME__, sizeof(__TIME__)-1);
  48. }
  49. zend_system_id[0] = '\0';
  50. }
  51. #define ZEND_HOOK_AST_PROCESS (1 << 0)
  52. #define ZEND_HOOK_COMPILE_FILE (1 << 1)
  53. #define ZEND_HOOK_EXECUTE_EX (1 << 2)
  54. #define ZEND_HOOK_EXECUTE_INTERNAL (1 << 3)
  55. void zend_finalize_system_id(void)
  56. {
  57. unsigned char digest[16];
  58. zend_uchar hooks = 0;
  59. if (zend_ast_process) {
  60. hooks |= ZEND_HOOK_AST_PROCESS;
  61. }
  62. if (zend_compile_file != compile_file) {
  63. hooks |= ZEND_HOOK_COMPILE_FILE;
  64. }
  65. if (zend_execute_ex != execute_ex) {
  66. hooks |= ZEND_HOOK_EXECUTE_EX;
  67. }
  68. if (zend_execute_internal) {
  69. hooks |= ZEND_HOOK_EXECUTE_INTERNAL;
  70. }
  71. PHP_MD5Update(&context, &hooks, sizeof hooks);
  72. for (int16_t i = 0; i < 256; i++) {
  73. if (zend_get_user_opcode_handler((zend_uchar) i) != NULL) {
  74. PHP_MD5Update(&context, &i, sizeof i);
  75. }
  76. }
  77. PHP_MD5Final(digest, &context);
  78. php_hash_bin2hex(zend_system_id, digest, sizeof digest);
  79. finalized = 1;
  80. }