+ int r;
+ char *buffer;
+ size_t len;
+ rnet_message_expand(message, 6);
+ buffer = (*message)->buffer;
+ r = gnutls_record_recv(session, buffer, 6);
+ if (buffer[0] == 0x01) {
+ len = chars2len(buffer+1);
+ rnet_message_expand(message, len);
+ buffer = (*message)->buffer + 6;
+ r = gnutls_record_recv(session, buffer, len);
+ inflateRecord(buffer - 6, len + 6, &out, &olen);
+ rnet_message_del(*message);
+ *message = NULL;
+ rnet_message_expand(message, olen);
+ memcpy((*message)->buffer, out, olen);
+ (*message)->len = olen;
+ free(out);
+ } else {
+ len = chars2len(buffer+1);
+ rnet_message_expand(message, len - 1);
+ buffer = (*message)->buffer + 6;
+ r = gnutls_record_recv(session, buffer, len - 1);
+ (*message)->len = len + 4;
+ rnet_message_strip(*message, 4);
+ }
+ return 0;
+}
+
+static void save_rec_file(char *cpf, char *buffer, int len, const struct rnetclient_args *args)
+{
+ int fd;
+ char cwd[PATH_MAX];
+ char *path, *fname, *tmp;
+ size_t fname_len, r;
+ /* If the user provided the output directory where she wishes
+ to save the receipt, then we use it. Otherwise, we save
+ the file in the current working directory (CWD). */
+ if (args->output_dir == NULL)
+ path = getcwd(cwd, PATH_MAX);
+ else {
+ struct stat st;
+ if (stat(args->output_dir, &st) < 0) {
+ fprintf(stderr, "Could not stat directory \"%s\": %s\n", args->output_dir, strerror(errno));
+ return;
+ }
+ if (!S_ISDIR(st.st_mode)) {
+ fprintf(stderr, "Error: \"%s\" is a not a directory.\n", args->output_dir);
+ return;
+ }
+ path = args->output_dir;
+ }
+ /* Now it's time to decide which filename to write. We use
+ the declaration's filename as a base layout, because the
+ proprietary version of the IRPF program only recognizes
+ receipts if they have the same name as the declaration
+ files (disconsidering the extensions). For example, if the
+ declaration file is named "123.DEC", the receipt should be
+ named "123.REC". Therefore, if the declaration file has
+ the ".DEC" extension, we strip it out and add the ".REC".
+ Otherwise, we use the default template, which is to save
+ the receipt with the name "$CPF.REC". */
+ tmp = strstr(args->input_file, ".DEC");
+ if (tmp != NULL && tmp[sizeof(".DEC") - 1] == '\0') {
+ const char *p;
+ /* We found the ".REC" extension. */
+ p = strdup(args->input_file);
+ /* Replacing the ".DEC" by ".REC". Fortunately, we
+ just have to change one letter. */
+ tmp = strstr(p, ".DEC");
+ tmp[1] = 'R';
+ fname_len = strlen(p) + strlen(path) + 2;
+ fname = alloca(fname_len);
+ snprintf(fname, fname_len, "%s/%s", path, p);
+ } else {
+ /* The declaration filename does not follow the
+ convention, so we will not use it as a template.
+ We just generate a filename using "$CPF.REC". */
+ fname_len = strlen(cpf) + strlen(path) + sizeof(".REC") + 2;
+ fname = alloca(fname_len);
+ snprintf(fname, fname_len, "%s/%s.REC", path, cpf);
+ }
+ /* Now, open the file and write. */
+ fd = open(fname, O_CREAT | O_WRONLY | O_EXCL, S_IRUSR | S_IWUSR);
+ if (fd < 0) {
+ fprintf(stderr, "Could not create receipt file \"%s\": %s\n", fname, strerror(errno));
+ return;
+ }
+ do {
+ r = write(fd, buffer, len);
+ } while (r != len && errno == EAGAIN);
+ if (r != len)
+ fprintf(stderr, "Could not write to receipt file: %s", strerror(errno));
+ else
+ fprintf(stderr, "Wrote the receipt file to %s.\n", fname);
+ close(fd);
+}
+
+static void handle_response_text_and_file(char *cpf, struct rnet_message *message, const struct rnetclient_args *args)
+{
+ char *value;
+ int vlen;
+ if (!rnet_message_parse(message, "texto", &value, &vlen))
+ fprintf(stderr, "%.*s\n", vlen, value);
+ if (!rnet_message_parse(message, "arquivo", &value, &vlen))
+ save_rec_file(cpf, value, vlen, args);
+}
+
+static void handle_response_already_found(char *cpf, struct rnet_message *message, const struct rnetclient_args *args)
+{
+ handle_response_text_and_file(cpf, message, args);
+}
+
+static void handle_response_error(struct rnet_message *message)
+{
+ char *value;
+ int vlen;
+ if (!rnet_message_parse(message, "texto", &value, &vlen))
+ fprintf(stderr, "%.*s\n", vlen, value);
+ fprintf(stderr, "Error transmiting DEC file.\n");
+}
+
+int main(int argc, char **argv)
+{
+ int c;
+ int r;
+ struct rnet_decfile *decfile;
+ struct rnet_message *message = NULL;
+ struct rnetclient_args rnet_args;