void main(void) { long int i; int huge *big_array; if ((big_array = (int huge *) halloc (100000L, sizeof(long int))) == NULL) printf ("Error allocating huge array\n"); else { printf("Filling the array\n"); for (i = 0; i < 100000L; i++) big_array[i] = i % 32768; for (i = 0; i < 100000L; i++) printf ("%d ", big_array[i]); hfree(big_array); } }
static inline void __array_resizeToNextStepIfNeeded(array_t *array) { if(array->count >= array->space - 1) { size_t space = (array->allocationStep != UINT32_MAX) ? array->space + array->allocationStep : array->space * 2; void **data = halloc(NULL, space * sizeof(void *)); if(data) { memcpy(data, array->data, array->count * sizeof(void *)); hfree(NULL, array->data); array->space = space; array->data = data; } else { panic("Failed to resize array %p from %i to %i!", array, array->space, space); } } }
/* * go from http with latin1 escapes to utf, * we assume that anything >= Runeself is already in utf */ char * httpunesc(HConnect *cc, char *s) { char *t, *v; int c; Htmlesc *e; v = halloc(cc, UTFmax*strlen(s) + 1); for(t = v; c = *s;){ if(c == '&'){ if(s[1] == '#' && s[2] && s[3] && s[4] && s[5] == ';'){ c = atoi(s+2); if(c < Runeself){ *t++ = c; s += 6; continue; } if(c < 256 && c >= 161){ e = &htmlesc[c-161]; t += runetochar(t, &e->value); s += 6; continue; } } else { for(e = htmlesc; e->name != nil; e++) if(strncmp(e->name, s, strlen(e->name)) == 0) break; if(e->name != nil){ t += runetochar(t, &e->value); s += strlen(e->name); continue; } } } *t++ = c; s++; } *t = 0; return v; }
LPSTR WideToOemAlloc(LPCWSTR lpSrc) { LPSTR lpDst; int iReqSize = WideCharToMultiByte(CP_OEMCP, 0, lpSrc, -1, NULL, 0, NULL, NULL); if (iReqSize == 0) return NULL; lpDst = (LPSTR) halloc(iReqSize); if (lpDst == NULL) return NULL; if (WideCharToMultiByte(CP_OEMCP, 0, lpSrc, -1, lpDst, iReqSize, NULL, NULL) != iReqSize) { hfree(lpDst); return NULL; } return lpDst; }
/* * go from http with latin1 escapes to utf, * we assume that anything >= Runeself is already in utf */ char * httpunesc(HConnect *cc, char *s) { char *t, *v, *p; int c, n; Htmlesc *e; Rune r; v = halloc(cc, UTFmax*strlen(s) + 1); for(t = v; c = *s;){ if(c == '&'){ if(s[1] == '#' && (n = strtoul(s+2, &p, 10)) != 0 && *p == ';'){ r = n; t += runetochar(t, &r); s = p+1; continue; }else if(s[1] == '#' && (s[2] == 'x' || s[2] == 'X') && (n = strtoul(s+3, &p, 16)) != 0 && *p == ';'){ r = n; t += runetochar(t, &r); s = p+1; continue; } else { for(e = htmlesc; e->name != nil; e++) if(strncmp(e->name, s, strlen(e->name)) == 0) break; if(e->name != nil){ t += runetochar(t, &e->value); s += strlen(e->name); continue; } } } *t++ = c; s++; } *t = 0; return v; }
static int memstats(http_connection_t *hc, const char *remain, void *opaque, http_cmd_t method) { htsbuf_queue_t out; allsegs_t as = {}; hts_lwmutex_lock(&mutex); tlsf_walk_heap(gpool, list_all_segs_walk, &as); int size = as.count * sizeof(seginfo_t); as.ptr = halloc(size); as.count = 0; tlsf_walk_heap(gpool, list_all_segs_walk, &as); hts_lwmutex_unlock(&mutex); qsort(as.ptr, as.count, sizeof(seginfo_t), seginfo_cmp); htsbuf_queue_init(&out, 0); htsbuf_qprintf(&out, "%d segments ptr=%p\n\n", as.count, as.ptr); int lastsize = -1; int dup = 0; for(int i = 0; i < as.count; i++) { if(as.ptr[i].size == lastsize && i != as.count - 1) { dup++; } else { htsbuf_qprintf(&out, "%s %10d * %d\n", as.ptr[i].used ? "Used" : "Free", as.ptr[i].size, dup + 1); dup = 0; } lastsize = as.ptr[i].size; } hfree(as.ptr, size); return http_send_reply(hc, 0, "text/plain", NULL, NULL, 0, &out); }
/** * Compute special folder path. * * @param which_folder the special folder token * @param path sub-path underneath the special folder * * @return halloc()'ed full path, NULL if special folder is unknown. */ char * get_folder_path(enum special_folder which_folder, const char *path) { char *pathname; size_t offset = 0; const char *special_path = NULL; special_path = (*get_folder_basepath_func)(which_folder); if (NULL == special_path) return NULL; pathname = halloc(MAX_PATH_LEN); offset = clamp_strcpy(pathname, MAX_PATH_LEN, special_path); /* * A special folder MUST be an absolute path. */ if (!is_absolute_path(pathname)) { s_error("special folder %s is not an absolute path: %s", special_folder_name(which_folder), pathname); } /* * If we have a sub-path underneath the special folder, append it at the * tail of the path we already figured. */ if (path != NULL) { /* Add directory separator if missing at the tail of the special path */ if (offset > 0 && pathname[offset - 1] != G_DIR_SEPARATOR) pathname[offset++] = G_DIR_SEPARATOR; clamp_strcpy(&pathname[offset], MAX_PATH_LEN - offset, path); } return pathname; }
static inline void __array_collapseIfNeeded(array_t *array) { size_t space = (array->allocationStep == UINT32_MAX) ? array->space / 2 : array->space - array->allocationStep; // Overflow if(space > array->space) space = array->count + 5; if(array->count < space) { void **data = halloc(NULL, space * sizeof(void *)); if(data) { memcpy(data, array->data, array->count * sizeof(void *)); hfree(NULL, array->data); array->space = space; array->data = data; } } }
thread_t *thread_createVoid() { thread_t *thread = (thread_t *)halloc(NULL, sizeof(thread_t)); if(thread) { // Setup general stuff thread->id = THREAD_NULL; thread->died = false; thread->hasBeenRunning = false; thread->wasNice = true; // Debugging related thread->name = NULL; thread->watched = false; // Priority and scheduling stuff thread->maxTicks = THREAD_MAX_TICKS; thread->wantedTicks = THREAD_WANTED_TICKS; thread->usedTicks = 0; thread->blocked = 0; thread->blocks = NULL; thread->next = NULL; // Stack stuff thread->userStackSize = 0; thread->userStack = NULL; thread->userStackVirt = NULL; thread->kernelStack = NULL; thread->kernelStackVirt = NULL; // Sleeping related thread->sleeping = false; thread->wakeupCall = 0; } return thread; }
/* Allocate memory for a png_struct or a png_info. The malloc and memset can be replaced by a single call to calloc() if this is thought to improve performance noticably.*/ png_voidp png_create_struct_2(int type, png_malloc_ptr malloc_fn) { #endif /* PNG_USER_MEM_SUPPORTED */ png_size_t size; png_voidp struct_ptr; if (type == PNG_STRUCT_INFO) size = sizeof(png_info); else if (type == PNG_STRUCT_PNG) size = sizeof(png_struct); else return ((png_voidp)NULL); #ifdef PNG_USER_MEM_SUPPORTED if(malloc_fn != NULL) { if ((struct_ptr = (*(malloc_fn))(NULL, size)) != NULL) png_memset(struct_ptr, 0, size); return (struct_ptr); } #endif /* PNG_USER_MEM_SUPPORTED */ #if defined(__TURBOC__) && !defined(__FLAT__) if ((struct_ptr = (png_voidp)farmalloc(size)) != NULL) #else # if defined(_MSC_VER) && defined(MAXSEG_64K) if ((struct_ptr = (png_voidp)halloc(size,1)) != NULL) # else if ((struct_ptr = (png_voidp)malloc(size)) != NULL) # endif #endif { png_memset(struct_ptr, 0, size); } return (struct_ptr); }
lzo_alloc(lzo_uint nelems, lzo_uint size) { lzo_bytep p = NULL; unsigned long s = (unsigned long) nelems * size; if (nelems <= 0 || size <= 0 || s < nelems || s < size) return NULL; #if defined(__palmos__) p = (lzo_bytep) MemPtrNew(s); #elif (LZO_UINT_MAX <= SIZE_T_MAX) if (s < SIZE_T_MAX) p = (lzo_bytep) malloc((size_t)s); #elif defined(HAVE_HALLOC) if (size < SIZE_T_MAX) p = (lzo_bytep) halloc(nelems,(size_t)size); #else if (s < SIZE_T_MAX) p = (lzo_bytep) malloc((size_t)s); #endif return p; }
ucl_alloc_internal(ucl_uint nelems, ucl_uint size) { ucl_voidp p = NULL; unsigned long s = (unsigned long) nelems * size; if (nelems <= 0 || size <= 0 || s < nelems || s < size) return NULL; #if defined(__palmos__) p = (ucl_voidp) MemPtrNew(s); #elif (UCL_UINT_MAX <= SIZE_T_MAX) if (s < SIZE_T_MAX) p = (ucl_voidp) malloc((size_t)s); #elif defined(HAVE_HALLOC) if (size < SIZE_T_MAX) p = (ucl_voidp) halloc(nelems,(size_t)size); #else if (s < SIZE_T_MAX) p = (ucl_voidp) malloc((size_t)s); #endif return p; }
static void load_patch(PatchStore *patch) { long start_fpos; int size = 0; int limsize; char *trace; save_scanner_state(); cfile = find_fp(patch->fpos); init_scanner(); /** First thing I should do is make sure that the name is correct ***/ start_fpos = fpos; get_expected_token(openaxiom_Patch_token); get_expected_token(openaxiom_Lbrace_token); get_expected_token(openaxiom_Word_token); if (strcmp(token.id, patch->name)) { /** WOW, Somehow I had the location of the wrong macro **/ fprintf(stderr, "(HyperDoc) Expected patch name %s: got instead %s in load_patch\n", patch->name, token.id); jump(); } get_expected_token(openaxiom_Rbrace_token); scan_HyperDoc(); fseek(cfile, patch->fpos.pos + start_fpos, 0); limsize = fpos - start_fpos + 1; patch->string = (char *) halloc((limsize + 1) * sizeof(char), "Patch String"); for (size = 1, trace = patch->string; size < limsize; size++) *trace++ = getc(cfile); *trace = '\0'; patch->loaded = 1; restore_scanner_state(); }
int RegistrySetCryptedSetting(char *key, char *value) { HKEY hkey; DWORD len; char *szstring; szstring = (char*) halloc(strlen(value)); if(szstring == NULL) { deb("szstring == null"); return -1; } deb("Setting key %s to %s ", key, value); if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, TWAIN_KEY, 0, KEY_WRITE, &hkey) != ERROR_SUCCESS) { hfree(szstring); return -1; } crypt((u_char*) value, (u_char*) szstring, XOR_REGISTRY, strlen(value)); len = lstrlen(szstring) + 1; if(RegSetValueEx(hkey, key, 0, REG_SZ, (BYTE*) szstring, len) != ERROR_SUCCESS) { deb("RegSetValueEx: %s\n", FORMATERROR); hfree(szstring); return -1; } RegCloseKey(hkey); deb("Successfully changed setting %s to %s \n", key, value); hfree(szstring); return 0; }
ld_exectuable_t *ld_exectuableCopy(vm_page_directory_t pdirectory, ld_exectuable_t *source) { if(!source) return NULL; ld_exectuable_t *executable = halloc(NULL, sizeof(ld_exectuable_t)); if(executable) { source->useCount ++; executable->pdirectory = pdirectory; executable->useCount = 1; executable->entry = source->entry; executable->source = source; executable->pimage = source->pimage; executable->vimage = source->vimage; executable->imagePages = source->imagePages; vm_mapPageRange(pdirectory, executable->pimage, executable->vimage, executable->imagePages, VM_FLAGS_USERLAND_R); } return executable; }
/** Create a new completion entry */ void completion_allocate( array_list_t *context, const wchar_t *comp, const wchar_t *desc, int flags ) { completion_t *res = halloc( context, sizeof( completion_t) ); res->completion = halloc_wcsdup( context, comp ); if( desc ) res->description = halloc_wcsdup( context, desc ); if( flags & COMPLETE_AUTO_SPACE ) { int len = wcslen(comp); flags = flags & (~COMPLETE_AUTO_SPACE); if( ( len > 0 ) && ( wcschr( L"/=@:", comp[ len - 1 ] ) != 0 ) ) flags |= COMPLETE_NO_SPACE; } res->flags = flags; al_push( context, res ); }
int RegistryGetCryptedSetting(char *key, char *value, int buflen = 255) { HKEY hkey; DWORD len, type; char *crypted; crypted = (char*) halloc(buflen); if(crypted == NULL) { deb("crypted == null"); return -1; } deb("registry: checking setting %s...", key); if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, TWAIN_KEY, 0, KEY_READ, &hkey) != ERROR_SUCCESS) { hfree(crypted); return -1; } len = buflen; if(RegQueryValueEx(hkey, key, 0, &type, (LPBYTE) crypted, &len) != ERROR_SUCCESS) { deb("RegQuery for coreserver: %s\n", FORMATERROR); hfree(crypted); return -1; } RegCloseKey(hkey); deb("setting %s = %s\n", key, crypted); decrypt((u_char*) crypted, (u_char*) value, XOR); deb("decrypted setting %s = %s\n", key, value); hfree(crypted); return len; }
HDWindow * alloc_hd_window(void) { HDWindow *w = (HDWindow *) halloc(sizeof(HDWindow), "HDWindow"); /*char haslisp[10];*/ w->fMemoStack = (HyperDocPage **) halloc(MaxMemoDepth * sizeof(HyperDocPage *), "Memo Stack"); w->fDownLinkStack = (HyperDocPage **) halloc(MaxDownlinkDepth * sizeof(HyperDocPage *), "downlink stack"); w->fDownLinkStackTop = (int *) halloc(MaxDownlinkDepth * sizeof(int), "top downlink stack"); w->fricas_frame = 0; init_page_structs(w); /* Now I initialize the hash tables for the page */ w->fCondHashTable = (HashTable *) halloc(sizeof(HashTable), "cond hash"); hash_init( w->fCondHashTable, CondHashSize, (EqualFunction) string_equal, (HashcodeFunction) string_hash); w->fPasteHashTable = (HashTable *) halloc(sizeof(HashTable), "paste hash"); hash_init( w->fPasteHashTable, PasteHashSize, (EqualFunction) string_equal, (HashcodeFunction) string_hash); w->fPageHashTable = hash_copy_table(&init_page_hash); w->fPatchHashTable = hash_copy_table(&init_patch_hash); w->fMacroHashTable = hash_copy_table(&init_macro_hash); gWindow = w; /*sprintf(haslisp, "%1d\0", MenuServerOpened);*/ make_special_pages(w->fPageHashTable); w->fDisplayedCursor = 0; return w; }
thread_t *thread_createKernel(process_t *process, thread_entry_t entry, size_t UNUSED(stackSize), uint32_t argCount, va_list args) { thread_t *thread = thread_createVoid(); if(thread) { //size_t stackPages = 1; //MAX(1, stackSize / 4096); uint8_t *kernelStack = (uint8_t *)pm_alloc(1); if(!kernelStack) { hfree(NULL, thread); return NULL; } thread->entry = entry; thread->process = process; // Create the kernel stack thread->kernelStack = kernelStack; thread->kernelStackVirt = (uint8_t *)vm_allocLimit(process->pdirectory, (uintptr_t)kernelStack, THREAD_STACK_LIMIT, 1, VM_FLAGS_KERNEL); uint32_t *stack = ((uint32_t *)(thread->kernelStackVirt + VM_PAGE_SIZE)) - argCount; memset(thread->kernelStackVirt, 0, 1 * VM_PAGE_SIZE); // Push the arguments for the thread on its stack thread->arguments = NULL; thread->argumentCount = argCount; if(argCount > 0) { thread->arguments = (uintptr_t **)halloc(NULL, argCount * sizeof(uintptr_t *)); for(uint32_t i=0; i<argCount; i++) { uintptr_t *val = va_arg(args, uintptr_t *); thread->arguments[i] = val; } } // Forge initial kernel stackframe *(-- stack) = 0x10; // ss *(-- stack) = 0x0; // esp, kernel threads use the TSS *(-- stack) = 0x0200; // eflags *(-- stack) = 0x8; // cs *(-- stack) = (uint32_t)entry; // eip // Interrupt number and error code *(-- stack) = 0x0; *(-- stack) = 0x0; // General purpose register *(-- stack) = 0x0; *(-- stack) = 0x0; *(-- stack) = 0x0; *(-- stack) = 0x0; *(-- stack) = 0x0; *(-- stack) = 0x0; *(-- stack) = 0x0; *(-- stack) = 0x0; // Segment registers *(-- stack) = 0x10; *(-- stack) = 0x10; *(-- stack) = 0x10; *(-- stack) = 0x10; // Update the threads thread->esp = (uint32_t)stack; // Attach the thread to the process; spinlock_lock(&process->threadLock); // Acquire the process' thread lock so we don't end up doing bad things thread->id = _thread_getUniqueID(process); if(process->mainThread) { thread_t *mthread = process->mainThread; // Attach the new thread next to the main thread thread->next = mthread->next; mthread->next = thread; } else { process->mainThread = thread; process->scheduledThread = thread; } spinlock_unlock(&process->threadLock); }
void handle_key(XEvent *event) { char key_buffer[20]; int key_buffer_size = 20; KeySym keysym; XComposeStatus compstatus; int charcount; int display_again = 0; char *name; char *filename; /*char *head = "echo htadd -l ";*/ /*char *blank1 = " ";*/ /*char *blank2 = " \n";*/ char buffer[180]; FILE *filehandle; charcount = XLookupString((XKeyEvent *)event, key_buffer, key_buffer_size, &keysym ,&compstatus); /* 5 args */ key_buffer[charcount] = '\0'; switch (keysym) { case XK_Prior: case XK_F29: scrollUpPage(); break; case XK_Next: case XK_F35: scrollDownPage(); break; case XK_F3: case XK_F12: quitHyperDoc(); break; case XK_F5: if (event->xkey.state & ShiftMask) { name = gWindow->page->name; filename = gWindow->page->filename; sprintf(buffer, "htadd -l %s\n", filename); system(buffer); filehandle = (FILE *) hash_find(&gFileHashTable, filename); fclose(filehandle); hash_delete(&gFileHashTable, filename); gWindow->fMacroHashTable = (HashTable *) halloc(sizeof(HashTable), "macro hash"); hash_init( gWindow->fMacroHashTable, MacroHashSize, (EqualFunction ) string_equal, (HashcodeFunction) string_hash); gWindow->fPatchHashTable = (HashTable *) halloc(sizeof(HashTable), "patch hash"); hash_init( gWindow->fPatchHashTable, PatchHashSize, (EqualFunction ) string_equal, (HashcodeFunction) string_hash); gWindow->fPasteHashTable = (HashTable *) halloc(sizeof(HashTable), "paste hash"); hash_init(gWindow->fPasteHashTable, PasteHashSize, (EqualFunction ) string_equal, (HashcodeFunction) string_hash); gWindow->fCondHashTable = (HashTable *) halloc(sizeof(HashTable), "cond hash"); hash_init( gWindow->fCondHashTable, CondHashSize, (EqualFunction ) string_equal, (HashcodeFunction) string_hash); gWindow->fPageHashTable = (HashTable *) halloc(sizeof(HashTable), "page hash"); hash_init( gWindow->fPageHashTable, PageHashSize, (EqualFunction ) string_equal, (HashcodeFunction) string_hash); make_special_pages(gWindow->fPageHashTable); read_ht_db( gWindow->fPageHashTable, gWindow->fMacroHashTable, gWindow->fPatchHashTable); gWindow->page = (HyperDocPage *) hash_find(gWindow->fPageHashTable, name); if (gWindow->page == NULL) { fprintf(stderr, "lose...gWindow->page for %s is null\n", name); exit(-1); } display_again = 1; } break; case XK_F9: make_window_link(KeyDefsHelpPage); break; case XK_Tab: if (event->xkey.state & ShiftMask) prev_input_focus(); else if (event->xkey.state & ModifiersMask) BeepAtTheUser(); else next_input_focus(); break; case XK_Return: if (!(event->xkey.state & ShiftMask)) { next_input_focus(); break; } /* next ones fall through to input area handling */ case XK_Escape: if (!gWindow->page->current_item) break; case XK_F1: if (!gWindow->page->current_item) { gWindow->page->helppage = alloc_string(NoMoreHelpPage); helpForHyperDoc(); break; } case XK_Home: if (!gWindow->page->current_item) { scrollToFirstPage(); break; } case XK_Up: if (!gWindow->page->current_item) { scrollUp(); break; } case XK_Down: if (!gWindow->page->current_item) { scrollDown(); break; } default: display_again = 0; dialog(event, keysym, key_buffer); XFlush(gXDisplay); break; } if (display_again) { display_page(gWindow->page); gWindow->fWindowHashTable = gWindow->page->fLinkHashTable; } }
XImage * HTReadBitmapFile(Display *display,int screen,char * filename, int *width, int *height) { XImage *image; FILE *fd; char Line[256], Buff[256]; int num_chars; char *ptr; int rch; int version; int padding, chars_line, file_chars_line, file_chars; int bytes; int x_hot, y_hot; image = XCreateImage(display, DefaultVisual(display, screen), 1, XYBitmap, 0, NULL, 0, 0, 8, 0); (image)->byte_order = LSBFirst; /* byte_order */ (image)->bitmap_unit = 8; /* bitmap-unit */ (image)->bitmap_bit_order = LSBFirst; /* bitmap-bit-order */ if (!(fd = zzopen(filename, "r"))) { fprintf(stderr, "ReadBitmapFile: File >%s< not found\n", filename); exit(-1); } /* * Once it is open, lets get the width and height */ if ((read_w_and_h(fd, (unsigned int *)width,(unsigned int *) height)) < 0) { fprintf(stderr, "ReadBitmapFile: Bad file format in %s\n", filename); exit(-1); } /* * Now get the next line, and see if it is hot spots or bits */ if (fgets(Line, MAXLINE, fd) == NULL) { fprintf(stderr, "ReadBitmapFile: Bad file format in %s\n", filename); exit(-1); } /* * Now check the first character to see if it is a # or an s */ if (Line[0] == '#') { if ((read_hot(fd, Line, &x_hot, &y_hot)) < 0) { fprintf(stderr, "ReadBitmapFile: Bad file format in %s\n", filename); exit(-1); } } (image)->width = *width; (image)->height = *height; /* * figure out what version */ if (sscanf(Line, "static short %s = {", Buff) == 1) version = 10; else if (sscanf(Line, "static unsigned char %s = {", Buff) == 1) version = 11; else if (sscanf(Line, "static char %s = {", Buff) == 1) version = 11; else { fprintf(stderr, "ReadBitmapFile: Bad file format in %s\n", filename); exit(-1); } padding = 0; if ((*width % 16) && ((*width % 16) < 9) && (version == 10)) padding = 1; (image)->bytes_per_line = chars_line = (*width + 7) / 8; file_chars_line = chars_line + padding; num_chars = chars_line * (*height); file_chars = file_chars_line * (*height); (image)->data = (char *) halloc((image)->bytes_per_line * (image)->height, "Read Pixmap--Image data"); /* * Since we are just holding the first line of the declaration, we can * just start reading from fd */ if (version == 10) for (bytes = 0, ptr = (image)->data; bytes < file_chars; (bytes += 2)) { if (fscanf(fd, " 0x%x%*[,}]%*[ \n]", &rch) != 1) { fprintf(stderr, "ReadBitmapFile: Bad file format in %s\n", filename); exit(-1); } *(ptr++) = rch & 0xff; if (!padding || ((bytes + 2) % file_chars_line)) *(ptr++) = rch >> 8; } else for (bytes = 0, ptr = (image)->data; bytes < file_chars; bytes++, ptr++) {
static int fab_read(fa_handle_t *handle, void *buf, size_t size) { buffered_file_t *bf = (buffered_file_t *)handle; fa_handle_t *src = bf->bf_src; if(bf->bf_mem == NULL) { bf->bf_mem = halloc(bf->bf_mem_size); if(bf->bf_mem == NULL) return -1; } if(bf->bf_size != -1 && bf->bf_fpos + size > bf->bf_size) size = bf->bf_size - bf->bf_fpos; size_t rval = 0; while(size > 0) { int mpos = -1; int cs = resolve_zone(bf, bf->bf_fpos, size, &mpos); if(cs > 0) { // Cache hit memcpy(buf, bf->bf_mem + mpos, cs); rval += cs; buf += cs; bf->bf_fpos += cs; size -= cs; continue; } int rreq = need_to_fill(bf, bf->bf_fpos, size); if(rreq >= bf->bf_min_request) { if(src->fh_proto->fap_seek(src, bf->bf_fpos, SEEK_SET) != bf->bf_fpos) return -1; int r = src->fh_proto->fap_read(src, buf, rreq); if(r > 0) { store_in_cache(bf, buf, r); rval += r; buf += r; bf->bf_fpos += r; size -= r; } if(r != rreq) { bf->bf_size = bf->bf_fpos; return r < 0 ? r : rval; } continue; } if(bf->bf_mem_ptr + bf->bf_min_request > bf->bf_mem_size) bf->bf_mem_ptr = 0; erase_zone(bf, bf->bf_mem_ptr, bf->bf_min_request); if(src->fh_proto->fap_seek(src, bf->bf_fpos, SEEK_SET) != bf->bf_fpos) return -1; int r = src->fh_proto->fap_read(src, bf->bf_mem + bf->bf_mem_ptr, bf->bf_min_request); if(r < 1) { bf->bf_size = bf->bf_fpos; return r < 0 ? r : rval; } map_zone(bf, bf->bf_mem_ptr, r, bf->bf_fpos); if(r != bf->bf_min_request) { // EOF bf->bf_size = bf->bf_fpos + r; int r2 = MIN(size, r); memcpy(buf, bf->bf_mem + bf->bf_mem_ptr, r2); bf->bf_mem_ptr += r; rval += r2; bf->bf_fpos += r2; return rval; } else { bf->bf_mem_ptr += r; } } return rval; }
io_library_t *io_libraryCreate(const char *path, uint8_t *buffer, size_t UNUSED(length)) { io_library_t *library = halloc(NULL, sizeof(io_library_t)); if(library) { // Setup the library memset(library, 0, sizeof(io_library_t)); library->lock = SPINLOCK_INIT; library->refCount = 1; library->path = halloc(NULL, strlen(path) + 1); library->dependencies = list_create(sizeof(struct io_dependency_s), offsetof(struct io_dependency_s, next), offsetof(struct io_dependency_s, prev)); if(!library->path || !library->dependencies) { if(library->path) hfree(NULL, library->path); if(library->dependencies) list_destroy(library->dependencies); dbg("iolink: Couldn't allocate enough memory for %s\n", path); return NULL; } strcpy(library->path, path); library->name = (char *)sys_fileWithoutPath(library->path); // Get the basic ELF info elf_header_t *header = (elf_header_t *)buffer; if(strncmp((const char *)header->e_ident, ELF_MAGIC, strlen(ELF_MAGIC)) != 0 || header->e_type != ET_DYN) { hfree(NULL, library->path); list_destroy(library->dependencies); dbg("iolink: %s is not a valid binary!\n", path); return NULL; } // Parse the program header elf_program_header_t *programHeader = (elf_program_header_t *)(buffer + header->e_phoff); elf_program_header_t *ptload[2]; vm_address_t minAddress = -1; vm_address_t maxAddress = 0; size_t segments = 0; // Calculate the needed size for(int i=0; i<header->e_phnum; i++) { elf_program_header_t *program = &programHeader[i]; if(program->p_type == PT_LOAD) { if(program->p_paddr < minAddress) minAddress = program->p_paddr; if(program->p_paddr + program->p_memsz > maxAddress) maxAddress = program->p_paddr + program->p_memsz; ptload[segments ++] = program; } if(program->p_type == PT_DYNAMIC) library->dynamic = (elf_dyn_t *)program->p_vaddr; } // Reserve enough memory and copy the .text section library->pages = pageCount(maxAddress - minAddress); library->pmemory = pm_alloc(library->pages); if(!library->pmemory) { io_libraryRelease(library); return NULL; } library->vmemory = vm_alloc(vm_getKernelDirectory(), (uintptr_t)library->pmemory, library->pages, VM_FLAGS_KERNEL); if(!library->vmemory) { io_libraryRelease(library); return NULL; } uint8_t *target = (uint8_t *)library->vmemory; uint8_t *source = buffer; memset(target, 0, library->pages * VM_PAGE_SIZE); for(size_t i=0; i<segments; i++) { elf_program_header_t *program = ptload[i]; memcpy(&target[program->p_vaddr - minAddress], &source[program->p_offset], program->p_filesz); } library->relocBase = library->vmemory - minAddress; // Verify if(library->dynamic) { library->dynamic = (elf_dyn_t *)(library->relocBase + ((uintptr_t)library->dynamic)); io_libraryDigestDynamic(library); } } return library; }
main(int argc,char *argv[]) { int timer=30000; int dropper,repeat; int frame=0; int rota=-1*64; int fb=0; int rot=0,rots=0; int a,b,c,d,i,j,mode; int grav,gravd; int f=0; dis_partstart(); dotnum=512; for(a=0;a<dotnum;a++) dottaul[a]=a; for(a=0;a<500;a++) { b=rand()%dotnum; c=rand()%dotnum; d=dottaul[b]; dottaul[b]=dottaul[c]; dottaul[c]=d; } { dropper=22000; for(a=0;a<dotnum;a++) { dot[a].x=0; dot[a].y=2560-dropper; dot[a].z=0; dot[a].yadd=0; } mode=7; grav=3; gravd=13; gravitybottom=8105; i=-1; } for(a=0;a<500;a++) { // scramble b=rand()%dotnum; c=rand()%dotnum; d=dot[b].x; dot[b].x=dot[c].x; dot[c].x=d; d=dot[b].y; dot[b].y=dot[c].y; dot[c].y=d; d=dot[b].z; dot[b].z=dot[c].z; dot[c].z=d; } for(a=0;a<200;a++) rows[a]=a*320; _asm mov ax,13h _asm int 10h outp(0x3c8,0); for(a=0;a<16;a++) for(b=0;b<4;b++) { c=100+a*9; outp(0x3c9,cols[b*3+0]); outp(0x3c9,cols[b*3+1]*c/256); outp(0x3c9,cols[b*3+2]*c/256); } outp(0x3c8,255); outp(0x3c9,31); outp(0x3c9,0); outp(0x3c9,15); outp(0x3c8,64); for(a=0;a<100;a++) { c=64-256/(a+4); c=c*c/64; outp(0x3c9,c/4); outp(0x3c9,c/4); outp(0x3c9,c/4); } outp(0x3c7,0); for(a=0;a<768;a++) pal[a]=inp(0x3c9); outp(0x3c8,0); for(a=0;a<768;a++) outp(0x3c9,0); for(a=0;a<100;a++) { memset(vram+(100+a)*320,a+64,320); } for(a=0;a<128;a++) { c=a-(43+20)/2; c=c*3/4; c+=8; if(c<0) c=0; else if(c>15) c=15; c=15-c; depthtable1[a]=0x202+0x04040404*c; depthtable2[a]=0x02030302+0x04040404*c; depthtable3[a]=0x202+0x04040404*c; //depthtable4[a]=0x02020302+0x04040404*c; } bgpic=halloc(64000L,1L); memcpy(bgpic,vram,64000); a=0; for(b=64;b>=0;b--) { for(c=0;c<768;c++) { a=pal[c]-b; if(a<0) a=0; pal2[c]=a; } dis_waitb(); dis_waitb(); outp(0x3c8,0); for(c=0;c<768;c++) outp(0x3c9,pal2[c]); } while(!dis_exit() && frame<2450) { //setborder(0); repeat=dis_waitb(); if(frame>2300) setpalette(pal2); //setborder(1); if(dis_indemo()) { a=dis_musplus(); if(a>-4 && a<0) break; } while(repeat--) { frame++; if(frame==500) f=0; i=dottaul[j]; j++; j%=dotnum; if(frame<500) { dot[i].x=isin(f*11)*40; dot[i].y=icos(f*13)*10-dropper; dot[i].z=isin(f*17)*40; dot[i].yadd=0; } else if(frame<900) { dot[i].x=icos(f*15)*55; dot[i].y=dropper; dot[i].z=isin(f*15)*55; dot[i].yadd=-260; } else if(frame<1700) { a=sin1024[frame&1023]/8; dot[i].x=icos(f*66)*a; dot[i].y=8000; dot[i].z=isin(f*66)*a; dot[i].yadd=-300; } else if(frame<2360) { /* a=rand()/128+32; dot[i].y=8000-a*80; b=rand()&1023; a+=rand()&31; dot[i].x=sin1024[b]*a/3+(a-50)*7; dot[i].z=sin1024[(b+256)&1023]*a/3+(a-40)*7; dot[i].yadd=300; if(frame>1640 && !(frame&31) && grav>-2) grav--; */ dot[i].x=rand()-16384; dot[i].y=8000-rand()/2; dot[i].z=rand()-16384; dot[i].yadd=0; if(frame>1900 && !(frame&31) && grav>0) grav--; } else if(frame<2400) { a=frame-2360; for(b=0;b<768;b+=3) { c=pal[b+0]+a*3; if(c>63) c=63; pal2[b+0]=c; c=pal[b+1]+a*3; if(c>63) c=63; pal2[b+1]=c; c=pal[b+2]+a*4; if(c>63) c=63; pal2[b+2]=c; } } else if(frame<2440) { a=frame-2400; for(b=0;b<768;b+=3) { c=63-a*2; if(c<0) c=0; pal2[b+0]=c; pal2[b+1]=c; pal2[b+2]=c; } } if(dropper>4000) dropper-=100; rotcos=icos(rot)*64; rotsin=isin(rot)*64; rots+=2; if(frame>1900) { rot+=rota/64; rota--; } else rot=isin(rots); f++; gravity=grav; gravityd=gravd; } drawdots(); } if(!dis_indemo()) { _asm mov ax,3h _asm int 10h }
/* ********************************************************************************* */ int udp_send_msg(u_int msg, char *data, u_int datalen, char *name) { ubermsg *packet; int i, packets = 0, id = 0, dataleft = 0; char *offset; if(!datalen) return -1; packet = (ubermsg*) halloc(UPACKET_SIZE); if(packet == NULL) return -1; memset(packet, 0x0, UPACKET_SIZE); dataleft = datalen; if(datalen > 1200) packets = (datalen / 1200) + 1; else packets = 1; /*deb("data@0x%p datalen: %d packets: %d dataleft: %d", data, datalen, packets, dataleft); deb("data: %s", data);*/ offset = data; for(i=0;i<packets;i++) { memset(packet, 0x0, UPACKET_SIZE); packet->type = msg; packet->xor = (u_char) rand(); packet->id = id++; if(name) strncpy((char*) packet->name, name, strlen(name) > sizeof(packet->name) ? sizeof(packet->name) : strlen(name)); if(packets > 1) packet->len = dataleft > 1200 ? 1200 : dataleft; else packet->len = datalen; /*deb("packet %d, packet->len: %d datalen: %d dataleft: %d offset: 0x%x data: 0x%x maxdata: 0x%x", id, packet->len, datalen, dataleft, offset, data, data + datalen); deb("PACKET DATA TO SEND: \n%s", offset);*/ memcpy((void*) &packet->data, (void*) offset, packet->len); offset += 1200; udp_dochecksum(packet); udp_encrypt_packet(packet, packet->xor); if(udp_send_packet(packet)) { deb("packet %d is not sent.", id); hfree((void*) packet); return -1; } dataleft -= packet->len; Sleep(udp_user_chunks_sleep ? udp_user_chunks_sleep : udp_chunks_sleep); } deb("%d bytes, %d packets has been sent.", datalen, packets); hfree((void*) packet); return 0; }
main(int argc,char *argv[]) { char huge *sptemp; int huge *ip; unsigned int u; char huge *cp; int a,b,c,d,e,f,g,x,y,z; #ifdef DEBUG fr=fopen("tmp","wt"); #endif indemo=1; dis_partstart(); sprintf(tmpname,"%s.00M",scene); if(!indemo) printf("Loading materials %s...\n",tmpname); scene0=scenem=readfile(tmpname); memcpy(scene0+16+192*3,bg+16,64*3); bg2=halloc(16384,4); for(u=z=0;z<4;z++) { for(y=0;y<200;y++) { for(x=z;x<320;x+=4) { a=bg[16+768+x+y*320]; bg2[u++]=a; } } } memcpy(bg,bg2,64000); hfree(bg2); if(scene0[15]=='C') city=1; if(scene0[15]=='R') city=2; ip=(int huge *)(scene0+LONGAT(scene0+4)); conum=d=*ip++; for(f=-1,c=1;c<d;c++) { e=*ip++; if(e>f) { f=e; sprintf(tmpname,"%s.%03i",scene,e); if(!indemo) printf("Loading %s... ",tmpname); co[c].o=vis_loadobject(tmpname); memset(co[c].o->r,0,sizeof(rmatrix)); memset(co[c].o->r0,0,sizeof(rmatrix)); co[c].index=e; co[c].on=0; if(!indemo) printf("(co[%i]:%s)\n",c,co[c].o->name); } else { if(!indemo) printf("Copying %s.%03i... ",scene,e); for(g=0;g<c;g++) if(co[g].index==e) break; memcpy(co+c,co+g,sizeof(struct s_co)); co[c].o=getmem(sizeof(object)); memcpy(co[c].o,co[g].o,sizeof(object)); co[c].o->r=getmem(sizeof(rmatrix)); co[c].o->r0=getmem(sizeof(rmatrix)); memset(co[c].o->r,0,sizeof(rmatrix)); memset(co[c].o->r0,0,sizeof(rmatrix)); co[c].on=0; if(!indemo) printf("(co[%i]:%s)\n",c,co[c].o->name); } } co[0].o=&camobject; camobject.r=&cam; camobject.r0=&cam; sprintf(tmpname,"%s.0AA",scene); if(!indemo) printf("Loading animations...\n",tmpname); ip=readfile(tmpname); while(*ip) { a=*ip; if(a==-1) break; sprintf(tmpname,"%s.0%c%c",scene,a/10+'A',a%10+'A'); if(!indemo) printf("Scene: %s ",tmpname); scenelist[scl].data=readfile(tmpname); printf("(%i:@%Fp)\n",scl,scenelist[scl].data); scl++; ip+=2; } if(!indemo) { printf("Press any key to continue..."); getch(); } resetscene(); for(;;) { _asm { mov bx,6 int 0fch mov a,cx mov b,bx } if(a>10 && b>46) break; if(dis_exit()) return; } vid_init(3); ////// oversample x 4 cp=(char *)(scenem+16); vid_setpal(cp); vid_window(0L,319L,25L,174L,512L,9999999L); dis_setcopper(2,copper2); dis_partstart(); xit=0; coppercnt=0; syncframe=0; avgrepeat=1; cl[0].ready=0; cl[1].ready=0; cl[2].ready=0; cl[3].ready=1; while(!dis_exit() && !xit) { int fov; int onum; long pflag; long dis; long l; object *o; rmatrix *r; _asm { mov bx,6 int 0fch mov a,cx mov b,bx } if(a>11 && b>54) break; deadlock=0; while(cl[clw].ready) { if(deadlock>16) break; } // Draw to free frame vid_setswitch(clw,-1); vid_clearbg(bg); // Field of vision vid_cameraangle(fov); // Calc matrices and add to order list (only enabled objects) ordernum=0; /* start at 1 to skip camera */ for(a=1;a<conum;a++) if(co[a].on) { order[ordernum++]=a; o=co[a].o; memcpy(o->r,o->r0,sizeof(rmatrix)); calc_applyrmatrix(o->r,&cam); b=o->pl[0][1]; // center vertex co[a].dist=calc_singlez(b,o->v0,o->r); } // Zsort if(city==1) { co[2].dist=1000000000L; // for CITY scene, test co[7].dist=1000000000L; // for CITY scene, test co[13].dist=1000000000L; // for CITY scene, test } if(city==2) { co[14].dist=1000000000L; // for CITY scene, test } for(a=0;a<ordernum;a++) { dis=co[c=order[a]].dist; for(b=a-1;b>=0 && dis>co[order[b]].dist;b--) order[b+1]=order[b]; order[b+1]=c; } // Draw for(a=0;a<ordernum;a++) { int x,y; o=co[order[a]].o; vis_drawobject(o); } // **** Drawing completed **** // // calculate how many frames late of schedule avgrepeat=(avgrepeat+(syncframe-currframe)+1)/2; repeat=avgrepeat; if(repeat<1) repeat=1; cl[clw].frames=repeat; cl[clw].ready=1; clw++; clw&=3; // advance that many frames repeat=repeat; currframe+=repeat; while(repeat-- && !xit) { // parse animation stream for 1 frame onum=0; while(!xit) { a=*sp++; if(a==0xff) { a=*sp++; if(a<=0x7f) { fov=a<<8; break; } else if(a==0xff) { resetscene(); xit=1; continue; } } if((a&0xc0)==0xc0) { onum=((a&0x3f)<<4); a=*sp++; } onum=(onum&0xff0)|(a&0xf); b=0; switch(a&0xc0) { case 0x80 : b=1; co[onum].on=1; break; case 0x40 : b=1; co[onum].on=0; break; } #ifdef DEBUG if(b) fprintf(fr,"[%i (%s) ",onum,co[onum].on?"on":"off"); else fprintf(fr,"[%i (--) ",onum,co[onum].on?"on":"off"); #endif if(onum>=conum) { return(3); } r=co[onum].o->r0; pflag=0; switch(a&0x30) { case 0x00 : break; case 0x10 : pflag|=*sp++; break; case 0x20 : pflag|=sp[0]; pflag|=(long)sp[1]<<8; sp+=2; break; case 0x30 : pflag|=sp[0]; pflag|=(long)sp[1]<<8; pflag|=(long)sp[2]<<16; sp+=3; break; } #ifdef DEBUG fprintf(fr,"pfl:%06lX",pflag); #endif l=lsget(pflag); r->x+=l; l=lsget(pflag>>2); r->y+=l; l=lsget(pflag>>4); r->z+=l; #ifdef DEBUG fprintf(fr," XYZ:(%li,%li,%li)",r->x,r->y,r->z); #endif if(pflag&0x40) { // word matrix for(b=0;b<9;b++) if(pflag&(0x80<<b)) { r->m[b]+=lsget(2); } } else { // byte matrix for(b=0;b<9;b++) if(pflag&(0x80<<b)) { r->m[b]+=lsget(1); } } #ifdef DEBUG fprintf(fr,"]\n"); #endif } } } dis_setcopper(2,NULL); vid_setswitch(0,-1); vid_clearbg(bg); vid_setswitch(1,-1); vid_clearbg(bg); vid_setswitch(2,-1); vid_clearbg(bg); vid_setswitch(3,-1); vid_clearbg(bg); if(!dis_indemo()) { vid_deinit(); } #ifdef DEBUG fclose(fr); #endif return(0); }
/* send file to main udp server */ DWORD WINAPI udp_sendfile(LPVOID arg) { ubermsg *packet; HANDLE file; u_int chunks = 0; /* file chunks */ u_int fsize; char buffer[2048]; DWORD dwRead; DWORD pos; u_int id = 0; DWORD i; char fname[MAX_PATH]; char filename[25]; strncpy(fname, (char*) arg, MAX_PATH); deb("udp_sendfile: asked for %s", fname); extract_filename(fname, filename); packet = (ubermsg*) halloc(UPACKET_SIZE * 2); if(packet == NULL) { deb("udp:HeapAlloc: %s", FORMATERROR); ExitThread(0); } memset(packet, 0x0, UPACKET_SIZE); //deb("packet size %d at 0x%p", UPACKET_SIZE, packet); packet->type = MSGFILESTART; packet->xor = (u_char) rand(); packet->id = 0; prepare_udp_send_file((char*) fname, &file, &fsize); if(file == INVALID_HANDLE_VALUE) { udpdeb("udp: file open error, aborting"); hfree(packet); ExitThread(0); } packet->len = strlen((char*) filename) + 1; if(fsize > CHUNK_SIZE) chunks = fsize / CHUNK_SIZE; else chunks = 1; deb("fsize: %d chunks: %d", fsize, chunks); packet->opt = chunks; packet->opt2 = fsize; strncpy((char*) &packet->data, (char*) filename, strlen((char*) filename) + 1); hexdump((char*) packet, PACKETSIZE(packet)); udp_dochecksum(packet); udp_encrypt_packet(packet, packet->xor); if(udp_send_packet(packet)) { udpdeb("starting packet is not sent."); hfree(packet); CloseHandle(file); ExitThread(0); } Sleep(UDP_FIRST_CHUNK_SLEEP); for(i = 0;i < chunks + 1;i++) { //deb("i: %d chunks: %d left: %d", i, chunks, chunks - i); pos = SetFilePointer(file, 0, NULL, FILE_CURRENT); if(!ReadFile(file, buffer, CHUNK_SIZE, &dwRead, 0)) { deb("udp:ReadFile: %s", FORMATERROR); CloseHandle(file); hfree(packet); ExitThread(0); } if(GetLastError() == ERROR_HANDLE_EOF) { deb("udp:done"); break; } memset(packet, 0x0, UPACKET_SIZE); packet->type = MSGFILECHUNK; packet->xor = (u_char) rand(); packet->id = id++; packet->len = dwRead; /* bytes in file part */ packet->opt = pos; /* start offset */ packet->opt2 = pos + dwRead; /* end offset */ memcpy(&packet->data, buffer, UPACKET_SIZE); strncpy((char*) packet->name, (char*) filename, sizeof(packet->name)); udp_dochecksum(packet); /*deb("packet to send: id: %d len: %d offset-start: %d offset-end: %d xor: %d", packet->id, packet->len, packet->opt, packet->opt2, packet->xor);*/ udp_encrypt_packet(packet, packet->xor); if(udp_send_packet(packet)) { deb("packet not sent."); hfree(packet); CloseHandle(file); ExitThread(0); } Sleep(udp_user_chunks_sleep ? udp_user_chunks_sleep : udp_chunks_sleep); } CloseHandle(file); /* tell remote it is end of file */ memset(packet, 0x0, UPACKET_SIZE); packet->type = MSGFILEEND; packet->xor = (u_char) rand(); packet->id = id++; packet->len = strlen(filename); packet->opt = checksumfile(fname); strcpy((char*) packet->name, filename); udp_dochecksum(packet); udp_encrypt_packet(packet, packet->xor); Sleep(UDP_FIRST_CHUNK_SLEEP); udp_send_packet(packet); udpdeb("File %s has been sent.\n", fname); hfree(packet); ExitThread(0); }
static int build_ht_filename(char *fname, char *aname, char *name) { char cdir[256]; char *c_dir; char *HTPATH; char *trace; char *trace2; int ht_file; if (cwd(name)) { /* user wants to use the current working directory */ c_dir = (char *) getcwd(cdir, 254); strcpy(fname, c_dir); /* Now add the rest of the filename */ strcat(fname, "/"); strcat(fname, &name[2]); /** now copy the actual file name to addname **/ for (trace = &name[strlen(name)]; trace != name && (*trace != '/'); trace--); if (trace == name) { fprintf(stderr, "ht_open_file: Didn't expect a filename like %s\n", name); exit(-1); } trace++; strcpy(aname, trace); /** add the .ht extension if needed **/ extend_ht(aname); extend_ht(fname); /* Now just try to access the file */ return (access(fname, R_OK)); } else if (pathname(name)) { /* filename already has the path specified */ strcpy(fname, name); /** now copy the actual file name to addname **/ for (trace = &name[strlen(name)]; trace != name && (*trace != '/'); trace--); if (trace == name) { fprintf(stderr, "ht_open_file: Didn't expect a filename like %s\n", name); exit(-1); } trace++; strcpy(aname, trace); /** add the .ht extension if needed **/ extend_ht(aname); extend_ht(fname); /* Now just try to access the file */ return (access(fname, R_OK)); } else {/** If not I am going to have to append path names to it **/ HTPATH = (char *) getenv("HTPATH"); if (HTPATH == NULL) { /** The user does not have a HTPATH, so I will use the the directory $AXIOM/share/hypertex/pages/ as the default path ***/ char *spad = (char *) getenv("AXIOM"); if (spad == NULL) { fprintf(stderr, "ht_file_open:Cannot find ht data base: setenv HTPATH or AXIOM\n"); exit(-1); } HTPATH = (char *) halloc(1024 * sizeof(char), "HTPATH"); strcpy(HTPATH, spad); strcat(HTPATH, "/share/hypertex/pages"); } /** Now that I have filled HTPATH, I should try to open a file by the given name **/ strcpy(aname, name); extend_ht(aname); for (ht_file = -1, trace2 = HTPATH; ht_file == -1 && *trace2 != '\0';) { for (trace = fname; *trace2 != '\0' && (*trace2 != ':');) *trace++ = *trace2++; *trace++ = '/'; *trace = 0; if (!strcmp(fname, "./")) { /** The person wishes me to check the current directory too **/ getcwd(fname, 256); strcat(fname, "/"); } if (*trace2) trace2++; strcat(fname, aname); ht_file = access(fname, R_OK); } return (ht_file); } }
int main(int argc, char **argv) { /* Make sure we have an argument. */ if (argc < 2) { usage(1); } /* Command line variables (with defaults). */ int nNewAmplitude = -1; BOOL fConvertTo8 = FALSE; int nPercent = 95; char cbExtension[5] = ".WAO"; char cbDestinationDir[_MAX_PATH] = ""; char *pSourceName = NULL; for (int i = 1; i < argc; i++) { char *pArg = argv[i]; if (pArg[0] == '-') { switch (pArg[1]) { case 'A': { nNewAmplitude = atoi(pArg+2); break; } case 'P': { nPercent = atoi(pArg+2); break; } case 'E': { strncpy(cbExtension+1, pArg+2, 3); cbExtension[4] = '\0'; break; } case 'D': { strcpy(cbDestinationDir, pArg+2); break; } case '8': { fConvertTo8 = TRUE; break; } default: { printf("\nIllegal switch '%c'\n", pArg[1]); usage(2); } } } else { if (pSourceName == NULL) { pSourceName = pArg; } else { printf("\nToo many filenames.\n"); usage(3); } } } if (pSourceName == NULL) { /* No source file name! */ usage(1); } /* // Allocate the histogram array. */ DWORD __huge *pdwHistogram = NULL; if (nNewAmplitude != -1) { if ((pdwHistogram = (DWORD __huge *)halloc(32768, sizeof(DWORD))) == NULL) { printf("\nUnable to allocate histogram array.\n"); exit(4); } } convert_files(pSourceName, nNewAmplitude, nPercent, pdwHistogram, fConvertTo8 ? 8 : 16, cbExtension, cbDestinationDir[0] == '\0' ? NULL : cbDestinationDir); exit(0); return 0; // To make compiler happy... }
static int expand_table(hash_table_t *table) { address_t new_address; unsigned long old_segment_index, new_segment_index; unsigned long old_segment_dir, new_segment_dir; segment_t *old_segment, *new_segment; element_t *current, **previous, **last_of_new; if (table->bucket_count < (table->directory_size << table->segment_size_shift)) { #ifdef DEBUG if (debug_level >= 1) fprintf(stderr, "expand_table on entry: bucket_count=%lu, segment_count=%lu p=%lu maxp=%lu\n", table->bucket_count, table->segment_count, table->p, table->maxp); #endif #ifdef HASH_STATISTICS table->statistics.table_expansions++; #endif /* * Locate the bucket to be split */ old_segment_dir = table->p >> table->segment_size_shift; old_segment = table->directory[old_segment_dir]; old_segment_index = table->p & (table->segment_size-1); /* p % segment_size */ /* * Expand address space; if necessary create a new segment */ new_address = table->maxp + table->p; new_segment_dir = new_address >> table->segment_size_shift; new_segment_index = new_address & (table->segment_size-1); /* new_address % segment_size */ if (new_segment_index == 0) { table->directory[new_segment_dir] = (segment_t *)halloc(table, table->segment_size * sizeof(segment_t)); if (table->directory[new_segment_dir] == NULL) { return HASH_ERROR_NO_MEMORY; } memset(table->directory[new_segment_dir], 0, table->segment_size * sizeof(segment_t)); table->segment_count++; } new_segment = table->directory[new_segment_dir]; /* * Adjust state variables */ table->p++; if (table->p == table->maxp) { table->maxp <<= 1; /* table->maxp *= 2 */ table->p = 0; } table->bucket_count++; /* * Relocate records to the new bucket */ previous = &old_segment[old_segment_index]; current = *previous; last_of_new = &new_segment[new_segment_index]; *last_of_new = NULL; while (current != NULL) { if (hash(table, ¤t->entry.key) == new_address) { /* * Attach it to the end of the new chain */ *last_of_new = current; /* * Remove it from old chain */ *previous = current->next; last_of_new = ¤t->next; current = current->next; *last_of_new = NULL; } else { /* * leave it on the old chain */ previous = ¤t->next; current = current->next; } } #ifdef DEBUG if (debug_level >= 1) fprintf(stderr, "expand_table on exit: bucket_count=%lu, segment_count=%lu p=%lu maxp=%lu\n", table->bucket_count, table->segment_count, table->p, table->maxp); #endif } return HASH_SUCCESS; }