Index: images/fl_jpeg.cxx
===================================================================
--- images/fl_jpeg.cxx	(revision 7677)
+++ images/fl_jpeg.cxx	(working copy)
@@ -41,15 +41,18 @@
 #include <setjmp.h>
 #include <string.h>
 
+#define xfprintf(...)
+
 typedef struct {
   struct jpeg_source_mgr pub;	/* public fields */
 
   JOCTET * buffer;		/* start of buffer */
 
   JOCTET * start_datas, * datas;
+  unsigned length;
 } my_source_mgr;
 
-static int INPUT_BUF_SIZE;
+static unsigned INPUT_BUF_SIZE;
 
 typedef my_source_mgr * my_src_ptr;
 
@@ -58,16 +61,82 @@
  * before any data is actually read.
  */
 
+/* 0 default, 1 none, 2 incr. by 2, -1 increment variable length, 9 eoi */
+static int jpeg_parse_tab[] = {
+/*             0   1   2   3   4   5   6   7   8   9   a   b   c   d   e   f */
+    /* 0 */    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
+    /* 1 */    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
+    /* 2 */    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
+    /* 3 */    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
+    /* 4 */    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
+    /* 5 */    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
+    /* 6 */    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
+    /* 7 */    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
+    /* 8 */    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
+    /* 9 */    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
+    /* a */    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
+    /* b */    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
+    /* c */   -1,  0, -1,  0, -1,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
+    /* d */    1,  1,  1,  1,  1,  1,  1,  1,  1,  9, -1, -1,  0,  2,  0,  0,
+    /* e */   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+    /* f */    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, -1,  0,
+};
+
+
 METHODDEF(void)
 init_source (j_decompress_ptr cinfo)
 {
   my_src_ptr src = (my_src_ptr) cinfo->src;
 
+  xfprintf(stderr,"init source %x\n", unsigned(src->start_datas));
+
   /* We reset the empty-input-file flag for each image,
    * but we don't clear the input buffer.
    * This is correct behavior for reading a series of images from one source.
    */
   src->datas = src->start_datas;
+
+  /* initialize length otherwise there are problems:
+     - memcpy might overlap (may be that's not so problematic
+     - we might access data out of boundaries
+
+     it's not so simple - we need to parse the stuff, as there are EOIs
+     in between
+  */
+  unsigned char *block = src->start_datas;
+  src->length = 0;
+
+  for ( unsigned i = 0; src->length == 0; i++ ) 
+  {
+      if ( block[i] == 0xff ) 
+      {
+	  switch ( jpeg_parse_tab[block[i+1]] ) 
+	  {
+	  case -1:
+	      xfprintf(stderr,"%x ff%x -1 ",i, block[i+1]);
+	      i += 1 + block[i+2] * 256 + block[i+3];
+	      break;
+	  case 1:
+	      xfprintf(stderr,"%x ff%x  1 ",i, block[i+1]);
+	      i += 1;
+	      break;
+	  case 2:
+	      xfprintf(stderr,"%x ff%x  2 ",i, block[i+1]);
+	      i += 3;
+	      break;
+	  case 9:
+	      xfprintf(stderr,"%x ff%x  9 ",i, block[i+1]);
+	      src->length = i + 2;
+	      break;
+	  default:
+	      xfprintf(stderr,"%x ff%x  0 ",i, block[i+1]);
+	      break;
+	  }
+	  xfprintf(stderr,"%x\n",i);
+      }
+  }
+
+  xfprintf(stderr,"jpeg size: %d\n", src->length);
 }
 
 
@@ -108,13 +177,35 @@
 fill_input_buffer (j_decompress_ptr cinfo)
 {
   my_src_ptr src = (my_src_ptr) cinfo->src;
-  unsigned nbytes = INPUT_BUF_SIZE;
+  unsigned nbytes = INPUT_BUF_SIZE > src->length ? src->length : INPUT_BUF_SIZE;
 
-  memcpy(src->buffer, src->datas, INPUT_BUF_SIZE);
-  src->datas += INPUT_BUF_SIZE;
+  /* do some checks */
 
+  xfprintf(stderr,"fill_input_buffer %x\n", unsigned(src->datas));
+
+/*
+  a < b && a + buf > b 
+*/
+
+  if (src->buffer < src->datas && src->buffer + nbytes > src->datas) {
+      fprintf(stderr,"----  overlap a by %x bytes\n", nbytes + src->buffer - src->datas);
+  }
+
+  if (src->datas < src->buffer && src->datas + nbytes > src->buffer) {
+      fprintf(stderr,"----  overlap b by %x bytes\n", nbytes + src->buffer - src->datas);
+  }
+
+
+  xfprintf(stderr,"memcpy(%x, %x, %d) at stack %x\n",
+	  unsigned(src->buffer), unsigned(src->datas), 
+	  nbytes, unsigned(&nbytes));
+
+  memmove(src->buffer, src->datas, nbytes);
+  src->datas += nbytes;
+
   src->pub.next_input_byte = src->buffer;
   src->pub.bytes_in_buffer = nbytes;
+  src->length -= nbytes;
 
   return TRUE;
 }
@@ -135,6 +226,8 @@
 METHODDEF(void)
 skip_input_data (j_decompress_ptr cinfo, long num_bytes)
 {
+    xfprintf(stderr,"skip input data %d\n", num_bytes);
+
   my_src_ptr src = (my_src_ptr) cinfo->src;
 
   /* Just a dumb implementation for now.  Could use fseek() except
@@ -148,7 +241,15 @@
       /* note we assume that fill_input_buffer will never return FALSE,
        * so suspension need not be handled.
        */
+      if ( src->length == 0 ) 
+      {
+	  break;
+      }
     }
+    if ( (unsigned)num_bytes > src->pub.bytes_in_buffer ) 
+    {
+	num_bytes = src->pub.bytes_in_buffer;
+    }
     src->pub.next_input_byte += (unsigned) num_bytes;
     src->pub.bytes_in_buffer -= (unsigned) num_bytes;
   }
@@ -176,6 +277,7 @@
 METHODDEF(void)
 term_source (j_decompress_ptr /*cinfo*/)
 {
+    xfprintf(stderr,"term_source\n");
   /* no work necessary here */
 }
