Fixed buffer overflows, makefile

This commit is contained in:
Mattia Mascarello 2022-10-04 15:48:54 +02:00
parent 751d2040c7
commit 58ff0b92a7
3 changed files with 104 additions and 103 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
ropipe

View File

@ -26,3 +26,4 @@ endif
update: update:
git pull git pull
make install make install

View File

@ -1,15 +1,18 @@
#include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#define RDIGITS 14 #define RDIGITS 14
int validateN(char c) {
int n = (int)c;
return c > 47 && n < 58;
}
/* /*
* Returns the value of a roman numeral digit * Returns the value of a roman numeral digit
* @param {char} c - Roman digit * @param {char} c - Roman digit
*/ */
static int roman_to_integer(char c) static int roman_to_integer(char c) {
{ switch (c) {
switch (c)
{
case 'I': case 'I':
return 1; return 1;
case 'V': case 'V':
@ -31,21 +34,16 @@ static int roman_to_integer(char c)
/* /*
* Returns the interger value of a roman number * Returns the interger value of a roman number
* @param {string} s - Roman number * @param {string} s - Roman number
*/ */
int roman_to_int(char *s) int roman_to_int(char *s) {
{
int i, int_num = roman_to_integer(s[0]); int i, int_num = roman_to_integer(s[0]);
for (i = 1; s[i] != '\0'; i++) for (i = 1; s[i] != '\0'; i++) {
{
int prev_num = roman_to_integer(s[i - 1]); int prev_num = roman_to_integer(s[i - 1]);
int cur_num = roman_to_integer(s[i]); int cur_num = roman_to_integer(s[i]);
if (prev_num < cur_num) if (prev_num < cur_num) {
{
int_num = int_num - prev_num + (cur_num - prev_num); int_num = int_num - prev_num + (cur_num - prev_num);
} } else {
else
{
int_num += cur_num; int_num += cur_num;
} }
} }
@ -55,9 +53,8 @@ int roman_to_int(char *s)
* Map of roman digits symbols and their value * Map of roman digits symbols and their value
* @elem {string} sym - digit symbol * @elem {string} sym - digit symbol
* @elem {int} val - digit symbol * @elem {int} val - digit symbol
*/ */
typedef struct typedef struct {
{
char *sym; char *sym;
int val; int val;
} numeral; } numeral;
@ -65,73 +62,75 @@ typedef struct
* Returns greatest numeral index * Returns greatest numeral index
* @elem {numeral} nu - map of roman digits symbols and their value * @elem {numeral} nu - map of roman digits symbols and their value
* @elem {int} num - number * @elem {int} num - number
*/ */
int maxNume(numeral *nu, int num) int maxNume(numeral *nu, int num) {
{
int i, index; int i, index;
for (i = 0; i < RDIGITS; i++) for (i = 0; i < RDIGITS; i++) { // RDIGITS numerals in array
{ //RDIGITS numerals in array
if (nu[i].val <= num) if (nu[i].val <= num)
index = i; index = i;
} }
//gretest value numeral index, not greater than number // gretest value numeral index, not greater than number
return index; return index;
} }
/* /*
* Prints roman number from integer * Prints roman number from integer
* @elem {numeral} nu - map of roman digits symbols and their value * @elem {numeral} nu - map of roman digits symbols and their value
* @elem {int} num - number * @elem {int} num - number
*/ */
void decToRoman(numeral *nu, int num) void decToRoman(numeral *nu, int num) {
{
int max; int max;
if (num != 0) if (num != 0) {
{
max = maxNume(nu, num); max = maxNume(nu, num);
printf("%s", nu[max].sym); printf("%s", nu[max].sym);
num -= nu[max].val; //decrease number num -= nu[max].val; // decrease number
decToRoman(nu, num); //recursively print numerals decToRoman(nu, num); // recursively print numerals
} }
} }
/* /*
* Main * Main
* @elem {int} argc - Argument count * @elem {int} argc - Argument count
* @elem {string[]} argv - Strings array * @elem {string[]} argv - Strings array
*/ */
int main(int argc, char *argv[]) int main(int argc, char *argv[]) {
{
int direction = 0; int direction = 0;
/* /*
* 0: integer to roman (default) * 0: integer to roman (default)
* 1: roman to integer * 1: roman to integer
*/ */
for (int i = 0; i < argc; i++) for (int i = 0; i < argc; i++) {
{ if (strcmp(argv[i], "-r")) {
if (strcmp(argv[i], "-r"))
{
direction = 1; direction = 1;
} } else {
else
{
direction = 0; direction = 0;
} }
} }
numeral nume[RDIGITS] = {{"I", 1}, {"IV", 4}, {"V", 5}, {"IX", 9}, {"X", 10}, {"XL", 40}, {"L", 50}, {"XC", 90}, {"C", 100}, {"CD", 400}, {"D", 500}, {"CM", 900}, {"M", 1000}, {"MMMM", 4000}}; numeral nume[RDIGITS] = {{"I", 1}, {"IV", 4}, {"V", 5}, {"IX", 9},
char *str1; {"X", 10}, {"XL", 40}, {"L", 50}, {"XC", 90},
{"C", 100}, {"CD", 400}, {"D", 500}, {"CM", 900},
{"M", 1000}, {"MMMM", 4000}};
char str[51];
int inputint = 0; int inputint = 0;
switch (direction) switch (direction) {
{
case 0: case 0:
while (scanf("%s", &str1) && !feof(stdin)) // read integers and convert to roman while (scanf("%50s", str) &&
!feof(stdin)) // read integers and convert to roman
{ {
printf("%d\n", roman_to_int(&str1)); printf("%d\n", roman_to_int(str));
scanf("%*[^\n]"); // discard all until newline
} }
break; break;
case 1: case 1:
while (scanf("%d", &inputint) && !feof(stdin)) // read roman and convert to integers while (scanf("%9s", str) &&
!feof(stdin)) // read roman and convert to integers
{ {
for (int i = 0; i < strlen(str); i++) {
if (!validateN(str[i]))
break;
}
sscanf(str, "%d", &inputint);
decToRoman(nume, inputint); decToRoman(nume, inputint);
printf("\n"); printf("\n");
scanf("%*[^\n]"); // discard all until newline
} }
break; break;
} }