kusano 7d535a
/*
kusano 7d535a
 * wrjpgcom.c
kusano 7d535a
 *
kusano 7d535a
 * Copyright (C) 1994-1997, Thomas G. Lane.
kusano 7d535a
 * This file is part of the Independent JPEG Group's software.
kusano 7d535a
 * For conditions of distribution and use, see the accompanying README file.
kusano 7d535a
 *
kusano 7d535a
 * This file contains a very simple stand-alone application that inserts
kusano 7d535a
 * user-supplied text as a COM (comment) marker in a JFIF file.
kusano 7d535a
 * This may be useful as an example of the minimum logic needed to parse
kusano 7d535a
 * JPEG markers.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#define JPEG_CJPEG_DJPEG	/* to get the command-line config symbols */
kusano 7d535a
#include "jinclude.h"		/* get auto-config symbols, <stdio.h> */</stdio.h>
kusano 7d535a
kusano 7d535a
#ifndef HAVE_STDLIB_H		/* <stdlib.h> should declare malloc() */</stdlib.h>
kusano 7d535a
extern void * malloc ();
kusano 7d535a
#endif
kusano 7d535a
#include <ctype.h>		/* to declare isupper(), tolower() */</ctype.h>
kusano 7d535a
#ifdef USE_SETMODE
kusano 7d535a
#include <fcntl.h>		/* to declare setmode()'s parameter macros */</fcntl.h>
kusano 7d535a
/* If you have setmode() but not <io.h>, just delete this line: */</io.h>
kusano 7d535a
#include <io.h>			/* to declare setmode() */</io.h>
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
#ifdef USE_CCOMMAND		/* command-line reader for Macintosh */
kusano 7d535a
#ifdef __MWERKS__
kusano 7d535a
#include <sioux.h>              /* Metrowerks needs this */</sioux.h>
kusano 7d535a
#include <console.h>		/* ... and this */</console.h>
kusano 7d535a
#endif
kusano 7d535a
#ifdef THINK_C
kusano 7d535a
#include <console.h>		/* Think declares it here */</console.h>
kusano 7d535a
#endif
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
#ifdef DONT_USE_B_MODE		/* define mode parameters for fopen() */
kusano 7d535a
#define READ_BINARY	"r"
kusano 7d535a
#define WRITE_BINARY	"w"
kusano 7d535a
#else
kusano 7d535a
#ifdef VMS			/* VMS is very nonstandard */
kusano 7d535a
#define READ_BINARY	"rb", "ctx=stm"
kusano 7d535a
#define WRITE_BINARY	"wb", "ctx=stm"
kusano 7d535a
#else				/* standard ANSI-compliant case */
kusano 7d535a
#define READ_BINARY	"rb"
kusano 7d535a
#define WRITE_BINARY	"wb"
kusano 7d535a
#endif
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
#ifndef EXIT_FAILURE		/* define exit() codes if not provided */
kusano 7d535a
#define EXIT_FAILURE  1
kusano 7d535a
#endif
kusano 7d535a
#ifndef EXIT_SUCCESS
kusano 7d535a
#ifdef VMS
kusano 7d535a
#define EXIT_SUCCESS  1		/* VMS is very nonstandard */
kusano 7d535a
#else
kusano 7d535a
#define EXIT_SUCCESS  0
kusano 7d535a
#endif
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
/* Reduce this value if your malloc() can't allocate blocks up to 64K.
kusano 7d535a
 * On DOS, compiling in large model is usually a better solution.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#ifndef MAX_COM_LENGTH
kusano 7d535a
#define MAX_COM_LENGTH 65000L	/* must be <= 65533 in any case */
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * These macros are used to read the input file and write the output file.
kusano 7d535a
 * To reuse this code in another application, you might need to change these.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
static FILE * infile;		/* input JPEG file */
kusano 7d535a
kusano 7d535a
/* Return next input byte, or EOF if no more */
kusano 7d535a
#define NEXTBYTE()  getc(infile)
kusano 7d535a
kusano 7d535a
static FILE * outfile;		/* output JPEG file */
kusano 7d535a
kusano 7d535a
/* Emit an output byte */
kusano 7d535a
#define PUTBYTE(x)  putc((x), outfile)
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* Error exit handler */
kusano 7d535a
#define ERREXIT(msg)  (fprintf(stderr, "%s\n", msg), exit(EXIT_FAILURE))
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* Read one byte, testing for EOF */
kusano 7d535a
static int
kusano 7d535a
read_1_byte (void)
kusano 7d535a
{
kusano 7d535a
  int c;
kusano 7d535a
kusano 7d535a
  c = NEXTBYTE();
kusano 7d535a
  if (c == EOF)
kusano 7d535a
    ERREXIT("Premature EOF in JPEG file");
kusano 7d535a
  return c;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
/* Read 2 bytes, convert to unsigned int */
kusano 7d535a
/* All 2-byte quantities in JPEG markers are MSB first */
kusano 7d535a
static unsigned int
kusano 7d535a
read_2_bytes (void)
kusano 7d535a
{
kusano 7d535a
  int c1, c2;
kusano 7d535a
kusano 7d535a
  c1 = NEXTBYTE();
kusano 7d535a
  if (c1 == EOF)
kusano 7d535a
    ERREXIT("Premature EOF in JPEG file");
kusano 7d535a
  c2 = NEXTBYTE();
kusano 7d535a
  if (c2 == EOF)
kusano 7d535a
    ERREXIT("Premature EOF in JPEG file");
kusano 7d535a
  return (((unsigned int) c1) << 8) + ((unsigned int) c2);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* Routines to write data to output file */
kusano 7d535a
kusano 7d535a
static void
kusano 7d535a
write_1_byte (int c)
kusano 7d535a
{
kusano 7d535a
  PUTBYTE(c);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static void
kusano 7d535a
write_2_bytes (unsigned int val)
kusano 7d535a
{
kusano 7d535a
  PUTBYTE((val >> 8) & 0xFF);
kusano 7d535a
  PUTBYTE(val & 0xFF);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static void
kusano 7d535a
write_marker (int marker)
kusano 7d535a
{
kusano 7d535a
  PUTBYTE(0xFF);
kusano 7d535a
  PUTBYTE(marker);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static void
kusano 7d535a
copy_rest_of_file (void)
kusano 7d535a
{
kusano 7d535a
  int c;
kusano 7d535a
kusano 7d535a
  while ((c = NEXTBYTE()) != EOF)
kusano 7d535a
    PUTBYTE(c);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * JPEG markers consist of one or more 0xFF bytes, followed by a marker
kusano 7d535a
 * code byte (which is not an FF).  Here are the marker codes of interest
kusano 7d535a
 * in this program.  (See jdmarker.c for a more complete list.)
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#define M_SOF0  0xC0		/* Start Of Frame N */
kusano 7d535a
#define M_SOF1  0xC1		/* N indicates which compression process */
kusano 7d535a
#define M_SOF2  0xC2		/* Only SOF0-SOF2 are now in common use */
kusano 7d535a
#define M_SOF3  0xC3
kusano 7d535a
#define M_SOF5  0xC5		/* NB: codes C4 and CC are NOT SOF markers */
kusano 7d535a
#define M_SOF6  0xC6
kusano 7d535a
#define M_SOF7  0xC7
kusano 7d535a
#define M_SOF9  0xC9
kusano 7d535a
#define M_SOF10 0xCA
kusano 7d535a
#define M_SOF11 0xCB
kusano 7d535a
#define M_SOF13 0xCD
kusano 7d535a
#define M_SOF14 0xCE
kusano 7d535a
#define M_SOF15 0xCF
kusano 7d535a
#define M_SOI   0xD8		/* Start Of Image (beginning of datastream) */
kusano 7d535a
#define M_EOI   0xD9		/* End Of Image (end of datastream) */
kusano 7d535a
#define M_SOS   0xDA		/* Start Of Scan (begins compressed data) */
kusano 7d535a
#define M_COM   0xFE		/* COMment */
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Find the next JPEG marker and return its marker code.
kusano 7d535a
 * We expect at least one FF byte, possibly more if the compressor used FFs
kusano 7d535a
 * to pad the file.  (Padding FFs will NOT be replicated in the output file.)
kusano 7d535a
 * There could also be non-FF garbage between markers.  The treatment of such
kusano 7d535a
 * garbage is unspecified; we choose to skip over it but emit a warning msg.
kusano 7d535a
 * NB: this routine must not be used after seeing SOS marker, since it will
kusano 7d535a
 * not deal correctly with FF/00 sequences in the compressed image data...
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
static int
kusano 7d535a
next_marker (void)
kusano 7d535a
{
kusano 7d535a
  int c;
kusano 7d535a
  int discarded_bytes = 0;
kusano 7d535a
kusano 7d535a
  /* Find 0xFF byte; count and skip any non-FFs. */
kusano 7d535a
  c = read_1_byte();
kusano 7d535a
  while (c != 0xFF) {
kusano 7d535a
    discarded_bytes++;
kusano 7d535a
    c = read_1_byte();
kusano 7d535a
  }
kusano 7d535a
  /* Get marker code byte, swallowing any duplicate FF bytes.  Extra FFs
kusano 7d535a
   * are legal as pad bytes, so don't count them in discarded_bytes.
kusano 7d535a
   */
kusano 7d535a
  do {
kusano 7d535a
    c = read_1_byte();
kusano 7d535a
  } while (c == 0xFF);
kusano 7d535a
kusano 7d535a
  if (discarded_bytes != 0) {
kusano 7d535a
    fprintf(stderr, "Warning: garbage data found in JPEG file\n");
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  return c;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Read the initial marker, which should be SOI.
kusano 7d535a
 * For a JFIF file, the first two bytes of the file should be literally
kusano 7d535a
 * 0xFF M_SOI.  To be more general, we could use next_marker, but if the
kusano 7d535a
 * input file weren't actually JPEG at all, next_marker might read the whole
kusano 7d535a
 * file and then return a misleading error message...
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
static int
kusano 7d535a
first_marker (void)
kusano 7d535a
{
kusano 7d535a
  int c1, c2;
kusano 7d535a
kusano 7d535a
  c1 = NEXTBYTE();
kusano 7d535a
  c2 = NEXTBYTE();
kusano 7d535a
  if (c1 != 0xFF || c2 != M_SOI)
kusano 7d535a
    ERREXIT("Not a JPEG file");
kusano 7d535a
  return c2;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Most types of marker are followed by a variable-length parameter segment.
kusano 7d535a
 * This routine skips over the parameters for any marker we don't otherwise
kusano 7d535a
 * want to process.
kusano 7d535a
 * Note that we MUST skip the parameter segment explicitly in order not to
kusano 7d535a
 * be fooled by 0xFF bytes that might appear within the parameter segment;
kusano 7d535a
 * such bytes do NOT introduce new markers.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
static void
kusano 7d535a
copy_variable (void)
kusano 7d535a
/* Copy an unknown or uninteresting variable-length marker */
kusano 7d535a
{
kusano 7d535a
  unsigned int length;
kusano 7d535a
kusano 7d535a
  /* Get the marker parameter length count */
kusano 7d535a
  length = read_2_bytes();
kusano 7d535a
  write_2_bytes(length);
kusano 7d535a
  /* Length includes itself, so must be at least 2 */
kusano 7d535a
  if (length < 2)
kusano 7d535a
    ERREXIT("Erroneous JPEG marker length");
kusano 7d535a
  length -= 2;
kusano 7d535a
  /* Skip over the remaining bytes */
kusano 7d535a
  while (length > 0) {
kusano 7d535a
    write_1_byte(read_1_byte());
kusano 7d535a
    length--;
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static void
kusano 7d535a
skip_variable (void)
kusano 7d535a
/* Skip over an unknown or uninteresting variable-length marker */
kusano 7d535a
{
kusano 7d535a
  unsigned int length;
kusano 7d535a
kusano 7d535a
  /* Get the marker parameter length count */
kusano 7d535a
  length = read_2_bytes();
kusano 7d535a
  /* Length includes itself, so must be at least 2 */
kusano 7d535a
  if (length < 2)
kusano 7d535a
    ERREXIT("Erroneous JPEG marker length");
kusano 7d535a
  length -= 2;
kusano 7d535a
  /* Skip over the remaining bytes */
kusano 7d535a
  while (length > 0) {
kusano 7d535a
    (void) read_1_byte();
kusano 7d535a
    length--;
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Parse the marker stream until SOFn or EOI is seen;
kusano 7d535a
 * copy data to output, but discard COM markers unless keep_COM is true.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
static int
kusano 7d535a
scan_JPEG_header (int keep_COM)
kusano 7d535a
{
kusano 7d535a
  int marker;
kusano 7d535a
kusano 7d535a
  /* Expect SOI at start of file */
kusano 7d535a
  if (first_marker() != M_SOI)
kusano 7d535a
    ERREXIT("Expected SOI marker first");
kusano 7d535a
  write_marker(M_SOI);
kusano 7d535a
kusano 7d535a
  /* Scan miscellaneous markers until we reach SOFn. */
kusano 7d535a
  for (;;) {
kusano 7d535a
    marker = next_marker();
kusano 7d535a
    switch (marker) {
kusano 7d535a
      /* Note that marker codes 0xC4, 0xC8, 0xCC are not, and must not be,
kusano 7d535a
       * treated as SOFn.  C4 in particular is actually DHT.
kusano 7d535a
       */
kusano 7d535a
    case M_SOF0:		/* Baseline */
kusano 7d535a
    case M_SOF1:		/* Extended sequential, Huffman */
kusano 7d535a
    case M_SOF2:		/* Progressive, Huffman */
kusano 7d535a
    case M_SOF3:		/* Lossless, Huffman */
kusano 7d535a
    case M_SOF5:		/* Differential sequential, Huffman */
kusano 7d535a
    case M_SOF6:		/* Differential progressive, Huffman */
kusano 7d535a
    case M_SOF7:		/* Differential lossless, Huffman */
kusano 7d535a
    case M_SOF9:		/* Extended sequential, arithmetic */
kusano 7d535a
    case M_SOF10:		/* Progressive, arithmetic */
kusano 7d535a
    case M_SOF11:		/* Lossless, arithmetic */
kusano 7d535a
    case M_SOF13:		/* Differential sequential, arithmetic */
kusano 7d535a
    case M_SOF14:		/* Differential progressive, arithmetic */
kusano 7d535a
    case M_SOF15:		/* Differential lossless, arithmetic */
kusano 7d535a
      return marker;
kusano 7d535a
kusano 7d535a
    case M_SOS:			/* should not see compressed data before SOF */
kusano 7d535a
      ERREXIT("SOS without prior SOFn");
kusano 7d535a
      break;
kusano 7d535a
kusano 7d535a
    case M_EOI:			/* in case it's a tables-only JPEG stream */
kusano 7d535a
      return marker;
kusano 7d535a
kusano 7d535a
    case M_COM:			/* Existing COM: conditionally discard */
kusano 7d535a
      if (keep_COM) {
kusano 7d535a
	write_marker(marker);
kusano 7d535a
	copy_variable();
kusano 7d535a
      } else {
kusano 7d535a
	skip_variable();
kusano 7d535a
      }
kusano 7d535a
      break;
kusano 7d535a
kusano 7d535a
    default:			/* Anything else just gets copied */
kusano 7d535a
      write_marker(marker);
kusano 7d535a
      copy_variable();		/* we assume it has a parameter count... */
kusano 7d535a
      break;
kusano 7d535a
    }
kusano 7d535a
  } /* end loop */
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* Command line parsing code */
kusano 7d535a
kusano 7d535a
static const char * progname;	/* program name for error messages */
kusano 7d535a
kusano 7d535a
kusano 7d535a
static void
kusano 7d535a
usage (void)
kusano 7d535a
/* complain about bad command line */
kusano 7d535a
{
kusano 7d535a
  fprintf(stderr, "wrjpgcom inserts a textual comment in a JPEG file.\n");
kusano 7d535a
  fprintf(stderr, "You can add to or replace any existing comment(s).\n");
kusano 7d535a
kusano 7d535a
  fprintf(stderr, "Usage: %s [switches] ", progname);
kusano 7d535a
#ifdef TWO_FILE_COMMANDLINE
kusano 7d535a
  fprintf(stderr, "inputfile outputfile\n");
kusano 7d535a
#else
kusano 7d535a
  fprintf(stderr, "[inputfile]\n");
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
  fprintf(stderr, "Switches (names may be abbreviated):\n");
kusano 7d535a
  fprintf(stderr, "  -replace         Delete any existing comments\n");
kusano 7d535a
  fprintf(stderr, "  -comment \"text\"  Insert comment with given text\n");
kusano 7d535a
  fprintf(stderr, "  -cfile name      Read comment from named file\n");
kusano 7d535a
  fprintf(stderr, "Notice that you must put quotes around the comment text\n");
kusano 7d535a
  fprintf(stderr, "when you use -comment.\n");
kusano 7d535a
  fprintf(stderr, "If you do not give either -comment or -cfile on the command line,\n");
kusano 7d535a
  fprintf(stderr, "then the comment text is read from standard input.\n");
kusano 7d535a
  fprintf(stderr, "It can be multiple lines, up to %u characters total.\n",
kusano 7d535a
	  (unsigned int) MAX_COM_LENGTH);
kusano 7d535a
#ifndef TWO_FILE_COMMANDLINE
kusano 7d535a
  fprintf(stderr, "You must specify an input JPEG file name when supplying\n");
kusano 7d535a
  fprintf(stderr, "comment text from standard input.\n");
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
  exit(EXIT_FAILURE);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
static int
kusano 7d535a
keymatch (char * arg, const char * keyword, int minchars)
kusano 7d535a
/* Case-insensitive matching of (possibly abbreviated) keyword switches. */
kusano 7d535a
/* keyword is the constant keyword (must be lower case already), */
kusano 7d535a
/* minchars is length of minimum legal abbreviation. */
kusano 7d535a
{
kusano 7d535a
  register int ca, ck;
kusano 7d535a
  register int nmatched = 0;
kusano 7d535a
kusano 7d535a
  while ((ca = *arg++) != '\0') {
kusano 7d535a
    if ((ck = *keyword++) == '\0')
kusano 7d535a
      return 0;			/* arg longer than keyword, no good */
kusano 7d535a
    if (isupper(ca))		/* force arg to lcase (assume ck is already) */
kusano 7d535a
      ca = tolower(ca);
kusano 7d535a
    if (ca != ck)
kusano 7d535a
      return 0;			/* no good */
kusano 7d535a
    nmatched++;			/* count matched characters */
kusano 7d535a
  }
kusano 7d535a
  /* reached end of argument; fail if it's too short for unique abbrev */
kusano 7d535a
  if (nmatched < minchars)
kusano 7d535a
    return 0;
kusano 7d535a
  return 1;			/* A-OK */
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * The main program.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
int
kusano 7d535a
main (int argc, char **argv)
kusano 7d535a
{
kusano 7d535a
  int argn;
kusano 7d535a
  char * arg;
kusano 7d535a
  int keep_COM = 1;
kusano 7d535a
  char * comment_arg = NULL;
kusano 7d535a
  FILE * comment_file = NULL;
kusano 7d535a
  unsigned int comment_length = 0;
kusano 7d535a
  int marker;
kusano 7d535a
kusano 7d535a
  /* On Mac, fetch a command line. */
kusano 7d535a
#ifdef USE_CCOMMAND
kusano 7d535a
  argc = ccommand(&argv);
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
  progname = argv[0];
kusano 7d535a
  if (progname == NULL || progname[0] == 0)
kusano 7d535a
    progname = "wrjpgcom";	/* in case C library doesn't provide it */
kusano 7d535a
kusano 7d535a
  /* Parse switches, if any */
kusano 7d535a
  for (argn = 1; argn < argc; argn++) {
kusano 7d535a
    arg = argv[argn];
kusano 7d535a
    if (arg[0] != '-')
kusano 7d535a
      break;			/* not switch, must be file name */
kusano 7d535a
    arg++;			/* advance over '-' */
kusano 7d535a
    if (keymatch(arg, "replace", 1)) {
kusano 7d535a
      keep_COM = 0;
kusano 7d535a
    } else if (keymatch(arg, "cfile", 2)) {
kusano 7d535a
      if (++argn >= argc) usage();
kusano 7d535a
      if ((comment_file = fopen(argv[argn], "r")) == NULL) {
kusano 7d535a
	fprintf(stderr, "%s: can't open %s\n", progname, argv[argn]);
kusano 7d535a
	exit(EXIT_FAILURE);
kusano 7d535a
      }
kusano 7d535a
    } else if (keymatch(arg, "comment", 1)) {
kusano 7d535a
      if (++argn >= argc) usage();
kusano 7d535a
      comment_arg = argv[argn];
kusano 7d535a
      /* If the comment text starts with '"', then we are probably running
kusano 7d535a
       * under MS-DOG and must parse out the quoted string ourselves.  Sigh.
kusano 7d535a
       */
kusano 7d535a
      if (comment_arg[0] == '"') {
kusano 7d535a
	comment_arg = (char *) malloc((size_t) MAX_COM_LENGTH);
kusano 7d535a
	if (comment_arg == NULL)
kusano 7d535a
	  ERREXIT("Insufficient memory");
kusano 7d535a
	strcpy(comment_arg, argv[argn]+1);
kusano 7d535a
	for (;;) {
kusano 7d535a
	  comment_length = (unsigned int) strlen(comment_arg);
kusano 7d535a
	  if (comment_length > 0 && comment_arg[comment_length-1] == '"') {
kusano 7d535a
	    comment_arg[comment_length-1] = '\0'; /* zap terminating quote */
kusano 7d535a
	    break;
kusano 7d535a
	  }
kusano 7d535a
	  if (++argn >= argc)
kusano 7d535a
	    ERREXIT("Missing ending quote mark");
kusano 7d535a
	  strcat(comment_arg, " ");
kusano 7d535a
	  strcat(comment_arg, argv[argn]);
kusano 7d535a
	}
kusano 7d535a
      }
kusano 7d535a
      comment_length = (unsigned int) strlen(comment_arg);
kusano 7d535a
    } else
kusano 7d535a
      usage();
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Cannot use both -comment and -cfile. */
kusano 7d535a
  if (comment_arg != NULL && comment_file != NULL)
kusano 7d535a
    usage();
kusano 7d535a
  /* If there is neither -comment nor -cfile, we will read the comment text
kusano 7d535a
   * from stdin; in this case there MUST be an input JPEG file name.
kusano 7d535a
   */
kusano 7d535a
  if (comment_arg == NULL && comment_file == NULL && argn >= argc)
kusano 7d535a
    usage();
kusano 7d535a
kusano 7d535a
  /* Open the input file. */
kusano 7d535a
  if (argn < argc) {
kusano 7d535a
    if ((infile = fopen(argv[argn], READ_BINARY)) == NULL) {
kusano 7d535a
      fprintf(stderr, "%s: can't open %s\n", progname, argv[argn]);
kusano 7d535a
      exit(EXIT_FAILURE);
kusano 7d535a
    }
kusano 7d535a
  } else {
kusano 7d535a
    /* default input file is stdin */
kusano 7d535a
#ifdef USE_SETMODE		/* need to hack file mode? */
kusano 7d535a
    setmode(fileno(stdin), O_BINARY);
kusano 7d535a
#endif
kusano 7d535a
#ifdef USE_FDOPEN		/* need to re-open in binary mode? */
kusano 7d535a
    if ((infile = fdopen(fileno(stdin), READ_BINARY)) == NULL) {
kusano 7d535a
      fprintf(stderr, "%s: can't open stdin\n", progname);
kusano 7d535a
      exit(EXIT_FAILURE);
kusano 7d535a
    }
kusano 7d535a
#else
kusano 7d535a
    infile = stdin;
kusano 7d535a
#endif
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Open the output file. */
kusano 7d535a
#ifdef TWO_FILE_COMMANDLINE
kusano 7d535a
  /* Must have explicit output file name */
kusano 7d535a
  if (argn != argc-2) {
kusano 7d535a
    fprintf(stderr, "%s: must name one input and one output file\n",
kusano 7d535a
	    progname);
kusano 7d535a
    usage();
kusano 7d535a
  }
kusano 7d535a
  if ((outfile = fopen(argv[argn+1], WRITE_BINARY)) == NULL) {
kusano 7d535a
    fprintf(stderr, "%s: can't open %s\n", progname, argv[argn+1]);
kusano 7d535a
    exit(EXIT_FAILURE);
kusano 7d535a
  }
kusano 7d535a
#else
kusano 7d535a
  /* Unix style: expect zero or one file name */
kusano 7d535a
  if (argn < argc-1) {
kusano 7d535a
    fprintf(stderr, "%s: only one input file\n", progname);
kusano 7d535a
    usage();
kusano 7d535a
  }
kusano 7d535a
  /* default output file is stdout */
kusano 7d535a
#ifdef USE_SETMODE		/* need to hack file mode? */
kusano 7d535a
  setmode(fileno(stdout), O_BINARY);
kusano 7d535a
#endif
kusano 7d535a
#ifdef USE_FDOPEN		/* need to re-open in binary mode? */
kusano 7d535a
  if ((outfile = fdopen(fileno(stdout), WRITE_BINARY)) == NULL) {
kusano 7d535a
    fprintf(stderr, "%s: can't open stdout\n", progname);
kusano 7d535a
    exit(EXIT_FAILURE);
kusano 7d535a
  }
kusano 7d535a
#else
kusano 7d535a
  outfile = stdout;
kusano 7d535a
#endif
kusano 7d535a
#endif /* TWO_FILE_COMMANDLINE */
kusano 7d535a
kusano 7d535a
  /* Collect comment text from comment_file or stdin, if necessary */
kusano 7d535a
  if (comment_arg == NULL) {
kusano 7d535a
    FILE * src_file;
kusano 7d535a
    int c;
kusano 7d535a
kusano 7d535a
    comment_arg = (char *) malloc((size_t) MAX_COM_LENGTH);
kusano 7d535a
    if (comment_arg == NULL)
kusano 7d535a
      ERREXIT("Insufficient memory");
kusano 7d535a
    comment_length = 0;
kusano 7d535a
    src_file = (comment_file != NULL ? comment_file : stdin);
kusano 7d535a
    while ((c = getc(src_file)) != EOF) {
kusano 7d535a
      if (comment_length >= (unsigned int) MAX_COM_LENGTH) {
kusano 7d535a
	fprintf(stderr, "Comment text may not exceed %u bytes\n",
kusano 7d535a
		(unsigned int) MAX_COM_LENGTH);
kusano 7d535a
	exit(EXIT_FAILURE);
kusano 7d535a
      }
kusano 7d535a
      comment_arg[comment_length++] = (char) c;
kusano 7d535a
    }
kusano 7d535a
    if (comment_file != NULL)
kusano 7d535a
      fclose(comment_file);
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Copy JPEG headers until SOFn marker;
kusano 7d535a
   * we will insert the new comment marker just before SOFn.
kusano 7d535a
   * This (a) causes the new comment to appear after, rather than before,
kusano 7d535a
   * existing comments; and (b) ensures that comments come after any JFIF
kusano 7d535a
   * or JFXX markers, as required by the JFIF specification.
kusano 7d535a
   */
kusano 7d535a
  marker = scan_JPEG_header(keep_COM);
kusano 7d535a
  /* Insert the new COM marker, but only if nonempty text has been supplied */
kusano 7d535a
  if (comment_length > 0) {
kusano 7d535a
    write_marker(M_COM);
kusano 7d535a
    write_2_bytes(comment_length + 2);
kusano 7d535a
    while (comment_length > 0) {
kusano 7d535a
      write_1_byte(*comment_arg++);
kusano 7d535a
      comment_length--;
kusano 7d535a
    }
kusano 7d535a
  }
kusano 7d535a
  /* Duplicate the remainder of the source file.
kusano 7d535a
   * Note that any COM markers occuring after SOF will not be touched.
kusano 7d535a
   */
kusano 7d535a
  write_marker(marker);
kusano 7d535a
  copy_rest_of_file();
kusano 7d535a
kusano 7d535a
  /* All done. */
kusano 7d535a
  exit(EXIT_SUCCESS);
kusano 7d535a
  return 0;			/* suppress no-return-value warnings */
kusano 7d535a
}