Created
December 3, 2016 00:37
-
-
Save valexey/844e72436d8b3e26f30d6f1ebd78a405 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stddef.h> | |
#include <string.h> | |
#include <assert.h> | |
#include <math.h> | |
#include <stdbool.h> | |
#include <o7c.h> | |
#include "CFiles.h" | |
#include "CLI.h" | |
#include "Out.h" | |
#define SectorBufSize_cnst 1024 | |
#define SectorsCount_cnst 65536 | |
#define ModCount_cnst ((65536 * (65536 / 4)) / (SectorsCount_cnst / 4)) | |
typedef struct Sector { | |
o7c_char name[12]; | |
int i; | |
o7c_char v[SectorBufSize_cnst]; | |
} Sector; | |
static o7c_tag_t Sector_tag; | |
static struct Sector sectors[SectorsCount_cnst]; | |
static int counts[ModCount_cnst]; | |
static struct FileSort_anon_0000 { | |
o7c_char buf[4096]; | |
int i; | |
} out; | |
static CFiles_File fin; | |
static CFiles_File fout; | |
static void AddToSector(struct Sector *s, o7c_tag_t s_tag, o7c_char v[/*len0*/], int v_len0, int ofs) { | |
CFiles_File f; | |
int len; | |
if ((*s).i == sizeof((*s).v) / sizeof ((*s).v[0])) { | |
if ((*s).name[0] == 0x00u) { | |
(*s).name[0] = (char unsigned)(o7c_add((int)(char unsigned)'A', o7c_div((int)v[o7c_ind(v_len0, o7c_add(ofs, 3))], 16))); | |
(*s).name[1] = (char unsigned)(o7c_add((int)(char unsigned)'A', o7c_mod((int)v[o7c_ind(v_len0, o7c_add(ofs, 3))], 16))); | |
(*s).name[2] = (char unsigned)(o7c_add((int)(char unsigned)'A', o7c_div((int)v[o7c_ind(v_len0, o7c_add(ofs, 2))], 16))); | |
(*s).name[3] = (char unsigned)(o7c_add((int)(char unsigned)'A', o7c_mod((int)v[o7c_ind(v_len0, o7c_add(ofs, 2))], 16))); | |
(*s).name[4] = (char unsigned)'.'; | |
(*s).name[5] = (char unsigned)'T'; | |
(*s).name[6] = (char unsigned)'M'; | |
(*s).name[7] = (char unsigned)'P'; | |
(*s).name[8] = 0x00u; | |
f = CFiles_Open((*s).name, 12, 0, "wb", 3); | |
} else { | |
f = CFiles_Open((*s).name, 12, 0, "ab", 3); | |
} | |
assert(f != NULL); | |
len = CFiles_Write(f, (*s).v, SectorBufSize_cnst, 0, sizeof((*s).v) / sizeof ((*s).v[0])); | |
assert(len == sizeof((*s).v) / sizeof ((*s).v[0])); | |
CFiles_Close(&f); | |
(*s).i = 0; | |
} | |
(*s).v[o7c_ind(SectorBufSize_cnst, (*s).i)] = v[o7c_ind(v_len0, ofs)]; | |
(*s).i = o7c_add((*s).i, 1);; | |
(*s).v[o7c_ind(SectorBufSize_cnst, (*s).i)] = v[o7c_ind(v_len0, o7c_add(ofs, 1))]; | |
(*s).i = o7c_add((*s).i, 1);; | |
} | |
static void WriteFromCounts(int base) { | |
int i, len; | |
for (i = 0; i < SectorsCount_cnst; ++i) { | |
while (counts[o7c_ind(ModCount_cnst, i)] > 0) { | |
counts[o7c_ind(ModCount_cnst, i)] = o7c_sub(counts[o7c_ind(ModCount_cnst, i)], 1);; | |
out.buf[o7c_ind(4096, o7c_add(out.i, 3))] = (char unsigned)(o7c_div(base, 256)); | |
out.buf[o7c_ind(4096, o7c_add(out.i, 2))] = (char unsigned)(o7c_mod(base, 256)); | |
out.buf[o7c_ind(4096, o7c_add(out.i, 1))] = (char unsigned)(o7c_div(i, 256)); | |
out.buf[o7c_ind(4096, o7c_add(out.i, 0))] = (char unsigned)(o7c_mod(i, 256)); | |
out.i = o7c_mod((o7c_add(out.i, 4)), sizeof(out.buf) / sizeof (out.buf[0])); | |
if (out.i == 0) { | |
len = CFiles_Write(fout, out.buf, 4096, 0, sizeof(out.buf) / sizeof (out.buf[0])); | |
assert(len == sizeof(out.buf) / sizeof (out.buf[0])); | |
} | |
} | |
} | |
} | |
static void WriteSector(struct Sector *s, o7c_tag_t s_tag, int v) { | |
int i, r; | |
o7c_char buf[4096]; | |
CFiles_File f; | |
bool del; | |
if (((*s).i > 0) || ((*s).name[0] != 0x00u)) { | |
while ((*s).i > 0) { | |
(*s).i = o7c_sub((*s).i, 2);; | |
counts[o7c_ind(ModCount_cnst, o7c_add((int)(*s).v[o7c_ind(SectorBufSize_cnst, (*s).i)], o7c_mul((int)(*s).v[o7c_ind(SectorBufSize_cnst, o7c_add((*s).i, 1))], 256)))] = o7c_add(counts[o7c_ind(ModCount_cnst, o7c_add((int)(*s).v[o7c_ind(SectorBufSize_cnst, (*s).i)], o7c_mul((int)(*s).v[o7c_ind(SectorBufSize_cnst, o7c_add((*s).i, 1))], 256)))], 1);; | |
} | |
if ((*s).name[0] != 0x00u) { | |
f = CFiles_Open((*s).name, 12, 0, "rb", 3); | |
assert(f != NULL); | |
do { | |
r = CFiles_Read(f, buf, 4096, 0, sizeof(buf) / sizeof (buf[0])); | |
for (i = 0; i <= o7c_sub(r, 2); i += 2) { | |
counts[o7c_ind(ModCount_cnst, o7c_add((int)buf[o7c_ind(4096, i)], o7c_mul((int)buf[o7c_ind(4096, o7c_add(i, 1))], 256)))] = o7c_add(counts[o7c_ind(ModCount_cnst, o7c_add((int)buf[o7c_ind(4096, i)], o7c_mul((int)buf[o7c_ind(4096, o7c_add(i, 1))], 256)))], 1);; | |
} | |
} while (!(r < sizeof(buf) / sizeof (buf[0]))); | |
CFiles_Close(&f); | |
del = CFiles_Remove((*s).name, 12, 0); | |
} | |
WriteFromCounts(v); | |
} | |
} | |
static void Go(void) { | |
int r, w, i; | |
o7c_char buf[4096]; | |
o7c_char nin[256], nout[256]; | |
int ninLen, noutLen; | |
bool copy; | |
if (CLI_count < 3) { | |
fin = CFiles_Open("input", 6, 0, "rb", 3); | |
fout = CFiles_Open("output", 7, 0, "wb", 3); | |
} else { | |
ninLen = 0; | |
noutLen = 0; | |
copy = CLI_Get(nin, 256, &ninLen, 1) && CLI_Get(nout, 256, &noutLen, 2); | |
assert(copy); | |
fin = CFiles_Open(nin, 256, 0, "rb", 3); | |
fout = CFiles_Open(nout, 256, 0, "wb", 3); | |
} | |
assert((fin != NULL) && (fout != NULL)); | |
do { | |
r = CFiles_Read(fin, buf, 4096, 0, sizeof(buf) / sizeof (buf[0])); | |
for (i = 0; i <= o7c_sub(r, 4); i += 4) { | |
AddToSector(§ors[o7c_ind(SectorsCount_cnst, o7c_add(o7c_mul((int)buf[o7c_ind(4096, o7c_add(i, 3))], 256), (int)buf[o7c_ind(4096, o7c_add(i, 2))]))], Sector_tag, buf, 4096, i); | |
} | |
} while (!(r < sizeof(buf) / sizeof (buf[0]))); | |
CFiles_Close(&fin); | |
for (i = 0; i < SectorsCount_cnst; ++i) { | |
WriteSector(§ors[o7c_ind(SectorsCount_cnst, i)], Sector_tag, i); | |
} | |
if (out.i > 0) { | |
w = CFiles_Write(fout, out.buf, 4096, 0, out.i); | |
assert(w == out.i); | |
} | |
CFiles_Close(&fout); | |
} | |
extern int main(int argc, char **argv) { | |
o7c_init(argc, argv); | |
CFiles_init(); | |
CLI_init(); | |
Out_init(); | |
o7c_tag_init(Sector_tag, NULL); | |
Go(); | |
return o7c_exit_code; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment