mirror of
https://github.com/facebook/sapling.git
synced 2024-10-11 01:07:15 +03:00
2f5e85ccb8
Test Plan: make local && build in clion Reviewers: #fastmanifest, durham Reviewed By: durham Subscribers: mitrandir, mjpieters Differential Revision: https://phabricator.intern.facebook.com/D3780622 Signature: t1:3780622:1472250774:fa702d78dab39eb74c8de58c0d589ae36a2c42b3
76 lines
2.3 KiB
C
76 lines
2.3 KiB
C
// Copyright 2016-present Facebook. All Rights Reserved.
|
|
//
|
|
// convert.h: hex-string conversions
|
|
//
|
|
// no-check-code
|
|
|
|
#ifndef __FASTMANIFEST_CONVERT_H__
|
|
#define __FASTMANIFEST_CONVERT_H__
|
|
|
|
#include <stdbool.h>
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
|
|
static int8_t hextable[256] = {
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /* 0-9 */
|
|
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* A-F */
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* a-f */
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
|
|
};
|
|
|
|
static char chartable[16] = {
|
|
'0', '1', '2', '3', '4', '5', '6', '7',
|
|
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
|
|
};
|
|
|
|
/*
|
|
* Turn a hex-encoded string into binary. Returns false on failure.
|
|
*/
|
|
static inline bool unhexlify(const char *input, int len, uint8_t *dst) {
|
|
if (len % 2 != 0) {
|
|
// wtf.
|
|
return false;
|
|
}
|
|
|
|
for (size_t ix = 0; ix < len; ix += 2, dst++) {
|
|
int hi = hextable[(unsigned char) input[ix]];
|
|
int lo = hextable[(unsigned char) input[ix + 1]];
|
|
|
|
if (hi < 0 || lo < 0) {
|
|
return false;
|
|
}
|
|
*dst = (hi << 4) | lo;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/*
|
|
* Turn binary data into a hex-encoded string.
|
|
*/
|
|
static inline void hexlify(const uint8_t *input, int len, char *dst) {
|
|
for (size_t ix = 0; ix < len; ix++, dst += 2) {
|
|
unsigned char ch = (unsigned char) input[ix];
|
|
char hi = chartable[ch >> 4];
|
|
char lo = chartable[ch & 0xf];
|
|
|
|
*dst = hi;
|
|
*(dst + 1) = lo;
|
|
}
|
|
}
|
|
|
|
|
|
#endif /* #ifndef __FASTMANIFEST_CONVERT_H__ */
|