Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test in-place encoding and/or decoding (one buffer for input and output) #2

Open
cmcqueen opened this issue Jul 1, 2010 · 3 comments

Comments

@cmcqueen
Copy link
Owner

cmcqueen commented Jul 1, 2010

Originally reported by: Craig McQueen (Bitbucket: cmcqueen1975, GitHub: @cmcqueen)


Examine whether the code supports in-place encoding and/or decoding. That is, output is written to the input buffer, overwriting the original input. It could be useful to save RAM in small embedded systems.

  • My guess is that in-place decoding should work as-is
  • In-place encoding should work I think, however the input would have to be offset from the start of the output buffer by a certain number of bytes. In the code, see COBS_ENCODE_SRC_OFFSET() and COBSR_ENCODE_SRC_OFFSET() which calculate the required offset, for a given data size.

Once this has been checked:

  • Add some unit tests to check this.
  • Add documentation to say whether/how it works.

@mokus0
Copy link

mokus0 commented Jun 3, 2020

I was looking through this just now for cobs_decode and I do believe that cobs_decode at least should be ok - every increment of src_read_ptr is followed by at most 1 increment of dst_write_ptr, so it should never be possible for the write pointer to get ahead of the read pointer. The only case where they are unmatched is when decoding a 0xff byte, in which case the src pointer gets ahead by one byte.

@TunaBicim
Copy link

I have been using the library on a new project and I can say that in place encode works if put the data on the buffer with and offset of 1. The header is populated properly and there is no issues.

For decode case it works without issues with no offset.

My project had a union that had a buffer and also a structure that contained information on the packets (cobs header, length crc etc) and if I tried to do in place using the structure and used an offset of 1 on the output, you get a zero in input error because the src pointer points to the decoded 0 present on the next step. So for a generic array it works, with a dedicated structure more work needs to be performed, or a copy needs to be made.

@cmcqueen
Copy link
Owner Author

cmcqueen commented Oct 8, 2020

@TunaBicim said:

I have been using the library on a new project and I can say that in place encode works if put the data on the buffer with and offset of 1.

An offset of 1 is fine if the data size is less than 255 bytes.

But, for larger data sizes, the offset needs to be bigger. In the code, see COBS_ENCODE_SRC_OFFSET() and COBSR_ENCODE_SRC_OFFSET() which calculate the required offset, for a given data size.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants