Skip to content

Instantly share code, notes, and snippets.

@bryc
Last active April 19, 2026 18:01
Show Gist options
  • Select an option

  • Save bryc/20588f955b730b981db4 to your computer and use it in GitHub Desktop.

Select an option

Save bryc/20588f955b730b981db4 to your computer and use it in GitHub Desktop.
C Code Snippets Archive #1 (old note: 2016 NOTE: USE fscanf() in future C projects)
#include <stdio.h>
#include <stdint.h>
int main(void)
{
uint64_t A1, A2, A3=0x13108B3C1, T0, T1, T2, T3, T4=0, T5, T6, T7, T8, T9, S1, V0, AT, S3;
int i;
unsigned char bytes[28] = {
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF
};
for (i = 0; i < sizeof(bytes); i++)
{
T8 = bytes[i]; // LBU
T5 = A3 & 0xFFFFFFFF; // LW
T9 = S1 & 0x000F;
T0 = T8 << T9;
T4 = A3 >> 32 ; // LW
T7 = T0 + T5;
T2 = T0 >> 0x1F;
// After JAL
A3 = (T4 << 32 ) + T7; // LD
A2 = A3 << (0x1F + 32);
A1 = A3 << 0x1F;
A2 = A2 >> 0x1F;
A1 = A1 >> (0x0 + 32);
A3 = A3 << (0xC + 32);
A2 = A2 | A1;
A3 = A3 >> (0x0 + 32);
A2 = A2 ^ A3;
A3 = A2 >> 0x14;
A3 = A3 & 0xFFF;
A3 = A3 ^ A2;
// After JR
V0 = A3 & 0xFFFFFFFF;
S1 += 0x7;
// After BNE
S3 = S3 ^ V0;
}
for (i = sizeof(bytes)-1; i >= 0; i--)
{
T1 = bytes[i]; // LBU
T3 = A3 & 0xFFFFFFFF; // LW
T8 = S1 & 0x000F;
T9 = T1 << T8;
T2 = A3 >> 32 ; // LW
T5 = T9 + T3;
T0 = T9 >> 0x1F;
// After JAL
A3 = (T2 << 32) + T5; // LD
A2 = A3 << (0x1F + 32);
A1 = A3 << 0x1F;
A2 = A2 >> 0x1F;
A1 = A1 >> (0x0 + 32);
A3 = A3 << (0xC + 32);
A2 = A2 | A1;
A3 = A3 >> (0x0 + 32);
A2 = A2 ^ A3;
A3 = A2 >> 0x14;
A3 = A3 & 0xFFF;
A3 = A3 ^ A2;
// After JR
V0 = A3 & 0xFFFFFFFF;
S1 += 0x3;
// After BNE
S3 = S3 ^ V0;
}
printf("Checksum: %lX\n", S3);
return 0;
}
#include<stdio.h>
main()
{
unsigned char data[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00
};
int i;
unsigned long long A3 = 0x13108B3C1, A2, S1=0, S3=0;
for (i = 0; i < sizeof(data); i++)
{
A3 += data[i] << (S1 & 0x0F);
A2 = A3<<0x3F>>0x1F | A3>>1 ^ A3<<0x2C>>0x20;
A3 = A2 >> 0x14 & 0xFFF ^ A2;
S3 ^= A3;
S1 += 7;
}
for ( i-= 1; i >= 0 ; i--)
{
A3 += data[i] << (S1 & 0x0F);
A2 = A3<<0x3F>>0x1F | A3>>1 ^ A3<<0x2C>>0x20;
A3 = A2 >> 0x14 & 0xFFF ^ A2;
S3 ^= A3;
S1 += 3;
}
printf("\n---\n%X",S3);
}
#include <stdio.h>
#include <stdint.h>
int main(void) {
uint64_t A1, A2, A3=0x13108B3C1, S1=0, S3=0;
int i;
unsigned char bytes[28] = {
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF
};
for (i = 0; i < 28; i++) {
A3 = (A3 & 0x100000000) + (bytes[i] << (S1 & 15)) + (A3 & 0xFFFFFFFF);
A2 = A3 << 32 | A3 >> 1;
A3 = A3 << 44 >> 32;
A2 = A2 ^ A3;
A3 = A2 >> 20 & 0xFFF ^ A2;
S3 = S3 ^ A3 & 0xFFFFFFFF;
S1 += 0x7;
}
for (i = i - 1; i >= 0; i--) {
A3 = (A3 & 0x100000000) + (bytes[i] << (S1 & 15)) + (A3 & 0xFFFFFFFF);
A2 = A3 << 32 | A3 >> 1;
A3 = A3 << 44 >> 32;
A2 = A2 ^ A3;
A3 = A2 >> 20 & 0xFFF ^ A2;
S3 = S3 ^ A3 & 0xFFFFFFFF;
S1 += 0x3;
}
printf("Checksum: %lX\n", S3);
return 0;
}
#include <stdio.h>
unsigned long long value = 0x13108B3C1;
unsigned long long value2 = 0;
unsigned long long checksum = 0;
unsigned int carry = 1;
int bp = 0;
int sd = 0;
unsigned char data[] = {
0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0x0C
};
int algo(void)
{
value = (value & 0x1FFFFFFFF) + (data[bp] << (sd & 0x0F));
//value &= 0x1FFFFFFFF;
value2 = (value >> 1 | value << 32) ^ (value << 44) >> 32;
carry = value & 0x01;
value = value2 ^ (value2 >> 20) & 0x0FFF;
checksum = value ^ checksum;
}
int main(void)
{
for(; bp < sizeof(data); bp++, sd += 7)
algo();
bp--;
for(; bp >= 0; bp--, sd += 3)
algo();
printf("%08X", checksum);
return 0;
}
#include <stdio.h>
unsigned char arr[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
main()
{
unsigned int a = 0x3108B3C1;
unsigned int t = 1;
unsigned int b = 0;
unsigned int bt = 0;
long chk = 0;
int i = 0, c = 0;
for(i = 0; i < sizeof(arr); i++, c += 7)
{
a += arr[i] << (c & 0xF);
b = (a >> 1 | t << 31) ^ (a & 0xFFFFF) << 12;t = a & 1;
a = (b >> 20) & 0xFFF ^ b;
chk = a ^ chk;
}
bt = chk;
for(i--; i >= 0; i--, c += 3)
{
a += arr[i] << (c & 0xF);
b = (a >> 1 | t << 31) ^ (a & 0xFFFFF) << 12;t = a & 1;
a = (b >> 20) & 0xFFF ^ b;
chk = a ^ chk;
}
printf("---\n%lX\n", chk);
}
#include<stdio.h>
main()
{
unsigned char data[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00
};
int i;
unsigned long long A3 = 0x13108B3C1, A2, S1=0, S3=0;
for (i = 0; i < sizeof(data); i++)
{
A3 += data[i] << (S1 & 0x0F);
A2 = A3<<0x3F>>0x1F | A3>>1 ^ A3<<0x2C>>0x20;
A3 = A2 >> 0x14 & 0xFFF ^ A2;
S3 ^= A3;
S1 += 7;
}
for ( i-= 1; i >= 0 ; i--)
{
A3 += data[i] << (S1 & 0x0F);
A2 = A3<<0x3F>>0x1F | A3>>1 ^ A3<<0x2C>>0x20;
A3 = A2 >> 0x14 & 0xFFF ^ A2;
S3 ^= A3;
S1 += 3;
}
printf("\n---\n%X",S3);
}
#include <stdio.h>
#include <stdint.h>
#define NUMBYTES 28
int main(void)
{
uint64_t A1, A2, A3=0x13108B3C1, T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, S1=0, S3=0, V0, AT;
int i;
unsigned char bytes[NUMBYTES] = {
0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00
};
for (i = 0; i < NUMBYTES; i++)
{
T8 = bytes[i]; // LBU
T5 = A3 & 0xFFFFFFFF; // LW
T9 = S1 & 0x000F;
T0 = T8 << T9;
T4 = A3 >> 32 ; // LW
T7 = T0 + T5;
T2 = T0 >> 0x1F;
if (T7 < T5){ AT = 1; } else { AT = 0; }
T6 = AT + T2;
T6 = T6 + T4;
// After JAL
A3 = (T6 << 32 ) + T7; // LD
A2 = A3 << (0x1F + 32);
A1 = A3 << 0x1F;
A2 = A2 >> 0x1F;
A1 = A1 >> (0x0 + 32);
A3 = A3 << (0xC + 32);
A2 = A2 | A1;
A3 = A3 >> (0x0 + 32);
A2 = A2 ^ A3;
A3 = A2 >> 0x14;
A3 = A3 & 0xFFF;
A3 = A3 ^ A2;
V0 = A3 << (0x0 + 32);
// After JR
V0 = V0 >> (0x0 + 32);
S1 += 0x7;
// After BNE
S3 = S3 ^ V0;
}
for (i = NUMBYTES - 1; i >= 0; i--)
{
T1 = bytes[i]; // LBU
T3 = A3 & 0xFFFFFFFF; // LW
T8 = S1 & 0x000F;
T9 = T1 << T8;
T2 = A3 >> 32 ; // LW
T5 = T9 + T3;
T0 = T9 >> 0x1F;
if (T5 < T3){ AT = 1; } else { AT = 0; }
T4 = AT + T0;
T4 = T4 + T2;
// After JAL
A3 = (T4 << 32) + T5; // LD
A2 = A3 << (0x1F + 32);
A1 = A3 << 0x1F;
A2 = A2 >> 0x1F;
A1 = A1 >> (0x0 + 32);
A3 = A3 << (0xC + 32);
A2 = A2 | A1;
A3 = A3 >> (0x0 + 32);
A2 = A2 ^ A3;
A3 = A2 >> 0x14;
A3 = A3 & 0xFFF;
A3 = A3 ^ A2;
V0 = A3 << (0x0 + 32);
// After JR
V0 = V0 >> (0x0 + 32);
S1 += 0x3;
// After BNE
S3 = S3 ^ V0;
}
printf("Checksum: %lX\n", S3);
return 0;
}
#include <stdio.h>
#ifdef __unix__
# include <unistd.h>
#elif defined _WIN32
# include <windows.h>
#define sleep(x) Sleep(1000 * x)
#endif
int main(int argc, char **argv)
{
while(1){
FILE *stream = fopen(argv[1], "rb");
unsigned char buffer[512];
fread(buffer, 1, 512, stream);
int i;
for (i = 0; i < 512; i++)
{
printf("%.2X ", buffer[i]);
if (i % 16 == 15)
{
printf("\n");
}
}
sleep(100);
system("cls");
}
getchar();
return 0;
}
#include <stdio.h>
int main( int argc, char *argv[] )
{
while(1){
system("cls");
FILE *file = fopen( argv[1], "r" );
int x;
int i;
i = 0;
while ( ( x = fgetc( file ) ) != EOF )
{
if(i==16){ i=0; printf( "\n" ); }
printf( "%02x ", x );
i++;
}
fclose( file );
}
printf( "\n" );
system( "pause" );
}
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int main(int argc, char **argv)
{
while(1)
{
FILE *stream = fopen(argv[1], "rb");
unsigned char buffer[2048];
fread(buffer, 1, 2048, stream);
int i;
for (i = 0; i < 2048; i++)
{
printf("%.2X ", buffer[i]);
if (i % 26 == 25)
{
printf("\n");
}
}
Sleep(500);
system("cls");
}
return 0;
}
#include <windows.h>
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
/* Make the class name into a global variable */
char szClassName[ ] = "CodeBlocksWindowsApp";
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default colour as the background of the window */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"Code::Blocks Template Windows App", /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
544, /* The programs width */
375, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow (hwnd, nCmdShow);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
#include <windows.h>
char glpszText[1024];
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
sprintf(glpszText,
"Hello World\nGetCommandLine(): [%s]\n"
"WinMain lpCmdLine: [%s]\n",
lpCmdLine, GetCommandLine() );
WNDCLASSEX wcex;
wcex.cbSize = sizeof(wcex);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = "HELLO";
wcex.hIconSm = NULL;
if (!RegisterClassEx(&wcex))
return FALSE;
HWND hWnd;
hWnd = CreateWindow("HELLO", "Hello", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
if (!hWnd)
return FALSE;
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
RECT rt;
GetClientRect(hWnd, &rt);
DrawText(hdc, glpszText, strlen(glpszText), &rt, DT_TOP | DT_LEFT);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment