Created
November 27, 2023 15:17
-
-
Save UplinkCoder/480c0be25973ff6bf51d5c021b31c966 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
/* | |
' is replaced with ' | |
& is replaced with & | |
" is replaced with " | |
< is replaced with < | |
> is replaced with > | |
*/ | |
int main(int argc, const char* argv[]) | |
{ | |
int i; | |
char inputBuffer[8192]; | |
char outputBuffer[8192]; | |
int inputBufferUsed = 0; | |
int inputPos = 0; | |
int lastInputPos = 0; | |
int outputPos = 0; | |
for(i = 1; i < argc; i++) | |
{ | |
const char* arg = argv[i]; | |
inputBufferUsed += snprintf(inputBuffer + inputBufferUsed, | |
sizeof(inputBuffer) - inputBufferUsed, | |
"%s\n", arg | |
); | |
} | |
while(inputPos < inputBufferUsed) | |
{ | |
const char* ampBeg = memchr(inputBuffer + inputPos, '&', inputBufferUsed - inputPos); | |
if (!ampBeg) break; | |
// p is now at the beginning of the next potential escape sequence | |
inputPos = (ampBeg - inputBuffer); | |
{ | |
int cpyLen = inputPos - lastInputPos; | |
int remLen = (inputBufferUsed - inputPos); | |
memcpy(outputBuffer + outputPos, inputBuffer + lastInputPos, cpyLen); | |
outputPos += cpyLen; | |
if (remLen > 5 && ampBeg[1] == 'a' && ampBeg[2] == 'p' && ampBeg[3] == 'o' && ampBeg[4] == 's' && ampBeg[5] == ';') | |
{ | |
inputPos += 6; | |
outputBuffer[outputPos++] = '\''; | |
} | |
else if (remLen > 4 && ampBeg[1] == 'a' && ampBeg[2] == 'm' && ampBeg[3] == 'p' && ampBeg[4] == ';') | |
{ | |
inputPos += 5; | |
outputBuffer[outputPos++] = '&'; | |
} | |
else if (remLen > 5 && ampBeg[1] == 'q' && ampBeg[2] == 'u' && ampBeg[3] == 'o' && ampBeg[4] == 't'&& ampBeg[5] == ';') | |
{ | |
inputPos += 6; | |
outputBuffer[outputPos++] = '"'; | |
} | |
else if (remLen > 3 && ampBeg[1] == 'l' && ampBeg[2] == 't'&& ampBeg[3] == ';') | |
{ | |
inputPos += 4; | |
outputBuffer[outputPos++] = '<'; | |
} | |
else if (remLen > 3 && ampBeg[1] == 'g' && ampBeg[2] == 't' && ampBeg[3] == ';') | |
{ | |
inputPos += 4; | |
outputBuffer[outputPos++] = '>'; | |
} | |
else | |
{ | |
fprintf(stderr, "unknown escape seq: %.*s\n", 4, ampBeg); | |
} | |
lastInputPos = inputPos; | |
} | |
} | |
// cpy rest | |
memcpy(outputBuffer + outputPos, inputBuffer + lastInputPos, (inputBufferUsed - inputPos)); | |
outputPos += (inputBufferUsed - inputPos); | |
outputBuffer[outputPos++] = '\0'; | |
puts(outputBuffer); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment