libetpan - fdik
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

11171 lines
267 KiB

/*
* libEtPan! -- a mail stuff library
*
* Copyright (C) 2001, 2005 - DINH Viet Hoa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the libEtPan! project nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* $Id: mailimap_parser.c,v 1.74 2011/07/13 09:28:36 hoa Exp $
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "mailstream.h"
#include "mailimap_keywords.h"
#include "mailimap_parser.h"
#include "mailimap_extension.h"
#include "mmapstring.h"
#include "mail.h"
#include "timeutils.h"
#ifndef UNSTRICT_SYNTAX
#define UNSTRICT_SYNTAX
#endif
/*
Document: internet-drafts/draft-crispin-imapv-15.txt
RFC 2060 (IMAP but rather used draft)
RFC 2234 for all token that are not defined such as ALPHA
*/
/* ************************************************************************* */
/* ************************************************************************* */
/* ************************************************************************* */
/* ************************************************************************* */
/* ************************************************************************* */
/* ************************************************************************* */
typedef int mailimap_struct_parser_progress(mailstream * fd, MMAPString * buffer,
size_t * indx, void * result,
size_t progr_rate,
progress_function * progr_fun,
mailprogress_function * body_progr_fun,
mailprogress_function * items_progr_fun,
void * context,
mailimap_msg_att_handler * msg_att_handler,
void * msg_att_context);
static int mailimap_address_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_address ** result,
size_t progr_rate,
progress_function * progr_fun);
static int mailimap_addr_adl_parse(mailstream * fd, MMAPString * buffer,
size_t * indx, char ** result,
size_t progr_rate,
progress_function * progr_fun);
static int mailimap_addr_host_parse(mailstream * fd, MMAPString * buffer,
size_t * indx, char ** result,
size_t progr_rate,
progress_function * progr_fun);
static int mailimap_addr_mailbox_parse(mailstream * fd, MMAPString * buffer,
size_t * indx, char ** result,
size_t progr_rate,
progress_function * progr_fun);
static int mailimap_addr_name_parse(mailstream * fd, MMAPString * buffer,
size_t * indx, char ** result,
size_t progr_rate,
progress_function * progr_fun);
static int mailimap_auth_type_parse(mailstream * fd, MMAPString * buffer,
size_t * indx, char ** result,
size_t progr_rate,
progress_function * progr_fun);
static int mailimap_base64_parse(mailstream * fd, MMAPString * buffer,
size_t * indx, char ** result,
size_t progr_rate,
progress_function * progr_fun);
static int mailimap_body_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_body ** result,
size_t progr_rate,
progress_function * progr_fun);
static int
mailimap_body_extension_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_body_extension ** result,
size_t progr_rate,
progress_function * progr_fun);
static int
mailimap_body_ext_1part_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_body_ext_1part ** result,
size_t progr_rate,
progress_function * progr_fun);
static int
mailimap_body_ext_mpart_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_body_ext_mpart ** result,
size_t progr_rate,
progress_function * progr_fun);
static int
mailimap_body_fields_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_body_fields ** result,
size_t progr_rate,
progress_function * progr_fun);
static int mailimap_body_fld_desc_parse(mailstream * fd, MMAPString * buffer,
size_t * indx, char ** result,
size_t progr_rate,
progress_function * progr_fun);
static int
mailimap_body_fld_dsp_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_body_fld_dsp ** result,
size_t progr_rate,
progress_function * progr_fun);
static int
mailimap_body_fld_enc_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_body_fld_enc ** result,
size_t progr_rate,
progress_function * progr_fun);
static int mailimap_body_fld_id_parse(mailstream * fd, MMAPString * buffer,
size_t * indx, char ** result,
size_t progr_rate,
progress_function * progr_fun);
static int
mailimap_body_fld_lang_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_body_fld_lang ** result,
size_t progr_rate,
progress_function * progr_fun);
static int mailimap_body_fld_lines_parse(mailstream * fd,
MMAPString * buffer, size_t * indx,
uint32_t * result);
static int mailimap_body_fld_loc_parse(mailstream * fd, MMAPString * buffer,
size_t * indx, char ** result,
size_t progr_rate,
progress_function * progr_fun);
static int mailimap_body_fld_md5_parse(mailstream * fd, MMAPString * buffer,
size_t * indx, char ** result,
size_t progr_rate,
progress_function * progr_fun);
static int mailimap_body_fld_octets_parse(mailstream * fd,
MMAPString * buffer, size_t * indx,
uint32_t * result);
static int
mailimap_body_fld_param_parse(mailstream * fd,
MMAPString * buffer, size_t * indx,
struct mailimap_body_fld_param ** result,
size_t progr_rate,
progress_function * progr_fun);
static int
mailimap_body_type_1part_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_body_type_1part ** result,
size_t progr_rate,
progress_function * progr_fun);
static int
mailimap_body_type_basic_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_body_type_basic ** result,
size_t progr_rate,
progress_function * progr_fun);
static int
mailimap_body_type_mpart_parse(mailstream * fd,
MMAPString * buffer,
size_t * indx,
struct mailimap_body_type_mpart ** result,
size_t progr_rate,
progress_function * progr_fun);
static int
mailimap_body_type_msg_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_body_type_msg ** result,
size_t progr_rate,
progress_function * progr_fun);
static int
mailimap_body_type_text_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_body_type_text **
result,
size_t progr_rate,
progress_function * progr_fun);
static int
mailimap_capability_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_capability ** result,
size_t progr_rate,
progress_function * progr_fun);
/*
static gboolean mailimap_date_day_parse(mailstream * fd,
MMAPString * buffer,
guint32 * indx,
gint * result);
*/
static int mailimap_date_day_fixed_parse(mailstream * fd,
MMAPString * buffer,
size_t * indx,
int * result);
static int mailimap_date_month_parse(mailstream * fd, MMAPString * buffer,
size_t * indx, int * result);
/*
struct mailimap_date_text {
gint day;
gint month;
gint year;
};
static gboolean
mailimap_date_text_parse(mailstream * fd, MMAPString * buffer,
guint32 * indx, struct mailimap_date_text ** result);
static void mailimap_date_text_free(struct mailimap_date_text * date_text);
*/
static int mailimap_date_year_parse(mailstream * fd, MMAPString * buffer,
size_t * indx, int * result);
static int mailimap_date_time_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_date_time ** t,
size_t progr_rate,
progress_function * progr_fun);
#ifndef UNSTRICT_SYNTAX
static int mailimap_digit_nz_parse(mailstream * fd, MMAPString * buffer,
size_t * indx, int * result);
#endif
static int mailimap_envelope_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_envelope ** result,
size_t progr_rate,
progress_function * progr_fun);
static int
mailimap_env_bcc_parse(mailstream * fd, MMAPString * buffer,
size_t * indx, struct mailimap_env_bcc ** result,
size_t progr_rate,
progress_function * progr_fun);
static int
mailimap_env_cc_parse(mailstream * fd, MMAPString * buffer,
size_t * indx, struct mailimap_env_cc ** result,
size_t progr_rate,
progress_function * progr_fun);
static int mailimap_env_date_parse(mailstream * fd, MMAPString * buffer,
size_t * indx, char ** result,
size_t progr_rate,
progress_function * progr_fun);
static int
mailimap_env_from_parse(mailstream * fd, MMAPString * buffer,
size_t * indx, struct mailimap_env_from ** result,
size_t progr_rate,
progress_function * progr_fun);
static int mailimap_env_in_reply_to_parse(mailstream * fd,
MMAPString * buffer,
size_t * indx, char ** result,
size_t progr_rate,
progress_function * progr_fun);
static int mailimap_env_message_id_parse(mailstream * fd,
MMAPString * buffer,
size_t * indx, char ** result,
size_t progr_rate,
progress_function * progr_fun);
static int
mailimap_env_reply_to_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_env_reply_to ** result,
size_t progr_rate,
progress_function * progr_fun);
static int
mailimap_env_sender_parse(mailstream * fd, MMAPString * buffer,
size_t * indx, struct mailimap_env_sender ** result,
size_t progr_rate,
progress_function * progr_fun);
static int mailimap_env_subject_parse(mailstream * fd, MMAPString * buffer,
size_t * indx, char ** result,
size_t progr_rate,
progress_function * progr_fun);
static int
mailimap_env_to_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_env_to ** result,
size_t progr_rate,
progress_function * progr_fun);
static int mailimap_flag_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_flag ** result,
size_t progr_rate,
progress_function * progr_fun);
static int mailimap_flag_extension_parse(mailstream * fd,
MMAPString * buffer,
size_t * indx,
char ** result,
size_t progr_rate,
progress_function * progr_fun);
static int
mailimap_flag_fetch_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_flag_fetch ** result,
size_t progr_rate,
progress_function * progr_fun);
static int
mailimap_flag_perm_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_flag_perm ** result,
size_t progr_rate,
progress_function * progr_fun);
static int mailimap_flag_keyword_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
char ** result,
size_t progr_rate,
progress_function * progr_fun);
static int mailimap_flag_list_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_flag_list ** result,
size_t progr_rate,
progress_function * progr_fun);
static int
mailimap_header_fld_name_parse(mailstream * fd,
MMAPString * buffer,
size_t * indx,
char ** result,
size_t progr_rate,
progress_function * progr_fun);
static int
mailimap_header_list_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_header_list ** result,
size_t progr_rate,
progress_function * progr_fun);
static int mailimap_literal_parse(mailstream * fd, MMAPString * buffer,
size_t * indx, char ** result,
size_t * result_len,
size_t progr_rate,
progress_function * progr_fun);
static int mailimap_literal_parse_progress(mailstream * fd, MMAPString * buffer,
size_t * indx, char ** result,
size_t * result_len,
size_t progr_rate,
progress_function * progr_fun,
mailprogress_function * body_progr_fun,
mailprogress_function * items_progr_fun,
void * context,
mailimap_msg_att_handler * msg_att_handler,
void * msg_att_context);
static int
mailimap_mailbox_data_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_mailbox_data ** result,
size_t progr_rate,
progress_function * progr_fun);
static int
mailimap_mbx_list_flags_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_mbx_list_flags ** result,
size_t progr_rate,
progress_function * progr_fun);
static int
mailimap_mbx_list_oflag_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_mbx_list_oflag ** result,
size_t progr_rate,
progress_function * progr_fun);
static int
mailimap_mbx_list_oflag_no_sflag_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_mbx_list_oflag ** result,
size_t progr_rate,
progress_function * progr_fun);
static int
mailimap_mbx_list_sflag_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
int * result);
static int
mailimap_media_basic_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_media_basic ** result,
size_t progr_rate,
progress_function * progr_fun);
static int
mailimap_media_message_parse(mailstream * fd, MMAPString * buffer,
size_t * indx);
static int
mailimap_media_subtype_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
char ** result,
size_t progr_rate,
progress_function * progr_fun);
static int mailimap_media_text_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
char ** result,
size_t progr_rate,
progress_function * progr_fun);
static int
mailimap_message_data_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_message_data ** result,
size_t progr_rate,
progress_function * progr_fun);
static int
mailimap_msg_att_parse(mailstream * fd, MMAPString * buffer,
size_t * indx, struct mailimap_msg_att ** result,
size_t progr_rate,
progress_function * progr_fun);
static int
mailimap_msg_att_parse_progress(mailstream * fd, MMAPString * buffer,
size_t * indx, struct mailimap_msg_att ** result,
size_t progr_rate,
progress_function * progr_fun,
mailprogress_function * body_progr_fun,
mailprogress_function * items_progr_fun,
void * context,
mailimap_msg_att_handler * msg_att_handler,
void * msg_att_context);
static int
mailimap_msg_att_dynamic_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_msg_att_dynamic ** result,
size_t progr_rate,
progress_function * progr_fun);
static int
mailimap_msg_att_static_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_msg_att_static ** result,
size_t progr_rate,
progress_function * progr_fun);
static int
mailimap_msg_att_static_parse_progress(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_msg_att_static ** result,
size_t progr_rate,
progress_function * progr_fun,
mailprogress_function * body_progr_fun,
mailprogress_function * items_progr_fun,
void * context,
mailimap_msg_att_handler * msg_att_handler,
void * msg_att_context);
static int
mailimap_quoted_parse(mailstream * fd, MMAPString * buffer,
size_t * indx, char ** result,
size_t progr_rate,
progress_function * progr_fun);
static int
mailimap_quoted_specials_parse(mailstream * fd, MMAPString * buffer,
size_t * indx, char * result);
static int
mailimap_response_done_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_response_done ** result,
size_t progr_rate,
progress_function * progr_fun);
static int
mailimap_response_fatal_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_response_fatal ** result,
size_t progr_rate,
progress_function * progr_fun);
static int
mailimap_response_tagged_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_response_tagged ** result,
size_t progr_rate,
progress_function * progr_fun);
static int
mailimap_resp_cond_auth_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_resp_cond_auth ** result,
size_t progr_rate,
progress_function * progr_fun);
static int
mailimap_resp_cond_bye_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_resp_cond_bye ** result,
size_t progr_rate,
progress_function * progr_fun);
static int
mailimap_resp_cond_state_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_resp_cond_state ** result,
size_t progr_rate,
progress_function * progr_fun);
static int
mailimap_resp_text_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_resp_text ** result,
size_t progr_rate,
progress_function * progr_fun);
static int
mailimap_resp_text_code_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_resp_text_code ** result,
size_t progr_rate,
progress_function * progr_fun);
static int
mailimap_section_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_section ** result,
size_t progr_rate,
progress_function * progr_fun);
static int
mailimap_section_msgtext_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_section_msgtext ** result,
size_t progr_rate,
progress_function * progr_fun);
static int
mailimap_section_part_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_section_part ** result,
size_t progr_rate,
progress_function * progr_fun);
static int
mailimap_section_spec_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_section_spec ** result,
size_t progr_rate,
progress_function * progr_fun);
static int
mailimap_section_text_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_section_text ** result,
size_t progr_rate,
progress_function * progr_fun);
static int mailimap_tag_parse(mailstream * fd, MMAPString * buffer,
size_t * indx, char ** result,
size_t progr_rate,
progress_function * progr_fun);
static int mailimap_text_parse(mailstream * fd, MMAPString * buffer,
size_t * indx, char ** result,
size_t progr_rate,
progress_function * progr_fun);
static int mailimap_time_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
int * phour, int * pmin, int * psec);
static int mailimap_zone_parse(mailstream * fd, MMAPString * buffer,
size_t * indx, int * result);
static int
mailimap_mailbox_gmail_parse(mailstream * fd, MMAPString * buffer,
size_t * indx, char ** result,
size_t progr_rate,
progress_function * progr_fun);
static int
mailimap_response_data_parse_progress(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_response_data ** result,
size_t progr_rate,
progress_function * progr_fun,
mailprogress_function * body_progr_fun,
mailprogress_function * items_progr_fun,
void * context,
mailimap_msg_att_handler * msg_att_handler,
void * msg_att_context);
static int mailimap_nstring_parse_progress(mailstream * fd, MMAPString * buffer,
size_t * indx, char ** result,
size_t * result_len,
size_t progr_rate,
progress_function * progr_fun,
mailprogress_function * body_progr_fun,
mailprogress_function * items_progr_fun,
void * context,
mailimap_msg_att_handler * msg_att_handler,
void * msg_att_context);
static int
mailimap_string_parse_progress(mailstream * fd, MMAPString * buffer,
size_t * indx, char ** result,
size_t * result_len,
size_t progr_rate,
progress_function * progr_fun,
mailprogress_function * body_progr_fun,
mailprogress_function * items_progr_fun,
void * context,
mailimap_msg_att_handler * msg_att_handler,
void * msg_att_context);
static int has_crlf(MMAPString * buffer, size_t index);
/* ************************************************************************* */
/* ************************************************************************* */
/* ************************************************************************* */
/* ************************************************************************* */
/* ************************************************************************* */
/* ************************************************************************* */
/* ******************** TOOLS **************************** */
static int mailimap_unstrict_char_parse(mailstream * fd, MMAPString * buffer,
size_t * indx, char token)
{
size_t cur_token;
int r;
cur_token = * indx;
#ifdef UNSTRICT_SYNTAX
/* can accept unstrict syntax */
mailimap_space_parse(fd, buffer, &cur_token);
if (token == ' ') {
* indx = cur_token;
return MAILIMAP_NO_ERROR;
}
#endif
r = mailimap_char_parse(fd, buffer, &cur_token, token);
if (r != MAILIMAP_NO_ERROR)
return r;
* indx = cur_token;
return MAILIMAP_NO_ERROR;
}
int mailimap_oparenth_parse(mailstream * fd, MMAPString * buffer,
size_t * indx)
{
return mailimap_unstrict_char_parse(fd, buffer, indx, '(');
}
int mailimap_cparenth_parse(mailstream * fd, MMAPString * buffer,
size_t * indx)
{
return mailimap_unstrict_char_parse(fd, buffer, indx, ')');
}
static int mailimap_oaccolade_parse(mailstream * fd, MMAPString * buffer,
size_t * indx)
{
return mailimap_unstrict_char_parse(fd, buffer, indx, '{');
}
static int mailimap_caccolade_parse(mailstream * fd, MMAPString * buffer,
size_t * indx)
{
return mailimap_unstrict_char_parse(fd, buffer, indx, '}');
}
static int mailimap_plus_parse(mailstream * fd, MMAPString * buffer,
size_t * indx)
{
return mailimap_unstrict_char_parse(fd, buffer, indx, '+');
}
static int mailimap_minus_parse(mailstream * fd, MMAPString * buffer,
size_t * indx)
{
return mailimap_unstrict_char_parse(fd, buffer, indx, '-');
}
static int mailimap_star_parse(mailstream * fd, MMAPString * buffer,
size_t * indx)
{
return mailimap_unstrict_char_parse(fd, buffer, indx, '*');
}
static int mailimap_dot_parse(mailstream * fd, MMAPString * buffer,
size_t * indx)
{
return mailimap_unstrict_char_parse(fd, buffer, indx, '.');
}
int mailimap_colon_parse(mailstream * fd, MMAPString * buffer,
size_t * indx)
{
return mailimap_unstrict_char_parse(fd, buffer, indx, ':');
}
static int mailimap_lower_parse(mailstream * fd, MMAPString * buffer,
size_t * indx)
{
return mailimap_unstrict_char_parse(fd, buffer, indx, '<');
}
static int mailimap_greater_parse(mailstream * fd, MMAPString * buffer,
size_t * indx)
{
return mailimap_unstrict_char_parse(fd, buffer, indx, '>');
}
static int mailimap_obracket_parse(mailstream * fd, MMAPString * buffer,
size_t * indx)
{
return mailimap_unstrict_char_parse(fd, buffer, indx, '[');
}
static int mailimap_cbracket_parse(mailstream * fd, MMAPString * buffer,
size_t * indx)
{
return mailimap_unstrict_char_parse(fd, buffer, indx, ']');
}
int mailimap_dquote_parse(mailstream * fd, MMAPString * buffer,
size_t * indx)
{
return mailimap_char_parse(fd, buffer, indx, '\"');
}
static int mailimap_crlf_parse(mailstream * fd, MMAPString * buffer,
size_t * indx)
{
size_t cur_token = * indx;
int r;
#ifdef UNSTRICT_SYNTAX
mailimap_space_parse(fd, buffer, &cur_token);
#endif
r = mailimap_token_case_insensitive_parse(fd, buffer, &cur_token, "\r\n");
if (r == MAILIMAP_NO_ERROR) {
* indx = cur_token;
return MAILIMAP_NO_ERROR;
}
#ifdef UNSTRICT_SYNTAX
r = mailimap_unstrict_char_parse(fd, buffer, &cur_token, '\n');
if (r == MAILIMAP_NO_ERROR) {
* indx = cur_token;
return MAILIMAP_NO_ERROR;
}
#endif
return MAILIMAP_ERROR_PARSE;
}
static int
mailimap_struct_multiple_parse_progress(mailstream * fd, MMAPString * buffer,
size_t * indx, clist ** result,
mailimap_struct_parser_progress * parser,
mailimap_struct_destructor * destructor,
size_t progr_rate,
progress_function * progr_fun,
mailprogress_function * body_progr_fun,
mailprogress_function * items_progr_fun,
void * context,
mailimap_msg_att_handler * msg_att_handler,
void * msg_att_context)
{
clist * struct_list;
size_t cur_token;
void * value;
int r;
int res;
cur_token = * indx;
r = parser(fd, buffer, &cur_token, &value, progr_rate, progr_fun,
body_progr_fun, items_progr_fun, context, msg_att_handler, msg_att_context);
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto err;
}
struct_list = clist_new();
if (struct_list == NULL) {
destructor(value);
res = MAILIMAP_ERROR_MEMORY;
goto err;
}
if (value != NULL) {
r = clist_append(struct_list, value);
if (r < 0) {
destructor(value);
res = MAILIMAP_ERROR_MEMORY;
goto free_list;
}
}
while (1) {
r = parser(fd, buffer, &cur_token, &value, progr_rate, progr_fun,
body_progr_fun, items_progr_fun, context, msg_att_handler, msg_att_context);
if (r == MAILIMAP_ERROR_PARSE)
break;
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto free_list;
}
if (value != NULL) {
r = clist_append(struct_list, value);
if (r < 0) {
destructor(value);
res = MAILIMAP_ERROR_MEMORY;
goto free_list;
}
}
}
* result = struct_list;
* indx = cur_token;
return MAILIMAP_NO_ERROR;
free_list:
clist_foreach(struct_list, (clist_func) destructor, NULL);
clist_free(struct_list);
err:
return res;
}
int
mailimap_struct_multiple_parse(mailstream * fd, MMAPString * buffer,
size_t * indx, clist ** result,
mailimap_struct_parser * parser,
mailimap_struct_destructor * destructor,
size_t progr_rate,
progress_function * progr_fun)
{
clist * struct_list;
size_t cur_token;
void * value;
int r;
int res;
cur_token = * indx;
r = parser(fd, buffer, &cur_token, &value, progr_rate, progr_fun);
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto err;
}
struct_list = clist_new();
if (struct_list == NULL) {
destructor(value);
res = MAILIMAP_ERROR_MEMORY;
goto err;
}
r = clist_append(struct_list, value);
if (r < 0) {
destructor(value);
res = MAILIMAP_ERROR_MEMORY;
goto free_list;
}
while (1) {
r = parser(fd, buffer, &cur_token, &value, progr_rate, progr_fun);
if (r == MAILIMAP_ERROR_PARSE)
break;
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto free_list;
}
r = clist_append(struct_list, value);
if (r < 0) {
destructor(value);
res = MAILIMAP_ERROR_MEMORY;
goto free_list;
}
}
* result = struct_list;
* indx = cur_token;
return MAILIMAP_NO_ERROR;
free_list:
clist_foreach(struct_list, (clist_func) destructor, NULL);
clist_free(struct_list);
err:
return res;
}
int
mailimap_struct_list_parse_progress(mailstream * fd, MMAPString * buffer,
size_t * indx, clist ** result,
char symbol,
mailimap_struct_parser_progress * parser,
mailimap_struct_destructor * destructor,
size_t progr_rate,
progress_function * progr_fun,
mailprogress_function * body_progr_fun,
mailprogress_function * items_progr_fun,
void * context,
mailimap_msg_att_handler * msg_att_handler,
void * msg_att_context)
{
clist * struct_list;
size_t cur_token;
void * value;
size_t final_token;
int r;
int res;
cur_token = * indx;
struct_list = NULL;
r = parser(fd, buffer, &cur_token, &value, progr_rate, progr_fun,
body_progr_fun, items_progr_fun, context, msg_att_handler, msg_att_context);
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto err;
}
struct_list = clist_new();
if (struct_list == NULL) {
destructor(value);
res = MAILIMAP_ERROR_MEMORY;
goto err;
}
r = clist_append(struct_list, value);
if (r < 0) {
destructor(value);
res = MAILIMAP_ERROR_MEMORY;
goto free_list;
}
final_token = cur_token;
while (1) {
r = mailimap_unstrict_char_parse(fd, buffer, &cur_token, symbol);
if (r == MAILIMAP_ERROR_PARSE)
break;
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto free_list;
}
r = parser(fd, buffer, &cur_token, &value, progr_rate, progr_fun,
body_progr_fun, items_progr_fun, context, msg_att_handler, msg_att_context);
if (r == MAILIMAP_ERROR_PARSE)
break;
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto free_list;
}
r = clist_append(struct_list, value);
if (r < 0) {
destructor(value);
res = MAILIMAP_ERROR_MEMORY;
goto free_list;
}
final_token = cur_token;
}
* result = struct_list;
* indx = final_token;
return MAILIMAP_NO_ERROR;
free_list:
clist_foreach(struct_list, (clist_func) destructor, NULL);
clist_free(struct_list);
err:
return res;
}
int
mailimap_struct_list_parse(mailstream * fd, MMAPString * buffer,
size_t * indx, clist ** result,
char symbol,
mailimap_struct_parser * parser,
mailimap_struct_destructor * destructor,
size_t progr_rate,
progress_function * progr_fun)
{
clist * struct_list;
size_t cur_token;
void * value;
size_t final_token;
int r;
int res;
cur_token = * indx;
struct_list = NULL;
r = parser(fd, buffer, &cur_token, &value, progr_rate, progr_fun);
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto err;
}
struct_list = clist_new();
if (struct_list == NULL) {
destructor(value);
res = MAILIMAP_ERROR_MEMORY;
goto err;
}
r = clist_append(struct_list, value);
if (r < 0) {
destructor(value);
res = MAILIMAP_ERROR_MEMORY;
goto free_list;
}
final_token = cur_token;
while (1) {
r = mailimap_unstrict_char_parse(fd, buffer, &cur_token, symbol);
if (r == MAILIMAP_ERROR_PARSE)
break;
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto free_list;
}
r = parser(fd, buffer, &cur_token, &value, progr_rate, progr_fun);
if (r == MAILIMAP_ERROR_PARSE)
break;
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto free_list;
}
r = clist_append(struct_list, value);
if (r < 0) {
destructor(value);
res = MAILIMAP_ERROR_MEMORY;
goto free_list;
}
final_token = cur_token;
}
* result = struct_list;
* indx = final_token;
return MAILIMAP_NO_ERROR;
free_list:
clist_foreach(struct_list, (clist_func) destructor, NULL);
clist_free(struct_list);
err:
return res;
}
int
mailimap_struct_spaced_list_parse(mailstream * fd, MMAPString * buffer,
size_t * indx, clist ** result,
mailimap_struct_parser * parser,
mailimap_struct_destructor * destructor,
size_t progr_rate,
progress_function * progr_fun)
{
return mailimap_struct_list_parse(fd, buffer, indx, result,
' ', parser, destructor,
progr_rate, progr_fun);
}
static int
mailimap_struct_spaced_list_parse_progress(mailstream * fd, MMAPString * buffer,
size_t * indx, clist ** result,
mailimap_struct_parser_progress * parser,
mailimap_struct_destructor * destructor,
size_t progr_rate,
progress_function * progr_fun,
mailprogress_function * body_progr_fun,
mailprogress_function * items_progr_fun,
void * context,
mailimap_msg_att_handler * msg_att_handler,
void * msg_att_context)
{
return mailimap_struct_list_parse_progress(fd, buffer, indx, result,
' ', parser, destructor,
progr_rate, progr_fun,
body_progr_fun, items_progr_fun, context,
msg_att_handler, msg_att_context);
}
static int
mailimap_custom_string_parse(mailstream * fd, MMAPString * buffer,
size_t * indx, char ** result,
int (* is_custom_char)(char))
{
size_t begin;
size_t end;
char * gstr;
begin = * indx;
#ifdef UNSTRICT_SYNTAX
mailimap_space_parse(fd, buffer, &begin);
#endif
end = begin;
while (is_custom_char(buffer->str[end]))
end ++;
if (end != begin) {
gstr = malloc(end - begin + 1);
if (gstr == NULL)
return MAILIMAP_ERROR_MEMORY;
strncpy(gstr, buffer->str + begin, end - begin);
gstr[end - begin] = '\0';
* indx = end;
* result = gstr;
return MAILIMAP_NO_ERROR;
}
else
return MAILIMAP_ERROR_PARSE;
}
int
mailimap_nz_number_alloc_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
uint32_t ** result,
size_t progr_rate,
progress_function * progr_fun)
{
uint32_t number;
uint32_t * number_alloc;
size_t cur_token;
int r;
cur_token = * indx;
r = mailimap_nz_number_parse(fd, buffer, &cur_token, &number);
if (r != MAILIMAP_NO_ERROR)
return r;
number_alloc = mailimap_number_alloc_new(number);
if (number_alloc == NULL)
return MAILIMAP_ERROR_MEMORY;
* indx = cur_token;
* result = number_alloc;
return MAILIMAP_NO_ERROR;
}
static int is_ctl(char ch)
{
unsigned char uch = (unsigned char) ch;
return (uch <= 0x1F);
}
static int is_char(char ch)
{
#ifdef UNSTRICT_SYNTAX
return (ch != 0);
#else
unsigned char uch = ch;
return (uch >= 0x01) && (uch <= 0x7f);
#endif
}
static int is_alpha(char ch)
{
return ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && (ch <= 'z')));
}
static int is_digit(char ch)
{
return (ch >= '0') && (ch <= '9');
}
static int mailimap_digit_parse(mailstream * fd, MMAPString * buffer,
size_t * indx, int * result)
{
size_t cur_token;
cur_token = * indx;
if (is_digit(buffer->str[cur_token])) {
* result = buffer->str[cur_token] - '0';
cur_token ++;
* indx = cur_token;
return MAILIMAP_NO_ERROR;
}
else
return MAILIMAP_ERROR_PARSE;
}
/* ******************** parser **************************** */
/*
address = "(" addr-name SP addr-adl SP addr-mailbox SP
addr-host ")"
*/
static int mailimap_address_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_address ** result,
size_t progr_rate,
progress_function * progr_fun)
{
size_t cur_token;
char * addr_name;
char * addr_adl;
char * addr_mailbox;
char * addr_host;
struct mailimap_address * addr;
int r;
int res;
cur_token = * indx;
addr_name = NULL;
addr_adl = NULL;
addr_mailbox = NULL;
addr_host = NULL;
r = mailimap_oparenth_parse(fd, buffer, &cur_token);
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto err;
}
r = mailimap_addr_name_parse(fd, buffer, &cur_token, &addr_name,
progr_rate, progr_fun);
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto err;
}
r = mailimap_space_parse(fd, buffer, &cur_token);
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto addr_name_free;
}
r = mailimap_addr_adl_parse(fd, buffer, &cur_token, &addr_adl,
progr_rate, progr_fun);
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto addr_name_free;
}
r = mailimap_space_parse(fd, buffer, &cur_token);
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto addr_adl_free;
}
r = mailimap_addr_mailbox_parse(fd, buffer, &cur_token, &addr_mailbox,
progr_rate, progr_fun);
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto addr_adl_free;
}
r = mailimap_space_parse(fd, buffer, &cur_token);
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto addr_mailbox_free;
}
r = mailimap_addr_host_parse(fd, buffer, &cur_token, &addr_host,
progr_rate, progr_fun);
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto addr_mailbox_free;
}
r = mailimap_cparenth_parse(fd, buffer, &cur_token);
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto addr_host_free;
}
addr = mailimap_address_new(addr_name, addr_adl, addr_mailbox, addr_host);
if (addr == NULL) {
res = MAILIMAP_ERROR_MEMORY;
goto addr_host_free;
}
* result = addr;
* indx = cur_token;
return MAILIMAP_NO_ERROR;
addr_host_free:
mailimap_addr_host_free(addr_host);
addr_mailbox_free:
mailimap_addr_mailbox_free(addr_mailbox);
addr_adl_free:
mailimap_addr_adl_free(addr_adl);
addr_name_free:
mailimap_addr_name_free(addr_name);
err:
return res;
}
/*
addr-adl = nstring
; Holds route from [RFC-822] route-addr if
; non-NIL
*/
static int mailimap_addr_adl_parse(mailstream * fd, MMAPString * buffer,
size_t * indx, char ** result,
size_t progr_rate,
progress_function * progr_fun)
{
return mailimap_nstring_parse(fd, buffer, indx, result, NULL,
progr_rate, progr_fun);
}
/*
addr-host = nstring
; NIL indicates [RFC-822] group syntax.
; Otherwise, holds [RFC-822] domain name
*/
static int mailimap_addr_host_parse(mailstream * fd, MMAPString * buffer,
size_t * indx, char ** result,
size_t progr_rate,
progress_function * progr_fun)
{
return mailimap_nstring_parse(fd, buffer, indx, result, NULL,
progr_rate, progr_fun);
}
/*
addr-mailbox = nstring
; NIL indicates end of [RFC-822] group; if
; non-NIL and addr-host is NIL, holds
; [RFC-822] group name.
; Otherwise, holds [RFC-822] local-part
; after removing [RFC-822] quoting
*/
static int mailimap_addr_mailbox_parse(mailstream * fd, MMAPString * buffer,
size_t * indx, char ** result,
size_t progr_rate,
progress_function * progr_fun)
{
return mailimap_nstring_parse(fd, buffer, indx, result, NULL,
progr_rate, progr_fun);
}
/*
addr-name = nstring
; If non-NIL, holds phrase from [RFC-822]
; mailbox after removing [RFC-822] quoting
*/
static int mailimap_addr_name_parse(mailstream * fd, MMAPString * buffer,
size_t * indx, char ** result,
size_t progr_rate,
progress_function * progr_fun)
{
return mailimap_nstring_parse(fd, buffer, indx, result, NULL,
progr_rate, progr_fun);
}
/*
NOT IMPLEMENTED
append = "APPEND" SP mailbox [SP flag-list] [SP date-time] SP
literal
*/
/*
astring = 1*ASTRING-CHAR / string
*/
static int is_astring_char(char ch);
static int
mailimap_atom_astring_parse(mailstream * fd, MMAPString * buffer,
size_t * indx, char ** result,
size_t progr_rate,
progress_function * progr_fun)
{
return mailimap_custom_string_parse(fd, buffer, indx, result,
is_astring_char);
}
int
mailimap_astring_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
char ** result,
size_t progr_rate,
progress_function * progr_fun)
{
size_t cur_token;
char * astring;
int r;
cur_token = * indx;
r = mailimap_atom_astring_parse(fd, buffer, &cur_token, &astring,
progr_rate, progr_fun);
switch (r) {
case MAILIMAP_NO_ERROR:
break;
case MAILIMAP_ERROR_PARSE:
r = mailimap_string_parse(fd, buffer, &cur_token, &astring, NULL,
progr_rate, progr_fun);
if (r != MAILIMAP_NO_ERROR)
return r;
break;
default:
return r;
}
* result = astring;
* indx = cur_token;
return MAILIMAP_NO_ERROR;
}
/*
ASTRING-CHAR = ATOM-CHAR / resp-specials
*/
static int is_atom_char(char ch);
static int is_resp_specials(char ch);
static int is_astring_char(char ch)
{
if (is_atom_char(ch))
return TRUE;
if (is_resp_specials(ch))
return TRUE;
return FALSE;
}
/*
atom = 1*ATOM-CHAR
*/
int mailimap_atom_parse(mailstream * fd, MMAPString * buffer,
size_t * indx, char ** result,
size_t progr_rate,
progress_function * progr_fun)
{
return mailimap_custom_string_parse(fd, buffer, indx, result,
is_atom_char);
}
/*
ATOM-CHAR = <any CHAR except atom-specials>
*/
static int is_atom_specials(char ch);
static int is_atom_char(char ch)
{
if (is_atom_specials(ch))
return FALSE;
return is_char(ch);
}
/*
atom-specials = "(" / ")" / "{" / SP / CTL / list-wildcards /
quoted-specials / resp-specials
no "}" because there is no need (Mark Crispin)
*/
static int is_quoted_specials(char ch);
static int is_list_wildcards(char ch);
static int is_atom_specials(char ch)
{
switch (ch) {
case '(':
case ')':
case '{':
case ' ':
return TRUE;
};
if (is_ctl(ch))
return TRUE;
if (is_list_wildcards(ch))
return TRUE;
if (is_resp_specials(ch))
return TRUE;
return is_quoted_specials(ch);
}
/*
NOT IMPLEMENTED
authenticate = "AUTHENTICATE" SP auth-type *(CRLF base64)
*/
/*
auth-type = atom
; Defined by [SASL]
*/
static int mailimap_auth_type_parse(mailstream * fd, MMAPString * buffer,
size_t * indx, char ** result,
size_t progr_rate,
progress_function * progr_fun)
{
return mailimap_atom_parse(fd, buffer, indx, result,
progr_rate, progr_fun);
}
/*
base64 = *(4base64-char) [base64-terminal]
*/
static int is_base64_4char(char * str);
static int is_base64_terminal(char * str);
static int mailimap_base64_parse(mailstream * fd, MMAPString * buffer,
size_t * indx, char ** result,
size_t progr_rate,
progress_function * progr_fun)
{
size_t begin;
size_t end;
char * gstr;
begin = * indx;
end = begin;
while (is_base64_4char(buffer->str + end))
end += 4;
if (is_base64_terminal(buffer->str + end))
end += 4;
if (begin == end)
return MAILIMAP_ERROR_PARSE;
gstr = malloc(end - begin + 1);
if (gstr == NULL)
return MAILIMAP_ERROR_MEMORY;
strncpy(gstr, buffer->str + begin, end - begin);
gstr[end - begin] = '\0';
* result = gstr;
* indx = end;
return MAILIMAP_NO_ERROR;
}
/*
base64-char = ALPHA / DIGIT / "+" / "/"
; Case-sensitive
*/
static int is_base64_char(char ch)
{
return (is_alpha(ch) || is_digit(ch) || ch == '+' || ch == '/');
}
static int is_base64_4char(char * str)
{
size_t i;
for (i = 0 ; i < 4 ; i++)
if (!is_base64_char(str[i]))
return FALSE;
return TRUE;
}
/*
base64-terminal = (2base64-char "==") / (3base64-char "=")
*/
static int is_base64_terminal(char * str)
{
if (str[0] == 0)
return FALSE;
if (str[1] == 0)
return FALSE;
if (str[2] == 0)
return FALSE;
if (str[3] == 0)
return FALSE;
if (is_base64_char(str[0]) && is_base64_char(str[1])
&& str[2] == '=' && str[3] == '=')
return TRUE;
if (is_base64_char(str[0]) && is_base64_char(str[1])
&& is_base64_char(str[2]) && str[3] == '=')
return TRUE;
return FALSE;
}
/*
body = "(" (body-type-1part / body-type-mpart) ")"
*/
static int mailimap_body_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_body ** result,
size_t progr_rate,
progress_function * progr_fun)
{
struct mailimap_body_type_1part * body_type_1part;
struct mailimap_body_type_mpart * body_type_mpart;
struct mailimap_body * body;
size_t cur_token;
int type;
int r;
int res;
cur_token = * indx;
body_type_1part = NULL;
body_type_mpart = NULL;
r = mailimap_oparenth_parse(fd, buffer, &cur_token);
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto err;
}
type = MAILIMAP_BODY_ERROR; /* XXX - removes a gcc warning */
r = mailimap_body_type_1part_parse(fd, buffer, &cur_token, &body_type_1part,
progr_rate, progr_fun);
if (r == MAILIMAP_NO_ERROR)
type = MAILIMAP_BODY_1PART;
if (r == MAILIMAP_ERROR_PARSE) {
r = mailimap_body_type_mpart_parse(fd, buffer, &cur_token,
&body_type_mpart,
progr_rate, progr_fun);
if (r == MAILIMAP_NO_ERROR)
type = MAILIMAP_BODY_MPART;
}
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto err;
}
r = mailimap_cparenth_parse(fd, buffer, &cur_token);
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto free;
}
body = mailimap_body_new(type, body_type_1part, body_type_mpart);
if (body == NULL) {
res = MAILIMAP_ERROR_MEMORY;
goto free;
}
* result = body;
* indx = cur_token;
return MAILIMAP_NO_ERROR;
free:
if (body_type_1part)
mailimap_body_type_1part_free(body_type_1part);
if (body_type_mpart)
mailimap_body_type_mpart_free(body_type_mpart);
err:
return res;
}
/*
body-extension = nstring / number /
"(" body-extension *(SP body-extension) ")"
; Future expansion. Client implementations
; MUST accept body-extension fields. Server
; implementations MUST NOT generate
; body-extension fields except as defined by
; future standard or standards-track
; revisions of this specification.
*/
/*
"(" body-extension *(SP body-extension) ")"
*/
static int
mailimap_body_ext_list_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
clist ** result,
size_t progr_rate,
progress_function * progr_fun)
{
size_t cur_token;
clist * list;
int r;
int res;
cur_token = * indx;
r = mailimap_oparenth_parse(fd, buffer, &cur_token);
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto err;
}
r = mailimap_struct_spaced_list_parse(fd, buffer,
&cur_token, &list,
(mailimap_struct_parser * )
mailimap_body_extension_parse,
(mailimap_struct_destructor * )
mailimap_body_extension_free,
progr_rate, progr_fun);
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto err;
}
r = mailimap_cparenth_parse(fd, buffer, &cur_token);
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto free_list;
}
* indx = cur_token;
* result = list;
return MAILIMAP_NO_ERROR;
free_list:
clist_foreach(list, (clist_func) mailimap_body_extension_free, NULL);
clist_free(list);
err:
return res;
}
/*
body-extension = nstring / number /
"(" body-extension *(SP body-extension) ")"
; Future expansion. Client implementations
; MUST accept body-extension fields. Server
; implementations MUST NOT generate
; body-extension fields except as defined by
; future standard or standards-track
; revisions of this specification.
*/
static int
mailimap_body_extension_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_body_extension ** result,
size_t progr_rate,
progress_function * progr_fun)
{
size_t cur_token;
uint32_t number;
char * nstring;
clist * body_extension_list;
struct mailimap_body_extension * body_extension;
int type;
int r;
int res;
cur_token = * indx;
nstring = NULL;
number = 0;
body_extension_list = NULL;
type = MAILIMAP_BODY_EXTENSION_ERROR; /* XXX - removes a gcc warning */
r = mailimap_nstring_parse(fd, buffer, &cur_token, &nstring, NULL,
progr_rate, progr_fun);
if (r == MAILIMAP_NO_ERROR)
type = MAILIMAP_BODY_EXTENSION_NSTRING;
if (r == MAILIMAP_ERROR_PARSE) {
r = mailimap_number_parse(fd, buffer, &cur_token, &number);
if (r == MAILIMAP_NO_ERROR)
type = MAILIMAP_BODY_EXTENSION_NUMBER;
}
if (r == MAILIMAP_ERROR_PARSE) {
r = mailimap_body_ext_list_parse(fd, buffer, &cur_token,
&body_extension_list,
progr_rate, progr_fun);
if (r == MAILIMAP_NO_ERROR)
type = MAILIMAP_BODY_EXTENSION_LIST;
}
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto err;
}
body_extension = mailimap_body_extension_new(type, nstring, number,
body_extension_list);
if (body_extension == NULL) {
res = MAILIMAP_ERROR_MEMORY;
goto free;
}
* result = body_extension;
* indx = cur_token;
return MAILIMAP_NO_ERROR;
free:
if (nstring != NULL)
mailimap_nstring_free(nstring);
if (body_extension_list) {
clist_foreach(body_extension_list,
(clist_func) mailimap_body_extension_free,
NULL);
clist_free(body_extension_list);
}
err:
return res;
}
/*
body-ext-1part = body-fld-md5 [SP body-fld-dsp [SP body-fld-lang
*(SP body-extension)]]
; MUST NOT be returned on non-extensible
; "BODY" fetch
*/
/*
*(SP body-extension)
*/
static int
mailimap_body_ext_1part_4_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
clist ** body_ext_list,
size_t progr_rate,
progress_function * progr_fun)
{
size_t cur_token;
int r;
cur_token = * indx;
* body_ext_list = NULL;
r = mailimap_space_parse(fd, buffer, &cur_token);
if (r != MAILIMAP_NO_ERROR)
return r;
r = mailimap_struct_spaced_list_parse(fd, buffer, &cur_token,
body_ext_list,
(mailimap_struct_parser *)
mailimap_body_extension_parse,
(mailimap_struct_destructor *)
mailimap_body_extension_free,
progr_rate, progr_fun);
if (r != MAILIMAP_NO_ERROR)
return r;
* indx = cur_token;
return MAILIMAP_NO_ERROR;
}
/*
[SP body-fld-loc *(SP body-extension)]]]
*/
static int
mailimap_body_ext_1part_3_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
char ** fld_loc,
clist ** body_ext_list,
size_t progr_rate,
progress_function * progr_fun)
{
size_t cur_token;
int r;
cur_token = * indx;
* fld_loc = NULL;
* body_ext_list = NULL;
r = mailimap_space_parse(fd, buffer, &cur_token);
if (r != MAILIMAP_NO_ERROR)
return r;
r = mailimap_body_fld_loc_parse(fd, buffer, &cur_token, fld_loc,
progr_rate, progr_fun);
if (r != MAILIMAP_NO_ERROR)
return r;
r = mailimap_body_ext_1part_4_parse(fd, buffer, &cur_token,
body_ext_list, progr_rate, progr_fun);
if ((r != MAILIMAP_NO_ERROR) && (r != MAILIMAP_ERROR_PARSE))
return r;
* indx = cur_token;
return MAILIMAP_NO_ERROR;
}
/*
[SP body-fld-lang
*(SP body-extension)]]
*/
static int
mailimap_body_ext_1part_2_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_body_fld_lang ** fld_lang,
char ** fld_loc,
clist ** body_ext_list,
size_t progr_rate,
progress_function * progr_fun)
{
size_t cur_token;
int r;
cur_token = * indx;
* fld_lang = NULL;
* fld_loc = NULL;
* body_ext_list = NULL;
r = mailimap_space_parse(fd, buffer, &cur_token);
if (r != MAILIMAP_NO_ERROR)
return r;
r = mailimap_body_fld_lang_parse(fd, buffer, &cur_token, fld_lang,
progr_rate, progr_fun);
if (r != MAILIMAP_NO_ERROR)
return r;
r = mailimap_body_ext_1part_3_parse(fd, buffer, &cur_token,
fld_loc, body_ext_list, progr_rate, progr_fun);
if ((r != MAILIMAP_NO_ERROR) && (r != MAILIMAP_ERROR_PARSE))
return r;
* indx = cur_token;
return MAILIMAP_NO_ERROR;
}
/*
SP body-fld-dsp [SP body-fld-lang
*(SP body-extension)]]
*/
static int
mailimap_body_ext_1part_1_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_body_fld_dsp ** fld_dsp,
struct mailimap_body_fld_lang ** fld_lang,
char ** fld_loc,
clist ** body_ext_list,
size_t progr_rate,
progress_function * progr_fun)
{
size_t cur_token;
int r;
cur_token = * indx;
* fld_dsp = NULL;
* fld_lang = NULL;
* fld_loc = NULL;
* body_ext_list = NULL;
r = mailimap_space_parse(fd, buffer, &cur_token);
if (r != MAILIMAP_NO_ERROR)
return r;
r = mailimap_body_fld_dsp_parse(fd, buffer, &cur_token, fld_dsp,
progr_rate, progr_fun);
if (r != MAILIMAP_NO_ERROR)
return r;
r = mailimap_body_ext_1part_2_parse(fd, buffer, &cur_token,
fld_lang, fld_loc, body_ext_list,
progr_rate, progr_fun);
if ((r != MAILIMAP_NO_ERROR) && (r != MAILIMAP_ERROR_PARSE))
return r;
* indx = cur_token;
return MAILIMAP_NO_ERROR;
}
/*
body-ext-1part = body-fld-md5 [SP body-fld-dsp [SP body-fld-lang
*(SP body-extension)]]
; MUST NOT be returned on non-extensible
; "BODY" fetch
*/
static int
mailimap_body_ext_1part_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_body_ext_1part ** result,
size_t progr_rate,
progress_function * progr_fun)
{
size_t cur_token;
char * fld_md5;
struct mailimap_body_fld_dsp * fld_dsp;
struct mailimap_body_fld_lang * fld_lang;
char * fld_loc;
clist * body_ext_list;
int r;
int res;
struct mailimap_body_ext_1part * ext_1part;
cur_token = * indx;
fld_md5 = NULL;
fld_dsp = NULL;
fld_lang = NULL;
fld_loc = NULL;
body_ext_list = NULL;
r = mailimap_body_fld_md5_parse(fd, buffer, &cur_token, &fld_md5,
progr_rate, progr_fun);
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto err;
}
r = mailimap_body_ext_1part_1_parse(fd, buffer, &cur_token,
&fld_dsp,
&fld_lang,
&fld_loc,
&body_ext_list,
progr_rate, progr_fun);
if ((r != MAILIMAP_NO_ERROR) && (r != MAILIMAP_ERROR_PARSE)) {
res = r;
goto free;
}
ext_1part = mailimap_body_ext_1part_new(fld_md5, fld_dsp, fld_lang, fld_loc,
body_ext_list);
if (ext_1part == NULL) {
res = MAILIMAP_ERROR_MEMORY;
goto free;
}
* result = ext_1part;
* indx = cur_token;
return MAILIMAP_NO_ERROR;
free:
if (body_ext_list) {
clist_foreach(body_ext_list, (clist_func) mailimap_body_extension_free,
NULL);
clist_free(body_ext_list);
}
mailimap_body_fld_loc_free(fld_loc);
if (fld_lang)
mailimap_body_fld_lang_free(fld_lang);
if (fld_dsp)
mailimap_body_fld_dsp_free(fld_dsp);
mailimap_body_fld_md5_free(fld_md5);
err:
return res;
}
/*
body-ext-mpart = body-fld-param [SP body-fld-dsp [SP body-fld-lang
*(SP body-extension)]]
; MUST NOT be returned on non-extensible
; "BODY" fetch
*/
static int
mailimap_body_ext_mpart_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_body_ext_mpart ** result,
size_t progr_rate,
progress_function * progr_fun)
{
size_t cur_token;
struct mailimap_body_fld_dsp * fld_dsp;
struct mailimap_body_fld_lang * fld_lang;
char * fld_loc;
struct mailimap_body_fld_param * fld_param;
clist * body_ext_list;
struct mailimap_body_ext_mpart * ext_mpart;
int r;
int res;
cur_token = * indx;
fld_param = NULL;
fld_dsp = NULL;
fld_lang = NULL;
fld_loc = NULL;
body_ext_list = NULL;
r = mailimap_body_fld_param_parse(fd, buffer, &cur_token, &fld_param,
progr_rate, progr_fun);
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto err;
}
r = mailimap_body_ext_1part_1_parse(fd, buffer, &cur_token,
&fld_dsp,
&fld_lang,
&fld_loc,
&body_ext_list,
progr_rate, progr_fun);
if ((r != MAILIMAP_NO_ERROR) && (r != MAILIMAP_ERROR_PARSE)) {
res = r;
goto free;
}
ext_mpart = mailimap_body_ext_mpart_new(fld_param, fld_dsp, fld_lang, fld_loc,
body_ext_list);
if (ext_mpart == NULL) {
res = MAILIMAP_ERROR_MEMORY;
goto free;
}
* result = ext_mpart;
* indx = cur_token;
return MAILIMAP_NO_ERROR;
free:
if (body_ext_list) {
clist_foreach(body_ext_list, (clist_func) mailimap_body_extension_free,
NULL);
clist_free(body_ext_list);
}
mailimap_body_fld_loc_free(fld_loc);
if (fld_lang)
mailimap_body_fld_lang_free(fld_lang);
if (fld_dsp)
mailimap_body_fld_dsp_free(fld_dsp);
if (fld_param != NULL)
mailimap_body_fld_param_free(fld_param);
err:
return res;
}
/*
body-fields = body-fld-param SP body-fld-id SP body-fld-desc SP
body-fld-enc SP body-fld-octets
*/
static int
mailimap_body_fields_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_body_fields ** result,
size_t progr_rate,
progress_function * progr_fun)
{
struct mailimap_body_fields * body_fields;
size_t cur_token;
struct mailimap_body_fld_param * body_fld_param;
char * body_fld_id;
char * body_fld_desc;
struct mailimap_body_fld_enc * body_fld_enc;
uint32_t body_fld_octets;
int r;
int res;
body_fld_param = NULL;
body_fld_id = NULL;
body_fld_desc = NULL;
body_fld_enc = NULL;
body_fld_octets = 0;
cur_token = * indx;
r = mailimap_body_fld_param_parse(fd, buffer, &cur_token, &body_fld_param,
progr_rate, progr_fun);
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto err;
}
r = mailimap_space_parse(fd, buffer, &cur_token);
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto fld_param_free;
}
r = mailimap_body_fld_id_parse(fd, buffer, &cur_token, &body_fld_id,
progr_rate, progr_fun);
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto fld_param_free;
}
r = mailimap_space_parse(fd, buffer, &cur_token);
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto fld_id_free;
}
r = mailimap_body_fld_desc_parse(fd, buffer, &cur_token, &body_fld_desc,
progr_rate, progr_fun);
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto fld_id_free;
}
r = mailimap_space_parse(fd, buffer, &cur_token);
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto fld_desc_free;
}
r = mailimap_body_fld_enc_parse(fd, buffer, &cur_token, &body_fld_enc,
progr_rate, progr_fun);
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto fld_desc_free;
}
r = mailimap_space_parse(fd, buffer, &cur_token);
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto fld_enc_free;
}
r = mailimap_body_fld_octets_parse(fd, buffer, &cur_token,
&body_fld_octets);
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto fld_enc_free;
}
body_fields = mailimap_body_fields_new(body_fld_param,
body_fld_id,
body_fld_desc,
body_fld_enc,
body_fld_octets);
if (body_fields == NULL) {
res = MAILIMAP_ERROR_MEMORY;
goto fld_enc_free;
}
* result = body_fields;
* indx = cur_token;
return MAILIMAP_NO_ERROR;
fld_enc_free:
mailimap_body_fld_enc_free(body_fld_enc);
fld_desc_free:
mailimap_body_fld_desc_free(body_fld_desc);
fld_id_free:
mailimap_body_fld_id_free(body_fld_id);
fld_param_free:
if (body_fld_param != NULL)
mailimap_body_fld_param_free(body_fld_param);
err:
return res;
}
/*
body-fld-desc = nstring
*/
static int mailimap_body_fld_desc_parse(mailstream * fd, MMAPString * buffer,
size_t * indx, char ** result,
size_t progr_rate,
progress_function * progr_fun)
{
return mailimap_nstring_parse(fd, buffer, indx, result, NULL,
progr_rate, progr_fun);
}
/*
body-fld-dsp = "(" string SP body-fld-param ")" / nil
*/
static int
mailimap_body_fld_dsp_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_body_fld_dsp ** result,
size_t progr_rate,
progress_function * progr_fun)
{
size_t cur_token;
char * name;
struct mailimap_body_fld_param * body_fld_param;
struct mailimap_body_fld_dsp * body_fld_dsp;
int res;
int r;
cur_token = * indx;
name = NULL;
body_fld_param = NULL;
r = mailimap_nil_parse(fd, buffer, &cur_token);
if (r == MAILIMAP_NO_ERROR) {
* result = NULL;
* indx = cur_token;
return MAILIMAP_NO_ERROR;
}
if (r != MAILIMAP_ERROR_PARSE) {
res = r;
goto err;
}
r = mailimap_oparenth_parse(fd, buffer, &cur_token);
if (r != MAILIMAP_NO_ERROR) {
// workaround for exchange
r = mailimap_string_parse(fd, buffer, &cur_token, &name, NULL,
+ progr_rate, progr_fun);
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto err;
}
r = mailimap_space_parse(fd, buffer, &cur_token);
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto string_free;
}
r = mailimap_body_fld_param_parse(fd, buffer, &cur_token,
&body_fld_param,
progr_rate, progr_fun);
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto string_free;
}
goto create_body_fld_dsp;
}
r = mailimap_string_parse(fd, buffer, &cur_token, &name, NULL,
progr_rate, progr_fun);
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto err;
}
r = mailimap_space_parse(fd, buffer, &cur_token);
/* workaround for IMAPrev1 Citadel */
if (r == MAILIMAP_ERROR_PARSE) {
r = mailimap_cparenth_parse(fd, buffer, &cur_token);
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto string_free;
}
body_fld_param = NULL;
goto create_body_fld_dsp;
}
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto string_free;
}
r = mailimap_body_fld_param_parse(fd, buffer, &cur_token,
&body_fld_param,
progr_rate, progr_fun);
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto string_free;
}
r = mailimap_cparenth_parse(fd, buffer, &cur_token);
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto string_free;
}
create_body_fld_dsp:
body_fld_dsp = mailimap_body_fld_dsp_new(name, body_fld_param);
if (body_fld_dsp == NULL) {
res = MAILIMAP_ERROR_MEMORY;
goto fld_param_free;
}
* indx = cur_token;
* result = body_fld_dsp;
return MAILIMAP_NO_ERROR;
fld_param_free:
if (body_fld_param != NULL)
mailimap_body_fld_param_free(body_fld_param);
string_free:
mailimap_string_free(name);
err:
return res;
}
/*
body-fld-enc = (DQUOTE ("7BIT" / "8BIT" / "BINARY" / "BASE64"/
"QUOTED-PRINTABLE") DQUOTE) / string
*/
static inline int
mailimap_body_fld_known_enc_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
int * result,
size_t progr_rate,
progress_function * progr_fun)
{
size_t cur_token;
int type;
int r;
int res;
cur_token = * indx;
r = mailimap_dquote_parse(fd, buffer, &cur_token);
if (r == MAILIMAP_ERROR_PARSE) {
/* workaround for exchange */
type = mailimap_encoding_get_token_value(fd, buffer, &cur_token);
if (type == -1) {
res = MAILIMAP_ERROR_PARSE;
goto err;
}
* result = type;
* indx = cur_token;
return MAILIMAP_NO_ERROR;
}
else if (r != MAILIMAP_NO_ERROR) {
res = r;
goto err;
}
type = mailimap_encoding_get_token_value(fd, buffer, &cur_token);
if (type == -1) {
res = MAILIMAP_ERROR_PARSE;
goto err;
}
r = mailimap_dquote_parse(fd, buffer, &cur_token);
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto err;
}
* result = type;
* indx = cur_token;
return MAILIMAP_NO_ERROR;
err:
return res;
}
static int
mailimap_body_fld_enc_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_body_fld_enc ** result,
size_t progr_rate,
progress_function * progr_fun)
{
size_t cur_token;
int type;
char * value;
struct mailimap_body_fld_enc * body_fld_enc;
int r;
int res;
cur_token = * indx;
r = mailimap_body_fld_known_enc_parse(fd, buffer, &cur_token,
&type, progr_rate, progr_fun);
if (r == MAILIMAP_NO_ERROR) {
value = NULL;
}
else if (r == MAILIMAP_ERROR_PARSE) {
type = MAILIMAP_BODY_FLD_ENC_OTHER;
r = mailimap_nstring_parse(fd, buffer, &cur_token, &value, NULL,
progr_rate, progr_fun);
if (r == MAILIMAP_ERROR_PARSE) {
/* workaround for exchange */
r = mailimap_astring_parse(fd, buffer, &cur_token, &value,
progr_rate, progr_fun);
}
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto err;
}
if (value == NULL) {
type = MAILIMAP_BODY_FLD_ENC_8BIT;
}
}
else {
res = r;
goto err;
}
body_fld_enc = mailimap_body_fld_enc_new(type, value);
if (body_fld_enc == NULL) {
res = MAILIMAP_ERROR_MEMORY;
goto value_free;
}
* result = body_fld_enc;
* indx = cur_token;
return MAILIMAP_NO_ERROR;
value_free:
if (value)
mailimap_string_free(value);
err:
return res;
}
/*
body-fld-id = nstring
*/
static int mailimap_body_fld_id_parse(mailstream * fd, MMAPString * buffer,
size_t * indx, char ** result,
size_t progr_rate,
progress_function * progr_fun)
{
int r;
r = mailimap_nstring_parse(fd, buffer, indx, result, NULL,
progr_rate, progr_fun);
if (r == MAILIMAP_NO_ERROR)
return MAILIMAP_NO_ERROR;
r = mailimap_atom_parse(fd, buffer, indx, result,
progr_rate, progr_fun);
if (r == MAILIMAP_NO_ERROR)
return MAILIMAP_NO_ERROR;
return r;
}
/*
body-fld-lang = nstring / "(" string *(SP string) ")"
*/
/*
"(" string *(SP string) ")"
*/
static int
mailimap_body_fld_lang_list_parse(mailstream * fd, MMAPString * buffer,
size_t * indx, clist ** result,
size_t progr_rate,
progress_function * progr_fun)
{
size_t cur_token;
clist * list;
int r;
int res;
cur_token = * indx;
r = mailimap_oparenth_parse(fd, buffer, &cur_token);
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto err;
}
list = clist_new();
if (list == NULL) {
res = MAILIMAP_ERROR_MEMORY;
goto err;
}
while (1) {
char * elt;
mailimap_space_parse(fd, buffer, &cur_token);
r = mailimap_string_parse(fd, buffer, &cur_token, &elt, NULL,
progr_rate, progr_fun);
if (r == MAILIMAP_ERROR_PARSE)
break;
else if (r == MAILIMAP_NO_ERROR) {
r = clist_append(list, elt);
if (r < 0) {
mailimap_string_free(elt);
res = r;
goto list_free;
}
}
else {
res = r;
goto list_free;
}
}
r = mailimap_cparenth_parse(fd, buffer, &cur_token);
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto list_free;
}
* indx = cur_token;
* result = list;
return MAILIMAP_NO_ERROR;
list_free:
clist_foreach(list, (clist_func) mailimap_string_free, NULL);
clist_free(list);
err:
return res;
}
/*
body-fld-lang = nstring / "(" string *(SP string) ")"
*/
static int
mailimap_body_fld_lang_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_body_fld_lang ** result,
size_t progr_rate,
progress_function * progr_fun)
{
char * value;
clist * list;
struct mailimap_body_fld_lang * fld_lang;
int type;
int r;
int res;
size_t cur_token;
cur_token = * indx;
value = NULL;
list = NULL;
type = MAILIMAP_BODY_FLD_LANG_ERROR; /* XXX - removes a gcc warning */
r = mailimap_nstring_parse(fd, buffer, &cur_token, &value, NULL,
progr_rate, progr_fun);
if (r == MAILIMAP_NO_ERROR)
type = MAILIMAP_BODY_FLD_LANG_SINGLE;
if (r == MAILIMAP_ERROR_PARSE) {
r = mailimap_body_fld_lang_list_parse(fd, buffer, &cur_token, &list,
progr_rate, progr_fun);
if (r == MAILIMAP_NO_ERROR)
type = MAILIMAP_BODY_FLD_LANG_LIST;
}
if (r != MAILIMAP_NO_ERROR) {
res = r;
goto err;
}
fld_lang = mailimap_body_fld_lang_new(type, value, list);
if (fld_lang == NULL) {
res = MAILIMAP_ERROR_MEMORY;
goto free;
}
* indx = cur_token;
* result = fld_lang;
return MAILIMAP_NO_ERROR;
free:
if (value)
mailimap_nstring_free(value);
if (list) {
clist_foreach(list, (clist_func) mailimap_string_free, NULL);
clist_free(list);
}
err:
return res;
}
/*
body-fld-lines = number
*/
static int mailimap_body_fld_lines_parse(mailstream * fd,
MMAPString * buffer, size_t * indx,
uint32_t * result)
{
return mailimap_number_parse(fd, buffer, indx, result);
}
/*
body-fld-md5 = nstring
*/
static int mailimap_body_fld_md5_parse(mailstream * fd, MMAPString * buffer,
size_t * indx, char ** result,
size_t progr_rate,
progress_function * progr_fun)
{
int r;
uint32_t value;
size_t cur_token;
char * md5_value;
r = mailimap_nstring_parse(fd, buffer, indx, result, NULL,
progr_rate, progr_fun);
if (r == MAILIMAP_NO_ERROR)
return r;
cur_token = * indx;
/* workaround for google IMAP */
r = mailimap_number_parse(fd, buffer, &cur_token, &value);
if (r != MAILIMAP_NO_ERROR) {
return r;
}
r = mailimap_space_parse(fd, buffer, &cur_token);
if (r != MAILIMAP_NO_ERROR) {
return r;
}
r = mailimap_nstring_parse(fd, buffer, &cur_token, &md5_value, NULL,
progr_rate, progr_fun);
if (r != MAILIMAP_NO_ERROR) {
return r;
}
* indx = cur_token;
* result = md5_value;
return MAILIMAP_NO_ERROR;
}
static int mailimap_body_fld_loc_parse(mailstream * fd, MMAPString * buffer,
size_t * indx, char ** result,
size_t progr_rate,
progress_function * progr_fun)
{
return mailimap_nstring_parse(fd, buffer, indx, result, NULL,
progr_rate, progr_fun);
}
/*
body-fld-octets = number
*/
static int mailimap_body_fld_octets_parse(mailstream * fd,
MMAPString * buffer, size_t * indx,
uint32_t * result)
{
int r;
r = mailimap_number_parse(fd, buffer, indx, result);
if (r == MAILIMAP_NO_ERROR)
return r;
// workaround for Microsoft Exchange: it can provides -1 as octets
r = mailimap_token_case_insensitive_parse(fd, buffer, indx, "-1");
if (r == MAILIMAP_NO_ERROR) {
* result = 0;
return MAILIMAP_NO_ERROR;
}
// workaround for mbox mail for mac
r = mailimap_nil_parse(fd, buffer, indx);
if (r == MAILIMAP_NO_ERROR) {
* result = 0;
return MAILIMAP_NO_ERROR;
}
return MAILIMAP_ERROR_PARSE;
}
/*
body-fld-param = "(" string SP string *(SP string SP string) ")" / nil
*/
/*
string SP string
*/
static int
mailimap_single_body_fld_param_parse(mailstream * fd, MMAPString * buffer,
size_t * indx,
struct mailimap_single_body_fld_param **
result,
size_t progr_rate,
progress_function * progr_fun)
{
struct mailimap_single_body_fld_param * param;
char * name;
char * value;
size_t cur_token;
int r;
int res;
int workaround_used;
cur_token = * indx;
name = NULL;
value = NULL;
r = mailimap_string_parse(fd, buffer, &cur_token, &name, NULL,
progr_rate, progr_fun);