diff mbox

[1/25] Remove nested functions: crypt/md5-crypt.c

Message ID CAGQ9bdwGB0mM4HnCdsqPVF9Ott8MsGdCmJvNSUQiXOx5Rxd92w@mail.gmail.com
State Committed
Headers show

Commit Message

Kostya Serebryany May 21, 2014, 12:39 p.m. UTC
Here it is:

2014-05-21  Kostya Serebryany  <konstantin.s.serebryany@gmail.com>

        * crypt/crypt-private.h: Declare __b64_from_24bit, include
ufc-crypt.h.
        * crypt/crypt_util.c(__b64_from_24bit): New function.
        (b64t): New static const variable.
        * crypt/md5-crypt.c (b64_from_24bit): Remove function.
        (b64t): Remove variable.
        (__md5_crypt_r): Replace b64_from_24bit with __b64_from_24bit.
        * crypt/sha256-crypt.c: Include crypt-private.h.
        (b64t): Remove variable.
        (__sha256_crypt_r): Remove b64_from_24bit and replace
        with __b64_from_24bit.
        * crypt/sha512-crypt.c: Likewise.

--kcc


On Wed, May 21, 2014 at 3:56 PM, Siddhesh Poyarekar <siddhesh@redhat.com> wrote:
> On Wed, May 21, 2014 at 03:48:32PM +0400, Konstantin Serebryany wrote:
>> Let me prepare another patch then.
>
> Let this patch go in as is.  Move the function out with the patch to
> sha256-crypt.c or whatever you're changing next, assuming that this
> patch is in.
>
>> Does __b64_from_24bit declared in crypt-private.h and defined in
>> crypt_util.c sound good?
>
> Yes.
>
> Siddhesh

Comments

Siddhesh Poyarekar May 22, 2014, 3:29 a.m. UTC | #1
On Wed, May 21, 2014 at 04:39:22PM +0400, Konstantin Serebryany wrote:
> Here it is:
> 
> 2014-05-21  Kostya Serebryany  <konstantin.s.serebryany@gmail.com>
> 
>         * crypt/crypt-private.h: Declare __b64_from_24bit, include
> ufc-crypt.h.

This should be:

	* crypt/crypt-private.h: Include ufc-crypt.h.
	(__b64_from_24bit): Declare extern.

>         * crypt/crypt_util.c(__b64_from_24bit): New function.
>         (b64t): New static const variable.
>         * crypt/md5-crypt.c (b64_from_24bit): Remove function.
>         (b64t): Remove variable.
>         (__md5_crypt_r): Replace b64_from_24bit with __b64_from_24bit.
>         * crypt/sha256-crypt.c: Include crypt-private.h.
>         (b64t): Remove variable.
>         (__sha256_crypt_r): Remove b64_from_24bit and replace
>         with __b64_from_24bit.
>         * crypt/sha512-crypt.c: Likewise.
> 
> --kcc
> 
> 
> On Wed, May 21, 2014 at 3:56 PM, Siddhesh Poyarekar <siddhesh@redhat.com> wrote:
> > On Wed, May 21, 2014 at 03:48:32PM +0400, Konstantin Serebryany wrote:
> >> Let me prepare another patch then.
> >
> > Let this patch go in as is.  Move the function out with the patch to
> > sha256-crypt.c or whatever you're changing next, assuming that this
> > patch is in.
> >
> >> Does __b64_from_24bit declared in crypt-private.h and defined in
> >> crypt_util.c sound good?
> >
> > Yes.
> >
> > Siddhesh

> diff --git a/crypt/crypt-private.h b/crypt/crypt-private.h
> index fbaf9c4..8245087 100644
> --- a/crypt/crypt-private.h
> +++ b/crypt/crypt-private.h
> @@ -28,6 +28,8 @@
>  #include <features.h>
>  #include <stdbool.h>
>  
> +#include "ufc-crypt.h"
> +
>  /* crypt.c */
>  extern void _ufc_doit_r (ufc_long itr, struct crypt_data * __restrict __data,
>  			 ufc_long *res);
> @@ -57,4 +59,8 @@ extern char *__crypt_r (const char *__key, const char *__salt,
>  			     struct crypt_data * __restrict __data);
>  extern char *fcrypt (const char *key, const char *salt);
>  
> +extern void __b64_from_24bit (char **cp, int *buflen,
> +                              unsigned int b2, unsigned int b1, unsigned int b0,
> +                              int n);
> +

