NODE* parse_any(SCANNER *s, int mode) { NODE *x = NULL; int c; skip_white(s); if (s_eof(s)) return raise(s, "unexpected end of file"); c = s_peek(s); if (c == '(') { s_getc(s); x = parse_paren(s, mode); if (x == NULL) return NULL; if (s_eof(s)) { return raise(s, "unexpected end of file"); } skip_white(s); if (s_getc(s) != ')') { return invalid_token(s); } } else if (c == '\'') x = parse_quote(s); else if (c == '`') return parse_bquote(s); else if (c == '"') x = parse_string(s); else if (isalnum((int)c) || strchr(SYMBOL_CHARS, c)) x = parse_primitive(s); else return invalid_token(s); return x; }
static NODE* parse_string(SCANNER *s) { char *buf = NULL; int n = 0, l = 0; int c = 0; NODE *node; buf = (char*)malloc(10); s_getc(s); while (!s_eof(s)) { c = s_getc(s); if (c == '\\' && !s_eof(s)) { c = s_peek(s); switch (c) { case '\\': c = '\\'; break; case 'b': c = '\b'; break; case 'f': c = '\f'; break; case 'n': c = '\n'; break; case 'r': c = '\r'; break; case 't': c = '\t'; break; default: free(buf); return invalid_token(s); } } else if (c == '"') break; if (n == l) { buf = (char*)realloc(buf, l+20); l += 20; } buf[n++] = c; } buf[n] = 0; if (c != '"') { free(buf); return invalid_token(s); } node = new_node(); node->t = NODE_STRING; node->s = (char*)realloc(buf, n+1); return node; }
int do_crypt(FILE *in, FILE *out, int do_encrypt, unsigned char *key_data, int key_data_len, unsigned char *salt) { /* Allow enough space in output buffer for additional block */ int i, nrounds = 2; unsigned char key[16], iv[16]; unsigned char inbuf[1024], outbuf[1024 + EVP_MAX_BLOCK_LENGTH]; memset(inbuf, 0, 1024); int inlen, outlen; EVP_CIPHER_CTX ctx; /* Bogus key and IV: we'd normally set these from * another source. */ i = EVP_BytesToKey(EVP_aes_128_cbc(), EVP_sha1(), salt, key_data, key_data_len, nrounds, key, iv); if (i != 16) { printf("Key size is %d bits - should be 256 bits\n", i); return -1; } /* Don't set key or IV right away; we want to check lengths */ EVP_CIPHER_CTX_init(&ctx); EVP_CipherInit_ex(&ctx, EVP_aes_128_cbc(), NULL, NULL, NULL, do_encrypt); OPENSSL_assert(EVP_CIPHER_CTX_key_length(&ctx) == 16); OPENSSL_assert(EVP_CIPHER_CTX_iv_length(&ctx) == 16); /* Now we can set key and IV */ EVP_CipherInit_ex(&ctx, NULL, NULL, key, iv, do_encrypt); for (;;) { inlen = fread(inbuf, 1, 1024, in); if (inlen <= 0) break; memset(outbuf, 0, 1024 + EVP_MAX_BLOCK_LENGTH); if (!EVP_CipherUpdate(&ctx, outbuf, &outlen, inbuf, inlen)) { /* Error */ EVP_CIPHER_CTX_cleanup(&ctx); return 0; } fwrite(outbuf, 1, outlen, out); memset(inbuf, 0, 1024); } if (!EVP_CipherFinal_ex(&ctx, outbuf, &outlen)) { /* Error */ EVP_CIPHER_CTX_cleanup(&ctx); fclose(in); fclose(out); invalid_token(); } fwrite(outbuf, 1, outlen, out); EVP_CIPHER_CTX_cleanup(&ctx); fclose(in); fclose(out); return 1; }