473,396 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

bunzip2 when exec()-ed from perl script outputs garbage data.

Hello,
When the following perl script is executed:

#!/usr/bin/env perl
use strict;
use diagnostics;
use warnings;

# Header bytes for different zip formats
my $GZIP_HEADER = "\x1f\x8b\x08\x08";
my $BZIP_HEADER = "BZh9"; # 42 5a 68 39

my $header_bytes;

read STDIN, $header_bytes, 4 or die "Trouble reading input: $!";
if ($header_bytes eq $GZIP_HEADER) {
exec "gunzip -f";
die "gunzip doesn't exist or can't be accessed: $!";
} elsif ($header_bytes eq $BZIP_HEADER) {
exec "bunzip2 -f";
die "bunzip2 doesn't exist or can't be accessed: $!";
} else { die "Not a proper zip file or unsupported zip format." }

I get output like this:
BZh91AY (blah blah blah.... I can't copy it because it contains NULs)

I've used print statements to prove that it executed bzip2 and found the
correct magic number.
Jul 19 '05 #1
2 3919
Thomas Covello wrote:

read STDIN, $header_bytes, 4 or die "Trouble reading input: $!";
You have consumed four bytes from the input stream.
exec "gunzip -f";
exec "bunzip2 -f";


These program need to see the entire file, including the
four bytes that you consumed. You'll have to put those
four bytes back into the stream that the program reads.
-Joe
Jul 19 '05 #2
[Followup-To set to COL]

In comp.os.linux Thomas Covello <pm********@shaw.ca> wrote:
read STDIN, $header_bytes, 4 or die "Trouble reading input: $!";
if ($header_bytes eq $GZIP_HEADER) {
exec "gunzip -f";
die "gunzip doesn't exist or can't be accessed: $!";
} elsif ($header_bytes eq $BZIP_HEADER) {
exec "bunzip2 -f";
die "bunzip2 doesn't exist or can't be accessed: $!";
} else { die "Not a proper zip file or unsupported zip format." }


You have a fundamental flaw. You are reading in 4 bytes from stdin, and
then execing gunzip or bunzip, which will read in the data, but it WON'T
get the 4 bytes you've already read in, therefore the header won't be
recognised, and you'll get an error, or something weird.

You need to either find some way of peeking the contents of stdin
(without removing it from the queue (you'll likely need to use sysread),
or don't take the input from stdin. Take it from a file instead, that

Also, if you don't redirect the output of this program, you're going to
get (very likely) binary gibberish being output. But you probably knew
that part already.

--
Cameron Kerr
ca**********@paradise.net.nz : http://nzgeeks.org/cameron/
Empowered by Perl!
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: serge | last post by:
Using "SELECT * " is a bad practice even when using a VIEW instead of a table? I have some stored procedures that are identical with the difference of one statement in the WHERE clause. If I...
4
by: prasad | last post by:
I am getting sql error during binding a program which access a temporary table. The temporary table declaration and access methods are given below. EXEC SQL DECLARE GLOBAL TEMPORARY TABLE TEM88...
5
by: snicks | last post by:
I'm trying to exec a program external to my ASP.NET app using the following code. The external app is a VB.NET application. Dim sPPTOut As String sPPTOut = MDEPDirStr + sID + ".ppt" Dim p As...
2
by: Uldis Bojars | last post by:
Hi All, I have encountered problems with JS RegExp.exec() and can't find what is the problem. Could you help me? formRequest is a function that extracts some information from XMLHTTPRequest...
4
by: comp.lang.php | last post by:
I wrote my own version of memory_get_usage() if that function is not available: if (!function_exists('memory_get_usage')) { /** * Determine the amount of memory you are allowed to have * *...
2
by: xml0x1a | last post by:
How do I use exec? Python 2.4.3 ---- from math import * G = 1 def d(): L = 1 exec "def f(x): return L + log(G) " in globals(), locals() f(1)
1
by: Matimus | last post by:
I think I'm going to create a new issue in Pythons issue database, but I wanted to run it by the news group first. See if I can get any useful feed back. The following session demonstrates the...
0
by: Gordon Fraser | last post by:
Hi, I'm trying to parse Python code to an AST, apply some changes to the AST and then compile and run the AST, but I'm running into problems when trying to evaluate/execute the resulting code...
0
by: Orestis Markou | last post by:
Have you tried passing in empty dicts for globals and locals? I think that the defaults will be the *current* globals and locals, and then of course your namespace is broken... On Tue, Oct 7,...
0
by: Chris Rebert | last post by:
On Fri, Nov 14, 2008 at 10:40 AM, Indian <write2abdul@gmail.comwrote: You don't need and shouldn't be using `exec` here. It appears as though you're just doing the following in a much more obtuse...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.