Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

we are at working stage with md5 #1

Merged
merged 5 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

3 changes: 2 additions & 1 deletion .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
{
"name": "Linux",
"includePath": [
"${default}"
"${default}",
"${Z88DK_HOME}/include"
],
"defines": [],
"compilerPath": "${Z88DK_HOME}/bin/zcc"
Expand Down
14 changes: 13 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,19 @@
"unistd.h": "c",
"string.h": "c",
"arch.h": "c",
"config_zx.h": "c"
"config_zx.h": "c",
"cstdio": "c",
"fstream": "c",
"err.h": "c",
"random": "c",
"md5.h": "c",
"stdlib.h": "c",
"cli.h": "c",
"memory_resource": "c",
"strings.h": "c",
"errno.h": "c",
"string": "c",
"string_view": "c"
},
"C_Cpp.default.includePath": [
"${default}",
Expand Down
Empty file added md5sum/LICENSE
Empty file.
Binary file modified md5sum/build/MD5SUM
Binary file not shown.
99 changes: 99 additions & 0 deletions md5sum/lib/cli/cli.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#include "cli.h"

// Global variables
CommandLineOptions CLO;

// CLI arguments
static char *cliOptions[] = {
// Show help
"-h",
"--help",
// filename
"-f",
// Show progress
"-p",
// Output file
"-o",
};

#define OP_H cliOptions[0]
#define OP_HELP cliOptions[1]
#define OP_F cliOptions[2]
#define OP_P cliOptions[3]
#define OP_O cliOptions[4]

void usage(void) {
puts("usage: .md5sum -f [-p | -o | -h]");
}
void help() {
puts("v1.0.0 by Sergii Skorokhodov");
puts("md5sum - Print MD5 (128-bit) checksums.");
puts("");
usage();
puts("");
puts("DESCRIPTION");
puts("\n-h, --help\tShow this help");
puts("-f\t\tSpecify input file (Required)");
puts("-o\t\tSpecify output file");
puts("\t\tIf omitted, result printed to stdout");
puts("\t\tIf specified without a file, a .md5 file will be created at the same path as the original");
puts("\nDerived from RSA Data Security, ported to ZXNext");
}

/**
* @brief
*
* @param str - The string
* @param prefix - The prefix to match against
* @return int
*/
int startsWith(const char *str, const char *prefix) {
// Compare the first strlen(prefix) characters of str with prefix
return strncmp(str, prefix, strlen(prefix)) == 0;
}

CommandLineOptions* getCommandLineOptions(int argc, char *argv[]) {
// Init the structure with the list of empty values
CLO.file = 0;
CLO.help = 0;
CLO.progress = 0;
CLO.output = 0;

if (argc < 2) {
usage();
return CLI_ERROR_STATUS;
}

for (unsigned char i = 1; i < argc; i++) {
// Check for the help
if (strcmp(argv[i], OP_H) == 0 || strcmp(argv[i], OP_HELP) == 0) {
CLO.help = 1;
}

if (strcmp(argv[i], OP_F) == 0) {
CLO.file = 1;
// increase iterator, as the filename parameter
// must be placed exactly after the `-f` option
i++;
// Save the reference
CLO.filename = argv[i];
}

// To show progress
if (strcmp(argv[i], OP_P) == 0) {
CLO.progress = 1;
}

// To generate output file
if (strcmp(argv[i], OP_O) == 0) {
CLO.output = 1;
// Check if the next argument is an option
if (startsWith(argv[i + 1], "-") != 1) {
i++;
// if not, save the reference
CLO.outputFilename = argv[i];
}
}
}
return &CLO;
}
22 changes: 22 additions & 0 deletions md5sum/lib/cli/cli.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef CLI_H
#define CLI_H

#include <stdio.h>
#include <string.h>

typedef struct {
unsigned char help; // Show help
unsigned char progress; // Show progress
unsigned char file; // Use file for input
unsigned char output; // To produce the ouptut file with the hash result
char *filename; // The filename to operate with
char *outputFilename; // Output filename
} CommandLineOptions;

#define CLI_ERROR_STATUS 0

CommandLineOptions* getCommandLineOptions(int argc, char *argv[]);
void usage(void);
void help(void);

#endif
59 changes: 40 additions & 19 deletions md5sum/md5.c → md5sum/lib/md5/md5.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,29 +195,50 @@ void md5Step(uint32_t *buffer, uint32_t *input){
* Functions that run the algorithm on the provided input and put the digest into result.
* result should be able to store 16 bytes.
*/
void md5String(char *input, uint8_t *result){
MD5Context ctx;
md5Init(&ctx);
md5Update(&ctx, (uint8_t *)input, strlen(input));
md5Finalize(&ctx);
// void md5String(char *input, uint8_t *result){
// MD5Context ctx;
// md5Init(&ctx);
// md5Update(&ctx, (uint8_t *)input, strlen(input));
// md5Finalize(&ctx);

memcpy(result, ctx.digest, 16);
}
// memcpy(result, ctx.digest, 16);
// }

// void md5File(FILE *file, uint8_t *result){
// char *input_buffer = malloc(1024);
// size_t input_size = 0;
void md5File(
unsigned char *file,
uint8_t *result,
uint8_t progress
){
unsigned char input_buffer[MD5_READ_CHUNK_SIZE];
uint16_t bytesRead = 0;
uint16_t br = 0;

// MD5Context ctx;
// md5Init(&ctx);
MD5Context ctx;
md5Init(&ctx);

// while((input_size = fread(input_buffer, 1, 1024, file)) > 0){
// md5Update(&ctx, (uint8_t *)input_buffer, input_size);
// }
do {
bytesRead = esx_f_read(
*file,
input_buffer,
MD5_READ_CHUNK_SIZE
);
if (progress == 1) {
putchar('.');
}
if (bytesRead > 0) {
md5Update(
&ctx,
(uint8_t *)input_buffer,
bytesRead
);
}
} while (bytesRead > 0);

// md5Finalize(&ctx);
md5Finalize(&ctx);

// free(input_buffer);
memcpy(result, ctx.digest, 16);

// memcpy(result, ctx.digest, 16);
// }
if (progress == 1) {
puts("\n");
}
}
11 changes: 9 additions & 2 deletions md5sum/md5.h → md5sum/lib/md5/md5.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <arch/zxn/esxdos.h>

#define MD5_READ_CHUNK_SIZE 1024

typedef struct{
uint64_t size; // Size of input in bytes
Expand All @@ -18,7 +21,11 @@ void md5Update(MD5Context *ctx, uint8_t *input, size_t input_len);
void md5Finalize(MD5Context *ctx);
void md5Step(uint32_t *buffer, uint32_t *input);

void md5String(char *input, uint8_t *result);
// void md5File(FILE *file, uint8_t *result);
// void md5String(char *input, uint8_t *result);
void md5File(
unsigned char *file,
uint8_t *result,
uint8_t progress
);

#endif
124 changes: 95 additions & 29 deletions md5sum/main.c
Original file line number Diff line number Diff line change
@@ -1,47 +1,113 @@
#define __ZXNEXT 1

#include <stdio.h>
#include <strings.h>
#include <arch/zxn.h>
#include <arch/zxn/esxdos.h>
#include "md5.h"
#include <errno.h>
#include "lib/cli/cli.h"
#include "lib/md5/md5.h"

// Missing 28mhz variable
#ifndef __RTM_28MHZ
#define __RTM_28MHZ 0x03
#endif

void print_hash(uint8_t *p){
// global variables
unsigned char fin = 0xff; // file descriptor
unsigned char fout = 0xff; // file descriptor

char errorMessage[] = "Error opening file: ";

// clean up at exit
static unsigned char old_cpu_speed;
void cleanup(void) {
// Ensure all files are closed
if (fin != 0xff) esx_f_close(fin);
if (fout != 0xff) esx_f_close(fout);
ZXN_NEXTREGA(REG_TURBO_MODE, old_cpu_speed);
}

// hash value to string
void hashToString(uint8_t *p, char *hashString){
for(unsigned int i = 0; i < 16; ++i){
printf("%02x", p[i]);
sprintf(hashString + i * 2, "%02x", p[i]);
}
printf("\n");
}

// global variables
int main(int argc, char *argv[]) {
uint32_t len = 0; // File length
uint8_t result[16]; // MD5 result
char outputFilename[128]; // Auto generated output filename (original filename + .md5)
char hashString[32];

unsigned char fin = 0xff; // file descriptor
unsigned char fout = 0xff; // file descriptor
CommandLineOptions* args = getCommandLineOptions(argc, argv);

// clean up at exit
// In case of errors, terminate now
if (args == CLI_ERROR_STATUS) {
return 2;
}

static unsigned char old_cpu_speed;
// Show help and do not go any furhter
if (args->help) {
help();
return 0;
}

void cleanup(void)
{
if (fin != 0xff) esx_f_close(fin);
if (fout != 0xff) esx_f_close(fout);

puts(" ");
// Prepare
old_cpu_speed = ZXN_READ_REG(REG_TURBO_MODE);
ZXN_NEXTREG(REG_TURBO_MODE, __RTM_28MHZ);
atexit(cleanup);

ZXN_NEXTREGA(REG_TURBO_MODE, old_cpu_speed);
}
if (args->file == 0) {
// error!
puts("Please specify the file name!");
return 1;
} else if (args->file == 1) {
// We use file as a source
// Try to open the file
fin = esx_f_open(args->filename, ESXDOS_MODE_R);
if (errno) {
printf("%s%s\n", errorMessage, args->filename);
return 2;
}

md5File(&fin, result, args->progress);

// Close the file
esx_f_close(fin);
}

// Turn a hash to the string
hashToString(result, hashString);

if (args->output == 0) {
// Result to the screen
printf("%s", hashString);
} else if (args->output == 1) {
// Result to the file
if (args->outputFilename != NULL) {
// Try to open the file
fout = esx_f_open(args->outputFilename, ESXDOS_MODE_CT | ESXDOS_MODE_W);
} else {
strcpy(outputFilename, args->filename);
strcat(outputFilename, ".md5");
// Try to open the file
fout = esx_f_open(outputFilename, ESXDOS_MODE_CT | ESXDOS_MODE_W);
}
if (errno) {
printf("%s%s\n", errorMessage, args->outputFilename);
return 2;
}

// Save result to the file
esx_f_write(
fout,
hashString,
32 // MD5 hash is always 32 characters long, hence 32 bytes
);
esx_f_close(fout);
}

int main()
{
old_cpu_speed = ZXN_READ_REG(REG_TURBO_MODE);
ZXN_NEXTREG(REG_TURBO_MODE, RTM_14MHZ);

atexit(cleanup);

puts("Hello next!");
uint8_t result[16];
md5String("Hello next!", result);
print_hash(result);
return 0;
return 0;
}
Loading