[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 17:54:48 EST 2011
On Wed, Dec 21, 2011 at 4:48 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>
> ---
> 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)
> +{
> + static const char *hex_digits = "0123456789abcdef";
static not needed with const now that this is local to the function,
not a big deal though.
> + char *str;
> + int i;
> +
> + MALLOC(str, 2*size+1, return NULL);
Man, this happens too much ,time to update HACKING.
Bad:
2*size+1
Good:
2 * size + 1
We like spaces around here, so please use them in all places in this
patch you are using operators- this should have been clear from the
code you removed (e.g., "pos + 1").
> +
> + 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);
Space after comma too...
> }
>
> /** Get the sha256 sum of file.
> @@ -960,29 +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