[pacman-dev] [PATCH] Created hex_representation() in lib/libalpm/util.c which is used in alpm_compute_md5sum() and alpm_compute_sha256sum().

Dan McGee dpmcgee at gmail.com
Wed Dec 21 18:18:48 EST 2011


On Wed, Dec 21, 2011 at 5:02 PM, Diogo Sousa <diogogsousa at gmail.com> wrote:
> Created hex_representation() in lib/libalpm/util.c which is
> used in alpm_compute_md5sum() and alpm_compute_sha256sum().
>
> Signed-off-by: Diogo Sousa <diogogsousa at gmail.com>

Looks good now, except...

$ git am -3 -s < /tmp/hex.patch
Applying: Created hex_representation() in lib/libalpm/util.c which is
used in alpm_compute_md5sum() and alpm_compute_sha256sum().
Using index info to reconstruct a base tree...
<stdin>:15: trailing whitespace.
 *
<stdin>:29: trailing whitespace.
	for (i=0; i < size; i++) {		
error: patch failed: lib/libalpm/util.c:960
error: lib/libalpm/util.c: patch does not apply
Did you hand edit your patch?
It does not apply to blobs recorded in its index.
Cannot fall back to three-way merge.
Patch failed at 0001 Created hex_representation() in
lib/libalpm/util.c which is used in alpm_compute_md5sum() and
alpm_compute_sha256sum().
When you have resolved this problem run "git am --resolved".
If you would prefer to skip this patch, instead run "git am --skip".
To restore the original branch and stop patching run "git am --abort".


(You also didn't space the for loop i=0 bit correctly.)

> ---
>  lib/libalpm/util.c |   56 ++++++++++++++++++++++++++--------------------------
>  1 files changed, 28 insertions(+), 28 deletions(-)
>
> diff --git a/lib/libalpm/util.c b/lib/libalpm/util.c
> index 4eeb0cd..2fe6087 100644
> --- a/lib/libalpm/util.c
> +++ b/lib/libalpm/util.c
> @@ -917,7 +917,30 @@ static int sha2_file(const char *path, unsigned char output[32], int is224)
>  }
>  #endif
>
> -static const char *hex_digits = "0123456789abcdef";
> +/** Create a string representing bytes in hexadecimal.
> + *
> + * @param bytes the bytes to represent in hexadecimal.
> + * @param size number of bytes to consider.
> + * @return a nul terminated string with the hexadecimal representation
> + *         of bytes or NULL on error. This string must be freed.
> + */
> +static char *hex_representation(unsigned char *bytes, size_t size)
> +{
> +       const char *hex_digits = "0123456789abcdef";
> +       char *str;
> +       int i;
> +
> +       MALLOC(str, 2 * size + 1, return NULL);
> +
> +       for (i=0; i < size; i++) {
> +               str[2 * i] = hex_digits[bytes[i] >> 4];
> +               str[2 * i + 1] = hex_digits[bytes[i] & 0x0f];
> +       }
> +
> +       str[2 * size] = '\0';
> +
> +       return str;
> +}
>
>  /** Get the md5 sum of file.
>  * @param filename name of the file
> @@ -927,29 +950,17 @@ static const char *hex_digits = "0123456789abcdef";
>  char SYMEXPORT *alpm_compute_md5sum(const char *filename)
>  {
>        unsigned char output[16];
> -       char *md5sum;
> -       int ret, i;
> +       int ret;
>
>        ASSERT(filename != NULL, return NULL);
>
> -       MALLOC(md5sum, (size_t)33, return NULL);
> -       /* defined above for OpenSSL, otherwise defined in md5.h */
>        ret = md5_file(filename, output);
>
>        if(ret > 0) {
> -               free(md5sum);
>                return NULL;
>        }
>
> -       /* Convert the result to something readable */
> -       for(i = 0; i < 16; i++) {
> -               int pos = i * 2;
> -               /* high 4 bits are first digit, low 4 are second */
> -               md5sum[pos] = hex_digits[output[i] >> 4];
> -               md5sum[pos + 1] = hex_digits[output[i] & 0x0f];
> -       }
> -       md5sum[32] = '\0';
> -       return md5sum;
> +       return hex_representation(output, 16);
>  }
>
>  /** Get the sha256 sum of file.
> @@ -960,30 +971,18 @@ char SYMEXPORT *alpm_compute_md5sum(const char *filename)
>  char SYMEXPORT *alpm_compute_sha256sum(const char *filename)
>  {
>        unsigned char output[32];
> -       char *sha256sum;
> -       int ret, i;
> +       int ret;
>
>        ASSERT(filename != NULL, return NULL);
>
> -       MALLOC(sha256sum, (size_t)65, return NULL);
>        /* defined above for OpenSSL, otherwise defined in sha2.h */
>        ret = sha2_file(filename, output, 0);
>
>        if(ret > 0) {
> -               free(sha256sum);
>                return NULL;
>        }
>
> -       /* Convert the result to something readable */
> -       for(i = 0; i < 32; i++) {
> -               int pos = i * 2;
> -               /* high 4 bits are first digit, low 4 are second */
> -               sha256sum[pos] = hex_digits[output[i] >> 4];
> -               sha256sum[pos + 1] = hex_digits[output[i] & 0x0f];
> -       }
> -       sha256sum[64] = '\0';
> -       return sha256sum;
> +       return hex_representation(output, 32);
>  }
>
>  /** Calculates a file's MD5 or SHA2 digest  and compares it to an expected value.
> --
> 1.7.8
>
>


More information about the pacman-dev mailing list