• Dump contents in raw packet data (packets are in ASCII format) in C

    ShikhaTan Member

    Dump contents in raw packet data (packets are in ASCII format).
    I need raw packet data (packets are in ascii format). I’m limited in size because I’m using 3rd party lib print utility that redirect the logs to an equipment

  • Ganesh Member

    Would calling your print utility in a loop until all the packed is dumped be be an option or is there an issue with that? E.g. like

    unsigned int written = 0;
    
    for (written = 0; i < packet_size - packet_size % 90; written += 90) {
    
      // write 90 bytes
      log_3rd_party(packet_buffer + written, 90);
    }
    
    // log the remainder
    log_3rd_party(packet_buffer + written, packet_size % 90);
    
  • ShikhaTan Member

    If log_3rd_party does not allow to pass a length, so we may need to copy to a temporary buffer.

    unsigned int written = 0;
    char buf[92];
    
    for (written = 0; i < packet_size - packet_size % 90; written += 90) {
    
      // write 90 bytes
      memcpy(buf,packet_buffer + written, 90); // could also use 'snprintf()', won't terminate iether
      buf[91] = '\0'; // zero-terminate buffer
      log_3rd_party(buf);
    }
    
    // log the remainder
    unsigned remainder =  packet_size % 90;
    memcpy(buf,packet_buffer + written,  remainder); // could also use 'snprintf()', won't terminate iether
    buf[ remainder + 1] = '\0'; // zero-terminate buffer
    log_3rd_party(buf);
    
  • ShikhaTan Member

    A couple of thoughts come to mind….

    Are you sure that the buffer is longer than the data? It could be a simple buffer overrun.

    Do you need to flush the output file? It’s possible that the 3rd party I/O package can’t handle that much data that fast.

  • Ganesh Member
    // write 90 bytes
      memcpy(buf,packet_buffer + written, 90); // could also use 'snprintf()', won't terminate iether
      buf[91] = '\0'; // zero-terminate buffer
    

    The buffer terminates at buf[91] what means it is the 92th byte. so after 90 characters the buffer contains 1 byte with random contents. also if the print function expects a 90-byte buffer, it is probably too much to pass 92 bytes since it might write beyond buffer boundaries in the print function by 1 or two bytes.

    so the code should be

    char buf[90] = { '\0' };  // initialize the buffer
    for (written = 0; written < packet_size - remainder; written += 89) {
    
     // copy 89 bytes (or less at end)
      int ncopy = min(89, packet_size-remainder-written);
      memcpy(buf, packet_buffer + written, ncopy); 
      buf[ncopy] = '\0'; // zero-terminate buffer in 90th byte (is buf[89])
    
Viewing 4 reply threads
  • You must be logged in to reply to this topic.