FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > ArchLinux > ArchLinux Pacman Development

 
 
LinkBack Thread Tools
 
Old 12-21-2011, 09:54 PM
Dan McGee
 
Default Created hex_representation() in lib/libalpm/util.c which is used in alpm_compute_md5sum() and alpm_compute_sha256sum().

On Wed, Dec 21, 2011 at 4:48 PM, Diogo Sousa <diogogsousa@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@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] = '';
> +
> + * * * 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] = '';
> - * * * 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] = '';
> - * * * 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
 
Old 12-21-2011, 10:00 PM
Diogo Sousa
 
Default Created hex_representation() in lib/libalpm/util.c which is used in alpm_compute_md5sum() and alpm_compute_sha256sum().

On 12/21/2011 10:54 PM, Dan McGee wrote:
> On Wed, Dec 21, 2011 at 4:48 PM, Diogo Sousa <diogogsousa@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@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] = '';
>> +
>> + 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] = '';
>> - 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] = '';
>> - 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
>

Sorry,

will submit in a few minutes.

Thanks,
Diogo Sousa
 
Old 12-21-2011, 10:18 PM
Dan McGee
 
Default Created hex_representation() in lib/libalpm/util.c which is used in alpm_compute_md5sum() and alpm_compute_sha256sum().

On Wed, Dec 21, 2011 at 5:02 PM, Diogo Sousa <diogogsousa@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@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] = '';
> +
> + * * * 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] = '';
> - * * * 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] = '';
> - * * * 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
>
>
 

Thread Tools




All times are GMT. The time now is 05:24 AM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org