queue_t queue_new(int size) { queue_t q = safe_alloc(sizeof(queue)); q->ptrs = safe_alloc(sizeof(void *) * size); q->size = size; queue_init(q); return q; }
int gettitleandpath(const char *filename, char **filepath, char **filetitle) { char *cwd; char *absname; int freeabsname; char *firstslash; freeabsname = 0; /* First get the the absolute file title. */ if(filename[0] != '/') { /* Not an absolute filename */ cwd = get_current_dir_name(); if(!cwd) { report_error("Unable to get cwd"); return -1; } /* Put the cwd at the beginning */ absname = safe_alloc(strlen(cwd) + strlen(filename) + 1); strcpy((char*)absname, cwd); strcat((char*)absname, "/"); strcat((char*)absname, filename); freeabsname = 1; free(cwd); } else absname = (char*)filename; /* Have the absolute name now extract the filetitle */ /* Find the first slash and then copy everything after this into the file title. */ firstslash = rindex(absname, '/'); if(!firstslash) { report_error("Unable to find / in the filename"); if(freeabsname) free((void*)absname); return -1; } (*filetitle) = safe_alloc(strlen(firstslash + 1)); if(!(*filetitle)) { if(freeabsname) free((void*)absname); return -1; } strcpy((char*)(*filetitle), (firstslash + 1)); /* Now copy in the path */ (*filepath) = safe_alloc((firstslash - absname) + 1); if(!(*filepath)) { free((void*)absname); free((void*)(*filetitle)); } strncpy((char*)*filepath, absname, (firstslash - absname)); /* Will not be null terminated */ (*filepath)[firstslash - absname] = 0; return 0; }
void UpdateFLIDs(UpdateList &update_list) { for (int i = 0; i < update_list.getCount(); ++i) { if(update_list[i].file_id == -1 && update_list[i].update.szUpdateURL && strcmp(update_list[i].update.szUpdateURL, UPDATER_AUTOREGISTER) == 0) { int file_id = FindFileID(update_list[i].update.szComponentName, MC_PLUGINS, 0); if (file_id == -1) file_id = FindFileID(update_list[i].update.szComponentName, MC_LOCALIZATION, 0); if (file_id != -1) { update_list[i].file_id = file_id; char *buff = (char *)safe_alloc((int)strlen(MIM_DOWNLOAD_URL_PREFIX) + 9); sprintf(buff, MIM_DOWNLOAD_URL_PREFIX "%d", file_id); update_list[i].update.szUpdateURL = buff; update_list[i].shortName = safe_strdup(update_list[i].update.szComponentName); if(update_list[i].update.szBetaVersionURL) { update_list[i].update_options.fixed = false; LoadUpdateOptions(update_list[i].update.szComponentName, &update_list[i].update_options); } } } } }
int c_syb_make (int m_size) { int count; error_message = (char *) malloc (sizeof (char) * (m_size + ERROR_MESSAGE_SIZE)); clear_error (); max_size = m_size; if (dbinit () == FAIL) { return error_number; /* exit(ERREXIT); */ } dberrhandle ((EHANDLEFUNC)err_handler); dbmsghandle ((MHANDLEFUNC)msg_handler); if (login == NULL) { login = safe_alloc (dblogin()); DBSETLCHARSET (login, "utf8"); } for (count = 0; count < MAX_DESCRIPTOR; count++) { descriptor[count] = NULL; } return error_number; }
object* alloc_object(void) { object *obj = NULL; if (free_list == NULL) { gc(); obj = safe_alloc(); if (obj == NULL) { error("no memory for objects"); } } else { obj = safe_alloc(); } assert(obj != NULL); return obj; }
int dm_resume_and_reinstate_key(const char *name, size_t key_size, const char *key) { int msg_size = key_size * 2 + 10; // key set <key> char *msg; int r = 0; if (!_dm_check_versions()) return -ENOTSUP; if (!_dm_crypt_wipe_key_supported) return -ENOTSUP; msg = safe_alloc(msg_size); if (!msg) return -ENOMEM; memset(msg, 0, msg_size); strcpy(msg, "key set "); hex_key(&msg[8], key_size, key); if (!_dm_message(name, msg) || !_dm_simple(DM_DEVICE_RESUME, name, 1)) r = -EINVAL; safe_free(msg); return r; }
/* Initialize Roots nodes */ void initialize(node **root) { (*root) = safe_alloc(sizeof(node)); (*root) -> previous = NULL; (*root) -> next = NULL; (*root) -> from = new_pair(MARKER,MARKER); (*root) -> to = new_pair(MARKER,MARKER); }
struct snapshot * acquire_snapshot(kstat_ctl_t *kc, int types, struct iodev_filter *iodev_filter) { struct snapshot *ss = NULL; int err; retry: err = 0; /* ensure any partial resources are freed on a retry */ free_snapshot(ss); ss = safe_alloc(sizeof (struct snapshot)); (void) memset(ss, 0, sizeof (struct snapshot)); ss->s_types = types; /* wait for a possibly up-to-date chain */ while (kstat_chain_update(kc) == -1) { if (errno == EAGAIN) (void) poll(NULL, 0, RETRY_DELAY); else fail(1, "kstat_chain_update failed"); } if (!err && (types & SNAP_INTERRUPTS)) err = acquire_intrs(ss, kc); if (!err && (types & (SNAP_CPUS | SNAP_SYSTEM | SNAP_PSETS))) err = acquire_cpus(ss, kc); if (!err && (types & SNAP_PSETS)) err = acquire_psets(ss); if (!err && (types & (SNAP_IODEVS | SNAP_CONTROLLERS | SNAP_IOPATHS_LI | SNAP_IOPATHS_LTI))) err = acquire_iodevs(ss, kc, iodev_filter); if (!err && (types & SNAP_SYSTEM)) err = acquire_sys(ss, kc); switch (err) { case 0: break; case EAGAIN: (void) poll(NULL, 0, RETRY_DELAY); /* a kstat disappeared from under us */ /*FALLTHRU*/ case ENXIO: case ENOENT: goto retry; default: fail(1, "acquiring snapshot failed"); } return (ss); }
SMALL_EXPORT list *list_new(int size) { list *l = safe_alloc(sizeof(list)); l->head = l->tail = NULL; l->pool = pool_new(size * sizeof(list_item), sizeof(list_item)); l->size = size; list_init(l); return l; }
pair *new_pair(int left, int right) { pair *p; p = safe_alloc(sizeof(pair)); p->begin = left; p->end = right; return p; }
int syb_new_descriptor () { int result = syb_first_descriptor_available (); if (result != NO_MORE_DESCRIPTOR) { descriptor[result] = safe_alloc (dbopen (login, NULL)); } return result; }
static void build_mnt_list(FILE *mpt) { mnt_t *item; mnt_t **which; mnt_t *tmp; int found; struct extmnttab mnt; if (mpt) { while (nfs) { free(nfs->device_name); free(nfs->mount_point); free(nfs->devinfo); tmp = nfs; nfs = nfs->next; free(tmp); } while (ufs) { free(ufs->device_name); free(ufs->mount_point); free(ufs->devinfo); tmp = ufs; ufs = ufs->next; free(tmp); } (void) memset(&mnt, 0, sizeof (struct extmnttab)); resetmnttab(mpt); while ((found = getextmntent(mpt, &mnt, sizeof (struct extmnttab))) != -1) { if (found == 0) { if (strcmp(mnt.mnt_fstype, MNTTYPE_UFS) == 0) which = &ufs; else if (strcmp(mnt.mnt_fstype, MNTTYPE_NFS) == 0) which = &nfs; else which = 0; if (which) { item = safe_alloc(sizeof (mnt_t)); item->device_name = safe_strdup(mnt.mnt_special); item->mount_point = safe_strdup(mnt.mnt_mountp); item->devinfo = safe_strdup(mnt.mnt_mntopts); item->minor = mnt.mnt_minor; item->next = *which; *which = item; } } } } }
void register_raw() { proto_t *p = (proto_t *)safe_alloc(sizeof(proto_t)); p->name = PROTO_NAME_RAW; p->longname = "Raw Header"; p->layer = L5; p->decoder = decode_raw; p->dissect = NULL; proto_register_byname(PROTO_NAME_RAW, p); }
void register_tcp() { proto_t *p = (proto_t *)safe_alloc(sizeof(proto_t)); p->name = PROTO_NAME_TCP; p->longname = "Trasmission Control Protocol"; p->layer = L4; p->decoder = decode_tcp; p->dissect = l_dissect_tcp; proto_register_byname(PROTO_NAME_TCP, p); }
void register_udp() { proto_t *p = (proto_t *)safe_alloc(sizeof(proto_t)); p->name = PROTO_NAME_UDP; p->longname = "User Datagram Protocol"; p->layer = L4; p->decoder = decode_udp; p->dissect = l_dissect_udp; proto_register_byname(PROTO_NAME_UDP, p); }
node *create_node(pair *left, pair *right) { node *new_node; new_node = safe_alloc(sizeof(node)); new_node -> from = left; new_node -> to = right; new_node -> previous = NULL; new_node -> next = NULL; return new_node; }
void register_ftp() { proto_t *p = (proto_t *)safe_alloc(sizeof(proto_t)); p->name = PROTO_NAME_FTP; p->longname = "File Transfer Protocol"; p->layer = L5; p->decoder = decode_ftp; p->dissect = l_dissect_ftp; proto_register_byname(PROTO_NAME_FTP, p); proto_register_byport(21, p); }
static char *get_params(const char *device, uint64_t skip, uint64_t offset, const char *cipher, size_t key_size, const char *key) { char *params; char *hexkey; hexkey = safe_alloc(key_size * 2 + 1); if (!hexkey) return NULL; hex_key(hexkey, key_size, key); params = safe_alloc(strlen(hexkey) + strlen(cipher) + strlen(device) + 64); if (!params) goto out; sprintf(params, "%s %s %" PRIu64 " %s %" PRIu64, cipher, hexkey, skip, device, offset); out: safe_free(hexkey); return params; }
int move_file(const char *oldname, const char *newname) { int retval; char *buf; buf = safe_alloc(strlen(oldname) + strlen(newname) + strlen("mv ")); if(!buf) return -1; /* Use the UNIX command mv to do the work for us */ strcpy(buf, "mv "); strcat(buf, oldname); strcat(buf, " "); strcat(buf, newname); retval = system(buf); free(buf); if(retval != 0) { report_error("Move command failed"); } return retval; }
void* allocmem(size_t size) { void* result = safe_alloc(size); { long h; { long i; h=hash(result); for (i=0; i<maxptrs; i++) if (ptr[h]==NULL) break; else if (++h>=maxptrs) h=0; if (i==maxptrs) { /*error("Object table overflow (%ld). Try increasing 'maxobjects'.\n" ,chunks); */ /* we're just going to dynamically grow the object table */ newmem(3*maxptrs/2); /* increase maxptrs by 50% */ free(result); return allocmem(size); /* safe to call again now */ } } ignore_intr(); /* don't interrupt while updating |ptr| */ ptr[h]=result; ++chunks; allow_intr(); } return result; }
packet_t* packet_decode(buffer_t* buffer) { int error = buffer_ok; uint16_t length; uint8_t id; buffer->position = 0; error |= buffer_get_short(buffer, &length); error |= buffer_get_byte(buffer, &id); error |= buffer_available(buffer) == length; if (error || length > 5000 || length == 0) return NULL; uint8_t payload[length]; memcpy(payload, buffer->payload + buffer->position, length); packet_t* packet = safe_alloc(sizeof(packet_t)); packet->id = id; packet->buffer = buffer_wrap(payload, length); packet->buffer->position = 0; return packet; }
static void *new_watcher_data(void) { watcher_data *wdata = (watcher_data *)safe_alloc(sizeof(watcher_data)); return wdata; }
INT_PTR Register(WPARAM wParam, LPARAM lParam) { Update *update = (Update *)lParam; UpdateInternal update_internal = {0}; // remove registered plugin if already there EnterCriticalSection(&list_cs); int i = FindFileInList(update->szComponentName); if (i >= 0) update_list.remove(i); LeaveCriticalSection(&list_cs); if(update->szComponentName == 0 || update->pbVersion == 0) return 1; if(update->szVersionURL == 0 && (update->szUpdateURL == 0 || strcmp(update->szUpdateURL, UPDATER_AUTOREGISTER) != 0) && update->szBetaVersionURL == 0) // both betas and normal updates disabled - complain return 1; update_internal.cat = MC_UNKNOWN; // duplicate all the data...sigh update_internal.update.szComponentName = safe_strdup(update->szComponentName); if(update->szVersionURL) update_internal.update.szVersionURL = safe_strdup(update->szVersionURL); if(update->szUpdateURL) update_internal.update.szUpdateURL = safe_strdup(update->szUpdateURL); // if revision update url is from the fl, and we can find the file_id, use xml data if available // otherwise set this to -1 to check url's specified if(update_internal.update.szUpdateURL) update_internal.file_id = CheckForFileID(update_internal.update.szUpdateURL, update_internal.update.szVersionURL, update_internal.update.szComponentName); else update_internal.file_id = -1; if(update_internal.file_id != -1) { // ensure correct format for file listing version string search data char *buff = (char *)safe_alloc(strlen("class=\"fileNameHeader\">") + strlen(update->szComponentName) + 2); sprintf(buff, "class=\"fileNameHeader\">%s ", update->szComponentName); update_internal.update.pbVersionPrefix = (BYTE *)buff; update_internal.update.cpbVersionPrefix = (int)strlen(buff); update_internal.shortName = safe_strdup(update->szComponentName); } else { if(update->pbVersionPrefix) update_internal.update.pbVersionPrefix = safe_bytedup(update->pbVersionPrefix, update->cpbVersionPrefix); update_internal.update.cpbVersionPrefix = update->cpbVersionPrefix; } // leave beta alone if(update->szBetaVersionURL) update_internal.update.szBetaVersionURL = safe_strdup(update->szBetaVersionURL); if(update->pbBetaVersionPrefix) update_internal.update.pbBetaVersionPrefix = safe_bytedup(update->pbBetaVersionPrefix, update->cpbBetaVersionPrefix); update_internal.update.cpbBetaVersionPrefix = update->cpbBetaVersionPrefix; if(update->szBetaUpdateURL) update_internal.update.szBetaUpdateURL = safe_strdup(update->szBetaUpdateURL); update_internal.update.pbVersion = safe_bytedup(update->pbVersion, update->cpbVersion); update_internal.update.cpbVersion = update->cpbVersion; if(update->cbSize > UPDATE_V1_SIZE && update->szBetaChangelogURL) update_internal.update.szBetaChangelogURL = safe_strdup(update->szBetaChangelogURL); update_internal.update_options.fixed = (update->szVersionURL == 0 || strcmp(update->szUpdateURL, UPDATER_AUTOREGISTER) == 0 || update->szBetaVersionURL == 0); // set 'fixed' flag update_internal.update_options.use_beta = (update->szVersionURL == 0 || strcmp(update->szUpdateURL, UPDATER_AUTOREGISTER) == 0); LoadUpdateOptions(update_internal.update.szComponentName, &update_internal.update_options); EnterCriticalSection(&list_cs); update_list.insert(new UpdateInternal(update_internal)); LeaveCriticalSection(&list_cs); //if(strcmp(update_internal.update.szComponentName, "My Details") == 0) { // MessageBox(0, "My Details registered", "msg", MB_OK); //} return 0; }
bool RegisterForFileListing(int file_id, const char *fl_name, DWORD version, bool auto_register, const Category cat) { // allow multiple registration of same plugin only if new plugin not automatically registered // if multiple registration of an auto registered plugin occurs, use newest file id and version EnterCriticalSection(&list_cs); int i = FindFileInList(fl_name); if (i >= 0 && !auto_register) update_list.remove(i); else if (i >= 0) { if (update_list[i].auto_register) { update_list[i].file_id = file_id; // in case plugin file id changes (i.e. scan from xml data will overwrite settings read from db on startup) char version_str[16]; update_list[i].update.pbVersion = (BYTE *)safe_strdup(CreateVersionString(version, version_str)); update_list[i].update.cpbVersion = (int)strlen(version_str); } LeaveCriticalSection(&list_cs); // plugin already registered - set file id if AUTOREGISTER if (update_list[i].update.szUpdateURL && strcmp(update_list[i].update.szUpdateURL, UPDATER_AUTOREGISTER) == 0) { update_list[i].file_id = file_id; char *buff = (char *)safe_alloc(strlen(MIM_DOWNLOAD_URL_PREFIX) + 9); sprintf(buff, MIM_DOWNLOAD_URL_PREFIX "%d", file_id); update_list[i].update.szUpdateURL = buff; update_list[i].shortName = safe_strdup(update_list[i].update.szComponentName); if(update_list[i].update.szBetaVersionURL) { update_list[i].update_options.fixed = false; LoadUpdateOptions(update_list[i].update.szComponentName, &update_list[i].update_options); } } return false; } LeaveCriticalSection(&list_cs); UpdateInternal update_internal = {0}; char version_str[16]; char *buff; update_internal.cat = cat; update_internal.shortName = safe_strdup(fl_name); update_internal.update.szComponentName = safe_strdup(fl_name); update_internal.update.pbVersion = (BYTE *)safe_strdup(CreateVersionString(version, version_str)); update_internal.update.cpbVersion = (int)strlen(version_str); buff = (char *)safe_alloc(strlen(MIM_DOWNLOAD_URL_PREFIX) + 9); sprintf(buff, MIM_DOWNLOAD_URL_PREFIX "%d", file_id); update_internal.update.szUpdateURL = buff; ///////// only used when not using the xml backend //////////// buff = (char *)safe_alloc(strlen("class=\"fileNameHeader\">") + strlen(fl_name) + 2); sprintf(buff, "class=\"fileNameHeader\">%s ", fl_name); update_internal.update.pbVersionPrefix = (BYTE *)buff; update_internal.update.cpbVersionPrefix = (int)strlen(buff); buff = (char *)safe_alloc(strlen(MIM_VIEW_URL_PREFIX) + 9); sprintf(buff, MIM_VIEW_URL_PREFIX "%d", file_id); update_internal.update.szVersionURL = buff; /////////////////////////////////////////////////////////////// // same as register, except for fileID update_internal.file_id = file_id; update_internal.auto_register = auto_register; update_internal.update_options.fixed = true; update_internal.update_options.use_beta = false; LoadUpdateOptions(update_internal.update.szComponentName, &update_internal.update_options); EnterCriticalSection(&list_cs); update_list.insert(new UpdateInternal(update_internal)); LeaveCriticalSection(&list_cs); return true; }
int dm_query_device(const char *name, char **device, uint64_t *size, uint64_t *skip, uint64_t *offset, char **cipher, int *key_size, char **key, int *read_only, int *suspended, char **uuid) { struct dm_task *dmt; struct dm_info dmi; uint64_t start, length, val64; char *target_type, *params, *rcipher, *key_, *rdevice, *endp, buffer[3], *tmp_uuid; void *next = NULL; int i, r = -EINVAL; if (!(dmt = dm_task_create(DM_DEVICE_TABLE))) goto out; if (!dm_task_set_name(dmt, name)) goto out; r = -ENODEV; if (!dm_task_run(dmt)) goto out; r = -EINVAL; if (!dm_task_get_info(dmt, &dmi)) goto out; if (!dmi.exists) { r = -ENODEV; goto out; } next = dm_get_next_target(dmt, next, &start, &length, &target_type, ¶ms); if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 || start != 0 || next) goto out; if (size) *size = length; rcipher = strsep(¶ms, " "); /* cipher */ if (cipher) *cipher = strdup(rcipher); /* skip */ key_ = strsep(¶ms, " "); if (!params) goto out; val64 = strtoull(params, ¶ms, 10); if (*params != ' ') goto out; params++; if (skip) *skip = val64; /* device */ rdevice = strsep(¶ms, " "); if (device) *device = lookup_dev(rdevice); /*offset */ if (!params) goto out; val64 = strtoull(params, ¶ms, 10); if (*params) goto out; if (offset) *offset = val64; /* key_size */ if (key_size) *key_size = strlen(key_) / 2; /* key */ if (key_size && key) { *key = safe_alloc(*key_size); if (!*key) { r = -ENOMEM; goto out; } buffer[2] = '\0'; for(i = 0; i < *key_size; i++) { memcpy(buffer, &key_[i * 2], 2); (*key)[i] = strtoul(buffer, &endp, 16); if (endp != &buffer[2]) { safe_free(key); *key = NULL; goto out; } } } memset(key_, 0, strlen(key_)); if (read_only) *read_only = dmi.read_only; if (suspended) *suspended = dmi.suspended; if (uuid && (tmp_uuid = (char*)dm_task_get_uuid(dmt)) && !strncmp(tmp_uuid, DM_UUID_PREFIX, DM_UUID_PREFIX_LEN)) *uuid = strdup(tmp_uuid + DM_UUID_PREFIX_LEN); r = (dmi.open_count > 0); out: if (dmt) dm_task_destroy(dmt); return r; }