Dur DurParse(char *s, char *stem) { if (streq(stem, "sec")) { return((Dur)FloatParse(s, -1)); } else if (streq(stem, "min")) { return((Dur)60.0*FloatParse(s, -1)); } else if (streq(stem, "hrs")) { return((Dur)3600.0*FloatParse(s, -1)); } else if (streq(stem, "day")) { return((Dur)24.0*3600.0*FloatParse(s, -1)); } else if (streq(stem, "yrs")) { return((Dur)DAYSPERYEAR*24.0*3600.0*FloatParse(s, -1)); } else if (streq(stem, "hhmm")) { return(IntParse(s, 2)*3600L + IntParse(s+2, 2)*60L); } else if (streq(stem, "hhmmss")) { return(IntParse(s, 2)*3600L + IntParse(s+2, 2)*60L + (long)IntParse(s+4, 2)); } else if (streq(stem, "ns")) { return((Dur)0); /* todo */ } else if (streq(stem, "ms")) { return((Dur)0); /* todo */ } else { Dbg(DBGGEN, DBGBAD, "DurParse: trouble parsing <%s> <%s>", s, stem); return(DURERR); } }
Tod TodParse(char *s) { int len; if (streq(s, "na")) return(TODNA); len = strlen(s); switch (len) { case 2: return(atoi(s)*3600L); case 4: return(IntParse(s, 2)*3600L + IntParse(s+2, 2)*60L); case 6: return(IntParse(s, 2)*3600L + IntParse(s+2, 2)*60L + (long)IntParse(s+4, 2)); default: Dbg(DBGGEN, DBGBAD, "TodParse: trouble parsing <%s>", s); return(TODERR); } }
Bool TsParse(Ts *ts, char *s) { int len; ts->flag = 0; ts->cx = ContextRoot; ts->unixts = UNIXTSNA; /* REDUNDANT */ if (streq(s, "na") || streq(s, "?")) { ts->unixts = UNIXTSNA; return(1); } else if (streq(s, "Inf") || streq(s, "+Inf")) { ts->unixts = UNIXTSPOSINF; return(1); } else if (streq(s, "-Inf")) { ts->unixts = UNIXTSNEGINF; return(1); } len = strlen(s); switch (len) { case 4: /* YYYY */ ts->unixts = YMDHMSToUnixTs(atoi(s), 1, 1, 0, 0, 0); break; case 6: /* YYYYMM */ ts->unixts = YMDHMSToUnixTs(IntParse(s, 4), IntParse(s+4, 2), 1, 0, 0, 0); break; case 8: /* YYYYMMDD */ ts->unixts = YMDHMSToUnixTs(IntParse(s, 4), IntParse(s+4, 2), IntParse(s+6, 2), 0, 0, 0); break; case 15: /* YYYYMMDDTHHMMSS */ /* todo: time zones */ ts->unixts = YMDHMSToUnixTs(IntParse(s, 4), IntParse(s+4, 2), IntParse(s+6, 2), IntParse(s+9, 2), IntParse(s+11, 2), IntParse(s+13, 2)); break; case 16: /* YYYYMMDDTHHMMSSZ */ /* todo: time zones */ ts->unixts = YMDHMSToUnixTs(IntParse(s, 4), IntParse(s+4, 2), IntParse(s+6, 2), IntParse(s+9, 2), IntParse(s+11, 2), IntParse(s+13, 2)); break; case 20: /* YYYYMMDDTHHMMSS-HHMM */ /* todo: time zones */ ts->unixts = YMDHMSToUnixTs(IntParse(s, 4), IntParse(s+4, 2), IntParse(s+6, 2), IntParse(s+9, 2), IntParse(s+11, 2), IntParse(s+13, 2)); break; default: Dbg(DBGGEN, DBGBAD, "TsParse: trouble parsing <%s>", s); ts->unixts = UNIXTSNA; return(0); } return(1); }
int SaveXrefs(FILE *fp, int index, int gap, int from, int to, int newStartxref,int boundary) { List writer = InitList(); LPSTR str = ""; int cursor,i,len; Xref xref; for (cursor = from; cursor < to; cursor++) { //xref之前 xref=(Xref)GetNodeData(xrefs,cursor); while (index < xref->xrefOffset) { fputc(PDFInByte[index++],fp); } //xref地址部分 while (PDFInByte[index] != 13 && PDFInByte[index] != 10) fputc(PDFInByte[index++],fp); while (PDFInByte[index] == 13 || PDFInByte[index] == 10) fputc(PDFInByte[index++],fp); len=xref->sections->length; Section sec; for (i=0;i<len;i++) { int count,j; sec=(Section)GetNodeData(xref->sections,i); while (PDFInByte[index] != 13 && PDFInByte[index] != 10) fputc(PDFInByte[index++],fp); while (PDFInByte[index] == 13 || PDFInByte[index] == 10) fputc(PDFInByte[index++],fp); for (count = 0; count < sec->count; count++) { char Addr[11]; for (j = 0; j < 10; j++) { Addr[j] = PDFInByte[index + j]; } Addr[j]=0; int num=IntParse(Addr); if (num > boundary) { num += gap; for (int j = 0; j < 10; j++) { Addr[9 - j] = num % 10 + 48; num /= 10; } } fputs(Addr,fp); index += 10; for (j = 0; j < 10; j++) { fputc(PDFInByte[index++],fp); } } } //trailer的处理 while (PDFInByte[index] != 62 || PDFInByte[index + 1] != 62) { if (PDFInByte[index] == 80 && PDFInByte[index + 1] == 114 && PDFInByte[index + 2] == 101 && PDFInByte[index + 3] == 118)//Prev { fputs("Prev ",fp); index +=(int)strlen("Prev "); str=IntToString(xref->newPrev); fputs(str,fp); index += (int)strlen(str); continue; } if (PDFInByte[index] == 88 && PDFInByte[index + 1] == 82 && PDFInByte[index + 2] == 101 && PDFInByte[index + 3] == 102)//XRef { writer=InitList(); int len = (int)strlen("XRefStm ")+(int)strlen(IntToString(xref->trailer->XRefStm)); index += len; fputs("DelTag ",fp); for (i = 0; i < len - 7; i++)fputc('0',fp); continue; } fputc(PDFInByte[index++],fp); }//结束时index指向>>的第一个> //看是否有startxref if (cursor == xrefs->length - 1) { int see = index + 2; while (PDFInByte[see] == 13 || PDFInByte[see] == 10) see++; //结束时指向下一行第一个字符 //若有start if (PDFInByte[see] == 115 && PDFInByte[see + 1] == 116 && PDFInByte[see + 2] == 97 && PDFInByte[see + 3] == 114 && PDFInByte[see + 4] == 116) { while (PDFInByte[see] < 48 || PDFInByte[see] > 57) see++;//see定位在第一个数字 while (index < see) fputc(PDFInByte[index++],fp);//结束时index指向第一个数字 str = IntToString(newStartxref); fputs(str,fp); index += (int)strlen(str); if (cursor == xrefs->length- 1) { while (index < FileLength) { fputc(PDFInByte[index++],fp); } break; } } } } return index; }
//读取PDF的信息 void ReadPath(char *PDFPath) { FILE *fp; if((fp=fopen(PDFPath,"rb"))==0) { printf("cannot open %s\n",PDFPath); } Title=L""; Author=L""; Subject=L""; Keywords=L""; CreationDate=L""; ModDate=L""; Creator=L""; Producer=L""; xrefs =InitList(); Tails=InitList(); Attrs=InitList(); FileLength=getFileSize(PDFPath); printf("FileLength: %d\n",FileLength); PDFInByte=(unsigned char*)malloc(sizeof(char)*FileLength); fread(PDFInByte,1,FileLength,fp); fclose(fp); Version[0]=PDFInByte[5]; Version[1]= '.' ; Version[2]=PDFInByte[7]; Version[3]=0; printf("Version: %s\n",Version); int index = FileLength; index = findStringUp(PDFInByte,FileLength,index, "%%EOF") - 5; while (PDFInByte[index] > 57 || PDFInByte[index] < 48) index--; int i = 1,j; StartXref = 0; while (PDFInByte[index] <= 57 && PDFInByte[index] >= 48) { StartXref += (PDFInByte[index--] - 48) * i; i *= 10; } printf("StartXref: %d\n",StartXref); ReadXref(StartXref); LPSTR* Addrs=(LPSTR*)malloc(sizeof(char*)*Size); Xref xref; Section section; for(i=0;i<xrefs->length;i++) { xref=(Xref)GetNodeData(xrefs,i); for(j=0;j< xref->sections->length;j++) { section=(Section)GetNodeData(xref->sections,j); for (int cursor = 0; cursor < section->count; cursor++) { Addrs[section->startID+cursor]=(LPSTR)GetNodeData(section->Addrs,cursor); //用新的覆盖旧的 } } } //读取Info if (Info == 0) return; index=InfoAt = IntParse(Substring(Addrs[Info],0, 10)); while (PDFInByte[index++] != 47) ; //找到第一个/,结束时index指向tag的第一个字符 List tag = InitList(); LPSTR tagString; LPWSTR valueString; valueString=(LPWSTR)malloc(sizeof(wchar_t)); while (PDFInByte[index] != 62)//> { //tag.Clear(); tag = InitList(); while (PDFInByte[index] != 32 && PDFInByte[index] != 40) //找到空格或者(,结束时index指向空格或( { AddNode(tag,(void*)PDFInByte[index]); index++; } index = omitBlank(PDFInByte,index); tagString =CharListGetString(tag); //开始读取value if (PDFInByte[index] != 40) //指向其他obj的情况 { int tagID = getNumber(PDFInByte, &index); index++; while (PDFInByte[index] != 47 && PDFInByte[index] != 62) index++; //找下一个/或者结束符>,结束时index指向下一个/或结束符> char *tagAddr = Substring(Addrs[tagID],0, 10); int cursor = IntParse(tagAddr); while (PDFInByte[cursor++] != 40) ; //找(,结束时指向属性的第一个字符 ReadValue(cursor,&valueString); } else //跟随的括号中有属性的情况 { index = ReadValue(++index, &valueString) + 1; //index此时指向)后面一个字符 while (PDFInByte[index] != 47 && PDFInByte[index] != 62) index++; //找下一个/或者结束符>,结束时index指向下一个/或结束符> } index++; //index最终定位在下一个tag的第一个字符或结束符> int TagNum=-1,i; for(i=0;i<8;i++) { if(strcmp(tagString,InfoTags[i])==0) { TagNum=i; break; } } switch (TagNum) { case 0: Title = valueString; break;//Title case 1: Author = valueString; break;//Author case 2: Subject = valueString; break;//Subject case 3: Keywords = valueString; break;//Keywords case 4: CreationDate = valueString; break; case 5: ModDate =valueString; break;//ModDate case 6: Creator = valueString; break;//Creator case 7: Producer = valueString; break;//Producer default: Attr attr=(Attr)malloc(sizeof(_Attr)); attr->Name=tagString; attr->Value=valueString; AddNode(Attrs,attr); break; } } while (PDFInByte[index] != 106) index++;//将index指向endobj的j InfoEnd = index + 1; //读取Tail if (Tail == 0) return; index=TailAt = IntParse(Substring(Addrs[Tail],0, 10)); while (PDFInByte[index++] != 47) ; //找到第一个/,结束时index指向tag的第一个字符 while (PDFInByte[index] != 62)//> { //tag.Clear(); tag=InitList(); while (PDFInByte[index] != 32 && PDFInByte[index] != 40) //找到空格或者(,结束时index指向空格或( { AddNode(tag,(void*)PDFInByte[index]); index++; } index = omitBlank(PDFInByte,index); tagString =CharListGetString(tag); //开始读取value,默认Tail的属性只以空号的形式保存 index = ReadValue(++index, &valueString) + 1; //index此时指向)后面一个字符 while (PDFInByte[index] != 47 && PDFInByte[index] != 62) index++; //找下一个/或者结束符>,结束时index指向下一个/或结束符> index++; //index最终定位在下一个tag的第一个字符或结束符> Attr tail=(Attr)malloc(sizeof(_Attr)); tail->Name=tagString; tail->Value=valueString; AddNode(Tails,tail); } while (PDFInByte[index] != 106) index++;//将index指向endobj的j TailEnd = index + 1; fclose(fp);//已经关闭过了? }