From owner-freebsd-hackers@freebsd.org Thu Oct 4 03:58:38 2018 Return-Path: Delivered-To: freebsd-hackers@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 6E15510BE343 for ; Thu, 4 Oct 2018 03:58:38 +0000 (UTC) (envelope-from khanzf@gmail.com) Received: from mail-it1-x12d.google.com (mail-it1-x12d.google.com [IPv6:2607:f8b0:4864:20::12d]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 081F78155E for ; Thu, 4 Oct 2018 03:58:38 +0000 (UTC) (envelope-from khanzf@gmail.com) Received: by mail-it1-x12d.google.com with SMTP id j81-v6so11356945ite.0 for ; Wed, 03 Oct 2018 20:58:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=k9bPql1rMP8+h0xKMgEMhTBjuAkTCTDxMSGzqUvG0hs=; b=aWEfyuh7NFChSzb5onDoD/Sp9XPlg7LS3eYNWtUEZ/n3FI+2EjQY+LSsKzvze8eI/Z SIClsHxCQU3Bx9v9mP9C3n00fc0hkkIJX4QpgScDbMESQzwrQ/msM91Wo0DeUQl/e+z1 3OWupwlVUazlexr/tXl1nsaDQsTu3rf0qsWBgOKiz47ObgeQzyaeHTC3wn3GhMLeltVN mecLNdq3QAktWSppA2tdjhQbhxgA708wUPPOPu+h/ux6CARdPWmb4VRYR4zE0Zmf7qZj 4MjwtOte6HwoKD7DHfa9j9rg+AzMFDIpgniv2CXL4EWtk6WLOhXk3Vs6CnXrDO1zY7Mq kA4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=k9bPql1rMP8+h0xKMgEMhTBjuAkTCTDxMSGzqUvG0hs=; b=jxHoA7Ljp01lvjmczJo846CCH9Nx/LekThoONMxFJltK8fk5vV7rYyouKJcvyVt/12 J740iYxBc+sNhZ59wNUfYpSirUASe9Vi6O6h3ns1ny4ijQTH4pinOsBA4+H3aiTveje3 9wyMi3iPkKUSTmx62Qe9j7hwImgCFOLgKH+dy+aOHRV7n3nsTLUSQS3J4I1SpFij8g+y BubPReQXwFIckwR9jplmZLB3L/Q8mrKInN/Oo1syswE1sT8LlxrfG4qX4lJJUJXVzNHT u2m2nOYaY4Cd2LESvtco1aAMkQNhxsIr+k9qaxmA6qzdQORgxFwwq/KWsAIPMoRfuLXD b9ZA== X-Gm-Message-State: ABuFfoii7aZDXyfsgEjZOYmxXKxrSDMQFjIYkqBmsRduBuODsTYNLYLf 8qgvsDBmhqZvq4G+BTD3iUjulRVtR5292GorRRHs7sTZ X-Google-Smtp-Source: ACcGV61mXqobX8PjVK3+cZAw0pPOtJ6grBLn5DZDk6Ry4ZagrHWBtxukDc7inhm5lS5PBMm3v9UAfaSWkGKiWUIKaAg= X-Received: by 2002:a02:aa99:: with SMTP id u25-v6mr3729336jai.73.1538625517195; Wed, 03 Oct 2018 20:58:37 -0700 (PDT) MIME-Version: 1.0 From: Farhan Khan Date: Wed, 3 Oct 2018 23:58:25 -0400 Message-ID: Subject: Problem with zlib deflation header To: freebsd-hackers@freebsd.org Content-Type: text/plain; charset="UTF-8" X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.27 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 04 Oct 2018 03:58:38 -0000 Hi all, I am trying to use zlib to deflate (compress?) data from a textfile.. It seems to work when I compress a file, but I am trying to prepend the zlib compressed file with custom header. Both the file and header should be compressed. However, when I add the header, the length of the compressed (deflated) file is much shorter than expected and comes out as an invalid zlib compressed object. The code works great, until I add the header block of code between the XXX comments below. The "FILE *source" variable is a sample file, I typically use /etc/passwd and the "char *header" is "blob 2172\0". Without the header block, the output is 904 bytes and deflatable (decompressable), but with the header it comes out to only 30 bytes. It also comes out as an invalid zlib object with the header block of code. Any ideas where I am making a mistake, specifically why the output is invalid and shorter *with* the header? Thanks Code below: ---------------------------- int zcompress_and_header(FILE *source, char *header) { int ret, flush; z_stream strm; unsigned int have; unsigned char in[Z_CHUNK]; unsigned char out[Z_CHUNK]; FILE *dest = stdout; // This is a temporary test strm.zalloc = Z_NULL; strm.zfree = Z_NULL; strm.opaque = Z_NULL; ret = deflateInit(&strm, Z_BEST_SPEED); //ret = deflateInit2(&strm, Z_BEST_SPEED, Z_DEFLATED, 15 | 16, 8, Z_DEFAULT_STRATEGY); if (ret != Z_OK) return ret; /* XXX Beginning of writing the header */ strm.next_in = (unsigned char *) header; strm.avail_in = strlen(header) + 1; do { strm.avail_out = Z_CHUNK; strm.next_out = out; if (deflate (& strm, Z_FINISH) < 0) { fprintf(stderr, "returned a bad status of.\n"); exit(0); } have = Z_CHUNK - strm.avail_out; fwrite(out, 1, have, stdout); } while(strm.avail_out == 0); /* XXX End of writing the header */ do { strm.avail_in = fread(in, 1, Z_CHUNK, source); if (ferror(source)) { (void)deflateEnd(&strm); return Z_ERRNO; } flush = feof(source) ? Z_FINISH : Z_NO_FLUSH; strm.next_in = in; do { strm.avail_out = Z_CHUNK; strm.next_out = out; ret = deflate(&strm, flush); have = Z_CHUNK - strm.avail_out; if (fwrite(out, 1, have, dest) != have || ferror(dest)) { (void)deflateEnd(&strm); return Z_ERRNO; } } while(strm.avail_out == 0); } while (flush != Z_FINISH); } // End of function ---------------------------- -- Farhan Khan PGP Fingerprint: B28D 2726 E2BC A97E 3854 5ABE 9A9F 00BC D525 16EE