Jump to content


Check out our Community Blogs

sakishrist

Member Since 04 Dec 2007
Offline Last Active Feb 18 2013 01:16 AM
-----

Topics I've Started

[Linux] For loop leads to segmentation fault

29 June 2011 - 07:46 AM

Hello everyone,

I would like a little help with some code of mine. The problem is not just what is written at the title of the thread, there is one more.

First of all, here is the code (if you need to know what the file that I process contains, then I will post that information too):
#include <iostream>
#include <zlib.h>
#include <fstream>

using namespace std;

int _decompress(Bytef * in_data, long in_size, Bytef * out_data, long * out_size){

    z_stream z;

    int status;
    
    z.zalloc = Z_NULL;
    z.zfree = Z_NULL;
    z.opaque = Z_NULL;
    
    z.avail_in = in_size;
    z.next_in = in_data;
    
    z.avail_out = *out_size;
    z.next_out = out_data;

    status = inflateInit(&z);   [B][COLOR=Red] //SEGMENTATION FAULT[/COLOR][/B]

    if ( status != Z_OK )
        return status;
    
    if ( z.avail_in != 0 && *out_size != 0 ) {
        status = inflate( &z, Z_NO_FLUSH );    [B][COLOR=Red]//SEGMENTATION FAULT[/COLOR][/B]
        if ( status < 0 ){
            cout << "Error: " << status << endl;
            return -11;
        }
        else if ( status == Z_OK && *out_size > 0 ){
            cout << "Not enough output space!\n";
            return -12;
        }

    }else if( *out_size != 0 ){
        cout << "No input?\n";
        return -10;
    }
    
    while ( status == Z_OK  ) {
        
        *out_size += 1024;
        z.avail_out = *out_size;
        delete out_data;
        out_data = new Bytef[*out_size];
        z.next_out = out_data;
        
        status = inflate( &z, Z_NO_FLUSH );    [B][COLOR=Red]//SEGMENTATION FAULT[/COLOR][/B]
    }
    
    if ( status != Z_STREAM_END );
        cout << "Final Error: " << status << endl;

    (void)inflateEnd(&z);
    
    return status;
} 



int main(){
    ifstream fp_in;
    fp_in.open("r.0.0.mcr", ios::in);
    
    char *a = new char[5]; 


    for(int i = 0; i<1024; i++){ 
        
        fp_in.seekg(i*4);
        fp_in.read(a, 4); 
        cout << i << ":";
        
        if((int)a[0]!=0 || (int)a[1]!=0 || (int)a[2]!=0 || (int)a[3]!=0){
            
            cout     << (i+1)%32 
                    << "x" 
                    << (i+1)/32 
                    << " ( Pos: " 
                    << (  (int)(unsigned char)a[0]*65536 
                        + (int)(unsigned char)a[1]*255 
                        + (int)(unsigned char)a[2]            )*4096 
                    << " Size: ";

            fp_in.seekg((      (int)(unsigned char)a[0]*65536 
                             + (int)(unsigned char)a[1]*256 
                             + (int)(unsigned char)a[2]            )*4096);
            

            fp_in.read(a,5);

            long size =   (int)(unsigned char)a[0]*16777216 
                        + (int)(unsigned char)a[1]*65536 
                        + (int)(unsigned char)a[2]*256 
                        + (int)(unsigned char)a[3];
            
            cout << (int)(unsigned char)a[0] << ":" ;        [B][COLOR=Red]//PROBLEM WITH COUT[/COLOR][/B]
            cout << (int)(unsigned char)a[1] << ":" ;
            cout << (int)(unsigned char)a[2] << ":" ;
            cout << (int)(unsigned char)a[3] << " = "; 
            cout << size << " ) \t:";

            char *buff = new char[size-1];

            long out_size = 0; 
            Bytef *out;
            
            fp_in.read(buff,size-1);
            int status = _decompress((Bytef*)buff,size-1,out,&out_size);

            if(status>=0)
                cout << "Decompressed size: " << out_size << " bytes.";
            else
                cout << "Error: " << status;
        }
        cout << endl;
    }

    for(int i = 0; i<1024; i++)        [B][COLOR=Red]//SEGMENTATION FAULT[/COLOR][/B]
        cout << "Anything";

    
    fp_in.close();

    return 0;
}
So, here are the problems I have

1) At the five lines with the "Problem with cout" comment, if I attempt to do that in one line I get a weird error that I think is from zlib, and not g++ (this is a runtime error):
*** glibc detected *** ./zlib_c: munmap_chunk(): invalid pointer: 0x00007f739d6f5048 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x76bb6)[0x7f739c826bb6]
./zlib_c[0x400f69]
./zlib_c[0x4013e8]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xff)[0x7f739c7ceeff]
./zlib_c[0x400d19]
======= Memory map: ========
00400000-00402000 r-xp 00000000 08:03 3014917                            /home/sakishrist/Desktop/c++/zlib/zlib_c
00601000-00602000 r--p 00001000 08:03 3014917                            /home/sakishrist/Desktop/c++/zlib/zlib_c
00602000-00603000 rw-p 00002000 08:03 3014917                            /home/sakishrist/Desktop/c++/zlib/zlib_c
00d2e000-00d4f000 rw-p 00000000 00:00 0                                  [heap]
7f739c7b0000-7f739c93a000 r-xp 00000000 08:03 265932                     /lib/x86_64-linux-gnu/libc-2.13.so
7f739c93a000-7f739cb39000 ---p 0018a000 08:03 265932                     /lib/x86_64-linux-gnu/libc-2.13.so
7f739cb39000-7f739cb3d000 r--p 00189000 08:03 265932                     /lib/x86_64-linux-gnu/libc-2.13.so
7f739cb3d000-7f739cb3e000 rw-p 0018d000 08:03 265932                     /lib/x86_64-linux-gnu/libc-2.13.so
7f739cb3e000-7f739cb44000 rw-p 00000000 00:00 0 
7f739cb44000-7f739cb59000 r-xp 00000000 08:03 265960                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7f739cb59000-7f739cd58000 ---p 00015000 08:03 265960                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7f739cd58000-7f739cd59000 r--p 00014000 08:03 265960                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7f739cd59000-7f739cd5a000 rw-p 00015000 08:03 265960                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7f739cd5a000-7f739cdde000 r-xp 00000000 08:03 265969                     /lib/x86_64-linux-gnu/libm-2.13.so
7f739cdde000-7f739cfdd000 ---p 00084000 08:03 265969                     /lib/x86_64-linux-gnu/libm-2.13.so
7f739cfdd000-7f739cfde000 r--p 00083000 08:03 265969                     /lib/x86_64-linux-gnu/libm-2.13.so
7f739cfde000-7f739cfdf000 rw-p 00084000 08:03 265969                     /lib/x86_64-linux-gnu/libm-2.13.so
7f739cfdf000-7f739d0c7000 r-xp 00000000 08:03 3416616                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.14
7f739d0c7000-7f739d2c6000 ---p 000e8000 08:03 3416616                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.14
7f739d2c6000-7f739d2ce000 r--p 000e7000 08:03 3416616                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.14
7f739d2ce000-7f739d2d0000 rw-p 000ef000 08:03 3416616                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.14
7f739d2d0000-7f739d2e5000 rw-p 00000000 00:00 0 
7f739d2e5000-7f739d2fd000 r-xp 00000000 08:03 3824230                    /usr/local/lib/libz.so.1.2.5
7f739d2fd000-7f739d4fc000 ---p 00018000 08:03 3824230                    /usr/local/lib/libz.so.1.2.5
7f739d4fc000-7f739d4fd000 r--p 00017000 08:03 3824230                    /usr/local/lib/libz.so.1.2.5
7f739d4fd000-7f739d4fe000 rw-p 00018000 08:03 3824230                    /usr/local/lib/libz.so.1.2.5
7f739d4fe000-7f739d51f000 r-xp 00000000 08:03 265919                     /lib/x86_64-linux-gnu/ld-2.13.so
7f739d6f2000-7f739d6f7000 rw-p 00000000 00:00 0 
7f739d71b000-7f739d71e000 rw-p 00000000 00:00 0 
7f739d71e000-7f739d71f000 r--p 00020000 08:03 265919                     /lib/x86_64-linux-gnu/ld-2.13.so
7f739d71f000-7f739d721000 rw-p 00021000 08:03 265919                     /lib/x86_64-linux-gnu/ld-2.13.so
7fffb9a67000-7fffb9a88000 rw-p 00000000 00:00 0                          [stack]
7fffb9bfd000-7fffb9bfe000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
Aborted
2) At the end, there is a for loop. That loop produces a Segmentation Fault. If I remove it, everything is fine. I also tried to remove all the three lines that have the "Segmentation Fault" comment. The Segmentation Fault is gone then as well. So my guess is that this has to do with zlib but I have absolutely no idea how a for loop could be related to zlib.

I appreciate any help with any of these problems!

Thanks in advance!

[Linux] Convert and then convert again and ...

19 June 2011 - 07:13 AM

Hello everybody,

I am having a problem (or lots of that type) finding a working way to convert some char(s) to Bytef type (used by the zlib).

I am kind of new to C++ programing and syntax so I might be missing something obvious.

Anyhow, here is my program:
#include <iostream>
#include <zlib.h>

using namespace std;

