int handle_client(t_selfd *fd, t_server *serv) { char *cmd; int r; ssize_t swr; if (ISREADABLE(fd)) { if (((r = read_from_client(fd)) < 0 && errno != EINTR) || (r == 0)) { log_connection(((t_client *)fd->data)->sock, "Client disconnected from:"); return (destroy_connection(serv, fd)); } } if (ISWRITEABLE(fd) && (r = write_to_client(fd)) < 0 && errno != EINTR) return (destroy_connection(serv, fd)); while ((cmd = get_command(fd))) handle_add_cmd(serv, fd, cmd); swr = ring_buffer_left_read(fd->wbuff); if (!swr && fd->to_close) return (destroy_connection(serv, fd)); if (swr) CHECKWRITE(fd); CHECKREAD(fd); push_instruction(serv, fd); return (0); }
int handle_newconnection(t_selfd *fd, t_server *serv) { t_net *bind_sock; t_net *nsock; t_selfd *tmpfd; t_client *client; CHECKREAD(fd); if (!ISREADABLE(fd)) return (EXIT_FAILURE); bind_sock = (t_net*)fd->data; if (!(nsock = accept_connection(bind_sock))) return (EXIT_FAILURE); if ((!(client = malloc(sizeof(t_client)))) || !(tmpfd = create_fd(nsock->socket, client, &handle_client))) { free(client); close_connection(nsock); return (EXIT_FAILURE); } nsock->peer = peer(nsock); client->sock = nsock; log_connection(nsock, "New connection from:"); return (init_new_client(serv, tmpfd, client)); }
/*------------------------------------------------------------------- * MakeAtabIndex 09.06.90 GH * * Reads AUTHTAB.DIR from buffer ATABbuf and fills in the * atabdir struct with stripped authornames and filenames. * Also fills in the anums_index array. This routine * replaces both of the olld MakeAtabIndex and MakeAnumIndex * returns the total author count. * * last modified : 06.14.93 GH *------------------------------------------------------------------- */ int MakeAtabIndex( void ) { int buf_index=0,auth_count=0,auth_num=0; char tmp_auth_name[MAX_AUTHNAME_LEN]; BYTE *ATABbuf; FILE *TLGatab; ATABbuf = (BYTE *) TLGmalloc( ATABBUFSIZE*sizeof(BYTE) ); if(!(TLGatab = OpenTLG( "AUTHTAB.DIR" ))) SYSTEMCRASH(EXIT_BADOPEN); CHECKREAD(ATABbuf, sizeof(BYTE), ATABBUFSIZE, TLGatab ); fclose( TLGatab ); /* Create the auth_nums array */ auth_nums = ( int * )TLGmalloc( MAX_AUTH_NUM * sizeof(int)); /* First pass we get the first filename outside of loop */ ATAB_FILE(auth_count) = (char *)TLGmalloc(MAX_FILENAME_LEN+1); buf_index = GetFileName(ATAB_FILE(auth_count),buf_index, ATABbuf); while( stricmp( ATAB_FILE(auth_count),"*END" ) !=0 ) { if( auth_count >= MAX_AUTH ) SYSTEMCRASH(EXIT_ATABMAXAUTH ); /* fill in the auth_nums entry for this author/pass */ if(( ATAB_FILE(auth_count)[0] != '*' ) && ( strnicmp( ATAB_FILE(auth_count),"DOCCAN",6)!=0)) { auth_num = FileNametoNumber(ATAB_FILE(auth_count)); if(( auth_num > MAX_AUTH_NUM ) || ( auth_num <= 0 )) SYSTEMCRASH( EXIT_ATABAUTHNUM ); auth_nums[auth_num]=auth_count; } /* Get author_name for this author/pass */ buf_index = GetAuthName( buf_index, tmp_auth_name, ATABbuf ); ATAB_AUTH(auth_count) = ( char * )TLGmalloc( strlen( tmp_auth_name)+1); strncpy( ATAB_AUTH(auth_count),tmp_auth_name, strlen(tmp_auth_name)+1); /* Skip to beginning of next record ( author ) */ while( ATABbuf[buf_index] != ATAB_RECORD_END ) buf_index++; while( ATABbuf[buf_index] == ATAB_RECORD_END ) buf_index++; /* update auth_count and get filename for next pass */ auth_count++; ATAB_FILE(auth_count) = (char *)TLGmalloc(MAX_FILENAME_LEN+1); buf_index = GetFileName(ATAB_FILE(auth_count),buf_index, ATABbuf); if( buf_index == 0x00 ) break; } /* put the author_count into a static so we can get at it later */ ATAB_AUTH_COUNT = --auth_count; /* and return it just to make everyone happy */ TLGfree( ATABbuf ); return(auth_count); }/*
/*------------------------------------------------------------------- * CanBibGetBlock 01.22.90 GH * * last modfied 07/31/93 *------------------------------------------------------------------- */ int CanBibGetBlock( long block_num, BYTE *text_index[], BYTE *cit_index[] ) { int line_count; long seek_pos; BYTE *buf_ptr = canbib_buf; seek_pos = ( block_num * (long) TLGBLOCKSIZE ); fseek( TLGcantxt, seek_pos, SEEK_SET ); CHECKREAD( buf_ptr,sizeof(BYTE),TLGBLOCKSIZE, TLGcantxt ); line_count = IndexBlock( buf_ptr, text_index, cit_index ); if( line_count >= CANBIB_BLK_MAX_LINES ) SYSTEMCRASH( EXIT_BLOCKTOOBIG ); return( line_count ); }
int main(int argc, char* argv[]) { int opt; char* outdir = NULL; bool orphaned_check = false, recreate = false, quiet = false; while((opt = getopt(argc,argv,"roqd:hD")) != -1) switch(opt) { case 'o': orphaned_check = true; break; case 'r': recreate = true; break; case 'd': outdir = optarg; break; case 'q': quiet = true; break; case 'D': puts(doc); return 0; case 'h': usage(argv[0],1); default : usage(argv[0],0); } if(argc - optind != 1) usage(argv[0],0); if(recreate && !outdir) outdir = "."; const char* browsefile = argv[optind]; FILE* f = fopen(browsefile,"rb"); if(!f) bail("%s could not be read.\n",browsefile); char buf[15]; CHECKREAD(buf,1,15,f); if(memcmp(buf,magic,15)) bail("%s: Not a valid JBF.\n",browsefile); uint16_t version[2]; READBE(version,2,2,f); uint32_t nb_images; READLE(&nb_images,4,1,f); char path[256]; CHECKREAD(path,1,256,f); path[255] = '\0'; char volume[32]; CHECKREAD(volume,1,32,f); volume[31] = '\0'; fseek(f,713,SEEK_CUR); // End header if(!quiet) { printf("JBF version %d.%d (PSP %s)\n",version[0],version[1],ver_lookup(version[0],version[1])); if(volume[0]) printf("[%s] ",volume); printf("%s\n%d images\n",path,nb_images); puts(" type resolution bpp size modified name"); } size_t outdir_len; if(outdir) outdir_len = strlen(outdir); if(recreate) { outdir_len += strlen(path); outdir = strcpy(malloc(outdir_len+1),outdir); char* pp = path,* op = outdir + strlen(outdir); *op++ = '/'; do switch(*pp) { case '\\': *op++ = '/'; break; case '/' : *op++ = '\\'; break; case ':' : break; default : *op++ = *pp; } while(*pp++); } char* browsedir; size_t browsedir_len; if(orphaned_check) { char* browsecpy = strdup(browsefile); browsedir = strdup(dirname(browsecpy)); browsedir_len = strlen(browsedir); free(browsecpy); } for(uint32_t i = 0; i < nb_images; i++) { uint32_t name_len; if(version[0] == 1 && version[1] == 0) name_len = 13; else READLE(&name_len,4,1,f); if(name_len > 255) //Windows maximum, image names from valid JBFs shouldn't exceed this bail("%s+%lx: Filename too long for image #%"PRIu32" (%"PRIu32")\n",browsefile,ftell(f)-4,i,name_len); char name[256]; READLE(name,1,name_len,f); name[name_len] = '\0'; if(version[0] == 1 && version[1] == 0) name_len--; uint32_t type, width, height, depth, pels, filesize; time_t epochtime; char type_v1[4] = {'\0'}; if(version[0] > 1) { uint64_t filetime; READLE(&filetime,8,1,f); epochtime = MSFILETIME_TO_EPOCHTIME(filetime); READLE(&type,4,1,f); } else if(version[1] < 3) { //1.3 has no type code READBE(type_v1,3,1,f); CHECKREAD(type_v1+3,1,1,f); // \0 } READLE(&width,4,1,f); READLE(&height,4,1,f); READLE(&depth,4,1,f); if(version[0] == 2) READLE(&pels,4,1,f); // Still not sure what this field is for, roughly equals w*h*channels READLE(&filesize,4,1,f); if(version[0] == 1) { uint32_t filetime; READLE(&filetime,4,1,f); epochtime = filetime; } // Truncated file entries contain a single null word in place of the '0x00000002 0x00000001 0xFFFFFFFF' pattern uint32_t has_thumb = true; if(version[0] == 2) READLE(&has_thumb,4,1,f); bool orphaned = false; if(orphaned_check) { char test[browsedir_len+name_len+2]; sprintf(test,"%s/%s",browsedir,name); orphaned = access(test,F_OK); } bool extract = outdir && (!orphaned_check || orphaned); if(!quiet) printf("%c%6s %6"PRIu32"x%-6"PRIu32" %2"PRIu32" %10"PRIu32" %.20s %s %s %s\n", (has_thumb?' ':'-'),version[0]<2?type_v1:type_lookup(type),width,height,depth,filesize,ctime(&epochtime)+4,(orphaned?"\033[1;31m":""),name,(orphaned?"\033[0m":"")); if(!has_thumb) continue; if(extract && recreate) { if(!mkdir_r(outdir)) bail("%s: Couldn't recreate path: %s\n%s\n",browsefile,outdir,strerror(errno)); #if !defined(_WIN32) || (WINVER >= 0x0600) // place a symlink back to the source jbf char* browsecpy = strdup(browsefile); char* browsebase = basename(browsecpy); free(browsecpy); char linkback[outdir_len+strlen(browsebase)+1]; strcat(strcpy(linkback,outdir),browsebase); char* browseabs = realpath(browsefile,NULL); symlink(browseabs,linkback); free(browseabs); #endif recreate = false; } if(version[0] == 2) { uint32_t sig[3] = {has_thumb}; READLE(sig+1,4,2,f); if(memcmp(sig,v2_thumb_signature,sizeof(v2_thumb_signature))) bail("%s+%lx: Wrong signature (%08"PRIX32" %08"PRIX32" %08"PRIX32"); parse integrity lost.\n",browsefile,ftell(f)-3,sig[0],sig[1],sig[2]); uint32_t imglen; READLE(&imglen,4,1,f); if(extract) { char writepath[outdir_len+name_len+6]; sprintf(writepath,"%s/%s.jpg",outdir,name); FILE* thumb = fopen(writepath,"wb"); if(thumb) { char* buf = malloc(imglen); if(!buf) bail("%s+%lx: Allocation failed for image #%"PRIu32", length %"PRIu32"\n",browsefile,ftell(f)-4,i,imglen); imglen = fread(buf,1,imglen,f); // If the file is truncated for any reason we will write a partial JPEG and fail the next time around if(fwrite(buf,1,imglen,thumb) != imglen) fprintf(stderr,"Error writing to %s\n",writepath); fclose(thumb); free(buf); utime(writepath,&(struct utimbuf){epochtime,epochtime}); continue; } fprintf(stderr,"Unable to open %s\n",writepath); } fseek(f,imglen,SEEK_CUR); }
long SearchWordList( WORD word , WORD *word_info[], BOOL keep_text ) { extern FILE *TLGwlist; extern char diacrit[]; int match_len=0,ncb=0,block_cnt=0; long words_fnd=0; BYTE *block,*block_index,*word_beg,*last_pos; BOOL srch_abort = FALSE; block = (BYTE *) TLGmalloc( TLGBLOCKSIZE ); block_cnt = word.word_loc.end_wlblock - word.word_loc.start_wlblock; do /* Loop over all necessary blocks */ { if(srch_abort = CheckForEscape()) goto short_exit; CHECKREAD( block, TLGBLOCKSIZE, 1, TLGwlist); block_index = block; last_pos = block; while( (word_beg = MatchPattern( (BYTE *) word.word_text->m_text,last_pos, TLGBLOCKSIZE - ( last_pos - block ) )) && ((last_pos-block) < TLGBLOCKSIZE) ) { if(srch_abort=CheckForEscape()) goto short_exit; /* Bail out if we need the full word and this ain't it */ if( LetterVal(word.word_text->m_text[0]) && !HIBITSET(word_beg[-1]) ) { last_pos = (BYTE *) end_of_patt; continue; } if( !HIBITSET(*end_of_patt) ) { last_pos = (BYTE *) end_of_patt; continue; } if( words_fnd >= WORDARRAYSIZE ) goto short_exit; /* * Get whole word and character count */ CURRWORD = (WORD *) TLGmalloc(sizeof(WORD)); while(!HIBITSET(word_beg[-1])) word_beg--; block_index = word_beg; match_len=0; if(keep_text) { CURRWORD->word_text = (WORDTEXT *) TLGmalloc( sizeof(WORDTEXT) ); while( !HIBITSET(*block_index) ) CURRWORD->word_text->text[match_len++] = *block_index++; CURRWORD->word_text->text[match_len] = '\0'; MassageWord( CURRWORD->word_text->text,CURRWORD->word_text->m_text); } block_index = word_beg-1; /* * Find the number of word occurences from count bytes. */ ncb = 0; while( HIBITSET(*block_index) ) { ncb++; block_index--; } CURRWORD->total = BitVal7xN((BYTE *)block_index+1,ncb); CURRWORD->word_loc.start_wlblock = word.word_loc.start_wlblock; CURRWORD->word_loc.WordNum = CountWords((BYTE *)block, (BYTE *)word_beg); words_fnd++; word_beg+=match_len; last_pos = word_beg; while(!HIBITSET(*last_pos) && ((*last_pos) != 0) ) last_pos++;; }/*END WHILE Strsrch finds match*/ word.word_loc.start_wlblock++; } while( block_cnt-- ); short_exit: match_count = words_fnd; if(srch_abort) { FreeWordList(); words_fnd = -1; } TLGfree(block); return(words_fnd); }/*