Thread: Payload modification - If or initiation problem

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    26

    Payload modification - If or initiation problem

    Hi,

    I want to change N-th byte of payload. This code works but does not do what I want. I see that a-th byte of payload is set to 0xAA. What I am doing wrong here?

    Code:
    unsigned char data[4]={0x00,0x00,0x01,0xb3};
    int a=55;
     int k = 0;
    //int j;
    int N = 51;
    while(k < m->data_len) {
    
     
        if (m->payload[k] != 0x00) {
            k++;
                           }
        else {
        if (memcmp(&m->payload[k],data,sizeof(data))==0) {
    int j;
    if(j % N == 0) { 
    m->payload[a]=0xAA;     
    m->payload[j]=0xFF; // ??????
    
    }
         else {break;} 
     j++;                         } 
    else {
    k++;   }
         }
                      }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MiniComa
    I want to change N-th byte of payload. This code works but does not do what I want. I see that a-th byte of payload is set to 0xAA. What I am doing wrong here?
    For starters, you need to indent your code properly, e.g.,
    Code:
    unsigned char data[4] = {0x00, 0x00, 0x01, 0xb3};
    int a = 55;
    int k = 0;
    //int j;
    int N = 51;
    while (k < m->data_len) {
        if (m->payload[k] != 0x00) {
            k++;
        }
        else {
            if (memcmp(&m->payload[k], data, sizeof(data)) == 0) {
                int j;
                if (j % N == 0) {
                    m->payload[a] = 0xAA;
                    m->payload[j] = 0xFF; // ??????
                }
                else {
                    break;
                }
                j++;
            }
            else {
                k++;
            }
        }
    }
    Anyway, if you want to change the Nth byte of payload instead of the jth byte, then just write:
    Code:
    m->payload[N] = 0xFF;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    26
    I want to change every N-th byte of payload. And if I set m->payload[N] = 0xFF; it will change only first one. Sorry if my question was obscure.

    P.S. How do you made code to look so nice?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MiniComa
    I want to change every N-th byte of payload. And if I set m->payload[N] = 0xFF; it will change only first one.
    Right. In that case:
    Code:
    int n = N;
    
    // ...
    
    m->payload[n] = 0xFF;
    n += N;
    Quote Originally Posted by MiniComa
    How do you made code to look so nice?
    You're already using code bbcode tags, so that part is done
    Notice that in each block of code, I indent by one level, fixed to 4 spaces.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    88
    In the code below , j has junk in it (not initialized ) and may or may not be evenly divisible by N, regardless of what k is.

    Code:
    int j;
    if (j % N == 0) {

  6. #6
    Registered User
    Join Date
    Jul 2012
    Posts
    26
    Did I understand you correctly and code should look like this? Because it gives me the same output.
    Code:
    unsigned char data[4]={0x00,0x00,0x01,0xb3};
     int k = 0;
    int j;
    int N=5;
    int n=N;
    while(k < m->data_len) {
         if (m->payload[k] != 0x00) {
                     k++;
         }
         else {
             if (memcmp(&m->payload[k],data,sizeof(data))==0) {
                 if(j % N == 0) { 
                 m->payload[a]=0xAA;     
                 m->payload[n]=0xFF;
                 n += N;
         }
              else{
              break;
         } 
              j++;                 } 
         else {
              k++;   
         }
      }
    }

  7. #7
    Registered User
    Join Date
    Jul 2012
    Posts
    26
    Quote Originally Posted by sparkomemphis View Post
    In the code below , j has junk in it (not initialized ) and may or may not be evenly divisible by N, regardless of what k is.

    Code:
    int j;
    if (j % N == 0) {
    What can I do to avoid that?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Taking into account sparkomemphis' observation and your use of j in your original code, I think that you were trying to use j as my example's n, except that you did it wrongly. I would expect something along these lines:
    Code:
    unsigned char data[4] = {0x00, 0x00, 0x01, 0xb3};
    int k = 0;
    int j = 0;
    int N = 5;
    while (k < m->data_len) {
        if (m->payload[k] == 0x00 && memcmp(&m->payload[k], data, sizeof(data)) == 0) {
            if (j % N == 0) {
                m->payload[a] = 0xAA;
                m->payload[j] = 0xFF;
            }
            else {
                break;
            }
            j++;
        }
        else {
            k++;
        }
    }
    I have taken the liberty of condensing your if statement logic. Notice that j is correctly initialised. Nonetheless, I don't expect this code to work: you need to get your variables right, and because I don't actually know what you are trying to do, I cannot correct them for you.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jul 2012
    Posts
    26
    Could you say what do you mean by saying this "you need to get your variables right"? Because English is not my first language and I did not understand what you mean.
    Talking about whole code, I can comment a little. I work with iptables and libipq in Ubuntu. I put some MPEG2 video packets in QUEUE, take them out, modify and put back to QUEUE. Unsigned char data is beginning of byte sequence of MPEG2 sequence header. So I try to identify these packets and modify payload. After all I try to evaluate how quality of video has changed after modification.
    Last edited by MiniComa; 08-16-2012 at 11:37 AM. Reason: typo

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Things like: are you incrementing j and k at the correct places? What is the variable named a?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Jul 2012
    Posts
    26
    Quote Originally Posted by laserlight View Post
    Things like: are you incrementing j and k at the correct places? What is the variable named a?
    Would you look into full code if I post a link? With "a" variable I try to divide stream into two parts where in one part I have modified packets with sequence header and in the other I have all other packets .

    Link: [C] C code - Pastebin.com
    Last edited by MiniComa; 08-16-2012 at 11:51 AM.

  12. #12
    Registered User
    Join Date
    Jul 2012
    Posts
    26
    I have figured it out by myself. Code looks like this:

    Code:
    unsigned char data[4] = {0x00, 0x00, 0x01, 0xb3};
    int k = 0;
    int j = 0;
    int N = 5;
    while (k < m->data_len) {
        if (m->payload[k] == 0x00 && memcmp(&m->payload[k], data, sizeof(data)) == 0) {
            if (j % N == 0) {
                m->payload[a] = 0xAA;
                m->payload[j] = 0xFF;
            }
            else {
                j++;
            }
            j++;
        }
        else {
            k++;
        }
    }
    Have to change only one line. Thank you laserlight. You have helped me a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data modification
    By MiniComa in forum C Programming
    Replies: 0
    Last Post: 07-09-2012, 01:59 PM
  2. File modification problem
    By led1090 in forum C Programming
    Replies: 14
    Last Post: 09-26-2010, 02:09 AM
  3. Bigint modification - please help
    By grigorianvlad in forum C++ Programming
    Replies: 7
    Last Post: 09-16-2010, 06:21 AM
  4. SSHDOS linux modification GET problem
    By berg in forum C Programming
    Replies: 3
    Last Post: 04-11-2007, 06:13 AM
  5. quake I modification
    By matecno in forum Game Programming
    Replies: 8
    Last Post: 04-06-2005, 04:05 PM