cms_comp.c 957 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /* Simple S/MIME compress example */
  2. #include <openssl/pem.h>
  3. #include <openssl/cms.h>
  4. #include <openssl/err.h>
  5. int main(int argc, char **argv)
  6. {
  7. BIO *in = NULL, *out = NULL;
  8. CMS_ContentInfo *cms = NULL;
  9. int ret = 1;
  10. /*
  11. * On OpenSSL 0.9.9 only:
  12. * for streaming set CMS_STREAM
  13. */
  14. int flags = CMS_STREAM;
  15. OpenSSL_add_all_algorithms();
  16. ERR_load_crypto_strings();
  17. /* Open content being compressed */
  18. in = BIO_new_file("comp.txt", "r");
  19. if (!in)
  20. goto err;
  21. /* compress content */
  22. cms = CMS_compress(in, NID_zlib_compression, flags);
  23. if (!cms)
  24. goto err;
  25. out = BIO_new_file("smcomp.txt", "w");
  26. if (!out)
  27. goto err;
  28. /* Write out S/MIME message */
  29. if (!SMIME_write_CMS(out, cms, in, flags))
  30. goto err;
  31. ret = 0;
  32. err:
  33. if (ret)
  34. {
  35. fprintf(stderr, "Error Compressing Data\n");
  36. ERR_print_errors_fp(stderr);
  37. }
  38. if (cms)
  39. CMS_ContentInfo_free(cms);
  40. if (in)
  41. BIO_free(in);
  42. if (out)
  43. BIO_free(out);
  44. return ret;
  45. }