Git-Url: http://git.frugalware.org/gitweb/gitweb.cgi?p=pacman-g2.git;a=commitdiff;h=25ed91ebc59fee0ed0557d3814c97ce0a5dd1336
commit 25ed91ebc59fee0ed0557d3814c97ce0a5dd1336 Author: Michel Hermier <herm...@frugalware.org> Date: Wed May 15 12:28:58 2013 +0200 libpacman: Clean up sha1 public API like md5 one. diff --git a/lib/libflib/fsha1.c b/lib/libflib/fsha1.c index 36a6499..2daaef7 100644 --- a/lib/libflib/fsha1.c +++ b/lib/libflib/fsha1.c @@ -30,9 +30,47 @@ #include <sys/types.h> +#include <limits.h> #include <stdlib.h> -#include <time.h> #include <string.h> +#include <time.h> + +#define rol(x,n) ( ((x) << (n)) | ((x) >> (32 -(n))) ) +/* The code below is from md5.h (from coreutils), little modifications */ +#define UINT_MAX_32_BITS 4294967295U + +#if UINT_MAX == UINT_MAX_32_BITS + typedef unsigned int sha_uint32; +#else +#if USHRT_MAX == UINT_MAX_32_BITS + typedef unsigned short sha_uint32; +#else +#if ULONG_MAX == UINT_MAX_32_BITS + typedef unsigned long sha_uint32; +#else + /* The following line is intended to evoke an error. Using #error is not portable enough. */ +#error "Cannot determine unsigned 32-bit data type" +#endif +#endif +#endif +/* We have to make a guess about the integer type equivalent in size + to pointers which should always be correct. */ +typedef unsigned long int sha_uintptr; + +/* Structure to save state of computation between the single steps. */ +struct sha_ctx +{ + sha_uint32 A; + sha_uint32 B; + sha_uint32 C; + sha_uint32 D; + sha_uint32 E; + + sha_uint32 total[2]; + sha_uint32 buflen; + char buffer[128]; +}; + /* Not-swap is a macro that does an endian swap on architectures that are @@ -366,3 +404,15 @@ sha_process_block (const void *buffer, size_t len, struct sha_ctx *ctx) } } +FSHA1 *f_sha1_new () { + FSHA1 *sha1 = f_malloc (sizeof (*sha1)); + + if (sha1 != NULL) { + f_sha1_init (sha1); + } + return sha1; +} + +void f_sha1_delete (FSHA1 *sha1) { + f_free_sha1; +} diff --git a/lib/libflib/fsha1.h b/lib/libflib/fsha1.h index f6aea71..e233b58 100644 --- a/lib/libflib/fsha1.h +++ b/lib/libflib/fsha1.h @@ -16,48 +16,14 @@ along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -#include <stdio.h> -#include <limits.h> - -#define rol(x,n) ( ((x) << (n)) | ((x) >> (32 -(n))) ) -/* The code below is from md5.h (from coreutils), little modifications */ -#define UINT_MAX_32_BITS 4294967295U - -#if UINT_MAX == UINT_MAX_32_BITS - typedef unsigned int sha_uint32; -#else -#if USHRT_MAX == UINT_MAX_32_BITS - typedef unsigned short sha_uint32; -#else -#if ULONG_MAX == UINT_MAX_32_BITS - typedef unsigned long sha_uint32; -#else - /* The following line is intended to evoke an error. Using #error is not portable enough. */ -#error "Cannot determine unsigned 32-bit data type" -#endif -#endif -#endif -/* We have to make a guess about the integer type equivalent in size - to pointers which should always be correct. */ -typedef unsigned long int sha_uintptr; - -/* Structure to save state of computation between the single steps. */ -struct sha_ctx -{ - sha_uint32 A; - sha_uint32 B; - sha_uint32 C; - sha_uint32 D; - sha_uint32 E; - - sha_uint32 total[2]; - sha_uint32 buflen; - char buffer[128]; -}; - -typedef struct sha_ctx sha_ctx; - -void f_sha1_init (sha_ctx *); -void f_sha1_update (sha_ctx *, const void *, size_t); -void *f_sha1_fini (sha_ctx *, void *); +#include <sys/types.h> + +typedef struct sha_ctx FSHA1; + +FSHA1 *f_sha1_new (); +void f_sha1_delete (FSHA1 *sha1); + +void f_sha1_init (FSHA1 *sha1); +void f_sha1_update (FSHA1 *sha1, const void *, size_t); +void *f_sha1_fini (FSHA1 *sha1, void *); diff --git a/lib/libpacman/sha1.c b/lib/libpacman/sha1.c index 2f560f9..8882507 100644 --- a/lib/libpacman/sha1.c +++ b/lib/libpacman/sha1.c @@ -41,22 +41,25 @@ These notices must be retained in any copies of any part of this documentation and/or software. */ - char* _pacman_SHAFile(char *filename) { FILE *file; - struct sha_ctx context; + FSHA1 *sha1; int len = 0, i, x; unsigned char buffer[1024], digest[20]; char *ret; + if ((sha1 = f_sha1_new ()) == NULL) { + return NULL; + } + if((file = fopen(filename, "rb")) == NULL) { fprintf(stderr, _("%s can't be opened\n"), filename); } else { - f_sha1_init (&context); + f_sha1_init (sha1); while((len = fread(buffer, 1, 1024, file))) { - f_sha1_update (&context, buffer, len); + f_sha1_update (sha1, buffer, len); } - f_sha1_fini (&context, digest); + f_sha1_fini (sha1, digest); fclose(file); #ifdef DEBUG SHAPrint(digest); @@ -71,5 +74,8 @@ char* _pacman_SHAFile(char *filename) { return(ret); } + f_sha1_delete (sha1); + return(NULL); } + _______________________________________________ Frugalware-git mailing list Frugalware-git@frugalware.org http://frugalware.org/mailman/listinfo/frugalware-git