xref: /aosp_15_r20/external/zstd/tests/loremOut.c (revision 01826a4963a0d8a59bc3812d29bdf0fb76416722)
1*01826a49SYabin Cui /*
2*01826a49SYabin Cui  * Copyright (c) Meta Platforms, Inc. and affiliates.
3*01826a49SYabin Cui  * All rights reserved.
4*01826a49SYabin Cui  *
5*01826a49SYabin Cui  * This source code is licensed under both the BSD-style license (found in the
6*01826a49SYabin Cui  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7*01826a49SYabin Cui  * in the COPYING file in the root directory of this source tree).
8*01826a49SYabin Cui  * You may select, at your option, one of the above-listed licenses.
9*01826a49SYabin Cui  */
10*01826a49SYabin Cui 
11*01826a49SYabin Cui /* Implementation notes:
12*01826a49SYabin Cui  * Generates a stream of Lorem ipsum paragraphs to stdout,
13*01826a49SYabin Cui  * up to the requested size, which can be very large (> 4 GB).
14*01826a49SYabin Cui  * Note that, beyond 1 paragraph, this generator produces
15*01826a49SYabin Cui  * a different content than LOREM_genBuffer (even when using same seed).
16*01826a49SYabin Cui  */
17*01826a49SYabin Cui 
18*01826a49SYabin Cui #include "loremOut.h"
19*01826a49SYabin Cui #include <assert.h>
20*01826a49SYabin Cui #include <stdio.h>
21*01826a49SYabin Cui #include "lorem.h"    /* LOREM_genBlock */
22*01826a49SYabin Cui #include "platform.h" /* Compiler options, SET_BINARY_MODE */
23*01826a49SYabin Cui 
24*01826a49SYabin Cui #define MIN(a, b) ((a) < (b) ? (a) : (b))
25*01826a49SYabin Cui #define LOREM_BLOCKSIZE (1 << 10)
LOREM_genOut(unsigned long long size,unsigned seed)26*01826a49SYabin Cui void LOREM_genOut(unsigned long long size, unsigned seed)
27*01826a49SYabin Cui {
28*01826a49SYabin Cui     char buff[LOREM_BLOCKSIZE] = { 0 };
29*01826a49SYabin Cui     unsigned long long total   = 0;
30*01826a49SYabin Cui     size_t genBlockSize        = (size_t)MIN(size, LOREM_BLOCKSIZE);
31*01826a49SYabin Cui 
32*01826a49SYabin Cui     /* init */
33*01826a49SYabin Cui     SET_BINARY_MODE(stdout);
34*01826a49SYabin Cui 
35*01826a49SYabin Cui     /* Generate Ipsum text, one paragraph at a time */
36*01826a49SYabin Cui     while (total < size) {
37*01826a49SYabin Cui         size_t generated =
38*01826a49SYabin Cui                 LOREM_genBlock(buff, genBlockSize, seed++, total == 0, 0);
39*01826a49SYabin Cui         assert(generated <= genBlockSize);
40*01826a49SYabin Cui         total += generated;
41*01826a49SYabin Cui         assert(total <= size);
42*01826a49SYabin Cui         fwrite(buff,
43*01826a49SYabin Cui                1,
44*01826a49SYabin Cui                generated,
45*01826a49SYabin Cui                stdout); /* note: should check potential write error */
46*01826a49SYabin Cui         if (size - total < genBlockSize)
47*01826a49SYabin Cui             genBlockSize = (size_t)(size - total);
48*01826a49SYabin Cui     }
49*01826a49SYabin Cui     assert(total == size);
50*01826a49SYabin Cui }
51