1
0
mirror of https://github.com/stefanocasazza/ULib.git synced 2025-09-28 19:05:55 +08:00
This commit is contained in:
stefanocasazza 2016-08-23 19:00:31 +02:00
parent e9df7c930a
commit ee3e6f5741
8 changed files with 119 additions and 137 deletions

View File

@ -35,6 +35,9 @@
# ifndef LINUX_VERSION_CODE
# error "You need to use at least 2.0 Linux kernel."
# endif
# ifdef U_APEX_ENABLE
# include <ulib/base/apex/apex_memmove.h>
# endif
#endif
#ifdef USE_LIBSSL
@ -256,6 +259,7 @@ U_EXPORT void u_init_http_method_list(void);
U_EXPORT const char* u_basename(const char* restrict path) __pure;
U_EXPORT const char* u_getsuffix(const char* restrict path, uint32_t len) __pure;
U_EXPORT bool u_is_overlap(const char* restrict dst, const char* restrict src, size_t n);
/* conversion number to string */
extern U_EXPORT const char u_ctn2s[201];
@ -354,6 +358,27 @@ U_EXPORT uint32_t u_sprintcrtl(char* restrict buffer, unsigned char c);
U_EXPORT uint32_t u__snprintf( char* restrict buffer, uint32_t buffer_size, const char* restrict format, ...);
U_EXPORT uint32_t u__vsnprintf(char* restrict buffer, uint32_t buffer_size, const char* restrict format, va_list argp);
#ifdef DEBUG
/* NB: u_strlen() and u_memcpy() conflit with /usr/include/unicode/urename.h */
U_EXPORT size_t u__strlen(const char* restrict s, const char* function);
U_EXPORT void u__strcpy( char* restrict dest, const char* restrict src);
U_EXPORT void u__memcpy( void* restrict dest, const void* restrict src, size_t n, const char* function);
U_EXPORT char* u__strncpy( char* restrict dest, const char* restrict src, size_t n);
#else
# define u__strlen(s,func) strlen((s))
# define u__strcpy(dest,src) (void) strcpy((dest),(src))
# define u__strncpy(dest,src,n) strncpy((dest),(src),(n))
# ifdef U_APEX_ENABLE
# define u__memcpy(dest,src,n,func) (void) apex_memcpy((dest),(src),(n))
# else
# define u__memcpy(dest,src,n,func) (void) memcpy((dest),(src),(n))
# endif
#endif
#ifndef U_APEX_ENABLE
# define apex_memcpy( dest,src,n) memcpy( (dest),(src),(n))
# define apex_memmove(dest,src,n) memmove((dest),(src),(n))
#endif
#if (defined(U_LINUX) || defined(_MSWINDOWS_)) && !defined(__suseconds_t_defined)
typedef long suseconds_t;
#endif

View File

