Splicing char array

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm working on a project where I am getting data packets from a server. The data is received as a char array, and the length can be upwards of 1500 in size. Too big for me to store. I can only store data in blocks go 64. When I receive it, I need to copy the data array to a uint8_t array[64].  As I am iterating over the data array, I want to take 64 bytes at a time. Here is my dumbed down code. When I look at the data coming in, copy to temp array, the data is incorrect. I'm almost 100% positive that my memcpy function is incorrect.

 


​void store_file_packet(char * data, uint32_t length) {
  if ((data == NULL) || (length < 1)) {

    return;
  }

  if (data != NULL) {

    uint8_t temp[64];
    uint16_t chunks = length / 64;
    uint8_t rem = length % 64;

    start = 0;

    for (uint16_t i = 0; i < chunks; i++) {

      memcpy(temp, &data[start], 64); // bug here???

      // store file from temp array
      start += 64;
    }
    // handle remainder...

  }
}

 

"When all else fails, read the directions"

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ran that snippet through a lint; added a definition declaration of 'start'

FlexeLint for C/C++ (Unix) Vers. 9.00L, Copyright Gimpel Software 1985-2014
--- Module: diy.c (C)
     1  typedef unsigned char uint8_t;
     2  typedef unsigned int uint16_t;
     3  typedef unsigned long int uint32_t;
     4  typedef unsigned int size_t;
     5  #define NULL 0
     6  void * memcpy (void *, const void *, size_t);
     7
     8  void store_file_packet(char * data, uint32_t length) {
     9    if ((data == NULL) || (length < 1)) {
    10
    11      return;
    12    }
    13
                      _
    14    if (data != NULL) {
diy.c  14  Info 774:  Boolean within 'if' always evaluates to True [Reference: file diy.c: lines 9, 14]
    15
    16      uint8_t temp[64];
    17      uint16_t chunks = length / 64;
    18      uint8_t rem = length % 64;
    19      uint16_t start;
    20
    21      start = 0;
    22
    23      for (uint16_t i = 0; i < chunks; i++) {
    24
    25        memcpy(temp, &data[start], 64); // bug here???
    26
    27        // store file from temp array
    28        start += 64;
    29      }
    30      // handle remainder...
    31
          _
    32    }
diy.c  32  Warning 529:  Symbol 'rem' (line 18) not subsequently referenced
diy.c  32  Warning 438:  Last value assigned to variable 'rem' (defined at line 18) not used
        _
    33  }
diy.c  33  Info 818:  Pointer parameter 'data' (line 8) could be declared as pointing to const
    34  

/// Start of Pass 2 ///

--- Module: diy.c (C)
     1  typedef unsigned char uint8_t;
     2  typedef unsigned int uint16_t;
     3  typedef unsigned long int uint32_t;
     4  typedef unsigned int size_t;
     5  #define NULL 0
     6  void * memcpy (void *, const void *, size_t);
     7
     8  void store_file_packet(char * data, uint32_t length) {
     9    if ((data == NULL) || (length < 1)) {
    10
    11      return;
    12    }
    13
    14    if (data != NULL) {
    15
    16      uint8_t temp[64];
    17      uint16_t chunks = length / 64;
    18      uint8_t rem = length % 64;
    19      uint16_t start;
    20
    21      start = 0;
    22
    23      for (uint16_t i = 0; i < chunks; i++) {
    24
    25        memcpy(temp, &data[start], 64); // bug here???
    26
    27        // store file from temp array
    28        start += 64;
    29      }
    30      // handle remainder...
    31
    32    }
    33  }
    34  

--- Global Wrap-up

Warning 526:  Symbol 'memcpy(void *, const void *, unsigned int)' (line 6, file diy.c) not defined
Info 714:  Symbol 'store_file_packet(char *, unsigned long)' (line 8, file diy.c) not referenced

http://www.gimpel-online.com/OnlineTesting.html (Do-It-Yourself Example (C))

 

"Dare to be naïve." - Buckminster Fuller

Last Edited: Fri. May 25, 2018 - 03:28 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

gchapman wrote:
http://www.gimpel-online.com/Onl... (Do-It-Yourself Example (C))

Cool resource but not very helpful in this situation.

 

I was able to track down part of the issue. Comparing the binary I am coping, I'm off by 2 bytes in my for loop. Still not sure why.

"When all else fails, read the directions"

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Might help to say what "data is incorrect" actually means. For example this isn't about a transport stream doing /n to/from /r/n conversion or something?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
Might help to say what "data is incorrect" actually means

Sorry - I will try to provide working example.

"When all else fails, read the directions"

Last Edited: Fri. May 25, 2018 - 08:49 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am confused.  Clearly a 1500 byte buffer is expensive for SRAM.

 

If you are just looking for some "less than 64 byte" sequence at some random place in the 1500 byte,   you read into a circular buffer as the data arrives,   searching as you go.    Extract the stuff you want and discard the rest.

 

Conventional buffered input will stop and start the remote transmitter as required e.g. with ctl-S and ctrl-Q.

Think of your PC.    Or a printer.  

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Issue resolved. The snippet of code I posted works...I have a bug in another part of my code.

"When all else fails, read the directions"