Add -raw option to rdjpgcom for outputting non-printable characters as-is; Make rdjpgcom locale-aware.

git-svn-id: svn+ssh://svn.code.sf.net/p/libjpeg-turbo/code/trunk@246 632fc199-4ca6-4c93-a231-07263d6284db
This commit is contained in:
DRC
2010-10-10 06:04:05 +00:00
2 changed files with 36 additions and 8 deletions

View File

@@ -1,9 +1,12 @@
.TH RDJPGCOM 1 "11 October 1997" .TH RDJPGCOM 1 "02 April 2009"
.SH NAME .SH NAME
rdjpgcom \- display text comments from a JPEG file rdjpgcom \- display text comments from a JPEG file
.SH SYNOPSIS .SH SYNOPSIS
.B rdjpgcom .B rdjpgcom
[ [
.B \-raw
]
[
.B \-verbose .B \-verbose
] ]
[ [
@@ -25,6 +28,12 @@ file. The maximum size of a COM block is 64K, but you can have as many of
them as you like in one JPEG file. them as you like in one JPEG file.
.SH OPTIONS .SH OPTIONS
.TP .TP
.B \-raw
Normally
.B rdjpgcom
escapes non-printable characters in comments, for security reasons.
This option avoids that.
.PP
.B \-verbose .B \-verbose
Causes Causes
.B rdjpgcom .B rdjpgcom

View File

@@ -2,6 +2,7 @@
* rdjpgcom.c * rdjpgcom.c
* *
* Copyright (C) 1994-1997, Thomas G. Lane. * Copyright (C) 1994-1997, Thomas G. Lane.
* Modified 2009 by Bill Allombert, Guido Vollbeding.
* This file is part of the Independent JPEG Group's software. * This file is part of the Independent JPEG Group's software.
* For conditions of distribution and use, see the accompanying README file. * For conditions of distribution and use, see the accompanying README file.
* *
@@ -14,6 +15,9 @@
#define JPEG_CJPEG_DJPEG /* to get the command-line config symbols */ #define JPEG_CJPEG_DJPEG /* to get the command-line config symbols */
#include "jinclude.h" /* get auto-config symbols, <stdio.h> */ #include "jinclude.h" /* get auto-config symbols, <stdio.h> */
#ifdef HAVE_LOCALE_H
#include <locale.h> /* Bill Allombert: use locale for isprint */
#endif
#include <ctype.h> /* to declare isupper(), tolower() */ #include <ctype.h> /* to declare isupper(), tolower() */
#ifdef USE_SETMODE #ifdef USE_SETMODE
#include <fcntl.h> /* to declare setmode()'s parameter macros */ #include <fcntl.h> /* to declare setmode()'s parameter macros */
@@ -218,12 +222,17 @@ skip_variable (void)
*/ */
static void static void
process_COM (void) process_COM (int raw)
{ {
unsigned int length; unsigned int length;
int ch; int ch;
int lastch = 0; int lastch = 0;
/* Bill Allombert: set locale properly for isprint */
#ifdef HAVE_LOCALE_H
setlocale(LC_CTYPE, "");
#endif
/* Get the marker parameter length count */ /* Get the marker parameter length count */
length = read_2_bytes(); length = read_2_bytes();
/* Length includes itself, so must be at least 2 */ /* Length includes itself, so must be at least 2 */
@@ -233,12 +242,14 @@ process_COM (void)
while (length > 0) { while (length > 0) {
ch = read_1_byte(); ch = read_1_byte();
if (raw) {
putc(ch, stdout);
/* Emit the character in a readable form. /* Emit the character in a readable form.
* Nonprintables are converted to \nnn form, * Nonprintables are converted to \nnn form,
* while \ is converted to \\. * while \ is converted to \\.
* Newlines in CR, CR/LF, or LF form will be printed as one newline. * Newlines in CR, CR/LF, or LF form will be printed as one newline.
*/ */
if (ch == '\r') { } else if (ch == '\r') {
printf("\n"); printf("\n");
} else if (ch == '\n') { } else if (ch == '\n') {
if (lastch != '\r') if (lastch != '\r')
@@ -254,6 +265,11 @@ process_COM (void)
length--; length--;
} }
printf("\n"); printf("\n");
/* Bill Allombert: revert to C locale */
#ifdef HAVE_LOCALE_H
setlocale(LC_CTYPE, "C");
#endif
} }
@@ -321,7 +337,7 @@ process_SOFn (int marker)
*/ */
static int static int
scan_JPEG_header (int verbose) scan_JPEG_header (int verbose, int raw)
{ {
int marker; int marker;
@@ -362,7 +378,7 @@ scan_JPEG_header (int verbose)
return marker; return marker;
case M_COM: case M_COM:
process_COM(); process_COM(raw);
break; break;
case M_APP12: case M_APP12:
@@ -371,7 +387,7 @@ scan_JPEG_header (int verbose)
*/ */
if (verbose) { if (verbose) {
printf("APP12 contains:\n"); printf("APP12 contains:\n");
process_COM(); process_COM(raw);
} else } else
skip_variable(); skip_variable();
break; break;
@@ -398,6 +414,7 @@ usage (void)
fprintf(stderr, "Usage: %s [switches] [inputfile]\n", progname); fprintf(stderr, "Usage: %s [switches] [inputfile]\n", progname);
fprintf(stderr, "Switches (names may be abbreviated):\n"); fprintf(stderr, "Switches (names may be abbreviated):\n");
fprintf(stderr, " -raw Display non-printable characters in comments (unsafe)\n");
fprintf(stderr, " -verbose Also display dimensions of JPEG image\n"); fprintf(stderr, " -verbose Also display dimensions of JPEG image\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@@ -438,7 +455,7 @@ main (int argc, char **argv)
{ {
int argn; int argn;
char * arg; char * arg;
int verbose = 0; int verbose = 0, raw = 0;
/* On Mac, fetch a command line. */ /* On Mac, fetch a command line. */
#ifdef USE_CCOMMAND #ifdef USE_CCOMMAND
@@ -457,6 +474,8 @@ main (int argc, char **argv)
arg++; /* advance over '-' */ arg++; /* advance over '-' */
if (keymatch(arg, "verbose", 1)) { if (keymatch(arg, "verbose", 1)) {
verbose++; verbose++;
} else if (keymatch(arg, "raw", 1)) {
raw = 1;
} else } else
usage(); usage();
} }
@@ -488,7 +507,7 @@ main (int argc, char **argv)
} }
/* Scan the JPEG headers. */ /* Scan the JPEG headers. */
(void) scan_JPEG_header(verbose); (void) scan_JPEG_header(verbose, raw);
/* All done. */ /* All done. */
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);