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

Add support for parsing of incomplete base64 and QP MIME parts #224

Merged
merged 1 commit into from
Feb 28, 2016

Conversation

disaykin
Copy link

@disaykin disaykin commented Feb 8, 2016

It useful when MIME part is requested and loaded to memory by small chunks.

@dinhvh
Copy link
Owner

dinhvh commented Feb 8, 2016

Could you post some example about how to use it?

I feel like we might want to return some different error code when more data are required, only in the case of parsing partial data.
IMAP has MAILIMAP_ERROR_NEEDS_MORE_DATA.
What do you think?

Thanks!

@disaykin
Copy link
Author

My use case is loading big attachment by imap_fetch_range. In pseudocode:

uint32_t received = 0;
uint32_t partLength = bodystructure[partId]->length;
for (;;) {
  struct imap_range_t range = { received, 1024*1024 };
  char *result;
  size_t result_len;
  imap_fetch_part_range(uid, partId, range, &result, &result_len);

  if (result_len == 0) {
    // requested range is empty. part is completely fetched
    break;
  }

  bool isThisRangeLast = (received + result_len >= partLength); // determine that the received data is the last, may be more difficult (in case of invalid metadata on the server).

  char *decoded;
  size_t decoded_len;
  if (isThisRangeLast) {
    uint32_t index = 0;
    mailmime_part_parse(result, result_len, encoding, &index, &decoded, &decoded_len);
    break;
  }
  else {
    uint32_t index = 0;
    mailmime_part_parse_partial(result, result_len, encoding, &index, &decoded, &decoded_len);
    // we may have some non-decoded bytes at the end of chunk.
    // in this case we just request it in the next chunk 
    received += index;
  }
}

@disaykin
Copy link
Author

I think that return the MAILIMAP_ERROR_NEEDS_MORE_DATA in this case is not needed. It is not an error, it is normal scenario in handling of data stream.

But I can add this, if need.

@dinhvh
Copy link
Owner

dinhvh commented Feb 16, 2016

No need to add the return code. Your example is helpful. I have some comments though.

}
written += 3;
}
}

if (chunk_index != 0) {
if (chunk_index != 0 && partial == FALSE) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could change partial == FALSE to !partial ?

@dinhvh
Copy link
Owner

dinhvh commented Feb 23, 2016

ping?

@disaykin
Copy link
Author

I'm a little busy right now. In the near future I will proceed.

@disaykin
Copy link
Author

Done.

@@ -1837,6 +1860,9 @@ int mailmime_quoted_printable_body_parse(const char * message, size_t length,

case '\r':
if (cur_token + 2 >= length) {
if (!partial) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you mean if (partial) here?
or you did you change a behavior?
or did you fix an issue?

@dinhvh
Copy link
Owner

dinhvh commented Feb 28, 2016

I just have a question.
Thanks!

@disaykin
Copy link
Author

it was a missed change of behavior. I deleted those lines.

dinhvh added a commit that referenced this pull request Feb 28, 2016
Add support for parsing of incomplete base64 and QP MIME parts
@dinhvh dinhvh merged commit 3af17cf into dinhvh:master Feb 28, 2016
@dinhvh
Copy link
Owner

dinhvh commented Feb 28, 2016

Thanks a lot for that contribution!

@disaykin disaykin deleted the partial-decoding branch June 28, 2016 21:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants