a mega cool windows xp app
1#pragma once
2#include <windows.h>
3
4#define QR_SIZE 21
5#define MAX_TEXT_LEN 256
6
7// Pure C struct for QR code - no C++ classes
8typedef struct {
9 BOOL modules[QR_SIZE][QR_SIZE];
10 char text[MAX_TEXT_LEN];
11} QRCode;
12
13// Function prototypes
14void QRCode_Init(QRCode* qr, const char* inputText);
15void QRCode_GeneratePattern(QRCode* qr);
16void QRCode_AddFinderPattern(QRCode* qr, int x, int y);
17BOOL QRCode_IsReserved(int x, int y);
18void QRCode_DrawToHDC(QRCode* qr, HDC hdc, int startX, int startY, int moduleSize);
19int QRCode_GetSize(void);
20const char* QRCode_GetText(QRCode* qr);
21
22// Implementation
23void QRCode_Init(QRCode* qr, const char* inputText) {
24 int x, y, i;
25
26 // Initialize modules array
27 for (y = 0; y < QR_SIZE; y++) {
28 for (x = 0; x < QR_SIZE; x++) {
29 qr->modules[y][x] = FALSE;
30 }
31 }
32
33 // Copy text (safe copy)
34 i = 0;
35 while (inputText[i] && i < MAX_TEXT_LEN - 1) {
36 qr->text[i] = inputText[i];
37 i++;
38 }
39 qr->text[i] = '\0';
40
41 // Generate pattern
42 QRCode_GeneratePattern(qr);
43}
44
45void QRCode_GeneratePattern(QRCode* qr) {
46 int i, x, y;
47 unsigned int hash = 0;
48 unsigned char textBytes[MAX_TEXT_LEN];
49 int textLen = 0;
50
51 // Add finder patterns (corners)
52 QRCode_AddFinderPattern(qr, 0, 0);
53 QRCode_AddFinderPattern(qr, QR_SIZE - 7, 0);
54 QRCode_AddFinderPattern(qr, 0, QR_SIZE - 7);
55
56 // Add timing patterns
57 for (i = 8; i < QR_SIZE - 8; i++) {
58 qr->modules[6][i] = (i % 2 == 0) ? TRUE : FALSE;
59 qr->modules[i][6] = (i % 2 == 0) ? TRUE : FALSE;
60 }
61
62 // Convert text to bytes and calculate length
63 while (qr->text[textLen] && textLen < MAX_TEXT_LEN - 1) {
64 textBytes[textLen] = (unsigned char)qr->text[textLen];
65 textLen++;
66 }
67
68 // Add format information (fake but realistic looking)
69 // These would normally encode error correction level and mask pattern
70 qr->modules[8][0] = TRUE;
71 qr->modules[8][1] = FALSE;
72 qr->modules[8][2] = TRUE;
73 qr->modules[8][3] = TRUE;
74 qr->modules[8][4] = FALSE;
75 qr->modules[8][5] = TRUE;
76
77 // Add data in a more realistic zigzag pattern
78 int bitIndex = 0;
79 BOOL upward = TRUE;
80
81 for (x = QR_SIZE - 1; x > 0; x -= 2) {
82 if (x == 6) x--; // Skip timing column
83
84 for (i = 0; i < QR_SIZE; i++) {
85 y = upward ? (QR_SIZE - 1 - i) : i;
86
87 // Fill two columns (right to left)
88 for (int col = 0; col < 2; col++) {
89 int currentX = x - col;
90 if (currentX >= 0 && !QRCode_IsReserved(currentX, y)) {
91 // Use text data in a more structured way
92 BOOL bit = FALSE;
93 if (bitIndex < textLen * 8) {
94 int byteIndex = bitIndex / 8;
95 int bitPos = 7 - (bitIndex % 8);
96 bit = (textBytes[byteIndex] >> bitPos) & 1;
97 bitIndex++;
98 } else {
99 // Padding pattern
100 bit = ((currentX + y) % 3 == 0) ? TRUE : FALSE;
101 }
102 qr->modules[y][currentX] = bit;
103 }
104 }
105 }
106 upward = !upward;
107 }
108}
109
110void QRCode_AddFinderPattern(QRCode* qr, int x, int y) {
111 int dx, dy;
112 BOOL dark;
113
114 for (dy = 0; dy < 7; dy++) {
115 for (dx = 0; dx < 7; dx++) {
116 if (x + dx < QR_SIZE && y + dy < QR_SIZE) {
117 dark = (dx == 0 || dx == 6 || dy == 0 || dy == 6 ||
118 (dx >= 2 && dx <= 4 && dy >= 2 && dy <= 4)) ? TRUE : FALSE;
119 qr->modules[y + dy][x + dx] = dark;
120 }
121 }
122 }
123}
124
125BOOL QRCode_IsReserved(int x, int y) {
126 // Check if position is part of finder patterns
127 if ((x < 9 && y < 9) ||
128 (x >= QR_SIZE - 8 && y < 9) ||
129 (x < 9 && y >= QR_SIZE - 8)) {
130 return TRUE;
131 }
132
133 // Check timing patterns
134 if (x == 6 || y == 6) {
135 return TRUE;
136 }
137
138 // Check format information areas
139 if ((x < 9 && y == 8) || (x == 8 && y < 9)) {
140 return TRUE;
141 }
142 if ((x >= QR_SIZE - 8 && y == 8) || (x == 8 && y >= QR_SIZE - 7)) {
143 return TRUE;
144 }
145
146 return FALSE;
147}
148
149void QRCode_DrawToHDC(QRCode* qr, HDC hdc, int startX, int startY, int moduleSize) {
150 HBRUSH blackBrush = CreateSolidBrush(RGB(0, 0, 0));
151 HBRUSH whiteBrush = CreateSolidBrush(RGB(255, 255, 255));
152 int x, y;
153 RECT rect;
154
155 for (y = 0; y < QR_SIZE; y++) {
156 for (x = 0; x < QR_SIZE; x++) {
157 rect.left = startX + x * moduleSize;
158 rect.top = startY + y * moduleSize;
159 rect.right = startX + (x + 1) * moduleSize;
160 rect.bottom = startY + (y + 1) * moduleSize;
161
162 FillRect(hdc, &rect, qr->modules[y][x] ? blackBrush : whiteBrush);
163 }
164 }
165
166 DeleteObject(blackBrush);
167 DeleteObject(whiteBrush);
168}
169
170int QRCode_GetSize(void) {
171 return QR_SIZE;
172}
173
174const char* QRCode_GetText(QRCode* qr) {
175 return qr->text;
176}