Merge branch 'upstream' into tizen_base
[platform/upstream/libjpeg-turbo.git] / wrjpgcom.c
index 531c152..060925f 100644 (file)
@@ -4,7 +4,7 @@
  * This file was part of the Independent JPEG Group's software:
  * Copyright (C) 1994-1997, Thomas G. Lane.
  * libjpeg-turbo Modifications:
- * Copyright (C) 2014, D. R. Commander.
+ * Copyright (C) 2014, 2022, D. R. Commander.
  * For conditions of distribution and use, see the accompanying README.ijg
  * file.
  *
  * JPEG markers.
  */
 
+#ifdef _MSC_VER
+#define _CRT_SECURE_NO_DEPRECATE
+#endif
+
 #define JPEG_CJPEG_DJPEG        /* to get the command-line config symbols */
 #include "jinclude.h"           /* get auto-config symbols, <stdio.h> */
 
-#ifndef HAVE_STDLIB_H           /* <stdlib.h> should declare malloc() */
-extern void *malloc ();
-#endif
 #include <ctype.h>              /* to declare isupper(), tolower() */
 #ifdef USE_SETMODE
 #include <fcntl.h>              /* to declare setmode()'s parameter macros */
@@ -27,16 +28,6 @@ extern void *malloc ();
 #include <io.h>                 /* to declare setmode() */
 #endif
 
-#ifdef USE_CCOMMAND             /* command-line reader for Macintosh */
-#ifdef __MWERKS__
-#include <SIOUX.h>              /* Metrowerks needs this */
-#include <console.h>            /* ... and this */
-#endif
-#ifdef THINK_C
-#include <console.h>            /* Think declares it here */
-#endif
-#endif
-
 #ifdef DONT_USE_B_MODE          /* define mode parameters for fopen() */
 #define READ_BINARY     "r"
 #define WRITE_BINARY    "w"
@@ -57,7 +48,7 @@ extern void *malloc ();
  */
 
 #ifndef MAX_COM_LENGTH
-#define MAX_COM_LENGTH 65000L   /* must be <= 65533 in any case */
+#define MAX_COM_LENGTH  65000L  /* must be <= 65533 in any case */
 #endif
 
 
@@ -83,7 +74,7 @@ static FILE *outfile;           /* output JPEG file */
 
 /* Read one byte, testing for EOF */
 static int
