Merge branch 'ew/hash-with-openssl-evp'
[git.git] / hex.h
blob87abf6660214151a50f45197572b6749c10944cc
1 #ifndef HEX_H
2 #define HEX_H
4 #include "hash-ll.h"
6 extern const signed char hexval_table[256];
7 static inline unsigned int hexval(unsigned char c)
9 return hexval_table[c];
13 * Convert two consecutive hexadecimal digits into a char. Return a
14 * negative value on error. Don't run over the end of short strings.
16 static inline int hex2chr(const char *s)
18 unsigned int val = hexval(s[0]);
19 return (val & ~0xf) ? val : (val << 4) | hexval(s[1]);
23 * Try to read a hash (specified by the_hash_algo) in hexadecimal
24 * format from the 40 (or whatever length the hash algorithm uses)
25 * characters starting at hex. Write the 20-byte (or the length of
26 * the hash) result to hash in binary form.
27 * Return 0 on success. Reading stops if a NUL is encountered in the
28 * input, so it is safe to pass this function an arbitrary
29 * null-terminated string.
31 int get_hash_hex(const char *hex, unsigned char *hash);
32 int get_oid_hex(const char *hex, struct object_id *oid);
34 /* Like get_oid_hex, but for an arbitrary hash algorithm. */
35 int get_oid_hex_algop(const char *hex, struct object_id *oid, const struct git_hash_algo *algop);
38 * Read `len` pairs of hexadecimal digits from `hex` and write the
39 * values to `binary` as `len` bytes. Return 0 on success, or -1 if
40 * the input does not consist of hex digits).
42 int hex_to_bytes(unsigned char *binary, const char *hex, size_t len);
45 * Convert a binary hash in "unsigned char []" or an object name in
46 * "struct object_id *" to its hex equivalent. The `_r` variant is reentrant,
47 * and writes the NUL-terminated output to the buffer `out`, which must be at
48 * least `GIT_MAX_HEXSZ + 1` bytes, and returns a pointer to out for
49 * convenience.
51 * The non-`_r` variant returns a static buffer, but uses a ring of 4
52 * buffers, making it safe to make multiple calls for a single statement, like:
54 * printf("%s -> %s", hash_to_hex(one), hash_to_hex(two));
55 * printf("%s -> %s", oid_to_hex(one), oid_to_hex(two));
57 char *hash_to_hex_algop_r(char *buffer, const unsigned char *hash, const struct git_hash_algo *);
58 char *oid_to_hex_r(char *out, const struct object_id *oid);
59 char *hash_to_hex_algop(const unsigned char *hash, const struct git_hash_algo *); /* static buffer result! */
60 char *hash_to_hex(const unsigned char *hash); /* same static buffer */
61 char *oid_to_hex(const struct object_id *oid); /* same static buffer */
64 * Parse a 40-character hexadecimal object ID starting from hex, updating the
65 * pointer specified by end when parsing stops. The resulting object ID is
66 * stored in oid. Returns 0 on success. Parsing will stop on the first NUL or
67 * other invalid character. end is only updated on success; otherwise, it is
68 * unmodified.
70 int parse_oid_hex(const char *hex, struct object_id *oid, const char **end);
72 /* Like parse_oid_hex, but for an arbitrary hash algorithm. */
73 int parse_oid_hex_algop(const char *hex, struct object_id *oid, const char **end,
74 const struct git_hash_algo *algo);
78 * These functions work like get_oid_hex and parse_oid_hex, but they will parse
79 * a hex value for any algorithm. The algorithm is detected based on the length
80 * and the algorithm in use is returned. If this is not a hex object ID in any
81 * algorithm, returns GIT_HASH_UNKNOWN.
83 int get_oid_hex_any(const char *hex, struct object_id *oid);
84 int parse_oid_hex_any(const char *hex, struct object_id *oid, const char **end);
86 #endif