一堆代码

This commit is contained in:
2026-04-21 02:12:12 +08:00
parent 27a51a0986
commit 11d87f16a5
11 changed files with 2205 additions and 3 deletions

View File

@@ -0,0 +1,411 @@
#include "cmd_runtime.h"
#include <stdio.h>
#define USH_BMPVIEW_DEFAULT_COLS 80ULL
#define USH_BMPVIEW_MAX_COLS 160ULL
#define USH_BMPVIEW_MAX_ROWS 120ULL
#define USH_BMPVIEW_MAX_IMAGE_WIDTH 4096ULL
#define USH_BMPVIEW_MAX_IMAGE_HEIGHT 4096ULL
#define USH_BMPVIEW_MAX_ROW_BYTES 16384U
typedef struct ush_bmpview_info {
u64 width;
u64 height;
u64 bytes_per_pixel;
u64 row_stride;
u64 pixel_offset;
int top_down;
} ush_bmpview_info;
static unsigned char ush_bmpview_row_buf[USH_BMPVIEW_MAX_ROW_BYTES];
static char ush_bmpview_render[USH_BMPVIEW_MAX_ROWS][USH_BMPVIEW_MAX_COLS + 1U];
static u64 ush_bmpview_sample_rows[USH_BMPVIEW_MAX_ROWS];
static unsigned int ush_bmpview_le16(const unsigned char *ptr) {
return (unsigned int)ptr[0] | ((unsigned int)ptr[1] << 8U);
}
static unsigned int ush_bmpview_le32(const unsigned char *ptr) {
return (unsigned int)ptr[0] | ((unsigned int)ptr[1] << 8U) | ((unsigned int)ptr[2] << 16U) |
((unsigned int)ptr[3] << 24U);
}
static int ush_bmpview_le32s(const unsigned char *ptr) {
return (int)((unsigned int)ush_bmpview_le32(ptr));
}
static int ush_bmpview_read_exact(u64 fd, unsigned char *out, u64 size) {
u64 done = 0ULL;
if (out == (unsigned char *)0 || size == 0ULL) {
return 0;
}
while (done < size) {
u64 got = cleonos_sys_fd_read(fd, out + done, size - done);
if (got == (u64)-1 || got == 0ULL) {
return 0;
}
done += got;
}
return 1;
}
static int ush_bmpview_skip_bytes(u64 fd, u64 size) {
unsigned char scratch[256];
u64 left = size;
while (left > 0ULL) {
u64 want = left;
u64 got;
if (want > (u64)sizeof(scratch)) {
want = (u64)sizeof(scratch);
}
got = cleonos_sys_fd_read(fd, scratch, want);
if (got == (u64)-1 || got == 0ULL) {
return 0;
}
left -= got;
}
return 1;
}
static int ush_bmpview_parse_header(u64 fd, ush_bmpview_info *out_info) {
unsigned char header[54];
unsigned int dib_size;
int width_s;
int height_s;
unsigned int planes;
unsigned int bits_per_pixel;
unsigned int compression;
u64 width_u;
u64 height_u;
u64 row_stride;
u64 bytes_per_pixel;
if (out_info == (ush_bmpview_info *)0) {
return 0;
}
if (ush_bmpview_read_exact(fd, header, (u64)sizeof(header)) == 0) {
return 0;
}
if (header[0] != (unsigned char)'B' || header[1] != (unsigned char)'M') {
return 0;
}
dib_size = ush_bmpview_le32(&header[14]);
if (dib_size < 40U) {
return 0;
}
width_s = ush_bmpview_le32s(&header[18]);
height_s = ush_bmpview_le32s(&header[22]);
planes = ush_bmpview_le16(&header[26]);
bits_per_pixel = ush_bmpview_le16(&header[28]);
compression = ush_bmpview_le32(&header[30]);
if (planes != 1U) {
return 0;
}
if (width_s <= 0 || height_s == 0) {
return 0;
}
if (bits_per_pixel != 24U && bits_per_pixel != 32U) {
return 0;
}
if (compression != 0U) {
return 0;
}
width_u = (u64)(unsigned int)width_s;
if (height_s < 0) {
i64 signed_h = (i64)height_s;
height_u = (u64)(-signed_h);
out_info->top_down = 1;
} else {
height_u = (u64)(unsigned int)height_s;
out_info->top_down = 0;
}
if (width_u == 0ULL || height_u == 0ULL) {
return 0;
}
if (width_u > USH_BMPVIEW_MAX_IMAGE_WIDTH || height_u > USH_BMPVIEW_MAX_IMAGE_HEIGHT) {
return 0;
}
bytes_per_pixel = (u64)(bits_per_pixel / 8U);
row_stride = (width_u * bytes_per_pixel + 3ULL) & ~3ULL;
if (row_stride == 0ULL || row_stride > (u64)USH_BMPVIEW_MAX_ROW_BYTES) {
return 0;
}
out_info->pixel_offset = (u64)ush_bmpview_le32(&header[10]);
if (out_info->pixel_offset < (u64)sizeof(header)) {
return 0;
}
out_info->width = width_u;
out_info->height = height_u;
out_info->bytes_per_pixel = bytes_per_pixel;
out_info->row_stride = row_stride;
return 1;
}
static char ush_bmpview_luma_char(unsigned int r, unsigned int g, unsigned int b) {
static const char ramp[] = " .:-=+*#%@";
unsigned int luma = (77U * r + 150U * g + 29U * b) >> 8U;
unsigned int idx = (luma * 9U) / 255U;
return ramp[idx];
}
static int ush_bmpview_parse_args(const char *arg, char *out_path, u64 out_path_size, u64 *out_cols) {
char first[USH_PATH_MAX];
char second[32];
const char *rest = "";
const char *rest2 = "";
if (out_path == (char *)0 || out_cols == (u64 *)0 || out_path_size == 0ULL) {
return 0;
}
*out_cols = USH_BMPVIEW_DEFAULT_COLS;
out_path[0] = '\0';
if (arg == (const char *)0 || arg[0] == '\0') {
return 0;
}
if (ush_split_first_and_rest(arg, first, (u64)sizeof(first), &rest) == 0) {
return 0;
}
if (ush_streq(first, "--help") != 0 || ush_streq(first, "-h") != 0) {
return 2;
}
ush_copy(out_path, out_path_size, first);
if (rest != (const char *)0 && rest[0] != '\0') {
if (ush_split_first_and_rest(rest, second, (u64)sizeof(second), &rest2) == 0) {
return 0;
}
if (ush_parse_u64_dec(second, out_cols) == 0 || *out_cols == 0ULL) {
return 0;
}
if (rest2 != (const char *)0 && rest2[0] != '\0') {
return 0;
}
}
return 1;
}
static int ush_cmd_bmpview(const ush_state *sh, const char *arg) {
ush_bmpview_info info;
char path_arg[USH_PATH_MAX];
char abs_path[USH_PATH_MAX];
u64 cols = USH_BMPVIEW_DEFAULT_COLS;
u64 out_w;
u64 out_h;
u64 file_row;
u64 fd;
int parse_ret;
if (sh == (const ush_state *)0) {
return 0;
}
parse_ret = ush_bmpview_parse_args(arg, path_arg, (u64)sizeof(path_arg), &cols);
if (parse_ret == 2) {
ush_writeln("usage: bmpview <file.bmp> [cols]");
ush_writeln("note: supports uncompressed 24/32-bit BMP");
return 1;
}
if (parse_ret == 0) {
ush_writeln("bmpview: usage bmpview <file.bmp> [cols]");
return 0;
}
if (ush_resolve_path(sh, path_arg, abs_path, (u64)sizeof(abs_path)) == 0) {
ush_writeln("bmpview: invalid path");
return 0;
}
if (cleonos_sys_fs_stat_type(abs_path) != 1ULL) {
ush_writeln("bmpview: file not found");
return 0;
}
fd = cleonos_sys_fd_open(abs_path, CLEONOS_O_RDONLY, 0ULL);
if (fd == (u64)-1) {
ush_writeln("bmpview: open failed");
return 0;
}
ush_zero(&info, (u64)sizeof(info));
if (ush_bmpview_parse_header(fd, &info) == 0) {
(void)cleonos_sys_fd_close(fd);
ush_writeln("bmpview: unsupported or invalid bmp");
return 0;
}
if (info.pixel_offset > 54ULL) {
if (ush_bmpview_skip_bytes(fd, info.pixel_offset - 54ULL) == 0) {
(void)cleonos_sys_fd_close(fd);
ush_writeln("bmpview: failed to seek pixel data");
return 0;
}
}
if (cols > USH_BMPVIEW_MAX_COLS) {
cols = USH_BMPVIEW_MAX_COLS;
}
if (cols > info.width) {
cols = info.width;
}
if (cols == 0ULL) {
cols = 1ULL;
}
out_w = cols;
out_h = (info.height * out_w) / (info.width * 2ULL);
if (out_h == 0ULL) {
out_h = 1ULL;
}
if (out_h > USH_BMPVIEW_MAX_ROWS) {
out_h = USH_BMPVIEW_MAX_ROWS;
}
if (out_h > info.height) {
out_h = info.height;
}
{
u64 oy;
u64 ox;
for (oy = 0ULL; oy < out_h; oy++) {
ush_bmpview_sample_rows[oy] = (oy * info.height) / out_h;
for (ox = 0ULL; ox < out_w; ox++) {
ush_bmpview_render[oy][ox] = ' ';
}
ush_bmpview_render[oy][out_w] = '\0';
}
}
for (file_row = 0ULL; file_row < info.height; file_row++) {
u64 display_row;
u64 oy;
if (ush_bmpview_read_exact(fd, ush_bmpview_row_buf, info.row_stride) == 0) {
(void)cleonos_sys_fd_close(fd);
ush_writeln("bmpview: read pixel data failed");
return 0;
}
if (info.top_down != 0) {
display_row = file_row;
} else {
display_row = (info.height - 1ULL) - file_row;
}
for (oy = 0ULL; oy < out_h; oy++) {
u64 ox;
if (ush_bmpview_sample_rows[oy] != display_row) {
continue;
}
for (ox = 0ULL; ox < out_w; ox++) {
u64 src_x = (ox * info.width) / out_w;
u64 pix_off = src_x * info.bytes_per_pixel;
unsigned int b;
unsigned int g;
unsigned int r;
if (pix_off + 2ULL >= info.row_stride) {
ush_bmpview_render[oy][ox] = ' ';
continue;
}
b = (unsigned int)ush_bmpview_row_buf[pix_off + 0ULL];
g = (unsigned int)ush_bmpview_row_buf[pix_off + 1ULL];
r = (unsigned int)ush_bmpview_row_buf[pix_off + 2ULL];
ush_bmpview_render[oy][ox] = ush_bmpview_luma_char(r, g, b);
}
}
}
(void)cleonos_sys_fd_close(fd);
{
u64 oy;
for (oy = 0ULL; oy < out_h; oy++) {
ush_writeln(ush_bmpview_render[oy]);
}
}
return 1;
}
int cleonos_app_main(void) {
ush_cmd_ctx ctx;
ush_cmd_ret ret;
ush_state sh;
char initial_cwd[USH_PATH_MAX];
int has_context = 0;
int success = 0;
const char *arg = "";
ush_zero(&ctx, (u64)sizeof(ctx));
ush_zero(&ret, (u64)sizeof(ret));
ush_init_state(&sh);
ush_copy(initial_cwd, (u64)sizeof(initial_cwd), sh.cwd);
if (ush_command_ctx_read(&ctx) != 0) {
if (ctx.cmd[0] != '\0' && ush_streq(ctx.cmd, "bmpview") != 0) {
has_context = 1;
arg = ctx.arg;
if (ctx.cwd[0] == '/') {
ush_copy(sh.cwd, (u64)sizeof(sh.cwd), ctx.cwd);
ush_copy(initial_cwd, (u64)sizeof(initial_cwd), sh.cwd);
}
}
}
success = ush_cmd_bmpview(&sh, arg);
if (has_context != 0) {
if (ush_streq(sh.cwd, initial_cwd) == 0) {
ret.flags |= USH_CMD_RET_FLAG_CWD;
ush_copy(ret.cwd, (u64)sizeof(ret.cwd), sh.cwd);
}
if (sh.exit_requested != 0) {
ret.flags |= USH_CMD_RET_FLAG_EXIT;
ret.exit_code = sh.exit_code;
}
(void)ush_command_ret_write(&ret);
}
return (success != 0) ? 0 : 1;
}

View File

@@ -13,6 +13,8 @@ static int ush_cmd_help(void) {
ush_writeln(" exec|run <path|name> [args...]");
ush_writeln(" clear");
ush_writeln(" ansi / ansitest / color");
ush_writeln(" bmpview <file.bmp> [cols]");
ush_writeln(" qrcode [--ecc <L|M|Q|H>] <text>");
ush_writeln(" wavplay <file.wav> [steps] [ticks] / wavplay --stop");
ush_writeln(" fastfetch [--plain]");
ush_writeln(" doom [wad_path] (framebuffer bootstrap renderer)");

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,385 @@
/*
* QR Code generator library (C)
*
* Copyright (c) Project Nayuki. (MIT License)
* https://www.nayuki.io/page/qr-code-generator-library
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
* - The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* - The Software is provided "as is", without warranty of any kind, express or
* implied, including but not limited to the warranties of merchantability,
* fitness for a particular purpose and noninfringement. In no event shall the
* authors or copyright holders be liable for any claim, damages or other
* liability, whether in an action of contract, tort or otherwise, arising from,
* out of or in connection with the Software or the use or other dealings in the
* Software.
*/
#pragma once
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/*
* This library creates QR Code symbols, which is a type of two-dimension barcode.
* Invented by Denso Wave and described in the ISO/IEC 18004 standard.
* A QR Code structure is an immutable square grid of dark and light cells.
* The library provides functions to create a QR Code from text or binary data.
* The library covers the QR Code Model 2 specification, supporting all versions (sizes)
* from 1 to 40, all 4 error correction levels, and 4 character encoding modes.
*
* Ways to create a QR Code object:
* - High level: Take the payload data and call qrcodegen_encodeText() or qrcodegen_encodeBinary().
* - Low level: Custom-make the list of segments and call
* qrcodegen_encodeSegments() or qrcodegen_encodeSegmentsAdvanced().
* (Note that all ways require supplying the desired error correction level and various byte buffers.)
*/
/*---- Enum and struct types----*/
/*
* The error correction level in a QR Code symbol.
*/
enum qrcodegen_Ecc {
// Must be declared in ascending order of error protection
// so that an internal qrcodegen function works properly
qrcodegen_Ecc_LOW = 0 , // The QR Code can tolerate about 7% erroneous codewords
qrcodegen_Ecc_MEDIUM , // The QR Code can tolerate about 15% erroneous codewords
qrcodegen_Ecc_QUARTILE, // The QR Code can tolerate about 25% erroneous codewords
qrcodegen_Ecc_HIGH , // The QR Code can tolerate about 30% erroneous codewords
};
/*
* The mask pattern used in a QR Code symbol.
*/
enum qrcodegen_Mask {
// A special value to tell the QR Code encoder to
// automatically select an appropriate mask pattern
qrcodegen_Mask_AUTO = -1,
// The eight actual mask patterns
qrcodegen_Mask_0 = 0,
qrcodegen_Mask_1,
qrcodegen_Mask_2,
qrcodegen_Mask_3,
qrcodegen_Mask_4,
qrcodegen_Mask_5,
qrcodegen_Mask_6,
qrcodegen_Mask_7,
};
/*
* Describes how a segment's data bits are interpreted.
*/
enum qrcodegen_Mode {
qrcodegen_Mode_NUMERIC = 0x1,
qrcodegen_Mode_ALPHANUMERIC = 0x2,
qrcodegen_Mode_BYTE = 0x4,
qrcodegen_Mode_KANJI = 0x8,
qrcodegen_Mode_ECI = 0x7,
};
/*
* A segment of character/binary/control data in a QR Code symbol.
* The mid-level way to create a segment is to take the payload data
* and call a factory function such as qrcodegen_makeNumeric().
* The low-level way to create a segment is to custom-make the bit buffer
* and initialize a qrcodegen_Segment struct with appropriate values.
* Even in the most favorable conditions, a QR Code can only hold 7089 characters of data.
* Any segment longer than this is meaningless for the purpose of generating QR Codes.
* Moreover, the maximum allowed bit length is 32767 because
* the largest QR Code (version 40) has 31329 modules.
*/
struct qrcodegen_Segment {
// The mode indicator of this segment.
enum qrcodegen_Mode mode;
// The length of this segment's unencoded data. Measured in characters for
// numeric/alphanumeric/kanji mode, bytes for byte mode, and 0 for ECI mode.
// Always zero or positive. Not the same as the data's bit length.
int numChars;
// The data bits of this segment, packed in bitwise big endian.
// Can be null if the bit length is zero.
uint8_t *data;
// The number of valid data bits used in the buffer. Requires
// 0 <= bitLength <= 32767, and bitLength <= (capacity of data array) * 8.
// The character count (numChars) must agree with the mode and the bit buffer length.
int bitLength;
};
/*---- Macro constants and functions ----*/
#define qrcodegen_VERSION_MIN 1 // The minimum version number supported in the QR Code Model 2 standard
#define qrcodegen_VERSION_MAX 40 // The maximum version number supported in the QR Code Model 2 standard
// Calculates the number of bytes needed to store any QR Code up to and including the given version number,
// as a compile-time constant. For example, 'uint8_t buffer[qrcodegen_BUFFER_LEN_FOR_VERSION(25)];'
// can store any single QR Code from version 1 to 25 (inclusive). The result fits in an int (or int16).
// Requires qrcodegen_VERSION_MIN <= n <= qrcodegen_VERSION_MAX.
#define qrcodegen_BUFFER_LEN_FOR_VERSION(n) ((((n) * 4 + 17) * ((n) * 4 + 17) + 7) / 8 + 1)
// The worst-case number of bytes needed to store one QR Code, up to and including
// version 40. This value equals 3918, which is just under 4 kilobytes.
// Use this more convenient value to avoid calculating tighter memory bounds for buffers.
#define qrcodegen_BUFFER_LEN_MAX qrcodegen_BUFFER_LEN_FOR_VERSION(qrcodegen_VERSION_MAX)
/*---- Functions (high level) to generate QR Codes ----*/
/*
* Encodes the given text string to a QR Code, returning true if successful.
* If the data is too long to fit in any version in the given range
* at the given ECC level, then false is returned.
*
* The input text must be encoded in UTF-8 and contain no NULs.
* Requires 1 <= minVersion <= maxVersion <= 40.
*
* The smallest possible QR Code version within the given range is automatically
* chosen for the output. Iff boostEcl is true, then the ECC level of the result
* may be higher than the ecl argument if it can be done without increasing the
* version. The mask is either between qrcodegen_Mask_0 to 7 to force that mask, or
* qrcodegen_Mask_AUTO to automatically choose an appropriate mask (which may be slow).
*
* About the arrays, letting len = qrcodegen_BUFFER_LEN_FOR_VERSION(maxVersion):
* - Before calling the function:
* - The array ranges tempBuffer[0 : len] and qrcode[0 : len] must allow
* reading and writing; hence each array must have a length of at least len.
* - The two ranges must not overlap (aliasing).
* - The initial state of both ranges can be uninitialized
* because the function always writes before reading.
* - After the function returns:
* - Both ranges have no guarantee on which elements are initialized and what values are stored.
* - tempBuffer contains no useful data and should be treated as entirely uninitialized.
* - If successful, qrcode can be passed into qrcodegen_getSize() and qrcodegen_getModule().
*
* If successful, the resulting QR Code may use numeric,
* alphanumeric, or byte mode to encode the text.
*
* In the most optimistic case, a QR Code at version 40 with low ECC
* can hold any UTF-8 string up to 2953 bytes, or any alphanumeric string
* up to 4296 characters, or any digit string up to 7089 characters.
* These numbers represent the hard upper limit of the QR Code standard.
*
* Please consult the QR Code specification for information on
* data capacities per version, ECC level, and text encoding mode.
*/
bool qrcodegen_encodeText(const char *text, uint8_t tempBuffer[], uint8_t qrcode[],
enum qrcodegen_Ecc ecl, int minVersion, int maxVersion, enum qrcodegen_Mask mask, bool boostEcl);
/*
* Encodes the given binary data to a QR Code, returning true if successful.
* If the data is too long to fit in any version in the given range
* at the given ECC level, then false is returned.
*
* Requires 1 <= minVersion <= maxVersion <= 40.
*
* The smallest possible QR Code version within the given range is automatically
* chosen for the output. Iff boostEcl is true, then the ECC level of the result
* may be higher than the ecl argument if it can be done without increasing the
* version. The mask is either between qrcodegen_Mask_0 to 7 to force that mask, or
* qrcodegen_Mask_AUTO to automatically choose an appropriate mask (which may be slow).
*
* About the arrays, letting len = qrcodegen_BUFFER_LEN_FOR_VERSION(maxVersion):
* - Before calling the function:
* - The array ranges dataAndTemp[0 : len] and qrcode[0 : len] must allow
* reading and writing; hence each array must have a length of at least len.
* - The two ranges must not overlap (aliasing).
* - The input array range dataAndTemp[0 : dataLen] should normally be
* valid UTF-8 text, but is not required by the QR Code standard.
* - The initial state of dataAndTemp[dataLen : len] and qrcode[0 : len]
* can be uninitialized because the function always writes before reading.
* - After the function returns:
* - Both ranges have no guarantee on which elements are initialized and what values are stored.
* - dataAndTemp contains no useful data and should be treated as entirely uninitialized.
* - If successful, qrcode can be passed into qrcodegen_getSize() and qrcodegen_getModule().
*
* If successful, the resulting QR Code will use byte mode to encode the data.
*
* In the most optimistic case, a QR Code at version 40 with low ECC can hold any byte
* sequence up to length 2953. This is the hard upper limit of the QR Code standard.
*
* Please consult the QR Code specification for information on
* data capacities per version, ECC level, and text encoding mode.
*/
bool qrcodegen_encodeBinary(uint8_t dataAndTemp[], size_t dataLen, uint8_t qrcode[],
enum qrcodegen_Ecc ecl, int minVersion, int maxVersion, enum qrcodegen_Mask mask, bool boostEcl);
/*---- Functions (low level) to generate QR Codes ----*/
/*
* Encodes the given segments to a QR Code, returning true if successful.
* If the data is too long to fit in any version at the given ECC level,
* then false is returned.
*
* The smallest possible QR Code version is automatically chosen for
* the output. The ECC level of the result may be higher than the
* ecl argument if it can be done without increasing the version.
*
* About the byte arrays, letting len = qrcodegen_BUFFER_LEN_FOR_VERSION(qrcodegen_VERSION_MAX):
* - Before calling the function:
* - The array ranges tempBuffer[0 : len] and qrcode[0 : len] must allow
* reading and writing; hence each array must have a length of at least len.
* - The two ranges must not overlap (aliasing).
* - The initial state of both ranges can be uninitialized
* because the function always writes before reading.
* - The input array segs can contain segments whose data buffers overlap with tempBuffer.
* - After the function returns:
* - Both ranges have no guarantee on which elements are initialized and what values are stored.
* - tempBuffer contains no useful data and should be treated as entirely uninitialized.
* - Any segment whose data buffer overlaps with tempBuffer[0 : len]
* must be treated as having invalid values in that array.
* - If successful, qrcode can be passed into qrcodegen_getSize() and qrcodegen_getModule().
*
* Please consult the QR Code specification for information on
* data capacities per version, ECC level, and text encoding mode.
*
* This function allows the user to create a custom sequence of segments that switches
* between modes (such as alphanumeric and byte) to encode text in less space.
* This is a low-level API; the high-level API is qrcodegen_encodeText() and qrcodegen_encodeBinary().
*/
bool qrcodegen_encodeSegments(const struct qrcodegen_Segment segs[], size_t len,
enum qrcodegen_Ecc ecl, uint8_t tempBuffer[], uint8_t qrcode[]);
/*
* Encodes the given segments to a QR Code, returning true if successful.
* If the data is too long to fit in any version in the given range
* at the given ECC level, then false is returned.
*
* Requires 1 <= minVersion <= maxVersion <= 40.
*
* The smallest possible QR Code version within the given range is automatically
* chosen for the output. Iff boostEcl is true, then the ECC level of the result
* may be higher than the ecl argument if it can be done without increasing the
* version. The mask is either between qrcodegen_Mask_0 to 7 to force that mask, or
* qrcodegen_Mask_AUTO to automatically choose an appropriate mask (which may be slow).
*
* About the byte arrays, letting len = qrcodegen_BUFFER_LEN_FOR_VERSION(qrcodegen_VERSION_MAX):
* - Before calling the function:
* - The array ranges tempBuffer[0 : len] and qrcode[0 : len] must allow
* reading and writing; hence each array must have a length of at least len.
* - The two ranges must not overlap (aliasing).
* - The initial state of both ranges can be uninitialized
* because the function always writes before reading.
* - The input array segs can contain segments whose data buffers overlap with tempBuffer.
* - After the function returns:
* - Both ranges have no guarantee on which elements are initialized and what values are stored.
* - tempBuffer contains no useful data and should be treated as entirely uninitialized.
* - Any segment whose data buffer overlaps with tempBuffer[0 : len]
* must be treated as having invalid values in that array.
* - If successful, qrcode can be passed into qrcodegen_getSize() and qrcodegen_getModule().
*
* Please consult the QR Code specification for information on
* data capacities per version, ECC level, and text encoding mode.
*
* This function allows the user to create a custom sequence of segments that switches
* between modes (such as alphanumeric and byte) to encode text in less space.
* This is a low-level API; the high-level API is qrcodegen_encodeText() and qrcodegen_encodeBinary().
*/
bool qrcodegen_encodeSegmentsAdvanced(const struct qrcodegen_Segment segs[], size_t len, enum qrcodegen_Ecc ecl,
int minVersion, int maxVersion, enum qrcodegen_Mask mask, bool boostEcl, uint8_t tempBuffer[], uint8_t qrcode[]);
/*
* Tests whether the given string can be encoded as a segment in numeric mode.
* A string is encodable iff each character is in the range 0 to 9.
*/
bool qrcodegen_isNumeric(const char *text);
/*
* Tests whether the given string can be encoded as a segment in alphanumeric mode.
* A string is encodable iff each character is in the following set: 0 to 9, A to Z
* (uppercase only), space, dollar, percent, asterisk, plus, hyphen, period, slash, colon.
*/
bool qrcodegen_isAlphanumeric(const char *text);
/*
* Returns the number of bytes (uint8_t) needed for the data buffer of a segment
* containing the given number of characters using the given mode. Notes:
* - Returns SIZE_MAX on failure, i.e. numChars > INT16_MAX or the internal
* calculation of the number of needed bits exceeds INT16_MAX (i.e. 32767).
* - Otherwise, all valid results are in the range [0, ceil(INT16_MAX / 8)], i.e. at most 4096.
* - It is okay for the user to allocate more bytes for the buffer than needed.
* - For byte mode, numChars measures the number of bytes, not Unicode code points.
* - For ECI mode, numChars must be 0, and the worst-case number of bytes is returned.
* An actual ECI segment can have shorter data. For non-ECI modes, the result is exact.
*/
size_t qrcodegen_calcSegmentBufferSize(enum qrcodegen_Mode mode, size_t numChars);
/*
* Returns a segment representing the given binary data encoded in
* byte mode. All input byte arrays are acceptable. Any text string
* can be converted to UTF-8 bytes and encoded as a byte mode segment.
*/
struct qrcodegen_Segment qrcodegen_makeBytes(const uint8_t data[], size_t len, uint8_t buf[]);
/*
* Returns a segment representing the given string of decimal digits encoded in numeric mode.
*/
struct qrcodegen_Segment qrcodegen_makeNumeric(const char *digits, uint8_t buf[]);
/*
* Returns a segment representing the given text string encoded in alphanumeric mode.
* The characters allowed are: 0 to 9, A to Z (uppercase only), space,
* dollar, percent, asterisk, plus, hyphen, period, slash, colon.
*/
struct qrcodegen_Segment qrcodegen_makeAlphanumeric(const char *text, uint8_t buf[]);
/*
* Returns a segment representing an Extended Channel Interpretation
* (ECI) designator with the given assignment value.
*/
struct qrcodegen_Segment qrcodegen_makeEci(long assignVal, uint8_t buf[]);
/*---- Functions to extract raw data from QR Codes ----*/
/*
* Returns the side length of the given QR Code, assuming that encoding succeeded.
* The result is in the range [21, 177]. Note that the length of the array buffer
* is related to the side length - every 'uint8_t qrcode[]' must have length at least
* qrcodegen_BUFFER_LEN_FOR_VERSION(version), which equals ceil(size^2 / 8 + 1).
*/
int qrcodegen_getSize(const uint8_t qrcode[]);
/*
* Returns the color of the module (pixel) at the given coordinates, which is false
* for light or true for dark. The top left corner has the coordinates (x=0, y=0).
* If the given coordinates are out of bounds, then false (light) is returned.
*/
bool qrcodegen_getModule(const uint8_t qrcode[], int x, int y);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,265 @@
#include "cmd_runtime.h"
#include "qrcode/qrcodegen.h"
#define USH_QRCODE_MAX_TEXT 640U
#define USH_QRCODE_MAX_VERSION 15
#define USH_QRCODE_BORDER 2
static int ush_qrcode_streq_ignore_case(const char *left, const char *right) {
u64 i = 0ULL;
if (left == (const char *)0 || right == (const char *)0) {
return 0;
}
while (left[i] != '\0' && right[i] != '\0') {
char lc = left[i];
char rc = right[i];
if (lc >= 'a' && lc <= 'z') {
lc = (char)(lc - ('a' - 'A'));
}
if (rc >= 'a' && rc <= 'z') {
rc = (char)(rc - ('a' - 'A'));
}
if (lc != rc) {
return 0;
}
i++;
}
return (left[i] == '\0' && right[i] == '\0') ? 1 : 0;
}
static int ush_qrcode_parse_ecc(const char *text, enum qrcodegen_Ecc *out_ecc) {
if (text == (const char *)0 || out_ecc == (enum qrcodegen_Ecc *)0) {
return 0;
}
if (ush_qrcode_streq_ignore_case(text, "L") != 0 || ush_qrcode_streq_ignore_case(text, "LOW") != 0) {
*out_ecc = qrcodegen_Ecc_LOW;
return 1;
}
if (ush_qrcode_streq_ignore_case(text, "M") != 0 || ush_qrcode_streq_ignore_case(text, "MEDIUM") != 0) {
*out_ecc = qrcodegen_Ecc_MEDIUM;
return 1;
}
if (ush_qrcode_streq_ignore_case(text, "Q") != 0 || ush_qrcode_streq_ignore_case(text, "QUARTILE") != 0) {
*out_ecc = qrcodegen_Ecc_QUARTILE;
return 1;
}
if (ush_qrcode_streq_ignore_case(text, "H") != 0 || ush_qrcode_streq_ignore_case(text, "HIGH") != 0) {
*out_ecc = qrcodegen_Ecc_HIGH;
return 1;
}
return 0;
}
static void ush_qrcode_usage(void) {
ush_writeln("usage: qrcode [--ecc <L|M|Q|H>] <text>");
ush_writeln(" qrcode --help");
ush_writeln("note: pipeline input supported when <text> omitted");
}
/* return: 0 fail, 1 ok, 2 help */
static int ush_qrcode_parse_args(const char *arg, char *out_text, u64 out_text_size, enum qrcodegen_Ecc *out_ecc) {
char first[USH_PATH_MAX];
char second[USH_PATH_MAX];
const char *rest = "";
const char *rest2 = "";
if (out_text == (char *)0 || out_text_size == 0ULL || out_ecc == (enum qrcodegen_Ecc *)0) {
return 0;
}
*out_ecc = qrcodegen_Ecc_LOW;
out_text[0] = '\0';
if (arg == (const char *)0 || arg[0] == '\0') {
if (ush_pipeline_stdin_text != (const char *)0 && ush_pipeline_stdin_text[0] != '\0') {
if (ush_pipeline_stdin_len + 1ULL > out_text_size) {
return 0;
}
ush_copy(out_text, out_text_size, ush_pipeline_stdin_text);
return 1;
}
return 0;
}
if (ush_split_first_and_rest(arg, first, (u64)sizeof(first), &rest) == 0) {
return 0;
}
if (ush_streq(first, "--help") != 0 || ush_streq(first, "-h") != 0) {
return 2;
}
if (ush_streq(first, "--ecc") != 0) {
if (ush_split_first_and_rest(rest, second, (u64)sizeof(second), &rest2) == 0) {
return 0;
}
if (ush_qrcode_parse_ecc(second, out_ecc) == 0) {
return 0;
}
if (rest2 == (const char *)0 || rest2[0] == '\0') {
if (ush_pipeline_stdin_text != (const char *)0 && ush_pipeline_stdin_text[0] != '\0') {
if (ush_pipeline_stdin_len + 1ULL > out_text_size) {
return 0;
}
ush_copy(out_text, out_text_size, ush_pipeline_stdin_text);
return 1;
}
return 0;
}
ush_copy(out_text, out_text_size, rest2);
return 1;
}
if (first[0] == '-' && first[1] == '-') {
const char *prefix = "--ecc=";
u64 i = 0ULL;
int match = 1;
while (prefix[i] != '\0') {
if (first[i] != prefix[i]) {
match = 0;
break;
}
i++;
}
if (match != 0) {
if (first[i] == '\0') {
return 0;
}
if (ush_qrcode_parse_ecc(&first[i], out_ecc) == 0) {
return 0;
}
if (rest == (const char *)0 || rest[0] == '\0') {
if (ush_pipeline_stdin_text != (const char *)0 && ush_pipeline_stdin_text[0] != '\0') {
if (ush_pipeline_stdin_len + 1ULL > out_text_size) {
return 0;
}
ush_copy(out_text, out_text_size, ush_pipeline_stdin_text);
return 1;
}
return 0;
}
ush_copy(out_text, out_text_size, rest);
return 1;
}
}
ush_copy(out_text, out_text_size, arg);
return 1;
}
static void ush_qrcode_emit_ascii(const uint8_t qrcode[]) {
int size = qrcodegen_getSize(qrcode);
int y;
int x;
for (y = -USH_QRCODE_BORDER; y < size + USH_QRCODE_BORDER; y++) {
for (x = -USH_QRCODE_BORDER; x < size + USH_QRCODE_BORDER; x++) {
int dark = (x >= 0 && y >= 0 && x < size && y < size && qrcodegen_getModule(qrcode, x, y)) ? 1 : 0;
ush_write(dark != 0 ? "##" : " ");
}
ush_write_char('\n');
}
}
static int ush_cmd_qrcode(const char *arg) {
char text[USH_QRCODE_MAX_TEXT];
enum qrcodegen_Ecc ecc;
uint8_t qrcode[qrcodegen_BUFFER_LEN_FOR_VERSION(USH_QRCODE_MAX_VERSION)];
uint8_t temp[qrcodegen_BUFFER_LEN_FOR_VERSION(USH_QRCODE_MAX_VERSION)];
int parse_ret;
int ok;
parse_ret = ush_qrcode_parse_args(arg, text, (u64)sizeof(text), &ecc);
if (parse_ret == 2) {
ush_qrcode_usage();
return 1;
}
if (parse_ret == 0 || text[0] == '\0') {
ush_qrcode_usage();
return 0;
}
ok = qrcodegen_encodeText(text, temp, qrcode, ecc, qrcodegen_VERSION_MIN, USH_QRCODE_MAX_VERSION,
qrcodegen_Mask_AUTO, true);
if (ok == 0) {
ush_writeln("qrcode: encode failed (input too long or invalid)");
return 0;
}
ush_qrcode_emit_ascii(qrcode);
return 1;
}
int cleonos_app_main(void) {
ush_cmd_ctx ctx;
ush_cmd_ret ret;
ush_state sh;
char initial_cwd[USH_PATH_MAX];
int has_context = 0;
int success = 0;
const char *arg = "";
ush_zero(&ctx, (u64)sizeof(ctx));
ush_zero(&ret, (u64)sizeof(ret));
ush_init_state(&sh);
ush_copy(initial_cwd, (u64)sizeof(initial_cwd), sh.cwd);
if (ush_command_ctx_read(&ctx) != 0) {
if (ctx.cmd[0] != '\0' && ush_streq(ctx.cmd, "qrcode") != 0) {
has_context = 1;
arg = ctx.arg;
if (ctx.cwd[0] == '/') {
ush_copy(sh.cwd, (u64)sizeof(sh.cwd), ctx.cwd);
ush_copy(initial_cwd, (u64)sizeof(initial_cwd), sh.cwd);
}
}
}
success = ush_cmd_qrcode(arg);
if (has_context != 0) {
if (ush_streq(sh.cwd, initial_cwd) == 0) {
ret.flags |= USH_CMD_RET_FLAG_CWD;
ush_copy(ret.cwd, (u64)sizeof(ret.cwd), sh.cwd);
}
if (sh.exit_requested != 0) {
ret.flags |= USH_CMD_RET_FLAG_EXIT;
ret.exit_code = sh.exit_code;
}
(void)ush_command_ret_write(&ret);
}
return (success != 0) ? 0 : 1;
}

View File

@@ -135,6 +135,8 @@ static int ush_cmd_help(void) {
ush_writeln(" exec|run <path|name> [args...]");
ush_writeln(" clear");
ush_writeln(" ansi / ansitest / color");
ush_writeln(" bmpview <file.bmp> [cols]");
ush_writeln(" qrcode [--ecc <L|M|Q|H>] <text>");
ush_writeln(" wavplay <file.wav> [steps] [ticks] / wavplay --stop");
ush_writeln(" fastfetch [--plain]");
ush_writeln(" doom [wad_path] (framebuffer bootstrap renderer)");