home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / libmime / mimesun.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  11.3 KB  |  353 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. /* mimemult.h --- definition of the MimeMultipart class (see mimei.h)
  20.    Created: Jamie Zawinski <jwz@netscape.com>, 15-May-96.
  21.  */
  22.  
  23. #include "mimesun.h"
  24.  
  25. #define MIME_SUPERCLASS mimeMultipartClass
  26. MimeDefClass(MimeSunAttachment, MimeSunAttachmentClass,
  27.              mimeSunAttachmentClass, &MIME_SUPERCLASS);
  28.  
  29. static MimeMultipartBoundaryType MimeSunAttachment_check_boundary(MimeObject *,
  30.                                                                   const char *,
  31.                                                                   int32);
  32. static int MimeSunAttachment_create_child(MimeObject *);
  33. static int MimeSunAttachment_parse_child_line (MimeObject *, char *, int32,
  34.                                                XP_Bool);
  35. static int MimeSunAttachment_parse_begin (MimeObject *);
  36. static int MimeSunAttachment_parse_eof (MimeObject *, XP_Bool);
  37.  
  38. static int
  39. MimeSunAttachmentClassInitialize(MimeSunAttachmentClass *class)
  40. {
  41.   MimeObjectClass    *oclass = (MimeObjectClass *)    class;
  42.   MimeMultipartClass *mclass = (MimeMultipartClass *) class;
  43.  
  44.   XP_ASSERT(!oclass->class_initialized);
  45.   oclass->parse_begin      = MimeSunAttachment_parse_begin;
  46.   oclass->parse_eof        = MimeSunAttachment_parse_eof;
  47.   mclass->check_boundary   = MimeSunAttachment_check_boundary;
  48.   mclass->create_child     = MimeSunAttachment_create_child;
  49.   mclass->parse_child_line = MimeSunAttachment_parse_child_line;
  50.   return 0;
  51. }
  52.  
  53.  
  54. static int
  55. MimeSunAttachment_parse_begin (MimeObject *obj)
  56. {
  57.   int status = ((MimeObjectClass*)&MIME_SUPERCLASS)->parse_begin(obj);
  58.   if (status < 0) return status;
  59.  
  60.   /* Sun messages always have separators at the beginning. */
  61.   return MimeObject_write_separator(obj);
  62. }
  63.  
  64. static int
  65. MimeSunAttachment_parse_eof (MimeObject *obj, XP_Bool abort_p)
  66. {
  67.   int status = 0;
  68.  
  69.   status = ((MimeObjectClass*)&MIME_SUPERCLASS)->parse_eof(obj, abort_p);
  70.   if (status < 0) return status;
  71.  
  72.   /* Sun messages always have separators at the end. */
  73.   if (!abort_p)
  74.     {
  75.       status = MimeObject_write_separator(obj);
  76.       if (status < 0) return status;
  77.     }
  78.  
  79.   return 0;
  80. }
  81.  
  82.  
  83. static MimeMultipartBoundaryType
  84. MimeSunAttachment_check_boundary(MimeObject *obj, const char *line,
  85.                                  int32 length)
  86. {
  87.   /* ten dashes */
  88.  
  89.   if (line &&
  90.       line[0] == '-' && line[1] == '-' && line[2] == '-' && line[3] == '-' &&
  91.       line[4] == '-' && line[5] == '-' && line[6] == '-' && line[7] == '-' &&
  92.       line[8] == '-' && line[9] == '-' &&
  93.       (line[10] == CR || line[10] == LF))
  94.     return MimeMultipartBoundaryTypeSeparator;
  95.   else
  96.     return MimeMultipartBoundaryTypeNone;
  97. }
  98.  
  99.  
  100. static int
  101. MimeSunAttachment_create_child(MimeObject *obj)
  102. {
  103.   MimeMultipart *mult = (MimeMultipart *) obj;
  104.   int status = 0;
  105.  
  106.   char *sun_data_type = 0;
  107.   const char *mime_ct = 0, *sun_enc_info = 0, *mime_cte = 0;
  108.   char *mime_ct2 = 0;    /* sometimes we need to copy; this is for freeing. */
  109.   MimeObject *child = 0;
  110.  
  111.   mult->state = MimeMultipartPartLine;
  112.  
  113.   sun_data_type = (mult->hdrs
  114.                    ? MimeHeaders_get (mult->hdrs, HEADER_X_SUN_DATA_TYPE,
  115.                                       TRUE, FALSE)
  116.                    : 0);
  117.   if (sun_data_type)
  118.     {
  119.       int i;
  120.       static const struct { const char *in, *out; } sun_types[] = {
  121.  
  122.         /* Convert recognised Sun types to the corresponding MIME types,
  123.            and convert unrecognized ones based on the file extension and
  124.            the mime.types file.
  125.  
  126.            These are the magic types used by MailTool that I can determine.
  127.            The only actual written spec I've found only listed the first few.
  128.            The rest were found by inspection (both of real-world messages,
  129.            and by running `strings' on the MailTool binary, and on the file
  130.            /usr/openwin/lib/cetables/cetables (the "Class Engine", Sun's
  131.            equivalent to .mailcap and mime.types.)
  132.          */
  133.         { "default",                TEXT_PLAIN },
  134.         { "default-doc",            TEXT_PLAIN },
  135.         { "text",                    TEXT_PLAIN },
  136.         { "scribe",                    TEXT_PLAIN },
  137.         { "sgml",                    TEXT_PLAIN },
  138.         { "tex",                    TEXT_PLAIN },
  139.         { "troff",                    TEXT_PLAIN },
  140.         { "c-file",                    TEXT_PLAIN },
  141.         { "h-file",                    TEXT_PLAIN },
  142.         { "readme-file",            TEXT_PLAIN },
  143.         { "shell-script",            TEXT_PLAIN },
  144.         { "cshell-script",            TEXT_PLAIN },
  145.         { "makefile",                TEXT_PLAIN },
  146.         { "hidden-docs",            TEXT_PLAIN },
  147.         { "message",                MESSAGE_RFC822 },
  148.         { "mail-message",            MESSAGE_RFC822 },
  149.         { "mail-file",                TEXT_PLAIN },
  150.         { "gif-file",                IMAGE_GIF },
  151.         { "jpeg-file",                IMAGE_JPG },
  152.         { "ppm-file",                IMAGE_PPM },
  153.         { "pgm-file",                "image/x-portable-graymap" },
  154.         { "pbm-file",                "image/x-portable-bitmap" },
  155.         { "xpm-file",                "image/x-xpixmap" },
  156.         { "ilbm-file",                "image/ilbm" },
  157.         { "tiff-file",                "image/tiff" },
  158.         { "photocd-file",            "image/x-photo-cd" },
  159.         { "sun-raster",                "image/x-sun-raster" },
  160.         { "audio-file",                AUDIO_BASIC },
  161.         { "postscript",                APPLICATION_POSTSCRIPT },
  162.         { "postscript-file",        APPLICATION_POSTSCRIPT },
  163.         { "framemaker-document",    "application/x-framemaker" },
  164.         { "sundraw-document",        "application/x-sun-draw" },
  165.         { "sunpaint-document",        "application/x-sun-paint" },
  166.         { "sunwrite-document",        "application/x-sun-write" },
  167.         { "islanddraw-document",    "application/x-island-draw" },
  168.         { "islandpaint-document",    "application/x-island-paint" },
  169.         { "islandwrite-document",    "application/x-island-write" },
  170.         { "sun-executable",            APPLICATION_OCTET_STREAM },
  171.         { "default-app",            APPLICATION_OCTET_STREAM },
  172.         { 0, 0 }};
  173.       for (i = 0; sun_types[i].in; i++)
  174.         if (!strcasecomp(sun_data_type, sun_types[i].in))
  175.           {
  176.             mime_ct = sun_types[i].out;
  177.             break;
  178.           }
  179.     }
  180.  
  181.   /* If we didn't find a type, look at the extension on the file name.
  182.    */
  183.   if (!mime_ct &&
  184.       obj->options &&
  185.       obj->options->file_type_fn)
  186.     {
  187.       char *name = MimeHeaders_get_name(mult->hdrs);
  188.       if (name)
  189.         {
  190.           mime_ct2 = obj->options->file_type_fn(name,
  191.                                                 obj->options->stream_closure);
  192.           mime_ct = mime_ct2;
  193.           XP_FREE(name);
  194.           if (!mime_ct2 || !strcasecomp (mime_ct2, UNKNOWN_CONTENT_TYPE))
  195.             {
  196.               FREEIF(mime_ct2);
  197.               mime_ct = APPLICATION_OCTET_STREAM;
  198.             }
  199.         }
  200.     }
  201.   if (!mime_ct)
  202.     mime_ct = APPLICATION_OCTET_STREAM;
  203.  
  204.   FREEIF(sun_data_type);
  205.  
  206.  
  207.   /* Convert recognised Sun encodings to the corresponding MIME encodings.
  208.      However, if the X-Sun-Encoding-Info field contains more than one
  209.      encoding (that is, contains a comma) then assign it the encoding of
  210.      the *rightmost* element in the list; and change its Content-Type to
  211.      application/octet-stream.  Examples:
  212.  
  213.              Sun Type:                    Translates To:
  214.         ==================            ====================
  215.         type:     TEXT                type:     text/plain
  216.         encoding: COMPRESS            encoding: x-compress
  217.  
  218.         type:     POSTSCRIPT          type:     application/x-compress
  219.         encoding: COMPRESS,UUENCODE   encoding: x-uuencode
  220.  
  221.         type:     TEXT                type:     application/octet-stream
  222.         encoding: UNKNOWN,UUENCODE    encoding: x-uuencode
  223.    */
  224.  
  225.   sun_data_type = (mult->hdrs
  226.                    ? MimeHeaders_get (mult->hdrs, HEADER_X_SUN_ENCODING_INFO,
  227.                                       FALSE,FALSE)
  228.                    : 0);
  229.   sun_enc_info = sun_data_type;
  230.  
  231.  
  232.   /* this "adpcm-compress" pseudo-encoding is some random junk that
  233.      MailTool adds to the encoding description of .AU files: we can
  234.      ignore it if it is the leftmost element of the encoding field.
  235.      (It looks like it's created via `audioconvert -f g721'.  Why?
  236.      Who knows.)
  237.    */
  238.   if (sun_enc_info && !strncasecomp (sun_enc_info, "adpcm-compress", 14))
  239.     {
  240.       sun_enc_info += 14;
  241.       while (XP_IS_SPACE(*sun_enc_info) || *sun_enc_info == ',')
  242.         sun_enc_info++;
  243.     }
  244.  
  245.   /* Extract the last element of the encoding field, changing the content
  246.      type if necessary (as described above.)
  247.    */
  248.   if (sun_enc_info && *sun_enc_info)
  249.     {
  250.       const char *prev;
  251.       const char *end = XP_STRRCHR(sun_enc_info, ',');
  252.       if (end)
  253.         {
  254.           const char *start = sun_enc_info;
  255.           sun_enc_info = end + 1;
  256.           while (XP_IS_SPACE(*sun_enc_info))
  257.             sun_enc_info++;
  258.           for (prev = end-1; prev > start && *prev != ','; prev--)
  259.             ;
  260.           if (*prev == ',') prev++;
  261.  
  262.           if (!strncasecomp (prev, "uuencode", end-prev))
  263.             mime_ct = APPLICATION_UUENCODE;
  264.           else if (!strncasecomp (prev, "gzip", end-prev))
  265.             mime_ct = APPLICATION_GZIP;
  266.           else if (!strncasecomp (prev, "compress", end-prev))
  267.             mime_ct = APPLICATION_COMPRESS;
  268.           else if (!strncasecomp (prev, "default-compress", end-prev))
  269.             mime_ct = APPLICATION_COMPRESS;
  270.           else
  271.             mime_ct = APPLICATION_OCTET_STREAM;
  272.         }
  273.     }
  274.  
  275.   /* Convert the remaining Sun encoding to a MIME encoding.
  276.      If it isn't known, change the content-type instead.
  277.    */
  278.   if (!sun_enc_info || !*sun_enc_info)
  279.     ;
  280.   else if (!strcasecomp(sun_enc_info,"compress")) mime_cte = ENCODING_COMPRESS;
  281.   else if (!strcasecomp(sun_enc_info,"uuencode")) mime_cte = ENCODING_UUENCODE;
  282.   else if (!strcasecomp(sun_enc_info,"gzip"))      mime_cte = ENCODING_GZIP;
  283.   else                                        mime_ct = APPLICATION_OCTET_STREAM;
  284.  
  285.   FREEIF(sun_data_type);
  286.  
  287.  
  288.   /* Now that we know its type and encoding, create a MimeObject to represent
  289.      this part.
  290.    */
  291.   child = mime_create(mime_ct, mult->hdrs, obj->options);
  292.   if (!child)
  293.     {
  294.       status = MK_OUT_OF_MEMORY;
  295.       goto FAIL;
  296.     }
  297.  
  298.   /* Fake out the child's content-type and encoding (it probably doesn't have
  299.      one right now, because the X-Sun- headers aren't generally recognised by
  300.      the rest of this library.)
  301.    */
  302.   FREEIF(child->content_type);
  303.   FREEIF(child->encoding);
  304.   XP_ASSERT(mime_ct);
  305.   child->content_type = (mime_ct  ? XP_STRDUP(mime_ct)  : 0);
  306.   child->encoding     = (mime_cte ? XP_STRDUP(mime_cte) : 0);
  307.  
  308.   status = ((MimeContainerClass *) obj->class)->add_child(obj, child);
  309.   if (status < 0)
  310.     {
  311.       mime_free(child);
  312.       child = 0;
  313.       goto FAIL;
  314.     }
  315.  
  316.   /* Sun attachments always have separators between parts. */
  317.   status = MimeObject_write_separator(obj);
  318.   if (status < 0) goto FAIL;
  319.  
  320.   /* And now that we've added this new object to our list of
  321.      children, start its parser going. */
  322.   status = child->class->parse_begin(child);
  323.   if (status < 0) goto FAIL;
  324.  
  325.  FAIL:
  326.   FREEIF(mime_ct2);
  327.   FREEIF(sun_data_type);
  328.   return status;
  329. }
  330.  
  331.  
  332. static int
  333. MimeSunAttachment_parse_child_line (MimeObject *obj, char *line, int32 length,
  334.                                     XP_Bool first_line_p)
  335. {
  336.   MimeContainer *cont = (MimeContainer *) obj;
  337.   MimeObject *kid;
  338.  
  339.   /* This is simpler than MimeMultipart->parse_child_line in that it doesn't
  340.      play games about body parts without trailing newlines.
  341.    */
  342.  
  343.   XP_ASSERT(cont->nchildren > 0);
  344.   if (cont->nchildren <= 0)
  345.     return -1;
  346.  
  347.   kid = cont->children[cont->nchildren-1];
  348.   XP_ASSERT(kid);
  349.   if (!kid) return -1;
  350.  
  351.   return kid->class->parse_buffer (line, length, kid);
  352. }
  353.