Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
chpock committed Jun 22, 2024
1 parent 32cd16e commit 4855b26
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 50 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
2024-06-23 Konstantin Kushnir <chpock@gmail.com>
* Add support for brotli compression
* Refactor code

2024-06-21 Konstantin Kushnir <chpock@gmail.com>
* Refactor compression functions
Expand Down
2 changes: 1 addition & 1 deletion generic/pagesCmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,7 @@ static int CookfsPagesCmdCompression(Cookfs_Pages *pages, Tcl_Interp *interp, in
}
Cookfs_PagesUnlock(pages);

Tcl_SetObjResult(interp, Tcl_NewStringObj(cookfsCompressionNames[oCompression], -1));
Tcl_SetObjResult(interp, Tcl_NewStringObj(Cookfs_CompressionGetName(oCompression), -1));

return TCL_OK;

Expand Down
75 changes: 51 additions & 24 deletions generic/pagesCompr.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,29 +56,6 @@ const char *cookfsCompressionOptions[] = {
NULL
};

/* names for all defined compressions */
const char *cookfsCompressionNames[15] = {
"none", /* 0 */
"zlib", /* 1 */
"bz2", /* 2 */
"lzma", /* 3 */
"zstd", /* 4 */
"brotli", /* 5 */
"", "", "", "", "", "", "", "", /* 6 - 13 */
"custom" /* 14 */
};

const unsigned char cookfsCompressionLevels[15] = {
0, /* none - 0 */
6, /* zlib - 1 */
9, /* bz2 - 2 */
5, /* lzma - 3 */
3, /* zstd - 4 */
6, /* brotli - 5 */
0, 0, 0, 0, 0, 0, 0, 0, /* 6 - 13 */
0 /* custom - 14 */
};

const int cookfsCompressionOptionMap[] = {
COOKFS_COMPRESSION_NONE,
COOKFS_COMPRESSION_ZLIB,
Expand All @@ -98,6 +75,56 @@ const int cookfsCompressionOptionMap[] = {
-1 /* dummy entry */
};

const char *Cookfs_CompressionGetName(int compression) {
switch (compression) {
case COOKFS_COMPRESSION_NONE:
return "none";
case COOKFS_COMPRESSION_ZLIB:
return "zlib";
case COOKFS_COMPRESSION_BZ2:
return "bz2";
case COOKFS_COMPRESSION_LZMA:
return "lzma";
case COOKFS_COMPRESSION_ZSTD:
return "zstd";
case COOKFS_COMPRESSION_BROTLI:
return "brotli";
case COOKFS_COMPRESSION_CUSTOM:
return "custom";
case COOKFS_COMPRESSION_ANY:
return "any";
default:
return "unknown";
}
}

static int Cookfs_CompressionGetDefaultLevel(int compression) {
switch (compression) {
case COOKFS_COMPRESSION_NONE:
return 0;
case COOKFS_COMPRESSION_ZLIB:
return COOKFS_DEFAULT_COMPRESSION_LEVEL_ZLIB;
#ifdef COOKFS_USEBZ2
case COOKFS_COMPRESSION_BZ2:
return COOKFS_DEFAULT_COMPRESSION_LEVEL_BZ2;
#endif
#ifdef COOKFS_USELZMA
case COOKFS_COMPRESSION_LZMA:
return COOKFS_DEFAULT_COMPRESSION_LEVEL_LZMA;
#endif
#ifdef COOKFS_USEZSTD
case COOKFS_COMPRESSION_ZSTD:
return COOKFS_DEFAULT_COMPRESSION_LEVEL_ZSTD;
#endif
#ifdef COOKFS_USEBROTLI
case COOKFS_COMPRESSION_BROTLI:
return COOKFS_DEFAULT_COMPRESSION_LEVEL_BROTLI;
#endif
default:
return 255;
}
}

/*
*----------------------------------------------------------------------
*
Expand Down Expand Up @@ -204,7 +231,7 @@ int Cookfs_CompressionFromObj(Tcl_Interp *interp, Tcl_Obj *obj,
done:
*compressionPtr = compression;
if (compressionLevel == -1) {
compressionLevel = cookfsCompressionLevels[compression];
compressionLevel = Cookfs_CompressionGetDefaultLevel(compression);
}
*compressionLevelPtr = compressionLevel;
CookfsLog(printf("Cookfs_CompressionFromObj: return method [%d]"
Expand Down
29 changes: 4 additions & 25 deletions generic/pagesCompr.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,20 @@
#ifndef COOKFS_PAGESCOMPR_H
#define COOKFS_PAGESCOMPR_H 1

extern const char *cookfsCompressionOptions[];
extern const char *cookfsCompressionNames[];
extern const int cookfsCompressionOptionMap[];

#define COOKFS_COMPRESSION_NONE 0
#define COOKFS_COMPRESSION_ZLIB 1
#define COOKFS_COMPRESSION_BZ2 2
#define COOKFS_COMPRESSION_LZMA 3
#define COOKFS_COMPRESSION_ZSTD 4
#define COOKFS_COMPRESSION_BROTLI 5
#define COOKFS_COMPRESSION_CUSTOM 14
#define COOKFS_COMPRESSION_ANY 15

enum {
cookfsCompressionOptNone,
cookfsCompressionOptZlib,
#ifdef COOKFS_USEBZ2
cookfsCompressionOptBz2,
#endif /* COOKFS_USEBZ2 */
#ifdef COOKFS_USELZMA
cookfsCompressionOptLzma,
#endif /* COOKFS_USELZMA */
#ifdef COOKFS_USEZSTD
cookfsCompressionOptZstd,
#endif /* COOKFS_USEZSTD */
#ifdef COOKFS_USEBROTLI
cookfsCompressionOptBrotli,
#endif /* COOKFS_USEBROTLI */
cookfsCompressionOptCustom,
cookfsCompressionOptMax
};
#define COOKFS_COMPRESSION_CUSTOM 254
#define COOKFS_COMPRESSION_ANY 255

