📄 afflib_util.cpp
字号:
/* * utility functions used by AFFLIB. * These functions do not actually read or write data into the AFF File. *//* * Copyright (c) 2005 * Simson L. Garfinkel and Basis Technology, Inc. * All rights reserved. * * This code is derrived from software contributed by * Simson L. Garfinkel * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by Simson L. Garfinkel * and Basis Technology Corp. * 4. Neither the name of Simson Garfinkel, Basis Technology, or other * contributors to this program may be used to endorse or promote * products derived from this software without specific prior written * permission. * * THIS SOFTWARE IS PROVIDED BY SIMSON GARFINKEL, BASIS TECHNOLOGY, * AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL SIMSON GARFINKEL, BAIS TECHNOLOGy, * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */#include "config.h"#include "afflib.h"#include "afflib_i.h"/* * af_hexbuf: * Turn a binay string into a hex string, optionally with spaces. */const char *af_hexbuf(char *dst,int dst_len,const unsigned char *bin,int bytes,int flag){ int charcount = 0; const char *start = dst; // remember where the start of the string is const char *fmt = (flag & AF_HEXBUF_UPPERCASE) ? "%02X" : "%02x"; *dst = 0; // begin with null termination while(bytes>0 && dst_len > 3){ sprintf(dst,fmt,*bin); // convert the next byte dst += 2; bin += 1; dst_len -= 2; bytes--; charcount++; // how many characters bool add_spaces = false; if(flag & AF_HEXBUF_SPACE2) add_spaces = true; if((flag & AF_HEXBUF_SPACE4) && charcount%2==0){ *dst++ = ' '; *dst = '\000'; dst_len -= 1; } } return start; // return the start}/* Add commas */const char *af_commas(char buf[64],int64 val){ char tmp[64]; char t2[64]; int negative = 0; buf[0] = 0; if(val==0){ strcpy(buf,"0"); } if(val<0){ negative = 1; val = -val; } while(val>0){ int digits = val % 1000; // get the residue val = val / 1000; // and shift what's over if(val>0){ // we will still have more to do sprintf(tmp,",%03d",digits); // so pad it with zeros and a comma } else { sprintf(tmp,"%d",digits); // otherwise just get the value } strcpy(t2,buf); // copy out the buffer strcpy(buf,tmp); // copy in what we just did strcat(buf,t2); // and put back what was there } if(negative){ strcpy(t2,buf); strcpy(buf,"-"); strcat(buf,t2); } return buf;}uint64 af_decode_q(unsigned char buf[8]){ struct aff_quad *q = (struct aff_quad *)buf; // point q to buf. assert(sizeof(*q)==8); // be sure! return (((uint64)ntohl(q->high)) << 32) + ((uint64)ntohl(q->low));}/* parse the segment number. * The extra %c picks up characters that might be after the number, * so that page5_hash doesn't match for page5. */int64 af_segname_page_number(const char *name){ int64 pagenum; char ch; if(sscanf(name,AF_PAGE"%c",&pagenum,&ch)==1){ return pagenum; // new-style page number } if(sscanf(name,AF_SEG_D"%c",&pagenum,&ch)==1){ return pagenum; // old-style page number } return -1;}int64 af_segname_hash_page_number(const char *name,char *hash,int hashlen){ const char *cc = strchr((char *)name,'_'); if(!cc) return -1; // not possibly correct char *copy = strdup(name); // get a local copy char *dd = strchr(copy,'_'); if(!dd){free(copy);return -1;} // really weird; shouldn't happen *dd++ = '\000'; // terminate at _ if(strcmp(dd,"md5")!=0){free(copy);return -1;} // not a valid hash int64 page = af_segname_page_number(copy); if(page<0){free(copy);return -1;} // wasn't what we wanted strlcpy(hash,dd,hashlen); return page;}int af_hasmeta(const char *infile){ /* return 1 if the string has shell metacharacters */ for(const char *cc = infile;*cc;cc++){ switch(*cc){ case '?': return 1; case '*': return 1; case '&': return 1; case '`': return 1; case '(': return 1; case ')': return 1; } } return 0;}#ifndef HAVE_STRLCPYsize_t strlcpy(char *dest,const char *src,size_t dest_size){ strncpy(dest,src,dest_size); dest[dest_size-1] = '\000'; return strlen(dest);}#endif#ifndef HAVE_STRLCATsize_t strlcat(char *dest,const char *src,size_t dest_size){ int dest_len = strlen(dest); int src_len = strlen(src); int room = dest_size - (dest_len +src_len+1); if(room>0){ /* There is room; just copy over what we have and return */ strcat(dest,src); return strlen(dest); } /* Not room; figure out how many bytes we can copy... */ int left = dest_size - (dest_len+1); strncpy(dest+dest_len,src,left); dest[dest_len-1] = '\000'; return strlen(dest);}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -