17ec681f3Smrg/* 27ec681f3Smrg * Copyright 2020 Lag Free Games, LLC 37ec681f3Smrg * All Rights Reserved. 47ec681f3Smrg * 57ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a 67ec681f3Smrg * copy of this software and associated documentation files (the 77ec681f3Smrg * "Software"), to deal in the Software without restriction, including 87ec681f3Smrg * without limitation the rights to use, copy, modify, merge, publish, 97ec681f3Smrg * distribute, sub license, and/or sell copies of the Software, and to 107ec681f3Smrg * permit persons to whom the Software is furnished to do so, subject to 117ec681f3Smrg * the following conditions: 127ec681f3Smrg * 137ec681f3Smrg * The above copyright notice and this permission notice (including the 147ec681f3Smrg * next paragraph) shall be included in all copies or substantial portions 157ec681f3Smrg * of the Software. 167ec681f3Smrg * 177ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 187ec681f3Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 197ec681f3Smrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 207ec681f3Smrg * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR 217ec681f3Smrg * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 227ec681f3Smrg * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 237ec681f3Smrg * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 247ec681f3Smrg */ 257ec681f3Smrg 267ec681f3Smrg#include "memstream.h" 277ec681f3Smrg 287ec681f3Smrg#include <stdlib.h> 297ec681f3Smrg 307ec681f3Smrgbool 317ec681f3Smrgu_memstream_open(struct u_memstream *mem, char **bufp, size_t *sizep) 327ec681f3Smrg{ 337ec681f3Smrg#if defined(_WIN32) 347ec681f3Smrg bool success = false; 357ec681f3Smrg 367ec681f3Smrg char path[MAX_PATH]; 377ec681f3Smrg DWORD dwResult = GetTempPath(MAX_PATH, path); 387ec681f3Smrg if ((dwResult > 0) && (dwResult < MAX_PATH)) { 397ec681f3Smrg char *temp = mem->temp; 407ec681f3Smrg UINT uResult = GetTempFileName(path, "MEMSTREAM", 0, temp); 417ec681f3Smrg if (uResult != 0) { 427ec681f3Smrg FILE *f = fopen(temp, "w+b"); 437ec681f3Smrg success = f != NULL; 447ec681f3Smrg if (success) 457ec681f3Smrg { 467ec681f3Smrg mem->f = f; 477ec681f3Smrg mem->bufp = bufp; 487ec681f3Smrg mem->sizep = sizep; 497ec681f3Smrg } 507ec681f3Smrg } 517ec681f3Smrg } 527ec681f3Smrg 537ec681f3Smrg return success; 547ec681f3Smrg#elif defined(__APPLE__) 557ec681f3Smrg return false; 567ec681f3Smrg#else 577ec681f3Smrg FILE *const f = open_memstream(bufp, sizep); 587ec681f3Smrg mem->f = f; 597ec681f3Smrg return f != NULL; 607ec681f3Smrg#endif 617ec681f3Smrg} 627ec681f3Smrg 637ec681f3Smrgvoid 647ec681f3Smrgu_memstream_close(struct u_memstream *mem) 657ec681f3Smrg{ 667ec681f3Smrg FILE *const f = mem->f; 677ec681f3Smrg 687ec681f3Smrg#ifdef _WIN32 697ec681f3Smrg long size = ftell(f); 707ec681f3Smrg if (size > 0) { 717ec681f3Smrg char *buf = malloc(size); 727ec681f3Smrg fseek(f, 0, SEEK_SET); 737ec681f3Smrg fread(buf, 1, size, f); 747ec681f3Smrg 757ec681f3Smrg *mem->bufp = buf; 767ec681f3Smrg *mem->sizep = size; 777ec681f3Smrg } 787ec681f3Smrg 797ec681f3Smrg remove(mem->temp); 807ec681f3Smrg#endif 817ec681f3Smrg 827ec681f3Smrg fclose(f); 837ec681f3Smrg} 84