This commit is contained in:
2026-06-16 16:09:42 +08:00
commit bffb0cb6b7
644 changed files with 86620 additions and 0 deletions

View File

@@ -0,0 +1,81 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "pyzlib.h"
int main(void)
{
printf("=== Basic Self-Implemented zlib Test ===\n\n");
printf("1. Adler-32:\n");
unsigned long a = zlib_adler32((const unsigned char *)"Hello", 5, 1);
printf(" adler32(\"Hello\") = 0x%08lX\n", a);
unsigned long a2 = zlib_adler32((const unsigned char *)"Hel", 3, 1);
a2 = zlib_adler32((const unsigned char *)"lo", 2, a2);
printf(" incremental = 0x%08lX %s\n", a2, a == a2 ? "MATCH" : "MISMATCH");
printf("\n2. CRC-32:\n");
unsigned long c = zlib_crc32((const unsigned char *)"Hello", 5, 0);
printf(" crc32(\"Hello\") = 0x%08lX\n", c);
unsigned long c2 = zlib_crc32((const unsigned char *)"Hel", 3, 0);
c2 = zlib_crc32((const unsigned char *)"lo", 2, c2);
printf(" incremental = 0x%08lX %s\n", c2, c == c2 ? "MATCH" : "MISMATCH");
printf("\n3. Compress (zlib format):\n");
const char *input = "Hello, World!";
size_t comp_len = 0;
unsigned char *comp = zlib_compress((const unsigned char *)input, strlen(input),
Z_DEFAULT_COMPRESSION, MAX_WBITS, &comp_len);
if (comp) {
printf(" Input: %zu bytes\n", strlen(input));
printf(" Output: %zu bytes\n", comp_len);
printf(" Hex: ");
for (size_t i = 0; i < comp_len && i < 40; i++) printf("%02X ", comp[i]);
printf("\n");
printf("\n4. Decompress (zlib format):\n");
size_t dec_len = 0;
unsigned char *dec = zlib_decompress(comp, comp_len, MAX_WBITS, DEF_BUF_SIZE, &dec_len);
if (dec) {
printf(" Output: %zu bytes\n", dec_len);
printf(" Content: \"%.*s\"\n", (int)dec_len, (char *)dec);
printf(" Match: %s\n", dec_len == strlen(input) && memcmp(dec, input, dec_len) == 0 ? "YES" : "NO");
free(dec);
} else {
printf(" FAILED! Error: %s (code %d)\n", zlib_get_error(), zlib_get_error_code());
}
free(comp);
} else {
printf(" FAILED! Error: %s (code %d)\n", zlib_get_error(), zlib_get_error_code());
}
printf("\n5. Compress (raw deflate):\n");
comp_len = 0;
comp = zlib_compress((const unsigned char *)input, strlen(input),
Z_DEFAULT_COMPRESSION, -MAX_WBITS, &comp_len);
if (comp) {
printf(" Output: %zu bytes\n", comp_len);
printf(" Hex: ");
for (size_t i = 0; i < comp_len && i < 40; i++) printf("%02X ", comp[i]);
printf("\n");
size_t dec_len = 0;
unsigned char *dec = zlib_decompress(comp, comp_len, -MAX_WBITS, DEF_BUF_SIZE, &dec_len);
if (dec) {
printf(" Decompress: \"%.*s\" %s\n", (int)dec_len, (char *)dec,
dec_len == strlen(input) && memcmp(dec, input, dec_len) == 0 ? "OK" : "FAIL");
free(dec);
} else {
printf(" Decompress FAILED! Error: %s\n", zlib_get_error());
}
free(comp);
} else {
printf(" FAILED! Error: %s (code %d)\n", zlib_get_error(), zlib_get_error_code());
}
printf("\nDone.\n");
return 0;
}