Support extracting of thumbnail offsets.

AssetFileDescriptors work by defining a region of an underlying
FileDescriptor across process boundaries, so they require the offset
and length of the EXIF thumbnail region.

Bug: 10412208
Change-Id: I7520fdbbddd7afb1c454bd523ec32eb3602a998f
diff --git a/jhead.h b/jhead.h
index 596bfea..a3825e8 100644
--- a/jhead.h
+++ b/jhead.h
@@ -52,6 +52,7 @@
 typedef struct {
     uchar *  Data;
     int      Type;
+    unsigned Offset;
     unsigned Size;
 }Section_t;
 
diff --git a/jpgfile.c b/jpgfile.c
index 1ef9e99..214c888 100755
--- a/jpgfile.c
+++ b/jpgfile.c
@@ -156,6 +156,7 @@
 
 
         Sections[SectionsRead].Type = marker;
+        Sections[SectionsRead].Offset = ftell(infile);
   
         // Read the length of the section.
         lh = fgetc(infile);
@@ -221,6 +222,7 @@
 
                     CheckSectionsAllocated();
                     Sections[SectionsRead].Data = Data;
+                    Sections[SectionsRead].Offset = cp;
                     Sections[SectionsRead].Size = size;
                     Sections[SectionsRead].Type = PSEUDO_IMAGE_MARKER;
                     SectionsRead ++;
@@ -351,6 +353,7 @@
         }
 
         Sections[SectionsRead].Type = marker;
+        Sections[SectionsRead].Offset = pos;
 
         // Read the length of the section.
         lh = buffer[pos++];
@@ -410,6 +413,7 @@
 
                     CheckSectionsAllocated();
                     Sections[SectionsRead].Data = Data;
+                    Sections[SectionsRead].Offset = pos;
                     Sections[SectionsRead].Size = size;
                     Sections[SectionsRead].Type = PSEUDO_IMAGE_MARKER;
                     SectionsRead ++;
diff --git a/main.c b/main.c
index b8f1445..ace5bdd 100644
--- a/main.c
+++ b/main.c
@@ -475,6 +475,35 @@
     return NULL;
 }
 
+static jlongArray getThumbnailRange(JNIEnv *env, jobject jobj, jstring jfilename) {
+    jlongArray resultArray = NULL;
+    const char* filename = (*env)->GetStringUTFChars(env, jfilename, NULL);
+    if (filename) {
+        loadExifInfo(filename, FALSE);
+        Section_t* ExifSection = FindSection(M_EXIF);
+        if (ExifSection == NULL || ImageInfo.ThumbnailSize == 0) {
+            goto done;
+        }
+
+        jlong result[2];
+        result[0] = ExifSection->Offset + ImageInfo.ThumbnailOffset + 8;
+        result[1] = ImageInfo.ThumbnailSize;
+
+        resultArray = (*env)->NewLongArray(env, 2);
+        if (resultArray == NULL) {
+            goto done;
+        }
+
+        (*env)->SetLongArrayRegion(env, resultArray, 0, 2, result);
+    }
+done:
+    if (filename) {
+        (*env)->ReleaseStringUTFChars(env, jfilename, filename);
+    }
+    DiscardData();
+    return resultArray;
+}
+
 static int attributeCount;      // keep track of how many attributes we've added
 
 // returns new buffer length
@@ -728,6 +757,7 @@
   {"appendThumbnailNative", "(Ljava/lang/String;Ljava/lang/String;)Z", (void*)appendThumbnail },
   {"commitChangesNative", "(Ljava/lang/String;)V", (void*)commitChanges },
   {"getThumbnailNative", "(Ljava/lang/String;)[B", (void*)getThumbnail },
+  {"getThumbnailRangeNative", "(Ljava/lang/String;)[J", (void*)getThumbnailRange },
 };
 
 /*