]> Sergey Matveev's repositories - tofuproxy.git/blobdiff - cmd/zstd/enzstd.c
Get rid of endian.h dependency
[tofuproxy.git] / cmd / zstd / enzstd.c
index 3655d79aa571e487c83fab902da02c1e99c1d237..6dbbe985a6367080b0384a4a89151e512210d78e 100644 (file)
@@ -1,6 +1,6 @@
 /*
 enzstd -- .warc.zst compressor
-Copyright (C) 2021 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2021-2023 Sergey Matveev <stargrave@stargrave.org>
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
@@ -18,38 +18,45 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #include <inttypes.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <sys/endian.h>
 
 #include <zstd.h>
 
+#ifdef __FreeBSD__
+#include "capsicum.c.in"
+#endif // __FreeBSD__
+
 int
 main(int argc, char **argv)
 {
+#ifdef __FreeBSD__
+    capsicum_start();
+#endif // __FreeBSD__
     ZSTD_CCtx *ctx = ZSTD_createCCtx();
     if (ctx == NULL) {
         fputs("can not initialize ZSTD_createCCtx\n", stderr);
         return EXIT_FAILURE;
-    };
+    }
     size_t zCode = ZSTD_CCtx_setParameter(ctx, ZSTD_c_checksumFlag, 1);
     if (ZSTD_isError(zCode)) {
         fprintf(stderr, "can not setParameter: %s\n", ZSTD_getErrorName(zCode));
         return EXIT_FAILURE;
-    };
+    }
     zCode = ZSTD_CCtx_setParameter(ctx, ZSTD_c_compressionLevel, ZSTD_maxCLevel());
     if (ZSTD_isError(zCode)) {
         fprintf(stderr, "can not setParameter: %s\n", ZSTD_getErrorName(zCode));
         return EXIT_FAILURE;
-    };
+    }
 
+    char *src      = NULL;
+    char *dst      = NULL;
     int rc         = EXIT_FAILURE;
     size_t srcSize = 8;
-    uint8_t *src   = malloc(srcSize);
+    src            = malloc(srcSize);
     if (src == NULL) {
         fprintf(stderr, "can not allocate memory: %zu\n", srcSize);
         goto Exit;
-    };
+    }
     size_t dstSize     = 0;
-    uint8_t *dst       = NULL;
     size_t srcWantSize = 0;
     size_t dstWantSize = 0;
     size_t n           = 0;
@@ -58,11 +65,14 @@ main(int argc, char **argv)
         if (n < 8) {
             if (feof(stdin)) {
                 break;
-            };
+            }
             perror("can not fread(stdin)");
             goto Exit;
-        };
-        srcWantSize = (size_t)be64dec(src);
+        }
+        srcWantSize = ((uint64_t)(src[0]) << 56) | ((uint64_t)(src[1]) << 48) |
+                      ((uint64_t)(src[2]) << 40) | ((uint64_t)(src[3]) << 32) |
+                      ((uint64_t)(src[4]) << 24) | ((uint64_t)(src[5]) << 16) |
+                      ((uint64_t)(src[6]) << 8) | (uint64_t)(src[7]);
         if (srcWantSize > srcSize) {
             free(src);
             srcSize = srcWantSize;
@@ -70,13 +80,13 @@ main(int argc, char **argv)
             if (src == NULL) {
                 fprintf(stderr, "can not allocate memory: %zu\n", srcSize);
                 goto Exit;
-            };
-        };
+            }
+        }
         n = fread(src, 1, srcWantSize, stdin);
         if (n < srcWantSize) {
             fputs("insufficient data fed\n", stderr);
             goto Exit;
-        };
+        }
         dstWantSize = ZSTD_compressBound(srcWantSize);
         if (dstWantSize > dstSize) {
             free(dst);
@@ -85,19 +95,19 @@ main(int argc, char **argv)
             if (dst == NULL) {
                 fprintf(stderr, "can not allocate memory: %zu\n", dstSize);
                 goto Exit;
-            };
-        };
+            }
+        }
         zCode = ZSTD_compress2(ctx, dst, dstSize, src, srcWantSize);
         if (ZSTD_isError(zCode)) {
             fprintf(stderr, "can not compress: %s\n", ZSTD_getErrorName(zCode));
             goto Exit;
-        };
+        }
         n = fwrite(dst, 1, zCode, stdout);
         if (n < zCode) {
             perror("can not fwrite(stdout)");
             goto Exit;
-        };
-    };
+        }
+    }
     rc = EXIT_SUCCESS;
 
 Exit:
@@ -105,4 +115,4 @@ Exit:
     free(src);
     ZSTD_freeCCtx(ctx);
     return rc;
-};
+}