105436638acc7c010349a69c3395f1a57c642dc62Ying Wang/* xmemdup0.c -- copy a block of arbitrary bytes, plus a trailing NUL 205436638acc7c010349a69c3395f1a57c642dc62Ying Wang 305436638acc7c010349a69c3395f1a57c642dc62Ying Wang Copyright (C) 2008-2012 Free Software Foundation, Inc. 405436638acc7c010349a69c3395f1a57c642dc62Ying Wang 505436638acc7c010349a69c3395f1a57c642dc62Ying Wang This program is free software: you can redistribute it and/or modify 605436638acc7c010349a69c3395f1a57c642dc62Ying Wang it under the terms of the GNU General Public License as published by 705436638acc7c010349a69c3395f1a57c642dc62Ying Wang the Free Software Foundation; either version 3 of the License, or 805436638acc7c010349a69c3395f1a57c642dc62Ying Wang (at your option) any later version. 905436638acc7c010349a69c3395f1a57c642dc62Ying Wang 1005436638acc7c010349a69c3395f1a57c642dc62Ying Wang This program is distributed in the hope that it will be useful, 1105436638acc7c010349a69c3395f1a57c642dc62Ying Wang but WITHOUT ANY WARRANTY; without even the implied warranty of 1205436638acc7c010349a69c3395f1a57c642dc62Ying Wang MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1305436638acc7c010349a69c3395f1a57c642dc62Ying Wang GNU General Public License for more details. 1405436638acc7c010349a69c3395f1a57c642dc62Ying Wang 1505436638acc7c010349a69c3395f1a57c642dc62Ying Wang You should have received a copy of the GNU General Public License 1605436638acc7c010349a69c3395f1a57c642dc62Ying Wang along with this program. If not, see <http://www.gnu.org/licenses/>. */ 1705436638acc7c010349a69c3395f1a57c642dc62Ying Wang 1805436638acc7c010349a69c3395f1a57c642dc62Ying Wang#include <config.h> 1905436638acc7c010349a69c3395f1a57c642dc62Ying Wang 2005436638acc7c010349a69c3395f1a57c642dc62Ying Wang#include "xmemdup0.h" 2105436638acc7c010349a69c3395f1a57c642dc62Ying Wang#include "xalloc.h" 2205436638acc7c010349a69c3395f1a57c642dc62Ying Wang 2305436638acc7c010349a69c3395f1a57c642dc62Ying Wang#include <string.h> 2405436638acc7c010349a69c3395f1a57c642dc62Ying Wang 2505436638acc7c010349a69c3395f1a57c642dc62Ying Wang/* Clone an arbitrary block of bytes P of size S, with error checking, 2605436638acc7c010349a69c3395f1a57c642dc62Ying Wang and include a terminating NUL byte. P is of type 'void const *', 2705436638acc7c010349a69c3395f1a57c642dc62Ying Wang to make it easier to use this with other mem* functions that return 2805436638acc7c010349a69c3395f1a57c642dc62Ying Wang 'void *', but since appending a NUL byte only makes sense on bytes, 2905436638acc7c010349a69c3395f1a57c642dc62Ying Wang the return type is 'char *'. 3005436638acc7c010349a69c3395f1a57c642dc62Ying Wang 3105436638acc7c010349a69c3395f1a57c642dc62Ying Wang The terminating NUL makes it safe to use strlen or rawmemchr to 3205436638acc7c010349a69c3395f1a57c642dc62Ying Wang check for embedded NUL; it also speeds up algorithms such as escape 3305436638acc7c010349a69c3395f1a57c642dc62Ying Wang sequence processing on arbitrary memory, by making it always safe 3405436638acc7c010349a69c3395f1a57c642dc62Ying Wang to read the byte after the escape character rather than having to 3505436638acc7c010349a69c3395f1a57c642dc62Ying Wang check if each escape character is the last byte in the object. */ 3605436638acc7c010349a69c3395f1a57c642dc62Ying Wang 3705436638acc7c010349a69c3395f1a57c642dc62Ying Wangchar * 3805436638acc7c010349a69c3395f1a57c642dc62Ying Wangxmemdup0 (void const *p, size_t s) 3905436638acc7c010349a69c3395f1a57c642dc62Ying Wang{ 4005436638acc7c010349a69c3395f1a57c642dc62Ying Wang char *result = xcharalloc (s + 1); 4105436638acc7c010349a69c3395f1a57c642dc62Ying Wang memcpy (result, p, s); 4205436638acc7c010349a69c3395f1a57c642dc62Ying Wang result[s] = 0; 4305436638acc7c010349a69c3395f1a57c642dc62Ying Wang return result; 4405436638acc7c010349a69c3395f1a57c642dc62Ying Wang} 45