Skip to content
Browse files

Gratuitous run of indent(1)

  • Loading branch information...
1 parent c620813 commit 27e8757c9f06e3f052dfbbda1104af4794a912b6 @samueltardieu committed Mar 4, 2010
Showing with 112 additions and 106 deletions.
  1. +1 −0 .indent.pro
  2. +96 −94 recoverjpeg.c
  3. +7 −6 utils.c
  4. +8 −6 utils.h
View
1 .indent.pro
@@ -0,0 +1 @@
+-npcs -br
View
190 recoverjpeg.c
@@ -26,151 +26,152 @@
static int verbose = 0;
static int quiet = 0;
-static size_t max_size = 6*1024*1024;
+static size_t max_size = 6 * 1024 * 1024;
static void
-usage (int clean_exit)
+usage(int clean_exit)
{
- fprintf (stderr, "Usage: recoverjpeg [options] file|device\n");
- fprintf (stderr, "Options:\n");
- fprintf (stderr, " -b blocksize Block size in bytes "
- "(default: 512)\n");
- fprintf (stderr, " -f format Format string in printf syntax\n");
- fprintf (stderr, " -h This help message\n");
- fprintf (stderr, " -i index Initial picture index\n");
- fprintf (stderr, " -m maxsize Max jpeg file size in bytes "
- "(default: 6m)\n");
- fprintf (stderr, " -q Be quiet\n");
- fprintf (stderr, " -r readsize Size of disk reads in bytes "
- "(default: 128m)\n");
- fprintf (stderr, " -v verbose Replace progress bar by details\n");
- fprintf (stderr, " -V Display version and exit\n");
- exit (clean_exit ? 0 : 1);
+ fprintf(stderr, "Usage: recoverjpeg [options] file|device\n");
+ fprintf(stderr, "Options:\n");
+ fprintf(stderr, " -b blocksize Block size in bytes "
+ "(default: 512)\n");
+ fprintf(stderr, " -f format Format string in printf syntax\n");
+ fprintf(stderr, " -h This help message\n");
+ fprintf(stderr, " -i index Initial picture index\n");
+ fprintf(stderr, " -m maxsize Max jpeg file size in bytes "
+ "(default: 6m)\n");
+ fprintf(stderr, " -q Be quiet\n");
+ fprintf(stderr, " -r readsize Size of disk reads in bytes "
+ "(default: 128m)\n");
+ fprintf(stderr, " -v verbose Replace progress bar by details\n");
+ fprintf(stderr, " -V Display version and exit\n");
+ exit(clean_exit ? 0 : 1);
}
static inline int
-progressbar ()
+progressbar()
{
return !(quiet || verbose);
}
static void
-display_progressbar (off_t offset, unsigned int n)
+display_progressbar(off_t offset, unsigned int n)
{
off_t to_display;
static unsigned int old_n = -1;
static int gib_mode = 0;
static off_t old_to_display = 0.0;
- if (offset < 1024*1024*1024) {
+ if (offset < 1024 * 1024 * 1024) {
to_display = offset / 1024 * 10 / 1024;
- } else {
+ }
+ else {
gib_mode = 1;
- to_display = offset / (1024*1024) * 10 / 1024;
+ to_display = offset / (1024 * 1024) * 10 / 1024;
}
if (n != old_n || to_display != old_to_display) {
- printf ("\rRecovered files: %4u Analyzed: %4.1f %s ",
- n, to_display / 10.0, gib_mode ? "GiB" : "MiB");
- fflush (stdout);
+ printf("\rRecovered files: %4u Analyzed: %4.1f %s ",
+ n, to_display / 10.0, gib_mode ? "GiB" : "MiB");
+ fflush(stdout);
old_n = n;
old_to_display = to_display;
}
}
static void
-cleanup_progressbar ()
+cleanup_progressbar()
{
- printf ("\r \r");
+ printf("\r \r");
}
static size_t
-jpeg_size (unsigned char *start)
+jpeg_size(unsigned char *start)
{
unsigned char *addr;
unsigned char code;
size_t size;
- if (*start != 0xff || *(start+1) != 0xd8) {
+ if (*start != 0xff || *(start + 1) != 0xd8) {
return 0;
}
if (verbose) {
- fprintf (stderr, "Candidate jpeg found\n");
+ fprintf(stderr, "Candidate jpeg found\n");
}
for (addr = start + 2;;) {
if (*addr != 0xff) {
if (verbose) {
- fprintf (stderr,
- " Incorrect marker %02x, stopping prematurely\n", *addr);
+ fprintf(stderr,
+ " Incorrect marker %02x, stopping prematurely\n", *addr);
}
return 0;
}
- code = *(addr+1);
+ code = *(addr + 1);
addr += 2;
if (code == 0xd9) {
if (verbose) {
- fprintf (stderr,
- " Found end of image after %ld bytes\n", (long)(addr-start+1));
+ fprintf(stderr,
+ " Found end of image after %ld bytes\n",
+ (long) (addr - start + 1));
}
- return addr-start;
+ return addr - start;
}
if (code == 0x01 || code == 0xff) {
if (verbose) {
- fprintf (stderr,
- " Found lengthless section %02x\n", code);
+ fprintf(stderr, " Found lengthless section %02x\n", code);
}
continue;
}
- size = (*addr << 8) + *(addr+1);
+ size = (*addr << 8) + *(addr + 1);
addr += size;
if (verbose) {
- fprintf (stderr, " Found section %02x of len %ld\n", code, (long)size);
+ fprintf(stderr, " Found section %02x of len %ld\n", code,
+ (long) size);
}
if (size < 2 || size > max_size) {
if (verbose) {
- fprintf (stderr, " Section size is out of bounds, aborting\n");
+ fprintf(stderr, " Section size is out of bounds, aborting\n");
}
return 0;
}
if (code == 0xda) {
if (verbose) {
- fprintf (stderr, " Looking for end marker... ");
- fflush (stderr);
+ fprintf(stderr, " Looking for end marker... ");
+ fflush(stderr);
}
- for (;
- (size_t) (addr-start) < max_size &&
- (*addr != 0xff ||
- *(addr+1) == 0 || /* Escape */
- (*(addr + 1) >= 0xd0 && *(addr + 1) <= 0xd7) /* RSTn */);
+ for (; (size_t) (addr - start) < max_size && (*addr != 0xff || *(addr + 1) == 0 || /* Escape */
+ (*(addr + 1) >= 0xd0
+ && *(addr + 1) <=
+ 0xd7) /* RSTn */ );
addr++);
if ((size_t) (addr - start) >= max_size) {
if (verbose) {
- fprintf (stderr, "too big, aborting\n");
+ fprintf(stderr, "too big, aborting\n");
}
return 0;
}
if (verbose) {
- fprintf (stderr, "found at offset %ld\n", (long)(addr - start));
+ fprintf(stderr, "found at offset %ld\n", (long) (addr - start));
}
}
}
}
int
-main (int argc, char *argv[])
+main(int argc, char *argv[])
{
int fd, fdout;
size_t read_size, block_size;
@@ -183,80 +184,79 @@ main (int argc, char *argv[])
char *format;
int c;
- read_size = 128*1024*1024;
+ read_size = 128 * 1024 * 1024;
block_size = 512;
begin_index = 0;
format = "image%05d.jpg";
- while ((c = getopt (argc, argv, "b:f:hi:m:qr:vV")) != -1) {
+ while ((c = getopt(argc, argv, "b:f:hi:m:qr:vV")) != -1) {
switch (c) {
case 'b':
- block_size = atol_suffix (optarg);
+ block_size = atol_suffix(optarg);
break;
case 'f':
format = optarg;
break;
case 'i':
- begin_index = atoi (optarg);
+ begin_index = atoi(optarg);
break;
case 'm':
- max_size = atol_suffix (optarg);
+ max_size = atol_suffix(optarg);
break;
case 'q':
quiet = 1;
break;
case 'r':
- read_size = atol_suffix (optarg);
+ read_size = atol_suffix(optarg);
break;
case 'v':
verbose = 1;
break;
case 'V':
display_version_and_exit("recoverjpeg");
default:
- usage (c == 'h');
+ usage(c == 'h');
}
}
argc -= optind;
argv += optind;
if (argc != 1) {
- usage (0);
+ usage(0);
}
- fd = open (argv[0], O_RDONLY);
+ fd = open(argv[0], O_RDONLY);
if (fd < 0) {
- fprintf (stderr,
- "recoverjpeg: unable to open %s for reading (%s)\n",
- argv[argc-1], strerror (errno));
- exit (1);
+ fprintf(stderr,
+ "recoverjpeg: unable to open %s for reading (%s)\n",
+ argv[argc - 1], strerror(errno));
+ exit(1);
}
- page_size = getpagesize ();
+ page_size = getpagesize();
if (read_size % page_size || read_size < max_size) {
if (read_size < max_size) {
read_size = max_size;
}
read_size = (read_size + page_size - 1) / page_size * page_size;
if (!quiet) {
- fprintf (stderr, "Adjusted read size to %ld bytes\n",
- (long)read_size);
+ fprintf(stderr, "Adjusted read size to %ld bytes\n", (long) read_size);
}
}
- start = end = (unsigned char *) malloc (read_size);
+ start = end = (unsigned char *) malloc(read_size);
if (start == 0) {
- fprintf (stderr,
- "recoverjpeg: cannot allocate necessary memory (%s)\n",
- strerror (errno));
- exit (1);
+ fprintf(stderr,
+ "recoverjpeg: cannot allocate necessary memory (%s)\n",
+ strerror(errno));
+ exit(1);
}
for (i = 0, offset = 0, addr = NULL; addr < end;) {
- if (progressbar ()) {
- display_progressbar (offset, i);
+ if (progressbar()) {
+ display_progressbar(offset, i);
}
if (addr == NULL || (size_t) (start + read_size - addr) < max_size) {
@@ -265,56 +265,58 @@ main (int argc, char *argv[])
base_offset = offset / page_size * page_size;
- lseek (fd, base_offset, SEEK_SET);
- n = read (fd, start, read_size);
+ lseek(fd, base_offset, SEEK_SET);
+ n = read(fd, start, read_size);
if (n < 0) {
- fprintf (stderr, "recoverjpeg: unable to read data (%s)\n",
- strerror (errno));
- exit (1);
+ fprintf(stderr, "recoverjpeg: unable to read data (%s)\n",
+ strerror(errno));
+ exit(1);
}
end = start + n;
addr = start + (offset - base_offset);
}
- size = jpeg_size (addr);
+ size = jpeg_size(addr);
if (size > 0) {
size_t n;
- snprintf (buffer, sizeof buffer, format, begin_index + i);
+ snprintf(buffer, sizeof buffer, format, begin_index + i);
i++;
if (verbose) {
- printf ("%s %ld bytes\n", buffer, (long)size);
+ printf("%s %ld bytes\n", buffer, (long) size);
}
- fdout = open (buffer, O_WRONLY | O_CREAT, 0666);
+ fdout = open(buffer, O_WRONLY | O_CREAT, 0666);
if (fdout < 0) {
- fprintf (stderr, "Unable to open %s for writing\n", buffer);
- exit (1);
+ fprintf(stderr, "Unable to open %s for writing\n", buffer);
+ exit(1);
}
- if ((size_t) write (fdout, addr, size) != size) {
- fprintf (stderr, "Unable to write %ld bytes to %s\n", (long)size, buffer);
- exit (1);
+ if ((size_t) write(fdout, addr, size) != size) {
+ fprintf(stderr, "Unable to write %ld bytes to %s\n",
+ (long) size, buffer);
+ exit(1);
}
- close (fdout);
+ close(fdout);
n = ((size + block_size - 1) / block_size) * block_size;
addr += n;
offset += n;
- } else {
+ }
+ else {
addr += block_size;
offset += block_size;
}
}
- if (progressbar ()) {
- cleanup_progressbar ();
+ if (progressbar()) {
+ cleanup_progressbar();
}
if (!quiet) {
- printf ("Restored %d picture%s\n", i, i > 1 ? "s" : "");
+ printf("Restored %d picture%s\n", i, i > 1 ? "s" : "");
}
/* Free allocated memory to keep valgrind happy */
free(start);
- exit (0);
+ exit(0);
}
View
13 utils.c
@@ -16,18 +16,18 @@
#include "utils.h"
size_t
-atol_suffix (char *arg)
+atol_suffix(char *arg)
{
long multiplier = 1;
- switch (arg[strlen(arg)-1]) {
+ switch (arg[strlen(arg) - 1]) {
case 'g':
case 'G':
- multiplier = 1024*1024*1024;
+ multiplier = 1024 * 1024 * 1024;
break;
case 'm':
case 'M':
- multiplier = 1024*1024;
+ multiplier = 1024 * 1024;
break;
case 'k':
case 'K':
@@ -36,13 +36,14 @@ atol_suffix (char *arg)
}
if (multiplier != 1) {
- arg[strlen(arg)-1] = '\0';
+ arg[strlen(arg) - 1] = '\0';
}
return atol(arg) * multiplier;
}
-void display_version_and_exit(const char *program_name)
+void
+display_version_and_exit(const char *program_name)
{
printf("%s %s (from the `%s' package)\n", program_name, VERSION, PACKAGE);
exit(0);
View
14 utils.h
@@ -15,15 +15,17 @@
#include <sys/types.h>
#ifdef __cplusplus
-extern "C" {
-#endif // __cplusplus
+extern "C"
+{
+#endif // __cplusplus
-size_t atol_suffix(char *arg);
+ size_t atol_suffix(char *arg);
-void display_version_and_exit(const char *program_name) __attribute__((noreturn));
+ void display_version_and_exit(const char *program_name)
+ __attribute__ ((noreturn));
#ifdef __cplusplus
}
-#endif // __cplusplus
+#endif // __cplusplus
-#endif // _UTILS_H
+#endif // _UTILS_H

0 comments on commit 27e8757

Please sign in to comment.
Something went wrong with that request. Please try again.