From bde4c1af011e8ccc0e3e55f196696568e2827cd3 Mon Sep 17 00:00:00 2001 From: Robert Theis Date: Tue, 12 Aug 2014 18:25:46 -0700 Subject: [PATCH] Remove non-POSIX memory I/O. Partially addresses #25. Fixes #56. Fixes #58. --- .../leptonica/android/test/WriteFileTest.java | 27 -- tess-two/jni/Application.mk | 1 - .../Android.mk | 11 - .../config_auto.h | 2 +- .../stdio/extrastdio.h | 27 -- .../stdio/fmemopen.c | 277 ------------------ .../stdio/fopencookie.c | 186 ------------ .../stdio/open_memstream.c | 231 --------------- .../leptonica/android/WriteFile.java | 2 +- 9 files changed, 2 insertions(+), 762 deletions(-) delete mode 100644 tess-two/jni/com_googlecode_leptonica_android/stdio/extrastdio.h delete mode 100644 tess-two/jni/com_googlecode_leptonica_android/stdio/fmemopen.c delete mode 100644 tess-two/jni/com_googlecode_leptonica_android/stdio/fopencookie.c delete mode 100644 tess-two/jni/com_googlecode_leptonica_android/stdio/open_memstream.c diff --git a/tess-two-test/src/com/googlecode/leptonica/android/test/WriteFileTest.java b/tess-two-test/src/com/googlecode/leptonica/android/test/WriteFileTest.java index 3823b6320..ad6e25aeb 100644 --- a/tess-two-test/src/com/googlecode/leptonica/android/test/WriteFileTest.java +++ b/tess-two-test/src/com/googlecode/leptonica/android/test/WriteFileTest.java @@ -16,34 +16,7 @@ package com.googlecode.leptonica.android.test; -import android.graphics.Bitmap; -import android.graphics.BitmapFactory; -import android.test.suitebuilder.annotation.SmallTest; - -import com.googlecode.leptonica.android.Constants; -import com.googlecode.leptonica.android.Pix; -import com.googlecode.leptonica.android.ReadFile; -import com.googlecode.leptonica.android.WriteFile; - import junit.framework.TestCase; public class WriteFileTest extends TestCase { - @SmallTest - public void testWriteMem() { - Bitmap bmps = Bitmap.createBitmap(640, 480, Bitmap.Config.ARGB_8888); - Pix pixs = ReadFile.readBitmap(bmps); - byte[] encodedBytes = WriteFile.writeMem(pixs, Constants.IFF_BMP); - - assertNotNull(encodedBytes); - - Bitmap bmpd = BitmapFactory.decodeByteArray(encodedBytes, 0, encodedBytes.length); - - assertEquals(bmps.getWidth(), bmpd.getWidth()); - assertEquals(bmps.getHeight(), bmpd.getHeight()); - - bmps.recycle(); - pixs.recycle(); - encodedBytes = null; - bmpd.recycle(); - } } diff --git a/tess-two/jni/Application.mk b/tess-two/jni/Application.mk index 0e43645b7..2d0062e81 100644 --- a/tess-two/jni/Application.mk +++ b/tess-two/jni/Application.mk @@ -3,4 +3,3 @@ APP_ABI := armeabi armeabi-v7a mips x86 APP_OPTIM := release APP_PLATFORM := android-8 APP_CPPFLAGS += -fexceptions -frtti -NDK_TOOLCHAIN_VERSION := 4.8 diff --git a/tess-two/jni/com_googlecode_leptonica_android/Android.mk b/tess-two/jni/com_googlecode_leptonica_android/Android.mk index d9103610c..015ba62cc 100644 --- a/tess-two/jni/com_googlecode_leptonica_android/Android.mk +++ b/tess-two/jni/com_googlecode_leptonica_android/Android.mk @@ -23,17 +23,6 @@ LOCAL_CFLAGS := \ LOCAL_LDLIBS := \ -lz -# missing stdio functions - -ifneq ($(TARGET_SIMULATOR),true) -LOCAL_SRC_FILES += \ - stdio/open_memstream.c \ - stdio/fopencookie.c \ - stdio/fmemopen.c -LOCAL_C_INCLUDES += \ - stdio -endif - # jni LOCAL_SRC_FILES += \ diff --git a/tess-two/jni/com_googlecode_leptonica_android/config_auto.h b/tess-two/jni/com_googlecode_leptonica_android/config_auto.h index b9ef90105..8f4bf3b57 100644 --- a/tess-two/jni/com_googlecode_leptonica_android/config_auto.h +++ b/tess-two/jni/com_googlecode_leptonica_android/config_auto.h @@ -23,6 +23,6 @@ #define HAVE_LIBZ 1 #define HAVE_LIBGIF 0 #define HAVE_LIBUNGIF 0 -#define HAVE_FMEMOPEN 1 +#define HAVE_FMEMOPEN 0 #endif diff --git a/tess-two/jni/com_googlecode_leptonica_android/stdio/extrastdio.h b/tess-two/jni/com_googlecode_leptonica_android/stdio/extrastdio.h deleted file mode 100644 index b5f69280c..000000000 --- a/tess-two/jni/com_googlecode_leptonica_android/stdio/extrastdio.h +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef LEPTONICA__STDIO_H -#define LEPTONICA__STDIO_H - -#ifndef BUILD_HOST - -#include -#include - -typedef struct cookie_io_functions_t { - ssize_t (*read)(void *cookie, char *buf, size_t n); - ssize_t (*write)(void *cookie, const char *buf, size_t n); - int (*seek)(void *cookie, off_t *pos, int whence); - int (*close)(void *cookie); -} cookie_io_functions_t; - -FILE *fopencookie(void *cookie, const char *mode, cookie_io_functions_t functions); - -FILE *fmemopen(void *buf, size_t size, const char *mode); - -FILE *open_memstream(char **buf, size_t *size); - -FILE *__sfp(void); -int __sflags(const char *, int *); - -#endif - -#endif /* LEPTONICA__STDIO_H */ diff --git a/tess-two/jni/com_googlecode_leptonica_android/stdio/fmemopen.c b/tess-two/jni/com_googlecode_leptonica_android/stdio/fmemopen.c deleted file mode 100644 index 921db98de..000000000 --- a/tess-two/jni/com_googlecode_leptonica_android/stdio/fmemopen.c +++ /dev/null @@ -1,277 +0,0 @@ -/* Copyright (C) 2007 Eric Blake - * Permission to use, copy, modify, and distribute this software - * is freely granted, provided that this notice is preserved. - * - * Modifications for Android written Jul 2009 by Alan Viverette - */ - -/* -FUNCTION -<>---open a stream around a fixed-length string - -INDEX - fmemopen - -ANSI_SYNOPSIS - #include - FILE *fmemopen(void *restrict <[buf]>, size_t <[size]>, - const char *restrict <[mode]>); - -DESCRIPTION -<> creates a seekable <> stream that wraps a -fixed-length buffer of <[size]> bytes starting at <[buf]>. The stream -is opened with <[mode]> treated as in <>, where append mode -starts writing at the first NUL byte. If <[buf]> is NULL, then -<[size]> bytes are automatically provided as if by <>, with -the initial size of 0, and <[mode]> must contain <<+>> so that data -can be read after it is written. - -The stream maintains a current position, which moves according to -bytes read or written, and which can be one past the end of the array. -The stream also maintains a current file size, which is never greater -than <[size]>. If <[mode]> starts with <>, the position starts at -<<0>>, and file size starts at <[size]> if <[buf]> was provided. If -<[mode]> starts with <>, the position and file size start at <<0>>, -and if <[buf]> was provided, the first byte is set to NUL. If -<[mode]> starts with <>, the position and file size start at the -location of the first NUL byte, or else <[size]> if <[buf]> was -provided. - -When reading, NUL bytes have no significance, and reads cannot exceed -the current file size. When writing, the file size can increase up to -<[size]> as needed, and NUL bytes may be embedded in the stream (see -<> for an alternative that automatically enlarges the -buffer). When the stream is flushed or closed after a write that -changed the file size, a NUL byte is written at the current position -if there is still room; if the stream is not also open for reading, a -NUL byte is additionally written at the last byte of <[buf]> when the -stream has exceeded <[size]>, so that a write-only <[buf]> is always -NUL-terminated when the stream is flushed or closed (and the initial -<[size]> should take this into account). It is not possible to seek -outside the bounds of <[size]>. A NUL byte written during a flush is -restored to its previous value when seeking elsewhere in the string. - -RETURNS -The return value is an open FILE pointer on success. On error, -<> is returned, and <> will be set to EINVAL if <[size]> -is zero or <[mode]> is invalid, ENOMEM if <[buf]> was NULL and memory -could not be allocated, or EMFILE if too many streams are already -open. - -PORTABILITY -This function is being added to POSIX 200x, but is not in POSIX 2001. - -Supporting OS subroutines required: <>. -*/ - -#include -#include -#include -#include -#include -#include "extrastdio.h" - -/* Describe details of an open memstream. */ -typedef struct fmemcookie { - void *storage; /* storage to free on close */ - char *buf; /* buffer start */ - size_t pos; /* current position */ - size_t eof; /* current file size */ - size_t max; /* maximum file size */ - char append; /* nonzero if appending */ - char writeonly; /* 1 if write-only */ - char saved; /* saved character that lived at pos before write-only NUL */ -} fmemcookie; - -/* Read up to non-zero N bytes into BUF from stream described by - COOKIE; return number of bytes read (0 on EOF). */ -static int -fmemread(void *cookie, char *buf, int n) -{ - fmemcookie *c = (fmemcookie *) cookie; - /* Can't read beyond current size, but EOF condition is not an error. */ - if (c->pos > c->eof) - return 0; - if (n >= c->eof - c->pos) - n = c->eof - c->pos; - memcpy (buf, c->buf + c->pos, n); - c->pos += n; - return n; -} - -/* Write up to non-zero N bytes of BUF into the stream described by COOKIE, - returning the number of bytes written or EOF on failure. */ -static int -fmemwrite(void *cookie, const char *buf, int n) -{ - fmemcookie *c = (fmemcookie *) cookie; - int adjust = 0; /* true if at EOF, but still need to write NUL. */ - - /* Append always seeks to eof; otherwise, if we have previously done - a seek beyond eof, ensure all intermediate bytes are NUL. */ - if (c->append) - c->pos = c->eof; - else if (c->pos > c->eof) - memset (c->buf + c->eof, '\0', c->pos - c->eof); - /* Do not write beyond EOF; saving room for NUL on write-only stream. */ - if (c->pos + n > c->max - c->writeonly) - { - adjust = c->writeonly; - n = c->max - c->pos; - } - /* Now n is the number of bytes being modified, and adjust is 1 if - the last byte is NUL instead of from buf. Write a NUL if - write-only; or if read-write, eof changed, and there is still - room. When we are within the file contents, remember what we - overwrite so we can restore it if we seek elsewhere later. */ - if (c->pos + n > c->eof) - { - c->eof = c->pos + n; - if (c->eof - adjust < c->max) - c->saved = c->buf[c->eof - adjust] = '\0'; - } - else if (c->writeonly) - { - if (n) - { - c->saved = c->buf[c->pos + n - adjust]; - c->buf[c->pos + n - adjust] = '\0'; - } - else - adjust = 0; - } - c->pos += n; - if (n - adjust) - memcpy (c->buf + c->pos - n, buf, n - adjust); - else - { - return EOF; - } - return n; -} - -/* Seek to position POS relative to WHENCE within stream described by - COOKIE; return resulting position or fail with EOF. */ -static fpos_t -fmemseek(void *cookie, fpos_t pos, int whence) -{ - fmemcookie *c = (fmemcookie *) cookie; - off_t offset = (off_t) pos; - - if (whence == SEEK_CUR) - offset += c->pos; - else if (whence == SEEK_END) - offset += c->eof; - if (offset < 0) - { - offset = -1; - } - else if (offset > c->max) - { - offset = -1; - } - else - { - if (c->writeonly && c->pos < c->eof) - { - c->buf[c->pos] = c->saved; - c->saved = '\0'; - } - c->pos = offset; - if (c->writeonly && c->pos < c->eof) - { - c->saved = c->buf[c->pos]; - c->buf[c->pos] = '\0'; - } - } - return (fpos_t) offset; -} - -/* Reclaim resources used by stream described by COOKIE. */ -static int -fmemclose(void *cookie) -{ - fmemcookie *c = (fmemcookie *) cookie; - free (c->storage); - return 0; -} - -/* Open a memstream around buffer BUF of SIZE bytes, using MODE. - Return the new stream, or fail with NULL. */ -FILE * -fmemopen(void *buf, size_t size, const char *mode) -{ - FILE *fp; - fmemcookie *c; - int flags; - int dummy; - - if ((flags = __sflags (mode, &dummy)) == 0) - return NULL; - if (!size || !(buf || flags & __SAPP)) - { - return NULL; - } - if ((fp = (FILE *) __sfp ()) == NULL) - return NULL; - if ((c = (fmemcookie *) malloc (sizeof *c + (buf ? 0 : size))) == NULL) - { - fp->_flags = 0; /* release */ - - return NULL; - } - - c->storage = c; - c->max = size; - /* 9 modes to worry about. */ - /* w/a, buf or no buf: Guarantee a NUL after any file writes. */ - c->writeonly = (flags & __SWR) != 0; - c->saved = '\0'; - if (!buf) - { - /* r+/w+/a+, and no buf: file starts empty. */ - c->buf = (char *) (c + 1); - *(char *) buf = '\0'; - c->pos = c->eof = 0; - c->append = (flags & __SAPP) != 0; - } - else - { - c->buf = (char *) buf; - switch (*mode) - { - case 'a': - /* a/a+ and buf: position and size at first NUL. */ - buf = memchr (c->buf, '\0', size); - c->eof = c->pos = buf ? (char *) buf - c->buf : size; - if (!buf && c->writeonly) - /* a: guarantee a NUL within size even if no writes. */ - c->buf[size - 1] = '\0'; - c->append = 1; - break; - case 'r': - /* r/r+ and buf: read at beginning, full size available. */ - c->pos = c->append = 0; - c->eof = size; - break; - case 'w': - /* w/w+ and buf: write at beginning, truncate to empty. */ - c->pos = c->append = c->eof = 0; - *c->buf = '\0'; - break; - default: - abort(); - } - } - - fp->_file = -1; - fp->_flags = flags; - fp->_cookie = c; - fp->_read = flags & (__SRD | __SRW) ? fmemread : NULL; - fp->_write = flags & (__SWR | __SRW) ? fmemwrite : NULL; - fp->_seek = fmemseek; - fp->_close = fmemclose; - - return fp; -} - diff --git a/tess-two/jni/com_googlecode_leptonica_android/stdio/fopencookie.c b/tess-two/jni/com_googlecode_leptonica_android/stdio/fopencookie.c deleted file mode 100644 index 8b6d9d5ac..000000000 --- a/tess-two/jni/com_googlecode_leptonica_android/stdio/fopencookie.c +++ /dev/null @@ -1,186 +0,0 @@ -/* Copyright (C) 2007 Eric Blake - * Permission to use, copy, modify, and distribute this software - * is freely granted, provided that this notice is preserved. - * - * Modifications for Android written Jul 2009 by Alan Viverette - */ - -/* -FUNCTION -<>---open a stream with custom callbacks - -INDEX - fopencookie - -ANSI_SYNOPSIS - #include - typedef ssize_t (*cookie_read_function_t)(void *_cookie, char *_buf, - size_t _n); - typedef ssize_t (*cookie_write_function_t)(void *_cookie, - const char *_buf, size_t _n); - typedef int (*cookie_seek_function_t)(void *_cookie, off_t *_off, - int _whence); - typedef int (*cookie_close_function_t)(void *_cookie); - FILE *fopencookie(const void *<[cookie]>, const char *<[mode]>, - cookie_io_functions_t <[functions]>); - -DESCRIPTION -<> creates a <> stream where I/O is performed using -custom callbacks. The callbacks are registered via the structure: - -. typedef struct -. { -. cookie_read_function_t *read; -. cookie_write_function_t *write; -. cookie_seek_function_t *seek; -. cookie_close_function_t *close; -. } cookie_io_functions_t; - -The stream is opened with <[mode]> treated as in <>. The -callbacks <[functions.read]> and <[functions.write]> may only be NULL -when <[mode]> does not require them. - -<[functions.read]> should return -1 on failure, or else the number of -bytes read (0 on EOF). It is similar to <>, except that -<[cookie]> will be passed as the first argument. - -<[functions.write]> should return -1 on failure, or else the number of -bytes written. It is similar to <>, except that <[cookie]> -will be passed as the first argument. - -<[functions.seek]> should return -1 on failure, and 0 on success, with -*<[_off]> set to the current file position. It is a cross between -<> and <>, with the <[_whence]> argument interpreted in -the same manner. A NULL <[functions.seek]> makes the stream behave -similarly to a pipe in relation to stdio functions that require -positioning. - -<[functions.close]> should return -1 on failure, or 0 on success. It -is similar to <>, except that <[cookie]> will be passed as the -first argument. A NULL <[functions.close]> merely flushes all data -then lets <> succeed. A failed close will still invalidate -the stream. - -Read and write I/O functions are allowed to change the underlying -buffer on fully buffered or line buffered streams by calling -<>. They are also not required to completely fill or empty -the buffer. They are not, however, allowed to change streams from -unbuffered to buffered or to change the state of the line buffering -flag. They must also be prepared to have read or write calls occur on -buffers other than the one most recently specified. - -RETURNS -The return value is an open FILE pointer on success. On error, -<> is returned, and <> will be set to EINVAL if a -function pointer is missing or <[mode]> is invalid, ENOMEM if the -stream cannot be created, or EMFILE if too many streams are already -open. - -PORTABILITY -This function is a newlib extension, copying the prototype from Linux. -It is not portable. See also the <> interface from BSD. - -Supporting OS subroutines required: <>. -*/ - -#include -#include -#include -#include "extrastdio.h" - -typedef struct fccookie { - void *cookie; - FILE *fp; - ssize_t (*readfn)(void *, char *, size_t); - ssize_t (*writefn)(void *, const char *, size_t); - int (*seekfn)(void *, off_t *, int); - int (*closefn)(void *); -} fccookie; - -static int -fcread(void *cookie, char *buf, int n) -{ - int result; - fccookie *c = (fccookie *) cookie; - result = c->readfn (c->cookie, buf, n); - return result; -} - -static int -fcwrite(void *cookie, const char *buf, int n) -{ - int result; - fccookie *c = (fccookie *) cookie; - if (c->fp->_flags & __SAPP && c->fp->_seek) - { - c->fp->_seek (cookie, 0, SEEK_END); - } - result = c->writefn (c->cookie, buf, n); - return result; -} - -static fpos_t -fcseek(void *cookie, fpos_t pos, int whence) -{ - fccookie *c = (fccookie *) cookie; - off_t offset = (off_t) pos; - - c->seekfn (c->cookie, &offset, whence); - - return (fpos_t) offset; -} - -static int -fcclose(void *cookie) -{ - int result = 0; - fccookie *c = (fccookie *) cookie; - if (c->closefn) - { - result = c->closefn (c->cookie); - } - free (c); - return result; -} - -FILE * -fopencookie(void *cookie, const char *mode, cookie_io_functions_t functions) -{ - FILE *fp; - fccookie *c; - int flags; - int dummy; - - if ((flags = __sflags (mode, &dummy)) == 0) - return NULL; - if (((flags & (__SRD | __SRW)) && !functions.read) - || ((flags & (__SWR | __SRW)) && !functions.write)) - { - return NULL; - } - if ((fp = (FILE *) __sfp ()) == NULL) - return NULL; - if ((c = (fccookie *) malloc (sizeof *c)) == NULL) - { - fp->_flags = 0; - return NULL; - } - - fp->_file = -1; - fp->_flags = flags; - c->cookie = cookie; - c->fp = fp; - fp->_cookie = c; - c->readfn = functions.read; - fp->_read = fcread; - c->writefn = functions.write; - fp->_write = fcwrite; - c->seekfn = functions.seek; - fp->_seek = functions.seek ? fcseek : NULL; - c->closefn = functions.close; - fp->_close = fcclose; - - return fp; -} - - diff --git a/tess-two/jni/com_googlecode_leptonica_android/stdio/open_memstream.c b/tess-two/jni/com_googlecode_leptonica_android/stdio/open_memstream.c deleted file mode 100644 index 8b598a044..000000000 --- a/tess-two/jni/com_googlecode_leptonica_android/stdio/open_memstream.c +++ /dev/null @@ -1,231 +0,0 @@ -/* Copyright (C) 2007 Eric Blake - * Permission to use, copy, modify, and distribute this software - * is freely granted, provided that this notice is preserved. - * - * Modifications for Android written Jul 2009 by Alan Viverette - */ - -/* -FUNCTION -<>---open a write stream around an arbitrary-length string - -INDEX - open_memstream - -ANSI_SYNOPSIS - #include - FILE *open_memstream(char **restrict <[buf]>, - size_t *restrict <[size]>); - -DESCRIPTION -<> creates a seekable <> stream that wraps an -arbitrary-length buffer, created as if by <>. The current -contents of *<[buf]> are ignored; this implementation uses *<[size]> -as a hint of the maximum size expected, but does not fail if the hint -was wrong. The parameters <[buf]> and <[size]> are later stored -through following any call to <> or <>, set to the -current address and usable size of the allocated string; although -after fflush, the pointer is only valid until another stream operation -that results in a write. Behavior is undefined if the user alters -either *<[buf]> or *<[size]> prior to <>. - -The stream is write-only, since the user can directly read *<[buf]> -after a flush; see <> for a way to wrap a string with a -readable stream. The user is responsible for calling <> on -the final *<[buf]> after <>. - -Any time the stream is flushed, a NUL byte is written at the current -position (but is not counted in the buffer length), so that the string -is always NUL-terminated after at most *<[size]> bytes. However, data -previously written beyond the current stream offset is not lost, and -the NUL byte written during a flush is restored to its previous value -when seeking elsewhere in the string. - -RETURNS -The return value is an open FILE pointer on success. On error, -<> is returned, and <> will be set to EINVAL if <[buf]> -or <[size]> is NULL, ENOMEM if memory could not be allocated, or -EMFILE if too many streams are already open. - -PORTABILITY -This function is being added to POSIX 200x, but is not in POSIX 2001. - -Supporting OS subroutines required: <>. -*/ - -#include -#include -#include -#include -#include "extrastdio.h" - -/* Describe details of an open memstream. */ -typedef struct memstream { - void *storage; /* storage to free on close */ - char **pbuf; /* pointer to the current buffer */ - size_t *psize; /* pointer to the current size, smaller of pos or eof */ - size_t pos; /* current position */ - size_t eof; /* current file size */ - size_t max; /* current malloc buffer size, always > eof */ - char saved; /* saved character that lived at *psize before NUL */ -} memstream; - -/* Write up to non-zero N bytes of BUF into the stream described by COOKIE, - returning the number of bytes written or EOF on failure. */ -static int -memwrite(void *cookie, const char *buf, int n) -{ - memstream *c = (memstream *) cookie; - char *cbuf = *c->pbuf; - - /* size_t is unsigned, but off_t is signed. Don't let stream get so - big that user cannot do ftello. */ - if (sizeof (off_t) == sizeof (size_t) && (ssize_t) (c->pos + n) < 0) - { - return EOF; - } - /* Grow the buffer, if necessary. Choose a geometric growth factor - to avoid quadratic realloc behavior, but use a rate less than - (1+sqrt(5))/2 to accomodate malloc overhead. Overallocate, so - that we can add a trailing \0 without reallocating. The new - allocation should thus be max(prev_size*1.5, c->pos+n+1). */ - if (c->pos + n >= c->max) - { - size_t newsize = c->max * 3 / 2; - if (newsize < c->pos + n + 1) - newsize = c->pos + n + 1; - cbuf = realloc (cbuf, newsize); - if (! cbuf) - return EOF; /* errno already set to ENOMEM */ - *c->pbuf = cbuf; - c->max = newsize; - } - /* If we have previously done a seek beyond eof, ensure all - intermediate bytes are NUL. */ - if (c->pos > c->eof) - memset (cbuf + c->eof, '\0', c->pos - c->eof); - memcpy (cbuf + c->pos, buf, n); - c->pos += n; - /* If the user has previously written further, remember what the - trailing NUL is overwriting. Otherwise, extend the stream. */ - if (c->pos > c->eof) - c->eof = c->pos; - else - c->saved = cbuf[c->pos]; - cbuf[c->pos] = '\0'; - *c->psize = c->pos; - return n; -} - -/* Seek to position POS relative to WHENCE within stream described by - COOKIE; return resulting position or fail with EOF. */ -static fpos_t -memseek(void *cookie, fpos_t pos, int whence) -{ - memstream *c = (memstream *) cookie; - off_t offset = (off_t) pos; - - if (whence == SEEK_CUR) - offset += c->pos; - else if (whence == SEEK_END) - offset += c->eof; - if (offset < 0) - { - offset = -1; - } - else if ((size_t) offset != offset) - { - offset = -1; - } - else - { - if (c->pos < c->eof) - { - (*c->pbuf)[c->pos] = c->saved; - c->saved = '\0'; - } - c->pos = offset; - if (c->pos < c->eof) - { - c->saved = (*c->pbuf)[c->pos]; - (*c->pbuf)[c->pos] = '\0'; - *c->psize = c->pos; - } - else - *c->psize = c->eof; - } - return (fpos_t) offset; -} - -/* Reclaim resources used by stream described by COOKIE. */ -static int -memclose(void *cookie) -{ - memstream *c = (memstream *) cookie; - char *buf; - - /* Be nice and try to reduce any unused memory. */ - buf = realloc (*c->pbuf, *c->psize + 1); - if (buf) - *c->pbuf = buf; - free (c->storage); - return 0; -} - -/* Open a memstream that tracks a dynamic buffer in BUF and SIZE. - Return the new stream, or fail with NULL. */ -FILE * -open_memstream(char **buf, size_t *size) -{ - FILE *fp; - memstream *c; - int flags; - - if (!buf || !size) - { - return NULL; - } - if ((fp = (FILE *) __sfp ()) == NULL) - return NULL; - if ((c = (memstream *) malloc (sizeof *c)) == NULL) - { - fp->_flags = 0; /* release */ - return NULL; - } - - /* Use *size as a hint for initial sizing, but bound the initial - malloc between 64 bytes (same as asprintf, to avoid frequent - mallocs on small strings) and 64k bytes (to avoid overusing the - heap if *size was garbage). */ - c->max = *size; - if (c->max < 64) - c->max = 64; - else if (c->max > 64 * 1024) - c->max = 64 * 1024; - *size = 0; - *buf = malloc (c->max); - if (!*buf) - { - fp->_flags = 0; /* release */ - free (c); - return NULL; - } - **buf = '\0'; - - c->storage = c; - c->pbuf = buf; - c->psize = size; - c->eof = 0; - c->saved = '\0'; - - fp->_file = -1; - fp->_flags = __SWR; - fp->_cookie = c; - fp->_read = NULL; - fp->_write = memwrite; - fp->_seek = memseek; - fp->_close = memclose; - - return fp; -} - diff --git a/tess-two/src/com/googlecode/leptonica/android/WriteFile.java b/tess-two/src/com/googlecode/leptonica/android/WriteFile.java index 179a7c628..87c37a056 100644 --- a/tess-two/src/com/googlecode/leptonica/android/WriteFile.java +++ b/tess-two/src/com/googlecode/leptonica/android/WriteFile.java @@ -118,7 +118,7 @@ public static byte[] writeMem(Pix pixs, int format) { if (pixs == null) throw new IllegalArgumentException("Source pix must be non-null"); - return nativeWriteMem(pixs.mNativePix, format); + throw new RuntimeException("writeMem() is not currently supported"); } /**