All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] ecryptfs: remove private bin2hex implementation
@ 2016-09-20 23:17 Rasmus Villemoes
  2016-10-10 20:08 ` Tyler Hicks
  0 siblings, 1 reply; 2+ messages in thread
From: Rasmus Villemoes @ 2016-09-20 23:17 UTC (permalink / raw)
  To: Tyler Hicks; +Cc: Rasmus Villemoes, ecryptfs, linux-kernel

Calling sprintf in a loop is not very efficient, and in any case, we
already have an implementation of bin-to-hex conversion in lib/ which
we might as well use.

Note that ecryptfs_to_hex used to nul-terminate the destination (and
the kernel doc was wrong about the required output size), while
bin2hex doesn't. [All but one user of ecryptfs_to_hex explicitly
nul-terminates the result anyway.]

Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
---
 fs/ecryptfs/crypto.c          | 15 ---------------
 fs/ecryptfs/ecryptfs_kernel.h |  8 +++++++-
 2 files changed, 7 insertions(+), 16 deletions(-)

diff --git a/fs/ecryptfs/crypto.c b/fs/ecryptfs/crypto.c
index e5e29f8c920b..7acd57da4f14 100644
--- a/fs/ecryptfs/crypto.c
+++ b/fs/ecryptfs/crypto.c
@@ -42,21 +42,6 @@
 #define ENCRYPT		1
 
 /**
- * ecryptfs_to_hex
- * @dst: Buffer to take hex character representation of contents of
- *       src; must be at least of size (src_size * 2)
- * @src: Buffer to be converted to a hex string representation
- * @src_size: number of bytes to convert
- */
-void ecryptfs_to_hex(char *dst, char *src, size_t src_size)
-{
-	int x;
-
-	for (x = 0; x < src_size; x++)
-		sprintf(&dst[x * 2], "%.2x", (unsigned char)src[x]);
-}
-
-/**
  * ecryptfs_from_hex
  * @dst: Buffer to take the bytes from src hex; must be at least of
  *       size (src_size / 2)
diff --git a/fs/ecryptfs/ecryptfs_kernel.h b/fs/ecryptfs/ecryptfs_kernel.h
index 4ba1547bb9ad..548caac3a907 100644
--- a/fs/ecryptfs/ecryptfs_kernel.h
+++ b/fs/ecryptfs/ecryptfs_kernel.h
@@ -51,7 +51,13 @@
 #define ECRYPTFS_XATTR_NAME "user.ecryptfs"
 
 void ecryptfs_dump_auth_tok(struct ecryptfs_auth_tok *auth_tok);
-extern void ecryptfs_to_hex(char *dst, char *src, size_t src_size);
+static inline void
+ecryptfs_to_hex(char *dst, char *src, size_t src_size)
+{
+	char *end = bin2hex(dst, src, src_size);
+	*end = '\0';
+}
+
 extern void ecryptfs_from_hex(char *dst, char *src, int dst_size);
 
 struct ecryptfs_key_record {
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] ecryptfs: remove private bin2hex implementation
  2016-09-20 23:17 [PATCH] ecryptfs: remove private bin2hex implementation Rasmus Villemoes
@ 2016-10-10 20:08 ` Tyler Hicks
  0 siblings, 0 replies; 2+ messages in thread
From: Tyler Hicks @ 2016-10-10 20:08 UTC (permalink / raw)
  To: Rasmus Villemoes; +Cc: ecryptfs, linux-kernel


[-- Attachment #1.1: Type: text/plain, Size: 2527 bytes --]

On 09/20/2016 06:17 PM, Rasmus Villemoes wrote:
> Calling sprintf in a loop is not very efficient, and in any case, we
> already have an implementation of bin-to-hex conversion in lib/ which
> we might as well use.
> 
> Note that ecryptfs_to_hex used to nul-terminate the destination (and
> the kernel doc was wrong about the required output size), while
> bin2hex doesn't. [All but one user of ecryptfs_to_hex explicitly
> nul-terminates the result anyway.]
> 
> Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>

ecryptfs_kernel.h needs to include <linux/kernel.h> for the declaration
of bin2hex() (it didn't cause a problem because other included header
files, such as <linux/namei.h> pulled in <linux/kernel.h>). I've made
that change and will push the patch to the eCryptfs next branch. Thank you!

Tyler

> ---
>  fs/ecryptfs/crypto.c          | 15 ---------------
>  fs/ecryptfs/ecryptfs_kernel.h |  8 +++++++-
>  2 files changed, 7 insertions(+), 16 deletions(-)
> 
> diff --git a/fs/ecryptfs/crypto.c b/fs/ecryptfs/crypto.c
> index e5e29f8c920b..7acd57da4f14 100644
> --- a/fs/ecryptfs/crypto.c
> +++ b/fs/ecryptfs/crypto.c
> @@ -42,21 +42,6 @@
>  #define ENCRYPT		1
>  
>  /**
> - * ecryptfs_to_hex
> - * @dst: Buffer to take hex character representation of contents of
> - *       src; must be at least of size (src_size * 2)
> - * @src: Buffer to be converted to a hex string representation
> - * @src_size: number of bytes to convert
> - */
> -void ecryptfs_to_hex(char *dst, char *src, size_t src_size)
> -{
> -	int x;
> -
> -	for (x = 0; x < src_size; x++)
> -		sprintf(&dst[x * 2], "%.2x", (unsigned char)src[x]);
> -}
> -
> -/**
>   * ecryptfs_from_hex
>   * @dst: Buffer to take the bytes from src hex; must be at least of
>   *       size (src_size / 2)
> diff --git a/fs/ecryptfs/ecryptfs_kernel.h b/fs/ecryptfs/ecryptfs_kernel.h
> index 4ba1547bb9ad..548caac3a907 100644
> --- a/fs/ecryptfs/ecryptfs_kernel.h
> +++ b/fs/ecryptfs/ecryptfs_kernel.h
> @@ -51,7 +51,13 @@
>  #define ECRYPTFS_XATTR_NAME "user.ecryptfs"
>  
>  void ecryptfs_dump_auth_tok(struct ecryptfs_auth_tok *auth_tok);
> -extern void ecryptfs_to_hex(char *dst, char *src, size_t src_size);
> +static inline void
> +ecryptfs_to_hex(char *dst, char *src, size_t src_size)
> +{
> +	char *end = bin2hex(dst, src, src_size);
> +	*end = '\0';
> +}
> +
>  extern void ecryptfs_from_hex(char *dst, char *src, int dst_size);
>  
>  struct ecryptfs_key_record {
> 



[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 801 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2016-10-10 20:08 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-09-20 23:17 [PATCH] ecryptfs: remove private bin2hex implementation Rasmus Villemoes
2016-10-10 20:08 ` Tyler Hicks

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.