This example is a simple command line application that takes one or more arguments.
This example is a simple command line application that takes one or more arguments. It demonstrates a typical use of the hashing API with allocation, initialization, updating, and finalizing.
#include "config.h"
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#if HAVE_IO_H
#include <io.h>
#endif
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#define SIZE 65536
static struct AVHashContext *hash;
static int out_b64;
{
int i = 0;
const char *name;
printf("usage: ffhash [b64:]algorithm [input]...\n");
printf("Supported hash algorithms:");
do {
if (name)
printf(" %s", name);
i++;
} while(name);
printf("\n");
}
static void finish(void)
{
if (out_b64) {
printf("b64:%s", res);
} else {
printf("0x%s", res);
}
}
static int check(char *file)
{
uint8_t buffer[SIZE];
int fd, flags = O_RDONLY;
int ret = 0;
#ifdef O_BINARY
flags |= O_BINARY;
#endif
if (file) fd = open(file, flags);
else fd = 0;
if (fd == -1) {
ret = 1;
goto end;
}
for (;;) {
int size = read(fd, buffer, SIZE);
if (size < 0) {
int err = errno;
close(fd);
finish();
printf("+READ-FAILED: %s", strerror(err));
ret = 2;
goto end;
} else if(!size)
break;
}
close(fd);
finish();
end:
if (file)
printf(" *%s", file);
printf("\n");
return ret;
}
int main(
int argc,
char **argv)
{
int i;
int ret = 0;
const char *hash_name;
if (argc == 1) {
return 0;
}
hash_name = argv[1];
switch(ret) {
printf("Invalid hash type: %s\n", hash_name);
break;
printf("%s\n", strerror(errno));
break;
}
return 1;
}
for (i = 2; i < argc; i++)
ret |= check(argv[i]);
if (argc < 3)
ret |= check(NULL);
return ret;
}
int main(int argc, char *argv[])
static void usage(const char *program_name)
void av_hash_freep(struct AVHashContext **ctx)
Free hash context and set hash context pointer to NULL.
void av_hash_final_b64(struct AVHashContext *ctx, uint8_t *dst, int size)
Finalize a hash context and store the Base64 representation of the actual hash value as a string.
const char * av_hash_get_name(const struct AVHashContext *ctx)
Get the name of the algorithm corresponding to the given hash context.
void av_hash_final_hex(struct AVHashContext *ctx, uint8_t *dst, int size)
Finalize a hash context and store the hexadecimal representation of the actual hash value as a string...
void av_hash_init(struct AVHashContext *ctx)
Initialize or reset a hash context.
void av_hash_update(struct AVHashContext *ctx, const uint8_t *src, size_t len)
Update a hash context with additional data.
int av_hash_alloc(struct AVHashContext **ctx, const char *name)
Allocate a hash context for the algorithm specified by name.
const char * av_hash_names(int i)
Get the names of available hash algorithms.
#define AV_HASH_MAX_SIZE
Maximum value that av_hash_get_size() will currently return.
int av_strstart(const char *str, const char *pfx, const char **ptr)
Return non-zero if pfx is a prefix of str.