static AS3_Val init(void * self, AS3_Val args) { void * ref; void * src; void * dest; AS3_ArrayValue(args, "AS3ValType, AS3ValType, AS3ValType", &ref, &src, &dest); flashErrorsRef = (AS3_Val)ref; config.wave.file = funopen((void *)src, readByteArray, writeByteArray, seekByteArray, closeByteArray); config.wave.output = funopen((void *)dest, readByteArray, writeByteArray, seekByteArray, closeByteArray); if (config.wave.file == NULL || config.wave.output == NULL) { ERROR("Unable to set bytes arrays"); } wave_open(); set_defaults(); check_config(); start_compress(); return AS3_Int(1); }
FILE * zopen(const char *fname, const char *mode) { gzFile gz = gzopen(fname, mode); if(gz == NULL) return NULL; if(*mode == 'r') return (funopen(gz, xgzread, NULL, NULL, gzclose)); else return (funopen(gz, NULL, xgzwrite, NULL, gzclose)); }
TEST(STDIO_TEST, funopen_seek) { #if defined(__BIONIC__) auto read_fn = [](void*, char*, int) { return -1; }; auto seek_fn = [](void*, fpos_t, int) -> fpos_t { return 0xfedcba12; }; auto seek64_fn = [](void*, fpos64_t, int) -> fpos64_t { return 0xfedcba12345678; }; FILE* fp = funopen(nullptr, read_fn, nullptr, seek_fn, nullptr); ASSERT_TRUE(fp != nullptr); fpos_t pos; #if defined(__LP64__) EXPECT_EQ(0, fgetpos(fp, &pos)) << strerror(errno); EXPECT_EQ(0xfedcba12LL, pos); #else EXPECT_EQ(-1, fgetpos(fp, &pos)) << strerror(errno); EXPECT_EQ(EOVERFLOW, errno); #endif FILE* fp64 = funopen64(nullptr, read_fn, nullptr, seek64_fn, nullptr); ASSERT_TRUE(fp64 != nullptr); fpos64_t pos64; EXPECT_EQ(0, fgetpos64(fp64, &pos64)) << strerror(errno); EXPECT_EQ(0xfedcba12345678, pos64); #else GTEST_LOG_(INFO) << "glibc uses fopencookie instead.\n"; #endif }
FILE * cfile_get_filehandle( char * filename, char * mode ) { FILE * fp; char temp[256]; descent_critical_error = 0; #ifdef ANDROID_NDK AAsset* asset = AAssetManager_open(Asset_manager, filename, AASSET_MODE_BUFFER); if (!asset) { fp = NULL; } else { fp = funopen(asset, android_read, android_write, android_seek, android_close); } #else sprintf(temp, "%s/%s", Resource_path, filename); fp = fopen( temp, mode ); #endif if ( fp && descent_critical_error ) { fclose(fp); fp = NULL; } if ( (fp==NULL) && (AltHogdir_initialized) ) { strcpy( temp, AltHogDir ); strcat( temp, filename ); descent_critical_error = 0; fp = fopen( temp, mode ); if ( fp && descent_critical_error ) { fclose(fp); fp = NULL; } } return fp; }
FILE* apk_fopen(const char* fname, const char* mode) { if(mode[0] == 'w' || THApkFile_AAssetManager == NULL ) return NULL; AAsset* asset = AAssetManager_open((AAssetManager*)THApkFile_AAssetManager, fname, 0); if(!asset) return NULL; return funopen(asset, apk_read, apk_write, apk_seek, apk_close); }
FILE *open_memstream(char **ptr, size_t *sizeloc) { if (ptr && sizeloc) { struct memstream *ms = calloc(1, sizeof(struct memstream)); FILE *fp = 0; if (!ms) return 0; /* errno == ENOMEM */ ms->position = ms->size = 0; ms->capacity = 4096; ms->contents = calloc(ms->capacity, 1); if (!ms->contents) { free(ms); return 0; } /* errno == ENOMEM */ ms->ptr = ptr; ms->sizeloc = sizeloc; memstream_print(ms); fp = funopen(ms, memstream_read, memstream_write, memstream_seek, memstream_close); if (!fp) { free(ms->contents); free(ms); return 0; /* errno set by funopen */ } *ptr = ms->contents; *sizeloc = ms->size; return fp; } errno = EINVAL; return 0; }
TEST(STDIO_TEST, fseek_ftell_unseekable) { #if defined(__BIONIC__) // glibc has fopencookie instead. auto read_fn = [](void*, char*, int) { return -1; }; FILE* fp = funopen(nullptr, read_fn, nullptr, nullptr, nullptr); ASSERT_TRUE(fp != nullptr); // Check that ftell balks on an unseekable FILE*. errno = 0; ASSERT_EQ(-1, ftell(fp)); ASSERT_EQ(ESPIPE, errno); // SEEK_CUR is rewritten as SEEK_SET internally... errno = 0; ASSERT_EQ(-1, fseek(fp, 0, SEEK_CUR)); ASSERT_EQ(ESPIPE, errno); // ...so it's worth testing the direct seek path too. errno = 0; ASSERT_EQ(-1, fseek(fp, 0, SEEK_SET)); ASSERT_EQ(ESPIPE, errno); fclose(fp); #else GTEST_LOG_(INFO) << "glibc uses fopencookie instead.\n"; #endif }
/* * Prepare a memstream. */ FILE* open_memstream(char** bufp, size_t* sizep) { FILE* fp; MemStream* stream; if (bufp == NULL || sizep == NULL) { errno = EINVAL; return NULL; } stream = (MemStream*) calloc(1, sizeof(MemStream)); if (stream == NULL) return NULL; fp = funopen(stream, NULL, write_memstream, seek_memstream, close_memstream); if (fp == NULL) { free(stream); return NULL; } *sizep = 0; *bufp = NULL; stream->bufp = bufp; stream->sizep = sizep; return fp; }
int ccv_read_impl(const void* in, ccv_dense_matrix_t** x, int type, int rows, int cols, int scanline) { FILE* fd = 0; if (type & CCV_IO_ANY_FILE) { assert(rows == 0 && cols == 0 && scanline == 0); fd = fopen((const char*)in, "rb"); if (!fd) return CCV_IO_ERROR; return _ccv_read_and_close_fd(fd, x, type); } else if (type & CCV_IO_ANY_STREAM) { assert(rows > 8 && cols == 0 && scanline == 0); assert((type & 0xFF) != CCV_IO_DEFLATE_STREAM); // deflate stream (compressed stream) is not supported yet #if _XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L || defined(__APPLE__) || defined(BSD) // this is only supported by glibc #if _XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L fd = fmemopen((void*)in, (size_t)rows, "rb"); #else ccv_io_mem_t mem = { .size = rows, .pos = 0, .buffer = (char*)in, }; fd = funopen(&mem, readfn, 0, seekfn, 0); #endif if (!fd) return CCV_IO_ERROR; // mimicking itself as a "file" type = (type & ~0x10) | 0x20; return _ccv_read_and_close_fd(fd, x, type); #endif } else if (type & CCV_IO_ANY_RAW) {
/* Create a memstream. */ FILE *open_memstream(char **ptr, size_t *sizeloc) { FILE *f; struct mem_stream *stream; if (ptr == NULL || sizeloc == NULL) { errno = EINVAL; return NULL; } stream = (struct mem_stream *) calloc(1, sizeof(struct mem_stream)); if (stream == NULL) return NULL; f = funopen(stream, NULL, write_memstream, NULL, close_memstream); if (f == NULL) { free(stream); return NULL; } *ptr = NULL; *sizeloc = 0; stream->buf = ptr; stream->sizeloc = sizeloc; return f; }
static FILE *cookieopen(void *cookie, const char *mode, ssize_t (*cread)(void *, char *, size_t), ssize_t (*cwrite)(void *, const char *, size_t), int (*cclose)(void *)) { if (!cookie) return 0; #ifdef HAVE_FUNOPEN return funopen(cookie, (int (*)(void *, char *, int))(*mode == 'r' ? cread: NULL),/* readfn */ (int (*)(void *, const char *, int))(*mode == 'w' ? cwrite : NULL), /* writefn */ (fpos_t (*)(void *, fpos_t, int))NULL, /* seekfn */ cclose ); #elif defined(HAVE_FOPENCOOKIE) cookie_io_functions_t cio; memset(&cio, 0, sizeof(cio)); if (*mode == 'r') cio.read = cread; else if (*mode == 'w') cio.write = cwrite; cio.close = cclose; return fopencookie(cookie, *mode == 'w' ? "w" : "r", cio); #else # error Need to implement custom I/O #endif }
ATF_TC_BODY(bad_big5_getwc, tc) { const char buf[] = { 0xcf, 0x20 }; struct ibuf ib = { .buf = buf, .buflen = sizeof(buf), }; FILE *fp = funopen(&ib, readfn, NULL, NULL, NULL); ATF_REQUIRE(fp != NULL); setlocale(LC_CTYPE, "zh_TW.Big5"); ATF_REQUIRE_EQ(getwc(fp), WEOF); fclose(fp); } ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, bad_big5_wprintf); ATF_TP_ADD_TC(tp, bad_big5_swprintf); ATF_TP_ADD_TC(tp, good_big5_wprintf); ATF_TP_ADD_TC(tp, good_big5_swprintf); ATF_TP_ADD_TC(tp, good_big5_getwc); ATF_TP_ADD_TC(tp, bad_big5_getwc); return atf_no_error(); }
/* * Create a new input string buffer stream. */ FILE * string_buf_input(const void *buf, size_t len, int copy) { struct input_buf *r; int esave; FILE *fp; if ((r = MALLOC(INPUT_MEM_TYPE, sizeof(*r))) == NULL) return (NULL); memset(r, 0, sizeof(*r)); if (copy) { if ((r->data = MALLOC(INPUT_MEM_TYPE, len)) == NULL) { FREE(INPUT_MEM_TYPE, r); return (NULL); } memcpy(r->data, buf, len); r->copied = 1; } else r->data = (char *)buf; r->pos = 0; r->len = len; if ((fp = funopen(r, string_buf_input_read, NULL, string_buf_input_seek, string_buf_input_close)) == NULL) { esave = errno; if (r->copied) FREE(INPUT_MEM_TYPE, r->data); FREE(INPUT_MEM_TYPE, r); errno = esave; } return (fp); }
FILE* SupernovaAndroid::platformFopen(const char* fname, const char* mode) { if(mode[0] == 'w') return NULL; AAsset* asset = AAssetManager_open(android_asset_manager, fname, 0); if(!asset) return NULL; return funopen(asset, android_read, android_write, android_seek, android_close); }
/* simple, but portable version of fmemopen for OS X / BSD */ FILE *fmemopen(void *buf, size_t size, const char *mode) { fmem_t *mem = (fmem_t *) malloc(sizeof(fmem_t)); memset(mem, 0, sizeof(fmem_t)); mem->size = size, mem->buffer = buf; return funopen(mem, readfn, writefn, seekfn, closefn); }
FILE* android_fopen(const char* fname, const char* mode) { if(mode[0] == 'w') return NULL; // __android_log_print(ANDROID_LOG_VERBOSE, "test", "opening\n"); AAsset* asset = AAssetManager_open(android_asset_manager, fname, 0); // __android_log_print(ANDROID_LOG_VERBOSE, "test", "%d\n", asset); if(!asset) return NULL; return funopen(asset, android_read, android_write, android_seek, android_close); }
TEST(STDIO_TEST, funopen_EINVAL) { #if defined(__BIONIC__) errno = 0; ASSERT_EQ(nullptr, funopen(nullptr, nullptr, nullptr, nullptr, nullptr)); ASSERT_EQ(EINVAL, errno); #else GTEST_LOG_(INFO) << "glibc uses fopencookie instead.\n"; #endif }
main() { run("-", stdout); run("funopen", funopen((void *)5, do_read, &do_write, do_seek, do_close)); run("fropen", fropen(NULL, do_read)); run("fwopen", fwopen(NULL, do_write)); }
// Replacement for fopen FILE *android_fopen(const char *fileName, const char *mode) { if (mode[0] == 'w') return NULL; AAsset *asset = AAssetManager_open(assetManager, fileName, 0); if (!asset) return NULL; return funopen(asset, android_read, android_write, android_seek, android_close); }
FILE *fmemopen(void *buf, size_t size, const char *mode) { chunk_t *cookie; INIT(cookie, .ptr = buf, .len = size, ); return funopen(cookie, (void*)fmemread, (void*)fmemwrite, NULL, fmemclose); }
/** * \brief portable version of SCFmemopen for OS X / BSD built on top of funopen() * \param buffer that holds the file content * \param size of the file buffer * \param mode mode of the file to open * \retval pointer to the file; NULL if something is wrong */ FILE *SCFmemopen(void *buf, size_t size, const char *mode) { SCFmem *mem = (SCFmem *) SCMalloc(sizeof(SCFmem)); if (mem == NULL) return NULL; memset(mem, 0, sizeof(SCFmem)); mem->size = size, mem->buffer = buf; return funopen(mem, ReadFn, WriteFn, SeekFn, CloseFn); }
FILE *fmemopen(void *buf, size_t size, const char *mode) { // This data is released on fclose. fmem_t* mem = (fmem_t *) malloc(sizeof(fmem_t)); // Zero-out the structure. memset(mem, 0, sizeof(fmem_t)); mem->size = size; mem->buffer = buf; // funopen's man page: https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/funopen.3.html return funopen(mem, readfn, writefn, seekfn, closefn); }
FILE* Tinker_Android_tfopen(const tstring& sFile, const tstring& sMode) { if (sMode[0] == 'w') return nullptr; InitializeAssetManager(); AAsset* pAsset = AAssetManager_open(g_pAssetManager, sFile.c_str(), AASSET_MODE_STREAMING); if (!pAsset) return nullptr; return funopen(pAsset, android_read, android_write, android_seek, android_close); }
/* Return a FP to be used for data output. The FILE pointer is valid until the end of a handler. So a close is not needed. Assuan does all the buffering needed to insert the status line as well as the required line wappping and quoting for data lines. We use GNU's custom streams here. There should be an alternative implementaion for systems w/o a glibc, a simple implementation could use a child process */ FILE * assuan_get_data_fp (assuan_context_t ctx) { #if defined (HAVE_FOPENCOOKIE) || defined (HAVE_FUNOPEN) if (ctx->outbound.data.fp) return ctx->outbound.data.fp; #ifdef HAVE_FUNOPEN ctx->outbound.data.fp = funopen (ctx, 0, fun1_cookie_write, 0, _assuan_cookie_write_flush); #else ctx->outbound.data.fp = funopen (ctx, 0, fun2_cookie_write, 0, _assuan_cookie_write_flush); #endif ctx->outbound.data.error = 0; return ctx->outbound.data.fp; #else gpg_err_set_errno (ENOSYS); return NULL; #endif }
FILE* android_fopen(const char* fname, const char* mode) { if (mode[0] == 'w') return NULL; if (fname[0] != '\0' && fname[0] == '.' && fname[1] == '/') { fname += 2; } std::string tmp(fname); boost::replace_all(tmp, "/./", "/"); AAsset* asset = AAssetManager_open(android_asset_manager, tmp.c_str(), 0); if (!asset) return NULL; return funopen(asset, android_read, android_write, android_seek, android_close); }
AS3_Val initialize3DS( void* self, AS3_Val args ) { FILE * file; void * dest; Entity * entity; A3DS * a3ds; DWORD render_mode; AS3_ArrayValue(args, "AS3ValType, PtrType, IntType", &dest, &entity, &render_mode); file = funopen((void *)dest, readByteArray, writeByteArray, seekByteArray, closeByteArray); a3ds = A3DS_Create( file, entity, render_mode ); return AS3_Array( "PtrType, IntType, IntType, PtrType, IntType, IntType, IntType", a3ds, a3ds->mNum, a3ds->tNum, a3ds->a3d_materialList->next, FPOS( A3DS_MaterialList, next ), FPOS( A3DS_MaterialList, texture ), FPOS( Texture, name ) ); }
int BIO_vprintf(BIO *bio, const char *format, va_list args) { FILE *fp; int ret; fp = funopen(bio, NULL, &_BIO_write, NULL, NULL); if (fp == NULL) { ret = -1; goto fail; } ret = vfprintf(fp, format, args); fclose(fp); fail: return (ret); }
FILE *open_memstream(char **ptr, size_t *sizeloc) { struct memstream *m; if(!(m = malloc(sizeof *m))) return 0; m->buffer = 0; m->size = 0; m->space = 0; m->ptr = ptr; m->sizeloc = sizeloc; *ptr = 0; *sizeloc = 0; return funopen(m, 0, /* read */ memstream_writefn, 0, /* seek */ 0); /* close */ }
FILE* fmemopen(void* buf, size_t capacity, const char* mode) { int flags; if (__sflags(mode, &flags) == 0) { errno = EINVAL; return nullptr; } fmemopen_cookie* ck = static_cast<fmemopen_cookie*>(calloc(sizeof(fmemopen_cookie), 1)); if (ck == nullptr) return nullptr; ck->buf = static_cast<char*>(buf); ck->capacity = capacity; if (ck->buf == nullptr) ck->buf = ck->allocation = static_cast<char*>(calloc(capacity, 1)); if (ck->buf == nullptr) { free(ck); return nullptr; } FILE* fp = funopen(ck, (flags & O_WRONLY) ? nullptr : fmemopen_read, (flags & O_RDONLY) ? nullptr : fmemopen_write, fmemopen_seek, fmemopen_close); if (fp == nullptr) { fmemopen_close(ck); return nullptr; } if (mode[0] == 'a') { ck->size = strnlen(ck->buf, ck->capacity); ck->offset = ck->size; ck->append = true; } else if (mode[0] == 'r') { ck->size = capacity; ck->offset = 0; } else if (mode[0] == 'w') { ck->size = 0; ck->offset = 0; if (capacity > 0) { ck->buf[0] = '\0'; } } return fp; }
static FILE *mygzfopen(gzFile* gzf) { #ifdef HAVE_FUNOPEN return funopen( gzf, (int (*)(void *, char *, int))cookie_gzread, (int (*)(void *, const char *, int))NULL, /* writefn */ (fpos_t (*)(void *, fpos_t, int))NULL, /* seekfn */ cookie_gzclose ); #elif defined(HAVE_FOPENCOOKIE) cookie_io_functions_t cio; memset(&cio, 0, sizeof(cio)); cio.read = cookie_gzread; cio.close = cookie_gzclose; return fopencookie(gzf, "r", cio); #else # error Need to implement custom I/O #endif }