[rs-commit] r40 - in /redwax-tool/trunk: redwax_p11kit.c redwax_util.c redwax_util.h

rs-commit at redwax.eu rs-commit at redwax.eu
Thu Nov 18 22:26:26 CET 2021


Author: minfrin at redwax.eu
Date: Thu Nov 18 22:26:26 2021
New Revision: 40

Log:
Pull in encoding api.

Modified:
    redwax-tool/trunk/redwax_p11kit.c
    redwax-tool/trunk/redwax_util.c
    redwax-tool/trunk/redwax_util.h

Modified: redwax-tool/trunk/redwax_p11kit.c
==============================================================================
--- redwax-tool/trunk/redwax_p11kit.c	(original)
+++ redwax-tool/trunk/redwax_p11kit.c	Thu Nov 18 22:26:26 2021
@@ -29,7 +29,6 @@
  */
 
 #include <apr_crypto.h>
-#include <apr_encode.h>
 #include <apr_escape.h>
 #include <apr_lib.h>
 #include <apr_strings.h>
@@ -1420,8 +1419,8 @@
                     rt_run_normalise_key(r, key, 1);
 
                     redwax_print_error(r, "pkcs11-in: private key: %s\n",
-                            apr_pencode_base16_binary(pool, key->common.id_der,
-                                    key->common.id_len, APR_ENCODE_LOWER, NULL));
+                            redwax_pencode_base16_binary(pool, key->common.id_der,
+                                    key->common.id_len, REDWAX_ENCODE_LOWER, NULL));
 
                 }
 

Modified: redwax-tool/trunk/redwax_util.c
==============================================================================
--- redwax-tool/trunk/redwax_util.c	(original)
+++ redwax-tool/trunk/redwax_util.c	Thu Nov 18 22:26:26 2021
@@ -27,62 +27,62 @@
 int redwax_certcmp(redwax_certificate_t *cert1, redwax_certificate_t *cert2)
 {
 
-	return 0;
+    return 0;
 }
 
 apr_size_t redwax_strrcspn(const char *s, const char *charset)
 {
-	apr_size_t last = 0, cur;
-
-	cur = strcspn(s, charset);
-
-	while (s[cur++]) {
-		last = cur;
-		cur += strcspn(s + cur, charset);
-	}
-
-	return last;
+    apr_size_t last = 0, cur;
+
+    cur = strcspn(s, charset);
+
+    while (s[cur++]) {
+        last = cur;
+        cur += strcspn(s + cur, charset);
+    }
+
+    return last;
 }
 
 const char *redwax_stroff(const char *s, apr_size_t off)
 {
-	while(off-- && *s) {
-		s++;
-	}
-
-	return s;
+    while(off-- && *s) {
+        s++;
+    }
+
+    return s;
 }
 
 const char *redwax_pstrntrim(apr_pool_t *p, const char *s, apr_size_t off)
 {
-	const char *start, *end;
-	apr_size_t remain = off;
-
-	while (remain) {
-
-		if (*s == ' ') {
-			s++;
-			remain--;
-		}
-		else {
-			break;
-		}
-
-	}
-
-	start = end = s;
-
-	while (remain) {
-
-		if (*s != ' ') {
-			end = s + 1;
-		}
-
-		s++;
-		remain--;
-	}
-
-	return apr_pstrndup(p, start, end - start);
+    const char *start, *end;
+    apr_size_t remain = off;
+
+    while (remain) {
+
+        if (*s == ' ') {
+            s++;
+            remain--;
+        }
+        else {
+            break;
+        }
+
+    }
+
+    start = end = s;
+
+    while (remain) {
+
+        if (*s != ' ') {
+            end = s + 1;
+        }
+
+        s++;
+        remain--;
+    }
+
+    return apr_pstrndup(p, start, end - start);
 }
 
 const char *redwax_pescape_echo(apr_pool_t *p, const char *str,
@@ -113,20 +113,20 @@
              d = apr_palloc(p, strlen(str) + count + 1);
              str = d;
 
-    		 *d++ = '\"';
+             *d++ = '\"';
 
              for (; *s; ++s) {
-            	 if (*s == '\"') {
-            		 *d++ = '\\';
-            	 }
-            	 *d++ = *s;
+                 if (*s == '\"') {
+                     *d++ = '\\';
+                 }
+                 *d++ = *s;
              }
 
              *d = 0;
 
-        	 if (close) {
-        		 *d++ = '\"';
-        	 }
+             if (close) {
+                 *d++ = '\"';
+             }
 
          }
          else {
@@ -136,14 +136,14 @@
 
              /* count spaces */
              for (i = 0; str[i]; i++) {
-            	 switch (str[i]) {
-            	 case ' ':
-            	 case ';':
+                 switch (str[i]) {
+                 case ' ':
+                 case ';':
                      count++;
-            		 break;
-            	 default:
-            		 break;
-            	 }
+                     break;
+                 default:
+                     break;
+                 }
              }
 
              /* escape spaces */
@@ -157,14 +157,14 @@
                  str = d;
 
                  for (; *s; ++s) {
-                	 switch (*s) {
-                	 case ' ':
-                	 case ';':
+                     switch (*s) {
+                     case ' ':
+                     case ';':
                          *d++ = '\\';
-                		 break;
-                	 default:
-                		 break;
-                	 }
+                         break;
+                     default:
+                         break;
+                     }
                      *d++ = *s;
                  }
 
@@ -178,3 +178,76 @@
      return str;
 }
 
+static const char base16[] = "0123456789ABCDEF";
+static const char base16lower[] = "0123456789abcdef";
+
+apr_status_t redwax_encode_base16_binary(char *dest,
+    const unsigned char *src, apr_ssize_t slen, int flags, apr_size_t * len)
+{
+    const unsigned char *in = src;
+    apr_ssize_t size;
+
+    if (!src) {
+        return APR_NOTFOUND;
+    }
+
+    if (dest) {
+        register char *bufout = dest;
+        const char *base;
+
+        if ((flags & REDWAX_ENCODE_LOWER)) {
+            base = base16lower;
+        }
+        else {
+            base = base16;
+        }
+
+        for (size = 0; size < slen; size++) {
+            if ((flags & REDWAX_ENCODE_COLON) && size) {
+                *(bufout++) = ':';
+            }
+            *(bufout++) = base[in[size] >> 4];
+            *(bufout++) = base[in[size] & 0xf];
+        }
+
+        if (len) {
+            *len = bufout - dest;
+        }
+
+        *bufout = 0;
+
+        return APR_SUCCESS;
+    }
+
+    if (len) {
+        if ((flags & REDWAX_ENCODE_COLON) && slen) {
+            *len = slen * 3;
+        }
+        else {
+            *len = slen * 2 + 1;
+        }
+    }
+
+    return APR_SUCCESS;
+}
+
+const char *redwax_pencode_base16_binary(apr_pool_t * p,
+        const unsigned char *src, apr_ssize_t slen, int flags,
+        apr_size_t * len)
+{
+    apr_size_t size;
+
+    switch (redwax_encode_base16_binary(NULL, src, slen, flags, &size)) {
+    case APR_SUCCESS:{
+            char *cmd = apr_palloc(p, size);
+            redwax_encode_base16_binary(cmd, src, slen, flags, len);
+            return cmd;
+        }
+    case APR_NOTFOUND:{
+            break;
+        }
+    }
+
+    return NULL;
+}
+

Modified: redwax-tool/trunk/redwax_util.h
==============================================================================
--- redwax-tool/trunk/redwax_util.h	(original)
+++ redwax-tool/trunk/redwax_util.h	Thu Nov 18 22:26:26 2021
@@ -37,5 +37,25 @@
 const char *redwax_pescape_echo(apr_pool_t *p, const char *str,
         redwax_token_quoted_e quoted, int close);
 
+apr_status_t redwax_encode_base16_binary(char *dest, const unsigned char *src,
+        apr_ssize_t slen, int flags, apr_size_t *len);
+
+const char* redwax_pencode_base16_binary(apr_pool_t *p,
+        const unsigned char *src, apr_ssize_t slen, int flags, apr_size_t *len);
+
+/**
+ * Generate RFC4648 base16/base32/base64.
+ */
+#define REDWAX_ENCODE_NONE 0
+
+/**
+ * Generate base16 with colons between each token.
+ */
+#define REDWAX_ENCODE_COLON 16
+
+/**
+ * Generate base16 with lower case characters.
+ */
+#define REDWAX_ENCODE_LOWER 32
 
 #endif



More information about the rs-commit mailing list