Skip to content

Instantly share code, notes, and snippets.

View cpq's full-sized avatar
🎭
Купатися чи не купатись?

Sergey Lyubka cpq

🎭
Купатися чи не купатись?
View GitHub Profile
@cpq
cpq / eval_arith.c
Last active August 29, 2015 14:05
Arithmetic expression evaluator. Demonstrates how to write recursive descent parser in C.
// Copyright (c) 2014 Sergey Lyubka. All rights reserved.
// Arithmetic expression evaluator.
//
// Compilation: cc -o eval_arith eval_arith.c
// Usage: ./eval_arith "2 - 3 * ( 4 - 2.7 * 11.5 ) + 1.78"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
@cpq
cpq / api.c
Created October 16, 2014 15:02
Extensible C API
#define END_OF_PARAMS (-1)
// Each extensible function is going to have it's own params enum
enum {
SEND_FILE_EXTRA_HEADER, // next arg is const char *
SEND_FILE_BYTE_RANGE, // next args are size_t, size_t
SEND_FILE_NUM_PARAMS
};
int mg_send_file(const char *path, ...) {
@cpq
cpq / callbacks.c
Created October 16, 2014 17:08
Callback APIs
// Task: user needs to set up a simple HTTP server
// Option 1
static void my_handler(struct ns_connection *nc, int ev, void *ev_data) {
switch (ev) {
case NS_HTTP_REQUEST:
mg_printf(nc, "HTTP/1.0 200 OK\r\n\r\n hi there!! ");
nc->flags |= NSF_FINISHED_SENDING_DATA; // Schedule connection close
break;
default: // We can catch low-level events like NS_RECV, etc
@cpq
cpq / producer_consumer.c
Created December 28, 2014 20:04
One-to-many producer/consumer implementation on top of POSIX API
struct task {
socket_t accepted_socket;
};
struct queue {
struct task tasks[NUM_WORKER_THREADS];
int num_tasks;
pthread_mutex_t mutex;
pthread_cond_t condvar;
};
@cpq
cpq / dash_editor.md
Last active August 29, 2015 17:25
Dashboard editor

DASHBOARD EDITOR

Overview

Smart.js IoT platform provides two components for building private smart device infrastructures:

  • Firmware for the embedded devices, which is capable of harvesting
@cpq
cpq / chromatic_vs_natural.py
Created December 16, 2011 12:44
Chromatic vs natural scale
#!/usr/bin/env python
# Copyright (c) 2011 by Sergey Lyubka
# All rights reserved
#
# This program calculates note frequencies for chromatic and natural scale.
# Chromatic scale is built by splitting an octave (2x frequency bump) onto
# 12 equal intervals (semitones).
semitones = ['%.5f' % pow(pow(2, x), 1 / 12.0) for x in range(13)]
tones = [0, 2, 4, 5, 7, 9, 11] # Major scale
@cpq
cpq / integer.h
Created July 29, 2013 15:13
Big number integer arithmetic in a single header file
/*
* Large integer manipulation functions.
* All integers are assumed to be unsigned and represented as
* a contiguous array of bytes (unsigned char *ptr, int len)
* in little-endian byte order (ptr points to the least significant byte).
*/
#define HUGE 4096 /* Length of the biggest integer in bytes */
/*
@cpq
cpq / quoted_string_parser.c
Created September 10, 2013 17:23
Igor Izvarin's question on parsing string
// Usage:
// cc -W -Wall ~/tmp/quoted_string_parser.c
// ./a.out " dwedewd, dede" " 'sds ee' ,'ddd'"
#include <stdio.h>
#include <string.h>
static const char *whitespaces = " \r\n";
static const char *whitespaces_or_comma = " \r\n,";
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>GitHub Stars</title>
</head>
<body>
hi
</body>
</html>
@cpq
cpq / overflow.c
Created April 4, 2016 12:33
Simple web server with buffer overflow vulnerability
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define LISTENING_PORT 8000
static void serve_new_connection(FILE *fp) {
char method[10], uri[100], protocol[20];