Release 2017.1
[cascardo/rnetclient.git] / decfile.c
1 /*
2  *  Copyright (C) 2012-2014  Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
3  *  Copyright (C) 2014  Alexandre Oliva <lxoliva@fsfla.org>
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 3 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License along
16  *  with this program; if not, write to the Free Software Foundation, Inc.,
17  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19
20 #define _GNU_SOURCE
21 #include "decfile.h"
22 #include <string.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <errno.h>
26 #include <unistd.h>
27 #include <gcrypt.h>
28 #include "pmhash.h"
29 #include "rnet_message.h"
30
31 #ifndef MAX
32 #define MAX(a,b) (a >= b) ? a : b
33 #endif
34
35 struct rnet_decfile {
36         char *filename;
37         FILE *file;
38         char **lines;
39         int lines_len;
40         struct pmhash *header;
41         struct rnet_message *message;
42 };
43
44 /*
45  * line should be an allocated buffer given to append_line
46  * this means, free(line) will be called when decfile is released
47  */
48 static int append_line(struct rnet_decfile *decfile, char *line)
49 {
50         size_t len;
51         char **old_lines;
52         decfile->lines_len += 1;
53         len = sizeof(*decfile->lines) * decfile->lines_len;
54         old_lines = decfile->lines;
55         decfile->lines = realloc(decfile->lines, len);
56         if (!decfile->lines) {
57                 decfile->lines = old_lines;
58                 goto out;
59         }
60         decfile->lines[decfile->lines_len - 1] = line;
61         return 0;
62 out:
63         decfile->lines_len -= 1;
64         return -ENOMEM;
65 }
66
67 static void decfile_release_lines(struct rnet_decfile *decfile)
68 {
69         int i;
70         for (i = 0; i < decfile->lines_len; i++)
71                 free(decfile->lines[i]);
72         free(decfile->lines);
73         decfile->lines = NULL;
74 }
75
76 static char * get_header(struct rnet_decfile *decfile);
77 static int decfile_parse_file(struct rnet_decfile *decfile);
78
79 #define parse(field, sz) \
80         r = -ENOMEM; \
81         val = malloc(sz + 1); \
82         if (!val) \
83                 goto out_val; \
84         val[sz] = 0; \
85         memcpy(val, p, sz); \
86         p += sz; \
87         key = strdup(field); \
88         if (!key) \
89                 goto out_key; \
90         if (pmhash_add(&hash, key, val)) \
91                 goto out_add;
92
93 static int decfile_parse_header(struct rnet_decfile *decfile)
94 {
95         char *buffer;
96         int r;
97         int exerc;
98         char *p;
99         char *tail;
100         char *key;
101         char *val;
102         struct pmhash *hash;
103
104         p = buffer = get_header(decfile);
105         if (!buffer)
106                 return -EINVAL;
107
108         hash = decfile->header;
109
110         /* Common header fields. Most of these are used by rnet_encode. */
111         parse("sistema", 8);
112         parse("exerc", 4);
113         parse("ano", 4);
114         parse("codigo_recnet", 4);
115         parse("in_ret", 1);
116         parse("cpf", 11);
117         parse("filler", 3);
118         parse("tipo_ni", 1);
119         parse("nr_versao", 3);
120         parse("nome", 60);
121         parse("uf", 2);
122         parse("hash", 10);
123
124         /* Assert the size of the common header matches the expectation. */
125         if (p - buffer != RNET_HEADER_HEAD_COMMON) {
126                 fprintf(stderr, "RNET_HEADER_HEAD_COMMON in decfile.h needs to be adjusted to %ti\n", p - buffer);
127                 goto out_val;
128         }
129
130         /* Retrieve exerc from the hash table. */
131         exerc = atoi(pmhash_get(hash, "exerc"));
132
133         /* Assert exerc is no less than the minimum supported version */
134         if (exerc < 2013) {
135                 fprintf(stderr, "This software does not support declarations older than 2013.\n");
136                 goto out_val;
137         }
138
139         /* Check for tested versions. */
140         if (exerc > 2016) {
141                 fprintf(stderr, "Unknown file version, but proceeding anyway.\n");
142                 return 0;
143         }
144
145         /* These fields exist at least since 2013. */
146         parse("in_cert", 1);
147         parse("dt_nasc", 8);
148         parse("in_comp", 1);
149         parse("in_res", 1);
150         parse("in_gerada", 1);
151         parse("nr_recibo_anterior", 10);
152         parse("in_pgd", 1);
153         parse("so", 14);
154         parse("versao_so", 7);
155         parse("jvm", 9);
156         parse("nr_recibo", 10);
157         parse("municipio", 4);
158         parse("conjuge", 11);
159         parse("obrig", 1);
160         parse("impdevido", 13);
161         parse("nr_recibo", 10);
162         parse("in_seg", 1);
163         parse("imppago", 2);
164         parse("impant", 1);
165         parse("mudend", 1);
166         parse("cep", 8);
167         parse("debito", 1);
168         parse("banco", 3);
169         parse("agencia", 4);
170         parse("filler", 1);
171         parse("data_julgado", 8);
172         parse("imppagar", 13);
173         parse("tribfonte", 1);
174         parse("cpfrra", 11);
175         parse("trib_rra", 1);
176         parse("cpf_rra2", 11);
177         parse("trib_3rra", 1);
178         parse("cpf_rra3", 11);
179
180         /* Fields added in 2014. */
181         if (exerc >= 2014) {
182                 parse("trib_4rra", 1);
183                 parse("cpf_rra4", 11);
184         }
185
186         /* These fields exist at least since 2013. */
187         parse("vr_doacao", 13);
188         parse("cnpj1", 14);
189         parse("cnpj2", 14);
190         parse("cnpj3", 14);
191         parse("cnpj4", 14);
192         parse("cpf_dep1", 11);
193         parse("dnas_dep1", 8);
194         parse("cpf_dep2", 11);
195         parse("dnas_dep2", 8);
196         parse("cpf_dep3", 11);
197         parse("dnas_dep3", 8);
198         parse("cpf_dep4", 11);
199         parse("dnas_dep4", 8);
200         parse("cpf_dep5", 11);
201         parse("dnas_dep5", 8);
202         parse("cpf_dep6", 11);
203         parse("dnas_dep6", 8);
204         parse("cnpj_med1", 14);
205         parse("cnpj_med2", 14);
206         parse("cpf_alim", 11);
207         parse("cpf_invent", 11);
208         parse("municipio", 40);
209         parse("contribuinte", 60);
210
211         /* The contents of this field until 2014 (cpf_empregada) were moved to
212          * the end of the header in 2015 (cpfdomestic@). This field has then
213          * been converted into a filler field. */
214         if (exerc <= 2014) {
215                 parse("cpf_empregada", 11);
216         } else {
217                 parse("filler", 11);
218         }
219
220         /* These fields exist at least since 2013. */
221         parse("mac", 12);
222         parse("data_nao_residente", 8);
223         parse("cpf_procurador", 11);
224         parse("obrigatoriedade", 3);
225         parse("rendtrib", 13);
226         parse("cnpj_prev", 14);
227         parse("cnpj_prev2", 14);
228         parse("vr_totisentos", 13);
229         parse("vr_totexclusivo", 13);
230         parse("vr_totpagamentos", 13);
231
232         /* End of header in 2013. */
233
234         /* Fields added in 2014. */
235         if (exerc >= 2014) {
236                 parse("nr_conta", 13);
237                 parse("nr_dv_conta", 2);
238                 parse("in_dv_conta", 1);
239         }
240
241         /* End of header in 2014. */
242
243         /* Fields added in 2015. */
244         if (exerc >= 2015) {
245                 parse("codnaturezaocup", 2);
246                 parse("cpfdomestic@", 11);
247                 parse("nitdomestic@", 11);
248                 parse("cpfdomestic@2", 11);
249                 parse("nitdomestic@2", 11);
250                 parse("cpfdomestic@3", 11);
251                 parse("nitdomestic@3", 11);
252                 parse("deciniciada", 1);
253                 parse("utilpgd", 1);
254                 parse("utilapp", 1);
255                 parse("utilonline", 1);
256                 parse("utilrascunho", 1);
257                 parse("utilprepreenchida", 1);
258                 parse("utilfontes", 1);
259                 parse("utilplanosaude", 1);
260                 parse("utilrecuperar", 1);
261                 parse("dectransmitida", 1);
262         }
263
264         /* End of header in 2015. */
265
266         /* Fields added in 2016. */
267         if (exerc >= 2016) {
268                 parse("dedutivelmaior1", 14);
269                 parse("dedutivelmaior2", 14);
270                 parse("dedutivelmaior3", 14);
271                 parse("dedutivelmaior4", 14);
272                 parse("dedutivelmaior5", 14);
273                 parse("dedutivelmaior6", 14);
274                 parse("funprespmaior", 14);
275         }
276
277         /* End of header in 2016. */
278
279         /* Tail fields, which exist at least since 2013. */
280         tail = p;
281         parse("versaotestpgd", 3);
282         parse("controle", 10);
283
284         if (*p++ != '\r') {
285                 fprintf(stderr,
286                         "missing CR at the %tith header character\n",
287                         p - buffer);
288                 goto out_val;
289         } else if (*p++ != '\n') {
290                 fprintf(stderr,
291                         "missing LF at the %tith header character\n",
292                         p - buffer);
293                 goto out_val;
294         } else if (*p != 0) {
295                 fprintf(stderr,
296                         "missing NUL at the %tith header character\n",
297                         p - buffer);
298                 goto out_val;
299         } else if (p - tail != RNET_HEADER_TAIL_COMMON) {
300                 fprintf(stderr, "RNET_HEADER_TAIL_COMMON in decfile.h needs to be adjusted to %ti\n", p - tail);
301                 goto out_val;
302         }
303
304         /* Verify header size */
305         switch (exerc) {
306                 case 2013:
307                         if (p - buffer != RNET_HEADER_SIZE_2013) {
308                                 fprintf(stderr, "RNET_HEADER_SIZE_2013 in decfile.h needs to be adjusted to %ti,\nor parse_header in decfile.c needs updating\n", p - buffer);
309                                 goto out_val;
310                         }
311                         break;
312                 case 2014:
313                         if (p - buffer != RNET_HEADER_SIZE_2014) {
314                                 fprintf(stderr, "RNET_HEADER_SIZE_2014 in decfile.h needs to be adjusted to %ti,\nor parse_header in decfile.c needs updating\n", p - buffer);
315                                 goto out_val;
316                         }
317                         break;
318                 case 2015:
319                         if (p - buffer != RNET_HEADER_SIZE_2015) {
320                                 fprintf(stderr, "RNET_HEADER_SIZE_2015 in decfile.h needs to be adjusted to %ti,\nor parse_header in decfile.c needs updating\n", p - buffer);
321                                 goto out_val;
322                         }
323                         break;
324                 case 2016:
325                         if (p - buffer != RNET_HEADER_SIZE_2016) {
326                                 fprintf(stderr, "RNET_HEADER_SIZE_2016 in decfile.h needs to be adjusted to %ti,\nor parse_header in decfile.c needs updating\n", p - buffer);
327                                 goto out_val;
328                         }
329                         break;
330                 default:
331                         /* This case should never be reached even for later
332                            years, because later years should not be parsed
333                            further than the common header. */
334                         fprintf(stderr, "Error while processing header. Unrecognized version\n");
335                         goto out_val;
336                         break;
337         }
338
339         return 0;
340 out_add:
341         free(key);
342 out_key:
343         free(val);
344 out_val:
345         return r;
346 }
347
348 #undef parse
349
350 static int decfile_parse(struct rnet_decfile *decfile)
351 {
352         char *buffer = NULL;
353         size_t len = 0;
354         int r;
355         while ((r = getline(&buffer, &len, decfile->file)) > 0) {
356                 r = append_line(decfile, buffer);
357                 if (r) {
358                         free(buffer);
359                         goto out;
360                 }
361                 buffer = NULL;
362                 len = 0;
363         }
364         if (!(r = decfile_parse_header(decfile)) && !(r = decfile_parse_file(decfile)))
365                 return 0;
366 out:
367         decfile_release_lines(decfile);
368         return r;
369 }
370
371 struct rnet_decfile * rnet_decfile_open(char *filename)
372 {
373         struct rnet_decfile *decfile;
374         int r = -ENOMEM;
375         decfile = malloc(sizeof(*decfile));
376         if (!decfile)
377                 return NULL;
378         decfile->header = pmhash_new();
379         if (!decfile->header)
380                 goto out_header;
381         decfile->message = rnet_message_new();
382         if (!decfile->message)
383                 goto out_message;
384         decfile->filename = strdup(filename);
385         if (!decfile->filename)
386                 goto out_filename;
387         decfile->file = fopen(filename, "r");
388         if (!decfile->file)
389                 goto out_file;
390         decfile->lines_len = 0;
391         decfile->lines = NULL;
392         if ((r = decfile_parse(decfile)))
393                 goto out_parse;
394         return decfile;
395 out_parse:
396         fclose(decfile->file);
397 out_file:
398         free(decfile->filename);
399 out_filename:
400         rnet_message_del(decfile->message);
401 out_message:
402         pmhash_del(decfile->header);
403 out_header:
404         free(decfile);
405         errno = -r;
406         return NULL;
407 }
408
409 void rnet_decfile_close(struct rnet_decfile *decfile)
410 {
411         decfile_release_lines(decfile);
412         fclose(decfile->file);
413         free(decfile->filename);
414         free(decfile);
415 }
416
417 static char * get_header(struct rnet_decfile *decfile)
418 {
419         int i;
420         for (i = 0; i < decfile->lines_len; i++) {
421                 if (!strncmp(decfile->lines[i], "IRPF", 4)) {
422                         return decfile->lines[i];
423                 }
424         }
425         return NULL;
426 }
427
428 char *rnet_decfile_get_header_field(struct rnet_decfile *decfile, char *field)
429 {
430         return pmhash_get(decfile->header, field);
431 }
432
433 /* returns true if register is declaration and not a receipt */
434 static int decfile_reg_is_dec(char *line)
435 {
436         if (line[0] >= '0' && line[0] <= '9' &&
437             line[1] >= '0' && line[1] <= '9')
438                 return 1;
439         if (!strncmp(line, "IRPF    ", 8))
440                 return 1;
441         if (!strncmp(line, "T9", 2))
442                 return 1;
443         return 0;
444 }
445
446 /* strip a register from its control number and append it to message */
447 static int append_stripped_reg_ctrl(struct rnet_message **message, char *line)
448 {
449         size_t len;
450         struct rnet_message *msg = *message;
451         int growth;
452         if (!decfile_reg_is_dec(line))
453                 return 0;
454         len = strlen(line);
455         if (len < 12)
456                 return -EINVAL;
457         growth = msg->len + len - 10 - msg->alen;
458         if (growth > 0) {
459                 if (rnet_message_expand(message, growth))
460                         return -ENOMEM;
461                 msg = *message;
462         }
463         memcpy(&msg->buffer[msg->len], line, len - 12);
464         msg->buffer[msg->len + len - 12] = '\r';
465         msg->buffer[msg->len + len - 11] = '\n';
466         msg->len += len - 10;
467         return 0;
468 }
469
470 static int decfile_parse_file(struct rnet_decfile *decfile)
471 {
472         int i;
473         int r;
474         for (i = 0; i < decfile->lines_len; i++) {
475                 r = append_stripped_reg_ctrl(&decfile->message,
476                                                 decfile->lines[i]);
477                 if (r)
478                         return r;
479         }
480         return 0;
481 }
482
483 struct rnet_message * rnet_decfile_get_file(struct rnet_decfile *decfile)
484 {
485         return decfile->message;
486 }
487
488 char * rnet_decfile_get_file_hash(struct rnet_decfile *decfile)
489 {
490         char *hash;
491         size_t len;
492         if (gcry_md_test_algo(GCRY_MD_MD5))
493                 return NULL;
494         len = gcry_md_get_algo_dlen(GCRY_MD_MD5);
495         hash = malloc(len);
496         if (!hash)
497                 return NULL;
498         gcry_md_hash_buffer(GCRY_MD_MD5, hash, decfile->message->buffer,
499                                         decfile->message->len);
500         return hash;
501 }
502
503 char * rnet_decfile_get_header(struct rnet_decfile *decfile)
504 {
505         return get_header(decfile);
506 }