1c2c66affSColin Finck /*
2c2c66affSColin Finck * wrjpgcom.c
3c2c66affSColin Finck *
4c2c66affSColin Finck * Copyright (C) 1994-1997, Thomas G. Lane.
5*ef4f5757SThomas Faber * Modified 2015-2017 by Guido Vollbeding.
6c2c66affSColin Finck * This file is part of the Independent JPEG Group's software.
7c2c66affSColin Finck * For conditions of distribution and use, see the accompanying README file.
8c2c66affSColin Finck *
9c2c66affSColin Finck * This file contains a very simple stand-alone application that inserts
10c2c66affSColin Finck * user-supplied text as a COM (comment) marker in a JFIF file.
11c2c66affSColin Finck * This may be useful as an example of the minimum logic needed to parse
12c2c66affSColin Finck * JPEG markers.
13c2c66affSColin Finck */
14c2c66affSColin Finck
15c2c66affSColin Finck #define JPEG_CJPEG_DJPEG /* to get the command-line config symbols */
16c2c66affSColin Finck #include "jinclude.h" /* get auto-config symbols, <stdio.h> */
17c2c66affSColin Finck
18c2c66affSColin Finck #ifndef HAVE_STDLIB_H /* <stdlib.h> should declare malloc() */
19c2c66affSColin Finck extern void * malloc ();
20c2c66affSColin Finck #endif
21c2c66affSColin Finck #include <ctype.h> /* to declare isupper(), tolower() */
22c2c66affSColin Finck #ifdef USE_SETMODE
23c2c66affSColin Finck #include <fcntl.h> /* to declare setmode()'s parameter macros */
24c2c66affSColin Finck /* If you have setmode() but not <io.h>, just delete this line: */
25c2c66affSColin Finck #include <io.h> /* to declare setmode() */
26c2c66affSColin Finck #endif
27c2c66affSColin Finck
28c2c66affSColin Finck #ifdef USE_CCOMMAND /* command-line reader for Macintosh */
29c2c66affSColin Finck #ifdef __MWERKS__
30c2c66affSColin Finck #include <SIOUX.h> /* Metrowerks needs this */
31c2c66affSColin Finck #include <console.h> /* ... and this */
32c2c66affSColin Finck #endif
33c2c66affSColin Finck #ifdef THINK_C
34c2c66affSColin Finck #include <console.h> /* Think declares it here */
35c2c66affSColin Finck #endif
36c2c66affSColin Finck #endif
37c2c66affSColin Finck
38c2c66affSColin Finck #ifdef DONT_USE_B_MODE /* define mode parameters for fopen() */
39c2c66affSColin Finck #define READ_BINARY "r"
40c2c66affSColin Finck #define WRITE_BINARY "w"
41c2c66affSColin Finck #else
42c2c66affSColin Finck #ifdef VMS /* VMS is very nonstandard */
43c2c66affSColin Finck #define READ_BINARY "rb", "ctx=stm"
44c2c66affSColin Finck #define WRITE_BINARY "wb", "ctx=stm"
45c2c66affSColin Finck #else /* standard ANSI-compliant case */
46c2c66affSColin Finck #define READ_BINARY "rb"
47c2c66affSColin Finck #define WRITE_BINARY "wb"
48c2c66affSColin Finck #endif
49c2c66affSColin Finck #endif
50c2c66affSColin Finck
51c2c66affSColin Finck #ifndef EXIT_FAILURE /* define exit() codes if not provided */
52c2c66affSColin Finck #define EXIT_FAILURE 1
53c2c66affSColin Finck #endif
54c2c66affSColin Finck #ifndef EXIT_SUCCESS
55c2c66affSColin Finck #ifdef VMS
56c2c66affSColin Finck #define EXIT_SUCCESS 1 /* VMS is very nonstandard */
57c2c66affSColin Finck #else
58c2c66affSColin Finck #define EXIT_SUCCESS 0
59c2c66affSColin Finck #endif
60c2c66affSColin Finck #endif
61c2c66affSColin Finck
62c2c66affSColin Finck /* Reduce this value if your malloc() can't allocate blocks up to 64K.
63c2c66affSColin Finck * On DOS, compiling in large model is usually a better solution.
64c2c66affSColin Finck */
65c2c66affSColin Finck
66c2c66affSColin Finck #ifndef MAX_COM_LENGTH
67c2c66affSColin Finck #define MAX_COM_LENGTH 65000L /* must be <= 65533 in any case */
68c2c66affSColin Finck #endif
69c2c66affSColin Finck
70c2c66affSColin Finck
71c2c66affSColin Finck /*
72c2c66affSColin Finck * These macros are used to read the input file and write the output file.
73c2c66affSColin Finck * To reuse this code in another application, you might need to change these.
74c2c66affSColin Finck */
75c2c66affSColin Finck
76c2c66affSColin Finck static FILE * infile; /* input JPEG file */
77c2c66affSColin Finck
78c2c66affSColin Finck /* Return next input byte, or EOF if no more */
79c2c66affSColin Finck #define NEXTBYTE() getc(infile)
80c2c66affSColin Finck
81c2c66affSColin Finck static FILE * outfile; /* output JPEG file */
82c2c66affSColin Finck
83c2c66affSColin Finck /* Emit an output byte */
84c2c66affSColin Finck #define PUTBYTE(x) putc((x), outfile)
85c2c66affSColin Finck
86c2c66affSColin Finck
87c2c66affSColin Finck /* Error exit handler */
88c2c66affSColin Finck #define ERREXIT(msg) (fprintf(stderr, "%s\n", msg), exit(EXIT_FAILURE))
89c2c66affSColin Finck
90c2c66affSColin Finck
91c2c66affSColin Finck /* Read one byte, testing for EOF */
92c2c66affSColin Finck static int
read_1_byte(void)93c2c66affSColin Finck read_1_byte (void)
94c2c66affSColin Finck {
95c2c66affSColin Finck int c;
96c2c66affSColin Finck
97c2c66affSColin Finck c = NEXTBYTE();
98c2c66affSColin Finck if (c == EOF)
99c2c66affSColin Finck ERREXIT("Premature EOF in JPEG file");
100c2c66affSColin Finck return c;
101c2c66affSColin Finck }
102c2c66affSColin Finck
103c2c66affSColin Finck /* Read 2 bytes, convert to unsigned int */
104c2c66affSColin Finck /* All 2-byte quantities in JPEG markers are MSB first */
105c2c66affSColin Finck static unsigned int
read_2_bytes(void)106c2c66affSColin Finck read_2_bytes (void)
107c2c66affSColin Finck {
108c2c66affSColin Finck int c1, c2;
109c2c66affSColin Finck
110c2c66affSColin Finck c1 = NEXTBYTE();
111c2c66affSColin Finck if (c1 == EOF)
112c2c66affSColin Finck ERREXIT("Premature EOF in JPEG file");
113c2c66affSColin Finck c2 = NEXTBYTE();
114c2c66affSColin Finck if (c2 == EOF)
115c2c66affSColin Finck ERREXIT("Premature EOF in JPEG file");
116c2c66affSColin Finck return (((unsigned int) c1) << 8) + ((unsigned int) c2);
117c2c66affSColin Finck }
118c2c66affSColin Finck
119c2c66affSColin Finck
120c2c66affSColin Finck /* Routines to write data to output file */
121c2c66affSColin Finck
122c2c66affSColin Finck static void
write_1_byte(int c)123c2c66affSColin Finck write_1_byte (int c)
124c2c66affSColin Finck {
125c2c66affSColin Finck PUTBYTE(c);
126c2c66affSColin Finck }
127c2c66affSColin Finck
128c2c66affSColin Finck static void
write_2_bytes(unsigned int val)129c2c66affSColin Finck write_2_bytes (unsigned int val)
130c2c66affSColin Finck {
131c2c66affSColin Finck PUTBYTE((val >> 8) & 0xFF);
132c2c66affSColin Finck PUTBYTE(val & 0xFF);
133c2c66affSColin Finck }
134c2c66affSColin Finck
135c2c66affSColin Finck static void
write_marker(int marker)136c2c66affSColin Finck write_marker (int marker)
137c2c66affSColin Finck {
138c2c66affSColin Finck PUTBYTE(0xFF);
139c2c66affSColin Finck PUTBYTE(marker);
140c2c66affSColin Finck }
141c2c66affSColin Finck
142c2c66affSColin Finck static void
copy_rest_of_file(void)143c2c66affSColin Finck copy_rest_of_file (void)
144c2c66affSColin Finck {
145c2c66affSColin Finck int c;
146c2c66affSColin Finck
147c2c66affSColin Finck while ((c = NEXTBYTE()) != EOF)
148c2c66affSColin Finck PUTBYTE(c);
149c2c66affSColin Finck }
150c2c66affSColin Finck
151c2c66affSColin Finck
152c2c66affSColin Finck /*
153c2c66affSColin Finck * JPEG markers consist of one or more 0xFF bytes, followed by a marker
154c2c66affSColin Finck * code byte (which is not an FF). Here are the marker codes of interest
155c2c66affSColin Finck * in this program. (See jdmarker.c for a more complete list.)
156c2c66affSColin Finck */
157c2c66affSColin Finck
158c2c66affSColin Finck #define M_SOF0 0xC0 /* Start Of Frame N */
159c2c66affSColin Finck #define M_SOF1 0xC1 /* N indicates which compression process */
160c2c66affSColin Finck #define M_SOF2 0xC2 /* Only SOF0-SOF2 are now in common use */
161c2c66affSColin Finck #define M_SOF3 0xC3
162c2c66affSColin Finck #define M_SOF5 0xC5 /* NB: codes C4 and CC are NOT SOF markers */
163c2c66affSColin Finck #define M_SOF6 0xC6
164c2c66affSColin Finck #define M_SOF7 0xC7
165c2c66affSColin Finck #define M_SOF9 0xC9
166c2c66affSColin Finck #define M_SOF10 0xCA
167c2c66affSColin Finck #define M_SOF11 0xCB
168c2c66affSColin Finck #define M_SOF13 0xCD
169c2c66affSColin Finck #define M_SOF14 0xCE
170c2c66affSColin Finck #define M_SOF15 0xCF
171c2c66affSColin Finck #define M_SOI 0xD8 /* Start Of Image (beginning of datastream) */
172c2c66affSColin Finck #define M_EOI 0xD9 /* End Of Image (end of datastream) */
173c2c66affSColin Finck #define M_SOS 0xDA /* Start Of Scan (begins compressed data) */
174c2c66affSColin Finck #define M_COM 0xFE /* COMment */
175c2c66affSColin Finck
176c2c66affSColin Finck
177c2c66affSColin Finck /*
178c2c66affSColin Finck * Find the next JPEG marker and return its marker code.
179c2c66affSColin Finck * We expect at least one FF byte, possibly more if the compressor used FFs
180c2c66affSColin Finck * to pad the file. (Padding FFs will NOT be replicated in the output file.)
181c2c66affSColin Finck * There could also be non-FF garbage between markers. The treatment of such
182c2c66affSColin Finck * garbage is unspecified; we choose to skip over it but emit a warning msg.
183c2c66affSColin Finck * NB: this routine must not be used after seeing SOS marker, since it will
184c2c66affSColin Finck * not deal correctly with FF/00 sequences in the compressed image data...
185c2c66affSColin Finck */
186c2c66affSColin Finck
187c2c66affSColin Finck static int
next_marker(void)188c2c66affSColin Finck next_marker (void)
189c2c66affSColin Finck {
190c2c66affSColin Finck int c;
191c2c66affSColin Finck int discarded_bytes = 0;
192c2c66affSColin Finck
193c2c66affSColin Finck /* Find 0xFF byte; count and skip any non-FFs. */
194c2c66affSColin Finck c = read_1_byte();
195c2c66affSColin Finck while (c != 0xFF) {
196c2c66affSColin Finck discarded_bytes++;
197c2c66affSColin Finck c = read_1_byte();
198c2c66affSColin Finck }
199c2c66affSColin Finck /* Get marker code byte, swallowing any duplicate FF bytes. Extra FFs
200c2c66affSColin Finck * are legal as pad bytes, so don't count them in discarded_bytes.
201c2c66affSColin Finck */
202c2c66affSColin Finck do {
203c2c66affSColin Finck c = read_1_byte();
204c2c66affSColin Finck } while (c == 0xFF);
205c2c66affSColin Finck
206c2c66affSColin Finck if (discarded_bytes != 0) {
207c2c66affSColin Finck fprintf(stderr, "Warning: garbage data found in JPEG file\n");
208c2c66affSColin Finck }
209c2c66affSColin Finck
210c2c66affSColin Finck return c;
211c2c66affSColin Finck }
212c2c66affSColin Finck
213c2c66affSColin Finck
214c2c66affSColin Finck /*
215c2c66affSColin Finck * Read the initial marker, which should be SOI.
216c2c66affSColin Finck * For a JFIF file, the first two bytes of the file should be literally
217c2c66affSColin Finck * 0xFF M_SOI. To be more general, we could use next_marker, but if the
218c2c66affSColin Finck * input file weren't actually JPEG at all, next_marker might read the whole
219c2c66affSColin Finck * file and then return a misleading error message...
220c2c66affSColin Finck */
221c2c66affSColin Finck
222c2c66affSColin Finck static int
first_marker(void)223c2c66affSColin Finck first_marker (void)
224c2c66affSColin Finck {
225c2c66affSColin Finck int c1, c2;
226c2c66affSColin Finck
227c2c66affSColin Finck c1 = NEXTBYTE();
228c2c66affSColin Finck c2 = NEXTBYTE();
229c2c66affSColin Finck if (c1 != 0xFF || c2 != M_SOI)
230c2c66affSColin Finck ERREXIT("Not a JPEG file");
231c2c66affSColin Finck return c2;
232c2c66affSColin Finck }
233c2c66affSColin Finck
234c2c66affSColin Finck
235c2c66affSColin Finck /*
236c2c66affSColin Finck * Most types of marker are followed by a variable-length parameter segment.
237c2c66affSColin Finck * This routine skips over the parameters for any marker we don't otherwise
238c2c66affSColin Finck * want to process.
239c2c66affSColin Finck * Note that we MUST skip the parameter segment explicitly in order not to
240c2c66affSColin Finck * be fooled by 0xFF bytes that might appear within the parameter segment;
241c2c66affSColin Finck * such bytes do NOT introduce new markers.
242c2c66affSColin Finck */
243c2c66affSColin Finck
244c2c66affSColin Finck static void
copy_variable(void)245c2c66affSColin Finck copy_variable (void)
246c2c66affSColin Finck /* Copy an unknown or uninteresting variable-length marker */
247c2c66affSColin Finck {
248c2c66affSColin Finck unsigned int length;
249c2c66affSColin Finck
250c2c66affSColin Finck /* Get the marker parameter length count */
251c2c66affSColin Finck length = read_2_bytes();
252c2c66affSColin Finck write_2_bytes(length);
253c2c66affSColin Finck /* Length includes itself, so must be at least 2 */
254c2c66affSColin Finck if (length < 2)
255c2c66affSColin Finck ERREXIT("Erroneous JPEG marker length");
256c2c66affSColin Finck length -= 2;
257*ef4f5757SThomas Faber /* Copy the remaining bytes */
258c2c66affSColin Finck while (length > 0) {
259c2c66affSColin Finck write_1_byte(read_1_byte());
260c2c66affSColin Finck length--;
261c2c66affSColin Finck }
262c2c66affSColin Finck }
263c2c66affSColin Finck
264c2c66affSColin Finck static void
skip_variable(void)265c2c66affSColin Finck skip_variable (void)
266c2c66affSColin Finck /* Skip over an unknown or uninteresting variable-length marker */
267c2c66affSColin Finck {
268c2c66affSColin Finck unsigned int length;
269c2c66affSColin Finck
270c2c66affSColin Finck /* Get the marker parameter length count */
271c2c66affSColin Finck length = read_2_bytes();
272c2c66affSColin Finck /* Length includes itself, so must be at least 2 */
273c2c66affSColin Finck if (length < 2)
274c2c66affSColin Finck ERREXIT("Erroneous JPEG marker length");
275c2c66affSColin Finck length -= 2;
276c2c66affSColin Finck /* Skip over the remaining bytes */
277c2c66affSColin Finck while (length > 0) {
278c2c66affSColin Finck (void) read_1_byte();
279c2c66affSColin Finck length--;
280c2c66affSColin Finck }
281c2c66affSColin Finck }
282c2c66affSColin Finck
283c2c66affSColin Finck
284c2c66affSColin Finck /*
285c2c66affSColin Finck * Parse the marker stream until SOFn or EOI is seen;
286c2c66affSColin Finck * copy data to output, but discard COM markers unless keep_COM is true.
287c2c66affSColin Finck */
288c2c66affSColin Finck
289c2c66affSColin Finck static int
scan_JPEG_header(int keep_COM)290c2c66affSColin Finck scan_JPEG_header (int keep_COM)
291c2c66affSColin Finck {
292c2c66affSColin Finck int marker;
293c2c66affSColin Finck
294c2c66affSColin Finck /* Expect SOI at start of file */
295c2c66affSColin Finck if (first_marker() != M_SOI)
296c2c66affSColin Finck ERREXIT("Expected SOI marker first");
297c2c66affSColin Finck write_marker(M_SOI);
298c2c66affSColin Finck
299c2c66affSColin Finck /* Scan miscellaneous markers until we reach SOFn. */
300c2c66affSColin Finck for (;;) {
301c2c66affSColin Finck marker = next_marker();
302c2c66affSColin Finck switch (marker) {
303c2c66affSColin Finck /* Note that marker codes 0xC4, 0xC8, 0xCC are not, and must not be,
304c2c66affSColin Finck * treated as SOFn. C4 in particular is actually DHT.
305c2c66affSColin Finck */
306c2c66affSColin Finck case M_SOF0: /* Baseline */
307c2c66affSColin Finck case M_SOF1: /* Extended sequential, Huffman */
308c2c66affSColin Finck case M_SOF2: /* Progressive, Huffman */
309c2c66affSColin Finck case M_SOF3: /* Lossless, Huffman */
310c2c66affSColin Finck case M_SOF5: /* Differential sequential, Huffman */
311c2c66affSColin Finck case M_SOF6: /* Differential progressive, Huffman */
312c2c66affSColin Finck case M_SOF7: /* Differential lossless, Huffman */
313c2c66affSColin Finck case M_SOF9: /* Extended sequential, arithmetic */
314c2c66affSColin Finck case M_SOF10: /* Progressive, arithmetic */
315c2c66affSColin Finck case M_SOF11: /* Lossless, arithmetic */
316c2c66affSColin Finck case M_SOF13: /* Differential sequential, arithmetic */
317c2c66affSColin Finck case M_SOF14: /* Differential progressive, arithmetic */
318c2c66affSColin Finck case M_SOF15: /* Differential lossless, arithmetic */
319c2c66affSColin Finck return marker;
320c2c66affSColin Finck
321c2c66affSColin Finck case M_SOS: /* should not see compressed data before SOF */
322c2c66affSColin Finck ERREXIT("SOS without prior SOFn");
323c2c66affSColin Finck break;
324c2c66affSColin Finck
325c2c66affSColin Finck case M_EOI: /* in case it's a tables-only JPEG stream */
326c2c66affSColin Finck return marker;
327c2c66affSColin Finck
328c2c66affSColin Finck case M_COM: /* Existing COM: conditionally discard */
329c2c66affSColin Finck if (keep_COM) {
330c2c66affSColin Finck write_marker(marker);
331c2c66affSColin Finck copy_variable();
332c2c66affSColin Finck } else {
333c2c66affSColin Finck skip_variable();
334c2c66affSColin Finck }
335c2c66affSColin Finck break;
336c2c66affSColin Finck
337c2c66affSColin Finck default: /* Anything else just gets copied */
338c2c66affSColin Finck write_marker(marker);
339c2c66affSColin Finck copy_variable(); /* we assume it has a parameter count... */
340c2c66affSColin Finck break;
341c2c66affSColin Finck }
342c2c66affSColin Finck } /* end loop */
343c2c66affSColin Finck }
344c2c66affSColin Finck
345c2c66affSColin Finck
346c2c66affSColin Finck /* Command line parsing code */
347c2c66affSColin Finck
348c2c66affSColin Finck static const char * progname; /* program name for error messages */
349c2c66affSColin Finck
350c2c66affSColin Finck
351c2c66affSColin Finck static void
usage(void)352c2c66affSColin Finck usage (void)
353c2c66affSColin Finck /* complain about bad command line */
354c2c66affSColin Finck {
355c2c66affSColin Finck fprintf(stderr, "wrjpgcom inserts a textual comment in a JPEG file.\n");
356c2c66affSColin Finck fprintf(stderr, "You can add to or replace any existing comment(s).\n");
357c2c66affSColin Finck
358c2c66affSColin Finck fprintf(stderr, "Usage: %s [switches] ", progname);
359c2c66affSColin Finck #ifdef TWO_FILE_COMMANDLINE
360c2c66affSColin Finck fprintf(stderr, "inputfile outputfile\n");
361c2c66affSColin Finck #else
362c2c66affSColin Finck fprintf(stderr, "[inputfile]\n");
363c2c66affSColin Finck #endif
364c2c66affSColin Finck
365c2c66affSColin Finck fprintf(stderr, "Switches (names may be abbreviated):\n");
366c2c66affSColin Finck fprintf(stderr, " -replace Delete any existing comments\n");
367c2c66affSColin Finck fprintf(stderr, " -comment \"text\" Insert comment with given text\n");
368c2c66affSColin Finck fprintf(stderr, " -cfile name Read comment from named file\n");
369c2c66affSColin Finck fprintf(stderr, "Notice that you must put quotes around the comment text\n");
370c2c66affSColin Finck fprintf(stderr, "when you use -comment.\n");
371c2c66affSColin Finck fprintf(stderr, "If you do not give either -comment or -cfile on the command line,\n");
372c2c66affSColin Finck fprintf(stderr, "then the comment text is read from standard input.\n");
373c2c66affSColin Finck fprintf(stderr, "It can be multiple lines, up to %u characters total.\n",
374c2c66affSColin Finck (unsigned int) MAX_COM_LENGTH);
375c2c66affSColin Finck #ifndef TWO_FILE_COMMANDLINE
376c2c66affSColin Finck fprintf(stderr, "You must specify an input JPEG file name when supplying\n");
377c2c66affSColin Finck fprintf(stderr, "comment text from standard input.\n");
378c2c66affSColin Finck #endif
379c2c66affSColin Finck
380c2c66affSColin Finck exit(EXIT_FAILURE);
381c2c66affSColin Finck }
382c2c66affSColin Finck
383c2c66affSColin Finck
384c2c66affSColin Finck static int
keymatch(char * arg,const char * keyword,int minchars)385c2c66affSColin Finck keymatch (char * arg, const char * keyword, int minchars)
386c2c66affSColin Finck /* Case-insensitive matching of (possibly abbreviated) keyword switches. */
387c2c66affSColin Finck /* keyword is the constant keyword (must be lower case already), */
388c2c66affSColin Finck /* minchars is length of minimum legal abbreviation. */
389c2c66affSColin Finck {
390c2c66affSColin Finck register int ca, ck;
391c2c66affSColin Finck register int nmatched = 0;
392c2c66affSColin Finck
393c2c66affSColin Finck while ((ca = *arg++) != '\0') {
394c2c66affSColin Finck if ((ck = *keyword++) == '\0')
395c2c66affSColin Finck return 0; /* arg longer than keyword, no good */
396c2c66affSColin Finck if (isupper(ca)) /* force arg to lcase (assume ck is already) */
397c2c66affSColin Finck ca = tolower(ca);
398c2c66affSColin Finck if (ca != ck)
399c2c66affSColin Finck return 0; /* no good */
400c2c66affSColin Finck nmatched++; /* count matched characters */
401c2c66affSColin Finck }
402c2c66affSColin Finck /* reached end of argument; fail if it's too short for unique abbrev */
403c2c66affSColin Finck if (nmatched < minchars)
404c2c66affSColin Finck return 0;
405c2c66affSColin Finck return 1; /* A-OK */
406c2c66affSColin Finck }
407c2c66affSColin Finck
408c2c66affSColin Finck
409c2c66affSColin Finck /*
410c2c66affSColin Finck * The main program.
411c2c66affSColin Finck */
412c2c66affSColin Finck
413c2c66affSColin Finck int
main(int argc,char ** argv)414c2c66affSColin Finck main (int argc, char **argv)
415c2c66affSColin Finck {
416c2c66affSColin Finck int argn;
417c2c66affSColin Finck char * arg;
418c2c66affSColin Finck int keep_COM = 1;
419c2c66affSColin Finck char * comment_arg = NULL;
420c2c66affSColin Finck FILE * comment_file = NULL;
421c2c66affSColin Finck unsigned int comment_length = 0;
422c2c66affSColin Finck int marker;
423c2c66affSColin Finck
424c2c66affSColin Finck /* On Mac, fetch a command line. */
425c2c66affSColin Finck #ifdef USE_CCOMMAND
426c2c66affSColin Finck argc = ccommand(&argv);
427c2c66affSColin Finck #endif
428c2c66affSColin Finck
429c2c66affSColin Finck progname = argv[0];
430c2c66affSColin Finck if (progname == NULL || progname[0] == 0)
431c2c66affSColin Finck progname = "wrjpgcom"; /* in case C library doesn't provide it */
432c2c66affSColin Finck
433c2c66affSColin Finck /* Parse switches, if any */
434c2c66affSColin Finck for (argn = 1; argn < argc; argn++) {
435c2c66affSColin Finck arg = argv[argn];
436c2c66affSColin Finck if (arg[0] != '-')
437c2c66affSColin Finck break; /* not switch, must be file name */
438c2c66affSColin Finck arg++; /* advance over '-' */
439c2c66affSColin Finck if (keymatch(arg, "replace", 1)) {
440c2c66affSColin Finck keep_COM = 0;
441c2c66affSColin Finck } else if (keymatch(arg, "cfile", 2)) {
442c2c66affSColin Finck if (++argn >= argc) usage();
443c2c66affSColin Finck if ((comment_file = fopen(argv[argn], "r")) == NULL) {
444c2c66affSColin Finck fprintf(stderr, "%s: can't open %s\n", progname, argv[argn]);
445c2c66affSColin Finck exit(EXIT_FAILURE);
446c2c66affSColin Finck }
447c2c66affSColin Finck } else if (keymatch(arg, "comment", 1)) {
448c2c66affSColin Finck if (++argn >= argc) usage();
449c2c66affSColin Finck comment_arg = argv[argn];
450c2c66affSColin Finck /* If the comment text starts with '"', then we are probably running
451c2c66affSColin Finck * under MS-DOG and must parse out the quoted string ourselves. Sigh.
452c2c66affSColin Finck */
453c2c66affSColin Finck if (comment_arg[0] == '"') {
454c2c66affSColin Finck comment_arg = (char *) malloc((size_t) MAX_COM_LENGTH);
455c2c66affSColin Finck if (comment_arg == NULL)
456c2c66affSColin Finck ERREXIT("Insufficient memory");
457c2c66affSColin Finck if (strlen(argv[argn]+1) >= (size_t) MAX_COM_LENGTH) {
458c2c66affSColin Finck fprintf(stderr, "Comment text may not exceed %u bytes\n",
459c2c66affSColin Finck (unsigned int) MAX_COM_LENGTH);
460c2c66affSColin Finck exit(EXIT_FAILURE);
461c2c66affSColin Finck }
462c2c66affSColin Finck strcpy(comment_arg, argv[argn]+1);
463c2c66affSColin Finck for (;;) {
464c2c66affSColin Finck comment_length = (unsigned int) strlen(comment_arg);
465c2c66affSColin Finck if (comment_length > 0 && comment_arg[comment_length-1] == '"') {
466c2c66affSColin Finck comment_arg[comment_length-1] = '\0'; /* zap terminating quote */
467c2c66affSColin Finck break;
468c2c66affSColin Finck }
469c2c66affSColin Finck if (++argn >= argc)
470c2c66affSColin Finck ERREXIT("Missing ending quote mark");
471c2c66affSColin Finck if (strlen(comment_arg) + 1 + strlen(argv[argn]) >=
472c2c66affSColin Finck (size_t) MAX_COM_LENGTH) {
473c2c66affSColin Finck fprintf(stderr, "Comment text may not exceed %u bytes\n",
474c2c66affSColin Finck (unsigned int) MAX_COM_LENGTH);
475c2c66affSColin Finck exit(EXIT_FAILURE);
476c2c66affSColin Finck }
477c2c66affSColin Finck strcat(comment_arg, " ");
478c2c66affSColin Finck strcat(comment_arg, argv[argn]);
479c2c66affSColin Finck }
480c2c66affSColin Finck } else if (strlen(comment_arg) >= (size_t) MAX_COM_LENGTH) {
481c2c66affSColin Finck fprintf(stderr, "Comment text may not exceed %u bytes\n",
482c2c66affSColin Finck (unsigned int) MAX_COM_LENGTH);
483c2c66affSColin Finck exit(EXIT_FAILURE);
484c2c66affSColin Finck }
485c2c66affSColin Finck comment_length = (unsigned int) strlen(comment_arg);
486c2c66affSColin Finck } else
487c2c66affSColin Finck usage();
488c2c66affSColin Finck }
489c2c66affSColin Finck
490c2c66affSColin Finck /* Cannot use both -comment and -cfile. */
491c2c66affSColin Finck if (comment_arg != NULL && comment_file != NULL)
492c2c66affSColin Finck usage();
493c2c66affSColin Finck /* If there is neither -comment nor -cfile, we will read the comment text
494c2c66affSColin Finck * from stdin; in this case there MUST be an input JPEG file name.
495c2c66affSColin Finck */
496c2c66affSColin Finck if (comment_arg == NULL && comment_file == NULL && argn >= argc)
497c2c66affSColin Finck usage();
498c2c66affSColin Finck
499c2c66affSColin Finck /* Open the input file. */
500c2c66affSColin Finck if (argn < argc) {
501c2c66affSColin Finck if ((infile = fopen(argv[argn], READ_BINARY)) == NULL) {
502c2c66affSColin Finck fprintf(stderr, "%s: can't open %s\n", progname, argv[argn]);
503c2c66affSColin Finck exit(EXIT_FAILURE);
504c2c66affSColin Finck }
505c2c66affSColin Finck } else {
506c2c66affSColin Finck /* default input file is stdin */
507c2c66affSColin Finck #ifdef USE_SETMODE /* need to hack file mode? */
508c2c66affSColin Finck setmode(fileno(stdin), O_BINARY);
509c2c66affSColin Finck #endif
510c2c66affSColin Finck #ifdef USE_FDOPEN /* need to re-open in binary mode? */
511c2c66affSColin Finck if ((infile = fdopen(fileno(stdin), READ_BINARY)) == NULL) {
512c2c66affSColin Finck fprintf(stderr, "%s: can't open stdin\n", progname);
513c2c66affSColin Finck exit(EXIT_FAILURE);
514c2c66affSColin Finck }
515c2c66affSColin Finck #else
516c2c66affSColin Finck infile = stdin;
517c2c66affSColin Finck #endif
518c2c66affSColin Finck }
519c2c66affSColin Finck
520c2c66affSColin Finck /* Open the output file. */
521c2c66affSColin Finck #ifdef TWO_FILE_COMMANDLINE
522c2c66affSColin Finck /* Must have explicit output file name */
523c2c66affSColin Finck if (argn != argc-2) {
524c2c66affSColin Finck fprintf(stderr, "%s: must name one input and one output file\n",
525c2c66affSColin Finck progname);
526c2c66affSColin Finck usage();
527c2c66affSColin Finck }
528c2c66affSColin Finck if ((outfile = fopen(argv[argn+1], WRITE_BINARY)) == NULL) {
529c2c66affSColin Finck fprintf(stderr, "%s: can't open %s\n", progname, argv[argn+1]);
530c2c66affSColin Finck exit(EXIT_FAILURE);
531c2c66affSColin Finck }
532c2c66affSColin Finck #else
533c2c66affSColin Finck /* Unix style: expect zero or one file name */
534c2c66affSColin Finck if (argn < argc-1) {
535c2c66affSColin Finck fprintf(stderr, "%s: only one input file\n", progname);
536c2c66affSColin Finck usage();
537c2c66affSColin Finck }
538c2c66affSColin Finck /* default output file is stdout */
539c2c66affSColin Finck #ifdef USE_SETMODE /* need to hack file mode? */
540c2c66affSColin Finck setmode(fileno(stdout), O_BINARY);
541c2c66affSColin Finck #endif
542c2c66affSColin Finck #ifdef USE_FDOPEN /* need to re-open in binary mode? */
543c2c66affSColin Finck if ((outfile = fdopen(fileno(stdout), WRITE_BINARY)) == NULL) {
544c2c66affSColin Finck fprintf(stderr, "%s: can't open stdout\n", progname);
545c2c66affSColin Finck exit(EXIT_FAILURE);
546c2c66affSColin Finck }
547c2c66affSColin Finck #else
548c2c66affSColin Finck outfile = stdout;
549c2c66affSColin Finck #endif
550c2c66affSColin Finck #endif /* TWO_FILE_COMMANDLINE */
551c2c66affSColin Finck
552c2c66affSColin Finck /* Collect comment text from comment_file or stdin, if necessary */
553c2c66affSColin Finck if (comment_arg == NULL) {
554c2c66affSColin Finck FILE * src_file;
555c2c66affSColin Finck int c;
556c2c66affSColin Finck
557c2c66affSColin Finck comment_arg = (char *) malloc((size_t) MAX_COM_LENGTH);
558c2c66affSColin Finck if (comment_arg == NULL)
559c2c66affSColin Finck ERREXIT("Insufficient memory");
560c2c66affSColin Finck comment_length = 0;
561c2c66affSColin Finck src_file = (comment_file != NULL ? comment_file : stdin);
562c2c66affSColin Finck while ((c = getc(src_file)) != EOF) {
563c2c66affSColin Finck if (comment_length >= (unsigned int) MAX_COM_LENGTH) {
564c2c66affSColin Finck fprintf(stderr, "Comment text may not exceed %u bytes\n",
565c2c66affSColin Finck (unsigned int) MAX_COM_LENGTH);
566c2c66affSColin Finck exit(EXIT_FAILURE);
567c2c66affSColin Finck }
568c2c66affSColin Finck comment_arg[comment_length++] = (char) c;
569c2c66affSColin Finck }
570c2c66affSColin Finck if (comment_file != NULL)
571c2c66affSColin Finck fclose(comment_file);
572c2c66affSColin Finck }
573c2c66affSColin Finck
574c2c66affSColin Finck /* Copy JPEG headers until SOFn marker;
575c2c66affSColin Finck * we will insert the new comment marker just before SOFn.
576c2c66affSColin Finck * This (a) causes the new comment to appear after, rather than before,
577c2c66affSColin Finck * existing comments; and (b) ensures that comments come after any JFIF
578c2c66affSColin Finck * or JFXX markers, as required by the JFIF specification.
579c2c66affSColin Finck */
580c2c66affSColin Finck marker = scan_JPEG_header(keep_COM);
581c2c66affSColin Finck /* Insert the new COM marker, but only if nonempty text has been supplied */
582c2c66affSColin Finck if (comment_length > 0) {
583c2c66affSColin Finck write_marker(M_COM);
584c2c66affSColin Finck write_2_bytes(comment_length + 2);
585c2c66affSColin Finck while (comment_length > 0) {
586c2c66affSColin Finck write_1_byte(*comment_arg++);
587c2c66affSColin Finck comment_length--;
588c2c66affSColin Finck }
589c2c66affSColin Finck }
590c2c66affSColin Finck /* Duplicate the remainder of the source file.
591c2c66affSColin Finck * Note that any COM markers occuring after SOF will not be touched.
592c2c66affSColin Finck */
593c2c66affSColin Finck write_marker(marker);
594c2c66affSColin Finck copy_rest_of_file();
595c2c66affSColin Finck
596c2c66affSColin Finck /* All done. */
597c2c66affSColin Finck exit(EXIT_SUCCESS);
598c2c66affSColin Finck return 0; /* suppress no-return-value warnings */
599c2c66affSColin Finck }
600