r/shittyprogramming • u/Internal_Ear_4683 • 2d ago
My friend told me that printf() and null-terminated strings are to insecure so i built it
here is source code(i am to lazy to make github repo)
```
#include <stdlib.h>
#include <unistd.h>
typedef struct {
int crypted;
int key;} crypted;
typedef struct {
char \*content;
int cap;
int count;} string;
crypted encode(int source, int key, int salt) {
crypted c;
c.key = key + salt / 2;
c.crypted = key \* key + source;
return c;}
char decode(crypted source, int salt) {
int key = source.key - salt / 2;
return source.crypted - key \* key;}
string initstr(int cap) {
string r;
r.content = (char \*)malloc(cap \* sizeof(char));
r.count = 0;
r.cap = cap;
return r;}
void str_putc(char c, string *s) {
if (s->count > s->cap) {
int newcap = s->cap + 2;
char \*tmp = realloc(s->content, newcap \* sizeof(char));
if (!tmp) {
goto end;
}
s->content = tmp;
}
s->content\[s->count\] = c;
s->count++;end:
}
void term_putchar(char c) {
write(1, &c, 1);}
void str_print(string s) {
for (int i = 0; i < s.count; i++) {
term_putchar(s.content\[i\]);
}}
int main() {
string s = initstr(12);
crypted c\[12\] = {encode(72, 12, 18), encode(69, 12, 18), encode(76, 12, 18), encode(76, 12, 18), encode(79, 12, 18), encode(32, 12, 18), encode(87, 12, 18), encode(79, 12, 18), encode(82, 12, 18), encode(76, 12, 18), encode(68, 12, 18), encode(33, 12, 18)};
for (int i = 0; i < 12; i++) {
str_putc(decode(c\[i\], 18), &s);
}
str_print(s);
term_putchar('\\n');
return 1;}
```
