home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / libmime / mimeiimg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  6.0 KB  |  211 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. /* mimeiimg.c --- definition of the MimeInlineImage class (see mimei.h)
  20.    Created: Jamie Zawinski <jwz@netscape.com>, 15-May-96.
  21.  */
  22.  
  23. #include "mimeiimg.h"
  24.  
  25. #define MIME_SUPERCLASS mimeLeafClass
  26. MimeDefClass(MimeInlineImage, MimeInlineImageClass,
  27.              mimeInlineImageClass, &MIME_SUPERCLASS);
  28.  
  29. static int MimeInlineImage_initialize (MimeObject *);
  30. static void MimeInlineImage_finalize (MimeObject *);
  31. static int MimeInlineImage_parse_begin (MimeObject *);
  32. static int MimeInlineImage_parse_line (char *, int32, MimeObject *);
  33. static int MimeInlineImage_parse_eof (MimeObject *, XP_Bool);
  34. static int MimeInlineImage_parse_decoded_buffer (char *, int32, MimeObject *);
  35.  
  36. static int
  37. MimeInlineImageClassInitialize(MimeInlineImageClass *class)
  38. {
  39.   MimeObjectClass *oclass = (MimeObjectClass *) class;
  40.   MimeLeafClass   *lclass = (MimeLeafClass *) class;
  41.  
  42.   XP_ASSERT(!oclass->class_initialized);
  43.   oclass->initialize   = MimeInlineImage_initialize;
  44.   oclass->finalize     = MimeInlineImage_finalize;
  45.   oclass->parse_begin  = MimeInlineImage_parse_begin;
  46.   oclass->parse_line   = MimeInlineImage_parse_line;
  47.   oclass->parse_eof    = MimeInlineImage_parse_eof;
  48.   lclass->parse_decoded_buffer = MimeInlineImage_parse_decoded_buffer;
  49.  
  50.   return 0;
  51. }
  52.  
  53.  
  54. static int
  55. MimeInlineImage_initialize (MimeObject *object)
  56. {
  57.   return ((MimeObjectClass*)&MIME_SUPERCLASS)->initialize(object);
  58. }
  59.  
  60. static void
  61. MimeInlineImage_finalize (MimeObject *object)
  62. {
  63.   ((MimeObjectClass*)&MIME_SUPERCLASS)->finalize(object);
  64. }
  65.  
  66. static int
  67. MimeInlineImage_parse_begin (MimeObject *obj)
  68. {
  69.   MimeInlineImage *img = (MimeInlineImage *) obj;
  70.   MimeInlineImageClass *class;
  71.  
  72.   int status;
  73.  
  74.   status = ((MimeObjectClass*)&MIME_SUPERCLASS)->parse_begin(obj);
  75.   if (status < 0) return status;
  76.  
  77.   if (!obj->output_p) return 0;
  78.  
  79.   if (!obj->options || !obj->options->output_fn)
  80.     return 0;
  81.  
  82.   class = (MimeInlineImageClass *) obj->class;
  83.  
  84.   if (obj->options &&
  85.       obj->options->image_begin &&
  86.       obj->options->write_html_p &&
  87.       obj->options->image_write_buffer)
  88.     {
  89.       char *html, *part, *image_url;
  90.       const char *ct;
  91.  
  92.       part = mime_part_address(obj);
  93.       if (!part) return MK_OUT_OF_MEMORY;
  94.       image_url = mime_set_url_part(obj->options->url, part, TRUE);
  95.       if (!image_url)
  96.         {
  97.           XP_FREE(part);
  98.           return MK_OUT_OF_MEMORY;
  99.         }
  100.       XP_FREE(part);
  101.  
  102.       ct = obj->content_type;
  103.       if (!ct) ct = IMAGE_GIF;  /* Can't happen?  Close enough. */
  104.  
  105.       img->image_data =
  106.         obj->options->image_begin(image_url, ct, obj->options->stream_closure);
  107.       XP_FREE(image_url);
  108.  
  109.       if (!img->image_data) return MK_OUT_OF_MEMORY;
  110.  
  111.       html = obj->options->make_image_html(img->image_data);
  112.       if (!html) return MK_OUT_OF_MEMORY;
  113.  
  114.       status = MimeObject_write(obj, html, XP_STRLEN(html), TRUE);
  115.       XP_FREE(html);
  116.       if (status < 0) return status;
  117.     }
  118.  
  119.   return 0;
  120. }
  121.  
  122.  
  123. static int
  124. MimeInlineImage_parse_eof (MimeObject *obj, XP_Bool abort_p)
  125. {
  126.   MimeInlineImage *img = (MimeInlineImage *) obj;
  127.   int status;
  128.   if (obj->closed_p) return 0;
  129.  
  130.   /* Force out any buffered data from the superclass (the base64 decoder.) */
  131.   status = ((MimeObjectClass*)&MIME_SUPERCLASS)->parse_eof(obj, abort_p);
  132.   if (status < 0) abort_p = TRUE;
  133.  
  134.   if (img->image_data)
  135.     {
  136.       obj->options->image_end(img->image_data,
  137.                               (status < 0 ? status : (abort_p ? -1 : 0)));
  138.       img->image_data = 0;
  139.     }
  140.  
  141.   return status;
  142. }
  143.  
  144.  
  145. static int
  146. MimeInlineImage_parse_decoded_buffer (char *buf, int32 size, MimeObject *obj)
  147. {
  148.   /* This is called (by MimeLeafClass->parse_buffer) with blocks of data
  149.      that have already been base64-decoded.  Pass this raw image data
  150.      along to the backend-specific image display code.
  151.    */
  152.   MimeInlineImage *img  = (MimeInlineImage *) obj;
  153.   int status;
  154.  
  155.   if (obj->output_p &&
  156.       obj->options &&
  157.       !obj->options->write_html_p)
  158.     {
  159.       /* in this case, we just want the raw data...
  160.          Make the stream, if it's not made, and dump the data out.
  161.        */
  162.  
  163.       if (!obj->options->state->first_data_written_p)
  164.         {
  165.           status = MimeObject_output_init(obj, 0);
  166.           if (status < 0) return status;
  167.           XP_ASSERT(obj->options->state->first_data_written_p);
  168.         }
  169.       
  170.       return MimeObject_write(obj, buf, size, TRUE);
  171.     }
  172.  
  173.  
  174.   if (!obj->options ||
  175.       !obj->options->image_write_buffer)
  176.     return 0;
  177.  
  178.   /* If we don't have any image data, the image_end method must have already
  179.      been called, so don't call image_write_buffer again. */
  180.   if (!img->image_data) return 0;
  181.  
  182.   /* Hand this data off to the backend-specific image display stream.
  183.    */
  184.   status = obj->options->image_write_buffer (buf, size, img->image_data);
  185.   
  186.   /* If the image display stream fails, then close the stream - but do not
  187.      return the failure status, and do not give up on parsing this object.
  188.      Just because the image data was corrupt doesn't mean we need to give up
  189.      on the whole document; we can continue by just skipping over the rest of
  190.      this part, and letting our parent continue.
  191.    */
  192.   if (status < 0)
  193.     {
  194.       obj->options->image_end (img->image_data, status);
  195.       img->image_data = 0;
  196.       status = 0;
  197.     }
  198.  
  199.   return status;
  200. }
  201.  
  202.  
  203. static int
  204. MimeInlineImage_parse_line (char *line, int32 length, MimeObject *obj)
  205. {
  206.   /* This method should never be called (inline images do no line buffering).
  207.    */
  208.   XP_ASSERT(0);
  209.   return -1;
  210. }
  211.