Chad Strings - The Chad way to handle strings in C.

Overview

chadstr.h

Chad Strings - The Chad way to handle strings in C.

One str(...) macro to handle them all.

Examples Usage:

int table = 13;
int id = 37; 
str test1 = str("SELECT * FROM ", table, " where person_id ", id);
str test2 = str(test1);         //copies test1 to test2
str test3 = str(test2, test1); // returns concat of test2 and test1

test2 = test1; // acceptable, but wrong since test2 now points to test1 not copies it.

str(*test1); // returns const char* to use in printf like functions
Ex: puts(str(*test1)); // prints test1;

"Why yes, i don't free(), OS cleans all memory for me, how did you know?"

animated

free(test1); // will free memory for whole str.

"Whoa, whoa, wait bro. But how about freeing str->data field?"

While allocating memory for 'str' struct itself it also reserves space beneath it to store that string. str->data points to that location. So freeing str itself also frees memory located at str->data.

If your coompiler does support "__attribute__((cleanup()))" ("aahem gcc/clang"), strings can be auto free'd while defined this way:

autofree str test1 = str("apple");
...
// no free(test1); !!!

or

chadstr test2 = str("pineapple");
...
// no free(test2); !!!

Operations on Chad Strings:

Instead of writing shitload of different functions for some specific task, you can utilize already existing tools in your OS to do that for you.

Examples Usage:

cmd CMD = (cmd){"echo"}
chadstr test1 = str("orange apple");
chadstr pipecmd = str(" | cut -z -d \" \" -f1 "); 
chadstr result = str(CMD, test1, pipecmd); // "echo orange apple | cut -z -d " " -f1" as you would do in shell

/* chadstr result = str((cmd){"echo"}, test1, pipecmd); is also acceptable */

puts(str(*result));

File embedding has never been easier with ChadSTR:

cmd CAT = (cmd){"cat"};
chadstr file = str(CAT, "README.md"); // cat README.md

puts(str(*file));

ChadSTR also has utility function range() to select range of string:

chadstr test1 = str("pineapple"); 
chadstr test1range = str((range)(test1, 3,6)); // eapp

/*
 * If you are comfortable with range starting at index 1,
 * #define HUMAN_RANGE before #include "chadstr.h"
 * Note: negative end indices are still in "human" format 
 * starting at 1 no matter HUMAN_RANGE defined or not.
*/

#define HUMAN_RANGE
#include "chadstr.h"

...

chadstr test1 = str("pineapple"); 
chadstr test1range = str((range)(test1, 3,6)); // neap

Another example on generating random string with buffer and per char copying:

#include <time.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "chadstr.h"

