utf8rewind  1.2.0
Cross-platform library for UTF-8 encoded text
base.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2015 Quinten Lansu
3 
4  Permission is hereby granted, free of charge, to any person
5  obtaining a copy of this software and associated documentation
6  files (the "Software"), to deal in the Software without
7  restriction, including without limitation the rights to use,
8  copy, modify, merge, publish, distribute, sublicense, and/or
9  sell copies of the Software, and to permit persons to whom the
10  Software is furnished to do so, subject to the following
11  conditions:
12 
13  The above copyright notice and this permission notice shall be
14  included in all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23  OTHER DEALINGS IN THE SOFTWARE.
24 */
25 
26 #ifndef _UTF8REWIND_INTERNAL_BASE_H_
27 #define _UTF8REWIND_INTERNAL_BASE_H_
28 
36 #include "utf8rewind.h"
37 
38 #if defined(__GNUC__) && !defined(COMPILER_ICC)
39  #define UTF8_UNUSED(_parameter) _parameter __attribute__ ((unused))
40 #else
41  #define UTF8_UNUSED(_parameter) _parameter
42 #endif
43 
44 #define UTF8_RETURN(_error, _result) \
45  if (errors != 0) { *errors = UTF8_ERR_ ## _error; } \
46  return _result;
47 
48 /* Validates input before transforming */
49 /* Check for parameter overlap using the separating axis theorem */
50 
51 #define UTF8_VALIDATE_PARAMETERS(_inputType, _targetType, _result) \
52  if (input == 0 || inputSize < sizeof(_inputType)) { UTF8_RETURN(INVALID_DATA, _result); } \
53  if (target != 0 && targetSize < sizeof(_targetType)) { UTF8_RETURN(NOT_ENOUGH_SPACE, _result); } \
54  if ((char*)input == (char*)target) { UTF8_RETURN(OVERLAPPING_PARAMETERS, _result); } \
55  { \
56  char* input_center = (char*)input + (inputSize / 2); \
57  char* target_center = (char*)target + (targetSize / 2); \
58  size_t delta = (size_t)((input_center > target_center) ? (input_center - target_center) : (target_center - input_center)); \
59  if (delta < (inputSize + targetSize) / 2) { UTF8_RETURN(OVERLAPPING_PARAMETERS, _result); } \
60  }
61 
64 #endif /* _UTF8REWIND_INTERNAL_BASE_H_ */
Public interface for UTF-8 functions.