diff --git a/components/esp_libc/CMakeLists.txt b/components/esp_libc/CMakeLists.txt index 5f3beec8a04..83fe92efe3d 100644 --- a/components/esp_libc/CMakeLists.txt +++ b/components/esp_libc/CMakeLists.txt @@ -77,7 +77,6 @@ else() list(APPEND srcs "src/picolibc/picolibc_init.c" "src/picolibc/rand.c" - "src/picolibc/open_memstream.c" "src/picolibc/errno.c" "src/picolibc/bufio_setvbuf.c") # TODO IDF-15494: remove this and the following lines list(APPEND EXTRA_LINK_FLAGS "-Wl,--wrap=__bufio_setvbuf") diff --git a/components/esp_libc/src/picolibc/open_memstream.c b/components/esp_libc/src/picolibc/open_memstream.c deleted file mode 100644 index 051a07b0d34..00000000000 --- a/components/esp_libc/src/picolibc/open_memstream.c +++ /dev/null @@ -1,128 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ -#include -#include -#include -#include -#include - -#define GET_BUF(mf) (*(mf->pbuf)) -#define GET_SIZE(mf) (*(mf->psize)) - -struct __file_mem { - struct __file_ext xfile; - char **pbuf; - size_t *psize; // == strlen(*pbuf); - size_t len; // Total allocated size - size_t pos; -}; - -static int __fmem_put(char c, FILE *f) -{ - struct __file_mem *mf = (struct __file_mem *) f; - if (mf->len == mf->pos) { - size_t newsize = mf->len * 3 / 2; - char *tmp = realloc(GET_BUF(mf), newsize); - if (!tmp) { - return _FDEV_ERR; - } - GET_BUF(mf) = tmp; - mf->len = newsize; - } - GET_BUF(mf)[mf->pos++] = c; - if (mf->pos > GET_SIZE(mf)) { - GET_SIZE(mf) = mf->pos; - } - return (unsigned char) c; -} - -static int __fmem_get(FILE *f) -{ - struct __file_mem *mf = (struct __file_mem *) f; - int c; - if (mf->pos < GET_SIZE(mf)) { - c = (unsigned char) GET_BUF(mf)[mf->pos++]; - if (c == '\0') { - c = _FDEV_EOF; - } - } else { - c = _FDEV_ERR; - } - return c; -} - -static int __fmem_flush(FILE *f) -{ - struct __file_mem *mf = (struct __file_mem *) f; - GET_BUF(mf)[mf->pos] = '\0'; - GET_SIZE(mf) = mf->pos; - return 0; -} - -static off_t __fmem_seek(FILE *f, off_t pos, int whence) -{ - struct __file_mem *mf = (struct __file_mem *) f; - - switch (whence) { - case SEEK_SET: - break; - case SEEK_CUR: - pos += mf->pos; - break; - case SEEK_END: - pos += GET_SIZE(mf); - break; - } - if (pos < 0 || GET_SIZE(mf) < (size_t) pos) { - return EOF; - } - mf->pos = pos; - return pos; -} - -static int __fmem_close(FILE *f) -{ - __fmem_flush(f); - free(f); - return 0; -} - -FILE * -open_memstream(char **buf, size_t *size) -{ - struct __file_mem *mf; - - if (!buf || !size) { - errno = EINVAL; - return NULL; - } - /* Allocate file structure and necessary buffers */ - mf = calloc(1, sizeof(struct __file_mem)); - - if (mf == NULL) { - errno = ENOMEM; - return NULL; - } - - *buf = malloc(BUFSIZ); - if (*buf == NULL) { - errno = ENOMEM; - free(mf); - return NULL; - } - - *mf = (struct __file_mem) { - .xfile = FDEV_SETUP_EXT(__fmem_put, __fmem_get, __fmem_flush, __fmem_close, - __fmem_seek, NULL, __SWR), - .pbuf = buf, - .psize = size, - .len = BUFSIZ, - .pos = 0, - }; - *size = 0; - *buf[0] = '\0'; - return (FILE *) mf; -} diff --git a/components/esp_libc/test_apps/newlib/main/CMakeLists.txt b/components/esp_libc/test_apps/newlib/main/CMakeLists.txt index 09ead453fc0..845cd944aeb 100644 --- a/components/esp_libc/test_apps/newlib/main/CMakeLists.txt +++ b/components/esp_libc/test_apps/newlib/main/CMakeLists.txt @@ -3,7 +3,6 @@ set(srcs "test_atomic.c" "test_locks.c" "test_misc.c" - "test_memstream.c" "test_newlib.c" "test_printf.c" "test_setjmp.c" diff --git a/components/esp_libc/test_apps/newlib/main/test_memstream.c b/components/esp_libc/test_apps/newlib/main/test_memstream.c deleted file mode 100644 index 06d6e70b1ad..00000000000 --- a/components/esp_libc/test_apps/newlib/main/test_memstream.c +++ /dev/null @@ -1,72 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Unlicense OR CC0-1.0 - */ -#include -#include -#include -#include -#include -#include -#include -#include "test_utils.h" - -TEST_CASE("memstream_fseek", "[newlib]") -{ - FILE *stream; - char *buf; - size_t len; - off_t eob; - - stream = open_memstream(&buf, &len); - fprintf(stream, "Hello Espressif!"); - fflush(stream); - TEST_ASSERT_EQUAL_STRING("Hello Espressif!", buf); - TEST_ASSERT_EQUAL_UINT32(strlen("Hello Espressif!"), len); - eob = ftell(stream); - fseek(stream, 0, SEEK_SET); - fprintf(stream, "HELLO"); - fseek(stream, eob, SEEK_SET); - fclose(stream); - TEST_ASSERT_EQUAL_STRING("HELLO Espressif!", buf); - TEST_ASSERT_EQUAL_UINT32(strlen("HELLO Espressif!"), len); - free(buf); -} - -TEST_CASE("memstream_overwrite", "[newlib]") -{ - FILE *stream; - char *buf; - size_t len; - - stream = open_memstream(&buf, &len); - fprintf(stream, "Hello Espressif!"); - fflush(stream); - TEST_ASSERT_EQUAL_STRING("Hello Espressif!", buf); - TEST_ASSERT_EQUAL_UINT32(strlen("Hello Espressif!"), len); - fseek(stream, 0, SEEK_SET); - fprintf(stream, "HELLO"); - fclose(stream); - TEST_ASSERT_EQUAL_STRING("HELLO", buf); - TEST_ASSERT_EQUAL_UINT32(strlen("HELLO"), len); - free(buf); -} - -TEST_CASE("memstream_realloc", "[newlib]") -{ - FILE *stream; - char *buf; - size_t len; - - stream = open_memstream(&buf, &len); - // Initial buf size is BUFSIZ. Try to go out of bounds. - for (int i = 0; i < BUFSIZ * 2; i++) { - fputc('A' + (i % ('Z' - 'A')), stream); - } - fclose(stream); - for (int i = 0; i < BUFSIZ * 2; i++) { - TEST_ASSERT_EQUAL_UINT32('A' + (i % ('Z' - 'A')), buf[i]); - } - free(buf); -}