/* let's gain at least 16 bytes and/or 5% to compress it */
#define SHOULD_COMPRESS(p, origSize, size) ((p->alwaysCompress) || ((size < (origSize - 16)) && ((size) <= (origSize - (origSize / 20)))))

const char *Cookfs_CompressionGetName(int compression);

void CookfsWriteCompression(Cookfs_Pages *p, int compression);

int Cookfs_CompressionFromObj(Tcl_Interp *interp, Tcl_Obj *obj,
Expand Down
2 changes: 2 additions & 0 deletions generic/pagesComprBrotli.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@
Cookfs_PageObj CookfsReadPageBrotli(Cookfs_Pages *p, int size, Tcl_Obj **err);
int CookfsWritePageBrotli(Cookfs_Pages *p, unsigned char *bytes, int origSize);

#define COOKFS_DEFAULT_COMPRESSION_LEVEL_BROTLI 6

#endif /* COOKFS_PAGESCOMPRBROTLI_H */
2 changes: 2 additions & 0 deletions generic/pagesComprBz2.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#ifndef COOKFS_PAGESCOMPRBZ2_H
#define COOKFS_PAGESCOMPRBZ2_H 1

#define COOKFS_DEFAULT_COMPRESSION_LEVEL_BZ2 9

Cookfs_PageObj CookfsReadPageBz2(Cookfs_Pages *p, int size, Tcl_Obj **err);
int CookfsWritePageBz2(Cookfs_Pages *p, unsigned char *bytes, int origSize);

Expand Down
2 changes: 2 additions & 0 deletions generic/pagesComprLzma.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#ifndef COOKFS_PAGESCOMPRLZMA_H
#define COOKFS_PAGESCOMPRLZMA_H 1

#define COOKFS_DEFAULT_COMPRESSION_LEVEL_LZMA 5

Cookfs_PageObj CookfsReadPageLzma(Cookfs_Pages *p, int size, Tcl_Obj **err);
int CookfsWritePageLzma(Cookfs_Pages *p, unsigned char *bytes, int origSize);

Expand Down
2 changes: 2 additions & 0 deletions generic/pagesComprZlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#ifndef COOKFS_PAGESCOMPRZLIB_H
#define COOKFS_PAGESCOMPRZLIB_H 1

#define COOKFS_DEFAULT_COMPRESSION_LEVEL_ZLIB 6

Cookfs_PageObj CookfsReadPageZlib(Cookfs_Pages *p, int size, Tcl_Obj **err);
int CookfsWritePageZlib(Cookfs_Pages *p, unsigned char *bytes, int origSize);

Expand Down
2 changes: 2 additions & 0 deletions generic/pagesComprZstd.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#ifndef COOKFS_PAGESCOMPRZSTD_H
#define COOKFS_PAGESCOMPRZSTD_H 1

#define COOKFS_DEFAULT_COMPRESSION_LEVEL_ZSTD 3

Cookfs_PageObj CookfsReadPageZstd(Cookfs_Pages *p, int size, Tcl_Obj **err);
int CookfsWritePageZstd(Cookfs_Pages *p, unsigned char *bytes, int origSize);

Expand Down

0 comments on commit 4855b26

Please sign in to comment.