Skip to content

Instantly share code, notes, and snippets.

@allanjude
Created April 21, 2016 03:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save allanjude/70b3cc21366ab5d161e11a8a2ceb55c1 to your computer and use it in GitHub Desktop.
Save allanjude/70b3cc21366ab5d161e11a8a2ceb55c1 to your computer and use it in GitHub Desktop.
/*
* Test if I broke opencrypto's deflate
*/
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Hackery because we're using sys/zlib.h code in userspace */
#define ZLIB_H
#define NO_DUMMY_DECL
#include <opencrypto/cryptodev.h>
#include <opencrypto/deflate.h>
#include <opencrypto/xform.h>
/* More hackery for kernel code in userspace, use userland malloc()/free() instead of kernel */
#include <opencrypto/xform_userland.h>
#include "cryptodeflate_local.c"
/* Use the kernel's zlib and deflate functions */
#include <libkern/zlib.c>
#include <opencrypto/xform_deflate.c>
int
main(int argc, char *argv[])
{
u_int8_t small[81] = "ALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLAN";
u_int8_t big[401] = "ALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLANALLAN";
u_int8_t *comp_output, *decomp_output;
u_int32_t outsize;
printf("small string = %lu bytes\n", sizeof(small));
outsize = comp_algo_deflate.compress(small, sizeof(small), &comp_output);
printf("Attempted compression of small string, result: %u\n", outsize);
outsize = comp_algo_deflate.decompress(comp_output, outsize, &decomp_output);
printf("Attempted decompression of small string, result: %u\n", outsize);
if (bcmp(decomp_output, small, sizeof(small)) == 0) {
printf("Decompressed small string was identical to input\n");
} else {
printf("Decompression of small string was incorrect!\n");
}
printf("\n");
printf("big string = %lu bytes\n", sizeof(big));
outsize = comp_algo_deflate.compress(big, sizeof(big), &comp_output);
printf("Attempted compression of big string, result: %u\n", outsize);
outsize = comp_algo_deflate.decompress(comp_output, outsize, &decomp_output);
printf("Attempted decompression of big string, result: %u\n", outsize);
if (bcmp(decomp_output, big, sizeof(big)) == 0) {
printf("Decompressed big string was identical to input\n");
} else {
printf("Decompression of big string was incorrect!\n");
}
printf("\n");
}
@allanjude
Copy link
Author

Attempted compression of small string, result: 11
Attempted decompression of small string, result: 81
Decompressed small string was identical to input

big string = 401 bytes
Attempted compression of big string, result: 13
Attempted decompression of big string, result: 401
Decompressed big string was identical to input

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment