Thread: Comperssion/Encoding

  1. #1
    Registered User Tarskin's Avatar
    Join Date
    Dec 2011
    Location
    The Hague, Netherland
    Posts
    6

    Comperssion/Encoding

    Dear People,

    I have been struggling with the following problem for about a week allready, which spans several C and Java programs (i believe the problem is in the final C program however) which i will try to distill in readable segments below.

    1. The true first input is a file containg a varying number of lines, where each line contains 2 floats seperated by a space.

    2. The first program compresses and encodes this file using the following code:

    Code:
      /* mz values */
      error=compress2(comp_buffer, &comp_length,(const Bytef*)data.mz ,(uLongf)length,Z_DEFAULT_COMPRESSION); /* compression */
      if (error != Z_OK) {fprintf(stderr,"zlib error..exiting"); exit(EXIT_FAILURE);}
      printf("Computational (m/z) length is %i\n",(int)comp_length); /* Length needed for uncompress later */
      mz_binary=g_base64_encode (comp_buffer,comp_length); /* encoding */
      mz_length=strlen(mz_binary);
      printf("mz: %s\n",mz_binary);
      printf("mz encoded length: %d\n",mz_length);
    3. This data is entered in a DB and a subsequent Java program retrieves specific data yielding a file that has a XML-like formatting with <something>value</something>. Important aspect here is that the length of the computational_buffer is printed in the XML-like file like
    Code:
    <decoded mz length>value</decoded mz length>
    .

    4. The C program i am struggling with attempts to restore the original data and so far i have come up with the following code (yielding a segmentation fault):

    Code:
    sscanf(spectral_buffer,"<decoded mz length>%i</decoded length>\n",&(glycan+teller)->decoded_mz_length);
            if (p_str=(char*)strstr(spectral_buffer,"<m/z>")) {
                p_begin=p_str+5;
                p_end=(char*)strstr(spectral_buffer,"</m/z>");
                length=p_end-p_begin;
                ((glycan+teller)->mz)=(char*)malloc(sizeof(char)*MAX_SIZE1);
                gchar *decoded_result;                                              /* pointer to memory for result in decoding */
                decoded_result=(char*)malloc(sizeof(char)*MAX_SIZE1);               /* allocating memory to the result 'block' */
                gsize decoded_mz_length;                                            /* variable for result length in decoding */
                strncpy(((glycan+teller)->mz),p_begin,length);
                *((glycan+teller)->mz+length)='\0';
                decoded_result = g_base64_decode((glycan+teller)->mz, &decoded_mz_length);   /* decoding */
                printf("length of decoded mz string is %i\n",decoded_mz_length);
                guchar *uncompressed_result;                                        /* pointer to memory for result of uncompress */
                uncompressed_result=(char*)malloc(sizeof(char)*MAX_SIZE1);          /* allocating memory to the result 'block' */
                uncompress(uncompressed_result,((glycan+teller)->decoded_mz_length),decoded_result,decoded_mz_length); /* MAGIC */
                printf("decoded & uncompressed string is %s\n",uncompressed_result);
            }
    The header for this program is:

    Code:
    #ifndef SPECTRAL_MATCHER
    #define SPECTRAL_MATCHER
    #define MAX_SIZE1 5120
    #define MAX_SIZE2 512
    char spectral_buffer[MAX_SIZE1];
    /* structure to store spectral data GOES into header file */
    typedef struct {
        float prec;
        int decoded_mz_length;
        int decoded_int_length;
        char* desc;
        char* mz;
        char* intensity;
    } glycopeptide;
    #endif

    Could anyone point me to where i am going wrong? I think that i am giving the correct length of the uncompressed/decoded string to uncompress (printed the value in the first C program) but somehow, something goes wrong

    Any help would be greatly appreciated.

    Kind Regards,
    Bas Jansen
    (C-Newbie)
    Last edited by Tarskin; 12-29-2011 at 07:44 AM. Reason: Trying to clarify variable conversion between 1st and 2d C program & Added the header file

  2. #2
    Registered User Tarskin's Avatar
    Join Date
    Dec 2011
    Location
    The Hague, Netherland
    Posts
    6
    Forgot to add tags, can someone still do that for clarity?

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    If you're getting a segmentation fault, then the first thing I would suggest is to compile in debug mode and run it in the debugger. That will help pinpoint the problem for you.

    If you're programming in C, the casts of the return value of malloc are unnecessary and should not be in the code.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    The C program i am struggling with attempts to restore the original data and so far i have come up with the following code (yielding a segmentation fault):
    Did you run this code through your debugger? The debugger should be able to tell you the line number where the fault was detected, and you should be able to look at the variables to see what looks out of whack.

    Jim

  5. #5
    Registered User Tarskin's Avatar
    Join Date
    Dec 2011
    Location
    The Hague, Netherland
    Posts
    6
    I use the command line gcc, it does give 2 warnings that i am not explicitly casting a variable but beyond that nothing. I will google a bit to see how debugging mode works for gcc.

    @rags: I didn't know that, i'll try it

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    You will use gdb as your debugger, and compile with -ggdb3 to compile with gcc in debug mode.

  7. #7
    Registered User Tarskin's Avatar
    Join Date
    Dec 2011
    Location
    The Hague, Netherland
    Posts
    6
    That shaves a few minutes off the googling heh

    I will try this straight away once i managed to get everything set (environment wise) on my netbook (as i allready left the office for today).
    Last edited by Tarskin; 12-29-2011 at 09:06 AM.

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You 're not checking the return value from scanf. If you do, I suspect it will return 0 since your XML end tag is incorrect.

  9. #9
    Registered User Tarskin's Avatar
    Join Date
    Dec 2011
    Location
    The Hague, Netherland
    Posts
    6
    Meh, that was a copy/paste fault on my part, i would fix it in the original post if i could o.O

  10. #10
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Meh yourself. Rather odd copy/paste error.

  11. #11
    Registered User Tarskin's Avatar
    Join Date
    Dec 2011
    Location
    The Hague, Netherland
    Posts
    6
    I found the problem when i re-examined ramp.c. I wasn't rebuilding the structure during decoding which meant that the uncompress would never make sense to begin with. Now.. time to fix them memory leaks :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to convert string in url encoding to html encoding?
    By Jerel2k11 in forum C Programming
    Replies: 6
    Last Post: 11-06-2011, 09:05 AM
  2. Encoding
    By /dev/bag in forum Linux Programming
    Replies: 1
    Last Post: 06-05-2011, 02:08 PM
  3. URL Encoding
    By nickname_changed in forum C# Programming
    Replies: 2
    Last Post: 03-13-2004, 03:22 AM
  4. Encoding
    By gvector1 in forum C# Programming
    Replies: 0
    Last Post: 06-20-2003, 10:17 AM
  5. WMA encoding problem...
    By Jperensky in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 05-23-2002, 11:39 AM