dedup

data deduplication program
git clone git://git.2f30.org/dedup.git
Log | Files | Refs | README | LICENSE

compress.c (2091B)


      1 #include <sys/types.h>
      2 
      3 #include <err.h>
      4 #include <stdint.h>
      5 #include <stdio.h>
      6 #include <string.h>
      7 #include <strings.h>
      8 
      9 #include "blake2.h"
     10 #include "dedup.h"
     11 
     12 static struct compr_ops {
     13 	int (*init)(struct compr_ctx *ctx);
     14 	size_t (*size)(struct compr_ctx *ctx, size_t n);
     15 	size_t (*compr)(struct compr_ctx *ctx, const void *in, void *out,
     16 	                size_t insize, size_t outsize);
     17 	size_t (*decompr)(struct compr_ctx *ctx, const void *in, void *out,
     18 	                  size_t insize, size_t outsize);
     19 	int (*final)(struct compr_ctx *ctx);
     20 } comprs[NR_COMPRS] = {
     21 	{
     22 		.init = none_init,
     23 		.size = none_size,
     24 		.compr = none_compr,
     25 		.decompr = none_decompr,
     26 		.final = none_final,
     27 	},
     28 	{
     29 		.init = lz4_init,
     30 		.size = lz4_size,
     31 		.compr = lz4_compr,
     32 		.decompr = lz4_decompr,
     33 		.final = lz4_final,
     34 	},
     35 	{
     36 		.init = snappy_init,
     37 		.size = snappy_size,
     38 		.compr = snappy_compr,
     39 		.decompr = snappy_decompr,
     40 		.final = snappy_final,
     41 	},
     42 };
     43 
     44 static char *algomap[NR_COMPRS] = {
     45 	[COMPR_NONE] = "none",
     46 	[COMPR_LZ4] = "lz4",
     47 	[COMPR_SNAPPY] = "snappy",
     48 };
     49 
     50 int
     51 compr_init(struct compr_ctx *ctx, int type)
     52 {
     53 	if (type < 0 || type >= NR_COMPRS)
     54 		return -1;
     55 
     56 	ctx->ops = &comprs[type];
     57 	return (*ctx->ops->init)(ctx);
     58 }
     59 
     60 int
     61 compr_size(struct compr_ctx *ctx, size_t n)
     62 {
     63 	return (*ctx->ops->size)(ctx, n);
     64 }
     65 
     66 size_t
     67 compr(struct compr_ctx *ctx, const void *in, void *out,
     68       size_t insize, size_t outsize)
     69 {
     70 	return (*ctx->ops->compr)(ctx, in, out, insize, outsize);
     71 }
     72 
     73 size_t
     74 decompr(struct compr_ctx *ctx, const void *in, void *out,
     75         size_t insize, size_t outsize)
     76 {
     77 	return (*ctx->ops->decompr)(ctx, in, out, insize, outsize);
     78 }
     79 
     80 int
     81 compr_final(struct compr_ctx *ctx)
     82 {
     83 	return (*ctx->ops->final)(ctx);
     84 }
     85 
     86 int
     87 compr_name2type(char *name)
     88 {
     89 	size_t i;
     90 
     91 	for (i = 0; i < NR_COMPRS; i++)
     92 		if (strcasecmp(algomap[i], name) == 0)
     93 			return i;
     94 	return -1;
     95 }
     96 
     97 char *
     98 compr_type2name(int type)
     99 {
    100 	if (type < 0 || type >= NR_HASHES)
    101 		return NULL;
    102 	return algomap[type];
    103 }
    104 
    105 void
    106 compr_list(int fd)
    107 {
    108 	size_t i;
    109 
    110 	for (i = 0; i < NR_COMPRS; i++)
    111 		dprintf(fd, "%s\n", algomap[i]);
    112 }