From 715e798d9d7fa8da101e8ea815467906b31f4729 Mon Sep 17 00:00:00 2001 From: Thadeu Lima de Souza Cascardo Date: Sat, 8 Aug 2015 21:27:17 -0300 Subject: [PATCH] =?utf8?q?Calcula=20hashes=20do=20arquivo=20da=20declara?= =?utf8?q?=C3=A7=C3=A3o.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Os dois hashes necessários para a declaração são calculados. O hash de recibo está na última linha, o que facilita sua inclusão. O hash do cabeçalho ainda não é impresso. --- lib/declaracao.h | 2 ++ lib/gera.c | 20 +++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/declaracao.h b/lib/declaracao.h index ddaa40f..e94fd41 100644 --- a/lib/declaracao.h +++ b/lib/declaracao.h @@ -53,6 +53,8 @@ struct declaracao { char *dvconta; int linhas[100]; /* Número de linhas escritas de cada tipo. */ struct pmhash *totais; + unsigned long hash; + unsigned long rhash; }; struct declaracao * declaracao_new(int ano); diff --git a/lib/gera.c b/lib/gera.c index 5bcce5d..828c9ed 100644 --- a/lib/gera.c +++ b/lib/gera.c @@ -441,7 +441,7 @@ static void gera_recibotrailler(struct declaracao *dec, FILE *f) fprintf(f, "R9"); fprintf(f, "%s", dec->cpf); /* CPF: 11 dígitos */ fprintf(f, "%-3.3s", ""); /* FILLER */ - fprintf(f, "%-10.10s", "0000000000"); /* FIXME: hash */ + fprintf(f, "%010u", dec->rhash); } static void gera_rendimento(struct declaracao *dec, FILE *f) @@ -544,6 +544,20 @@ static void gera_bem(struct declaracao *dec, FILE *f) typedef void (gera_linha)(struct declaracao *dec, FILE *f); +static void update_hash(struct declaracao *dec, char *buf, size_t len) +{ + int r; + int linha; + r = sscanf(buf, "%2d", &linha); + if (r == 1 || !strncmp(buf, "T9", 2)) { + dec->hash = crc32(dec->hash, buf, len - 2); + dec->rhash = crc32(dec->rhash, buf, len - 2); + return; + } else if (strncmp(buf, "R9", 2) && strncmp(buf, "IRPF", 4)) { + dec->rhash = crc32(dec->rhash, buf, len - 2); + } +} + static int wrap(gera_linha fn, struct declaracao *dec, FILE *f) { FILE *m; @@ -569,6 +583,7 @@ static int wrap(gera_linha fn, struct declaracao *dec, FILE *f) if (r == 1 && linha >= 0 && linha < 100) { dec->linhas[linha]++; } + update_hash(dec, buf, bsize); fwrite(buf, bsize, 1, f); free(buf); return 0; @@ -584,6 +599,9 @@ static int gera(struct declaracao *dec, char *filename) struct pagamento *pagamento; struct bem *bem; + dec->hash = crc32(0L, NULL, 0); + dec->rhash = crc32(0L, NULL, 0); + #define W(fn, dec, f) \ do { \ r = wrap(fn, dec, f); \ -- 2.20.1