xref: /openssl/demos/cms/cms_comp.c (revision da1c088f)
1 /*
2  * Copyright 2008-2023 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9 
10 /* Simple S/MIME compress example */
11 #include <openssl/pem.h>
12 #include <openssl/cms.h>
13 #include <openssl/err.h>
14 
main(int argc,char ** argv)15 int main(int argc, char **argv)
16 {
17     BIO *in = NULL, *out = NULL;
18     CMS_ContentInfo *cms = NULL;
19     int ret = EXIT_FAILURE;
20 
21     /*
22      * On OpenSSL 1.0.0+ only:
23      * for streaming set CMS_STREAM
24      */
25     int flags = CMS_STREAM;
26 
27     OpenSSL_add_all_algorithms();
28     ERR_load_crypto_strings();
29 
30     /* Open content being compressed */
31 
32     in = BIO_new_file("comp.txt", "r");
33 
34     if (!in)
35         goto err;
36 
37     /* compress content */
38     cms = CMS_compress(in, NID_zlib_compression, flags);
39 
40     if (!cms)
41         goto err;
42 
43     out = BIO_new_file("smcomp.txt", "w");
44     if (!out)
45         goto err;
46 
47     /* Write out S/MIME message */
48     if (!SMIME_write_CMS(out, cms, in, flags))
49         goto err;
50 
51     ret = EXIT_SUCCESS;
52 
53  err:
54 
55     if (ret != EXIT_SUCCESS) {
56         fprintf(stderr, "Error Compressing Data\n");
57         ERR_print_errors_fp(stderr);
58     }
59 
60     CMS_ContentInfo_free(cms);
61     BIO_free(in);
62     BIO_free(out);
63     return ret;
64 }
65