Skip to content

Instantly share code, notes, and snippets.

' This is file libzib-0.9.3_TJF.bi.
'
' FreeBasic-Header file for libzip, translated with help of h_2_bi.bas by
' Thomas.Freiherr@gmx.net.
'
' Licence:
'
' libzib-0.9.3_TJF.bi is free software: you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by the
' Free Software Foundation, either version 3 of the License, or (at your
@countingpine
countingpine / tinybasic.bas
Last active October 21, 2017 22:41
yetifoot's Tiny Basic (small version), originally found at streetcds.co.uk/tinybasic.bas, discussion thread at https://freebasic.net/forum/viewtopic.php?t=12788, full project at https://github.com/countingpine/tinybasic
#include once "crt.bi"
#undef Rem
#define Rem
Dim Shared As Integer CHAR_DBLQUOTE = 34
Dim Shared As Integer SYM_BAD = 0
Dim Shared As Integer SYM_DATATYPE = 1
Dim Shared As Integer SYM_VAR = 2
Dim Shared As Integer SYM_PROC = 3
@countingpine
countingpine / primesieve.bas
Last active October 10, 2020 14:04
Simple prime sieve written in FreeBASIC. It outputs the number of primes found, and the time it took. Uses around 512 MB of memory.
'' Simple prime sieve written in FreeBASIC. Compiles a table of all the primes under 2^32 (configurable).
'' It outputs the number of primes found (should be 203280221 under 2^32), and the time it took (roughly 2-3 mins on my old-ish PC).
'' It uses around 512 MB of memory (one bit for each odd number in the range).
#define MAX 4294967295
type LUT_INT as uinteger
#ASSERT cast(LUT_INT, MAX) = MAX
@countingpine
countingpine / ext2scan.c
Last active December 19, 2021 18:06
ext2scan: scan for ext2/ext3/ext4 partitions
/* ext2scan:
* Scans an input stream, sector by sector, for something that looks like an ext{2,3,4} partition.
* It does this by looking for a magic WORD, 0xEF53, at a known offset into the sector.
* For random data, this will occur by chance around once per 32MB of data, so we also
* check whether the first two sectors are all zeros, which is commonly true for ext partitions.
*
* Compile with:
* gcc ./ext2scan.c -o ./ext2scan
*
* Example usage:
@countingpine
countingpine / unrnd.bas
Last active May 16, 2021 15:35
Finds (negative) values of n that can be passed to QB's RND(n) function, in order to return the desired number
'' FreeBASIC has a number of pseudorandom algorithms: set the QB algorithm (4)
randomize 0, 4
function unrnd(byval r as double) as long
#define SEED2R(seed) ( ((seed) + ((seed) shr 24)) and &HFFFFFF )
'' accept [0,1) or [0,16777215]
if r > 0.0 and r < 1.0 then
r = int(r * 16777216)
@countingpine
countingpine / uintT.bas
Last active July 16, 2018 18:31
uintT.bas - UDT to represent an unsigned integer twice as large as the given type. Mainly intended for 128 bits, but designed to accommodate smaller types for easier testing.
'' FT = full type (doesn't always exist)
'' HT = half type (make up the two halves of the number)
'' QT = quarter type (used for multiplication)
'' 32 / 16 / 8
'type FT as ulong
'type HT as ushort
'type QT as ubyte
'' 64 / 32 / 16
<h3>This article has been tested for the following Ubuntu versions:</h3>
<p>&nbsp;</p>
<ul>
<li>
<p><a href="https://wiki.ubuntuusers.de/Trusty_Tahr/">Ubuntu 14.04</a> Trusty Tahr</p>
</li>
<li>
<p><a href="https://wiki.ubuntuusers.de/Precise_Pangolin/">Ubuntu 12.04</a> Precise Pangolin</p>
@countingpine
countingpine / inflate.c
Last active May 14, 2017 13:48
Skeleton code for zlib inflate routine
#include "inflate.h"
int inflate(char *output, int *output_length, const char *input, int input_length) {
final_block = 0;
do {
get_block_type();
switch(block_type) {
@countingpine
countingpine / lngmath.c
Last active September 9, 2017 14:58
lngmath
void lngcpy(unsigned char *dst, int dst_length, unsigned char *const src, int src_length) {
if (dst_length <= src_length) {
memcpy(dst, src, dst_length);
} else {
/* src_length < dst_length */
memcpy(dst, src, src_length);
memset(dst + src_length, 0, dst_length - src_length);
}
}
@countingpine
countingpine / dragon.c
Last active October 21, 2017 22:37
[WIP] dragon.c: Try to find the fastest strategy to guess the ordering of four items.
/* Game: given four different items, try to put them in order.
With each try, a number is returned indicating how many items are in their correct position. (As in a recent Crystal Maze game)
Challenge: find a strategy that minimises the (worst-case) number of moves needed to guess the order.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef unsigned char move_count;