8 spaces should be collapsed into tabs.  I didn't notice this in the
previous patch until I applied it today.  So you'll also have to
update this patch to apply correctly on top of my corrections, which
I'll push after noon UTC today.

>  #endif  /* crypt-private.h */
> diff --git a/crypt/crypt_util.c b/crypt/crypt_util.c
> index 2875931..1b7aaf1 100644
> --- a/crypt/crypt_util.c
> +++ b/crypt/crypt_util.c
> @@ -253,6 +253,10 @@ static ufc_long eperm32tab[4][256][2];
>   */
>  static ufc_long efp[16][64][2];
>  
> +/* Table with characters for base64 transformation.  */
> +static const char b64t[64] =
> +"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
> +
>  /*
>   * For use by the old, non-reentrant routines
>   * (crypt/encrypt/setkey)
> @@ -956,3 +960,17 @@ setkey(__key)
>  {
>    __setkey_r(__key, &_ufc_foobar);
>  }
> +
> +void
> +__b64_from_24bit (char **cp, int *buflen,
> +                  unsigned int b2, unsigned int b1, unsigned int b0,
> +                  int n)

Likewise here and all cases below.

> +{
> +  unsigned int w = (b2 << 16) | (b1 << 8) | b0;
> +  while (n-- > 0 && (*buflen) > 0)
> +    {
> +      *(*cp)++ = b64t[w & 0x3f];
> +      --(*buflen);
> +      w >>= 6;
> +    }
> +}
> diff --git a/crypt/md5-crypt.c b/crypt/md5-crypt.c
> index 5d5fc55..4d47380 100644
> --- a/crypt/md5-crypt.c
> +++ b/crypt/md5-crypt.c
> @@ -25,6 +25,7 @@
>  #include <sys/param.h>
>  
>  #include "md5.h"
> +#include "crypt-private.h"
>  
>  
>  #ifdef USE_NSS
> @@ -78,30 +79,12 @@ typedef int PRBool;
>     encryption implementations.  */
>  static const char md5_salt_prefix[] = "$1$";
>  
> -/* Table with characters for base64 transformation.  */
> -static const char b64t[64] =
> -"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
> -
>  
>  /* Prototypes for local functions.  */
>  extern char *__md5_crypt_r (const char *key, const char *salt,
>  			    char *buffer, int buflen);
>  extern char *__md5_crypt (const char *key, const char *salt);
>  
> -static void
> -b64_from_24bit (char **cp, int *buflen,
> -                unsigned int b2, unsigned int b1, unsigned int b0,
> -                int n)
> -{
> -  unsigned int w = (b2 << 16) | (b1 << 8) | b0;
> -  while (n-- > 0 && *buflen > 0)
> -    {
> -      *(*cp)++ = b64t[w & 0x3f];
> -      --*buflen;
> -      w >>= 6;
> -    }
> -}
> -
>  
>  /* This entry point is equivalent to the `crypt' function in Unix
>     libcs.  */
> @@ -282,18 +265,18 @@ __md5_crypt_r (key, salt, buffer, buflen)
>        --buflen;
>      }
>  
> -  b64_from_24bit (&cp, &buflen,
> -                  alt_result[0], alt_result[6], alt_result[12], 4);
> -  b64_from_24bit (&cp, &buflen,
> -                  alt_result[1], alt_result[7], alt_result[13], 4);
> -  b64_from_24bit (&cp, &buflen,
> -                  alt_result[2], alt_result[8], alt_result[14], 4);
> -  b64_from_24bit (&cp, &buflen,
> -                  alt_result[3], alt_result[9], alt_result[15], 4);
> -  b64_from_24bit (&cp, &buflen,
> -                  alt_result[4], alt_result[10], alt_result[5], 4);
> -  b64_from_24bit (&cp, &buflen,
> -                  0, 0, alt_result[11], 2);
> +  __b64_from_24bit (&cp, &buflen,
> +                    alt_result[0], alt_result[6], alt_result[12], 4);
> +  __b64_from_24bit (&cp, &buflen,
> +                    alt_result[1], alt_result[7], alt_result[13], 4);
> +  __b64_from_24bit (&cp, &buflen,
> +                    alt_result[2], alt_result[8], alt_result[14], 4);
> +  __b64_from_24bit (&cp, &buflen,
> +                    alt_result[3], alt_result[9], alt_result[15], 4);
> +  __b64_from_24bit (&cp, &buflen,
> +                    alt_result[4], alt_result[10], alt_result[5], 4);
> +  __b64_from_24bit (&cp, &buflen,
> +                    0, 0, alt_result[11], 2);
>    if (buflen <= 0)
>      {
>        __set_errno (ERANGE);
> diff --git a/crypt/sha256-crypt.c b/crypt/sha256-crypt.c
> index cf0abd2..17279dd 100644
> --- a/crypt/sha256-crypt.c
> +++ b/crypt/sha256-crypt.c
> @@ -26,6 +26,7 @@
>  #include <sys/param.h>
>  
>  #include "sha256.h"
> +#include "crypt-private.h"
>  
>  
>  #ifdef USE_NSS
> @@ -90,10 +91,6 @@ static const char sha256_rounds_prefix[] = "rounds=";
>  /* Maximum number of rounds.  */
>  #define ROUNDS_MAX 999999999
>  
> -/* Table with characters for base64 transformation.  */
> -static const char b64t[64] =
> -"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
> -
>  
>  /* Prototypes for local functions.  */
>  extern char *__sha256_crypt_r (const char *key, const char *salt,
> @@ -341,29 +338,28 @@ __sha256_crypt_r (key, salt, buffer, buflen)
>        --buflen;
>      }
>  
> -  void b64_from_24bit (unsigned int b2, unsigned int b1, unsigned int b0,
> -		       int n)
> -  {
> -    unsigned int w = (b2 << 16) | (b1 << 8) | b0;
> -    while (n-- > 0 && buflen > 0)
> -      {
> -	*cp++ = b64t[w & 0x3f];
> -	--buflen;
> -	w >>= 6;
> -      }
> -  }
> -
> -  b64_from_24bit (alt_result[0], alt_result[10], alt_result[20], 4);
> -  b64_from_24bit (alt_result[21], alt_result[1], alt_result[11], 4);
> -  b64_from_24bit (alt_result[12], alt_result[22], alt_result[2], 4);
> -  b64_from_24bit (alt_result[3], alt_result[13], alt_result[23], 4);
> -  b64_from_24bit (alt_result[24], alt_result[4], alt_result[14], 4);
> -  b64_from_24bit (alt_result[15], alt_result[25], alt_result[5], 4);
> -  b64_from_24bit (alt_result[6], alt_result[16], alt_result[26], 4);
> -  b64_from_24bit (alt_result[27], alt_result[7], alt_result[17], 4);
> -  b64_from_24bit (alt_result[18], alt_result[28], alt_result[8], 4);
> -  b64_from_24bit (alt_result[9], alt_result[19], alt_result[29], 4);
> -  b64_from_24bit (0, alt_result[31], alt_result[30], 3);
> +  __b64_from_24bit (&cp, &buflen,
> +                    alt_result[0], alt_result[10], alt_result[20], 4);
> +  __b64_from_24bit (&cp, &buflen,
> +                    alt_result[21], alt_result[1], alt_result[11], 4);
> +  __b64_from_24bit (&cp, &buflen,
> +                    alt_result[12], alt_result[22], alt_result[2], 4);
> +  __b64_from_24bit (&cp, &buflen,
> +                    alt_result[3], alt_result[13], alt_result[23], 4);
> +  __b64_from_24bit (&cp, &buflen,
> +                    alt_result[24], alt_result[4], alt_result[14], 4);
> +  __b64_from_24bit (&cp, &buflen,
> +                    alt_result[15], alt_result[25], alt_result[5], 4);
> +  __b64_from_24bit (&cp, &buflen,
> +                    alt_result[6], alt_result[16], alt_result[26], 4);
> +  __b64_from_24bit (&cp, &buflen,
> +                    alt_result[27], alt_result[7], alt_result[17], 4);
> +  __b64_from_24bit (&cp, &buflen,
> +                    alt_result[18], alt_result[28], alt_result[8], 4);
> +  __b64_from_24bit (&cp, &buflen,
> +                    alt_result[9], alt_result[19], alt_result[29], 4);
> +  __b64_from_24bit (&cp, &buflen,
> +                    0, alt_result[31], alt_result[30], 3);
>    if (buflen <= 0)
>      {
>        __set_errno (ERANGE);
> diff --git a/crypt/sha512-crypt.c b/crypt/sha512-crypt.c
> index c0338a7..e05c4ac 100644
> --- a/crypt/sha512-crypt.c
> +++ b/crypt/sha512-crypt.c
> @@ -26,6 +26,7 @@
>  #include <sys/param.h>
>  
>  #include "sha512.h"
> +#include "crypt-private.h"
>  
>  
>  #ifdef USE_NSS
> @@ -90,10 +91,6 @@ static const char sha512_rounds_prefix[] = "rounds=";
>  /* Maximum number of rounds.  */
>  #define ROUNDS_MAX 999999999
>  
> -/* Table with characters for base64 transformation.  */
> -static const char b64t[64] =
> -"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
> -
>  
>  /* Prototypes for local functions.  */
>  extern char *__sha512_crypt_r (const char *key, const char *salt,
> @@ -340,40 +337,50 @@ __sha512_crypt_r (key, salt, buffer, buflen)
>        --buflen;
>      }
>  
> -  void b64_from_24bit (unsigned int b2, unsigned int b1, unsigned int b0,
> -		       int n)
> -  {
> -    unsigned int w = (b2 << 16) | (b1 << 8) | b0;
> -    while (n-- > 0 && buflen > 0)
> -      {
> -	*cp++ = b64t[w & 0x3f];
> -	--buflen;
> -	w >>= 6;
> -      }
> -  }
> -
> -  b64_from_24bit (alt_result[0], alt_result[21], alt_result[42], 4);
> -  b64_from_24bit (alt_result[22], alt_result[43], alt_result[1], 4);
> -  b64_from_24bit (alt_result[44], alt_result[2], alt_result[23], 4);
> -  b64_from_24bit (alt_result[3], alt_result[24], alt_result[45], 4);
> -  b64_from_24bit (alt_result[25], alt_result[46], alt_result[4], 4);
> -  b64_from_24bit (alt_result[47], alt_result[5], alt_result[26], 4);
> -  b64_from_24bit (alt_result[6], alt_result[27], alt_result[48], 4);
> -  b64_from_24bit (alt_result[28], alt_result[49], alt_result[7], 4);
> -  b64_from_24bit (alt_result[50], alt_result[8], alt_result[29], 4);
> -  b64_from_24bit (alt_result[9], alt_result[30], alt_result[51], 4);
> -  b64_from_24bit (alt_result[31], alt_result[52], alt_result[10], 4);
> -  b64_from_24bit (alt_result[53], alt_result[11], alt_result[32], 4);
> -  b64_from_24bit (alt_result[12], alt_result[33], alt_result[54], 4);
> -  b64_from_24bit (alt_result[34], alt_result[55], alt_result[13], 4);
> -  b64_from_24bit (alt_result[56], alt_result[14], alt_result[35], 4);
> -  b64_from_24bit (alt_result[15], alt_result[36], alt_result[57], 4);
> -  b64_from_24bit (alt_result[37], alt_result[58], alt_result[16], 4);
> -  b64_from_24bit (alt_result[59], alt_result[17], alt_result[38], 4);
> -  b64_from_24bit (alt_result[18], alt_result[39], alt_result[60], 4);
> -  b64_from_24bit (alt_result[40], alt_result[61], alt_result[19], 4);
> -  b64_from_24bit (alt_result[62], alt_result[20], alt_result[41], 4);
> -  b64_from_24bit (0, 0, alt_result[63], 2);
> +  __b64_from_24bit (&cp, &buflen,
> +                    alt_result[0], alt_result[21], alt_result[42], 4);
> +  __b64_from_24bit (&cp, &buflen,
> +                    alt_result[22], alt_result[43], alt_result[1], 4);
> +  __b64_from_24bit (&cp, &buflen,
> +                    alt_result[44], alt_result[2], alt_result[23], 4);
> +  __b64_from_24bit (&cp, &buflen,
> +                    alt_result[3], alt_result[24], alt_result[45], 4);
> +  __b64_from_24bit (&cp, &buflen,
> +                    alt_result[25], alt_result[46], alt_result[4], 4);
> +  __b64_from_24bit (&cp, &buflen,
> +                    alt_result[47], alt_result[5], alt_result[26], 4);
> +  __b64_from_24bit (&cp, &buflen,
> +                    alt_result[6], alt_result[27], alt_result[48], 4);
> +  __b64_from_24bit (&cp, &buflen,
> +                    alt_result[28], alt_result[49], alt_result[7], 4);
> +  __b64_from_24bit (&cp, &buflen,
> +                    alt_result[50], alt_result[8], alt_result[29], 4);
> +  __b64_from_24bit (&cp, &buflen,
> +                    alt_result[9], alt_result[30], alt_result[51], 4);
> +  __b64_from_24bit (&cp, &buflen,
> +                    alt_result[31], alt_result[52], alt_result[10], 4);
> +  __b64_from_24bit (&cp, &buflen,
> +                    alt_result[53], alt_result[11], alt_result[32], 4);
> +  __b64_from_24bit (&cp, &buflen,
> +                    alt_result[12], alt_result[33], alt_result[54], 4);
> +  __b64_from_24bit (&cp, &buflen,
> +                    alt_result[34], alt_result[55], alt_result[13], 4);
> +  __b64_from_24bit (&cp, &buflen,
> +                    alt_result[56], alt_result[14], alt_result[35], 4);
> +  __b64_from_24bit (&cp, &buflen,
> +                    alt_result[15], alt_result[36], alt_result[57], 4);
> +  __b64_from_24bit (&cp, &buflen,
> +                    alt_result[37], alt_result[58], alt_result[16], 4);
> +  __b64_from_24bit (&cp, &buflen,
> +                    alt_result[59], alt_result[17], alt_result[38], 4);
> +  __b64_from_24bit (&cp, &buflen,
> +                    alt_result[18], alt_result[39], alt_result[60], 4);
> +  __b64_from_24bit (&cp, &buflen,
> +                    alt_result[40], alt_result[61], alt_result[19], 4);
> +  __b64_from_24bit (&cp, &buflen,
> +                    alt_result[62], alt_result[20], alt_result[41], 4);
> +  __b64_from_24bit (&cp, &buflen,
> +                    0, 0, alt_result[63], 2);
>  
>    if (buflen <= 0)
>      {
diff mbox

Patch

diff --git a/crypt/crypt-private.h b/crypt/crypt-private.h
index fbaf9c4..8245087 100644
--- a/crypt/crypt-private.h
+++ b/crypt/crypt-private.h
@@ -28,6 +28,8 @@ 
 #include <features.h>
 #include <stdbool.h>
 
+#include "ufc-crypt.h"
+
 /* crypt.c */
 extern void _ufc_doit_r (ufc_long itr, struct crypt_data * __restrict __data,
 			 ufc_long *res);
@@ -57,4 +59,8 @@  extern char *__crypt_r (const char *__key, const char *__salt,
 			     struct crypt_data * __restrict __data);
 extern char *fcrypt (const char *key, const char *salt);
 
+extern void __b64_from_24bit (char **cp, int *buflen,
+                              unsigned int b2, unsigned int b1, unsigned int b0,
+                              int n);
+
 #endif  /* crypt-private.h */
diff --git a/crypt/crypt_util.c b/crypt/crypt_util.c
index 2875931..1b7aaf1 100644
--- a/crypt/crypt_util.c
+++ b/crypt/crypt_util.c
@@ -253,6 +253,10 @@  static ufc_long eperm32tab[4][256][2];
  */
 static ufc_long efp[16][64][2];
 
+/* Table with characters for base64 transformation.  */
+static const char b64t[64] =
+"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
+
 /*
  * For use by the old, non-reentrant routines
  * (crypt/encrypt/setkey)
@@ -956,3 +960,17 @@  setkey(__key)
 {
   __setkey_r(__key, &_ufc_foobar);
 }
+
+void
+__b64_from_24bit (char **cp, int *buflen,
+                  unsigned int b2, unsigned int b1, unsigned int b0,
+                  int n)
+{
+  unsigned int w = (b2 << 16) | (b1 << 8) | b0;
+  while (n-- > 0 && (*buflen) > 0)
+    {
+      *(*cp)++ = b64t[w & 0x3f];
+      --(*buflen);
+      w >>= 6;
+    }
+}
diff --git a/crypt/md5-crypt.c b/crypt/md5-crypt.c
index 5d5fc55..4d47380 100644
--- a/crypt/md5-crypt.c
+++ b/crypt/md5-crypt.c
@@ -25,6 +25,7 @@ 
 #include <sys/param.h>
 
 #include "md5.h"
+#include "crypt-private.h"
 
 
 #ifdef USE_NSS
@@ -78,30 +79,12 @@  typedef int PRBool;
    encryption implementations.  */
 static const char md5_salt_prefix[] = "$1$";
 
-/* Table with characters for base64 transformation.  */
-static const char b64t[64] =
-"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
-
 
 /* Prototypes for local functions.  */
 extern char *__md5_crypt_r (const char *key, const char *salt,
 			    char *buffer, int buflen);
 extern char *__md5_crypt (const char *key, const char *salt);
 
-static void
-b64_from_24bit (char **cp, int *buflen,
-                unsigned int b2, unsigned int b1, unsigned int b0,
-                int n)
-{
-  unsigned int w = (b2 << 16) | (b1 << 8) | b0;
-  while (n-- > 0 && *buflen > 0)
-    {
-      *(*cp)++ = b64t[w & 0x3f];
-      --*buflen;
-      w >>= 6;
-    }
-}
-
 
 /* This entry point is equivalent to the `crypt' function in Unix
    libcs.  */
@@ -282,18 +265,18 @@  __md5_crypt_r (key, salt, buffer, buflen)
       --buflen;
     }
 
-  b64_from_24bit (&cp, &buflen,
-                  alt_result[0], alt_result[6], alt_result[12], 4);
-  b64_from_24bit (&cp, &buflen,
-                  alt_result[1], alt_result[7], alt_result[13], 4);
-  b64_from_24bit (&cp, &buflen,
-                  alt_result[2], alt_result[8], alt_result[14], 4);
-  b64_from_24bit (&cp, &buflen,
-                  alt_result[3], alt_result[9], alt_result[15], 4);
-  b64_from_24bit (&cp, &buflen,
-                  alt_result[4], alt_result[10], alt_result[5], 4);
-  b64_from_24bit (&cp, &buflen,
-                  0, 0, alt_result[11], 2);
+  __b64_from_24bit (&cp, &buflen,
+                    alt_result[0], alt_result[6], alt_result[12], 4);
+  __b64_from_24bit (&cp, &buflen,
+                    alt_result[1], alt_result[7], alt_result[13], 4);
+  __b64_from_24bit (&cp, &buflen,
+                    alt_result[2], alt_result[8], alt_result[14], 4);
+  __b64_from_24bit (&cp, &buflen,
+                    alt_result[3], alt_result[9], alt_result[15], 4);
+  __b64_from_24bit (&cp, &buflen,
+                    alt_result[4], alt_result[10], alt_result[5], 4);
+  __b64_from_24bit (&cp, &buflen,
+                    0, 0, alt_result[11], 2);
   if (buflen <= 0)
     {
       __set_errno (ERANGE);
diff --git a/crypt/sha256-crypt.c b/crypt/sha256-crypt.c
index cf0abd2..17279dd 100644
--- a/crypt/sha256-crypt.c
+++ b/crypt/sha256-crypt.c
@@ -26,6 +26,7 @@ 
 #include <sys/param.h>
 
 #include "sha256.h"
+#include "crypt-private.h"
 
 
 #ifdef USE_NSS
@@ -90,10 +91,6 @@  static const char sha256_rounds_prefix[] = "rounds=";
 /* Maximum number of rounds.  */
 #define ROUNDS_MAX 999999999
 
-/* Table with characters for base64 transformation.  */
-static const char b64t[64] =
-"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
-
 
 /* Prototypes for local functions.  */
 extern char *__sha256_crypt_r (const char *key, const char *salt,
@@ -341,29 +338,28 @@  __sha256_crypt_r (key, salt, buffer, buflen)
       --buflen;
     }
 
-  void b64_from_24bit (unsigned int b2, unsigned int b1, unsigned int b0,
-		       int n)
-  {
-    unsigned int w = (b2 << 16) | (b1 << 8) | b0;
-    while (n-- > 0 && buflen > 0)
-      {
-	*cp++ = b64t[w & 0x3f];
-	--buflen;
-	w >>= 6;
-      }
-  }
-
-  b64_from_24bit (alt_result[0], alt_result[10], alt_result[20], 4);
-  b64_from_24bit (alt_result[21], alt_result[1], alt_result[11], 4);
-  b64_from_24bit (alt_result[12], alt_result[22], alt_result[2], 4);
-  b64_from_24bit (alt_result[3], alt_result[13], alt_result[23], 4);
-  b64_from_24bit (alt_result[24], alt_result[4], alt_result[14], 4);
-  b64_from_24bit (alt_result[15], alt_result[25], alt_result[5], 4);
-  b64_from_24bit (alt_result[6], alt_result[16], alt_result[26], 4);
-  b64_from_24bit (alt_result[27], alt_result[7], alt_result[17], 4);
-  b64_from_24bit (alt_result[18], alt_result[28], alt_result[8], 4);
-  b64_from_24bit (alt_result[9], alt_result[19], alt_result[29], 4);
-  b64_from_24bit (0, alt_result[31], alt_result[30], 3);
+  __b64_from_24bit (&cp, &buflen,
+                    alt_result[0], alt_result[10], alt_result[20], 4);
+  __b64_from_24bit (&cp, &buflen,
+                    alt_result[21], alt_result[1], alt_result[11], 4);
+  __b64_from_24bit (&cp, &buflen,
+                    alt_result[12], alt_result[22], alt_result[2], 4);
+  __b64_from_24bit (&cp, &buflen,
+                    alt_result[3], alt_result[13], alt_result[23], 4);
+  __b64_from_24bit (&cp, &buflen,
+                    alt_result[24], alt_result[4], alt_result[14], 4);
+  __b64_from_24bit (&cp, &buflen,
+                    alt_result[15], alt_result[25], alt_result[5], 4);
+  __b64_from_24bit (&cp, &buflen,
+                    alt_result[6], alt_result[16], alt_result[26], 4);
+  __b64_from_24bit (&cp, &buflen,
+                    alt_result[27], alt_result[7], alt_result[17], 4);
+  __b64_from_24bit (&cp, &buflen,
+                    alt_result[18], alt_result[28], alt_result[8], 4);
+  __b64_from_24bit (&cp, &buflen,
+                    alt_result[9], alt_result[19], alt_result[29], 4);
+  __b64_from_24bit (&cp, &buflen,
+                    0, alt_result[31], alt_result[30], 3);
   if (buflen <= 0)
     {
       __set_errno (ERANGE);
diff --git a/crypt/sha512-crypt.c b/crypt/sha512-crypt.c
index c0338a7..e05c4ac 100644
--- a/crypt/sha512-crypt.c
+++ b/crypt/sha512-crypt.c
@@ -26,6 +26,7 @@ 
 #include <sys/param.h>
 
 #include "sha512.h"
+#include "crypt-private.h"
 
 
 #ifdef USE_NSS
@@ -90,10 +91,6 @@  static const char sha512_rounds_prefix[] = "rounds=";
 /* Maximum number of rounds.  */
 #define ROUNDS_MAX 999999999
 
-/* Table with characters for base64 transformation.  */
-static const char b64t[64] =
-"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
-
 
 /* Prototypes for local functions.  */
 extern char *__sha512_crypt_r (const char *key, const char *salt,
@@ -340,40 +337,50 @@  __sha512_crypt_r (key, salt, buffer, buflen)
       --buflen;
     }
 
-  void b64_from_24bit (unsigned int b2, unsigned int b1, unsigned int b0,
-		       int n)
-  {
-    unsigned int w = (b2 << 16) | (b1 << 8) | b0;
-    while (n-- > 0 && buflen > 0)
-      {
-	*cp++ = b64t[w & 0x3f];
-	--buflen;
-	w >>= 6;
-      }
-  }
-
-  b64_from_24bit (alt_result[0], alt_result[21], alt_result[42], 4);
-  b64_from_24bit (alt_result[22], alt_result[43], alt_result[1], 4);
-  b64_from_24bit (alt_result[44], alt_result[2], alt_result[23], 4);
-  b64_from_24bit (alt_result[3], alt_result[24], alt_result[45], 4);
-  b64_from_24bit (alt_result[25], alt_result[46], alt_result[4], 4);
-  b64_from_24bit (alt_result[47], alt_result[5], alt_result[26], 4);
-  b64_from_24bit (alt_result[6], alt_result[27], alt_result[48], 4);
-  b64_from_24bit (alt_result[28], alt_result[49], alt_result[7], 4);
-  b64_from_24bit (alt_result[50], alt_result[8], alt_result[29], 4);
-  b64_from_24bit (alt_result[9], alt_result[30], alt_result[51], 4);
-  b64_from_24bit (alt_result[31], alt_result[52], alt_result[10], 4);
-  b64_from_24bit (alt_result[53], alt_result[11], alt_result[32], 4);
-  b64_from_24bit (alt_result[12], alt_result[33], alt_result[54], 4);
-  b64_from_24bit (alt_result[34], alt_result[55], alt_result[13], 4);
-  b64_from_24bit (alt_result[56], alt_result[14], alt_result[35], 4);
-  b64_from_24bit (alt_result[15], alt_result[36], alt_result[57], 4);
-  b64_from_24bit (alt_result[37], alt_result[58], alt_result[16], 4);
-  b64_from_24bit (alt_result[59], alt_result[17], alt_result[38], 4);
-  b64_from_24bit (alt_result[18], alt_result[39], alt_result[60], 4);
-  b64_from_24bit (alt_result[40], alt_result[61], alt_result[19], 4);
-  b64_from_24bit (alt_result[62], alt_result[20], alt_result[41], 4);
-  b64_from_24bit (0, 0, alt_result[63], 2);
+  __b64_from_24bit (&cp, &buflen,
+                    alt_result[0], alt_result[21], alt_result[42], 4);
+  __b64_from_24bit (&cp, &buflen,
+                    alt_result[22], alt_result[43], alt_result[1], 4);
+  __b64_from_24bit (&cp, &buflen,
+                    alt_result[44], alt_result[2], alt_result[23], 4);
+  __b64_from_24bit (&cp, &buflen,
+                    alt_result[3], alt_result[24], alt_result[45], 4);
+  __b64_from_24bit (&cp, &buflen,
+                    alt_result[25], alt_result[46], alt_result[4], 4);
+  __b64_from_24bit (&cp, &buflen,
+                    alt_result[47], alt_result[5], alt_result[26], 4);
+  __b64_from_24bit (&cp, &buflen,
+                    alt_result[6], alt_result[27], alt_result[48], 4);
+  __b64_from_24bit (&cp, &buflen,
+                    alt_result[28], alt_result[49], alt_result[7], 4);
+  __b64_from_24bit (&cp, &buflen,
+                    alt_result[50], alt_result[8], alt_result[29], 4);
+  __b64_from_24bit (&cp, &buflen,
+                    alt_result[9], alt_result[30], alt_result[51], 4);
+  __b64_from_24bit (&cp, &buflen,
+                    alt_result[31], alt_result[52], alt_result[10], 4);
+  __b64_from_24bit (&cp, &buflen,
+                    alt_result[53], alt_result[11], alt_result[32], 4);
+  __b64_from_24bit (&cp, &buflen,
+                    alt_result[12], alt_result[33], alt_result[54], 4);
+  __b64_from_24bit (&cp, &buflen,
+                    alt_result[34], alt_result[55], alt_result[13], 4);
+  __b64_from_24bit (&cp, &buflen,
+                    alt_result[56], alt_result[14], alt_result[35], 4);
+  __b64_from_24bit (&cp, &buflen,
+                    alt_result[15], alt_result[36], alt_result[57], 4);
+  __b64_from_24bit (&cp, &buflen,
+                    alt_result[37], alt_result[58], alt_result[16], 4);
+  __b64_from_24bit (&cp, &buflen,
+                    alt_result[59], alt_result[17], alt_result[38], 4);
+  __b64_from_24bit (&cp, &buflen,
+                    alt_result[18], alt_result[39], alt_result[60], 4);
+  __b64_from_24bit (&cp, &buflen,
+                    alt_result[40], alt_result[61], alt_result[19], 4);
+  __b64_from_24bit (&cp, &buflen,
+                    alt_result[62], alt_result[20], alt_result[41], 4);
+  __b64_from_24bit (&cp, &buflen,
+                    0, 0, alt_result[63], 2);
 
   if (buflen <= 0)
     {