@supriyo_biswas

Condition matcher

updated August 10, 2020 · Python
import re

def match_condition(rule, s):
    if s == None:
        return False
    elif rule[0] == '/' and rule[-1] == '/':
        return bool(re.search(rule[1:-1], s))
    elif rule[0] == '/' and rule[-2:] == '/i':
        return bool(re.search(rule[1:-2], s, re.I))

    return rule == s

INI Parsing

updated June 16, 2020 · Bash
function parse_ini_file() {
    declare -A ini
    section='_root'
    
    sed -r 's/[ \t]*(\;.*)$//;/^$/d' | while read line; do
    	if [[ $line == \[*\] ]]; then
            section=${line:1:-1}
    	elif [[ $line =~ ^[a-zA-Z0-9_-]+= ]]; then
    	    key="$section:$(cut -d'=' -f1 <<< "$line")"
    	    value="$(cut -d'=' -f2 <<< "$line")"
            if [[ $value =~ \'*\' ]] || [[ $value =~ \"*\" ]]; then

Inspecting the sockaddr_in6 structure

updated June 14, 2020 · C
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>

int main() {
    // a sockaddr_in6 stores ipv6 addresses.
    printf("struct sockaddr_in6: %d\n\n", sizeof(struct sockaddr_in6));

    printf("-> sa_family_t sin6_family: %d\n", sizeof(sa_family_t));
    printf("-> in_port_t sin6_port: %d\n", sizeof(in_port_t));
    printf("-> uint32_t sin6_flowinfo: %d\n", sizeof(uint32_t));

Generating SMTP responses

June 05, 2020 · Python
def smtp_response(code, lines):
    response = []
    
    for i in range(len(lines) - 1):
        response.append(b'%i-%s\r\n' % (code, lines[i].encode()))
    
    response.append(b'%i %s\r\n' % (code, lines[-1].encode()))
    return b''.join(response)

if __name__ == '__main__':
    print(smtp_response(220, ['ESMTP server at your service', 'SMTPUTF8', '8BITMIME', 'STARTTLS']))

Parsing SMTP commands

updated June 05, 2020 · Python
import re

def parse_command(cmd):
    result = []
    for part in re.split(rb'\s+', cmd):
        result.append(re.sub(rb'^(\w+)', lambda p: p[1].lower(), part).decode())
    
    return result

if __name__ == '__main__':
    print(parse_command(b'mail from:<hello@a.com> size=512 command=12'))

Password Strength Estimation

updated May 25, 2020 · NodeJS
const commonWords = [
    'adrian',
    'alejandro',
    'alexander',
    'alexandra',
    'america',
    'angel',
    'angelina',
    'anthony',
    'arsenal',
    'asshole',

R plot test

April 18, 2020 · R
x1 <- seq(-4, 4, 0.1)
x1

y1 <- exp(2*x1)
y1

plot(x1, y1, xlab = "X", ylab = "Y")

fix_array

April 06, 2020 · PHP
<?php

function fix_array(array $a)
{
    $result = [];

    foreach ($a as $char => $values) {
        foreach ($values as $lang => $value) {
            $result[$lang][$value] = $char;
        }
    }

Recursive filter

updated April 23, 2020 · Python
def filter_rec(fn, elems):
    results = []

    for elem in elems:
        if isinstance(elem, list):
            result = filter_rec(fn, elem)
            if result:
                results.append(result)
        elif fn is None:
            if elem:
                results.append(elem)

Password hash importer

updated April 11, 2020 · PHP
#!/usr/bin/php
<?php

set_error_handler(function ($errno, $errstr, $errfile, $errline) {
    throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
});

function println($str) {
    echo $str, "\n";
}

Password importer

updated April 11, 2020 · PHP
#!/usr/bin/php
<?php

set_error_handler(function ($errno, $errstr, $errfile, $errline) {
    throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
});

function println($str) {
    echo $str, "\n";
}

Splitting integers

March 28, 2020 · PHP
<?php

function cut(int $total) {
    $result = [];

    while ($total > 0) {
        $new = random_int(1, $total);
        $result []= $new;
        $total -= $new;
    }

Sending an email using SES and boto3

updated April 16, 2020 · Python
import boto3
from base64 import b64decode
from urllib.parse import parse_qs

# Replace your email address here
send_to = 'your_email_address_here'

def lambda_handler(event, context):
    # We receive our data through POST requests. API gateway
    # sends the POST data as a Base64 encoded string in
    # event['body'], so we must decode it.

Pointers: Arithmetic on double pointers

updated April 11, 2020 · C
#include <stdio.h>

int main() {
    // a two dimensional array
    int a[2][4] = {{4, 2, 7, 8}, {1, 3, 5, 9}};

    // two loop variables
    int i, j;

    for (i = 0; i < 2; i++) {
        for (j = 0; j < 4; j++) {
#include <stdio.h>

int main() {
    // two int variables and pointers pointing to them
    int x = 10, *p = &x;
    int y = 20, *q = &y;

    // create a pointer-to-pointer, pointing to p
    int **r = &p;

    // print the value of x through r.

Pointers: constant pointers

updated April 11, 2020 · C
#include <stdio.h>

int main() {
    // declare two variables.
    int x = 10, y = 20;

    // declare a constant pointer to the variable x
    int *const p = &x;

    // print the value of a through x.
    printf("The value of x is %d\n", *p);

Pointers: constant pointers

updated April 11, 2020 · C
#include <stdio.h>

int main() {
    // declare two const-qualified variables.
    const int x = 10, y = 20;

    // declare a pointer to the const-qualified variable x
    const int *p = &x;

    // print the value of a through x.
    printf("The value of x is %d\n", *p);
#include <stdio.h>

void print_array(int q[], int size) {
    // q is a pointer constant.

    int i;

    for (i = 0; i < size; i++) {
        printf("%d ", q[i]);
    }
}
#include <stdio.h>

void add_one(int q[]) {
    // because 'q' is just a special kind of pointer
    // called a constant pointer, this statement works
    // as you would expect.

    *q = *q + 1;
}

int main() {

Pointers: passing arrays to functions

updated April 11, 2020 · C
#include <stdio.h>

void add_one(int *b, int size) {
    int i;

    // add one to each element of b
    for (i = 0; i < size; i++) {
        b[i]++;
    }
}