LCOV - code coverage report
Current view: top level - ballet/zstd - fd_gzip_pack.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 2 0.0 %
Date: 2026-08-14 04:54:57 Functions: 0 2 0.0 %

          Line data    Source code
       1             : /* fd_gzip_pack: build-time asset compressor.
       2             :      Usage: fd_gzip_pack <level> <in> <out> */
       3             : 
       4             : #include <stdio.h>
       5             : #include <stdlib.h>
       6             : 
       7             : #include "../../third_party/zlib/zlib.h"
       8             : 
       9             : #define DIE(...) do { fprintf( stderr, __VA_ARGS__ ); fputc( '\n', stderr ); exit( 1 ); } while( 0 )
      10             : 
      11             : /* zlib is vendored with Z_SOLO (no default allocator) */
      12           0 : static voidpf zalloc_( voidpf o, uInt n, uInt sz ) { (void)o; return calloc( n, sz ); }
      13           0 : static void   zfree_ ( voidpf o, voidpf p )        { (void)o; free( p ); }
      14             : 
      15             : int
      16             : main( int     argc,
      17             :       char ** argv ) {
      18             :   if( argc!=4 ) DIE( "usage: %s <level> <in> <out>", argv[0] );
      19             : 
      20             :   int level = atoi( argv[1] );
      21             :   if( level<1 || level>9 ) DIE( "bad level %s", argv[1] );
      22             : 
      23             :   FILE * in  = fopen( argv[2], "rb" );  if( !in  ) DIE( "open %s failed", argv[2] );
      24             :   FILE * out = fopen( argv[3], "wb" );  if( !out ) DIE( "open %s failed", argv[3] );
      25             : 
      26             :   z_stream strm = { .zalloc = zalloc_, .zfree = zfree_ };
      27             :   /* windowBits 15+16: deflate with gzip framing (RFC 1952) */
      28             :   if( deflateInit2( &strm, level, Z_DEFLATED, 15+16, 9, Z_DEFAULT_STRATEGY )!=Z_OK ) DIE( "deflateInit2 failed" );
      29             : 
      30             :   static unsigned char ibuf[ 1<<17 ];
      31             :   static unsigned char obuf[ 1<<17 ];
      32             : 
      33             :   for(;;) {
      34             :     size_t rd = fread( ibuf, 1UL, sizeof(ibuf), in );
      35             :     if( ferror( in ) ) DIE( "read %s failed", argv[2] );
      36             :     int flush = feof( in ) ? Z_FINISH : Z_NO_FLUSH;
      37             : 
      38             :     strm.next_in  = ibuf;
      39             :     strm.avail_in = (uInt)rd;
      40             :     do {
      41             :       strm.next_out  = obuf;
      42             :       strm.avail_out = (uInt)sizeof(obuf);
      43             :       if( deflate( &strm, flush )==Z_STREAM_ERROR ) DIE( "deflate failed" );
      44             :       size_t wr = sizeof(obuf) - strm.avail_out;
      45             :       if( wr && fwrite( obuf, 1UL, wr, out )!=wr ) DIE( "write %s failed", argv[3] );
      46             :     } while( !strm.avail_out );
      47             :     if( flush==Z_FINISH ) break;
      48             :   }
      49             : 
      50             :   deflateEnd( &strm );
      51             :   fclose( in );
      52             :   if( fclose( out ) ) DIE( "close %s failed", argv[3] );
      53             :   return 0;
      54             : }

Generated by: LCOV version 1.14