Sean Purcell | f07ddf8 | 2017-02-08 13:56:32 -0800 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (c) 2017-present, Facebook, Inc. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * This source code is licensed under the BSD-style license found in the |
| 6 | * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | This program uses hard-coded data compressed with Zstd legacy versions |
| 12 | and tests that the API decompresses them correctly |
| 13 | */ |
| 14 | |
| 15 | /*=========================================== |
| 16 | * Dependencies |
| 17 | *==========================================*/ |
| 18 | #include <stddef.h> /* size_t */ |
| 19 | #include <stdlib.h> /* malloc, free */ |
| 20 | #include <stdio.h> /* fprintf */ |
| 21 | #include <string.h> /* strlen */ |
| 22 | #include "zstd.h" |
| 23 | #include "zstd_errors.h" |
| 24 | |
| 25 | /*=========================================== |
| 26 | * Macros |
| 27 | *==========================================*/ |
| 28 | #define DISPLAY(...) fprintf(stderr, __VA_ARGS__) |
| 29 | |
| 30 | /*=========================================== |
| 31 | * Precompressed frames |
| 32 | *==========================================*/ |
| 33 | const char* const COMPRESSED; /* content is at end of file */ |
| 34 | size_t const COMPRESSED_SIZE = 917; |
| 35 | const char* const EXPECTED; /* content is at end of file */ |
| 36 | |
| 37 | |
| 38 | int testSimpleAPI(void) |
| 39 | { |
| 40 | size_t const size = strlen(EXPECTED); |
| 41 | char* const output = malloc(size); |
| 42 | |
| 43 | if (!output) { |
| 44 | DISPLAY("ERROR: Not enough memory!\n"); |
| 45 | return 1; |
| 46 | } |
| 47 | |
| 48 | { |
| 49 | size_t const ret = ZSTD_decompress(output, size, COMPRESSED, COMPRESSED_SIZE); |
| 50 | if (ZSTD_isError(ret)) { |
| 51 | if (ret == ZSTD_error_prefix_unknown) { |
| 52 | DISPLAY("ERROR: Invalid frame magic number, was this compiled " |
| 53 | "without legacy support?\n"); |
| 54 | } else { |
| 55 | DISPLAY("ERROR: %s\n", ZSTD_getErrorName(ret)); |
| 56 | } |
| 57 | return 1; |
| 58 | } |
| 59 | if (ret != size) { |
| 60 | DISPLAY("ERROR: Wrong decoded size\n"); |
| 61 | } |
| 62 | } |
| 63 | if (memcmp(EXPECTED, output, size) != 0) { |
| 64 | DISPLAY("ERROR: Wrong decoded output produced\n"); |
| 65 | return 1; |
| 66 | } |
| 67 | |
Przemyslaw Skibinski | 346ce32 | 2017-02-21 20:10:21 +0100 | [diff] [blame^] | 68 | free(output); |
Sean Purcell | f07ddf8 | 2017-02-08 13:56:32 -0800 | [diff] [blame] | 69 | DISPLAY("Simple API OK\n"); |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | int testStreamingAPI(void) |
| 74 | { |
| 75 | size_t const outBuffSize = ZSTD_DStreamOutSize(); |
| 76 | char* const outBuff = malloc(outBuffSize); |
| 77 | ZSTD_DStream* const stream = ZSTD_createDStream(); |
| 78 | ZSTD_inBuffer input = { COMPRESSED, COMPRESSED_SIZE, 0 }; |
| 79 | size_t outputPos = 0; |
| 80 | int needsInit = 1; |
| 81 | |
| 82 | if (outBuff == NULL) { |
| 83 | DISPLAY("ERROR: Could not allocate memory\n"); |
| 84 | return 1; |
| 85 | } |
| 86 | if (stream == NULL) { |
| 87 | DISPLAY("ERROR: Could not create dstream\n"); |
| 88 | return 1; |
| 89 | } |
| 90 | |
| 91 | while (1) { |
| 92 | ZSTD_outBuffer output = {outBuff, outBuffSize, 0}; |
| 93 | if (needsInit) { |
| 94 | size_t const ret = ZSTD_initDStream(stream); |
| 95 | if (ZSTD_isError(ret)) { |
| 96 | DISPLAY("ERROR: %s\n", ZSTD_getErrorName(ret)); |
| 97 | return 1; |
| 98 | } |
| 99 | } |
| 100 | { |
| 101 | size_t const ret = ZSTD_decompressStream(stream, &output, &input); |
| 102 | if (ZSTD_isError(ret)) { |
| 103 | DISPLAY("ERROR: %s\n", ZSTD_getErrorName(ret)); |
| 104 | return 1; |
| 105 | } |
| 106 | |
| 107 | if (ret == 0) { |
| 108 | needsInit = 1; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | if (memcmp(outBuff, EXPECTED + outputPos, output.pos) != 0) { |
| 113 | DISPLAY("ERROR: Wrong decoded output produced\n"); |
| 114 | return 1; |
| 115 | } |
| 116 | outputPos += output.pos; |
| 117 | if (input.pos == input.size && output.pos < output.size) { |
| 118 | break; |
| 119 | } |
| 120 | } |
| 121 | |
Przemyslaw Skibinski | 346ce32 | 2017-02-21 20:10:21 +0100 | [diff] [blame^] | 122 | free(outBuff); |
| 123 | ZSTD_freeDStream(stream); |
Sean Purcell | f07ddf8 | 2017-02-08 13:56:32 -0800 | [diff] [blame] | 124 | DISPLAY("Streaming API OK\n"); |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | int main(void) |
| 129 | { |
| 130 | int ret; |
| 131 | |
| 132 | ret = testSimpleAPI(); |
| 133 | if (ret) return ret; |
| 134 | ret = testStreamingAPI(); |
| 135 | if (ret) return ret; |
| 136 | |
| 137 | DISPLAY("OK\n"); |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | /* Consists of the "EXPECTED" string compressed with default settings on |
| 143 | - v0.4.3 |
| 144 | - v0.5.0 |
| 145 | - v0.6.0 |
| 146 | - v0.7.0 |
| 147 | - v0.8.0 |
| 148 | */ |
| 149 | const char* const COMPRESSED = |
| 150 | "\x24\xB5\x2F\xFD\x00\x00\x00\xBB\xB0\x02\xC0\x10\x00\x1E\xB0\x01" |
| 151 | "\x02\x00\x00\x80\x00\xE8\x92\x34\x12\x97\xC8\xDF\xE9\xF3\xEF\x53" |
| 152 | "\xEA\x1D\x27\x4F\x0C\x44\x90\x0C\x8D\xF1\xB4\x89\x17\x00\x18\x00" |
| 153 | "\x18\x00\x3F\xE6\xE2\xE3\x74\xD6\xEC\xC9\x4A\xE0\x71\x71\x42\x3E" |
| 154 | "\x64\x4F\x6A\x45\x4E\x78\xEC\x49\x03\x3F\xC6\x80\xAB\x8F\x75\x5E" |
| 155 | "\x6F\x2E\x3E\x7E\xC6\xDC\x45\x69\x6C\xC5\xFD\xC7\x40\xB8\x84\x8A" |
| 156 | "\x01\xEB\xA8\xD1\x40\x39\x90\x4C\x64\xF8\xEB\x53\xE6\x18\x0B\x67" |
| 157 | "\x12\xAD\xB8\x99\xB3\x5A\x6F\x8A\x19\x03\x01\x50\x67\x56\xF5\x9F" |
| 158 | "\x35\x84\x60\xA0\x60\x91\xC9\x0A\xDC\xAB\xAB\xE0\xE2\x81\xFA\xCF" |
| 159 | "\xC6\xBA\x01\x0E\x00\x54\x00\x00\x19\x00\x00\x54\x14\x00\x24\x24" |
| 160 | "\x04\xFE\x04\x84\x4E\x41\x00\x27\xE2\x02\xC4\xB1\x00\xD2\x51\x00" |
| 161 | "\x79\x58\x41\x28\x00\xE0\x0C\x01\x68\x65\x00\x04\x13\x0C\xDA\x0C" |
| 162 | "\x80\x22\x06\xC0\x00\x00\x25\xB5\x2F\xFD\x00\x00\x00\xAD\x12\xB0" |
| 163 | "\x7D\x1E\xB0\x01\x02\x00\x00\x80\x00\xE8\x92\x34\x12\x97\xC8\xDF" |
| 164 | "\xE9\xF3\xEF\x53\xEA\x1D\x27\x4F\x0C\x44\x90\x0C\x8D\xF1\xB4\x89" |
| 165 | "\x03\x01\x50\x67\x56\xF5\x9F\x35\x84\x60\xA0\x60\x91\xC9\x0A\xDC" |
| 166 | "\xAB\xAB\xE0\xE2\x81\xFA\xCF\xC6\xBA\xEB\xA8\xD1\x40\x39\x90\x4C" |
| 167 | "\x64\xF8\xEB\x53\xE6\x18\x0B\x67\x12\xAD\xB8\x99\xB3\x5A\x6F\x8A" |
| 168 | "\xF9\x63\x0C\xB8\xFA\x58\xE7\xF5\xE6\xE2\xE3\x67\xCC\x5D\x94\xC6" |
| 169 | "\x56\xDC\x7F\x0C\x84\x4B\xA8\xF8\x63\x2E\x3E\x4E\x67\xCD\x9E\xAC" |
| 170 | "\x04\x1E\x17\x27\xE4\x43\xF6\xA4\x56\xE4\x84\xC7\x9E\x34\x0E\x00" |
| 171 | "\x00\x32\x40\x80\xA8\x00\x01\x49\x81\xE0\x3C\x01\x29\x1D\x00\x87" |
| 172 | "\xCE\x80\x75\x08\x80\x72\x24\x00\x7B\x52\x00\x94\x00\x20\xCC\x01" |
| 173 | "\x86\xD2\x00\x81\x09\x83\xC1\x34\xA0\x88\x01\xC0\x00\x00\x26\xB5" |
| 174 | "\x2F\xFD\x42\xEF\x00\x00\xA6\x12\xB0\x7D\x1E\xB0\x01\x02\x00\x00" |
| 175 | "\x54\xA0\xBA\x24\x8D\xC4\x25\xF2\x77\xFA\xFC\xFB\x94\x7A\xC7\xC9" |
| 176 | "\x13\x03\x11\x24\x43\x63\x3C\x6D\x22\x03\x01\x50\x67\x56\xF5\x9F" |
| 177 | "\x35\x84\x60\xA0\x60\x91\xC9\x0A\xDC\xAB\xAB\xE0\xE2\x81\xFA\xCF" |
| 178 | "\xC6\xBA\xEB\xA8\xD1\x40\x39\x90\x4C\x64\xF8\xEB\x53\xE6\x18\x0B" |
| 179 | "\x67\x12\xAD\xB8\x99\xB3\x5A\x6F\x8A\xF9\x63\x0C\xB8\xFA\x58\xE7" |
| 180 | "\xF5\xE6\xE2\xE3\x67\xCC\x5D\x94\xC6\x56\xDC\x7F\x0C\x84\x4B\xA8" |
| 181 | "\xF8\x63\x2E\x3E\x4E\x67\xCD\x9E\xAC\x04\x1E\x17\x27\xE4\x43\xF6" |
| 182 | "\xA4\x56\xE4\x84\xC7\x9E\x34\x0E\x00\x35\x0B\x71\xB5\xC0\x2A\x5C" |
| 183 | "\x26\x94\x22\x20\x8B\x4C\x8D\x13\x47\x58\x67\x15\x6C\xF1\x1C\x4B" |
| 184 | "\x54\x10\x9D\x31\x50\x85\x4B\x54\x0E\x01\x4B\x3D\x01\xC0\x00\x00" |
| 185 | "\x27\xB5\x2F\xFD\x20\xEF\x00\x00\xA6\x12\xE4\x84\x1F\xB0\x01\x10" |
| 186 | "\x00\x00\x00\x35\x59\xA6\xE7\xA1\xEF\x7C\xFC\xBD\x3F\xFF\x9F\xEF" |
| 187 | "\xEE\xEF\x61\xC3\xAA\x31\x1D\x34\x38\x22\x22\x04\x44\x21\x80\x32" |
| 188 | "\xAD\x28\xF3\xD6\x28\x0C\x0A\x0E\xD6\x5C\xAC\x19\x8D\x20\x5F\x45" |
| 189 | "\x02\x2E\x17\x50\x66\x6D\xAC\x8B\x9C\x6E\x07\x73\x46\xBB\x44\x14" |
| 190 | "\xE7\x98\xC3\xB9\x17\x32\x6E\x33\x7C\x0E\x21\xB1\xDB\xCB\x89\x51" |
| 191 | "\x23\x34\xAB\x9D\xBC\x6D\x20\xF5\x03\xA9\x91\x4C\x2E\x1F\x59\xDB" |
| 192 | "\xD9\x35\x67\x4B\x0C\x95\x79\x10\x00\x85\xA6\x96\x95\x2E\xDF\x78" |
| 193 | "\x7B\x4A\x5C\x09\x76\x97\xD1\x5C\x96\x12\x75\x35\xA3\x55\x4A\xD4" |
| 194 | "\x0B\x00\x35\x0B\x71\xB5\xC0\x2A\x5C\xE6\x08\x45\xF1\x39\x43\xF1" |
| 195 | "\x1C\x4B\x54\x10\x9D\x31\x50\x85\x4B\x54\x0E\x01\x4B\x3D\x01\xC0" |
| 196 | "\x00\x00\x28\xB5\x2F\xFD\x24\xEF\x35\x05\x00\x92\x0B\x21\x1F\xB0" |
| 197 | "\x01\x10\x00\x00\x00\x35\x59\xA6\xE7\xA1\xEF\x7C\xFC\xBD\x3F\xFF" |
| 198 | "\x9F\xEF\xEE\xEF\x61\xC3\xAA\x31\x1D\x34\x38\x22\x22\x04\x44\x21" |
| 199 | "\x80\x32\xAD\x28\xF3\xD6\x28\x0C\x0A\x0E\xD6\x5C\xAC\x19\x8D\x20" |
| 200 | "\x5F\x45\x02\x2E\x17\x50\x66\x6D\xAC\x8B\x9C\x6E\x07\x73\x46\xBB" |
| 201 | "\x44\x14\xE7\x98\xC3\xB9\x17\x32\x6E\x33\x7C\x0E\x21\xB1\xDB\xCB" |
| 202 | "\x89\x51\x23\x34\xAB\x9D\xBC\x6D\x20\xF5\x03\xA9\x91\x4C\x2E\x1F" |
| 203 | "\x59\xDB\xD9\x35\x67\x4B\x0C\x95\x79\x10\x00\x85\xA6\x96\x95\x2E" |
| 204 | "\xDF\x78\x7B\x4A\x5C\x09\x76\x97\xD1\x5C\x96\x12\x75\x35\xA3\x55" |
| 205 | "\x4A\xD4\x0B\x00\x35\x0B\x71\xB5\xC0\x2A\x5C\xE6\x08\x45\xF1\x39" |
| 206 | "\x43\xF1\x1C\x4B\x54\x10\x9D\x31\x50\x85\x4B\x54\x0E\x01\x4B\x3D" |
| 207 | "\x01\xD2\x2F\x21\x80"; |
| 208 | |
| 209 | const char* const EXPECTED = |
| 210 | "snowden is snowed in / he's now then in his snow den / when does the snow end?\n" |
| 211 | "goodbye little dog / you dug some holes in your day / they'll be hard to fill.\n" |
| 212 | "when life shuts a door, / just open it. it’s a door. / that is how doors work.\n" |
| 213 | |
| 214 | "snowden is snowed in / he's now then in his snow den / when does the snow end?\n" |
| 215 | "goodbye little dog / you dug some holes in your day / they'll be hard to fill.\n" |
| 216 | "when life shuts a door, / just open it. it’s a door. / that is how doors work.\n" |
| 217 | |
| 218 | "snowden is snowed in / he's now then in his snow den / when does the snow end?\n" |
| 219 | "goodbye little dog / you dug some holes in your day / they'll be hard to fill.\n" |
| 220 | "when life shuts a door, / just open it. it’s a door. / that is how doors work.\n" |
| 221 | |
| 222 | "snowden is snowed in / he's now then in his snow den / when does the snow end?\n" |
| 223 | "goodbye little dog / you dug some holes in your day / they'll be hard to fill.\n" |
| 224 | "when life shuts a door, / just open it. it’s a door. / that is how doors work.\n" |
| 225 | |
| 226 | "snowden is snowed in / he's now then in his snow den / when does the snow end?\n" |
| 227 | "goodbye little dog / you dug some holes in your day / they'll be hard to fill.\n" |
| 228 | "when life shuts a door, / just open it. it’s a door. / that is how doors work.\n"; |
| 229 | |