Esempio n. 1
0
int
symcopy(struct sym *from, struct sym *to, struct allocator *al)
{
	iter_t iter;
	void *key, *data;

	to->flags = from->flags & 0xFFFF;
	to->idl_type = dupstr(from->idl_type, al);
	to->ndr_type = dupstr(from->ndr_type, al);
	to->interface = from->interface;

	hashmap_iterate(&from->attrs, &iter);
	while ((key = hashmap_next(&from->attrs, &iter))) {
		if ((data = hashmap_get(&from->attrs, key)) == NULL ||
				hashmap_put(&to->attrs, key, data) == -1) {
			AMSG("");
			return -1;
		}
	}
	linkedlist_iterate(&from->mems, &iter);
	while ((data = linkedlist_next(&from->mems, &iter))) {
		if (linkedlist_add(&to->mems, data) == -1) {
			AMSG("");
			return -1;
		}
	}

	to->name = dupstr(from->name, al);
	to->value = dupstr(from->value, al);
	to->ptr = from->ptr;
	to->align = from->align;

	return 0;
}
Esempio n. 2
0
void freeStuff()
{
  iter_t iter;
  Expr el;
  linkedlist_iterate(&exprPool, &iter);
  while ((el = linkedlist_next(&exprPool, &iter))){
    vc_deleteExpr(el);
  }
  linkedlist_clear(&exprPool, NULL, NULL);
}
Esempio n. 3
0
File: cvcl.c Progetto: MohsinN/jpf
void freeStuff()
{
  iter_t iter; char* varname;
  hashmap_iterate(&vars, &iter);
  while ((varname = (char*) hashmap_next(&vars, &iter)))
    free(varname);
  hashmap_clear(&vars, NULL, NULL, NULL);
  Expr el;
  linkedlist_iterate(&exprPool, &iter);
  while ((el = linkedlist_next(&exprPool, &iter))){
    vc_deleteExpr(el);
  }
  linkedlist_clear(&exprPool, NULL, NULL);
}
Esempio n. 4
0
struct sym *
get_descriminant(struct sym *u)
{
	const char *switch_is = hashmap_get(&u->attrs, "switch_is");
	iter_t iter;
	struct sym *mem;

	linkedlist_iterate(&u->parent->mems, &iter);
	while ((mem = linkedlist_next(&u->parent->mems, &iter))) {
		if (ident_index(switch_is, mem->name)) {
			return mem;
		}
	}

	return NULL;
}
Esempio n. 5
0
int
print_tree(struct idl *idl, struct sym *sym, int indent)
{
	char buf[1024];
	iter_t iter;
	struct sym *mem;

	sprint_sym(buf, sym, indent, 14);
	fputs(buf, stderr);
if (*buf)
	fputc('\n', stderr);

	linkedlist_iterate(&sym->mems, &iter);
	while ((mem = linkedlist_next(&sym->mems, &iter))) {
		print_tree(idl, mem, indent + 2);
	}

	return 0;
}
Esempio n. 6
0
int
inherit_iface(struct idl *idl, struct sym *sym, char *name)
{
	iter_t iter;
	struct sym *mem;

	if (!sym->interface) {
		sym->interface = name;
	}
	if (strcmp(idl->interface, sym->interface) != 0) {
		sym->flags |= FLAGS_IMPORTED;
	}

	linkedlist_iterate(&sym->mems, &iter);
	while ((mem = linkedlist_next(&sym->mems, &iter))) {
		if (inherit_iface(idl, mem, name) == -1) {
			AMSG("");
			return -1;
		}
	}

	return 0;
}
Esempio n. 7
0
int
symresolve(struct idl *idl, struct sym *sym)
{
	struct sym *s;
	char *key;
	iter_t iter;
	int align;
	size_t size;

 	s = symlook(idl, sym->idl_type);
	if (s) {

                                                      /* merge stuff */
		sym->flags |= s->flags & 0xFFFF;

		sym->ndr_type = s->ndr_type;
		if (sym->ptr && (!IS_PRIMATIVE(sym) || hashmap_get(&sym->attrs, "string"))) {
			sym->ndr_size = 4;
		} else {
			sym->ndr_size = s->ndr_size;
		}

		sym->align = sym->ptr ? 4 : sym->ndr_size;

                                               /* inherit attributes */
		hashmap_iterate(&s->attrs, &iter);
		while ((key = hashmap_next(&s->attrs, &iter))) {
			/* Only add attributes that the symbol does not have already
			 */
			if (hashmap_get(&sym->attrs, key) == NULL) {
				char *data;
				data = hashmap_get(&s->attrs, key);
				hashmap_put(&sym->attrs, key, data);
			}
		}

		if (sym->out_type == NULL) {
			if (s->out_type) {
				sym->out_type = s->out_type;
			} else if (IS_TYPEDEFD(sym)) {
				sym->out_type = sym->idl_type = sym->name;
			} else if (strncmp(sym->idl_type, "struct ", 7) == 0 ||
						strncmp(sym->idl_type, "union ", 6) == 0) {
					char buf[255];
					if (*sym->idl_type == 's') {
						sprintf(buf, "struct_%s", sym->idl_type + 7);
					} else {
						sprintf(buf, "union_%s", sym->idl_type + 7);
					}
					sym->out_type = dupstr(buf, idl->al);
			}
			if ((strcmp(idl->type, "java") == 0 || strcmp(idl->type, "jcifs") == 0) && IS_IMPORTED(sym)) {
				char buf[255];
				sprintf(buf, "%s.%s", sym->interface, sym->out_type);
				sym->out_type = dupstr(buf, idl->al);
			}
		}
	}

	align = 0;
	size = 0;

	linkedlist_iterate(&sym->mems, &iter);
	while ((s = linkedlist_next(&sym->mems, &iter))) {
		size_t msiz, mali, m;

		symresolve(idl, s);

		if (s->align > align) {
			align = s->align;
		}

		if (s->ptr) {
			msiz = mali = 4;
		} else {
			msiz = s->ndr_size;
			mali = s->align;
		}
		m = mali - 1;
		size = (size + m) & ~m;                                 /* align the type */
		s->offset = size;

		if (IS_ENUM(sym)) {
			size = msiz;   /* size only size of member not sum of mems */
		} else if (IS_UNION(sym)) {
                /* size is largest of mems (not really used for any logic so far) */
			if (msiz > size) {
				size = msiz;
			}
		} else {
			size += msiz;                                     /* and size of type */
		}
	}

	if (align) {
		sym->align = align;
	}
	if (size) {
		sym->ndr_size = size;
	}

                                                       /* check array conformance */
	hashmap_iterate(&sym->attrs, &iter);
	while ((key = hashmap_next(&sym->attrs, &iter))) {
		if (!IS_FIXED(sym) && (strcmp(key, "size_is") == 0 || strcmp(key, "max_is") == 0)) {
			sym->flags |= FLAGS_CONFORMANT;
			break;
		}
	}

	if (IS_PRIMATIVE(sym)) {
		if (!IS_OPERATION(sym)) {
			sym->interface = NULL;
		}
		sym->flags &= ~FLAGS_IMPORTED;
	}

	return 0;
}
Esempio n. 8
0
int
symexpand(struct idl *idl, struct sym *sym)
{
	char *key;
	struct sym *mem;
	iter_t iter;

	if (IS_EXPANDED(sym)) {
		return 0;
	}
	sym->flags |= FLAGS_EXPANDED;
	sym->orig = sym;

	if (IS_INTERFACE(sym)) {
		const char *pd = hashmap_get(&sym->attrs, "pointer_default");
		idl->ptr_default = PTR_TYPE_UNIQUE;
		if (pd) {
			if (strcmp(pd, "ptr") == 0) {
				idl->ptr_default = PTR_TYPE_PTR;
			} else if (strcmp(pd, "ref") == 0) {
				idl->ptr_default = PTR_TYPE_REF;
			}
		}
	} else if (IS_ENUM(sym)) {
		char buf[16];
		int val = 0;

		linkedlist_iterate(&sym->mems, &iter);
		while ((mem = linkedlist_next(&sym->mems, &iter))) {
			mem->flags = FLAGS_CONST | FLAGS_PRIMATIVE;
			if (mem->value) {
				val = strtoul(mem->value, NULL, 0);
			}
			sprintf(buf, "%d", val++);
			mem->value = dupstr(buf, idl->al);
		}
	} else if (sym->ptr) {
		if (hashmap_get(&sym->attrs, "unique")) {
			sym->ptr_type = PTR_TYPE_UNIQUE;
		} else if (hashmap_get(&sym->attrs, "ptr")) {
			sym->ptr_type = PTR_TYPE_PTR;
		} else if (hashmap_get(&sym->attrs, "ref")) {
			sym->ptr_type = PTR_TYPE_REF;
		} else if (IS_PARAMETER(sym) || IS_OPERATION(sym)) {
			sym->ptr_type = PTR_TYPE_REF;
		} else {
			sym->ptr_type = idl->ptr_default;
		}
	}

	/* If the symbol is typedef'd add it to the table using the
	 * typedef'd name too
	 */
	if (IS_TYPEDEFD(sym) && sym->name) {
		if (IS_ENUM(sym) && (mem = hashmap_get(idl->syms, sym->idl_type))) {
			mem->noemit = 1; /* supress redundant enum */
		}
		key = sym->name;
	} else {
		key = sym->idl_type;
	}
	if (hashmap_get(idl->syms, key) == NULL) {
		if (hashmap_put(idl->syms, key, sym) == -1) {
			AMSG("");
		}
	}

	/* If the symbol has members it is already expanded
	 */
	if (linkedlist_size(&sym->mems) == 0) {
		struct sym *s = symlook(idl, sym->idl_type);
		if (s) {
			sym->interface = s->interface;
			linkedlist_iterate(&s->mems, &iter);
			while ((mem = linkedlist_next(&s->mems, &iter))) {
				struct sym *cpy = symnew(idl->al);
				symcopy(mem, cpy, idl->al);
				linkedlist_add(&sym->mems, cpy);
			}
		} else {
			AMSG("");
			return -1;
		}
	}

	sym->id = idl->symid++;

	/* Perform expansion recursively on all symbols
	 */
	linkedlist_iterate(&sym->mems, &iter);
	while ((mem = linkedlist_next(&sym->mems, &iter))) {
		symexpand(idl, mem);
		mem->parent = sym;
	}

	return 0;
}
Esempio n. 9
0
int main(int argc, char **argv) {
  int       exit_code=0;

  CERBFIG*  cer_config=NULL;

  // linked list to store the messages in
  struct linkedlist* messages=NULL;

  CXMLROOT* xml_root=NULL;
  CFILE*    file=NULL;
  CFSYS*    cfsys=NULL;

  // log struct
  CLOG_INFO* log=NULL;

#ifdef MEMWATCH
  EF_ALIGNMENT=1;
  EF_PROTECT_BELOW=1;
  EF_PROTECT_FREE=1;
#endif

// memory checking
// mtrace();

// ##########################################################################
// ############=- CHECK CMD LINE PARAMETERS -=###############################
// ##########################################################################

  // start the logging so we can log!
  if( !(argc==4) ) {
    fprintf(stderr, "\nUsage:\n%.80s xml_config_file log_level log.txt \n\n", argv[0]);
  }

// ##########################################################################
// ############=- Run Test Suite -=##########################################
// ##########################################################################
#ifndef NOTEST
  if(1==argc) {
    CuString *output = CuStringNew();
    CuSuite* suite = CuSuiteNew();

    printf("Running Test Suite\n");

    CuSuiteAddSuite(suite, CuGetSuite());
    CuSuiteAddSuite(suite, CuStringGetSuite());
    CuSuiteAddSuite(suite, TestSuite__cstring());
    CuSuiteAddSuite(suite, TestSuite__cfile());
    CuSuiteAddSuite(suite, TestSuite__cxml());
//    CuSuiteAddSuite(suite, TestSuite__cmime());

    CuSuiteRun(suite);
    CuSuiteSummary(suite, output);
    CuSuiteDetails(suite, output);
    printf("%s\n", output->buffer);

    CuStringFree(output);
    CuSuiteFree(suite);

    return EX_USAGE;
  }
#endif

  log = clog_open(argv[3], clog_getlevel(argv[2]), NULL, 0);

  // if we couldn't log to the file, let's log to stderr!
  if(NULL!=log && NULL==log->s_logfile) {
    clog_setcallback(log, clog_stderr);
    clog_setcallbacklevel(log, clog_getlevel(argv[2]));
    clog(log, CERROR, "Could not log to file, logging to stderr!");
  }

  clog(log, CMARK, "Cerberus v. 2.x build %s Starting", BUILDNUMBER);

  clog(log, CDEBUG, "CMDLINE: Command line: %.80s %.80s %.80s", argv[0], argv[1], argv[2]);

  clog(log, CDEBUG, "CMDLINE: Command line arguments check passed");

  clog(log, CMARK, "Cerberus Starting...");

// ##########################################################################
// ############=- LOAD XML CONFIG FILE -=####################################
// ##########################################################################

  // parse the XML now
  clog(log, CDEBUG, "XML: Starting XML config file parsing");

  clog(log, CDEBUG, "XML: Creating XML DOM Variable");
  xml_root = cxml_root_new(log);
  clog(log, CDEBUG, "XML: XML DOM Variable Created");

  // make a new cer filesystem obj
  cfsys = cfile_init(0);
  
  cer_config = malloc(sizeof(CERBFIG));
  memset(cer_config, 0, sizeof(CERBFIG));

  cer_config->cfsys = cfsys;
  
  exit_code = cer_load_config(log, &xml_root, argv[1], &cer_config);

  // ##########################################################################
  // ################=- READ IN FILES -=#######################################
  // ##########################################################################

  if(0==exit_code) {
    CPOP3* pop3 = NULL;
    char *filename = NULL;

    messages = linkedlist_new(0);
    // if there is something in the list, process via pop3
    if(NULL!=cer_config->poplist) {

      exit_code = cer_curl_init(log, &cer_config, cer_config->curl_location);

      clog(log, CMARK, "Parser is in POP3 mode.");


      while(NULL!=(pop3=linkedlist_remove_last(cer_config->poplist))) {
        if(0==exit_code) {
          if(NULL!=pop3->user && NULL!=pop3->pass) {
            if(0==cpop3_connect(log, pop3)){
              if(0==cpop3_user(log, pop3, pop3->user)) {
                if(0==cpop3_pass(log, pop3, pop3->pass)) {
                  int x=0;
                  int y=0;
                  x = cpop3_stat(log, pop3);
                  while(y<x && y<cer_config->pop3_max) {
                    if(NULL!=(filename = cpop3_retr(log, pop3, cer_config->tmp_cerbmail->string))) {
                      int pid = 0;
                      linkedlist_add(messages, filename);

                      pid = cer_fork();
                      if(-1==pid) {
                        clog(log, CDEBUG, "FORK: Could not fork, running straight through");
                        // if we couldn't fork run the parser and risk killing entire process
                        exit_code = cer_parse_files(log, &cer_config, xml_root, &messages);

                        if(pop3->dele) {
                          if(1==cer_config->pop3_max_delete) {
                            cpop3_dele(log, pop3);
                          }
                          else if(0==exit_code) {
                            cpop3_dele(log, pop3);
                          }
                        }
                      }
                      else if(0==pid) {
                        clog(log, CDEBUG, "FORK: Forked, running file parser");
                        exit_code = cer_parse_files(log, &cer_config, xml_root, &messages);

                        if(pop3->dele) {
                          if(1==cer_config->pop3_max_delete) {
                            cpop3_dele(log, pop3);
                          }
                          else if(0==exit_code) {
                            cpop3_dele(log, pop3);
                          }
                        }
                        // didn't send quit, just close the fork'd connection
                        cpop3_disconnect(log, pop3);
                        cpop3_free(log, &pop3);

                        // remove the emails from the list that we've processed
                        linkedlist_iterate(messages);
                        while(NULL!=(filename=linkedlist_remove_last(messages))) {
                          free(filename);
                          filename = NULL;
                        }
                        linkedlist_del(cer_config->poplist, free);
                        linkedlist_del(messages, free);
                        goto CLEANUP;
                      }
                      else {
                        // must be the parent process
                        // clean up any forked children that are sitting around
                        clog(log, CDEBUG, "FORK: Forked, am parent waiting for child process");
                        while(0<(pid=cer_wait4(0, NULL, 0, NULL))) {
                          clog(log, CDEBUG, "WAIT: cleaned up after child %d!", pid);
                        };
                      }
                      // remove the emails from the list that we've processed
                      linkedlist_iterate(messages);
                      while(NULL!=(filename=linkedlist_remove_last(messages))) {
                        free(filename);
                        filename = NULL;
                      }
                    }
                    ++y;
                  }
                }
              }
              cpop3_quit(log, pop3);
              cpop3_disconnect(log, pop3);
            }
          }
          else {
            clog(log, CERROR, "POP3: User or Password was NULL, skipping");
          }

        }
        cpop3_free(log, &pop3);
      }

      linkedlist_del(cer_config->poplist, free);
    }
    // otherwise it's stdin
    else {
      clog(log, CMARK, "Parser is in PIPE mode, waiting for input");
      file = cer_save_input(log, cfsys, cer_config->tmp_cerbmail->string);
      if(NULL!=file) {
        filename = strdup(file->filename);
        linkedlist_add(messages, filename);
        cfile_close(&file);
        cfile_free(&file);
  
        exit_code = cer_curl_init(log, &cer_config, cer_config->curl_location);
  
        if(0==exit_code) {
          exit_code = cer_parse_files(log, &cer_config, xml_root, &messages);
        }
      }
    }

    // free the linked list
    linkedlist_del(messages, free);
  }

  // this is above cleanup to keep the forks from clobbering each other
  cfile_cleanup(&cfsys);
  
CLEANUP:

  if(NULL!=dl_curl_easy_cleanup) {
    // clean up the info in cURL
    dl_curl_easy_cleanup(cer_config->curl);
  }
  if(NULL!=dl_curl_global_cleanup) {
    // close down curl
    dl_curl_global_cleanup();
  }

#ifndef WIN32
  if(NULL!=dl_curl) {
     dlclose(dl_curl);
     dl_curl=NULL;
  }
#endif

  dl_curl = NULL;
  dl_curl_formadd=NULL;
  dl_curl_formfree=NULL;
  dl_curl_global_init=NULL;
  dl_curl_easy_init=NULL;
  dl_curl_easy_setopt=NULL;
  dl_curl_easy_perform=NULL;
  dl_curl_easy_cleanup=NULL;
  dl_curl_global_cleanup=NULL;

  // free the xml data
  cxml_root_free(log, &xml_root);
  if(NULL!=cer_config->xsp) cstring_free(&cer_config->xsp);

  cstring_free(&cer_config->curl_location);

  cstring_free(&cer_config->tmp_cerbmail);
  cstring_free(&cer_config->tmp_cerbmime);

  // free the optional SSL data
  cstring_free(&cer_config->curl_cainfo);
  cstring_free(&cer_config->curl_capath);
  cer_config->curl_verifyhostpeer = 0;

  clog(log, CMARK, "Shutting Down");

  // close the log file
  clog_close(log);
  
  free(cer_config);
  cer_config=NULL;

  return exit_code;
}
Esempio n. 10
0
static int
run(int argc, char **argv,
		const char *filename,
		const char *outname,
		const char *type,
		const char *symtabpath,
		struct hashmap *macros,
		int verbose)
{
	struct idl idl;
	struct sym iface;
	unsigned char _outname[PATH_MAX];

	memset(&idl, 0, sizeof(idl));
	idl.argc = argc;
	idl.argv = argv;
	idl.type = type;
	idl.macros = macros;
	idl.verbose = verbose;
	idl.al = NULL;
	if ((idl.syms = hashmap_new(hash_str, cmp_str, NULL, idl.al)) == NULL ||
				(idl.consts = hashmap_new(hash_str, cmp_str, NULL, idl.al)) == NULL ||
				(idl.tmp = hashmap_new(hash_str, cmp_str, NULL, idl.al)) == NULL) {
		AMSG("");
		return -1;
	}
	if (symload(&idl, symtabpath) == -1) {
		if (errno != ENOENT || symload(&idl, path_filename(symtabpath)) == -1) {
			AMSG("");
			return -1;
		}
	}
                                            /* generate parse tree in iface */
	syminit(&iface, idl.al);
	if (idl_process_file(&idl, filename, &iface) == -1) {
		AMSG("");
		return -1;
	}

	if (idl.verbose > 1) {
		print_tree(&idl, &iface, 0);                     /* print everything */
	} else if (idl.verbose) {
		iter_t iter;
		struct sym *mem;
	
		linkedlist_iterate(&iface.mems, &iter);
		while ((mem = linkedlist_next(&iface.mems, &iter))) {
			if (IS_OPERATION(mem) == 0) {
				continue;
			}
			print_tree(&idl, mem, 0); /* only print operations and their params */
		}
	}
	if (idl.verbose)
		fprintf(stderr, " No Flg   Type            Ptr   Name      OutType       NdrType Siz Aln Off Attributes\n");

	mkoutname(_outname, outname ? outname : filename, "");
	idl.outname = dupstr(path_filename(_outname), NULL);

	if (strcmp(type, "jcifs") == 0) {
		mkoutname(_outname, outname ? outname : filename, ".java");
		if (run_one(&idl, &iface, _outname, emit_stub_jcifs) == -1) {
			AMSG("");
			return -1;
		}
	} else if (strcmp(type, "java") == 0) {
		mkoutname(_outname, outname ? outname : filename, ".java");
		if (run_one(&idl, &iface, _outname, emit_stub_java) == -1) {
			AMSG("");
			return -1;
		}
	} else if (*type == 's') {
		mkoutname(_outname, outname ? outname : filename, ".c");
		if (run_one(&idl, &iface, _outname, emit_stub_samba) == -1) {
			AMSG("");
			return -1;
		}
	} else if (*type == 'c') {
		mkoutname(_outname, outname ? outname : filename, ".h");
		if (run_one(&idl, &iface, _outname, emit_hdr_c) == -1) {
			AMSG("");
			return -1;
		}
		mkoutname(_outname, outname ? outname : filename, "_s.c");
		if (run_one(&idl, &iface, _outname, emit_svr_stub_c) == -1) {
			AMSG("");
			return -1;
		}
		mkoutname(_outname, outname ? outname : filename, "_c.c");
	}

	return 0;
}
Esempio n. 11
0
int
LinkedlistExercise(int verbose, struct cfg *cfg, char *args[])
{
    int rate, i, n = 0, idx;
	char *str;
    struct linkedlist *l = linkedlist_new(EXERCISE_MED_COUNT, NULL);

	cfg = NULL; args[0] = NULL;

	if (l == NULL) {
		AMSG("");
		return -1;
	}

	rate = EXERCISE_R0;
	for (i = 0; i < EXERCISE_MED_COUNT; i++) {
		if (i == EXERCISE_MED_P1) {
			rate = EXERCISE_R1;
		} else if (i == EXERCISE_MED_P2) {
			rate = EXERCISE_R2;
		} else if (i == EXERCISE_MED_P3) {
			rate = EXERCISE_R3;
		}

		if (rand() % 10 < rate) {
			idx = 0;
	        str = malloc(8);
	        sprintf(str, "%07d", n++);
			if (rand() % 5) {
				idx = linkedlist_size(l);
				if (idx) {
					idx = rand() % idx;
				}
			}
	        if (linkedlist_insert(l, idx, str) == -1) {
				PMNO(errno);
				return -1;
			}
			tcase_printf(verbose, "INSERT: %s size now %d\n", str, linkedlist_size(l));
		} else {
			if (linkedlist_is_empty(l)) {
				tcase_printf(verbose, "EMPTY\n");
			} else {
				idx = rand() % linkedlist_size(l);
		        str = linkedlist_get(l, idx);
				if (linkedlist_remove_data(l, str) == NULL) {
					PMNO(errno);
					return -1;
				}
				if ((idx % 10) == 0) {
					unsigned int count = 0;
					iter_t iter;

					linkedlist_iterate(l, &iter);
					while (linkedlist_next(l, &iter)) {
						count++;
					}
					if (count != linkedlist_size(l)) {
						PMSG("count=%u,linkedlist_size=%u\n", count, linkedlist_size(l));
						return -1;
					}
				}
				if (str) {
	    	    	tcase_printf(verbose, "REMOVE: %s %d\n", str, linkedlist_size(l));
			       	free(str);
				} else {
					PMSG("remove failure");
					return -1;
				}
			}
		}
    }
    linkedlist_del(l, allocator_free, NULL);

    return 0;
}