-read_1_byte (void)
+read_1_byte(void)
 {
   int c;
 
@@ -96,7 +87,7 @@ read_1_byte (void)
 /* Read 2 bytes, convert to unsigned int */
 /* All 2-byte quantities in JPEG markers are MSB first */
 static unsigned int
-read_2_bytes (void)
+read_2_bytes(void)
 {
   int c1, c2;
 
@@ -106,34 +97,34 @@ read_2_bytes (void)
   c2 = NEXTBYTE();
   if (c2 == EOF)
     ERREXIT("Premature EOF in JPEG file");
-  return (((unsigned int) c1) << 8) + ((unsigned int) c2);
+  return (((unsigned int)c1) << 8) + ((unsigned int)c2);
 }
 
 
 /* Routines to write data to output file */
 
 static void
-write_1_byte (int c)
+write_1_byte(int c)
 {
   PUTBYTE(c);
 }
 
 static void
-write_2_bytes (unsigned int val)
+write_2_bytes(unsigned int val)
 {
   PUTBYTE((val >> 8) & 0xFF);
   PUTBYTE(val & 0xFF);
 }
 
 static void
-write_marker (int marker)
+write_marker(int marker)
 {
   PUTBYTE(0xFF);
   PUTBYTE(marker);
 }
 
 static void
-copy_rest_of_file (void)
+copy_rest_of_file(void)
 {
   int c;
 
@@ -148,23 +139,23 @@ copy_rest_of_file (void)
  * in this program.  (See jdmarker.c for a more complete list.)
  */
 
-#define M_SOF0  0xC0            /* Start Of Frame N */
-#define M_SOF1  0xC1            /* N indicates which compression process */
-#define M_SOF2  0xC2            /* Only SOF0-SOF2 are now in common use */
-#define M_SOF3  0xC3
-#define M_SOF5  0xC5            /* NB: codes C4 and CC are NOT SOF markers */
-#define M_SOF6  0xC6
-#define M_SOF7  0xC7
-#define M_SOF9  0xC9
-#define M_SOF10 0xCA
-#define M_SOF11 0xCB
-#define M_SOF13 0xCD
-#define M_SOF14 0xCE
-#define M_SOF15 0xCF
-#define M_SOI   0xD8            /* Start Of Image (beginning of datastream) */
-#define M_EOI   0xD9            /* End Of Image (end of datastream) */
-#define M_SOS   0xDA            /* Start Of Scan (begins compressed data) */
-#define M_COM   0xFE            /* COMment */
+#define M_SOF0   0xC0           /* Start Of Frame N */
+#define M_SOF1   0xC1           /* N indicates which compression process */
+#define M_SOF2   0xC2           /* Only SOF0-SOF2 are now in common use */
+#define M_SOF3   0xC3
+#define M_SOF5   0xC5           /* NB: codes C4 and CC are NOT SOF markers */
+#define M_SOF6   0xC6
+#define M_SOF7   0xC7
+#define M_SOF9   0xC9
+#define M_SOF10  0xCA
+#define M_SOF11  0xCB
+#define M_SOF13  0xCD
+#define M_SOF14  0xCE
+#define M_SOF15  0xCF
+#define M_SOI    0xD8           /* Start Of Image (beginning of datastream) */
+#define M_EOI    0xD9           /* End Of Image (end of datastream) */
+#define M_SOS    0xDA           /* Start Of Scan (begins compressed data) */
+#define M_COM    0xFE           /* COMment */
 
 
 /*
@@ -178,7 +169,7 @@ copy_rest_of_file (void)
  */
 
 static int
-next_marker (void)
+next_marker(void)
 {
   int c;
   int discarded_bytes = 0;
@@ -213,7 +204,7 @@ next_marker (void)
  */
 
 static int
-first_marker (void)
+first_marker(void)
 {
   int c1, c2;
 
@@ -235,7 +226,7 @@ first_marker (void)
  */
 
 static void
-copy_variable (void)
+copy_variable(void)
 /* Copy an unknown or uninteresting variable-length marker */
 {
   unsigned int length;
@@ -255,7 +246,7 @@ copy_variable (void)
 }
 
 static void
-skip_variable (void)
+skip_variable(void)
 /* Skip over an unknown or uninteresting variable-length marker */
 {
   unsigned int length;
@@ -268,7 +259,7 @@ skip_variable (void)
   length -= 2;
   /* Skip over the remaining bytes */
   while (length > 0) {
-    (void) read_1_byte();
+    (void)read_1_byte();
     length--;
   }
 }
@@ -280,7 +271,7 @@ skip_variable (void)
  */
 
 static int
-scan_JPEG_header (int keep_COM)
+scan_JPEG_header(int keep_COM)
 {
   int marker;
 
@@ -342,7 +333,7 @@ static const char *progname;    /* program name for error messages */
 
 
 static void
-usage (void)
+usage(void)
 /* complain about bad command line */
 {
   fprintf(stderr, "wrjpgcom inserts a textual comment in a JPEG file.\n");
@@ -364,7 +355,7 @@ usage (void)
   fprintf(stderr, "If you do not give either -comment or -cfile on the command line,\n");
   fprintf(stderr, "then the comment text is read from standard input.\n");
   fprintf(stderr, "It can be multiple lines, up to %u characters total.\n",
-          (unsigned int) MAX_COM_LENGTH);
+          (unsigned int)MAX_COM_LENGTH);
 #ifndef TWO_FILE_COMMANDLINE
   fprintf(stderr, "You must specify an input JPEG file name when supplying\n");
   fprintf(stderr, "comment text from standard input.\n");
@@ -375,7 +366,7 @@ usage (void)
 
 
 static int
-keymatch (char *arg, const char *keyword, int minchars)
+keymatch(char *arg, const char *keyword, int minchars)
 /* Case-insensitive matching of (possibly abbreviated) keyword switches. */
 /* keyword is the constant keyword (must be lower case already), */
 /* minchars is length of minimum legal abbreviation. */
@@ -404,7 +395,7 @@ keymatch (char *arg, const char *keyword, int minchars)
  */
 
 int
-main (int argc, char **argv)
+main(int argc, char **argv)
 {
   int argn;
   char *arg;
@@ -414,11 +405,6 @@ main (int argc, char **argv)
   unsigned int comment_length = 0;
   int marker;
 
-  /* On Mac, fetch a command line. */
-#ifdef USE_CCOMMAND
-  argc = ccommand(&argv);
-#endif
-
   progname = argv[0];
   if (progname == NULL || progname[0] == 0)
     progname = "wrjpgcom";      /* in case C library doesn't provide it */
@@ -444,38 +430,38 @@ main (int argc, char **argv)
        * under MS-DOG and must parse out the quoted string ourselves.  Sigh.
        */
       if (comment_arg[0] == '"') {
-        comment_arg = (char *) malloc((size_t) MAX_COM_LENGTH);
+        comment_arg = (char *)malloc((size_t)MAX_COM_LENGTH);
         if (comment_arg == NULL)
           ERREXIT("Insufficient memory");
-        if (strlen(argv[argn]) + 2 >= (size_t) MAX_COM_LENGTH) {
+        if (strlen(argv[argn]) + 2 >= (size_t)MAX_COM_LENGTH) {
           fprintf(stderr, "Comment text may not exceed %u bytes\n",
-                  (unsigned int) MAX_COM_LENGTH);
+                  (unsigned int)MAX_COM_LENGTH);
           exit(EXIT_FAILURE);
         }
-        strcpy(comment_arg, argv[argn]+1);
+        strcpy(comment_arg, argv[argn] + 1);
         for (;;) {
-          comment_length = (unsigned int) strlen(comment_arg);
-          if (comment_length > 0 && comment_arg[comment_length-1] == '"') {
-            comment_arg[comment_length-1] = '\0'; /* zap terminating quote */
+          comment_length = (unsigned int)strlen(comment_arg);
+          if (comment_length > 0 && comment_arg[comment_length - 1] == '"') {
+            comment_arg[comment_length - 1] = '\0'; /* zap terminating quote */
             break;
           }
           if (++argn >= argc)
             ERREXIT("Missing ending quote mark");
           if (strlen(comment_arg) + strlen(argv[argn]) + 2 >=
-              (size_t) MAX_COM_LENGTH) {
+              (size_t)MAX_COM_LENGTH) {
             fprintf(stderr, "Comment text may not exceed %u bytes\n",
-                    (unsigned int) MAX_COM_LENGTH);
+                    (unsigned int)MAX_COM_LENGTH);
             exit(EXIT_FAILURE);
           }
           strcat(comment_arg, " ");
           strcat(comment_arg, argv[argn]);
         }
-      } else if (strlen(argv[argn]) >= (size_t) MAX_COM_LENGTH) {
+      } else if (strlen(argv[argn]) >= (size_t)MAX_COM_LENGTH) {
         fprintf(stderr, "Comment text may not exceed %u bytes\n",
-                (unsigned int) MAX_COM_LENGTH);
+                (unsigned int)MAX_COM_LENGTH);
         exit(EXIT_FAILURE);
       }
-      comment_length = (unsigned int) strlen(comment_arg);
+      comment_length = (unsigned int)strlen(comment_arg);
     } else
       usage();
   }
@@ -513,18 +499,17 @@ main (int argc, char **argv)
   /* Open the output file. */
 #ifdef TWO_FILE_COMMANDLINE
   /* Must have explicit output file name */
-  if (argn != argc-2) {
-    fprintf(stderr, "%s: must name one input and one output file\n",
-            progname);
+  if (argn != argc - 2) {
+    fprintf(stderr, "%s: must name one input and one output file\n", progname);
     usage();
   }
-  if ((outfile = fopen(argv[argn+1], WRITE_BINARY)) == NULL) {
-    fprintf(stderr, "%s: can't open %s\n", progname, argv[argn+1]);
+  if ((outfile = fopen(argv[argn + 1], WRITE_BINARY)) == NULL) {
+    fprintf(stderr, "%s: can't open %s\n", progname, argv[argn + 1]);
     exit(EXIT_FAILURE);
   }
 #else
   /* Unix style: expect zero or one file name */
-  if (argn < argc-1) {
+  if (argn < argc - 1) {
     fprintf(stderr, "%s: only one input file\n", progname);
     usage();
   }
@@ -547,18 +532,18 @@ main (int argc, char **argv)
     FILE *src_file;
     int c;
 
-    comment_arg = (char *) malloc((size_t) MAX_COM_LENGTH);
+    comment_arg = (char *)malloc((size_t)MAX_COM_LENGTH);
     if (comment_arg == NULL)
       ERREXIT("Insufficient memory");
     comment_length = 0;
     src_file = (comment_file != NULL ? comment_file : stdin);
     while ((c = getc(src_file)) != EOF) {
-      if (comment_length >= (unsigned int) MAX_COM_LENGTH) {
+      if (comment_length >= (unsigned int)MAX_COM_LENGTH) {
         fprintf(stderr, "Comment text may not exceed %u bytes\n",
-                (unsigned int) MAX_COM_LENGTH);
+                (unsigned int)MAX_COM_LENGTH);
         exit(EXIT_FAILURE);
       }
-      comment_arg[comment_length++] = (char) c;
+      comment_arg[comment_length++] = (char)c;
     }
     if (comment_file != NULL)
       fclose(comment_file);
@@ -581,7 +566,7 @@ main (int argc, char **argv)
     }
   }
   /* Duplicate the remainder of the source file.
-   * Note that any COM markers occuring after SOF will not be touched.
+   * Note that any COM markers occurring after SOF will not be touched.
    */
   write_marker(marker);
   copy_rest_of_file();