瀏覽代碼

Add macros for baseXX encoding lengths

Taylor Yu 7 年之前
父節點
當前提交
e7f40baade
共有 1 個文件被更改,包括 20 次插入0 次删除
  1. 20 0
      src/common/util_format.h

+ 20 - 0
src/common/util_format.h

@@ -10,6 +10,26 @@
 #include "testsupport.h"
 #include "torint.h"
 
+/** @{ */
+/** These macros don't check for overflow.  Use them only for constant inputs
+ * (like array declarations).  The *_LEN macros are the raw encoding lengths
+ * (without terminating NUL), while the *_BUFSIZE macros count the terminating
+ * NUL. */
+#define BASE64_LEN(n) (CEIL_DIV((n), 3) * 4)
+#define BASE32_LEN(n) (CEIL_DIV((n), 5) * 8)
+#define BASE16_LEN(n) ((n) * 2)
+
+#define BASE64_BUFSIZE(n) (BASE64_LEN(n) + 1)
+#define BASE32_BUFSIZE(n) (BASE32_LEN(n) + 1)
+#define BASE16_BUFSIZE(n) (BASE16_LEN(n) + 1)
+
+#define BASE64_NOPAD_LEN(n) (CEIL_DIV((n) * 4, 3)
+#define BASE32_NOPAD_LEN(n) (CEIL_DIV((n) * 8, 5)
+
+#define BASE64_NOPAD_BUFSIZE(n) (BASE64_NOPAD_LEN(n) + 1))
+#define BASE32_NOPAD_BUFSIZE(n) (BASE32_NOPAD_LEN(n) + 1))
+/** @} */
+
 #define BASE64_ENCODE_MULTILINE 1
 size_t base64_encode_size(size_t srclen, int flags);
 int base64_encode(char *dest, size_t destlen, const char *src, size_t srclen,