// Online C++ compiler to run C++ program online
#include <stdio.h>
#include <string.h>
void before_patch(char* password) {
char *pos, *end, *sstart;
pos = password;
sstart = strchr(pos, '"');
if (sstart)
sstart = strrchr(sstart + 1, '"');
if (!sstart)
sstart = pos;
end = strchr(sstart, '#');
if (end)
*end-- = '\0';
else
end = pos + strlen(pos) - 1;
printf("result : %s\n", pos);
}
void after_patch(char* password) {
char *pos, *end, *sstart;
pos = password;
/*
* Remove # comments unless they are within a double quoted
* string.
*/
sstart = pos;
end = strchr(sstart, '#');
//printf("end: %s\n", end);
while (end) {
sstart = strchr(sstart, '"');
//printf("first quote: %s\n", sstart);
if (!sstart || sstart > end)
break;
sstart = strchr(sstart + 1, '"');
if (!sstart)
break;
sstart++;
if (sstart > end)
end = strchr(sstart, '#');
}
if (end)
*end-- = '\0';
else
end = pos + strlen(pos) - 1;
printf("result : %s\n", pos);
}
void testBeforeAfter(char* password) {
printf("full line: %s\n", password);
puts("### BEFORE PATCH##");
before_patch(password);
puts("### AFTER PATCH##");
after_patch(password);
puts("\n");
}
int main() {
char password[40] = "sae_password=\"0Y822#E$2C\"P/A;A4F#2x6D\"\"";
testBeforeAfter(password);
char password2[40] = "sae_password=\"\"######";
testBeforeAfter(password2);
char password3[40] = "sae_password=\"#\"#\"####";
testBeforeAfter(password3);
char password4[50] = "sae_password=\"0t3D#B!S0C\"G&D!D#a;D#1T3F%\"";
testBeforeAfter(password4);
return 0;
}