| 1 | /* Copyright (C) 1995-2020 Free Software Foundation, Inc. | 
|---|
| 2 | This file is part of the GNU C Library. | 
|---|
| 3 |  | 
|---|
| 4 | The GNU C Library is free software; you can redistribute it and/or | 
|---|
| 5 | modify it under the terms of the GNU Lesser General Public | 
|---|
| 6 | License as published by the Free Software Foundation; either | 
|---|
| 7 | version 2.1 of the License, or (at your option) any later version. | 
|---|
| 8 |  | 
|---|
| 9 | The GNU C Library is distributed in the hope that it will be useful, | 
|---|
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
|---|
| 12 | Lesser General Public License for more details. | 
|---|
| 13 |  | 
|---|
| 14 | You should have received a copy of the GNU Lesser General Public | 
|---|
| 15 | License along with the GNU C Library; if not, see | 
|---|
| 16 | <https://www.gnu.org/licenses/>.  */ | 
|---|
| 17 |  | 
|---|
| 18 | #include "libioP.h" | 
|---|
| 19 | #include "strfile.h" | 
|---|
| 20 | #include <stdio.h> | 
|---|
| 21 | #include <stdlib.h> | 
|---|
| 22 | #include <wchar.h> | 
|---|
| 23 |  | 
|---|
| 24 |  | 
|---|
| 25 | struct _IO_FILE_wmemstream | 
|---|
| 26 | { | 
|---|
| 27 | _IO_strfile _sf; | 
|---|
| 28 | wchar_t **bufloc; | 
|---|
| 29 | size_t *sizeloc; | 
|---|
| 30 | }; | 
|---|
| 31 |  | 
|---|
| 32 |  | 
|---|
| 33 | static int _IO_wmem_sync (FILE* fp) __THROW; | 
|---|
| 34 | static void _IO_wmem_finish (FILE* fp, int) __THROW; | 
|---|
| 35 |  | 
|---|
| 36 |  | 
|---|
| 37 | static const struct _IO_jump_t _IO_wmem_jumps libio_vtable = | 
|---|
| 38 | { | 
|---|
| 39 | JUMP_INIT_DUMMY, | 
|---|
| 40 | JUMP_INIT (finish, _IO_wmem_finish), | 
|---|
| 41 | JUMP_INIT (overflow, (_IO_overflow_t) _IO_wstr_overflow), | 
|---|
| 42 | JUMP_INIT (underflow, (_IO_underflow_t) _IO_wstr_underflow), | 
|---|
| 43 | JUMP_INIT (uflow, (_IO_underflow_t) _IO_wdefault_uflow), | 
|---|
| 44 | JUMP_INIT (pbackfail, (_IO_pbackfail_t) _IO_wstr_pbackfail), | 
|---|
| 45 | JUMP_INIT (xsputn, _IO_wdefault_xsputn), | 
|---|
| 46 | JUMP_INIT (xsgetn, _IO_wdefault_xsgetn), | 
|---|
| 47 | JUMP_INIT (seekoff, _IO_wstr_seekoff), | 
|---|
| 48 | JUMP_INIT (seekpos, _IO_default_seekpos), | 
|---|
| 49 | JUMP_INIT (setbuf, _IO_default_setbuf), | 
|---|
| 50 | JUMP_INIT (sync, _IO_wmem_sync), | 
|---|
| 51 | JUMP_INIT (doallocate, _IO_wdefault_doallocate), | 
|---|
| 52 | JUMP_INIT (read, _IO_default_read), | 
|---|
| 53 | JUMP_INIT (write, _IO_default_write), | 
|---|
| 54 | JUMP_INIT (seek, _IO_default_seek), | 
|---|
| 55 | JUMP_INIT (close, _IO_default_close), | 
|---|
| 56 | JUMP_INIT (stat, _IO_default_stat), | 
|---|
| 57 | JUMP_INIT (showmanyc, _IO_default_showmanyc), | 
|---|
| 58 | JUMP_INIT (imbue, _IO_default_imbue) | 
|---|
| 59 | }; | 
|---|
| 60 |  | 
|---|
| 61 | /* Open a stream that writes into a malloc'd buffer that is expanded as | 
|---|
| 62 | necessary.  *BUFLOC and *SIZELOC are updated with the buffer's location | 
|---|
| 63 | and the number of characters written on fflush or fclose.  */ | 
|---|
| 64 | FILE * | 
|---|
| 65 | open_wmemstream (wchar_t **bufloc, size_t *sizeloc) | 
|---|
| 66 | { | 
|---|
| 67 | struct locked_FILE | 
|---|
| 68 | { | 
|---|
| 69 | struct _IO_FILE_wmemstream fp; | 
|---|
| 70 | #ifdef _IO_MTSAFE_IO | 
|---|
| 71 | _IO_lock_t lock; | 
|---|
| 72 | #endif | 
|---|
| 73 | struct _IO_wide_data wd; | 
|---|
| 74 | } *new_f; | 
|---|
| 75 | wchar_t *buf; | 
|---|
| 76 |  | 
|---|
| 77 | new_f = (struct locked_FILE *) malloc (sizeof (struct locked_FILE)); | 
|---|
| 78 | if (new_f == NULL) | 
|---|
| 79 | return NULL; | 
|---|
| 80 | #ifdef _IO_MTSAFE_IO | 
|---|
| 81 | new_f->fp._sf._sbf._f._lock = &new_f->lock; | 
|---|
| 82 | #endif | 
|---|
| 83 |  | 
|---|
| 84 | buf = calloc (1, BUFSIZ); | 
|---|
| 85 | if (buf == NULL) | 
|---|
| 86 | { | 
|---|
| 87 | free (new_f); | 
|---|
| 88 | return NULL; | 
|---|
| 89 | } | 
|---|
| 90 | _IO_no_init (&new_f->fp._sf._sbf._f, 0, 0, &new_f->wd, &_IO_wmem_jumps); | 
|---|
| 91 | _IO_fwide (&new_f->fp._sf._sbf._f, 1); | 
|---|
| 92 | _IO_wstr_init_static (&new_f->fp._sf._sbf._f, buf, | 
|---|
| 93 | BUFSIZ / sizeof (wchar_t), buf); | 
|---|
| 94 | new_f->fp._sf._sbf._f._flags2 &= ~_IO_FLAGS2_USER_WBUF; | 
|---|
| 95 | new_f->fp._sf._s._allocate_buffer_unused = (_IO_alloc_type) malloc; | 
|---|
| 96 | new_f->fp._sf._s._free_buffer_unused = (_IO_free_type) free; | 
|---|
| 97 |  | 
|---|
| 98 | new_f->fp.bufloc = bufloc; | 
|---|
| 99 | new_f->fp.sizeloc = sizeloc; | 
|---|
| 100 |  | 
|---|
| 101 | /* Disable single thread optimization.  BZ 21735.  */ | 
|---|
| 102 | new_f->fp._sf._sbf._f._flags2 |= _IO_FLAGS2_NEED_LOCK; | 
|---|
| 103 |  | 
|---|
| 104 | return (FILE *) &new_f->fp._sf._sbf; | 
|---|
| 105 | } | 
|---|
| 106 |  | 
|---|
| 107 |  | 
|---|
| 108 | static int | 
|---|
| 109 | _IO_wmem_sync (FILE *fp) | 
|---|
| 110 | { | 
|---|
| 111 | struct _IO_FILE_wmemstream *mp = (struct _IO_FILE_wmemstream *) fp; | 
|---|
| 112 |  | 
|---|
| 113 | if (fp->_wide_data->_IO_write_ptr == fp->_wide_data->_IO_write_end) | 
|---|
| 114 | { | 
|---|
| 115 | _IO_wstr_overflow (fp, '\0'); | 
|---|
| 116 | --fp->_wide_data->_IO_write_ptr; | 
|---|
| 117 | } | 
|---|
| 118 |  | 
|---|
| 119 | *mp->bufloc = fp->_wide_data->_IO_write_base; | 
|---|
| 120 | *mp->sizeloc = (fp->_wide_data->_IO_write_ptr | 
|---|
| 121 | - fp->_wide_data->_IO_write_base); | 
|---|
| 122 |  | 
|---|
| 123 | return 0; | 
|---|
| 124 | } | 
|---|
| 125 |  | 
|---|
| 126 |  | 
|---|
| 127 | static void | 
|---|
| 128 | _IO_wmem_finish (FILE *fp, int dummy) | 
|---|
| 129 | { | 
|---|
| 130 | struct _IO_FILE_wmemstream *mp = (struct _IO_FILE_wmemstream *) fp; | 
|---|
| 131 |  | 
|---|
| 132 | *mp->bufloc = (wchar_t *) realloc (fp->_wide_data->_IO_write_base, | 
|---|
| 133 | (fp->_wide_data->_IO_write_ptr | 
|---|
| 134 | - fp->_wide_data->_IO_write_base + 1) | 
|---|
| 135 | * sizeof (wchar_t)); | 
|---|
| 136 | if (*mp->bufloc != NULL) | 
|---|
| 137 | { | 
|---|
| 138 | size_t len = (fp->_wide_data->_IO_write_ptr | 
|---|
| 139 | - fp->_wide_data->_IO_write_base); | 
|---|
| 140 | (*mp->bufloc)[len] = '\0'; | 
|---|
| 141 | *mp->sizeloc = len; | 
|---|
| 142 |  | 
|---|
| 143 | fp->_wide_data->_IO_buf_base = NULL; | 
|---|
| 144 | } | 
|---|
| 145 |  | 
|---|
| 146 | _IO_wstr_finish (fp, 0); | 
|---|
| 147 | } | 
|---|
| 148 |  | 
|---|