str random_string(size_t length)
{
    chadstr pool = str("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
    size_t randindex;
    size_t i;

    struct timespec ts;
    clock_gettime(CLOCK_MONOTONIC, &ts);

    /* using nano-seconds instead of seconds */
    srand((time_t)ts.tv_nsec);

    char buffer[length+1];

    for(i = 0; i < length; ++i)
    {
        randindex = rand() % pool->len;
        buffer[i] = pool->data[randindex] ^ ((rand() % 2) ? 0 : 0x20);  
    }

    buffer[i] = '\0';

    return str(buffer);
}

int main(void)
{
    for (int i = 0; i < 1000; ++i)
    {
        chadstr s = random_string(120);
        puts(str(*s));
    }
    return 0;
}

PRs are welcomed.

Comments
  • docs: fix simple typo, coompiler -> compiler

    docs: fix simple typo, coompiler -> compiler

    There is a small typo in README.md.

    Should read compiler rather than coompiler.

    Semi-automated pull request generated by https://github.com/timgates42/meticulous/blob/master/docs/NOTE.md

    opened by timgates42 1
  • made some performance changes

    made some performance changes

    So cmd function is broken for buffers larger than 8192. Meanwhile, I solved some memory leak and changed some strncpy with memcpy and calloc with malloc where it's possible.

    opened by sneedcat 1
  • 'cleanup' attribute only applies to local variables

    'cleanup' attribute only applies to local variables

    I know even if this is a kind-of meme, it's a severe limitation for my personal use. I guess compilers ain't that smart after all....

    Now I am back at the dumb strdup stuff I've been struggling with :(

    opened by nikp123 1
  • Cannot compile chadstr.h using MSVC

    Cannot compile chadstr.h using MSVC

    main.c

    #include "chadstr.h"
    
    int main() {
    	chadstr test = str("orange apple");
    	return 0;
    }
    

    The result when I try to compile it:

    D:\workspace\chadstr>cl /I. main.c
    Microsoft (R) C/C++ Optimizing Compiler Version 19.28.29915 for x86
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    main.c
    D:\workspace\chadstr\chadstr.h(69): fatal error C1112: compiler limit: '129' too many macro arguments, only '127' allowed
    
    opened by dhalucario 5
  • AddressSanitizer: dynamic-stack-buffer-overflow

    AddressSanitizer: dynamic-stack-buffer-overflow

    take example code generate random strings and compile with sanitizers.

    $ gcc -fsanitize=address main.c 
    # ./a.out 
    =================================================================
    ==567==ERROR: AddressSanitizer: dynamic-stack-buffer-overflow on address 0x7ffe55e4a198 at pc 0x55b34322c058 bp 0x7ffe55e4a0e0 sp 0x7ffe55e4a0d8
    WRITE of size 1 at 0x7ffe55e4a198 thread T0
        #0 0x55b34322c057 in random_string (/root/chadstr/a.out+0x4057)
        #1 0x55b34322c1f6 in main (/root/chadstr/a.out+0x41f6)
        #2 0x7f2811d11d09 in __libc_start_main ../csu/libc-start.c:308
        #3 0x55b34322a249 in _start (/root/chadstr/a.out+0x2249)
    
    Address 0x7ffe55e4a198 is located in stack of thread T0
    SUMMARY: AddressSanitizer: dynamic-stack-buffer-overflow (/root/chadstr/a.out+0x4057) in random_string
    Shadow bytes around the buggy address:
      0x10004abc13e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
      0x10004abc13f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
      0x10004abc1400: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
      0x10004abc1410: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
      0x10004abc1420: ca ca ca ca 00 00 00 00 00 00 00 00 00 00 00 00
    =>0x10004abc1430: 00 00 00[cb]cb cb cb cb 00 00 00 00 00 00 00 00
      0x10004abc1440: 00 00 00 00 00 00 f1 f1 f1 f1 00 f2 f2 f2 00 00
      0x10004abc1450: f3 f3 00 00 00 00 00 00 00 00 00 00 f1 f1 f1 f1
      0x10004abc1460: 00 f3 f3 f3 00 00 00 00 00 00 00 00 00 00 00 00
      0x10004abc1470: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
      0x10004abc1480: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    Shadow byte legend (one shadow byte represents 8 application bytes):
      Addressable:           00
      Partially addressable: 01 02 03 04 05 06 07 
      Heap left redzone:       fa
      Freed heap region:       fd
      Stack left redzone:      f1
      Stack mid redzone:       f2
      Stack right redzone:     f3
      Stack after return:      f5
      Stack use after scope:   f8
      Global redzone:          f9
      Global init order:       f6
      Poisoned by user:        f7
      Container overflow:      fc
      Array cookie:            ac
      Intra object redzone:    bb
      ASan internal:           fe
      Left alloca redzone:     ca
      Right alloca redzone:    cb
      Shadow gap:              cc
    ==567==ABORTING
    $
    
    opened by tankf33der 2
Owner
C-nile
null
StringCheese is a CTF tool to solve easy challenges automatically in many cases where a strings | grep is just not enough

StringCheese StringCheese is a script written in Python to extract CTF flags (or any other pattern with a prefix) automatically. It works like a simpl

Mathis HAMMEL 57 Sep 14, 2022
A fast Python Common substrings of multiple strings library with C++ implementation

A fast Python Common substrings of multiple strings library with C++ implementation Having a bunch of strings, can I print some substrings which appea

Đào Nguyên Dương 7 Aug 21, 2022
➿ mulle-c-string-escape turns data into C-strings

mulle-c-string-escape ➿ mulle-c-string-escape turns data into C-strings Non-ASCII characters will be escaped to hex or octal. C-escapes are used for k

Nat! 9 Apr 11, 2022
C++ library to manage strings with different encodings

StringSuite C++ library to manage strings and (almost) any kind of encoded data. License Encmetric is written under the GNU Lesser General Public Lice

Paolo 3 Feb 12, 2022
A way to delete a locked file, or current running executable, on disk.

??️ delete-self-poc A way to delete a locked, or current running executable, on disk. This was originally found by Jonas Lykkegaard - I just wrote the

Lloyd 367 Sep 21, 2022
✔️The smallest header-only GUI library(4 KLOC) for all platforms

Welcome to GUI-lite The smallest header-only GUI library (4 KLOC) for all platforms. 中文 Lightweight ✂️ Small: 4,000+ lines of C++ code, zero dependenc

null 6.5k Oct 2, 2022
Stealthy way to hijack the existing game process handle within the game launcher (currently supports Steam and Battle.net). Achieve external game process read/write with minimum footprint.

Launcher Abuser Stealthy way to hijack the existing game process handle within the game launcher (currently supports Steam and Battle.net). Achieve ex

Ricardo Nacif 77 Sep 25, 2022
An implementation of a weak handle interface to a packed vector in C++

Experimental handle container in C++ Overview Following on from c-handle-container, this library builds on the same ideas but supports a dynamic numbe

Tom Hulton-Harrop 12 Mar 11, 2022
A lightweight modern C++11 library for Win32 API, using lambdas to handle Windows messages.

WinLamb A lightweight modern C++11 library for Win32 API, using lambdas to handle Windows messages. Overview Setup Example Classes summary License 1.

Rodrigo 234 Sep 22, 2022
Header-only C++11 library to handle physical measures

cpp-measures Header-only C++11 library to handle physical measures License: This project is released under the Mozilla Public License 2.0. Purpose Thi

Carlo Milanesi 20 Jun 28, 2018
A simple proxyless tool that checks if a linktr.ee handle is available.

linktree_checker A simple proxyless tool that checks if a linktr.ee handle is available. Installation Use g++ to build the program. g++ main.cpp -o li

mayhaps 1 Nov 11, 2021
Simple, cross-platform library to handle multiple mice.

ManyMouse ManyMouse's website is https://icculus.org/manymouse/ This is a simple library to abstract away the reading of multiple input devices. It is

Ryan C. Gordon 30 Aug 29, 2022
Loading dbk64.sys and grabbing a handle to it

ceload A tool that allows you to manually load up CheatEngine's signed driver and get a handle to it for various kernel hacking operations. The code i

Layle | Luca 120 Sep 25, 2022
A library to handle Apple Property List format in binary or XML

libplist A small portable C library to handle Apple Property List files in binary or XML format. Features The project provides an interface to read an

libimobiledevice 418 Sep 30, 2022
Small strings compression library

SMAZ - compression for very small strings ----------------------------------------- Smaz is a simple compression library suitable for compressing ver

Salvatore Sanfilippo 1k Sep 26, 2022
Simple Dynamic Strings library for C

Simple Dynamic Strings Notes about version 2: this is an updated version of SDS in an attempt to finally unify Redis, Disque, Hiredis, and the stand a

Salvatore Sanfilippo 4k Oct 2, 2022
Small strings compression library

SMAZ - compression for very small strings ----------------------------------------- Smaz is a simple compression library suitable for compressing ver

Salvatore Sanfilippo 1k Sep 26, 2022
With xshellex you can paste any kind of c-shellcode strings in x64dbg, ollydbg & immunity debugger

With xshellex you can paste any kind of c-shellcode strings in x64dbg, ollydbg & immunity debugger. Also you can convert the "binary-copied-clipboard" to c-shellcode string.

David Reguera Garcia aka Dreg 29 Jul 27, 2022
Experimental managed C-strings library

Stricks Managed C strings library. ?? API Why ? Because handling C strings is tedious and error-prone. Appending while keeping track of length, null-t

Francois Alcover 85 Sep 9, 2022
StringCheese is a CTF tool to solve easy challenges automatically in many cases where a strings | grep is just not enough

StringCheese StringCheese is a script written in Python to extract CTF flags (or any other pattern with a prefix) automatically. It works like a simpl

Mathis HAMMEL 57 Sep 14, 2022