From b45c8de31f459df57f543777b003c4d6a4f2f2c0 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Sun, 1 Jan 2023 14:48:11 -0600 Subject: [PATCH] Whitespace cleanup. --- toys/pending/git.c | 383 ++++++++++++++++++++++++--------------------- 1 file changed, 206 insertions(+), 177 deletions(-) diff --git a/toys/pending/git.c b/toys/pending/git.c index f44006b6..7e9ca13d 100644 --- a/toys/pending/git.c +++ b/toys/pending/git.c @@ -70,8 +70,7 @@ config GITCHECKOUT #include "zlib.h" //ToDo: borrowed from libz to not refactor deflate.c GLOBALS( - char *url; - char *name; + char *url, *name; struct IndexV2 *i; ) @@ -86,7 +85,8 @@ struct IndexV2 { //git inxed format v2 https://github.com/git/git/blob/master/Do char idxsha1[20]; }; -static void read_index(struct IndexV2 *i){ +static void read_index(struct IndexV2 *i) +{ FILE *fpi; i=malloc(sizeof(i)); //i->fot={ 0 }; @@ -94,99 +94,110 @@ static void read_index(struct IndexV2 *i){ i->crc=malloc(sizeof(uint32_t)); i->offset=malloc(sizeof(uint32_t)); i->offset64=malloc(sizeof(long long)); - if (access(".git/object/pack/temp.idx", F_OK)==0) {//TODO: not used yet as index is not persisted yet - fpi=fopen(".git/object/pack/temp.idx","rb");//persistance needed for other git commands (not clone) + //TODO: not used yet as index is not persisted yet + if (access(".git/object/pack/temp.idx", F_OK)==0) { + //persistance needed for other git commands (not clone) + fpi=fopen(".git/object/pack/temp.idx", "rb"); printf("read header\n"); - fread(i->header, sizeof(i->header),1,fpi); - printf("Header: %s..Read fot\n",i->header); - fread(i->fot, 4,256,fpi); - printf("Elements %d..Read sha1\n",i->fot[255]); - fread(i->sha1, sizeof(i->fot),i->fot[255],fpi); + fread(i->header, sizeof(i->header), 1, fpi); + printf("Header: %s..Read fot\n", i->header); + fread(i->fot, 4, 256, fpi); + printf("Elements %d..Read sha1\n", i->fot[255]); + fread(i->sha1, sizeof(i->fot), i->fot[255], fpi); printf("read crc\n"); - fread(i->crc, sizeof(i->fot),i->fot[255],fpi); + fread(i->crc, sizeof(i->fot), i->fot[255], fpi); printf("read offset\n"); - fread(i->offset, sizeof(i->fot),i->fot[255],fpi); + fread(i->offset, sizeof(i->fot), i->fot[255], fpi); //TODO: Offsets for file size 2G missing here printf("read packsha\n"); - fread(i->packsha1, 20,1,fpi); + fread(i->packsha1, 20, 1, fpi); printf("read idxsha\n"); - fread(i->idxsha1, 20,1,fpi); + fread(i->idxsha1, 20, 1, fpi); fclose(fpi); } } -static char *l;//for saving the insertation position +static char *l; //for saving the insertion position -int cmp (const void *i, void *j){ +int cmp (const void *i, void *j) +{ l=j; //inject inseration position in compare to binary search //printf("Compare %p %p %d\n",i,j,strncmp(i,j,20)); return strncmp(i,j,20); } //inspired by musl bsearch -long bsearchpos(const void *k, const void *a,size_t h, size_t w){ -long l=0,m=0,r=0; -if (!h) return 0; -//printf("Array: %p Key:%p\n",a,k); -while(h>0){ - m=l+(h/2); - //m=(l+h)/2; - //printf("l: %ld m:%ld h:%ld\n",l,m,h); - r=strncmp(k,a+(m*w),20); - //printf("r: %ld\n",r); - if(!r||h==1)break;//match on search or position for insert - if(r<0){h/=2;}else{l=m;h-=h/2;} - //if(r<0){h=m-1;}else{l=m+1;} -} +long bsearchpos(const void *k, const void *a,size_t h, size_t w) +{ + long l = 0, m = 0, r = 0; + if (!h) return 0; + //printf("Array: %p Key:%p\n",a,k); + while(h>0){ + m=l+(h/2); + //m=(l+h)/2; + //printf("l: %ld m:%ld h:%ld\n",l,m,h); + r=strncmp(k,a+(m*w),20); + //printf("r: %ld\n",r); + if(!r||h==1)break;//match on search or position for insert + if(r<0){h/=2;}else{l=m;h-=h/2;} + //if(r<0){h=m-1;}else{l=m+1;} + } //printf("Return m: %ld r:%ld \n",m,r); -return m+=(r>0)?1:0;//For inserts check if insert is bigger obj at identified position + //For inserts check if insert is bigger obj at identified position + return m += (r>0) ? 1 : 0; } -long get_index(struct IndexV2 *i, char *h){ - long pos=bsearchpos(h, i->sha1[0], i->fot[255], 20);//, - //(int(*)(const void*,const void*)) cmp);//TODO: Should be placed by bsearchpos() below; cmp and *l to be removed too -// for (int j=0;j<20;j++) printf("%02x",h[j]); -// printf("\n"); - //if (pos == NULL){ - // for (int h=0;hfot[255];h++){ - // printf("%d: ",h); - // for (int j=0;j<20;j++) printf("%02x",i->sha1[h][j]); - // printf("\n"); - // } - //} -// printf("index pointer: %ld\n",pos); -// printf("fot[255]: %d\n",i->fot[255]); -// printf("sha1[0] pointer: %p\n",i->sha1[0]); -// printf("offset index : %ld\n",pos); -// return i->offset[(pos-i->sha1[0])/20]; +long get_index(struct IndexV2 *i, char *h) +{ + long pos=bsearchpos(h, i->sha1[0], i->fot[255], 20); //, + + //(int(*)(const void*,const void*)) cmp);//TODO: Should be placed by bsearchpos() below; cmp and *l to be removed too + // for (int j=0;j<20;j++) printf("%02x",h[j]); + // printf("\n"); + //if (pos == NULL){ + // for (int h=0;hfot[255];h++){ + // printf("%d: ",h); + // for (int j=0;j<20;j++) printf("%02x",i->sha1[h][j]); + // printf("\n"); + // } + //} + // printf("index pointer: %ld\n",pos); + // printf("fot[255]: %d\n",i->fot[255]); + // printf("sha1[0] pointer: %p\n",i->sha1[0]); + // printf("offset index : %ld\n",pos); + // return i->offset[(pos-i->sha1[0])/20]; + return i->offset[pos]; } +//https://github.com/git/git/blob/master/Documentation/technical/pack-format.txt#L30 +//https://yqintl.alicdn.com/eef7fe4f22cc97912cee011c99d3fe5821ae9e88.png + //read type and lenght of an packed object uint64_t unpack(FILE *fpp, int *type, long int *offset) -{//https://github.com/git/git/blob/master/Documentation/technical/pack-format.txt#L30 -//https://yqintl.alicdn.com/eef7fe4f22cc97912cee011c99d3fe5821ae9e88.png +{ int bitshift= 4; uint64_t length = 0; uint8_t data; + printf("Start unpack\n"); fseek(fpp,*offset,SEEK_SET); - printf("Offset set to: %ld\n",*offset); - fread(&data, 1,1,fpp); - printf("Data: %d\n",data); + printf("Offset set to: %ld\n", *offset); + fread(&data, 1, 1, fpp); + printf("Data: %d\n", data); *type=((data & 0x70)>>4); printf("Type: %d\n",*type); - length|= (uint64_t)(data & 0x0F); + length |= (uint64_t)(data & 0x0F); //(*offset)++; - while((data & 0x80) && fread(&data, 1,1,fpp)!=-1) + while((data & 0x80) && fread(&data, 1, 1, fpp)!=-1) { length |= (uint64_t)(data & 0x7F) << bitshift; - bitshift += 7;// (*offset)++; + bitshift += 7; // (*offset)++; //printf("Offset set to: %ld\n",*offset); } //printf("Offset set to: %ld\n",*offset); - printf("Length: %ld\n",length); + printf("Length: %ld\n", length); return length; } @@ -258,26 +269,32 @@ int inf(FILE *source, char *dest) //modified signature to ease use return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR; } -long set_object(struct IndexV2 *idx,int type, char *o, uint32_t count, uint32_t ofs){ - printf("Alloc... ");//TODO: Too many allocs in here 1) to concat the search string for hashing 2) to insert into the array (can be reduce to a single malloc in fetch as the pack header contains the number of objects in pack +//https://github.com/git/git/blob/master/Documentation/gitformat-pack.txt#L72 +long set_object(struct IndexV2 *idx,int type, char *o, uint32_t count, + uint32_t ofs) +{ +// TODO: Too many allocs in here 1) to concat the search string for hashing +// 2) to insert into the array (can be reduce to a single malloc in fetch as +// the pack header contains the number of objects in pack char *c,*p="",*h=(char*)xmalloc(sizeof(char)*20);//composition,prefix,hash - if (h == NULL) error_exit("Hash malloc failed in set_object"); long pos=0; - switch(type) - {//https://github.com/git/git/blob/master/Documentation/gitformat-pack.txt#L72 - case 1:p=xmprintf("commit %d",count);break;//count is used as o can contain \0 in the string - case 2:p=xmprintf("tree %d",count);break; - case 3:p=xmprintf("blob %d",count);break; - case 4:p=xmprintf("tag %d",count);break; - case 6:printf("REF_DELTA");break;//not expected in fetch packs as fetch packs are self-containing - case 7:printf("OBJ_DELTA\n");break; + + printf("Alloc... "); + if (h == NULL) error_exit("Hash malloc failed in set_object"); + switch(type) { + case 1:p=xmprintf("commit %d",count);break;//count is used as o can contain \0 in the string + case 2: p=xmprintf("tree %d",count); break; + case 3: p=xmprintf("blob %d",count); break; + case 4: p=xmprintf("tag %d",count); break; + case 6: printf("REF_DELTA"); break; //not expected in fetch packs as fetch packs are self-containing + case 7: printf("OBJ_DELTA\n"); break; } - c=(char*)xmalloc(strlen(p)+count+2);//Robs null terminator embedding + c = (char*)xmalloc(strlen(p)+count+2); //Robs null terminator embedding if (c == NULL) error_exit("c malloc failed in set_object"); - memcpy(c,p,strlen(p)+1);//Robs null terminator embedding - memcpy(c+strlen(p)+1,o,count+1);//Robs null terminator embedding + memcpy(c,p,strlen(p)+1); //Robs null terminator embedding + memcpy(c+strlen(p)+1,o,count+1); //Robs null terminator embedding //printf("Enriched Object: %s %ld\n",c,strlen(p)+count+2); - h=SHA1(c,strlen(p)+count+1,h);//ToDo: borrowed from OpenSSL to not to pipe or refactor SHA1SUM in toybox + h = SHA1(c,strlen(p)+count+1,h); //ToDo: borrowed from OpenSSL to not to pipe or refactor SHA1SUM in toybox printf("..Binary search\n"); //printf("\nidx->fot[255]=%d\n",idx->fot[255]); //printf("idx->sha1[fot[h[0]]]=%d\n",sizeof(idx->sha1)); @@ -297,30 +314,32 @@ long set_object(struct IndexV2 *idx,int type, char *o, uint32_t count, uint32_t // }else{ // pos=(strncmp(h,idx->sha1[0],20)<0)?0:1; // } - // } + // } //printf("Binary search position: %ld, %p %p\n",pos,idx->sha1[0],l); //l=NULL; - for (int j=0;j<20;j++) printf("%02x",h[j]);//find insert position - pos=bsearchpos(h,idx->sha1[0],idx->fot[255],20); - printf("\n..Insert pos %ld\n",pos); - printf("..Preloop\n"); - for (int i=h[0];i<=255;i++) + for (int j = 0; j<20; j++) printf("%02x",h[j]); //find insert position + pos = bsearchpos(h,idx->sha1[0],idx->fot[255],20); + printf("\n..Insert pos %ld\n",pos); + printf("..Preloop\n"); + for (int i = h[0]; i<=255; i++) idx->fot[i]+=1; //adjust of fanout table https://github.com/git/git/blob/master/Documentation/technical/pack-format.txt#L179 printf("Post loop\n"); printf("Resize sha1 array..idx->fot[255]%d\n",idx->fot[255]); //Memory management for insert TODO:Could be also a single malloc at gitfetch based on the nbr of objects in pack - idx->sha1=realloc(idx->sha1,(idx->fot[255]+1)*20*sizeof(char));//Did not fix the TODO yet, because set_object could be reused for other command im mem mgmt is here + + //Did not fix the TODO yet, because set_object could be reused for other command im mem mgmt is here + idx->sha1 = realloc(idx->sha1,(idx->fot[255]+1)*20*sizeof(char)); printf("Mem copy sha1 array..sizeof(idx->sha1)%ld\n",sizeof(idx->sha1)); memmove(&idx->sha1[pos+1],&idx->sha1[pos],(idx->fot[255]-pos)*20*sizeof(char)); printf("Resize offset\n"); - idx->offset=realloc(idx->offset,(idx->fot[255]+1)*sizeof(uint32_t)); + idx->offset = realloc(idx->offset, (idx->fot[255]+1)*sizeof(uint32_t)); printf("Mem copy offset\n"); - memmove(&idx->offset[pos+1],&idx->offset[pos],sizeof(uint32_t)*(idx->fot[255]-pos)); + memmove(&idx->offset[pos+1], &idx->offset[pos], sizeof(uint32_t)*(idx->fot[255]-pos)); printf("Set offset value\n"); - memcpy(&idx->sha1[pos],h,20);//insert SHA1 - idx->offset[pos]=ofs;//insert offset of SHA1 + memcpy(&idx->sha1[pos], h, 20);//insert SHA1 + idx->offset[pos] = ofs;//insert offset of SHA1 //ToDo: id->crc[idx->fot[h[0]]]=; printf("Write object\n"); - // printf("SetGet %d %ld\n :",idx->offset[pos],get_index(idx,h)); + // printf("SetGet %d %ld\n :",idx->offset[pos],get_index(idx,h)); //writeObject to local pack; // for (int h=0;hfot[255];h++){ // for (int j=0;j<20;j++) printf("%02x",idx->sha1[h][j]); @@ -328,44 +347,46 @@ long set_object(struct IndexV2 *idx,int type, char *o, uint32_t count, uint32_t // } free(h); free(c); + return ofs; } static void gitinit(char *name) { - if (mkdir(name,0755)!=0){//For git clone actually only refs and object/pack needed - mkdir(xmprintf("%s%s",name,"/.git"),0755);//I create the other for a git compliant folder structure - mkdir(xmprintf("%s%s",name,"/.git/objects"),0755); - mkdir(xmprintf("%s%s",name,"/.git/objects/pack"),0755); - mkdir(xmprintf("%s%s",name,"/.git/branches"),0755); - mkdir(xmprintf("%s%s",name,"/.git/hooks"),0755);//hook files skipped as implementations does not support hooks - mkdir(xmprintf("%s%s",name,"/.git/info"),0755); - mkdir(xmprintf("%s%s",name,"/.git/objects/info"),0755); - mkdir(xmprintf("%s%s",name,"/.git/refs"),0755); - mkdir(xmprintf("%s%s",name,"/.git/heads"),0755); - mkdir(xmprintf("%s%s",name,"/.git/tags"),0755); - xcreate(xmprintf("%s%s",name,"/.git/config"),O_CREAT,0644); - xcreate(xmprintf("%s%s",name,"/.git/description"),O_CREAT,0644); - xcreate(xmprintf("%s%s",name,"/.git/HEAD"),O_CREAT,0644); - xcreate(xmprintf("%s%s",name,"/.git/info/exclude"),O_CREAT,0644); + //For git clone actually only refs and object/pack needed + if (mkdir(name,0755)!=0){ + mkdir(xmprintf("%s%s", name, "/.git"), 0755);//I create the other for a git compliant folder structure + mkdir(xmprintf("%s%s", name, "/.git/objects"), 0755); + mkdir(xmprintf("%s%s", name, "/.git/objects/pack"), 0755); + mkdir(xmprintf("%s%s", name, "/.git/branches"), 0755); + mkdir(xmprintf("%s%s", name, "/.git/hooks"), 0755);//hook files skipped as implementations does not support hooks + mkdir(xmprintf("%s%s", name, "/.git/info"), 0755); + mkdir(xmprintf("%s%s", name, "/.git/objects/info"), 0755); + mkdir(xmprintf("%s%s", name, "/.git/refs"), 0755); + mkdir(xmprintf("%s%s", name, "/.git/heads"), 0755); + mkdir(xmprintf("%s%s", name, "/.git/tags"), 0755); + xcreate(xmprintf("%s%s", name, "/.git/config"), O_CREAT, 0644); + xcreate(xmprintf("%s%s", name, "/.git/description"), O_CREAT, 0644); + xcreate(xmprintf("%s%s", name, "/.git/HEAD"), O_CREAT, 0644); + xcreate(xmprintf("%s%s", name, "/.git/info/exclude"), O_CREAT, 0644); } } static void gitremote(char *url) { if (access(".git/config", F_OK)!=0) { - FILE *fp; - fp=fopen(".git/config","wb"); - fwrite("[core]\n",1,7,fp); - fwrite("\trepositoryformatversion = 0\n",1,29,fp); - fwrite("\tfilemode = false\n",1,18,fp); - fwrite("\tbare = false\n",1,14,fp); - fwrite("\tlogallrefupdates = true\n",1,25,fp); - fwrite("\tsymlinks = false\n",1,18,fp); - fwrite("\tignorecase = true\n",1,19,fp); - fwrite("[remote \"origin\"]\n",1,18,fp); - fwrite(xmprintf("\turl = %s/refs\n",TT.url),1,strlen(TT.url)+13,fp); - fwrite("\tfetch = +ref/heads/*:refs/remotes/origin/*\n",1,44,fp); + FILE *fp = fopen(".git/config","wb"); + + fwrite("[core]\n", 1, 7, fp); + fwrite("\trepositoryformatversion = 0\n", 1, 29, fp); + fwrite("\tfilemode = false\n", 1, 18, fp); + fwrite("\tbare = false\n", 1, 14, fp); + fwrite("\tlogallrefupdates = true\n", 1, 25, fp); + fwrite("\tsymlinks = false\n", 1, 18, fp); + fwrite("\tignorecase = true\n", 1 ,19, fp); + fwrite("[remote \"origin\"]\n", 1 ,18, fp); + fwrite(xmprintf("\turl = %s/refs\n",TT.url), 1, strlen(TT.url)+13, fp); + fwrite("\tfetch = +ref/heads/*:refs/remotes/origin/*\n", 1, 44, fp); fclose(fp); } } @@ -373,7 +394,8 @@ static void gitremote(char *url) //this is most likely still buggy and create a late observable heap overflow larger deltafied repos char* resolve_delta(char *s, char *d, long dsize, uint32_t *count) { //https://stackoverflow.com/a/14303988 - long pos=0,bitshift=0; + long pos=0, bitshift=0; + printf("Original Source: \n"); //for (int k=0;k<*count;k++){printf("%c",s[k]);} //printf("\n"); @@ -382,116 +404,123 @@ char* resolve_delta(char *s, char *d, long dsize, uint32_t *count) //printf("\n"); //https://github.com/git/git/blob/master/Documentation/technical/pack-format.txt#L88 - while((d[pos] & 0x80))pos++;// Skipping source size; did not find out why it is on the delta header as the source object header contains it too; maybe misunderstood and this makes things buggy, but I dont need it here + while((d[pos] & 0x80)) pos++; // Skipping source size; did not find out why it is on the delta header as the source object header contains it too; maybe misunderstood and this makes things buggy, but I dont need it here //{ - // ssize |= (uint64_t)(d[pos++] & 0x7F) << bitshift; - // bitshift += 7;// (*offset)++; + // ssize |= (uint64_t)(d[pos++] & 0x7F) << bitshift; + // bitshift += 7;// (*offset)++; //} - pos++;//fixes https://github.com/git/git/blob/master/Documentation/technical/pack-format.txt#L67 - *count=0; - bitshift=0; - while((d[pos] & 0x80))//reading target_size from header - { + pos++; //fixes https://github.com/git/git/blob/master/Documentation/technical/pack-format.txt#L67 + *count = 0; + bitshift = 0; + while((d[pos] & 0x80)) { //reading target_size from header *count |= (uint64_t)(d[pos++]& 0x7F) << bitshift; - bitshift += 7;// (*offset)++; + bitshift += 7; // (*offset)++; } *count |= (uint64_t)(d[pos++]& 0x7F) << bitshift; - printf("Target Count %d:\n",*count); - char *t=malloc(sizeof(char)*(*count+1)); + printf("Target Count %d:\n", *count); + char *t = malloc(sizeof(char)*(*count+1)); if (t == NULL) error_exit("t malloc failed in resolve_delta"); - *count=0; - while(posfot[255];h++){ - // for (int j=0;j<20;j++) printf("%02x",i->sha1[h][j]); + //printf("Print source: %s\n", source); + //for (int h = 0; hfot[255]; h++){ + // for (int j = 0; j<20; j++) printf("%02x", i->sha1[h][j]); // printf("\n"); //} free(h); - return resolve_delta(source,object,dcount,count);//recursion due to https://github.com/git/git/blob/master/Documentation/technical/pack-format.txt#L83 + + // recursion due to https://github.com/git/git/blob/master/Documentation/technical/pack-format.txt#L83 + return resolve_delta(source,object,dcount,count); } else { - printf("Type Else:\n"); - inf(fpp,object); - *count=dcount; - printf("Type Else end:\n"); - //printf("Unpacked Object: %s\n",object); - return object; + printf("Type Else:\n"); + inf(fpp, object); + *count = dcount; + printf("Type Else end:\n"); + //printf("Unpacked Object: %s\n", object); + + return object; } } -- 2.39.2