/** * @brief duplicate a string and return it */ tchar * tcsdup(const tchar *s) { size_t len; tchar *copy; len = tcslen(s) + 1; copy = xmalloc(len * sizeof *copy); tmemcpy(copy, s, len); return copy; }
LPTSTR SimpleHeap::Malloc(LPTSTR aBuf, size_t aLength) // v1.0.44.14: Added aLength to improve performance in cases where callers already know the length. // If aLength is at its default of -1, the length will be calculated here. // Caller must ensure that aBuf isn't NULL. { if (!aBuf || !*aBuf) // aBuf is checked for NULL because it's not worth avoiding it for such a low-level, frequently-called function. return _T(""); // Return the constant empty string to the caller (not aBuf itself since that might be volatile). if (aLength == -1) // Caller wanted us to calculate it. Compare directly to -1 since aLength is unsigned. aLength = _tcslen(aBuf); LPTSTR new_buf; if (!(new_buf = (LPTSTR)SimpleHeap::Malloc((aLength + 1) * sizeof(TCHAR)))) // +1 for the zero terminator. { g_script.ScriptError(ERR_OUTOFMEM, aBuf); return NULL; // Callers may rely on NULL vs. "" being returned in the event of failure. } if (aLength) tmemcpy(new_buf, aBuf, aLength); // memcpy() typically benchmarks slightly faster than strcpy(). //else only a terminator is needed. new_buf[aLength] = '\0'; // Terminate here for when aLength==0 and for the memcpy above so that caller's aBuf doesn't have to be terminated. return new_buf; }
static int compress_file(struct deflate_compressor *compressor, const tchar *path, const struct options *options) { tchar *newpath = NULL; struct file_stream in; struct file_stream out; struct stat stbuf; int ret; int ret2; if (path != NULL && !options->to_stdout) { size_t path_nchars, suffix_nchars; if (!options->force && has_suffix(path, options->suffix)) { msg("%"TS": already has .%"TS" suffix -- skipping", path, options->suffix); ret = -2; goto out; } path_nchars = tstrlen(path); suffix_nchars = tstrlen(options->suffix); newpath = xmalloc((path_nchars + 1 + suffix_nchars + 1) * sizeof(tchar)); tmemcpy(newpath, path, path_nchars); newpath[path_nchars] = '.'; tmemcpy(&newpath[path_nchars + 1], options->suffix, suffix_nchars + 1); } ret = xopen_for_read(path, &in); if (ret != 0) goto out_free_newpath; ret = stat_file(&in, &stbuf, options->force || newpath == NULL); if (ret != 0) goto out_close_in; ret = xopen_for_write(newpath, options->force, &out); if (ret != 0) goto out_close_in; if (!options->force && isatty(out.fd)) { msg("Refusing to write compressed data to terminal. " "Use -f to override.\nFor help, use -h."); ret = -1; goto out_close_out; } ret = map_file_contents(&in, stbuf.st_size); if (ret) goto out_close_out; ret = do_compress(compressor, &in, &out); if (ret != 0) goto out_close_out; if (path != NULL && newpath != NULL) restore_metadata(&out, newpath, &stbuf); ret = 0; out_close_out: ret2 = xclose(&out); if (ret == 0) ret = ret2; if (ret != 0 && newpath != NULL) tunlink(newpath); out_close_in: xclose(&in); if (ret == 0 && path != NULL && newpath != NULL && !options->keep) tunlink(path); out_free_newpath: free(newpath); out: return ret; }
static int decompress_file(struct deflate_decompressor *decompressor, const tchar *path, const struct options *options) { tchar *newpath = NULL; struct file_stream in; struct file_stream out; struct stat stbuf; int ret; int ret2; if (path != NULL && !options->to_stdout) { const tchar *suffix = get_suffix(path, options->suffix); if (suffix == NULL) { msg("\"%"TS"\" does not end with the .%"TS" suffix -- " "skipping", path, options->suffix); ret = -2; goto out; } newpath = xmalloc((suffix - path + 1) * sizeof(tchar)); tmemcpy(newpath, path, suffix - path); newpath[suffix - path] = '\0'; } ret = xopen_for_read(path, &in); if (ret != 0) goto out_free_newpath; if (!options->force && isatty(in.fd)) { msg("Refusing to read compressed data from terminal. " "Use -f to override.\nFor help, use -h."); ret = -1; goto out_close_in; } ret = stat_file(&in, &stbuf, options->force || newpath == NULL); if (ret != 0) goto out_close_in; ret = xopen_for_write(newpath, options->force, &out); if (ret != 0) goto out_close_in; ret = map_file_contents(&in, stbuf.st_size); if (ret != 0) goto out_close_out; ret = do_decompress(decompressor, &in, &out); if (ret != 0) goto out_close_out; if (path != NULL && newpath != NULL) restore_metadata(&out, newpath, &stbuf); ret = 0; out_close_out: ret2 = xclose(&out); if (ret == 0) ret = ret2; if (ret != 0 && newpath != NULL) tunlink(newpath); out_close_in: xclose(&in); if (ret == 0 && path != NULL && newpath != NULL && !options->keep) tunlink(path); out_free_newpath: free(newpath); out: return ret; }