static int save_att(zmail_parser_t * parser, zmail_mime_t * mime, int i) { char *sname, *p; char tmpname[256]; sname = mime->name_rd; if (ZEMPTY(sname)) { sname = mime->filename_rd; } if (ZEMPTY(sname)) { sprintf(tmpname, "att/unknown_%d.dat", i); } else { snprintf(tmpname, 255, "att/%s", sname); p = tmpname + 4; while (*p) { char ch = *p; if ((ch == '?') || (ch == '<') || (ch == '>')) { *p++ = ' '; continue; } if ((ch == '"') || (ch == '\'') || (ch == '|')) { *p++ = ' '; continue; } if ((ch == '/') || (ch == '\\') || (ch == '*')) { *p++ = ' '; continue; } p++; } } zbuf_t *out; int ret; out = zbuf_create(mime->body_len * 2); ret = zmail_parser_decode_mime_body(parser, mime, out); printf("save attachment %s\n", tmpname); if (ret < 0) { printf("mail_parser_decode_mime_body: error\n"); } else if (zfile_put_contents(tmpname, ZBUF_DATA(out), ret) < 0) { printf("mail_parser_decode_mime_body: save %m\n"); } if (mime->is_tnef) { int j = 0; ztnef_parser_t *tnef_parser; tnef_parser = ztnef_parser_create(ZBUF_DATA(out), ret); if (ztnef_parser_run(tnef_parser) < 0) { printf("can not decode tnef"); } for (j = 0; j < tnef_parser->attachment_mime_count; j++) { save_att_tnef(tnef_parser, tnef_parser->attachment_mime_list[j], j + 1); } ztnef_parser_free(tnef_parser); } zbuf_free(out); return 0; }
/* * Free zero copy buffers at request of descriptor. */ void bpf_zerocopy_free(struct bpf_d *d) { struct zbuf *zb; KASSERT(d->bd_bufmode == BPF_BUFMODE_ZBUF, ("bpf_zerocopy_free: not in zbuf mode")); zb = (struct zbuf *)d->bd_sbuf; if (zb != NULL) zbuf_free(zb); zb = (struct zbuf *)d->bd_hbuf; if (zb != NULL) zbuf_free(zb); zb = (struct zbuf *)d->bd_fbuf; if (zb != NULL) zbuf_free(zb); }
/* * Create a zbuf describing a range of user address space memory. Validate * page alignment, size requirements, etc. */ static int zbuf_setup(struct thread *td, vm_offset_t uaddr, size_t len, struct zbuf **zbp) { struct zbuf *zb; struct vm_map *map; int error, i; *zbp = NULL; /* * User address must be page-aligned. */ if (uaddr & PAGE_MASK) return (EINVAL); /* * Length must be an integer number of full pages. */ if (len & PAGE_MASK) return (EINVAL); /* * Length must not exceed per-buffer resource limit. */ if ((len / PAGE_SIZE) > BPF_MAX_PAGES) return (EINVAL); /* * Allocate the buffer and set up each page with is own sf_buf. */ error = 0; zb = malloc(sizeof(*zb), M_BPF, M_ZERO | M_WAITOK); zb->zb_uaddr = uaddr; zb->zb_size = len; zb->zb_numpages = len / PAGE_SIZE; zb->zb_pages = malloc(sizeof(struct sf_buf *) * zb->zb_numpages, M_BPF, M_ZERO | M_WAITOK); map = &td->td_proc->p_vmspace->vm_map; for (i = 0; i < zb->zb_numpages; i++) { zb->zb_pages[i] = zbuf_sfbuf_get(map, uaddr + (i * PAGE_SIZE)); if (zb->zb_pages[i] == NULL) { error = EFAULT; goto error; } } zb->zb_header = (struct bpf_zbuf_header *)sf_buf_kva(zb->zb_pages[0]); bzero(zb->zb_header, sizeof(*zb->zb_header)); *zbp = zb; return (0); error: zbuf_free(zb); return (error); }
static int _close(ZMAP * zm) { ZMAP_MYSQL *db; db = (ZMAP_MYSQL *) (zm->db); zbuf_free(db->qb); zbuf_free(db->query_expand); zstr_free(db->query_format); zstr_free(db->host); zstr_free(db->dbuser); zstr_free(db->dbpass); zstr_free(db->dbname); ___close(zm); zfree(db); return Z_OK; }
void test() { zbuf_t *bf; int result_len; bf = zbuf_create(102400); if (cmd == 'd') { result_len = zbase64_decode(reader.data, reader.len, bf); } else { result_len = zbase64_encode(reader.data, reader.len, bf, 1); } printf("result: %d\n%s\n", result_len, ZBUF_DATA(bf)); zbuf_free(bf); }
int zmail_parser_decode_text_mime_body(zmail_parser_t * parser, zmail_mime_t * mime, zbuf_t *dest) { int bq = 0, convert_len; char *in_src = parser->mail_data + mime->body_offset; int in_len = mime->body_len; char *encoding = mime->encoding; zbuf_t *zbuf3 = 0; char *buf3; int buf3_len; if (!encoding || !*encoding) { bq = 0; } else if (!strcasecmp(encoding, "base64")) { bq = 'b'; } else if (!strcasecmp(encoding, "quoted-printable")) { bq = 'q'; } else { bq = 0; } if (bq) { zbuf3 = zbuf_create(10240); if (bq == 'b') { buf3_len = zbase64_decode(in_src, in_len, zbuf3); } else { buf3_len = zqp_decode_2045(in_src, in_len, zbuf3); } buf3 = ZBUF_DATA(zbuf3); buf3_len = ZBUF_LEN(zbuf3); } else { buf3 = in_src; buf3_len = in_len; } convert_len = zmail_parser_iconv(parser, mime->charset, buf3, buf3_len, dest); if (zbuf3) { zbuf_free(zbuf3); } return convert_len; }
/* * Ioctl to configure zero-copy buffers -- may be done only once. */ int bpf_zerocopy_ioctl_setzbuf(struct thread *td, struct bpf_d *d, struct bpf_zbuf *bz) { struct zbuf *zba, *zbb; int error; KASSERT(d->bd_bufmode == BPF_BUFMODE_ZBUF, ("bpf_zerocopy_ioctl_setzbuf: not in zbuf mode")); /* * Must set both buffers. Cannot clear them. */ if (bz->bz_bufa == NULL || bz->bz_bufb == NULL) return (EINVAL); /* * Buffers must have a size greater than 0. Alignment and other size * validity checking is done in zbuf_setup(). */ if (bz->bz_buflen == 0) return (EINVAL); /* * Allocate new buffers. */ error = zbuf_setup(td, (vm_offset_t)bz->bz_bufa, bz->bz_buflen, &zba); if (error) return (error); error = zbuf_setup(td, (vm_offset_t)bz->bz_bufb, bz->bz_buflen, &zbb); if (error) { zbuf_free(zba); return (error); } /* * We only allow buffers to be installed once, so atomically check * that no buffers are currently installed and install new buffers. */ BPFD_WLOCK(d); if (d->bd_hbuf != NULL || d->bd_sbuf != NULL || d->bd_fbuf != NULL || d->bd_bif != NULL) { BPFD_WUNLOCK(d); zbuf_free(zba); zbuf_free(zbb); return (EINVAL); } /* * Point BPF descriptor at buffers; initialize sbuf as zba so that * it is always filled first in the sequence, per bpf(4). */ d->bd_fbuf = (caddr_t)zbb; d->bd_sbuf = (caddr_t)zba; d->bd_slen = 0; d->bd_hlen = 0; /* * We expose only the space left in the buffer after the size of the * shared management region. */ d->bd_bufsize = bz->bz_buflen - sizeof(struct bpf_zbuf_header); BPFD_WUNLOCK(d); return (0); }
int zdict_parse_line(ZDICT * zd, char *buf, int len) { int i, last_ch, now; char ch; ZBUF *zb, *zbk, *zbv; zbk = zbuf_create(128); zbv = zbuf_create(128); last_ch = -1; now = 1; zb = zbk; for (i = 0; i <= len; i++) { if (i == len) { ch = '\0'; } else { ch = buf[i]; } if (last_ch == '\\') { last_ch = -1; if (ch == ';' || ch == '=' || ch == '\\') { zbuf_put(zb, ch); continue; } zbuf_put(zb, '\\'); zbuf_put(zb, ch); continue; } if (ch == '\\') { last_ch = ch; continue; } if (ch == '=') { if (now == 1) { zb = zbv; now = 2; continue; } } if (ch == ';' || ch == '\0') { if (ZBUF_LEN(zbv) == 0) { zbuf_strcpy(zbv, "yes"); } if (ZBUF_LEN(zbk)) { ZBUF_TERMINATE(zbk); ZBUF_TERMINATE(zbv); zdict_enter_STR(zd, ZBUF_DATA(zbk), ZBUF_DATA(zbv)); } ZBUF_RESET(zbk); ZBUF_RESET(zbv); zb = zbk; now = 1; continue; } zbuf_put(zb, ch); } zbuf_free(zbk); zbuf_free(zbv); return 0; }