Added size suffixes

This commit is contained in:
Your Name
2025-09-28 08:57:43 -04:00
parent 52ed7af980
commit 1819ce7a32
5 changed files with 210 additions and 1059 deletions

160
README.md
View File

@@ -4,7 +4,7 @@ This is a C equivalent of the Python `main.py` script for reading data from True
## Overview
The program reads random data from a TrueRNG device via serial port, counts the number of ones and zeros in the data, and saves the raw data to a binary file called `random.bin`.
The program reads random data from a TrueRNG device via serial port, counts the number of ones and zeros in the data, and provides flexible output options including multiple formats and size specifications.
## Supported Devices
@@ -32,24 +32,83 @@ gcc -Wall -Wextra -std=c99 -O2 -o truerng main.c
## Usage
### Basic Usage
```bash
./truerng
```
The program will:
1. Automatically detect connected TrueRNG devices
2. Read 1024 blocks of 1024 bytes each (1MB total)
3. Count ones and zeros in real-time
4. Display read rate in KB/s
5. Save raw data to `random.bin`
6. Display final statistics
### Command Line Options
```bash
./truerng [OPTIONS]
```
**Options:**
- `-n, --bytes <N>` - Number of bytes to generate (default: 1048576)
- Supports size suffixes: K, MB, GB, TB (case-insensitive)
- Examples: `1024`, `1K`, `2.5MB`, `1GB`, `512k`, `1tb`
- `-f, --format <FORMAT>` - Output format: binary, hex, base64, decimal
- Default: binary when piped, hex in interactive mode
- `-o, --output <FILE>` - Output filename (works with all formats)
- `-q, --quiet` - Suppress statistics/progress output
- `-v, --verbose` - Show detailed device information
- `-h, --help` - Show help message
### Size Suffixes
The `-n` option supports convenient size suffixes (case-insensitive):
- **K** or **KB**: Kilobytes (1024 bytes)
- **M** or **MB**: Megabytes (1,048,576 bytes)
- **G** or **GB**: Gigabytes (1,073,741,824 bytes)
- **T** or **TB**: Terabytes (1,099,511,627,776 bytes)
Decimal values are supported: `1.5MB`, `2.5GB`, etc.
### Examples
```bash
# Basic usage - interactive mode with hex output
./truerng
# Generate 1KB of data in hex format
./truerng -n 1K -f hex
# Generate 2.5MB and pipe to another program
./truerng -n 2.5MB | xxd
# Save 1GB of binary data to file quietly
./truerng -n 1GB -o random.dat -q
# Generate 512KB as hex and save to file
./truerng -n 512K -f hex -o output.hex
# Generate base64 output with verbose device info
./truerng -n 1MB -f base64 -v
# Backward compatibility - plain numbers still work
./truerng -n 1048576 -f binary
```
### Output Formats
- **binary**: Raw binary data (default for piped output)
- **hex**: Hexadecimal representation (default for interactive mode)
- **base64**: Base64 encoded output
- **decimal**: Decimal byte values
### Modes
The program automatically detects the execution context:
- **Interactive mode**: Shows progress, statistics, and defaults to hex output
- **Piped mode**: Optimized for piping, defaults to binary output
- **File output**: Can be combined with any format using `-o` option
## Configuration
You can modify the following constants in [`main.c`](main.c:32):
You can modify the following constants in [`main.c`](main.c:34):
- `BLOCKSIZE`: Number of bytes to read per block (default: 1024)
- `NUMLOOPS`: Number of blocks to read (default: 1024)
## Permissions
@@ -70,30 +129,77 @@ The C implementation:
- Uses native Linux serial port APIs instead of pyserial
- Implements USB device detection via sysfs
- Uses lookup tables for efficient bit counting
- Provides enhanced command line interface with size suffixes
- Supports multiple output formats (binary, hex, base64, decimal)
- Provides equivalent functionality with better performance
## Output
## Sample Output
Sample output:
### Interactive Mode
```
TrueRNG Counting Ones vs Zeros Example
http://ubld.it
TrueRNG - True Random Number Generator
==================================================
TrueRNG Found
Using com port: /dev/ttyUSB0
Block Size: 1024 Bytes
Number of loops: 1024
Total size: 1048576 Bytes
Writing to: random.bin
==================================================
1048576 Bytes Read at 45.23 Kbytes/s
TrueRNGproV2 Found
Using port: /dev/ttyACM0
Generating: 1024 bytes
Starting data collection...
1024/1024 Bytes Read at 504.19 Kbytes/s
Results
=======
Total Ones : 4194234
Total Zeros: 4194314
Total time: 0.02 seconds
Total Ones: 4072
Total Zeros: 4120
Total Bits: 8192
Extra zeros: 48
=======
```
Total Bits : 8388608
### Piped Mode with Verbose
```
TrueRNG - True Random Number Generator
Mode: Piped (Verbose)
==================================================
TrueRNGproV2 Found
Using port: /dev/ttyACM0
Generating: 2048 bytes
Starting data collection...
2048/2048 Bytes Read at 462.17 Kbytes/s
There are 80 more zeros in the Capture!
=======
Results
=======
Total time: 0.04 seconds
Total Ones: 8144
Total Zeros: 8248
Total Bits: 16392
Extra zeros: 104
=======
```
### Quiet Mode
```bash
./truerng -n 1K -o data.bin -q
# No output - data saved silently to data.bin
```
### Help Output
```bash
./truerng --help
TrueRNG - True Random Number Generator
Usage: truerng [OPTIONS]
Options:
-n, --bytes <N> Number of bytes to generate (default: 1048576)
Supports suffixes: K, MB, GB, TB (e.g., 1K, 2.5MB, 1GB)
-f, --format <FORMAT> Output format: binary, hex, base64, decimal (default: binary when piped, interactive otherwise)
-o, --output <FILE> Output filename (ignored in piped mode)
-q, --quiet Suppress statistics/progress
-v, --verbose Show detailed device information
-h, --help Show this help message
Examples:
truerng -n 1024 -f hex # Interactive mode with hex output
truerng -n 1K -f hex # Same as above using K suffix
truerng -n 2.5MB | xxd # Piped mode with MB suffix
truerng -n 1GB -o random.dat -q # Save 1GB to file quietly
truerng -n 512K -f hex -o output.hex # Save 512KB as hex to file