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

Dev #3

Merged
merged 12 commits into from
Apr 29, 2018
Prev Previous commit
Next Next commit
small changes
  • Loading branch information
klmanion committed Apr 28, 2018
commit 4a040d68fd8cff0eb6ff43510b2c9dc6df15ed3a
29 changes: 15 additions & 14 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,11 @@ stobin(
{
uint8_t *a;
size_t a_len;
int mask[] = { 0x1, 0x2, 0x4, 0x8,
0x10, 0x20, 0x40, 0x80 };
static const int mask[] = { 0x1, 0x2, 0x4, 0x8,
0x10, 0x20, 0x40, 0x80 };
a_len = stobin_size(s);
a = (uint8_t *)malloc(a_len * sizeof(uint8_t));
memset(a, 0, (a_len));
memset(a, 0, a_len);
for(size_t i=0,j=0,len=strlen(s); i<len; ++i) {
//for each character to be encoded
for(int k=7; k>=0; --k,++j) {
Expand Down Expand Up @@ -238,18 +238,17 @@ encode_line(
//the 45->60 chars and the length newline and null
char *enc = (char *)malloc(63*sizeof(char));

//first encode the number of characters
(void)sprintf(enc, "%c", (int)(strlen(line)+32));
//encode the number of characters
sprintf(enc, "%c", (int)(strlen(line)+32));

//convert the string of chars into an array of bits
bin = stobin(line);//must be freed
for(size_t i=0,len=stobin_size(line); i<len; i+=6)//check the <=
for(size_t i=0,len=stobin_size(line); i<len; i+=6)
{
(void)sprintf(enc, "%s%c", enc, bitstoc(&bin[i]) + 32);
sprintf(enc, "%s%c", enc, bitstoc(&bin[i]) + 32);
}
(void)sprintf(enc, "%s\n", enc);
sprintf(enc, "%s\n", enc);
free(bin);
free(line);//experimental
return (void *)enc;
}

Expand Down Expand Up @@ -330,7 +329,7 @@ writeenc(
void *enc = NULL;
pthread_join(el->thread, &enc);
if (enc)
(void)fprintf(fd, "%s", (char *)enc);
fprintf(fd, "%s", (char *)enc);
free(enc);
if (el->cdr)
writeenc(el->cdr, fd);
Expand Down Expand Up @@ -364,10 +363,12 @@ encode(
FILE * f_write;
char *line = NULL;

if (ifile == NULL)
if (ifile == NULL) {
ifile = "stdin";
f_read = stdin;
else
} else {
f_read = fopen(ifile, "r");
}

if (ofile == NULL)
f_write = stdout;
Expand All @@ -379,15 +380,15 @@ encode(
if (!f_write)
errx(EBADOUT, "%s\n", "could not open output_file");

header(f_write, DEFAULT_PERM, ifile);
header(f_write, DEFAULT_PERM, ifile);

enclist_t *el = (enclist_t *)malloc(enclist_sz);
el->cdr = NULL;
while (readline(&line, f_read) != EOF)
{
pthread_create(newthread(el), NULL, &encode_line, (void *)line);

line = NULL;
free(line);
}
writeenc(el, f_write);
el = freeenc(el);
Expand Down