@ -51,10 +51,6 @@ typedef uint32_t in_addr_t;
# include <sys/sched.h>
#endif
#ifdef U_APEX_ENABLE
# include <ulib/base/apex/apex_memmove.h>
#endif
#ifndef HAVE_CPU_SET_T
typedef uint64_t cpu_set_t;
#endif
@ -153,32 +149,12 @@ U_EXPORT uint32_t u_memory_dump(char* restrict bp, unsigned char* restrict cp, u
U_EXPORT int u_getScreenWidth(void) __pure; /* Determine the width of the terminal we're running on */
U_EXPORT bool u_isNumber(const char* restrict s, uint32_t n) __pure;
U_EXPORT bool u_is_overlap(const char* restrict dst, const char* restrict src, size_t n);
U_EXPORT void u_printSize(char* restrict buffer, uint64_t bytes); /* print size using u_calcRate() */
U_EXPORT bool u_rmatch(const char* restrict haystack, uint32_t haystack_len, const char* restrict needle, uint32_t needle_len) __pure;
U_EXPORT uint32_t u_findEndHeader( const char* restrict s, uint32_t n) __pure; /* find sequence of U_CRLF2 or U_LF2 */
U_EXPORT uint32_t u_findEndHeader1(const char* restrict s, uint32_t n) __pure; /* find sequence of U_CRLF2 */
#ifdef DEBUG
/* NB: u_strlen() and u_memcpy() conflit with /usr/include/unicode/urename.h */
U_EXPORT size_t u__strlen(const char* restrict s, const char* function);
U_EXPORT void u__strcpy( char* restrict dest, const char* restrict src);
U_EXPORT void u__memcpy( void* restrict dest, const void* restrict src, size_t n, const char* function);
U_EXPORT char* u__strncpy( char* restrict dest, const char* restrict src, size_t n);
#else
# define u__strlen(s,func) strlen((s))
# define u__strcpy(dest,src) (void) strcpy((dest),(src))
# define u__strncpy(dest,src,n) strncpy((dest),(src),(n))
# ifdef U_APEX_ENABLE
# define u__memcpy(dest,src,n,func) (void) apex_memcpy((dest),(src),(n))
# endif
#endif
#ifndef U_APEX_ENABLE
# define apex_memcpy( dest,src,n) memcpy( (dest),(src),(n))
# define apex_memmove(dest,src,n) memmove((dest),(src),(n))
#endif
static inline void* u_find(const char* restrict a, uint32_t n1, const char* restrict b, uint32_t n2) /* check if string b is contained within string a */
{
U_INTERNAL_TRACE("u_find(%.*s,%u,%.*s,%u)", U_min(n1,128), a, n1, U_min(n2,128), b, n2)

View File

@ -260,6 +260,82 @@ void u_setPid(void)
u_pid_str_len = buffer + sizeof(buffer) - u_pid_str;
}
bool u_is_overlap(const char* restrict dst, const char* restrict src, size_t n)
{
U_INTERNAL_TRACE("u_is_overlap(%p,%p,%lu)", dst, src, n)
U_INTERNAL_ASSERT_MAJOR(n, 0)
if (src < dst) return ((src + n - 1) >= dst);
else if (dst < src) return ((dst + n - 1) >= src);
/* They start at same place. Since we know neither of them has zero length, they must overlap */
U_INTERNAL_ASSERT_EQUALS(dst, src)
return true;
}
#ifdef DEBUG
size_t u__strlen(const char* restrict s, const char* called_by_function)
{
U_INTERNAL_TRACE("u__strlen(%s,%s)", s, called_by_function)
U_INTERNAL_ASSERT_POINTER(called_by_function)
U_INTERNAL_ASSERT_POINTER_MSG(s,called_by_function)
return strlen(s);
}
void u__strcpy(char* restrict dest, const char* restrict src)
{
size_t n = u__strlen(src, __PRETTY_FUNCTION__);
U_INTERNAL_TRACE("u__strcpy(%p,%p,%lu)", dest, src, n)
U_INTERNAL_ASSERT_MAJOR(n, 0)
U_INTERNAL_ASSERT_POINTER(src)
U_INTERNAL_ASSERT_POINTER(dest)
U_INTERNAL_ASSERT_EQUALS(u_is_overlap(dest,src,n), false)
(void) strcpy(dest, src);
}
char* u__strncpy(char* restrict dest, const char* restrict src, size_t n)
{
U_INTERNAL_TRACE("u__strncpy(%p,%p,%lu)", dest, src, n)
U_INTERNAL_ASSERT_MAJOR(n, 0)
U_INTERNAL_ASSERT_POINTER(src)
U_INTERNAL_ASSERT_POINTER(dest)
U_INTERNAL_ASSERT_EQUALS(u_is_overlap(dest,src,n), false)
(void) strncpy(dest, src, n);
return dest;
}
void u__memcpy(void* restrict dst, const void* restrict src, size_t n, const char* called_by_function)
{
U_INTERNAL_TRACE("u__memcpy(%p,%p,%lu,%s)", dst, src, n, called_by_function)
U_INTERNAL_ASSERT_POINTER(src)
U_INTERNAL_ASSERT_POINTER(dst)
U_INTERNAL_ASSERT_POINTER(called_by_function)
if (n == 0) U_WARNING("*** Zero copy in memcpy *** - %s", called_by_function);
if (u_is_overlap((const char* restrict)dst, (const char* restrict)src, n))
{
U_WARNING("*** Source and Destination OVERLAP in memcpy *** - %s", called_by_function);
(void) apex_memmove(dst, src, n);
}
(void) apex_memcpy(dst, src, n);
}
#endif
void u_init_ulib_username(void)
{
struct passwd* restrict pw;

View File

@ -32,13 +32,6 @@
#include <ulib/base/miniz/miniz.h>
#ifdef U_APEX_ENABLE
# include <ulib/base/apex/apex_memmove.h>
#else
# define apex_memcpy( dest,src,n) (void) memcpy( (dest),(src),(n))
# define apex_memmove(dest,src,n) (void) memmove((dest),(src),(n))
#endif
typedef unsigned char mz_validate_uint16[sizeof(mz_uint16) == 2 ? 1 : -1];
typedef unsigned char mz_validate_uint32[sizeof(mz_uint32) == 4 ? 1 : -1];
typedef unsigned char mz_validate_uint64[sizeof(mz_uint64) == 8 ? 1 : -1];
@ -470,7 +463,7 @@ int mz_inflate(mz_streamp pStream, int flush)
if (pState->m_dict_avail)
{
n = MZ_MIN(pState->m_dict_avail, pStream->avail_out);
(void) apex_memcpy(pStream->next_out, pState->m_dict + pState->m_dict_ofs, n);
(void) memcpy(pStream->next_out, pState->m_dict + pState->m_dict_ofs, n);
pStream->next_out += n;
pStream->avail_out -= n;
pStream->total_out += n;
@ -495,7 +488,7 @@ int mz_inflate(mz_streamp pStream, int flush)
pState->m_dict_avail = (mz_uint)out_bytes;
n = MZ_MIN(pState->m_dict_avail, pStream->avail_out);
(void) apex_memcpy(pStream->next_out, pState->m_dict + pState->m_dict_ofs, n);
(void) memcpy(pStream->next_out, pState->m_dict + pState->m_dict_ofs, n);
pStream->next_out += n;
pStream->avail_out -= n;
pStream->total_out += n;
@ -586,7 +579,7 @@ const char *mz_error(int err)
// ------------------- Low-level Decompression (completely independent from all compression API's)
#define TINFL_MEMCPY(d, s, l) (void) apex_memcpy(d, s, l)
#define TINFL_MEMCPY(d, s, l) (void) memcpy(d, s, l)
#define TINFL_MEMSET(p, c, l) memset(p, c, l)
#define TINFL_CR_BEGIN \
@ -1598,8 +1591,8 @@ static void tdefl_start_dynamic_block(tdefl_compressor *d)
if (d->m_huff_code_sizes[1][num_dist_codes - 1])
break;
(void) apex_memcpy(code_sizes_to_pack, &d->m_huff_code_sizes[0][0], num_lit_codes);
(void) apex_memcpy(code_sizes_to_pack + num_lit_codes, &d->m_huff_code_sizes[1][0], num_dist_codes);
(void) memcpy(code_sizes_to_pack, &d->m_huff_code_sizes[0][0], num_lit_codes);
(void) memcpy(code_sizes_to_pack + num_lit_codes, &d->m_huff_code_sizes[1][0], num_dist_codes);
total_code_sizes_to_pack = num_lit_codes + num_dist_codes;
num_packed_code_sizes = 0;
rle_z_count = 0;
@ -1961,7 +1954,7 @@ static int tdefl_flush_block(tdefl_compressor *d, int flush)
else if (pOutput_buf_start == d->m_output_buf)
{
int bytes_to_copy = (int)MZ_MIN((size_t)n, (size_t)(*d->m_pOut_buf_size - d->m_out_buf_ofs));
(void) apex_memcpy((mz_uint8 *)d->m_pOut_buf + d->m_out_buf_ofs, d->m_output_buf, bytes_to_copy);
(void) memcpy((mz_uint8 *)d->m_pOut_buf + d->m_out_buf_ofs, d->m_output_buf, bytes_to_copy);
d->m_out_buf_ofs += bytes_to_copy;
if ((n -= bytes_to_copy) != 0)
{
@ -2097,9 +2090,9 @@ static mz_bool tdefl_compress_fast(tdefl_compressor *d)
while (num_bytes_to_process)
{
mz_uint32 n = MZ_MIN(TDEFL_LZ_DICT_SIZE - dst_pos, num_bytes_to_process);
(void) apex_memcpy(d->m_dict + dst_pos, d->m_pSrc, n);
(void) memcpy(d->m_dict + dst_pos, d->m_pSrc, n);
if (dst_pos < (TDEFL_MAX_MATCH_LEN - 1))
(void) apex_memcpy(d->m_dict + TDEFL_LZ_DICT_SIZE + dst_pos, d->m_pSrc, MZ_MIN(n, (TDEFL_MAX_MATCH_LEN - 1) - dst_pos));
(void) memcpy(d->m_dict + TDEFL_LZ_DICT_SIZE + dst_pos, d->m_pSrc, MZ_MIN(n, (TDEFL_MAX_MATCH_LEN - 1) - dst_pos));
d->m_pSrc += n;
dst_pos = (dst_pos + n) & TDEFL_LZ_DICT_SIZE_MASK;
num_bytes_to_process -= n;
@ -2454,7 +2447,7 @@ static tdefl_status tdefl_flush_output_buffer(tdefl_compressor *d)
if (d->m_pOut_buf_size)
{
size_t n = MZ_MIN(*d->m_pOut_buf_size - d->m_out_buf_ofs, d->m_output_flush_remaining);
(void) apex_memcpy((mz_uint8 *)d->m_pOut_buf + d->m_out_buf_ofs, d->m_output_buf + d->m_output_flush_ofs, n);
(void) memcpy((mz_uint8 *)d->m_pOut_buf + d->m_out_buf_ofs, d->m_output_buf + d->m_output_flush_ofs, n);
d->m_output_flush_ofs += (mz_uint)n;
d->m_output_flush_remaining -= (mz_uint)n;
d->m_out_buf_ofs += n;
@ -2624,7 +2617,7 @@ static mz_bool tdefl_output_buffer_putter(const void *pBuf, int len, void *pUser
p->m_pBuf = pNew_buf;
p->m_capacity = new_capacity;
}
(void) apex_memcpy((mz_uint8 *)p->m_pBuf + p->m_size, pBuf, len);
(void) memcpy((mz_uint8 *)p->m_pBuf + p->m_size, pBuf, len);
p->m_size = new_size;
return MZ_TRUE;
}
@ -2735,7 +2728,7 @@ void *tdefl_write_image_to_png_file_in_memory_ex(const void *pImage, int w, int
c = (mz_uint32)mz_crc32(MZ_CRC32_INIT, pnghdr + 12, 17);
for (i = 0; i < 4; ++i, c <<= 8)
((mz_uint8 *)(pnghdr + 29))[i] = (mz_uint8)(c >> 24);
(void) apex_memcpy(out_buf.m_pBuf, pnghdr, 41);
(void) memcpy(out_buf.m_pBuf, pnghdr, 41);
}
// write footer (IDAT CRC-32, followed by IEND chunk)
if (!tdefl_output_buffer_putter("\0\0\0\0\0\0\0\0\x49\x45\x4e\x44\xae\x42\x60\x82", 16, &out_buf))

View File

@ -71,82 +71,6 @@ const char* u_short_units[] = { "B", "KB", "MB", "GB", "TB", 0 };
uint32_t u_m_w = 521288629,
u_m_z = 362436069;
bool u_is_overlap(const char* restrict dst, const char* restrict src, size_t n)
{
U_INTERNAL_TRACE("u_is_overlap(%p,%p,%lu)", dst, src, n)
U_INTERNAL_ASSERT_MAJOR(n, 0)
if (src < dst) return ((src + n - 1) >= dst);
else if (dst < src) return ((dst + n - 1) >= src);
/* They start at same place. Since we know neither of them has zero length, they must overlap */
U_INTERNAL_ASSERT_EQUALS(dst, src)
return true;
}
#ifdef DEBUG
size_t u__strlen(const char* restrict s, const char* called_by_function)
{
U_INTERNAL_TRACE("u__strlen(%s,%s)", s, called_by_function)
U_INTERNAL_ASSERT_POINTER(called_by_function)
U_INTERNAL_ASSERT_POINTER_MSG(s,called_by_function)
return strlen(s);
}
void u__strcpy(char* restrict dest, const char* restrict src)
{
size_t n = u__strlen(src, __PRETTY_FUNCTION__);
U_INTERNAL_TRACE("u__strcpy(%p,%p,%lu)", dest, src, n)
U_INTERNAL_ASSERT_MAJOR(n, 0)
U_INTERNAL_ASSERT_POINTER(src)
U_INTERNAL_ASSERT_POINTER(dest)
U_INTERNAL_ASSERT_EQUALS(u_is_overlap(dest,src,n), false)
(void) strcpy(dest, src);
}
char* u__strncpy(char* restrict dest, const char* restrict src, size_t n)
{
U_INTERNAL_TRACE("u__strncpy(%p,%p,%lu)", dest, src, n)
U_INTERNAL_ASSERT_MAJOR(n, 0)
U_INTERNAL_ASSERT_POINTER(src)
U_INTERNAL_ASSERT_POINTER(dest)
U_INTERNAL_ASSERT_EQUALS(u_is_overlap(dest,src,n), false)
(void) strncpy(dest, src, n);
return dest;
}
void u__memcpy(void* restrict dst, const void* restrict src, size_t n, const char* called_by_function)
{
U_INTERNAL_TRACE("u__memcpy(%p,%p,%lu,%s)", dst, src, n, called_by_function)
U_INTERNAL_ASSERT_POINTER(src)
U_INTERNAL_ASSERT_POINTER(dst)
U_INTERNAL_ASSERT_POINTER(called_by_function)
if (n == 0) U_WARNING("*** Zero copy in memcpy *** - %s", called_by_function);
if (u_is_overlap((const char* restrict)dst, (const char* restrict)src, n))
{
U_WARNING("*** Source and Destination OVERLAP in memcpy *** - %s", called_by_function);
(void) apex_memmove(dst, src, n);
}
(void) apex_memcpy(dst, src, n);
}
#endif
__pure long u_strtol(const char* restrict s, const char* restrict e)
{
bool neg;

View File

@ -102,12 +102,6 @@ You can contact the author at :
# endif /* __STDC_VERSION__ */
#endif
#ifdef U_APEX_ENABLE
# include <ulib/base/apex/apex_memmove.h>
#else
# define apex_memcpy( dest,src,n) memcpy((dest),(src),(n))
#endif
/* *************************************
* Includes & Memory related functions
***************************************/
@ -118,7 +112,7 @@ static void* XXH_malloc(size_t s) { return malloc(s); }
static void XXH_free (void* p) { free(p); }
/* for memcpy() */
#include <string.h>
static void* XXH_memcpy(void* dest, const void* src, size_t size) { return apex_memcpy(dest,src,size); }
static void* XXH_memcpy(void* dest, const void* src, size_t size) { return memcpy(dest,src,size); }
#include <ulib/base/xxhash/xxhash.h>
@ -168,14 +162,14 @@ static U64 XXH_read64(const void* ptr) { return ((const unalign*)ptr)->u64; }
static U32 XXH_read32(const void* memPtr)
{
U32 val;
(void) apex_memcpy(&val, memPtr, sizeof(val));
(void) memcpy(&val, memPtr, sizeof(val));
return val;
}
static U64 XXH_read64(const void* memPtr)
{
U64 val;
(void) apex_memcpy(&val, memPtr, sizeof(val));
(void) memcpy(&val, memPtr, sizeof(val));
return val;
}
@ -617,7 +611,7 @@ XXH_PUBLIC_API XXH_errorcode XXH32_reset(XXH32_state_t* statePtr, unsigned int s
state.v2 = seed + PRIME32_2;
state.v3 = seed + 0;
state.v4 = seed - PRIME32_1;
(void) apex_memcpy(statePtr, &state, sizeof(state));
(void) memcpy(statePtr, &state, sizeof(state));
return XXH_OK;
}
@ -631,7 +625,7 @@ XXH_PUBLIC_API XXH_errorcode XXH64_reset(XXH64_state_t* statePtr, unsigned long
state.v2 = seed + PRIME64_2;
state.v3 = seed + 0;
state.v4 = seed - PRIME64_1;
(void) apex_memcpy(statePtr, &state, sizeof(state));
(void) memcpy(statePtr, &state, sizeof(state));
return XXH_OK;
}
@ -997,14 +991,14 @@ XXH_PUBLIC_API void XXH32_canonicalFromHash(XXH32_canonical_t* dst, XXH32_hash_t
{
XXH_STATIC_ASSERT(sizeof(XXH32_canonical_t) == sizeof(XXH32_hash_t));
if (XXH_CPU_LITTLE_ENDIAN) hash = XXH_swap32(hash);
(void) apex_memcpy(dst, &hash, sizeof(*dst));
(void) memcpy(dst, &hash, sizeof(*dst));
}
XXH_PUBLIC_API void XXH64_canonicalFromHash(XXH64_canonical_t* dst, XXH64_hash_t hash)
{
XXH_STATIC_ASSERT(sizeof(XXH64_canonical_t) == sizeof(XXH64_hash_t));
if (XXH_CPU_LITTLE_ENDIAN) hash = XXH_swap64(hash);
(void) apex_memcpy(dst, &hash, sizeof(*dst));
(void) memcpy(dst, &hash, sizeof(*dst));
}
XXH_PUBLIC_API XXH32_hash_t XXH32_hashFromCanonical(const XXH32_canonical_t* src)

View File

@ -142,7 +142,6 @@ plugin/product1.lo: plugin/product1.cpp /usr/include/stdc-predef.h \
../../include/ulib/debug/macro.h ../../include/ulib/debug/trace.h \
../../include/ulib/base/trace.h ../../include/ulib/base/utility.h \
/usr/include/dirent.h /usr/include/bits/dirent.h /usr/include/fnmatch.h \
../../include/ulib/base/apex/apex_memmove.h \
../../include/ulib/debug/error_simulation.h /usr/include/tdb.h \
/usr/lib/gcc/x86_64-pc-linux-gnu/6.2.0/include/stdbool.h \
../../include/ulib/debug/common.h ../../include/ulib/debug/error.h \
@ -546,8 +545,6 @@ plugin/product.h:
/usr/include/fnmatch.h:
../../include/ulib/base/apex/apex_memmove.h:
../../include/ulib/debug/error_simulation.h:
/usr/include/tdb.h:

View File

@ -142,7 +142,6 @@ plugin/product2.lo: plugin/product2.cpp /usr/include/stdc-predef.h \
../../include/ulib/debug/macro.h ../../include/ulib/debug/trace.h \
../../include/ulib/base/trace.h ../../include/ulib/base/utility.h \
/usr/include/dirent.h /usr/include/bits/dirent.h /usr/include/fnmatch.h \
../../include/ulib/base/apex/apex_memmove.h \
../../include/ulib/debug/error_simulation.h /usr/include/tdb.h \
/usr/lib/gcc/x86_64-pc-linux-gnu/6.2.0/include/stdbool.h \
../../include/ulib/debug/common.h ../../include/ulib/debug/error.h \
@ -546,8 +545,6 @@ plugin/product.h:
/usr/include/fnmatch.h:
../../include/ulib/base/apex/apex_memmove.h:
../../include/ulib/debug/error_simulation.h:
/usr/include/tdb.h: