113498266Sopenharmony_ci#ifndef HEADER_CURL_MIME_H
213498266Sopenharmony_ci#define HEADER_CURL_MIME_H
313498266Sopenharmony_ci/***************************************************************************
413498266Sopenharmony_ci *                                  _   _ ____  _
513498266Sopenharmony_ci *  Project                     ___| | | |  _ \| |
613498266Sopenharmony_ci *                             / __| | | | |_) | |
713498266Sopenharmony_ci *                            | (__| |_| |  _ <| |___
813498266Sopenharmony_ci *                             \___|\___/|_| \_\_____|
913498266Sopenharmony_ci *
1013498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
1113498266Sopenharmony_ci *
1213498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which
1313498266Sopenharmony_ci * you should have received as part of this distribution. The terms
1413498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html.
1513498266Sopenharmony_ci *
1613498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell
1713498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is
1813498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file.
1913498266Sopenharmony_ci *
2013498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
2113498266Sopenharmony_ci * KIND, either express or implied.
2213498266Sopenharmony_ci *
2313498266Sopenharmony_ci * SPDX-License-Identifier: curl
2413498266Sopenharmony_ci *
2513498266Sopenharmony_ci ***************************************************************************/
2613498266Sopenharmony_ci
2713498266Sopenharmony_ci#include "curl_setup.h"
2813498266Sopenharmony_ci
2913498266Sopenharmony_ci#define MIME_BOUNDARY_DASHES            24  /* leading boundary dashes */
3013498266Sopenharmony_ci#define MIME_RAND_BOUNDARY_CHARS        22  /* Nb. of random boundary chars. */
3113498266Sopenharmony_ci#define MAX_ENCODED_LINE_LENGTH         76  /* Maximum encoded line length. */
3213498266Sopenharmony_ci#define ENCODING_BUFFER_SIZE            256 /* Encoding temp buffers size. */
3313498266Sopenharmony_ci
3413498266Sopenharmony_ci/* Part flags. */
3513498266Sopenharmony_ci#define MIME_USERHEADERS_OWNER  (1 << 0)
3613498266Sopenharmony_ci#define MIME_BODY_ONLY          (1 << 1)
3713498266Sopenharmony_ci#define MIME_FAST_READ          (1 << 2)
3813498266Sopenharmony_ci
3913498266Sopenharmony_ci#define FILE_CONTENTTYPE_DEFAULT        "application/octet-stream"
4013498266Sopenharmony_ci#define MULTIPART_CONTENTTYPE_DEFAULT   "multipart/mixed"
4113498266Sopenharmony_ci#define DISPOSITION_DEFAULT             "attachment"
4213498266Sopenharmony_ci
4313498266Sopenharmony_ci/* Part source kinds. */
4413498266Sopenharmony_cienum mimekind {
4513498266Sopenharmony_ci  MIMEKIND_NONE = 0,            /* Part not set. */
4613498266Sopenharmony_ci  MIMEKIND_DATA,                /* Allocated mime data. */
4713498266Sopenharmony_ci  MIMEKIND_FILE,                /* Data from file. */
4813498266Sopenharmony_ci  MIMEKIND_CALLBACK,            /* Data from `read' callback. */
4913498266Sopenharmony_ci  MIMEKIND_MULTIPART,           /* Data is a mime subpart. */
5013498266Sopenharmony_ci  MIMEKIND_LAST
5113498266Sopenharmony_ci};
5213498266Sopenharmony_ci
5313498266Sopenharmony_ci/* Readback state tokens. */
5413498266Sopenharmony_cienum mimestate {
5513498266Sopenharmony_ci  MIMESTATE_BEGIN,              /* Readback has not yet started. */
5613498266Sopenharmony_ci  MIMESTATE_CURLHEADERS,        /* In curl-generated headers. */
5713498266Sopenharmony_ci  MIMESTATE_USERHEADERS,        /* In caller's supplied headers. */
5813498266Sopenharmony_ci  MIMESTATE_EOH,                /* End of headers. */
5913498266Sopenharmony_ci  MIMESTATE_BODY,               /* Placeholder. */
6013498266Sopenharmony_ci  MIMESTATE_BOUNDARY1,          /* In boundary prefix. */
6113498266Sopenharmony_ci  MIMESTATE_BOUNDARY2,          /* In boundary. */
6213498266Sopenharmony_ci  MIMESTATE_CONTENT,            /* In content. */
6313498266Sopenharmony_ci  MIMESTATE_END,                /* End of part reached. */
6413498266Sopenharmony_ci  MIMESTATE_LAST
6513498266Sopenharmony_ci};
6613498266Sopenharmony_ci
6713498266Sopenharmony_ci/* Mime headers strategies. */
6813498266Sopenharmony_cienum mimestrategy {
6913498266Sopenharmony_ci  MIMESTRATEGY_MAIL,            /* Mime mail. */
7013498266Sopenharmony_ci  MIMESTRATEGY_FORM,            /* HTTP post form. */
7113498266Sopenharmony_ci  MIMESTRATEGY_LAST
7213498266Sopenharmony_ci};
7313498266Sopenharmony_ci
7413498266Sopenharmony_ci/* Content transfer encoder. */
7513498266Sopenharmony_cistruct mime_encoder {
7613498266Sopenharmony_ci  const char *   name;          /* Encoding name. */
7713498266Sopenharmony_ci  size_t         (*encodefunc)(char *buffer, size_t size, bool ateof,
7813498266Sopenharmony_ci                               curl_mimepart *part);  /* Encoded read. */
7913498266Sopenharmony_ci  curl_off_t     (*sizefunc)(curl_mimepart *part);  /* Encoded size. */
8013498266Sopenharmony_ci};
8113498266Sopenharmony_ci
8213498266Sopenharmony_ci/* Content transfer encoder state. */
8313498266Sopenharmony_cistruct mime_encoder_state {
8413498266Sopenharmony_ci  size_t         pos;           /* Position on output line. */
8513498266Sopenharmony_ci  size_t         bufbeg;        /* Next data index in input buffer. */
8613498266Sopenharmony_ci  size_t         bufend;        /* First unused byte index in input buffer. */
8713498266Sopenharmony_ci  char           buf[ENCODING_BUFFER_SIZE]; /* Input buffer. */
8813498266Sopenharmony_ci};
8913498266Sopenharmony_ci
9013498266Sopenharmony_ci/* Mime readback state. */
9113498266Sopenharmony_cistruct mime_state {
9213498266Sopenharmony_ci  enum mimestate state;       /* Current state token. */
9313498266Sopenharmony_ci  void *ptr;                  /* State-dependent pointer. */
9413498266Sopenharmony_ci  curl_off_t offset;          /* State-dependent offset. */
9513498266Sopenharmony_ci};
9613498266Sopenharmony_ci
9713498266Sopenharmony_ci/* Boundary string length. */
9813498266Sopenharmony_ci#define MIME_BOUNDARY_LEN (MIME_BOUNDARY_DASHES + MIME_RAND_BOUNDARY_CHARS)
9913498266Sopenharmony_ci
10013498266Sopenharmony_ci/* A mime multipart. */
10113498266Sopenharmony_cistruct curl_mime {
10213498266Sopenharmony_ci  curl_mimepart *parent;           /* Parent part. */
10313498266Sopenharmony_ci  curl_mimepart *firstpart;        /* First part. */
10413498266Sopenharmony_ci  curl_mimepart *lastpart;         /* Last part. */
10513498266Sopenharmony_ci  char boundary[MIME_BOUNDARY_LEN + 1]; /* The part boundary. */
10613498266Sopenharmony_ci  struct mime_state state;         /* Current readback state. */
10713498266Sopenharmony_ci};
10813498266Sopenharmony_ci
10913498266Sopenharmony_ci/* A mime part. */
11013498266Sopenharmony_cistruct curl_mimepart {
11113498266Sopenharmony_ci  curl_mime *parent;               /* Parent mime structure. */
11213498266Sopenharmony_ci  curl_mimepart *nextpart;         /* Forward linked list. */
11313498266Sopenharmony_ci  enum mimekind kind;              /* The part kind. */
11413498266Sopenharmony_ci  unsigned int flags;              /* Flags. */
11513498266Sopenharmony_ci  char *data;                      /* Memory data or file name. */
11613498266Sopenharmony_ci  curl_read_callback readfunc;     /* Read function. */
11713498266Sopenharmony_ci  curl_seek_callback seekfunc;     /* Seek function. */
11813498266Sopenharmony_ci  curl_free_callback freefunc;     /* Argument free function. */
11913498266Sopenharmony_ci  void *arg;                       /* Argument to callback functions. */
12013498266Sopenharmony_ci  FILE *fp;                        /* File pointer. */
12113498266Sopenharmony_ci  struct curl_slist *curlheaders;  /* Part headers. */
12213498266Sopenharmony_ci  struct curl_slist *userheaders;  /* Part headers. */
12313498266Sopenharmony_ci  char *mimetype;                  /* Part mime type. */
12413498266Sopenharmony_ci  char *filename;                  /* Remote file name. */
12513498266Sopenharmony_ci  char *name;                      /* Data name. */
12613498266Sopenharmony_ci  curl_off_t datasize;             /* Expected data size. */
12713498266Sopenharmony_ci  struct mime_state state;         /* Current readback state. */
12813498266Sopenharmony_ci  const struct mime_encoder *encoder; /* Content data encoder. */
12913498266Sopenharmony_ci  struct mime_encoder_state encstate; /* Data encoder state. */
13013498266Sopenharmony_ci  size_t lastreadstatus;           /* Last read callback returned status. */
13113498266Sopenharmony_ci};
13213498266Sopenharmony_ci
13313498266Sopenharmony_ciCURLcode Curl_mime_add_header(struct curl_slist **slp, const char *fmt, ...)
13413498266Sopenharmony_ci  CURL_PRINTF(2, 3);
13513498266Sopenharmony_ci
13613498266Sopenharmony_ci#if !defined(CURL_DISABLE_MIME) && (!defined(CURL_DISABLE_HTTP) ||      \
13713498266Sopenharmony_ci                                    !defined(CURL_DISABLE_SMTP) ||      \
13813498266Sopenharmony_ci                                    !defined(CURL_DISABLE_IMAP))
13913498266Sopenharmony_ci
14013498266Sopenharmony_ci/* Prototypes. */
14113498266Sopenharmony_civoid Curl_mime_initpart(struct curl_mimepart *part);
14213498266Sopenharmony_civoid Curl_mime_cleanpart(struct curl_mimepart *part);
14313498266Sopenharmony_ciCURLcode Curl_mime_duppart(struct Curl_easy *data,
14413498266Sopenharmony_ci                           struct curl_mimepart *dst,
14513498266Sopenharmony_ci                           const curl_mimepart *src);
14613498266Sopenharmony_ciCURLcode Curl_mime_set_subparts(struct curl_mimepart *part,
14713498266Sopenharmony_ci                                struct curl_mime *subparts,
14813498266Sopenharmony_ci                                int take_ownership);
14913498266Sopenharmony_ciCURLcode Curl_mime_prepare_headers(struct Curl_easy *data,
15013498266Sopenharmony_ci                                   struct curl_mimepart *part,
15113498266Sopenharmony_ci                                   const char *contenttype,
15213498266Sopenharmony_ci                                   const char *disposition,
15313498266Sopenharmony_ci                                   enum mimestrategy strategy);
15413498266Sopenharmony_cicurl_off_t Curl_mime_size(struct curl_mimepart *part);
15513498266Sopenharmony_cisize_t Curl_mime_read(char *buffer, size_t size, size_t nitems,
15613498266Sopenharmony_ci                      void *instream);
15713498266Sopenharmony_ciCURLcode Curl_mime_rewind(struct curl_mimepart *part);
15813498266Sopenharmony_ciconst char *Curl_mime_contenttype(const char *filename);
15913498266Sopenharmony_civoid Curl_mime_unpause(struct curl_mimepart *part);
16013498266Sopenharmony_ci
16113498266Sopenharmony_ci#else
16213498266Sopenharmony_ci/* if disabled */
16313498266Sopenharmony_ci#define Curl_mime_initpart(x)
16413498266Sopenharmony_ci#define Curl_mime_cleanpart(x)
16513498266Sopenharmony_ci#define Curl_mime_duppart(x,y,z) CURLE_OK /* Nothing to duplicate. Succeed */
16613498266Sopenharmony_ci#define Curl_mime_set_subparts(a,b,c) CURLE_NOT_BUILT_IN
16713498266Sopenharmony_ci#define Curl_mime_prepare_headers(a,b,c,d,e) CURLE_NOT_BUILT_IN
16813498266Sopenharmony_ci#define Curl_mime_size(x) (curl_off_t) -1
16913498266Sopenharmony_ci#define Curl_mime_read NULL
17013498266Sopenharmony_ci#define Curl_mime_rewind(x) ((void)x, CURLE_NOT_BUILT_IN)
17113498266Sopenharmony_ci#define Curl_mime_unpause(x)
17213498266Sopenharmony_ci#endif
17313498266Sopenharmony_ci
17413498266Sopenharmony_ci
17513498266Sopenharmony_ci#endif /* HEADER_CURL_MIME_H */
176