LDPUBLIC32 dsk_err_t LDPUBLIC16 dsk_close(DSK_DRIVER **self) { dsk_err_t e, e2; COMPRESS_DATA *dc; DSK_OPTION *opt, *opt2; if (!self || (!(*self)) || (!(*self)->dr_class)) return DSK_ERR_BADPTR; e = ((*self)->dr_class->dc_close)(*self); dc = (*self)->dr_compress; if (dc) { if ((*self)->dr_dirty) e2 = comp_commit(&dc); else e2 = comp_abort (&dc); if (!e) e = e2; } /* Free any option blocks the driver has accumulated */ opt = (*self)->dr_options; while (opt) { opt2 = opt->do_next; dsk_free(opt); opt = opt2; } dsk_free (*self); *self = NULL; return e; }
void random_slice(DskBuffer* buf) { DskBuffer tmpbuf; char *copy, *copy_at; unsigned orig_size = buf->size; dsk_buffer_init (&tmpbuf); copy = dsk_malloc (buf->size); copy_at = copy; while (1) { int r; r = (rand () % 16384) + 1; r = dsk_buffer_read (buf, r, copy_at); dsk_buffer_append (&tmpbuf, r, copy_at); if (r == 0) break; } dsk_assert (copy_at == copy + orig_size); dsk_assert (buf->size == 0); dsk_assert (tmpbuf.size == orig_size); copy_at = dsk_malloc (orig_size); dsk_assert (dsk_buffer_read (&tmpbuf, orig_size, copy_at) == orig_size); dsk_assert (dsk_buffer_read (&tmpbuf, orig_size, copy_at) == 0); dsk_assert (memcmp (copy, copy_at, orig_size) == 0); dsk_free (copy); dsk_free (copy_at); }
dsk_err_t remote_close(DSK_DRIVER *self) { dsk_err_t err; RPCFUNC function; if (self == NULL || self->dr_remote == NULL) return DSK_ERR_BADPTR; function = self->dr_remote->rd_class->rc_call; /* Update the comment (if any) */ if (implements(self, RPC_DSK_SETCOMMENT)) { char *cmt; err = dsk_get_comment(self, &cmt); if (!err) dsk_r_set_comment(self, function, self->dr_remote->rd_handle, cmt); } /* Close the file */ dsk_r_close(self, function, self->dr_remote->rd_handle); /* Close the connection */ err = (*self->dr_remote->rd_class->rc_close)(self); if (self->dr_remote->rd_functions) dsk_free(self->dr_remote->rd_functions); if (self->dr_remote->rd_name) dsk_free(self->dr_remote->rd_name); dsk_free(self->dr_remote); return err; }
dsk_err_t dsk_r_properties(DSK_PDRIVER self, RPCFUNC func, unsigned int nDriver) { unsigned char ibuf[SMALLBUF], *iptr = ibuf; unsigned char obuf[400], *optr = obuf; dsk_err_t err; int ilen = sizeof ibuf; int olen = sizeof obuf; dsk_err_t err2; int16 fncount, fnid; int n; char *desc; if (!self || !self->dr_remote) return DSK_ERR_BADPTR; err = dsk_pack_i16 (&iptr, &ilen, RPC_DSK_PROPERTIES); if (err) return err; err = dsk_pack_i32 (&iptr, &ilen, nDriver); if (err) return err; err = (*func)(self, ibuf, iptr - ibuf, obuf, &olen); if (err) return err; err = dsk_unpack_err (&optr, &olen, &err2); if (err) return err; if (err2 == DSK_ERR_UNKRPC) return err2; err = dsk_unpack_i16 (&optr, &olen, &fncount); if (err) return err; if (self->dr_remote->rd_functions != NULL) { dsk_free(self->dr_remote->rd_functions); self->dr_remote->rd_functions = NULL; } if (fncount) { self->dr_remote->rd_functions = dsk_malloc((fncount+1) * sizeof(unsigned)); if (!self->dr_remote->rd_functions) { err2 = DSK_ERR_NOMEM; } else { for (n = 0; n < fncount; n++) { err = dsk_unpack_i16 (&optr, &olen, &fnid); if (err) return err; self->dr_remote->rd_functions[n] = fnid; } self->dr_remote->rd_functions[n] = 0; } } err = dsk_unpack_string(&optr, &olen, &desc); if (err) return err; if (self->dr_remote->rd_name) dsk_free(self->dr_remote->rd_name); self->dr_remote->rd_name = NULL; if (desc) { self->dr_remote->rd_name = dsk_malloc(1 + strlen(desc)); if (!self->dr_remote->rd_name) err2 = DSK_ERR_NOMEM; else strcpy(self->dr_remote->rd_name, desc); } return err2; }
dsk_err_t comp_mktemp(COMPRESS_DATA *self, FILE **fp) { char *tdir; int fd; char tmpdir[PATH_MAX]; self->cd_ufilename = dsk_malloc(PATH_MAX); /* Win32: Create temp file using GetTempFileName() */ #ifdef HAVE_GETTEMPFILENAME GetTempPath(PATH_MAX, tmpdir); if (!GetTempFileName(tmpdir, "dsk", 0, self->cd_ufilename)) { dsk_free(self->cd_ufilename); self->cd_ufilename = NULL; return DSK_ERR_SYSERR; } *fp = fopen(self->cd_ufilename, "wb"); (void)fd; (void)tdir; #else /* def HAVE_GETTEMPFILENAME */ /* Modern Unixes: Use mkstemp() */ #ifdef HAVE_MKSTEMP tdir = getenv("TMPDIR"); if (tdir) sprintf(tmpdir, "%s/libdskdXXXXXXXX", tdir); else sprintf(tmpdir, TMPDIR "/libdskXXXXXXXX"); fd = mkstemp(tmpdir); *fp = NULL; if (fd != -1) fp[0] = fdopen(fd, "wb"); strcpy(self->cd_ufilename, tmpdir); #else /* def HAVE_MKSTEMP */ /* Old Unixes, old xenix clones such as DOS */ tdir = getenv("TMP"); if (!tdir) tdir = getenv("TEMP"); if (tdir) sprintf(tmpdir, "%s/LDXXXXXX", tdir); else sprintf(tmpdir, "./LDXXXXXX"); strcpy(self->cd_ufilename, mktemp(tmpdir)); fp[0] = fopen(self->cd_ufilename, "wb"); (void)fd; #endif /* HAVE_MKSTEMP */ #endif /* HAVE_GETTEMPFILENAME */ if (!*fp) { dsk_free(self->cd_ufilename); self->cd_ufilename = NULL; return DSK_ERR_SYSERR; } return DSK_ERR_OK; }
dsk_err_t win16_format(DSK_DRIVER *self, DSK_GEOMETRY *geom, dsk_pcyl_t cylinder, dsk_phead_t head, const DSK_FORMAT *format, unsigned char filler) { WIN16_DSK_DRIVER *w16self; union REGS reg; struct SREGS sreg; dsk_err_t err; unsigned char *trkbuf; unsigned int n, tries; unsigned char old_dpt[11]; if (!format || !self || !geom || self->dr_class != &dc_win16) return DSK_ERR_BADPTR; w16self = (WIN16_DSK_DRIVER *)self; if (w16self->w16_readonly) return DSK_ERR_RDONLY; trkbuf = dsk_malloc(4 * geom->dg_sectors); if (!trkbuf) return DSK_ERR_NOMEM; err = win16_checkgeom(w16self, geom, old_dpt, filler); if (err) { dsk_free(trkbuf); return err; } for (n = 0; n < geom->dg_sectors; n++) { trkbuf[4*n ] = (unsigned char)format[n].fmt_cylinder; trkbuf[4*n+1] = (unsigned char)format[n].fmt_head; trkbuf[4*n+2] = (unsigned char)format[n].fmt_sector; trkbuf[4*n+3] = dsk_get_psh(format[n].fmt_secsize); } for (tries = 0; tries < 3; tries++) { reg.h.ah = 0x05; reg.h.al = geom->dg_sectors; reg.x.bx = FP_OFF(trkbuf); reg.h.ch = cylinder; reg.h.cl = 0; reg.h.dh = head; reg.h.dl = w16self->w16_unit - 1; reg.x.cflag = 1; int86x(0x13, ®, ®, &sreg); if (!reg.x.cflag) break; } dsk_free(trkbuf); set_dpt(old_dpt); if ( reg.x.cflag ) return translate_int13_error(reg.h.ah); return DSK_ERR_OK; }
LDPUBLIC32 dsk_err_t LDPUBLIC16 dsk_xcheck(DSK_DRIVER *self, const DSK_GEOMETRY *geom, const void *buf, dsk_pcyl_t cylinder, dsk_phead_t head, dsk_pcyl_t cyl_expect, dsk_phead_t head_expect, dsk_psect_t sector, size_t sector_len) { DRV_CLASS *dc; void *buf2; dsk_err_t e = DSK_ERR_UNKNOWN; unsigned n; if (!self || !geom || !buf || !self->dr_class) return DSK_ERR_BADPTR; dc = self->dr_class; WALK_VTABLE(dc, dc_xread) if (!dc->dc_xread) return DSK_ERR_NOTIMPL; buf2 = dsk_malloc(geom->dg_secsize); if (!buf2) return DSK_ERR_NOMEM; for (n = 0; n < self->dr_retry_count; n++) { e = (dc->dc_xread)(self,geom,buf2,cylinder,head, cyl_expect, head_expect, sector, sector_len, 0); if (!DSK_TRANSIENT_ERROR(e)) break; } if (e == DSK_ERR_OK && memcmp(buf, buf2, geom->dg_secsize)) { e = DSK_ERR_MISMATCH; } dsk_free(buf2); return e; }
dsk_err_t win16_secid(DSK_DRIVER *self, const DSK_GEOMETRY *geom, dsk_pcyl_t cylinder, dsk_phead_t head, DSK_FORMAT *result) { /* We don't implement a proper READ_SECTOR_ID, because Windows * doesn't expose this functionality. Instead, emulate just enough * to autodetect CPC-format discs (slowly) */ dsk_err_t err; unsigned char *buf = dsk_malloc(geom->dg_secsize); unsigned char psect; if (geom->dg_fm) return DSK_ERR_NOADDR; if (!buf) return DSK_ERR_NOMEM; err = win16_read(self, geom, buf, cylinder, head, psect=0x01); if (err) err = win16_read(self, geom, buf, cylinder, head, psect=0x41); if (err) err = win16_read(self, geom, buf, cylinder, head, psect=0xC1); if (result) { result->fmt_cylinder = cylinder; result->fmt_head = head; result->fmt_sector = psect; result->fmt_secsize = geom->dg_secsize; } dsk_free(buf); return err; }
static void handle_fd (DskPattern *pattern, const char *prefix_filename, unsigned buffer_size, int fd) { DskBuffer in = DSK_BUFFER_INIT; DskBuffer out = DSK_BUFFER_INIT; for (;;) { int rv = dsk_buffer_readv (&in, fd); if (rv == 0) break; char *line; while ((line = dsk_buffer_read_line (&in)) != NULL) { if (dsk_pattern_match (pattern, line)) { if (prefix_filename) { dsk_buffer_append_string (&out, prefix_filename); dsk_buffer_append_string (&out, ": "); } dsk_buffer_append_string (&out, line); dsk_buffer_append_byte (&out, '\n'); while (out.size > buffer_size) dsk_buffer_writev (&out, STDOUT_FILENO); } dsk_free (line); } } while (out.size > 0) dsk_buffer_writev (&out, STDOUT_FILENO); }
/* Attempt to open a DSK file with driver <ndrv> */ static dsk_err_t dsk_iopen(DSK_DRIVER **self, const char *filename, int ndrv, COMPRESS_DATA *cd) { DRV_CLASS *dc = classes[ndrv]; dsk_err_t err; /* If we're handling compressed data, use the temporary uncompressed file */ if (cd) filename = cd->cd_ufilename; if (!dc) return DSK_ERR_BADPTR; (*self) = dsk_malloc(dc->dc_selfsize); if (!*self) return DSK_ERR_NOMEM; dr_construct(*self, dc); err = (dc->dc_open)(*self, filename); /* printf("%s: open %s = %d\n", dc->dc_drvname, filename, err); */ if (err == DSK_ERR_OK) { (*self)->dr_compress = cd; return err; } dsk_free (*self); *self = NULL; return err; }
/* See if a file can be decompressed */ static dsk_err_t comp_iopen(COMPRESS_DATA **cd, const char *filename, int nc) { COMPRESS_CLASS *cc = classes[nc]; dsk_err_t err; if (!cc) return DSK_ERR_BADPTR; (*cd) = dsk_malloc(cc->cc_selfsize); if (!*cd) return DSK_ERR_NOMEM; memset((*cd), 0, cc->cc_selfsize); err = comp_construct(*cd, filename); (*cd)->cd_class = cc; if (err == DSK_ERR_OK) { char *s = dsk_malloc(strlen(cc->cc_description) + 50); if (s) { sprintf(s, "Checking compression: %s...", cc->cc_description); dsk_report(s); dsk_free(s); } else dsk_report("Checking compression..."); err = (cc->cc_open)(*cd); dsk_report_end(); } if (err == DSK_ERR_OK) return err; comp_free (*cd); *cd = NULL; return err; }
static void handle_dns_result (DskDnsLookupResult *result, void *callback_data) { const char *name = callback_data; char *str; switch (result->type) { case DSK_DNS_LOOKUP_RESULT_FOUND: str = dsk_ip_address_to_string (result->addr); printf ("%s: %s\n", name, str); dsk_free (str); break; case DSK_DNS_LOOKUP_RESULT_NOT_FOUND: printf ("%s: not found\n", name); break; case DSK_DNS_LOOKUP_RESULT_TIMEOUT: printf ("%s: timeout\n", name); break; case DSK_DNS_LOOKUP_RESULT_BAD_RESPONSE: printf ("%s: bad response: %s\n", name, result->message); break; default: dsk_assert_not_reached (); } if (result->type != DSK_DNS_LOOKUP_RESULT_FOUND) { exit_status = 1; if (fatal_errors) exit (1); } --n_running; }
static void dsk_ssl_context_finalize (DskSslContext *context) { if (context->ctx) SSL_CTX_free (context->ctx); dsk_free (context->password); }
/** * dsk_checksum_destroy: * @checksum: the checksum function. * * Free memory used by the checksum object. */ void dsk_checksum_destroy (DskChecksum *checksum) { /* This assert verifies that the checksum was created with dsk_checksum_new() */ dsk_assert (checksum->flags & 2); dsk_free (checksum); }
static void table_checkpoint_trivial__destroy(DskTableCheckpoint *checkpoint) { TrivialTableCheckpoint *cp = (TrivialTableCheckpoint *) checkpoint; if (munmap ((void*) cp->mmapped, cp->mmapped_size) < 0) dsk_warning ("error calling munmap(): %s", strerror (errno)); close (cp->fd); dsk_free (cp); }
STDMETHODIMP CLibrary::create(BSTR f, BSTR d, BSTR c, IDisk **ppDisk) { DSK_PDRIVER pd; dsk_err_t err; char *filename; char *driver; char *compress; if (!f || !ppDisk) { return MapError(DSK_ERR_BADPTR); } filename = get_string(f); driver = get_string(d); compress = get_string(c); if (driver[0] == 0) { dsk_free(driver); driver = NULL; } if (compress[0] == 0) { dsk_free(compress); compress = NULL; } err = dsk_creat(&pd, filename, driver, compress); if (filename) dsk_free(filename); if (driver) dsk_free(driver); if (compress) dsk_free(compress); if (err) { return MapError(err); } HRESULT hr = CDisk::CreateInstance(ppDisk); if (FAILED(hr)) return hr; ((CDisk *)ppDisk[0])->m_driver = pd; return hr; }
STDMETHODIMP CDisk::put_option(BSTR name, long newVal) { char *buf; dsk_err_t err; buf = get_string(name); if (!buf) return MapError(DSK_ERR_NOMEM); err = dsk_set_option(m_driver, buf, newVal); dsk_free(buf); return MapError(err); }
LDPUBLIC32 dsk_err_t LDPUBLIC16 dsk_set_comment(DSK_PDRIVER self, const char *comment) { if (!self || !comment) return DSK_ERR_BADPTR; if (self->dr_comment) dsk_free(self->dr_comment); self->dr_comment = dsk_malloc(1 + strlen(comment)); if (!self->dr_comment) return DSK_ERR_NOMEM; strcpy(self->dr_comment, comment); return DSK_ERR_OK; }
STDMETHODIMP CDisk::put_comment(BSTR newVal) { char *buf; dsk_err_t err; buf = get_string(newVal); if (!buf) return MapError(DSK_ERR_NOMEM); err = dsk_set_comment(m_driver, buf); dsk_free(buf); return MapError(err); }
dsk_err_t tele_close(DSK_DRIVER *s) { DC_CHECK(s); if (fclose(self->tele_fp)) return DSK_ERR_SYSERR; if (self->tele_comment) { dsk_free(self->tele_comment); self->tele_comment = NULL; } return DSK_ERR_OK; }
/* Return whether 'buffer' begins with 'str'; remove it if so. */ static dsk_boolean try_initial_remove (DskBuffer *buffer, const char *str) { unsigned len = strlen (str); if (buffer->size < len) return DSK_FALSE; char *tmp = dsk_malloc (len); dsk_buffer_read (buffer, len, tmp); dsk_boolean rv = memcmp (str, tmp, len) == 0; dsk_free (tmp); return rv; }
void dsk_json_parser_destroy (DskJsonParser *parser) { unsigned i, j; DskJsonValue *v; while ((v = dsk_json_parser_pop (parser)) != NULL) dsk_json_value_free (v); for (i = 0; i < parser->stack_size; i++) { switch (parser->stack[i].type) { case STACK_NODE_OBJECT: for (j = 0; j < parser->stack[i].n_subs; j++) { dsk_free (parser->stack[i].u.members[j].name); if (parser->stack[i].u.members[j].value) dsk_json_value_free (parser->stack[i].u.members[j].value); } break; case STACK_NODE_ARRAY: for (j = 0; j < parser->stack[i].n_subs; j++) dsk_json_value_free (parser->stack[i].u.values[j]); } dsk_free (parser->stack[i].u.members); } for ( ; i < parser->stack_alloced; i++) dsk_free (parser->stack[i].u.members); dsk_free (parser->stack); dsk_free (parser->str); dsk_free (parser); }
STDMETHODIMP CDisk::lcheck(IGeometry *g, VARIANT *buf, long lsect) { HRESULT hr; DSK_GEOMETRY geom; unsigned char *pData; g_to_dg(g, &geom); hr = GetBuffer(buf, &pData, geom.dg_secsize); if (FAILED(hr)) return hr; hr = MapError(dsk_lcheck(m_driver, &geom, pData, lsect)); dsk_free(pData); return hr; }
void decount(DskBuffer* buf, int start, int end) { char b[1024]; while (start <= end) { char *rv; sprintf (b, "%d", start++); rv = dsk_buffer_read_line (buf); dsk_assert (rv != NULL); dsk_assert (strcmp (b, rv) == 0); dsk_free (rv); } }
STDMETHODIMP CDisk::pwrite(IGeometry *g, VARIANT *buf, long cylinder, short head, short sector) { HRESULT hr; DSK_GEOMETRY geom; unsigned char *pData; g_to_dg(g, &geom); hr = GetBuffer(buf, &pData, geom.dg_secsize); if (FAILED(hr)) return hr; hr = MapError(dsk_pwrite(m_driver, &geom, pData, cylinder, head, sector)); dsk_free(pData); return hr; }
STDMETHODIMP CLibrary::pcwgeom(VARIANT *buffer, IGeometry **geom) { unsigned char *header; DSK_GEOMETRY dg; HRESULT hr = GetBuffer(buffer, &header, 512); if (FAILED(hr)) return hr; hr = MapError(dg_pcwgeom(&dg, header)); dsk_free(header); if (!FAILED(hr)) hr = CGeometry::CreateInstance(geom); if (!FAILED(hr)) dg_to_g(&dg, *geom); return hr; }
STDMETHODIMP CDisk::get_option(BSTR name, long *pVal) { char *buf; dsk_err_t err; int value; buf = get_string(name); if (!buf) return MapError(DSK_ERR_NOMEM); err = dsk_get_option(m_driver, buf, &value); dsk_free(buf); *pVal = value; return MapError(err); }
STDMETHODIMP CDisk::xcheck(IGeometry *g, VARIANT *buf, long cylinder, short head, long cyl_expected, short head_expected, /*[in]*/ short sector,long sector_len) { HRESULT hr; DSK_GEOMETRY geom; unsigned char *pData; g_to_dg(g, &geom); hr = GetBuffer(buf, &pData, sector_len); if (FAILED(hr)) return hr; hr = MapError(dsk_xcheck(m_driver, &geom, pData, cylinder, head, cyl_expected, head_expected, sector, sector_len)); dsk_free(pData); return hr; }
static void handle_get_games_list (DskHttpServerRequest *request) { unsigned n_games = 0; Game *game; DskJsonValue **game_info, **at; for (game = all_games; game; game = game->next_game) n_games++; game_info = dsk_malloc (sizeof (DskJsonValue *) * n_games); at = game_info; for (game = all_games; game; game = game->next_game, at++) { Object *object; unsigned n_players = 0; DskJsonValue **pat; DskJsonMember members[2] = { { "name", dsk_json_value_new_string (strlen (game->name), game->name) }, { "players", NULL }, }; DskJsonValue **players; for (object = game->objects[OBJECT_TYPE_USER]; object != NULL; object = object->next_in_game) n_players++; players = dsk_malloc (sizeof (DskJsonValue *) * n_players); pat = players; for (object = game->objects[OBJECT_TYPE_USER]; object != NULL; object = object->next_in_game) { User *player = (User *)object; *pat++ = dsk_json_value_new_string (strlen (player->name), player->name); } members[1].value = dsk_json_value_new_array (n_players, players); dsk_free (players); *at++ = dsk_json_value_new_object (DSK_N_ELEMENTS (members), members); } respond_take_json (request, dsk_json_value_new_array (n_games, game_info)); dsk_free (game_info); }
DskJsonValue * dsk_json_parser_pop (DskJsonParser *parser) { DskJsonValue *rv; ValueQueue *q; if (parser->queue_head == NULL) return NULL; rv = parser->queue_head->value; q = parser->queue_head; parser->queue_head = q->next; if (parser->queue_head == NULL) parser->queue_tail = NULL; dsk_free (q); return rv; }