/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * This Original Code has been modified by IBM Corporation. Modifications made * by IBM described herein are Copyright (c) International Business Machines * Corporation, 2000. Modifications to Mozilla code or documentation identified * per MPL Section 3.3 * * Date Modified by Description of modification * 04/20/2000 IBM Corp. OS/2 VisualAge build. */ /* Thoughts on how to implement this: = if the type of this multipart/related is not text/html, then treat it the same as multipart/mixed. = For each part in this multipart/related = if this part is not the "top" part = then save this part to a tmp file or a memory object, kind-of like what we do for multipart/alternative sub-parts. If this is an object we're blocked on (see below) send its data along. = else = emit this part (remember, it's of type text/html) = at some point, layout may load a URL for . we intercept that. = if one of our cached parts has that cid, return the data for it. = else, "block", the same way the image library blocks layout when it doesn't yet have the size of the image. = at some point, layout may load a URL for . we need to intercept that too. = expand the URL, and compare it to our cached objects. if it matches, return it. = else block on it. = once we get to the end, if we have any sub-part references that we're still blocked on, map over them: = if they're cid: references, close them ("broken image" results.) = if they're URLs, then load them in the normal way. -------------------------------------------------- Ok, that's fairly complicated. How about an approach where we go through all the parts first, and don't emit until the end? = if the type of this multipart/related is not text/html, then treat it the same as multipart/mixed. = For each part in this multipart/related = save this part to a tmp file or a memory object, like what we do for multipart/alternative sub-parts. = Emit the "top" part (the text/html one) = intercept all calls to NET_GetURL, to allow us to rewrite the URL. (hook into netlib, or only into imglib's calls to GetURL?) (make sure we're behaving in a context-local way.) = when a URL is loaded, look through our cached parts for a match. = if we find one, map that URL to a "cid:" URL = else, let it load normally = at some point, layout may load a URL for . it will do this either because that's what was in the HTML, or because that's how we "rewrote" the URLs when we intercepted NET_GetURL. = if one of our cached parts has the requested cid, return the data for it. = else, generate a "broken image" = free all the cached data -------------------------------------------------- How hard would be an approach where we rewrite the HTML? (Looks like it's not much easier, and might be more error-prone.) = if the type of this multipart/related is not text/html, then treat it the same as multipart/mixed. = For each part in this multipart/related = save this part to a tmp file or a memory object, like what we do for multipart/alternative sub-parts. = Parse the "top" part, and emit slightly different HTML: = for each , , ? Any others? = look through our cached parts for a matching URL = if we find one, map that URL to a "cid:" URL = else, let it load normally = at some point, layout may load a URL for . = if one of our cached parts has the requested cid, return the data for it. = else, generate a "broken image" = free all the cached data */ #include "mimemrel.h" #include #include "mimehdrs.h" #include "mimeleaf.h" #include "mimemapl.h" #include "mimemoz2.h" #include "mimemrel.h" #include "mimepbuf.h" #include "mozilla/CheckedInt.h" #include "mozilla/ScopeExit.h" #include "mozilla/TextUtils.h" #include "msgCore.h" #include "nsCOMPtr.h" #include "nsIURIMutator.h" #include "nsMailHeaders.h" #include "nsMimeStringResources.h" #include "nsMimeTypes.h" #include "nsMsgCompUtils.h" #include "nsMsgUtils.h" #include "nsString.h" #include "nsURLHelper.h" #include "plstr.h" #include "prlog.h" #include "prmem.h" #include "prprf.h" #define MIME_SUPERCLASS mimeMultipartClass MimeDefClass(MimeMultipartRelated, MimeMultipartRelatedClass, mimeMultipartRelatedClass, &MIME_SUPERCLASS); class MimeHashValue { public: MimeHashValue(MimeObject* obj, const char* url) { m_obj = obj; m_url = strdup(url); } virtual ~MimeHashValue() { if (m_url) PR_Free((void*)m_url); } MimeObject* m_obj; char* m_url; }; static int MimeMultipartRelated_initialize(MimeObject* obj) { MimeMultipartRelated* relobj = (MimeMultipartRelated*)obj; relobj->base_url = MimeHeaders_get(obj->headers, HEADER_CONTENT_BASE, false, false); /* rhp: need this for supporting Content-Location */ if (!relobj->base_url) { relobj->base_url = MimeHeaders_get(obj->headers, HEADER_CONTENT_LOCATION, false, false); } /* rhp: need this for supporting Content-Location */ /* I used to have code here to test if the type was text/html. Then I added multipart/alternative as being OK, too. Then I found that the VCard spec seems to talk about having the first part of a multipart/related be an application/directory. At that point, I decided to punt. We handle anything as the first part, and stomp on the HTML it generates to adjust tags to point into the other parts. This probably works out to something reasonable in most cases. */ relobj->hash = PL_NewHashTable(20, PL_HashString, PL_CompareStrings, PL_CompareValues, (PLHashAllocOps*)NULL, NULL); if (!relobj->hash) return MIME_OUT_OF_MEMORY; relobj->input_file_stream = nullptr; relobj->output_file_stream = nullptr; relobj->is_part_in_hidden_alternative = false; relobj->rawtext_end = nullptr; relobj->rawtext_end_matched = 0; return ((MimeObjectClass*)&MIME_SUPERCLASS)->initialize(obj); } static int mime_multipart_related_nukehash(PLHashEntry* table, int indx, void* arg) { if (table->key) PR_Free((char*)table->key); if (table->value) delete (MimeHashValue*)table->value; return HT_ENUMERATE_NEXT; /* XP_Maphash will continue traversing the hash */ } static void MimeMultipartRelated_finalize(MimeObject* obj) { MimeMultipartRelated* relobj = (MimeMultipartRelated*)obj; PR_FREEIF(relobj->base_url); PR_FREEIF(relobj->curtag); if (relobj->buffered_hdrs) { PR_FREEIF(relobj->buffered_hdrs->all_headers); PR_FREEIF(relobj->buffered_hdrs->heads); PR_FREEIF(relobj->buffered_hdrs); } PR_FREEIF(relobj->head_buffer); relobj->head_buffer_fp = 0; relobj->head_buffer_size = 0; if (relobj->hash) { PL_HashTableEnumerateEntries(relobj->hash, mime_multipart_related_nukehash, NULL); PL_HashTableDestroy(relobj->hash); relobj->hash = NULL; } if (relobj->input_file_stream) { relobj->input_file_stream->Close(); relobj->input_file_stream = nullptr; } if (relobj->output_file_stream) { relobj->output_file_stream->Close(); relobj->output_file_stream = nullptr; } if (relobj->file_buffer) { relobj->file_buffer->Remove(false); relobj->file_buffer = nullptr; } for (int i = 0; i < relobj->child_bufs_count; i++) { if (relobj->child_bufs[i]) MimePartBufferDestroy(relobj->child_bufs[i]); MimeHeaders_free(relobj->child_hdrs[i]); } PR_FREEIF(relobj->child_bufs); PR_FREEIF(relobj->child_hdrs); PR_FREEIF(relobj->child_objs); relobj->child_bufs_count = 0; if (relobj->headobj) { // In some error conditions when MimeMultipartRelated_parse_eof() isn't run // (for example, no temp disk space available to extract message parts), // the head object is also referenced as a child. // If we free it, we remove the child reference first ... or crash later :-( MimeContainer* cont = (MimeContainer*)relobj; for (int i = 0; i < cont->nchildren; i++) { if (cont->children[i] == relobj->headobj) { // Shift remaining children down. for (int j = i + 1; j < cont->nchildren; j++) { cont->children[j - 1] = cont->children[j]; } cont->children[--cont->nchildren] = nullptr; break; } } mime_free(relobj->headobj); relobj->headobj = nullptr; } ((MimeObjectClass*)&MIME_SUPERCLASS)->finalize(obj); } static bool MimeStartParamExists(MimeObject* obj, MimeObject* child) { char* ct = MimeHeaders_get(obj->headers, HEADER_CONTENT_TYPE, false, false); char* st = (ct ? MimeHeaders_get_parameter(ct, HEADER_PARM_START, NULL, NULL) : 0); PR_FREEIF(ct); if (!st) return false; PR_FREEIF(st); return true; } static bool MimeThisIsStartPart(MimeObject* obj, MimeObject* child) { bool rval = false; char *ct, *st, *cst; ct = MimeHeaders_get(obj->headers, HEADER_CONTENT_TYPE, false, false); st = (ct ? MimeHeaders_get_parameter(ct, HEADER_PARM_START, NULL, NULL) : 0); PR_FREEIF(ct); if (!st) return false; // Strip angle brackets from the start parameter value per RFC 2387. char* stStripped = st; if (*stStripped == '<') { stStripped++; int stLen = strlen(stStripped); if (stLen > 0 && stStripped[stLen - 1] == '>') { stStripped[stLen - 1] = '\0'; } } cst = MimeHeaders_get(child->headers, HEADER_CONTENT_ID, false, false); if (!cst) rval = false; else { char* tmp = cst; if (*tmp == '<') { int length; tmp++; length = strlen(tmp); if (length > 0 && tmp[length - 1] == '>') { tmp[length - 1] = '\0'; } } rval = (!strcmp(stStripped, tmp)); } PR_FREEIF(st); PR_FREEIF(cst); return rval; } /* rhp - gotta support the "start" parameter */ char* MakeAbsoluteURL(char* base_url, char* relative_url) { char* retString = nullptr; nsIURI* base = nullptr; nsIURI* url = nullptr; auto releaseUris = mozilla::MakeScopeExit([&] { NS_IF_RELEASE(url); NS_IF_RELEASE(base); }); // if either is NULL, just return the relative if safe... if (!base_url || !relative_url) { if (!relative_url) return nullptr; return PL_strdup(relative_url); } nsresult err = nsMimeNewURI(&base, base_url, nullptr); if (NS_FAILED(err)) return nullptr; nsAutoCString spec; err = nsMimeNewURI(&url, relative_url, base); if (NS_FAILED(err)) return nullptr; err = url->GetSpec(spec); if (NS_FAILED(err)) { return nullptr; } retString = ToNewCString(spec); return retString; } // Rewrites the query of `spec` to carry `child`'s content type and filename, // and returns the result in `result`. static nsresult BuildRelatedPartUrl(const char* spec, MimeObject* child, nsACString& result) { // Get the query as an URLParams object so we can safely modify it. nsCOMPtr uri; nsresult rv = NS_NewURI(getter_AddRefs(uri), spec); NS_ENSURE_SUCCESS(rv, rv); nsAutoCString query; rv = uri->GetQuery(query); NS_ENSURE_SUCCESS(rv, rv); mozilla::URLParams params; params.ParseInput(query); // Raw MIME part channels get their content type from the URL. Include // it so related resources which cannot be sniffed, such as SVG, load. if (child->content_type) { params.Set("type"_ns, nsDependentCString(child->content_type)); } char* name = MimeHeaders_get_name(child->headers, child->options); // let's stick the filename in the part so save as will work. if (!name) { // Mozilla platform code will correct the file extension // when copying the embedded image. That doesn't work // since our MailNews URLs don't allow setting the file // extension. So provide a filename and valid extension. char* ct = MimeHeaders_get(child->headers, HEADER_CONTENT_TYPE, false, false); if (ct) { name = ct; char* slash = strchr(name, '/'); if (slash) *slash = '.'; char* semi = strchr(name, ';'); if (semi) *semi = 0; } } if (name) { params.Set("filename"_ns, nsCString(name)); PR_Free(name); } // Update the query and convert the URI back to a string. params.Serialize(query, true); rv = NS_MutateURI(uri).SetQuery(query).Finalize(getter_AddRefs(uri)); NS_ENSURE_SUCCESS(rv, rv); return uri->GetSpec(result); } static bool MimeMultipartRelated_output_child_p(MimeObject* obj, MimeObject* child) { MimeMultipartRelated* relobj = (MimeMultipartRelated*)obj; if ((relobj->head_loaded) || (MimeStartParamExists(obj, child) && !MimeThisIsStartPart(obj, child))) { /* This is a child part. Just remember the mapping between the URL it represents and the part-URL to get it back. */ char* location = MimeHeaders_get(child->headers, HEADER_CONTENT_LOCATION, false, false); if (!location) { char* tmp = MimeHeaders_get(child->headers, HEADER_CONTENT_ID, false, false); if (tmp) { char* tmp2 = tmp; if (*tmp2 == '<') { int length; tmp2++; length = strlen(tmp2); if (length > 0 && tmp2[length - 1] == '>') { tmp2[length - 1] = '\0'; } } location = PR_smprintf("cid:%s", tmp2); PR_Free(tmp); } } if (!location) { // Without Content-ID or Content-Location this part cannot be referenced. // Keep it visible as a normal attachment. When composing, defer parsing // to the attachment replay at EOF; parse_begin would otherwise suppress // output and mark the part as hidden. return !(obj->options && obj->options->decompose_file_p); } char* base_url = MimeHeaders_get(child->headers, HEADER_CONTENT_BASE, false, false); char* absolute = MakeAbsoluteURL(base_url ? base_url : relobj->base_url, location); PR_FREEIF(base_url); PR_Free(location); if (absolute) { nsAutoCString partnum; nsAutoCString imappartnum; char* part = nullptr; partnum.Adopt(mime_part_address(child)); if (!partnum.IsEmpty()) { if (obj->options->missing_parts) { char* imappart = mime_imap_part_address(child); if (imappart) imappartnum.Adopt(imappart); } /* AppleDouble part need special care: we need to output only the data fork part of it. The problem at this point is that we haven't yet decoded the children of the AppleDouble part therefore we will have to hope the datafork is the second one! */ if (mime_typep(child, (MimeObjectClass*)&mimeMultipartAppleDoubleClass)) partnum.AppendLiteral(".2"); if (!imappartnum.IsEmpty()) part = mime_set_url_imap_part(obj->options->url, imappartnum.get(), partnum.get()); else { char* no_part_url = nullptr; if (obj->options->part_to_load && obj->options->format_out == nsMimeOutput::nsMimeMessageBodyDisplay) no_part_url = mime_get_base_url(obj->options->url); if (no_part_url) { part = mime_set_url_part(no_part_url, partnum.get(), false); PR_Free(no_part_url); } else part = mime_set_url_part(obj->options->url, partnum.get(), false); } } if (part) { nsAutoCString spec; nsresult rv = BuildRelatedPartUrl(part, child, spec); PR_Free(part); if (NS_FAILED(rv)) { PR_Free(absolute); return true; } MimeHashValue* value = new MimeHashValue(child, spec.get()); PL_HashTableAdd(relobj->hash, absolute, value); /* rhp - If this part ALSO has a Content-ID we need to put that into the hash table and this is what this code does */ { char* tloc; char* tmp = MimeHeaders_get(child->headers, HEADER_CONTENT_ID, false, false); if (tmp) { char* tmp2 = tmp; if (*tmp2 == '<') { int length; tmp2++; length = strlen(tmp2); if (length > 0 && tmp2[length - 1] == '>') { tmp2[length - 1] = '\0'; } } tloc = PR_smprintf("cid:%s", tmp2); PR_Free(tmp); if (tloc) { MimeHashValue* value; value = (MimeHashValue*)PL_HashTableLookup(relobj->hash, tloc); if (!value) { value = new MimeHashValue(child, spec.get()); PL_HashTableAdd(relobj->hash, tloc, value); } else PR_smprintf_free(tloc); } } } /* rhp - End of putting more stuff into the hash table */ } else { // Nothing took ownership of `absolute`. PR_Free(absolute); } } } else { /* Ah-hah! We're the head object. */ relobj->head_loaded = true; relobj->headobj = child; relobj->buffered_hdrs = MimeHeaders_copy(child->headers); char* base_url = MimeHeaders_get(child->headers, HEADER_CONTENT_BASE, false, false); if (!base_url) { base_url = MimeHeaders_get(child->headers, HEADER_CONTENT_LOCATION, false, false); } if (base_url) { /* If the head object has a base_url associated with it, use that instead of any base_url that may have been associated with the multipart/related. */ PR_FREEIF(relobj->base_url); nsCOMPtr url; nsresult rv = nsMimeNewURI(getter_AddRefs(url), base_url, nullptr); if (NS_SUCCEEDED(rv)) { relobj->base_url = base_url; } } } if (obj->options && !obj->options->write_html_p && !obj->options->decompose_file_p) { return true; } // Don't actually parse this child; we'll handle all that at eof time. return false; } static int MimeMultipartRelated_parse_child_line(MimeObject* obj, const char* line, int32_t length, bool first_line_p) { MimeContainer* cont = (MimeContainer*)obj; MimeMultipartRelated* relobj = (MimeMultipartRelated*)obj; MimeObject* kid; if (obj->options && !obj->options->write_html_p && !obj->options->decompose_file_p) { /* Oh, just go do the normal thing... */ return ((MimeMultipartClass*)&MIME_SUPERCLASS) ->parse_child_line(obj, line, length, first_line_p); } PR_ASSERT(cont->nchildren > 0); if (cont->nchildren <= 0) return -1; kid = cont->children[cont->nchildren - 1]; PR_ASSERT(kid); if (!kid) return -1; if (kid != relobj->headobj) { // Hidden related parts may later surface as attachments; keep decoded // sizes. if (!kid->output_p && mime_subclass_p(kid->clazz, (MimeObjectClass*)&mimeLeafClass)) { int32_t lineLength = length; if (lineLength > 0 && line[lineLength - 1] == '\n') lineLength--; if (lineLength > 0 && line[lineLength - 1] == '\r') lineLength--; if (!first_line_p) { char nl[] = MSG_LINEBREAK; int status = MimeLeaf_parse_buffer_for_size(nl, MSG_LINEBREAK_LEN, kid); if (status < 0) return status; } int status = MimeLeaf_parse_buffer_for_size(line, lineLength, kid); if (status < 0) return status; } // In decompose mode, buffer non-head child content so we can replay it // in parse_eof for parts the reader decides are visible attachments. if (obj->options && obj->options->decompose_file_p) { int idx = cont->nchildren - 1; if (idx >= relobj->child_bufs_count) { int oldcount = relobj->child_bufs_count; int newcount = idx + 1; auto newHdrs = (MimeHeaders**)PR_Malloc(newcount * sizeof(MimeHeaders*)); auto newBufs = (MimePartBufferData**)PR_Malloc( newcount * sizeof(MimePartBufferData*)); auto newObjs = (MimeObject**)PR_Malloc(newcount * sizeof(MimeObject*)); if (!newHdrs || !newBufs || !newObjs) { PR_FREEIF(newHdrs); PR_FREEIF(newBufs); PR_FREEIF(newObjs); return MIME_OUT_OF_MEMORY; } for (int i = 0; i < oldcount; i++) { newHdrs[i] = relobj->child_hdrs[i]; newBufs[i] = relobj->child_bufs[i]; newObjs[i] = relobj->child_objs[i]; } for (int i = oldcount; i < newcount; i++) { newHdrs[i] = nullptr; newBufs[i] = nullptr; newObjs[i] = nullptr; } PR_FREEIF(relobj->child_hdrs); PR_FREEIF(relobj->child_bufs); PR_FREEIF(relobj->child_objs); relobj->child_hdrs = newHdrs; relobj->child_bufs = newBufs; relobj->child_objs = newObjs; relobj->child_bufs_count = newcount; } if (!relobj->child_bufs[idx]) { relobj->child_bufs[idx] = MimePartBufferCreate(); relobj->child_hdrs[idx] = MimeHeaders_copy(kid->headers); relobj->child_objs[idx] = kid; } return MimePartBufferWrite(relobj->child_bufs[idx], line, length); } if (kid->output_p) return ((MimeMultipartClass*)&MIME_SUPERCLASS) ->parse_child_line(obj, line, length, first_line_p); return 0; } /* Buffer this up (###tw much code duplication from mimemalt.c) */ /* If we don't yet have a buffer (either memory or file) try and make a memory buffer. */ if (!relobj->head_buffer && !relobj->file_buffer) { int target_size = 1024 * 50; /* try for 50k */ while (target_size > 0) { relobj->head_buffer = (char*)PR_MALLOC(target_size); if (relobj->head_buffer) break; /* got it! */ target_size -= (1024 * 5); /* decrease it and try again */ } if (relobj->head_buffer) { relobj->head_buffer_size = target_size; } else { relobj->head_buffer_size = 0; } relobj->head_buffer_fp = 0; } nsresult rv; /* Ok, if at this point we still don't have either kind of buffer, try and make a file buffer. */ if (!relobj->head_buffer && !relobj->file_buffer) { nsCOMPtr file; rv = nsMsgCreateTempFile("nsma", getter_AddRefs(file)); NS_ENSURE_SUCCESS(rv, -1); relobj->file_buffer = file; rv = MsgNewBufferedFileOutputStream( getter_AddRefs(relobj->output_file_stream), relobj->file_buffer, PR_WRONLY | PR_CREATE_FILE, 00600); NS_ENSURE_SUCCESS(rv, -1); } PR_ASSERT(relobj->head_buffer || relobj->output_file_stream); /* If this line will fit in the memory buffer, put it there. */ if (relobj->head_buffer && relobj->head_buffer_fp + length < relobj->head_buffer_size) { memcpy(relobj->head_buffer + relobj->head_buffer_fp, line, length); relobj->head_buffer_fp += length; } else { /* Otherwise it won't fit; write it to the file instead. */ /* If the file isn't open yet, open it, and dump the memory buffer to it. */ if (!relobj->output_file_stream) { if (!relobj->file_buffer) { nsCOMPtr file; rv = nsMsgCreateTempFile("nsma", getter_AddRefs(file)); NS_ENSURE_SUCCESS(rv, -1); relobj->file_buffer = file; } nsresult rv = MsgNewBufferedFileOutputStream( getter_AddRefs(relobj->output_file_stream), relobj->file_buffer, PR_WRONLY | PR_CREATE_FILE, 00600); NS_ENSURE_SUCCESS(rv, -1); if (relobj->head_buffer && relobj->head_buffer_fp) { uint32_t bytesWritten; rv = relobj->output_file_stream->Write( relobj->head_buffer, relobj->head_buffer_fp, &bytesWritten); if (NS_FAILED(rv) || (bytesWritten < relobj->head_buffer_fp)) return MIME_UNABLE_TO_OPEN_TMP_FILE; } PR_FREEIF(relobj->head_buffer); relobj->head_buffer_fp = 0; relobj->head_buffer_size = 0; } /* Dump this line to the file. */ uint32_t bytesWritten; rv = relobj->output_file_stream->Write(line, length, &bytesWritten); if ((int32_t)bytesWritten < length || NS_FAILED(rv)) return MIME_UNABLE_TO_OPEN_TMP_FILE; } return 0; } static int real_write(MimeMultipartRelated* relobj, const char* buf, int32_t size) { MimeObject* obj = (MimeObject*)relobj; MimeClosure closure = relobj->real_output_closure; if (obj->options && obj->options->decompose_file_p && obj->options->decompose_file_output_fn) { // the buf here has already been decoded, but we want to use general output // functions here that permit decoded or encoded input, using the closure // to tell the difference. We'll temporarily disable the closure's decoder, // then restore it when we are done. Not sure if we shouldn't just turn it // off permanently though. mime_draft_data* mdd = obj->options->stream_closure.AsMimeDraftData(); if (!mdd) { return -1; } MimeDecoderData* old_decoder_data = mdd->decoder_data; mdd->decoder_data = nullptr; int status = obj->options->decompose_file_output_fn( buf, size, MimeClosure(MimeClosure::isMimeDraftData, mdd)); mdd->decoder_data = old_decoder_data; return status; } else { if (!closure) { MimeObject* lobj = (MimeObject*)relobj; closure = lobj->options->stream_closure; } return relobj->real_output_fn(buf, size, closure); } } static int push_tag(MimeMultipartRelated* relobj, const char* buf, int32_t size) { if (size < 0) return MIME_OUT_OF_MEMORY; /* curtag_length and curtag_max are int32_t. Refuse arithmetic that would overflow them, rather than skip the growth and let the memcpy overrun. */ mozilla::CheckedInt needed = mozilla::CheckedInt(relobj->curtag_length) + size; if (!needed.isValid()) return MIME_OUT_OF_MEMORY; if (needed.value() > relobj->curtag_max) { mozilla::CheckedInt new_max = mozilla::CheckedInt(relobj->curtag_max) + mozilla::CheckedInt(size) * 2; if (!new_max.isValid()) return MIME_OUT_OF_MEMORY; if (new_max.value() < 1024) new_max = 1024; char* newBuf = (char*)PR_Realloc(relobj->curtag, new_max.value()); NS_ENSURE_TRUE(newBuf, MIME_OUT_OF_MEMORY); relobj->curtag = newBuf; relobj->curtag_max = new_max.value(); } memcpy(relobj->curtag + relobj->curtag_length, buf, size); relobj->curtag_length += size; return 0; } static bool accept_related_part(MimeMultipartRelated* relobj, MimeObject* part_obj) { if (!relobj || !part_obj) return false; /* before accepting it as a valid related part, make sure we are able to display it inline as an embedded object. Else just ignore it, that will prevent any bad surprise... */ MimeObjectClass* clazz = mime_find_class(part_obj->content_type, part_obj->headers, part_obj->options, false, nullptr); if (clazz ? clazz->displayable_inline_p(clazz, part_obj->headers) : false) return true; /* ...but we always accept it if it's referenced by an anchor */ return (relobj->curtag && relobj->curtag_length >= 3 && (relobj->curtag[1] == 'A' || relobj->curtag[1] == 'a') && IS_SPACE(relobj->curtag[2])); } static bool hide_related_part_p(MimeMultipartRelated* relobj, MimeObject* part_obj) { if (!relobj || !part_obj || relobj->is_part_in_hidden_alternative) { return false; } // Surface named text/* parts as attachments even when CID-referenced; // they are files the user needs, not inline body content. char* name = MimeHeaders_get_name(part_obj->headers, part_obj->options); bool hasName = name != nullptr; PR_FREEIF(name); return !(hasName && part_obj->content_type && !PL_strncasecmp(part_obj->content_type, "text/", 5)); } static int flush_tag(MimeMultipartRelated* relobj) { int length = relobj->curtag_length; char* buf; int status; if (relobj->curtag == NULL || length == 0) return 0; status = push_tag(relobj, "", 1); /* Push on a trailing NULL. */ if (status < 0) return status; buf = relobj->curtag; PR_ASSERT(*buf == '<' && buf[length - 1] == '>'); while (*buf) { char c; char* absolute; char* part_url; char* ptr = buf; char* ptr2; char quoteDelimiter = '\0'; while (*ptr && *ptr != '=') ptr++; if (*ptr == '=') { /* Ignore = and leading space. */ /* Safe, because there's a '>' at the end! */ do { ptr++; } while (IS_SPACE(*ptr)); if (*ptr == '"' || *ptr == '\'') { quoteDelimiter = *ptr; /* Take up the quote and leading space here as well. */ /* Safe because there's a '>' at the end */ do { ptr++; } while (IS_SPACE(*ptr)); } } status = real_write(relobj, buf, ptr - buf); if (status < 0) return status; buf = ptr; if (!*buf) break; if (quoteDelimiter) { ptr = PL_strnchr(buf, quoteDelimiter, length - (buf - relobj->curtag)); } else { for (ptr = buf; *ptr; ptr++) { if (*ptr == '>' || IS_SPACE(*ptr)) break; } PR_ASSERT(*ptr); } if (!ptr || !*ptr) break; if (buf < ptr) { // If the whole value is a cid: URL, or the URL of a related part, // substitute the mailbox part URL. Not a word within the value: it may // e.g. be in the query of a URL to elsewhere, which would then get the // part URL. ptr2 = ptr; while ((ptr2 > buf) && IS_SPACE(ptr2[-1])) ptr2--; /* Compare the beginning of the word with "cid:". Yuck. */ if (((ptr2 - buf) > 4) && ((buf[0] == 'c' || buf[0] == 'C') && (buf[1] == 'i' || buf[1] == 'I') && (buf[2] == 'd' || buf[2] == 'D') && buf[3] == ':')) { // Make sure it's lowercase, otherwise it won't be found in the hash // table buf[0] = 'c'; buf[1] = 'i'; buf[2] = 'd'; /* Null terminate the word so we can... */ c = *ptr2; *ptr2 = '\0'; /* Construct a URL out of the word. */ absolute = MakeAbsoluteURL(relobj->base_url, buf); /* See if we have a mailbox part URL corresponding to this cid. */ part_url = nullptr; MimeHashValue* value = nullptr; if (absolute) { value = (MimeHashValue*)PL_HashTableLookup(relobj->hash, buf); part_url = value ? value->m_url : nullptr; PR_FREEIF(absolute); } /*If we found a mailbox part URL, write that out instead.*/ if (part_url && accept_related_part(relobj, value->m_obj)) { status = real_write(relobj, part_url, strlen(part_url)); if (status < 0) return status; buf = ptr2; /* skip over the cid: URL we substituted */ if (hide_related_part_p(relobj, value->m_obj)) value->m_obj->dontShowAsAttachment = true; } /* Restore the character that we nulled. */ *ptr2 = c; } /* rhp - if we get here, we should still check against the hash table! */ else { char holder = *ptr2; char* realout; *ptr2 = '\0'; /* Construct a URL out of the word. */ absolute = MakeAbsoluteURL(relobj->base_url, buf); /* See if we have a mailbox part URL corresponding to this cid. */ MimeHashValue* value; if (absolute) value = (MimeHashValue*)PL_HashTableLookup(relobj->hash, absolute); else value = (MimeHashValue*)PL_HashTableLookup(relobj->hash, buf); realout = value ? value->m_url : nullptr; *ptr2 = holder; PR_FREEIF(absolute); if (realout && accept_related_part(relobj, value->m_obj)) { status = real_write(relobj, realout, strlen(realout)); if (status < 0) return status; buf = ptr2; /* skip over the cid: URL we substituted */ if (hide_related_part_p(relobj, value->m_obj)) value->m_obj->dontShowAsAttachment = true; } } /* rhp - if we get here, we should still check against the hash table! */ // Include the trailing whitespace of the value. while ((ptr2 < ptr) && (IS_SPACE(*ptr2))) ptr2++; /* Write whatever original text remains after cid: URL substitution. */ status = real_write(relobj, buf, ptr2 - buf); if (status < 0) return status; buf = ptr2; } } if (buf && *buf) { status = real_write(relobj, buf, strlen(buf)); if (status < 0) return status; } relobj->curtag_length = 0; return 0; } // The end tags of elements whose content isn't parsed as markup. Not //