1 //
2 // Copyright (c) 2017 The Khronos Group Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 #include <string.h>
17 #if !defined(_MSC_VER)
18 #include <stdint.h>
19 #endif // !_MSC_VER
20
doReplace(char * dest,size_t destLength,const char * source,const char * stringToReplace1,const char * replaceWith1,const char * stringToReplace2,const char * replaceWith2)21 size_t doReplace(char* dest, size_t destLength, const char* source,
22 const char* stringToReplace1, const char* replaceWith1,
23 const char* stringToReplace2, const char* replaceWith2)
24 {
25 size_t copyCount = 0;
26 const char* sourcePtr = source;
27 char* destPtr = dest;
28 const char* ptr1;
29 const char* ptr2;
30 size_t nJump;
31 size_t len1, len2;
32 size_t lenReplace1, lenReplace2;
33 len1 = strlen(stringToReplace1);
34 len2 = strlen(stringToReplace2);
35 lenReplace1 = strlen(replaceWith1);
36 lenReplace2 = strlen(replaceWith2);
37 for (; copyCount < destLength && *sourcePtr;)
38 {
39 ptr1 = strstr(sourcePtr, stringToReplace1);
40 ptr2 = strstr(sourcePtr, stringToReplace2);
41 if (ptr1 != NULL && (ptr2 == NULL || ptr2 > ptr1))
42 {
43 nJump = ptr1 - sourcePtr;
44 if (((uintptr_t)ptr1 - (uintptr_t)sourcePtr)
45 > destLength - copyCount)
46 {
47 return -1;
48 }
49 copyCount += nJump;
50 strncpy(destPtr, sourcePtr, nJump);
51 destPtr += nJump;
52 sourcePtr += nJump + len1;
53 strcpy(destPtr, replaceWith1);
54 destPtr += lenReplace1;
55 }
56 else if (ptr2 != NULL && (ptr1 == NULL || ptr1 >= ptr2))
57 {
58 nJump = ptr2 - sourcePtr;
59 if (nJump > destLength - copyCount)
60 {
61 return -2;
62 }
63 copyCount += nJump;
64 strncpy(destPtr, sourcePtr, nJump);
65 destPtr += nJump;
66 sourcePtr += nJump + len2;
67 strcpy(destPtr, replaceWith2);
68 destPtr += lenReplace2;
69 }
70 else
71 {
72 nJump = strlen(sourcePtr);
73 if (nJump > destLength - copyCount)
74 {
75 return -3;
76 }
77 copyCount += nJump;
78 strcpy(destPtr, sourcePtr);
79 destPtr += nJump;
80 sourcePtr += nJump;
81 }
82 }
83 *destPtr = '\0';
84 return copyCount;
85 }
86
doSingleReplace(char * dest,size_t destLength,const char * source,const char * stringToReplace,const char * replaceWith)87 size_t doSingleReplace(char* dest, size_t destLength, const char* source,
88 const char* stringToReplace, const char* replaceWith)
89 {
90 size_t copyCount = 0;
91 const char* sourcePtr = source;
92 char* destPtr = dest;
93 const char* ptr;
94 size_t nJump;
95 size_t len;
96 size_t lenReplace;
97 len = strlen(stringToReplace);
98 lenReplace = strlen(replaceWith);
99 for (; copyCount < destLength && *sourcePtr;)
100 {
101 ptr = strstr(sourcePtr, stringToReplace);
102 if (ptr != NULL)
103 {
104 nJump = ptr - sourcePtr;
105 if (((uintptr_t)ptr - (uintptr_t)sourcePtr)
106 > destLength - copyCount)
107 {
108 return -1;
109 }
110 copyCount += nJump;
111 strncpy(destPtr, sourcePtr, nJump);
112 destPtr += nJump;
113 sourcePtr += nJump + len;
114 strcpy(destPtr, replaceWith);
115 destPtr += lenReplace;
116 }
117 else
118 {
119 nJump = strlen(sourcePtr);
120 if (nJump > destLength - copyCount)
121 {
122 return -3;
123 }
124 copyCount += nJump;
125 strcpy(destPtr, sourcePtr);
126 destPtr += nJump;
127 sourcePtr += nJump;
128 }
129 }
130 *destPtr = '\0';
131 return copyCount;
132 }
133