utf8rewind  1.5.0
System library for processing UTF-8 encoded text
base.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 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_SET_ERROR(_error) \
45  if (errors != 0) { *errors = UTF8_ERR_ ## _error; }
46 
47 /* Validates input before transforming */
48 /* Check for parameter overlap using the separating axis theorem */
49 
50 #define UTF8_VALIDATE_PARAMETERS_CHAR(_inputType, _result) \
51  if (input == 0) { \
52  UTF8_SET_ERROR(INVALID_DATA); \
53  return _result; \
54  } \
55  else if (inputSize < sizeof(_inputType)) { \
56  if (target != 0) { \
57  if (targetSize < 3) { \
58  UTF8_SET_ERROR(NOT_ENOUGH_SPACE); \
59  return _result; \
60  } \
61  memcpy(target, REPLACEMENT_CHARACTER_STRING, REPLACEMENT_CHARACTER_STRING_LENGTH); \
62  } \
63  UTF8_SET_ERROR(INVALID_DATA); \
64  return _result + REPLACEMENT_CHARACTER_STRING_LENGTH; \
65  } \
66  if (target != 0 && targetSize == 0) { \
67  UTF8_SET_ERROR(NOT_ENOUGH_SPACE); \
68  return _result; \
69  } \
70  if ((char*)input == target) { \
71  UTF8_SET_ERROR(OVERLAPPING_PARAMETERS); \
72  return _result; \
73  } \
74  { \
75  char* input_center = (char*)input + (inputSize / 2); \
76  char* target_center = target + (targetSize / 2); \
77  size_t delta = (size_t)((input_center > target_center) ? (input_center - target_center) : (target_center - input_center)); \
78  if (delta < (inputSize + targetSize) / 2) { \
79  UTF8_SET_ERROR(OVERLAPPING_PARAMETERS); \
80  return _result; \
81  } \
82  }
83 
84 #define UTF8_VALIDATE_PARAMETERS(_inputType, _outputType, _result) \
85  if (input == 0) { \
86  UTF8_SET_ERROR(INVALID_DATA); \
87  return _result; \
88  } \
89  else if (inputSize < sizeof(_inputType)) { \
90  if (target != 0) { \
91  if (targetSize < sizeof(_outputType)) { \
92  UTF8_SET_ERROR(NOT_ENOUGH_SPACE); \
93  return _result; \
94  } \
95  *target = REPLACEMENT_CHARACTER; \
96  } \
97  UTF8_SET_ERROR(INVALID_DATA); \
98  return _result + sizeof(_outputType); \
99  } \
100  if (target != 0 && targetSize < sizeof(_outputType)) { \
101  UTF8_SET_ERROR(NOT_ENOUGH_SPACE); \
102  return _result; \
103  } \
104  if ((char*)input == (char*)target) { \
105  UTF8_SET_ERROR(OVERLAPPING_PARAMETERS); \
106  return _result; \
107  } \
108  { \
109  char* input_center = (char*)input + (inputSize / 2); \
110  char* target_center = (char*)target + (targetSize / 2); \
111  size_t delta = (size_t)((input_center > target_center) ? (input_center - target_center) : (target_center - input_center)); \
112  if (delta < (inputSize + targetSize) / 2) { \
113  UTF8_SET_ERROR(OVERLAPPING_PARAMETERS); \
114  return _result; \
115  } \
116  }
117 
120 #endif /* _UTF8REWIND_INTERNAL_BASE_H_ */
Public interface for UTF-8 functions.