attribute_compat_text_section _IO_old_fopencookie (void *cookie, const char *mode, _IO_cookie_io_functions_t io_functions) { _IO_FILE *ret; ret = _IO_fopencookie (cookie, mode, io_functions); if (ret != NULL) _IO_JUMPS_FILE_plus (ret) = &_IO_old_cookie_jumps; return ret; }
FILE * __fmemopen (void *buf, size_t len, const char *mode) { cookie_io_functions_t iof; fmemopen_cookie_t *c; c = (fmemopen_cookie_t *) calloc (sizeof (fmemopen_cookie_t), 1); if (c == NULL) return NULL; c->mybuffer = (buf == NULL); if (c->mybuffer) { c->buffer = (char *) malloc (len); if (c->buffer == NULL) { free (c); return NULL; } c->buffer[0] = '\0'; } else { if (__glibc_unlikely ((uintptr_t) len > -(uintptr_t) buf)) { free (c); __set_errno (EINVAL); return NULL; } c->buffer = buf; /* POSIX states that w+ mode should truncate the buffer. */ if (mode[0] == 'w' && mode[1] == '+') c->buffer[0] = '\0'; if (mode[0] == 'a') c->maxpos = strnlen (c->buffer, len); } /* Mode | starting position (cookie::pos) | size (cookie::size) ------ |----------------------------------|----------------------------- read | beginning of the buffer | size argument write | beginning of the buffer | zero append | first null or size buffer + 1 | first null or size argument */ c->size = len; if (mode[0] == 'r') c->maxpos = len; c->append = mode[0] == 'a'; if (c->append) c->pos = c->maxpos; else c->pos = 0; iof.read = fmemopen_read; iof.write = fmemopen_write; iof.seek = fmemopen_seek; iof.close = fmemopen_close; return _IO_fopencookie (c, mode, iof); }
FILE * fmemopen (void *buf, size_t len, const char *mode) { cookie_io_functions_t iof; fmemopen_cookie_t *c; if (__builtin_expect (len == 0, 0)) { einval: __set_errno (EINVAL); return NULL; } c = (fmemopen_cookie_t *) malloc (sizeof (fmemopen_cookie_t)); if (c == NULL) return NULL; c->mybuffer = (buf == NULL); if (c->mybuffer) { c->buffer = (char *) malloc (len); if (c->buffer == NULL) { free (c); return NULL; } c->buffer[0] = '\0'; } else { if (__builtin_expect ((uintptr_t) len > -(uintptr_t) buf, 0)) { free (c); goto einval; } c->buffer = buf; } c->size = len; if (mode[0] == 'w') c->buffer[0] = '\0'; c->maxpos = strlen (c->buffer); if (mode[0] == 'a') c->pos = c->maxpos; else c->pos = 0; c->binmode = mode[0] != '\0' && mode[1] == 'b'; iof.read = fmemopen_read; iof.write = fmemopen_write; iof.seek = fmemopen_seek; iof.close = fmemopen_close; return _IO_fopencookie (c, mode, iof); }
FILE * __old_fmemopen (void *buf, size_t len, const char *mode) { cookie_io_functions_t iof; fmemopen_cookie_t *c; FILE *result; if (__glibc_unlikely (len == 0)) { einval: __set_errno (EINVAL); return NULL; } c = (fmemopen_cookie_t *) malloc (sizeof (fmemopen_cookie_t)); if (c == NULL) return NULL; c->mybuffer = (buf == NULL); if (c->mybuffer) { c->buffer = (char *) malloc (len); if (c->buffer == NULL) { free (c); return NULL; } c->buffer[0] = '\0'; c->maxpos = 0; } else { if (__glibc_unlikely ((uintptr_t) len > -(uintptr_t) buf)) { free (c); goto einval; } c->buffer = buf; if (mode[0] == 'w') c->buffer[0] = '\0'; c->maxpos = strnlen (c->buffer, len); } c->size = len; if (mode[0] == 'a') c->pos = c->maxpos; else c->pos = 0; c->binmode = mode[0] != '\0' && mode[1] == 'b'; iof.read = fmemopen_read; iof.write = fmemopen_write; iof.seek = fmemopen_seek; iof.close = fmemopen_close; result = _IO_fopencookie (c, mode, iof); if (__glibc_unlikely (result == NULL)) { if (c->mybuffer) free (c->buffer); free (c); } return result; }