void _compress(unsigned char & in_data, size_t in_size, unsigned char & out_data, size_t out_size){

z_stream z;

int status;

    z.avail_in = 0;
    z.next_out = &out_data;
    z.avail_out = out_size;

        if ( z.avail_in == 0 ) {
            z.next_in = &in_data;
            z.avail_in = in_size; //dfjngdfng
        }
        if ( z.avail_in != 0 )
            status = deflate( &z, Z_NO_FLUSH );
        int count = out_size - z.avail_out;

} 



int main(){
    unsigned char a[100];//13
    a=reinterpret_cast<unsigned char>("Hello world!");
    unsigned char b[100];
    _compress(*b,13,*b,100);
    cout << b << endl;
    return 0;
}
My goal is to make an easy-to-use function that will compress (and later de-compress) a buffer.

The error I get with this last compilation attempt is:
sakishrist@sakishrist-laptop:~/Desktop/c++/zlib$ g++ zlib.cpp -o  zlib_c -lz
zlib.cpp: In function ‘int main()’:
zlib.cpp:42:50: error: cast from ‘const char*’ to ‘unsigned char’ loses precision
zlib.cpp:42:50: error: incompatible types in assignment of ‘unsigned char’ to ‘unsigned char [100]’
I tried many different things but all of them fail. I think (I can be wrong with so many errors) that I know how to use * and & in general.

I hope you can give me a piece of advice. :)

Thanks in advance!!!

Is Perl trying to ... predict my moves?

12 April 2011 - 03:52 PM

Hello,

I have a weird problem, an "Out of memory" one. The weird thing is that the script I have is not even started when I get that error (I mean, I have a print line in the very beginning and it is not printed) and the memory meter does not move at all. Please help me with this situation.

My OS is Ubuntu.

I have marked the lines that when removed the "Out of memory" error is gone:
#!/usr/bin/perl






sub dictionary
{

    while (($n = read FILE, $data, 1) != 0) { 
        $nr += $n;
    
        if($data eq "e") {return;}
        if($data eq "d") {dictionary();}
        if($data eq "l") {list();}
        if($data eq "i") {integer();}

        if($data eq ":"){
            print LOG $buf."\n\n";
            [COLOR=red]$n = read FILE, $data, $buf;[/COLOR]
            $nr += $n;
            [COLOR=red]$buf="";[/COLOR]
            print LOG $data."\n\n";
            

        } [COLOR=red]else {
        $buf .= $data;}[/COLOR]
    }
    
}

sub list
{

    while (($n = read FILE, $data, 1) != 0) { 
        $nr += $n;
    
        if($data eq "e") {return;}
        if($data eq "d") {dictionary();}
        if($data eq "l") {list();}
        if($data eq "i") {integer();}

        if($data eq ":"){
            print LOG $buf."\n\n";
            [COLOR=red]$n = read FILE, $data, $buf;[/COLOR]
            $nr += $n;
            [COLOR=red]$buf="";[/COLOR]
            print LOG $data."\n\n";
            

        } [COLOR=red]else {
        $buf .= $data;}[/COLOR]
    }

}

sub integer
{

    while (($n = read FILE, $data, 1) != 0) { 
        $nr += $n;
    
        if($data eq "e") {return;}

        $buf .= $data;
        
    }
    #print LOG $buf."\n\n";
    print $n."\n";
}

{
print "started";
open FILE, "test.torrent" or die $!;
open LOG, ">log" or die $!;
binmode FILE;

my ($buf, $data, $n, $nr);
$nr=0;

while (($n = read FILE, $data, 1) != 0){
    
    if($data eq "d") {dictionary();}
    if($data eq "l") {list();}
    if($data eq "i") {integer();}
}

close(FILE);
close(LOG);
}
The purpose of the file is to read a torrent file section by section and it is incomplete.

Thanks in advance

Zlib decompression

13 March 2011 - 03:54 AM

Greetings!

I have a little problem decompressing some data with zlib. The thing is that I am new to Java and the problem is not so much about the zlib itself but the more basics of the language.

So, I have a file that is partially compressed. Actually it is something like this:
[COLOR=lime][INFO ABOUT DATA][/COLOR][COLOR=red][COMPRESSED DATA][/COLOR][COLOR=lime][INFO ABOUT DATA][/COLOR][COLOR=red][COMPRESSED DATA][/COLOR][COLOR=lime][INFO ABOUT DATA][/COLOR][COLOR=red][COMPRESSED DATA] [COLOR=black]...[/COLOR][/COLOR]


Now then, I need to decompress those parts. I tried to do so with
GZIPInputStream but it requires me to pass a file stream. So I am wondering how I should pass only those parts as a stream or if there is another method to do that.

Thanks in advance!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download