1 /*
2 * Copyright (c) 2020 Linumiz
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <stdio.h>
8 #include <zephyr.h>
9 #include <string.h>
10 #include <stdlib.h>
11 #include "lz4.h"
12
main(void)13 void main(void)
14 {
15 static const char *src = "Lorem ipsum dolor sit amet, consectetur "
16 "adipiscing elit. Quisque sodales lorem lorem, sed congue enim "
17 "vehicula a. Sed finibus diam sed odio ultrices pharetra. Nullam "
18 "dictum arcu ultricies turpis congue,vel venenatis turpis venenatis. "
19 "Nam tempus arcu eros, ac congue libero tristique congue. Proin velit "
20 "lectus, euismod sit amet quam in, maximus condimentum urna. Cras vel "
21 "erat luctus, mattis orci ut, varius urna. Nam eu lobortis velit."
22 "\n"
23 "Nullam sit amet diam vel odio sodales cursus vehicula eu arcu. Proin "
24 "fringilla, enim nec consectetur mollis, lorem orci interdum nisi, "
25 "vitae suscipit nisi mauris eu mi. Proin diam enim, mollis ac rhoncus "
26 "vitae, placerat et eros. Suspendisse convallis, ipsum nec rhoncus "
27 "aliquam, ex augue ultrices nisl, id aliquet mi diam quis ante. "
28 "Pellentesque venenatis ornare ultrices. Quisque et porttitor lectus. "
29 "Ut venenatis nunc et urna imperdiet porttitor non laoreet massa."
30 "Donec eleifend eros in mi sagittis egestas. Sed et mi nunc. Nunc "
31 "vulputate,mauris non ullamcorper viverra, lorem nulla vulputate "
32 "diam, et congue dui velit non erat. Duis interdum leo et ipsum "
33 "tempor consequat. In faucibus enim quis purus vulputate nullam."
34 "\n";
35
36 const int src_size = (int)(strlen(src) + 1);
37 const int max_dst_size = LZ4_compressBound(src_size);
38
39 char *compressed_data = malloc((size_t)max_dst_size);
40
41 if (compressed_data == NULL) {
42 printk("Failed to allocate memory for compressed data\n");
43 return;
44 }
45
46 const int compressed_data_size = LZ4_compress_default(src,
47 compressed_data, src_size,
48 max_dst_size);
49 if (compressed_data_size <= 0) {
50 printk("Failed to compress the data\n");
51 return;
52 }
53
54 printk("Original Data size: %d\n", src_size);
55 printk("Compressed Data size : %d\n", compressed_data_size);
56
57 compressed_data = (char *)realloc(compressed_data,
58 (size_t)compressed_data_size);
59 if (compressed_data == NULL) {
60 printk("Failed to re-alloc memory for compressed data\n");
61 return;
62 }
63
64 char * const decompressed_data = malloc(src_size);
65
66 if (decompressed_data == NULL) {
67 printk("Failed to allocate memory to decompress data\n");
68 return;
69 }
70
71 const int decompressed_size = LZ4_decompress_safe(compressed_data,
72 decompressed_data, compressed_data_size,
73 src_size);
74 if (decompressed_size < 0) {
75 printk("Failed to decompress the data\n");
76 return;
77 }
78
79 free(compressed_data);
80
81 if (decompressed_size >= 0) {
82 printk("Successfully decompressed some data\n");
83 }
84
85 if (decompressed_size != src_size) {
86 printk("Decompressed data is different from original\n");
87 return;
88 }
89
90 if (memcmp(src, decompressed_data, src_size) != 0) {
91 printk("Validation failed.\n");
92 printk("*src and *new_src are not identical\n");
93 return;
94 }
95
96 printk("Validation done. The string we ended up with is:\n%s\n",
97 decompressed_data);
98 }
99