THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

125 lines
3.4KB

  1. #include "cache.h"
  2. const signed char hexval_table[256] = {
  3. -1, -1, -1, -1, -1, -1, -1, -1, /* 00-07 */
  4. -1, -1, -1, -1, -1, -1, -1, -1, /* 08-0f */
  5. -1, -1, -1, -1, -1, -1, -1, -1, /* 10-17 */
  6. -1, -1, -1, -1, -1, -1, -1, -1, /* 18-1f */
  7. -1, -1, -1, -1, -1, -1, -1, -1, /* 20-27 */
  8. -1, -1, -1, -1, -1, -1, -1, -1, /* 28-2f */
  9. 0, 1, 2, 3, 4, 5, 6, 7, /* 30-37 */
  10. 8, 9, -1, -1, -1, -1, -1, -1, /* 38-3f */
  11. -1, 10, 11, 12, 13, 14, 15, -1, /* 40-47 */
  12. -1, -1, -1, -1, -1, -1, -1, -1, /* 48-4f */
  13. -1, -1, -1, -1, -1, -1, -1, -1, /* 50-57 */
  14. -1, -1, -1, -1, -1, -1, -1, -1, /* 58-5f */
  15. -1, 10, 11, 12, 13, 14, 15, -1, /* 60-67 */
  16. -1, -1, -1, -1, -1, -1, -1, -1, /* 68-67 */
  17. -1, -1, -1, -1, -1, -1, -1, -1, /* 70-77 */
  18. -1, -1, -1, -1, -1, -1, -1, -1, /* 78-7f */
  19. -1, -1, -1, -1, -1, -1, -1, -1, /* 80-87 */
  20. -1, -1, -1, -1, -1, -1, -1, -1, /* 88-8f */
  21. -1, -1, -1, -1, -1, -1, -1, -1, /* 90-97 */
  22. -1, -1, -1, -1, -1, -1, -1, -1, /* 98-9f */
  23. -1, -1, -1, -1, -1, -1, -1, -1, /* a0-a7 */
  24. -1, -1, -1, -1, -1, -1, -1, -1, /* a8-af */
  25. -1, -1, -1, -1, -1, -1, -1, -1, /* b0-b7 */
  26. -1, -1, -1, -1, -1, -1, -1, -1, /* b8-bf */
  27. -1, -1, -1, -1, -1, -1, -1, -1, /* c0-c7 */
  28. -1, -1, -1, -1, -1, -1, -1, -1, /* c8-cf */
  29. -1, -1, -1, -1, -1, -1, -1, -1, /* d0-d7 */
  30. -1, -1, -1, -1, -1, -1, -1, -1, /* d8-df */
  31. -1, -1, -1, -1, -1, -1, -1, -1, /* e0-e7 */
  32. -1, -1, -1, -1, -1, -1, -1, -1, /* e8-ef */
  33. -1, -1, -1, -1, -1, -1, -1, -1, /* f0-f7 */
  34. -1, -1, -1, -1, -1, -1, -1, -1, /* f8-ff */
  35. };
  36. int hex_to_bytes(unsigned char *binary, const char *hex, size_t len)
  37. {
  38. for (; len; len--, hex += 2) {
  39. unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);
  40. if (val & ~0xff)
  41. return -1;
  42. *binary++ = val;
  43. }
  44. return 0;
  45. }
  46. int get_sha1_hex(const char *hex, unsigned char *sha1)
  47. {
  48. int i;
  49. for (i = 0; i < the_hash_algo->rawsz; i++) {
  50. int val = hex2chr(hex);
  51. if (val < 0)
  52. return -1;
  53. *sha1++ = val;
  54. hex += 2;
  55. }
  56. return 0;
  57. }
  58. int get_oid_hex(const char *hex, struct object_id *oid)
  59. {
  60. return get_sha1_hex(hex, oid->hash);
  61. }
  62. int parse_oid_hex(const char *hex, struct object_id *oid, const char **end)
  63. {
  64. int ret = get_oid_hex(hex, oid);
  65. if (!ret)
  66. *end = hex + the_hash_algo->hexsz;
  67. return ret;
  68. }
  69. char *hash_to_hex_algop_r(char *buffer, const unsigned char *hash,
  70. const struct git_hash_algo *algop)
  71. {
  72. static const char hex[] = "0123456789abcdef";
  73. char *buf = buffer;
  74. int i;
  75. for (i = 0; i < algop->rawsz; i++) {
  76. unsigned int val = *hash++;
  77. *buf++ = hex[val >> 4];
  78. *buf++ = hex[val & 0xf];
  79. }
  80. *buf = '\0';
  81. return buffer;
  82. }
  83. char *sha1_to_hex_r(char *buffer, const unsigned char *sha1)
  84. {
  85. return hash_to_hex_algop_r(buffer, sha1, &hash_algos[GIT_HASH_SHA1]);
  86. }
  87. char *oid_to_hex_r(char *buffer, const struct object_id *oid)
  88. {
  89. return hash_to_hex_algop_r(buffer, oid->hash, the_hash_algo);
  90. }
  91. char *hash_to_hex_algop(const unsigned char *hash, const struct git_hash_algo *algop)
  92. {
  93. static int bufno;
  94. static char hexbuffer[4][GIT_MAX_HEXSZ + 1];
  95. bufno = (bufno + 1) % ARRAY_SIZE(hexbuffer);
  96. return hash_to_hex_algop_r(hexbuffer[bufno], hash, algop);
  97. }
  98. char *sha1_to_hex(const unsigned char *sha1)
  99. {
  100. return hash_to_hex_algop(sha1, &hash_algos[GIT_HASH_SHA1]);
  101. }
  102. char *hash_to_hex(const unsigned char *hash)
  103. {
  104. return hash_to_hex_algop(hash, the_hash_algo);
  105. }
  106. char *oid_to_hex(const struct object_id *oid)
  107. {
  108. return hash_to_hex_algop(oid->hash, the_hash_algo);
  109. }