object_t primitive_read(object_t argl) { object_t r = parse_sexp(read_sexp(stdin)); if(r == NULL) return obj_new_symbol("_empty_"); else return r; }
/**** * main ****/ int main(int argc, char **argv) { char buf[256]; /* string to sprintf to */ float vals[3]; /* place to put data */ sexp_t *sx; /*** method #1: create expression as string on one side, extract data on the other. ***/ printf("===>> PART 1 <<===\n"); sprintf(buf,"(thetag (1.0 2.0 3.0))"); sx = parse_sexp(buf,strlen(buf)); extract(sx,vals,3); printf("Extracted V1=%f V2=%f V3=%f\n",vals[0],vals[1],vals[2]); destroy_sexp(sx); /*** method #2: packing function creates expression, same extract function extracts data. print in between to show expression. ***/ printf("\n===>> PART 2 <<===\n"); sx = pack("part2tag",4.0,5.0,6.0); print_sexp(buf,256,sx); printf("SX=%s\n",buf); extract(sx,vals,3); printf("Extracted V1=%f V2=%f V3=%f\n",vals[0],vals[1],vals[2]); destroy_sexp(sx); return 0; }
int ml_eval(char **input, char resp[BUFSIZ], dict_t *env) { size_t len = strlen(*input); sexp_t *sx = parse_sexp(*input, len); char *v; memset(resp, 0, sizeof(*resp)); if (sx->ty != SEXP_LIST || sx->list->ty != SEXP_VALUE) { print_sexp(resp, BUFSIZ, sx); return 0; } v = sx->list->val; if (strcmp(v, "define") == 0) { insert(sx->list->next->val, sx->list->next->next, env); print_sexp(resp, BUFSIZ, sx->list->next->next); sx->list->next->next = NULL; } else if (strcmp(v, "env") == 0) { sexp_t *out, *vlist, *vptr; out = new_sexp_list(NULL); dict_t *_env = env; dict_t *next; char r2[BUFSIZ]; memset(r2, 0, BUFSIZ); while (_env != NULL) { next = _env->next; vlist = new_sexp_list(NULL); if (out->list == NULL) { out->list = vptr = vlist; } else { vptr->next = vlist; vptr = vlist; } vlist->list = new_sexp_atom(_env->varname, strlen(_env->varname), SEXP_BASIC); vlist->list->next = _env->valexp; _env = next; } print_sexp(resp, BUFSIZ, out); } else { sexp_t *ret = NULL; lookup(v, env, &ret); print_sexp(resp, BUFSIZ, ret); } return 0; }
object_t read_file(char *filename) { FILE *f = fopen(filename, "r"); char *input; object_t seq = cons(obj_new_symbol("begin"), NIL); while((input = read_sexp(f)) != NULL) { object_t exp = parse_sexp(input); storage_append(exp, seq); } return seq; }
/* Return the algo of a public RSA expressed as an canonical encoded S-expression. On error the algo is set to 0. */ gpg_error_t get_pk_algo_from_canon_sexp (const unsigned char *keydata, size_t keydatalen, int *r_algo) { gpg_error_t err; const unsigned char *buf, *tok; size_t buflen, toklen; int depth; *r_algo = 0; buf = keydata; buflen = keydatalen; depth = 0; if ((err = parse_sexp (&buf, &buflen, &depth, &tok, &toklen))) return err; if ((err = parse_sexp (&buf, &buflen, &depth, &tok, &toklen))) return err; if (!tok || toklen != 10 || memcmp ("public-key", tok, toklen)) return gpg_error (GPG_ERR_BAD_PUBKEY); if ((err = parse_sexp (&buf, &buflen, &depth, &tok, &toklen))) return err; if ((err = parse_sexp (&buf, &buflen, &depth, &tok, &toklen))) return err; if (!tok) return gpg_error (GPG_ERR_BAD_PUBKEY); if (toklen == 3 && !memcmp ("rsa", tok, toklen)) *r_algo = GCRY_PK_RSA; else if (toklen == 3 && !memcmp ("dsa", tok, toklen)) *r_algo = GCRY_PK_DSA; else if (toklen == 3 && !memcmp ("elg", tok, toklen)) *r_algo = GCRY_PK_ELG; else if (toklen == 5 && !memcmp ("ecdsa", tok, toklen)) *r_algo = GCRY_PK_ECDSA; else return gpg_error (GPG_ERR_PUBKEY_ALGO); return 0; }
object_t *parse_program(FILE *fp){ char buf; buf = getc(fp); if(buf==EOF){ return NULL; } else{ ungetc(buf,fp); return parse_sexp(fp); } }
int main(int argc, char **argv) { while(1) { printf("> "); struct node *lines = read_sexp(stdin); object_t sexp = parse_sexp(lines); if(*(char*)lines->data == 'q') { auxfor_each(lines, &free_node_and_string); return 0; } print_object(sexp); printf("\n"); auxfor_each(lines, &free_node_and_string); } }
object_t *parse_list(FILE *fp){ cons_t tmp_cons; char buf; buf=skip_space_getchar(fp); tmp_cons.car = parse_sexp(fp); buf=skip_space_getchar(fp); if(buf == ')'){ tmp_cons.cdr = NULL; } else{ ungetc(buf,fp); tmp_cons.cdr = parse_list_inner(fp); } return make_cons(tmp_cons.car,tmp_cons.cdr); }
Cons parse_cons(char *string, int *offset) { Cons cur, head, pre; int step; pre = head = make_cons(lt_nil, lt_nil); for (int i = 0; string[i] != '\0'; i += step) { switch (string[i]) { case '(': cur = make_cons(parse_cons(string + i + 1, &step), lt_nil); break; case ' ': case '\n': step = 1; continue; case ')': *offset = i + 2; pre = CDR(head); free_cons(head); return pre; case '\'': { /* Symbol quote; */ LispObject obj; /* quote = S("QUOTE"); */ obj = parse_sexp(string + i + 1, &step); /* cur = make_cons(make_cons(S("QUOTE"), make_cons(obj, lt_nil)), lt_nil); */ cur = make_cons(make_list(S("QUOTE"), obj), lt_nil); step++; break; } default : cur = make_cons(parse_atom(string + i, &step), lt_nil); } set_cdr(pre, cur); pre = cur; } pre = CDR(head); free_cons(head); return pre; }
static ptree_t * parse_sexp(octet_t * sexp) { ptree_t *ptp, *ntp = 0, *ptr; if (*sexp->val == '(') { ptp = (ptree_t *) Calloc(1, sizeof(ptree_t)); ptp->list = 1; sexp->val++; sexp->len--; while (sexp->len && *sexp->val != ')') { if ((ptr = parse_sexp(sexp)) == 0) { ptree_free(ptp); return 0; } if (ptp->part == 0) ntp = ptp->part = ptr; else { ntp->next = ptr; ntp = ntp->next; } } if (*sexp->val == ')') { sexp->val++; sexp->len--; } else { /* error */ ptree_free(ptp); return 0; } } else { ptp = (ptree_t *) Calloc(1, sizeof(ptree_t)); if (get_str(sexp, &ptp->val) != SPOCP_SUCCESS) { ptree_free(ptp); return 0; } } return ptp; }
object_t read_stream(FILE *stream) { input = read_sexp(stream); return parse_sexp(input); }
/* The `input' will be free in the future. */ LispObject parse_input(char *input) { int trash; return parse_sexp(input, &trash); }
/* Transform a sig-val style s-expression as returned by Libgcrypt to one which includes an algorithm identifier encoding the public key and the hash algorithm. The public key algorithm is taken directly from SIGVAL and the hash algorithm is given by MDALGO. This is required because X.509 merges the public key algorithm and the hash algorithm into one OID but Libgcrypt is not aware of that. The function ignores missing parameters so that it can also be used to create an siginfo value as expected by ksba_certreq_set_siginfo. To create a siginfo s-expression a public-key s-expression may be used instead of a sig-val. We only support RSA for now. */ gpg_error_t transform_sigval (const unsigned char *sigval, size_t sigvallen, int mdalgo, unsigned char **r_newsigval, size_t *r_newsigvallen) { gpg_error_t err; const unsigned char *buf, *tok; size_t buflen, toklen; int depth, last_depth1, last_depth2; int is_pubkey = 0; const unsigned char *rsa_s = NULL; size_t rsa_s_len; const char *oid; gcry_sexp_t sexp; *r_newsigval = NULL; if (r_newsigvallen) *r_newsigvallen = 0; buf = sigval; buflen = sigvallen; depth = 0; if ((err = parse_sexp (&buf, &buflen, &depth, &tok, &toklen))) return err; if ((err = parse_sexp (&buf, &buflen, &depth, &tok, &toklen))) return err; if (tok && toklen == 7 && !memcmp ("sig-val", tok, toklen)) ; else if (tok && toklen == 10 && !memcmp ("public-key", tok, toklen)) is_pubkey = 1; else return gpg_error (GPG_ERR_UNKNOWN_SEXP); if ((err = parse_sexp (&buf, &buflen, &depth, &tok, &toklen))) return err; if ((err = parse_sexp (&buf, &buflen, &depth, &tok, &toklen))) return err; if (!tok || toklen != 3 || memcmp ("rsa", tok, toklen)) return gpg_error (GPG_ERR_WRONG_PUBKEY_ALGO); last_depth1 = depth; while (!(err = parse_sexp (&buf, &buflen, &depth, &tok, &toklen)) && depth && depth >= last_depth1) { if (tok) return gpg_error (GPG_ERR_UNKNOWN_SEXP); if ((err = parse_sexp (&buf, &buflen, &depth, &tok, &toklen))) return err; if (tok && toklen == 1) { const unsigned char **mpi; size_t *mpi_len; switch (*tok) { case 's': mpi = &rsa_s; mpi_len = &rsa_s_len; break; default: mpi = NULL; mpi_len = NULL; break; } if (mpi && *mpi) return gpg_error (GPG_ERR_DUP_VALUE); if ((err = parse_sexp (&buf, &buflen, &depth, &tok, &toklen))) return err; if (tok && mpi) { *mpi = tok; *mpi_len = toklen; } } /* Skip to the end of the list. */ last_depth2 = depth; while (!(err = parse_sexp (&buf, &buflen, &depth, &tok, &toklen)) && depth && depth >= last_depth2) ; if (err) return err; } if (err) return err; /* Map the hash algorithm to an OID. */ switch (mdalgo) { case GCRY_MD_SHA1: oid = "1.2.840.113549.1.1.5"; /* sha1WithRSAEncryption */ break; case GCRY_MD_SHA256: oid = "1.2.840.113549.1.1.11"; /* sha256WithRSAEncryption */ break; case GCRY_MD_SHA384: oid = "1.2.840.113549.1.1.12"; /* sha384WithRSAEncryption */ break; case GCRY_MD_SHA512: oid = "1.2.840.113549.1.1.13"; /* sha512WithRSAEncryption */ break; default: return gpg_error (GPG_ERR_DIGEST_ALGO); } if (rsa_s && !is_pubkey) err = gcry_sexp_build (&sexp, NULL, "(sig-val(%s(s%b)))", oid, (int)rsa_s_len, rsa_s); else err = gcry_sexp_build (&sexp, NULL, "(sig-val(%s))", oid); if (err) return err; err = make_canon_sexp (sexp, r_newsigval, r_newsigvallen); gcry_sexp_release (sexp); return err; }
/* Return the parameters of a public RSA key expressed as an canonical encoded S-expression. */ gpg_error_t get_rsa_pk_from_canon_sexp (const unsigned char *keydata, size_t keydatalen, unsigned char const **r_n, size_t *r_nlen, unsigned char const **r_e, size_t *r_elen) { gpg_error_t err; const unsigned char *buf, *tok; size_t buflen, toklen; int depth, last_depth1, last_depth2; const unsigned char *rsa_n = NULL; const unsigned char *rsa_e = NULL; size_t rsa_n_len, rsa_e_len; *r_n = NULL; *r_nlen = 0; *r_e = NULL; *r_elen = 0; buf = keydata; buflen = keydatalen; depth = 0; if ((err = parse_sexp (&buf, &buflen, &depth, &tok, &toklen))) return err; if ((err = parse_sexp (&buf, &buflen, &depth, &tok, &toklen))) return err; if (!tok || toklen != 10 || memcmp ("public-key", tok, toklen)) return gpg_error (GPG_ERR_BAD_PUBKEY); if ((err = parse_sexp (&buf, &buflen, &depth, &tok, &toklen))) return err; if ((err = parse_sexp (&buf, &buflen, &depth, &tok, &toklen))) return err; if (!tok || toklen != 3 || memcmp ("rsa", tok, toklen)) return gpg_error (GPG_ERR_WRONG_PUBKEY_ALGO); last_depth1 = depth; while (!(err = parse_sexp (&buf, &buflen, &depth, &tok, &toklen)) && depth && depth >= last_depth1) { if (tok) return gpg_error (GPG_ERR_UNKNOWN_SEXP); if ((err = parse_sexp (&buf, &buflen, &depth, &tok, &toklen))) return err; if (tok && toklen == 1) { const unsigned char **mpi; size_t *mpi_len; switch (*tok) { case 'n': mpi = &rsa_n; mpi_len = &rsa_n_len; break; case 'e': mpi = &rsa_e; mpi_len = &rsa_e_len; break; default: mpi = NULL; mpi_len = NULL; break; } if (mpi && *mpi) return gpg_error (GPG_ERR_DUP_VALUE); if ((err = parse_sexp (&buf, &buflen, &depth, &tok, &toklen))) return err; if (tok && mpi) { /* Strip off leading zero bytes and save. */ for (; toklen && !*tok; toklen--, tok++) ; *mpi = tok; *mpi_len = toklen; } } /* Skip to the end of the list. */ last_depth2 = depth; while (!(err = parse_sexp (&buf, &buflen, &depth, &tok, &toklen)) && depth && depth >= last_depth2) ; if (err) return err; } if (err) return err; if (!rsa_n || !rsa_n_len || !rsa_e || !rsa_e_len) return gpg_error (GPG_ERR_BAD_PUBKEY); *r_n = rsa_n; *r_nlen = rsa_n_len; *r_e = rsa_e; *r_elen = rsa_e_len; return 0; }
// Takes in a string with the s-expression and breaks it // up into it's pieces, putting them in a deque<string>. // Example: input "(game-move-denied 'no game')" // returns <"game-move-denied", "no game"> sexp_t* extract_sexpr(string sexpr) { // STUB sexp_t* st = parse_sexp( (char *)sexpr.c_str(), sexpr.length() ); return st; }
int main(int argc, char **argv) { int fd; sexp_t *sx; sexp_iowrap_t *iow; int diff; unsigned int i; int ch; char fname[BUFSIZ]; CSTRING *s1,*s2; int passes, failures; char outbuf1[8192], outbuf2[8192]; s1 = s2 = NULL; passes = failures = 0; strcpy(fname,TESTFILE); while ((ch = getopt(argc,argv,"f:")) != -1) { switch ((char)ch) { case 'f': strcpy(fname,optarg); break; default: break; } } fd = open(fname,O_RDONLY); iow = init_iowrap(fd); printf("TESTING CSTRING BASED UNPARSE:\n"); sx = read_one_sexp(iow); while (sx != NULL) { print_sexp_cstr(&s1,sx,8); destroy_sexp(sx); sx = parse_sexp(s1->base,s1->curlen); if (sx == NULL) { fprintf(stderr,"ERROR: parser error state of %d\n", sexp_errno); exit(1); } print_sexp_cstr(&s2,sx,8); destroy_sexp(sx); sexp_cleanup(); diff = 0; for (i=0;i<s1->curlen;i++) { diff += abs((s1->base[i] - s2->base[i])); if (s1->base[i] == '\0') break; } /** * diff is the lexical difference between the first unparsing * of the original buffer and the unparsed version of the parsed * version of the first unparsed string. In other words, does: * * orig->parse->unparse == orig->parse->unparse->parse->unparse * * This catches issues with print and parse to make sure the meaning * of the original is kept (or at least, "bugs" in the parser have * matching "bugs" in the printer.) */ if (diff != 0) { printf("FIXED POINT MISSED (diff=%d): \nS1: %s\nS2: %s\n",diff, s1->base,s2->base); failures++; } else { passes++; } /** clean up strings **/ sdestroy(s1); sdestroy(s2); s1 = s2 = NULL; sx = read_one_sexp(iow); } destroy_iowrap(iow); close(fd); printf("TOTAL TESTS: %d PASS=%d FAIL=%d\n\n", passes+failures,passes,failures); passes = failures = 0; /*** *** now do normal fixed length buffer unparse testing ***/ fd = open(fname,O_RDONLY); iow = init_iowrap(fd); printf("TESTING FIXED SIZE BUFFER BASED UNPARSE:\n"); sx = read_one_sexp(iow); while (sx != NULL) { print_sexp(outbuf1,8192,sx); destroy_sexp(sx); sx = parse_sexp(outbuf1,8192); if (sx == NULL) { fprintf(stderr,"ERROR: parser error state of %d\n", sexp_errno); exit(1); } print_sexp(outbuf2,8192,sx); destroy_sexp(sx); sexp_cleanup(); diff = 0; for (i=0;i<8192;i++) { diff += abs((outbuf1[i] - outbuf2[i])); if (outbuf1[i] == '\0' || outbuf2[i] == '\0') break; } /** * diff is the lexical difference between the first unparsing * of the original buffer and the unparsed version of the parsed * version of the first unparsed string. In other words, does: * * orig->parse->unparse == orig->parse->unparse->parse->unparse * * This catches issues with print and parse to make sure the meaning * of the original is kept (or at least, "bugs" in the parser have * matching "bugs" in the printer.) */ if (diff != 0) { printf("FIXED POINT MISSED (diff=%d): \nS1: %s\nS2: %s\n",diff, outbuf1,outbuf2); failures++; } else { passes++; } sx = read_one_sexp(iow); } destroy_iowrap(iow); close(fd); printf("TOTAL TESTS: %d PASS=%d FAIL=%d\n", passes+failures,passes,failures); exit(0); }