include <stdio.h> #include <string.h> #define FRAME_START 0x7E // Start of Frame #define FRAME_END 0x7E // End of Frame #define STUFFING_CHAR 0x7D // Escape character #define ESCAPED_CHAR 0x20 // Value to append after STUFFING_CHAR void stuffCharacters(char *input, char *output) { int i, j = 0; int length = strlen(input); // Adding the frame start character output[j++] = FRAME_START; for (i = 0; i < length; i++) { if (input[i] == FRAME_START || input[i] == FRAME_END || input[i] == STUFFING_CHAR) { output[j++] = STUFFING_CHAR; // Stuffing character output[j++] = input[i] ^ ESCAPED_CHAR; // Escape the character } else { output[j++] = input[i]; // Normal character } } // Adding the frame end charactera output[j++] = FRAME_END; output[j] = '\0'; // Null-terminate the output string } void unstuffCharacters(char *input, char *output) { int i, j = 0; int length = strlen(input); // Skip frame start i = 1; while (i < length - 1) { // Skip frame end if (input[i] == STUFFING_CHAR) { // Unstuffing character output[j++] = input[i + 1] ^ ESCAPED_CHAR; i += 2; // Move past the stuffed character } else { output[j++] = input[i++]; } } output[j] = '\0'; // Null-terminate the output string } int main() { char input[256]; // Buffer for input message char stuffed[512]; // Buffer for stuffed message (larger to accommodate potential expansion) char unstuffed[256]; // Buffer for unstuffed message printf("Enter your message: "); fgets(input, sizeof(input), stdin); // Read input from the keyboard // Remove newline character if present input[strcspn(input, "\n")] = 0; printf("Original Message: %s\n", input); // Perform character stuffing stuffCharacters(input, stuffed); printf("Stuffed Message: "); for (int i = 0; stuffed[i] != '\0'; i++) { printf("%02X ", (unsigned char)stuffed[i]); } printf("\n"); // Perform character unstuffing unstuffCharacters(stuffed, unstuffed); printf("Unstuffed Message: %s\n", unstuffed); return 0; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter