static int os_buf_to_tmp_file_posix(Buf *contents, Buf *suffix, Buf *out_tmp_path) { const char *tmp_dir = getenv("TMPDIR"); if (!tmp_dir) { tmp_dir = P_tmpdir; } buf_resize(out_tmp_path, 0); buf_appendf(out_tmp_path, "%s/XXXXXX%s", tmp_dir, buf_ptr(suffix)); int fd = mkstemps(buf_ptr(out_tmp_path), buf_len(suffix)); if (fd < 0) { return ErrorFileSystem; } FILE *f = fdopen(fd, "wb"); if (!f) { zig_panic("fdopen failed"); } size_t amt_written = fwrite(buf_ptr(contents), 1, buf_len(contents), f); if (amt_written != (size_t)buf_len(contents)) zig_panic("write failed: %s", strerror(errno)); if (fclose(f)) zig_panic("close failed"); return 0; }
static int os_buf_to_tmp_file_windows(Buf *contents, Buf *suffix, Buf *out_tmp_path) { char tmp_dir[MAX_PATH + 1]; if (GetTempPath(MAX_PATH, tmp_dir) == 0) { zig_panic("GetTempPath failed"); } buf_init_from_str(out_tmp_path, tmp_dir); const char base64[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_"; assert(array_length(base64) == 64 + 1); for (int i = 0; i < 8; i += 1) { buf_append_char(out_tmp_path, base64[rand() % 64]); } buf_append_buf(out_tmp_path, suffix); FILE *f = fopen(buf_ptr(out_tmp_path), "wb"); if (!f) { zig_panic("unable to open %s: %s", buf_ptr(out_tmp_path), strerror(errno)); } size_t amt_written = fwrite(buf_ptr(contents), 1, buf_len(contents), f); if (amt_written != (size_t)buf_len(contents)) { zig_panic("write failed: %s", strerror(errno)); } if (fclose(f)) { zig_panic("fclose failed"); } return 0; }
void os_path_split(Buf *full_path, Buf *out_dirname, Buf *out_basename) { size_t len = buf_len(full_path); if (len != 0) { size_t last_index = len - 1; if (buf_ptr(full_path)[last_index] == '/') { last_index -= 1; } for (size_t i = last_index;;) { uint8_t c = buf_ptr(full_path)[i]; if (c == '/') { if (out_dirname) { buf_init_from_mem(out_dirname, buf_ptr(full_path), i); } if (out_basename) { buf_init_from_mem(out_basename, buf_ptr(full_path) + i + 1, buf_len(full_path) - (i + 1)); } return; } if (i == 0) break; i -= 1; } } if (out_dirname) buf_init_from_mem(out_dirname, ".", 1); if (out_basename) buf_init_from_buf(out_basename, full_path); }
int decode_vrrp(u_char *buf, int len, u_char *obuf, int olen) { struct buf *b, inbuf, outbuf; struct vrrp_header *vrrp; buf_init(&inbuf, buf, len); buf_init(&outbuf, obuf, olen); vrrp = (struct vrrp_header *)buf_ptr(&inbuf); if (buf_len(&inbuf) < sizeof(*vrrp)) return (0); /* We only care about VRRP_AUTH_SIMPLE */ if (ntohs(vrrp->vr_auth) != VRRP_AUTH_SIMPLE) return (0); /* XXX - probably want to verify checksum */ /* Forward to Authentication Data */ buf_skip(&inbuf, sizeof(*vrrp) + 8 + (vrrp->vr_naddr * 4)); if ((b = buf_tok(&inbuf, NULL, VRRP_AUTH_DATA_LEN)) == NULL) return (0); buf_put(&outbuf, buf_ptr(b), buf_len(b)); buf_put(&outbuf, "\n", 1); buf_end(&outbuf); return (buf_len(&outbuf)); }
void print_err_msg(ErrorMsg *err, ErrColor color) { if (color == ErrColorOn || (color == ErrColorAuto && os_stderr_tty())) { fprintf(stderr, WHITE "%s:%d:%d: " RED "error:" WHITE " %s" RESET "\n", buf_ptr(err->path), err->line_start + 1, err->column_start + 1, buf_ptr(err->msg)); fprintf(stderr, "%s\n", buf_ptr(&err->line_buf)); for (int i = 0; i < err->column_start; i += 1) { fprintf(stderr, " "); } fprintf(stderr, GREEN "^" RESET "\n"); } else { fprintf(stderr, "%s:%d:%d: error: %s\n", buf_ptr(err->path), err->line_start + 1, err->column_start + 1, buf_ptr(err->msg)); } for (int i = 0; i < err->notes.length; i += 1) { ErrorMsg *note = err->notes.at(i); print_err_msg(note, color); } }
buf_t buf_tok(buf_t buf, void *sep, int len) { static struct buf *savebuf, tokbuf; int off; if (buf != NULL) savebuf = buf; if (sep == NULL && buf_len(savebuf) >= len) { tokbuf.base = buf_ptr(savebuf); tokbuf.offset = 0; tokbuf.size = tokbuf.end = len; buf_skip(savebuf, len); } else if ((off = buf_index(savebuf, sep, len)) != -1) { tokbuf.base = buf_ptr(savebuf); tokbuf.offset = 0; tokbuf.size = tokbuf.end = off; buf_skip(savebuf, off + len); } else if (buf_len(savebuf) > 0) { tokbuf.base = buf_ptr(savebuf); tokbuf.offset = 0; tokbuf.size = tokbuf.end = buf_len(savebuf); savebuf->offset = savebuf->end; } else return (NULL); return (&tokbuf); }
static int process_smtp_client(struct smtp_info *smtp, char *data, int len) { struct buf *line, *body, buf; char *p; int i; buf_init(&buf, data, len); if (smtp->state != SMTP_DATA) { while ((i = buf_index(&buf, "\r\n", 2)) >= 0) { line = buf_tok(&buf, NULL, i + 2); line->base[line->end-1] = '\0'; p = buf_ptr(line); if (strncasecmp(p, "RSET", 4) == 0) { smtp->state = SMTP_HELO; } else if (smtp->state == SMTP_NONE && (strncasecmp(p, "HELO", 4) == 0 || strncasecmp(p, "EHLO", 4) == 0)) { smtp->state = SMTP_HELO; } else if (smtp->state == SMTP_HELO && (strncasecmp(p, "MAIL ", 5) == 0 || strncasecmp(p, "SEND ", 5) == 0 || strncasecmp(p, "SAML ", 5) == 0)) { smtp->from = grep_mail_address(p); smtp->state = SMTP_MAIL; } else if (smtp->state == SMTP_MAIL && strncasecmp(p, "RCPT ", 5) == 0) { smtp->state = SMTP_RCPT; } else if (smtp->state == SMTP_RCPT && strncasecmp(p, "DATA", 4) == 0) { smtp->state = SMTP_DATA; break; } } } if (smtp->state == SMTP_DATA) { if ((i = buf_index(&buf, "\r\n.\r\n", 5)) >= 0) { body = buf_tok(&buf, NULL, i); buf_skip(&buf, 5); body->base[body->end] = '\0'; if (regex_match(buf_ptr(body))) print_mbox_msg(smtp->from, buf_ptr(body)); if (smtp->from) { free(smtp->from); smtp->from = NULL; } smtp->state = SMTP_HELO; } } return (len - buf_len(&buf)); }
static void os_spawn_process_windows(const char *exe, ZigList<const char *> &args, int *return_code) { Buf stderr_buf = BUF_INIT; Buf stdout_buf = BUF_INIT; // TODO this is supposed to inherit stdout/stderr instead of capturing it os_exec_process(exe, args, return_code, &stderr_buf, &stdout_buf); fwrite(buf_ptr(&stderr_buf), 1, buf_len(&stderr_buf), stderr); fwrite(buf_ptr(&stdout_buf), 1, buf_len(&stdout_buf), stdout); }
void os_write_file(Buf *full_path, Buf *contents) { FILE *f = fopen(buf_ptr(full_path), "wb"); if (!f) { zig_panic("open failed"); } size_t amt_written = fwrite(buf_ptr(contents), 1, buf_len(contents), f); if (amt_written != (size_t)buf_len(contents)) zig_panic("write failed: %s", strerror(errno)); if (fclose(f)) zig_panic("close failed"); }
int buf_index(buf_t buf, void *ptr, int len) { u_char *p, *q; p = buf_ptr(buf); q = buf->base + buf->end; for (; q - p >= len; p++) { if (memcmp(p, ptr, len) == 0) return (p - buf_ptr(buf)); } return (-1); }
void os_path_join(Buf *dirname, Buf *basename, Buf *out_full_path) { buf_init_from_buf(out_full_path, dirname); uint8_t c = *(buf_ptr(out_full_path) + buf_len(out_full_path) - 1); if (c != '/') buf_append_char(out_full_path, '/'); buf_append_buf(out_full_path, basename); }
int os_delete_file(Buf *path) { if (remove(buf_ptr(path))) { return ErrorFileSystem; } else { return 0; } }
void os_path_split(Buf *full_path, Buf *out_dirname, Buf *out_basename) { int last_index = buf_len(full_path) - 1; if (last_index >= 0 && buf_ptr(full_path)[last_index] == '/') { last_index -= 1; } for (int i = last_index; i >= 0; i -= 1) { uint8_t c = buf_ptr(full_path)[i]; if (c == '/') { buf_init_from_mem(out_dirname, buf_ptr(full_path), i); buf_init_from_mem(out_basename, buf_ptr(full_path) + i + 1, buf_len(full_path) - (i + 1)); return; } } buf_init_from_mem(out_dirname, ".", 1); buf_init_from_buf(out_basename, full_path); }
int decode_imap(u_char *buf, int len, u_char *obuf, int olen) { struct buf *line, inbuf, outbuf; int i; buf_init(&inbuf, buf, len); buf_init(&outbuf, obuf, olen); while ((i = buf_index(&inbuf, "\r\n", 2)) != -1) { line = buf_tok(&inbuf, NULL, i); buf_skip(&inbuf, 2); if ((i = buf_index(line, " ", 1)) != -1) { buf_skip(line, i + 1); if (buf_cmp(line, "LOGIN ", 6) == 0) { buf_putf(&outbuf, "%.*s\n", buf_len(line), buf_ptr(line)); } } } buf_end(&outbuf); return (buf_len(&outbuf)); }
bool load(Farhan::TemporaryFile &tf) { try { vertex_info::size_type length; length = tf.readUInt32 (); vid = tf.readUInt32(); #ifdef VERBOSE std::cerr << ">> sizeof (length) = " << sizeof (length) << " sizeof (vid) = " << sizeof (vid) << '\n'; std::cerr << "Vertex length = " << length << " id = " << vid << '\n'; #endif length -= (sizeof(length) + sizeof (vid)); byte * buf; tf.readBytes(length,&buf); boost::scoped_array<byte> buf_ptr (buf); vi = vertex_info::make_from_buffer (&buf, (ptrdiff_t)length); return true; } catch(...) { return false; } }
int __cdecl main( int argc, char *argv[] ) { lex ilex; plexitem pil; buf in; str fn; arr out; uint i; uint fout; gentee_init(); printf("Start\n"); str_init( &fn ); str_copyzero( &fn, "gttbl.dat"); fout = os_fileopen( &fn, FOP_CREATE ); printf("Fout=%i %s\n", fout, str_ptr( &fn )); os_filewrite( fout, ( pubyte )&tbl_gt, 97 * sizeof( uint )); os_fileclose( ( pvoid )fout ); str_copyzero( &fn, "gtdotbl.dat"); fout = os_fileopen( &fn, FOP_CREATE ); printf("Fout=%i %s\n", fout, str_ptr( &fn )); str_delete( &fn ); os_filewrite( fout, ( pubyte )&tbl_gtdo, 81 * sizeof( uint )); os_fileclose( ( pvoid )fout ); arr_init( &out, sizeof( lexitem )); buf_init( &in ); buf_copyzero( &in, "</r/&xfa; &xfa; #ap/dfield( 'qwer ()ty' , \"my , name\" , qqq)#asdf.fgwsw/# se# &xaa;" "<2 qqq> </2><1 a2345=&xf0;> 223&</1><-qwe-rty->" "<mygt /asd = \"qwerty sese'\" qq21 = 'dedxd' 'esese;' aqaq=325623/>" "<a asdff /a> <mygtdd><a /><-ooops-><ad />< qq</>" "xxx </r/nm <_aa aqaqa /_aaaa /_a/_aa><a22222/ >" "<*abc ></abc><|*aaa = qqqq></aaa>ooops aaa</eee>\"\r\n</>"); // buf_copyzero( &in, "<mygt > <aaa asdff>qqqq</> </mygtdd>qq </> xxx </r/nm <_aa aqaqa /_aaaa /_aa> <a22222/ /> </ > "); printf("lex_init\n"); lex_init( &ilex, (puint)&tbl_gtdo ); printf("gentee_lex\n"); gentee_lex( &in, &ilex, &out ); if (arr_count(&ilex.state)) printf("================= State=%x/%i \n", arr_getuint( &ilex.state, arr_count(&ilex.state) - 1 ), arr_count(&ilex.state)); for ( i = 0; i < arr_count( &out ); i++ ) { pil = ( plexitem )arr_ptr( &out, i ); printf("ID=%x pos=%i len=%i \n", pil->type, pil->pos, pil->len, buf_ptr( &in ) + pil->pos ); } // gentee_compile(); lex_delete( &ilex ); buf_delete( &in ); arr_delete( &out ); gentee_deinit(); printf("OK\n"); getch(); return 0; }
int buf_cmp(buf_t buf, void *ptr, int len) { if (buf_len(buf) < len) return (-1); return (memcmp(buf_ptr(buf), ptr, len)); }
bool os_path_is_absolute(Buf *path) { #if defined(ZIG_OS_WINDOWS) #error "missing os_path_is_absolute implementation" #elif defined(ZIG_OS_POSIX) return buf_ptr(path)[0] == '/'; #else #error "missing os_path_is_absolute implementation" #endif }
int buf_put(buf_t buf, void *src, int len) { if (buf->offset + len > buf->size) { return (-1); } memcpy(buf_ptr(buf), src, len); buf->offset += len; return (len); }
int os_path_real(Buf *rel_path, Buf *out_abs_path) { #if defined(ZIG_OS_WINDOWS) buf_resize(out_abs_path, 4096); if (_fullpath(buf_ptr(out_abs_path), buf_ptr(rel_path), buf_len(out_abs_path)) == nullptr) { zig_panic("_fullpath failed"); } buf_resize(out_abs_path, strlen(buf_ptr(out_abs_path))); return ErrorNone; #elif defined(ZIG_OS_POSIX) buf_resize(out_abs_path, PATH_MAX + 1); char *result = realpath(buf_ptr(rel_path), buf_ptr(out_abs_path)); if (!result) { int err = errno; if (err == EACCES) { return ErrorAccess; } else if (err == ENOENT) { return ErrorFileNotFound; } else if (err == ENOMEM) { return ErrorNoMem; } else { return ErrorFileSystem; } } buf_resize(out_abs_path, strlen(buf_ptr(out_abs_path))); return ErrorNone; #else #error "missing os_path_real implementation" #endif }
int buf_isascii(buf_t buf) { u_char *p, *q; p = buf_ptr(buf); q = buf->base + buf->end; for (; p < q; p++) if (!isascii(*p)) return (0); return (1); }
void STDCALL gesave_finish( pvmEngine pThis ) { buf bt; pbuf pb = pThis->gesave; uint size = buf_len( pThis, pb ) - pThis->gesaveoff; if ( size <= 187 ) *( pubyte )(( pubyte )buf_ptr( pThis, pThis->gesave ) + pThis->gesaveoff + 5 ) = ( ubyte )size; else { buf_init( pThis, &bt ); pThis->gesave = &bt; if ( size < 16800 ) { size++; gesave_bwd( pThis, size ); } else if ( size < 0xFFF0 ) { gesave_addubyte( pThis, 0xFE ); size += 2; gesave_addushort( pThis, size ); } else { gesave_addubyte( pThis, 0xFF ); size += 4; gesave_adduint( pThis, size ); } // Write the size // We have already had one byte, so -1 buf_insert( pThis, pb, pThis->gesaveoff + 5, ( pubyte )&size /*any*/, buf_len( pThis, pThis->gesave ) - 1 ); mem_copy( pThis, buf_ptr( pThis, pb ) + pThis->gesaveoff + 5, buf_ptr( pThis, pThis->gesave ), buf_len( pThis, pThis->gesave )); buf_delete( pThis, &bt ); pThis->gesave = pb; } }
void STDCALL gesave_finish( void ) { buf bt; pbuf pb = gesave; uint size = buf_len( pb ) - gesaveoff; if ( size <= 187 ) *( pubyte )(( pubyte )buf_ptr( gesave ) + gesaveoff + 5 ) = ( ubyte )size; else { buf_init( &bt ); gesave = &bt; if ( size < 16800 ) { size++; gesave_bwdi( size ); } else if ( size < 0xFFF0 ) { gesave_addubyte( 0xFE ); size += 2; gesave_addushort( size ); } else { gesave_addubyte( 0xFF ); size += 4; gesave_adduint( size ); } // Write the size // We have already had one byte, so -1 buf_insert( pb, gesaveoff + 5, ( pubyte )&size /*any*/, buf_len( gesave ) - 1 ); mem_copy( buf_ptr( pb ) + gesaveoff + 5, buf_ptr( gesave ), buf_len( gesave )); buf_delete( &bt ); gesave = pb; } }
int os_get_cwd(Buf *out_cwd) { #if defined(ZIG_OS_WINDOWS) buf_resize(out_cwd, 4096); if (GetCurrentDirectory(buf_len(out_cwd), buf_ptr(out_cwd)) == 0) { zig_panic("GetCurrentDirectory failed"); } return 0; #elif defined(ZIG_OS_POSIX) int err = ERANGE; buf_resize(out_cwd, 512); while (err == ERANGE) { buf_resize(out_cwd, buf_len(out_cwd) * 2); err = getcwd(buf_ptr(out_cwd), buf_len(out_cwd)) ? 0 : errno; } if (err) zig_panic("unable to get cwd: %s", strerror(err)); return 0; #else #error "missing os_get_cwd implementation" #endif }
int decode_ftp(u_char *buf, int len, u_char *obuf, int olen) { struct buf *line, inbuf, outbuf; int i, n; if ((len = strip_telopts(buf, len)) == 0) return (0); buf_init(&inbuf, buf, len); buf_init(&outbuf, obuf, olen); if (!buf_isascii(&inbuf)) return (0); n = 0; while ((i = buf_index(&inbuf, "\n", 1)) != -1) { line = buf_tok(&inbuf, NULL, i); buf_skip(&inbuf, 1); if (i > 0 && line->base[i - 1] == '\r') line->end--; line->base[line->end] = '\0'; if (strncasecmp(buf_ptr(line), "USER ", 5) == 0 || strncasecmp(buf_ptr(line), "ACCT ", 5) == 0 || strncasecmp(buf_ptr(line), "PASS ", 5) == 0) { buf_putf(&outbuf, "%s\n", buf_ptr(line)); n++; } } if (n < 2) return (0); buf_end(&outbuf); return (buf_len(&outbuf)); }
int buf_rindex(buf_t buf, void *ptr, int len) { u_char *p, *q; p = buf->base + buf->end - len; q = buf_ptr(buf); for (; p > q; p--) { if (memcmp(p, ptr, len) == 0) return (p - q); } return (-1); }
int buf_putf(buf_t buf, const char *fmt, ...) { va_list ap; int i; va_start(ap, fmt); i = vsnprintf(buf_ptr(buf), buf_len(buf), fmt, ap); va_end(ap); buf_skip(buf, i); return (i); }
static void print_err_msg_type(ErrorMsg *err, ErrColor color, ErrType err_type) { const char *path = buf_ptr(err->path); size_t line = err->line_start + 1; size_t col = err->column_start + 1; const char *text = buf_ptr(err->msg); if (color == ErrColorOn || (color == ErrColorAuto && os_stderr_tty())) { if (err_type == ErrTypeError) { fprintf(stderr, WHITE "%s:%zu:%zu: " RED "error:" WHITE " %s" RESET "\n", path, line, col, text); } else if (err_type == ErrTypeNote) { fprintf(stderr, WHITE "%s:%zu:%zu: " CYAN "note:" WHITE " %s" RESET "\n", path, line, col, text); } else { zig_unreachable(); } fprintf(stderr, "%s\n", buf_ptr(&err->line_buf)); for (size_t i = 0; i < err->column_start; i += 1) { fprintf(stderr, " "); } fprintf(stderr, GREEN "^" RESET "\n"); } else { if (err_type == ErrTypeError) { fprintf(stderr, "%s:%zu:%zu: error: %s\n", path, line, col, text); } else if (err_type == ErrTypeNote) { fprintf(stderr, " %s:%zu:%zu: note: %s\n", path, line, col, text); } else { zig_unreachable(); } } for (size_t i = 0; i < err->notes.length; i += 1) { ErrorMsg *note = err->notes.at(i); print_err_msg_type(note, color, ErrTypeNote); } }
pubyte STDCALL collect_index( pcollect pclt, uint index ) { uint i; uint off = 0; if ( index >= pclt->count ) return 0; if ( pclt->flag & 0x01 ) { for( i = 0; i < index; i++ ) { // print("Type=%i\n", *( puint )( // buf_ptr( ( pbuf )pclt ) + pclt->pfor ) ); off += sizeof( uint ) + ((( povmtype )PCMD( *( puint )( buf_ptr( ( pbuf )pclt ) + off ) ))->stsize << 2 ); } } else off = ( index << 3 ); // print("Index = %i t=%i pfor=%i off = %i\n", index, // *( puint )buf_ptr( ( pbuf )pclt ), pclt->pfor, pclt->pfor + sizeof( uint ) ); return buf_ptr( ( pbuf )pclt ) + off + sizeof( uint ); }
static int process_pop_server(struct pop_info *pop, char *data, int len) { struct buf *line, *body, buf; int i; buf_init(&buf, data, len); if (pop->state == POP_NONE) return (len); if (pop->state == POP_RETR) { if ((i = buf_index(&buf, "\r\n", 2)) < 0) return (0); line = buf_tok(&buf, NULL, i + 2); if (buf_cmp(line, "+OK", 3) == 0) { pop->state = POP_DATA; } else pop->state = POP_NONE; } if (pop->state == POP_DATA) { if ((i = buf_index(&buf, "\r\n.\r\n", 5)) >= 0) { body = buf_tok(&buf, NULL, i); buf_skip(&buf, 5); body->base[body->end] = '\0'; if (regex_match(buf_ptr(body))) print_mbox_msg(NULL, buf_ptr(body)); pop->state = POP_NONE; } } return (len - buf_len(&buf)); }