pdf-object.c 95 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160
  1. // Copyright (C) 2004-2025 Artifex Software, Inc.
  2. //
  3. // This file is part of MuPDF.
  4. //
  5. // MuPDF is free software: you can redistribute it and/or modify it under the
  6. // terms of the GNU Affero General Public License as published by the Free
  7. // Software Foundation, either version 3 of the License, or (at your option)
  8. // any later version.
  9. //
  10. // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
  11. // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. // details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
  17. //
  18. // Alternative licensing terms are available from the licensor.
  19. // For commercial licensing, see <https://www.artifex.com/> or contact
  20. // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
  21. // CA 94129, USA, for further information.
  22. #include "mupdf/fitz.h"
  23. #include "mupdf/pdf.h"
  24. #include <stdarg.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <math.h>
  28. #define PDF_MAKE_NAME(STRING,NAME) STRING,
  29. static const char *PDF_NAME_LIST[] = {
  30. "", "", "", /* dummy slots for null, true, and false */
  31. #include "mupdf/pdf/name-table.h"
  32. };
  33. #undef PDF_MAKE_NAME
  34. typedef enum pdf_objkind_e
  35. {
  36. PDF_INT = 'i',
  37. PDF_REAL = 'f',
  38. PDF_STRING = 's',
  39. PDF_NAME = 'n',
  40. PDF_ARRAY = 'a',
  41. PDF_DICT = 'd',
  42. PDF_INDIRECT = 'r'
  43. } pdf_objkind;
  44. struct keyval
  45. {
  46. pdf_obj *k;
  47. pdf_obj *v;
  48. };
  49. enum
  50. {
  51. PDF_FLAGS_MARKED = 1,
  52. PDF_FLAGS_SORTED = 2,
  53. PDF_FLAGS_DIRTY = 4,
  54. PDF_FLAGS_MEMO_BASE = 8,
  55. PDF_FLAGS_MEMO_BASE_BOOL = 16
  56. };
  57. struct pdf_obj
  58. {
  59. short refs;
  60. unsigned char kind;
  61. unsigned char flags;
  62. };
  63. typedef struct
  64. {
  65. pdf_obj super;
  66. union
  67. {
  68. int64_t i;
  69. float f;
  70. } u;
  71. } pdf_obj_num;
  72. typedef struct
  73. {
  74. pdf_obj super;
  75. char *text; /* utf8 encoded text string */
  76. size_t len;
  77. char buf[FZ_FLEXIBLE_ARRAY];
  78. } pdf_obj_string;
  79. typedef struct
  80. {
  81. pdf_obj super;
  82. char n[FZ_FLEXIBLE_ARRAY];
  83. } pdf_obj_name;
  84. typedef struct
  85. {
  86. pdf_obj super;
  87. pdf_document *doc;
  88. int parent_num;
  89. int len;
  90. int cap;
  91. pdf_obj **items;
  92. } pdf_obj_array;
  93. typedef struct
  94. {
  95. pdf_obj super;
  96. pdf_document *doc;
  97. int parent_num;
  98. int len;
  99. int cap;
  100. struct keyval *items;
  101. } pdf_obj_dict;
  102. typedef struct
  103. {
  104. pdf_obj super;
  105. pdf_document *doc; /* Only needed for arrays, dicts and indirects */
  106. int num;
  107. int gen;
  108. } pdf_obj_ref;
  109. /* Each journal fragment represents a change to a PDF xref object. */
  110. typedef struct pdf_journal_fragment
  111. {
  112. struct pdf_journal_fragment *next;
  113. struct pdf_journal_fragment *prev;
  114. int obj_num;
  115. int newobj;
  116. pdf_obj *inactive;
  117. fz_buffer *stream;
  118. } pdf_journal_fragment;
  119. /* A journal entry represents a single notional 'change' to the
  120. * document, such as 'signing it' or 'filling in a field'. Each such
  121. * change consists of 1 or more 'fragments'. */
  122. typedef struct pdf_journal_entry
  123. {
  124. struct pdf_journal_entry *prev;
  125. struct pdf_journal_entry *next;
  126. char *title;
  127. #ifdef PDF_DEBUG_JOURNAL
  128. int changed_since_last_dumped;
  129. #endif
  130. pdf_journal_fragment *head;
  131. pdf_journal_fragment *tail;
  132. } pdf_journal_entry;
  133. /* A journal consists of a list of journal entries, rooted at head.
  134. * current is either NULL, or points to somewhere in the list. Anything
  135. * between head and current inclusive represents a journalled change
  136. * that is currently in force. Anything after current represents a
  137. * journalled change that has been 'undone'. If current is NULL, then
  138. * ALL changes in the list have been undone. */
  139. struct pdf_journal
  140. {
  141. pdf_journal_entry *head;
  142. pdf_journal_entry *current;
  143. int nesting;
  144. pdf_journal_entry *pending;
  145. pdf_journal_entry *pending_tail;
  146. };
  147. #define NAME(obj) ((pdf_obj_name *)(obj))
  148. #define NUM(obj) ((pdf_obj_num *)(obj))
  149. #define STRING(obj) ((pdf_obj_string *)(obj))
  150. #define DICT(obj) ((pdf_obj_dict *)(obj))
  151. #define ARRAY(obj) ((pdf_obj_array *)(obj))
  152. #define REF(obj) ((pdf_obj_ref *)(obj))
  153. pdf_obj *
  154. pdf_new_int(fz_context *ctx, int64_t i)
  155. {
  156. pdf_obj_num *obj;
  157. obj = Memento_label(fz_malloc(ctx, sizeof(pdf_obj_num)), "pdf_obj(int)");
  158. obj->super.refs = 1;
  159. obj->super.kind = PDF_INT;
  160. obj->super.flags = 0;
  161. obj->u.i = i;
  162. return &obj->super;
  163. }
  164. pdf_obj *
  165. pdf_new_real(fz_context *ctx, float f)
  166. {
  167. pdf_obj_num *obj;
  168. obj = Memento_label(fz_malloc(ctx, sizeof(pdf_obj_num)), "pdf_obj(real)");
  169. obj->super.refs = 1;
  170. obj->super.kind = PDF_REAL;
  171. obj->super.flags = 0;
  172. obj->u.f = f;
  173. return &obj->super;
  174. }
  175. pdf_obj *
  176. pdf_new_string(fz_context *ctx, const char *str, size_t len)
  177. {
  178. pdf_obj_string *obj;
  179. unsigned int l = (unsigned int)len;
  180. if ((size_t)l != len)
  181. fz_throw(ctx, FZ_ERROR_LIMIT, "Overflow in pdf string");
  182. obj = Memento_label(fz_malloc(ctx, offsetof(pdf_obj_string, buf) + len + 1), "pdf_obj(string)");
  183. obj->super.refs = 1;
  184. obj->super.kind = PDF_STRING;
  185. obj->super.flags = 0;
  186. obj->text = NULL;
  187. obj->len = l;
  188. memcpy(obj->buf, str, len);
  189. obj->buf[len] = '\0';
  190. return &obj->super;
  191. }
  192. pdf_obj *
  193. pdf_new_name(fz_context *ctx, const char *str)
  194. {
  195. pdf_obj_name *obj;
  196. int l = 3; /* skip dummy slots */
  197. int r = nelem(PDF_NAME_LIST) - 1;
  198. while (l <= r)
  199. {
  200. int m = (l + r) >> 1;
  201. int c = strcmp(str, PDF_NAME_LIST[m]);
  202. if (c < 0)
  203. r = m - 1;
  204. else if (c > 0)
  205. l = m + 1;
  206. else
  207. return (pdf_obj*)(intptr_t)m;
  208. }
  209. obj = Memento_label(fz_malloc(ctx, offsetof(pdf_obj_name, n) + strlen(str) + 1), "pdf_obj(name)");
  210. obj->super.refs = 1;
  211. obj->super.kind = PDF_NAME;
  212. obj->super.flags = 0;
  213. strcpy(obj->n, str);
  214. return &obj->super;
  215. }
  216. pdf_obj *
  217. pdf_new_indirect(fz_context *ctx, pdf_document *doc, int num, int gen)
  218. {
  219. pdf_obj_ref *obj;
  220. if (num < 0 || num > PDF_MAX_OBJECT_NUMBER)
  221. {
  222. fz_warn(ctx, "invalid object number (%d)", num);
  223. return PDF_NULL;
  224. }
  225. if (gen < 0 || gen > PDF_MAX_GEN_NUMBER)
  226. {
  227. fz_warn(ctx, "invalid generation number (%d)", gen);
  228. return PDF_NULL;
  229. }
  230. obj = Memento_label(fz_malloc(ctx, sizeof(pdf_obj_ref)), "pdf_obj(indirect)");
  231. obj->super.refs = 1;
  232. obj->super.kind = PDF_INDIRECT;
  233. obj->super.flags = 0;
  234. obj->doc = doc;
  235. obj->num = num;
  236. obj->gen = gen;
  237. return &obj->super;
  238. }
  239. #define OBJ_IS_NULL(obj) (obj == PDF_NULL)
  240. #define OBJ_IS_BOOL(obj) (obj == PDF_TRUE || obj == PDF_FALSE)
  241. #define OBJ_IS_NAME(obj) ((obj > PDF_FALSE && obj < PDF_LIMIT) || (obj >= PDF_LIMIT && obj->kind == PDF_NAME))
  242. #define OBJ_IS_INT(obj) \
  243. (obj >= PDF_LIMIT && obj->kind == PDF_INT)
  244. #define OBJ_IS_REAL(obj) \
  245. (obj >= PDF_LIMIT && obj->kind == PDF_REAL)
  246. #define OBJ_IS_NUMBER(obj) \
  247. (obj >= PDF_LIMIT && (obj->kind == PDF_REAL || obj->kind == PDF_INT))
  248. #define OBJ_IS_STRING(obj) \
  249. (obj >= PDF_LIMIT && obj->kind == PDF_STRING)
  250. #define OBJ_IS_ARRAY(obj) \
  251. (obj >= PDF_LIMIT && obj->kind == PDF_ARRAY)
  252. #define OBJ_IS_DICT(obj) \
  253. (obj >= PDF_LIMIT && obj->kind == PDF_DICT)
  254. #define OBJ_IS_INDIRECT(obj) \
  255. (obj >= PDF_LIMIT && obj->kind == PDF_INDIRECT)
  256. #define RESOLVE(obj) \
  257. if (OBJ_IS_INDIRECT(obj)) \
  258. obj = pdf_resolve_indirect_chain(ctx, obj); \
  259. int pdf_is_indirect(fz_context *ctx, pdf_obj *obj)
  260. {
  261. return OBJ_IS_INDIRECT(obj);
  262. }
  263. int pdf_is_null(fz_context *ctx, pdf_obj *obj)
  264. {
  265. RESOLVE(obj);
  266. return OBJ_IS_NULL(obj);
  267. }
  268. int pdf_is_bool(fz_context *ctx, pdf_obj *obj)
  269. {
  270. RESOLVE(obj);
  271. return OBJ_IS_BOOL(obj);
  272. }
  273. int pdf_is_int(fz_context *ctx, pdf_obj *obj)
  274. {
  275. RESOLVE(obj);
  276. return OBJ_IS_INT(obj);
  277. }
  278. int pdf_is_real(fz_context *ctx, pdf_obj *obj)
  279. {
  280. RESOLVE(obj);
  281. return OBJ_IS_REAL(obj);
  282. }
  283. int pdf_is_number(fz_context *ctx, pdf_obj *obj)
  284. {
  285. RESOLVE(obj);
  286. return OBJ_IS_NUMBER(obj);
  287. }
  288. int pdf_is_string(fz_context *ctx, pdf_obj *obj)
  289. {
  290. RESOLVE(obj);
  291. return OBJ_IS_STRING(obj);
  292. }
  293. int pdf_is_name(fz_context *ctx, pdf_obj *obj)
  294. {
  295. RESOLVE(obj);
  296. return OBJ_IS_NAME(obj);
  297. }
  298. int pdf_is_array(fz_context *ctx, pdf_obj *obj)
  299. {
  300. RESOLVE(obj);
  301. return OBJ_IS_ARRAY(obj);
  302. }
  303. int pdf_is_dict(fz_context *ctx, pdf_obj *obj)
  304. {
  305. RESOLVE(obj);
  306. return OBJ_IS_DICT(obj);
  307. }
  308. /* safe, silent failure, no error reporting on type mismatches */
  309. int pdf_to_bool(fz_context *ctx, pdf_obj *obj)
  310. {
  311. RESOLVE(obj);
  312. return obj == PDF_TRUE;
  313. }
  314. int pdf_to_bool_default(fz_context *ctx, pdf_obj *obj, int def)
  315. {
  316. RESOLVE(obj);
  317. return obj == PDF_TRUE ? 1 : obj == PDF_FALSE ? 0 : def;
  318. }
  319. int pdf_to_int(fz_context *ctx, pdf_obj *obj)
  320. {
  321. RESOLVE(obj);
  322. if (obj < PDF_LIMIT)
  323. return 0;
  324. if (obj->kind == PDF_INT)
  325. return (int)NUM(obj)->u.i;
  326. if (obj->kind == PDF_REAL)
  327. return (int)floorf(NUM(obj)->u.f + 0.5);
  328. return 0;
  329. }
  330. int pdf_to_int_default(fz_context *ctx, pdf_obj *obj, int def)
  331. {
  332. RESOLVE(obj);
  333. if (obj < PDF_LIMIT)
  334. return def;
  335. if (obj->kind == PDF_INT)
  336. return (int)NUM(obj)->u.i;
  337. if (obj->kind == PDF_REAL)
  338. return (int)floorf(NUM(obj)->u.f + 0.5);
  339. return def;
  340. }
  341. int64_t pdf_to_int64(fz_context *ctx, pdf_obj *obj)
  342. {
  343. RESOLVE(obj);
  344. if (obj < PDF_LIMIT)
  345. return 0;
  346. if (obj->kind == PDF_INT)
  347. return NUM(obj)->u.i;
  348. if (obj->kind == PDF_REAL)
  349. return (int64_t)floorf(NUM(obj)->u.f + 0.5);
  350. return 0;
  351. }
  352. float pdf_to_real(fz_context *ctx, pdf_obj *obj)
  353. {
  354. RESOLVE(obj);
  355. if (obj < PDF_LIMIT)
  356. return 0;
  357. if (obj->kind == PDF_REAL)
  358. return NUM(obj)->u.f;
  359. if (obj->kind == PDF_INT)
  360. return NUM(obj)->u.i;
  361. return 0;
  362. }
  363. float pdf_to_real_default(fz_context *ctx, pdf_obj *obj, float def)
  364. {
  365. RESOLVE(obj);
  366. if (obj < PDF_LIMIT)
  367. return def;
  368. if (obj->kind == PDF_REAL)
  369. return NUM(obj)->u.f;
  370. if (obj->kind == PDF_INT)
  371. return NUM(obj)->u.i;
  372. return def;
  373. }
  374. const char *pdf_to_name(fz_context *ctx, pdf_obj *obj)
  375. {
  376. RESOLVE(obj);
  377. if (obj < PDF_LIMIT)
  378. return PDF_NAME_LIST[((intptr_t)obj)];
  379. if (obj->kind == PDF_NAME)
  380. return NAME(obj)->n;
  381. return "";
  382. }
  383. char *pdf_to_str_buf(fz_context *ctx, pdf_obj *obj)
  384. {
  385. RESOLVE(obj);
  386. if (OBJ_IS_STRING(obj))
  387. return STRING(obj)->buf;
  388. return "";
  389. }
  390. size_t pdf_to_str_len(fz_context *ctx, pdf_obj *obj)
  391. {
  392. RESOLVE(obj);
  393. if (OBJ_IS_STRING(obj))
  394. return STRING(obj)->len;
  395. return 0;
  396. }
  397. const char *pdf_to_string(fz_context *ctx, pdf_obj *obj, size_t *sizep)
  398. {
  399. RESOLVE(obj);
  400. if (OBJ_IS_STRING(obj))
  401. {
  402. if (sizep)
  403. *sizep = STRING(obj)->len;
  404. return STRING(obj)->buf;
  405. }
  406. if (sizep)
  407. *sizep = 0;
  408. return "";
  409. }
  410. const char *pdf_to_text_string(fz_context *ctx, pdf_obj *obj)
  411. {
  412. RESOLVE(obj);
  413. if (OBJ_IS_STRING(obj))
  414. {
  415. if (!STRING(obj)->text)
  416. STRING(obj)->text = pdf_new_utf8_from_pdf_string(ctx, STRING(obj)->buf, STRING(obj)->len);
  417. return STRING(obj)->text;
  418. }
  419. return "";
  420. }
  421. void pdf_set_int(fz_context *ctx, pdf_obj *obj, int64_t i)
  422. {
  423. if (OBJ_IS_INT(obj))
  424. NUM(obj)->u.i = i;
  425. }
  426. void pdf_set_str_len(fz_context *ctx, pdf_obj *obj, size_t newlen)
  427. {
  428. RESOLVE(obj);
  429. if (!OBJ_IS_STRING(obj))
  430. return; /* This should never happen */
  431. if (newlen > STRING(obj)->len)
  432. return; /* This should never happen */
  433. STRING(obj)->buf[newlen] = 0;
  434. STRING(obj)->len = newlen;
  435. }
  436. int pdf_to_num(fz_context *ctx, pdf_obj *obj)
  437. {
  438. if (OBJ_IS_INDIRECT(obj))
  439. return REF(obj)->num;
  440. return 0;
  441. }
  442. int pdf_to_gen(fz_context *ctx, pdf_obj *obj)
  443. {
  444. if (OBJ_IS_INDIRECT(obj))
  445. return REF(obj)->gen;
  446. return 0;
  447. }
  448. /*
  449. DEPRECATED: Do not use in new code.
  450. */
  451. pdf_document *pdf_get_indirect_document(fz_context *ctx, pdf_obj *obj)
  452. {
  453. if (OBJ_IS_INDIRECT(obj))
  454. return REF(obj)->doc;
  455. return NULL;
  456. }
  457. /*
  458. DEPRECATED: Do not use in new code.
  459. */
  460. pdf_document *pdf_get_bound_document(fz_context *ctx, pdf_obj *obj)
  461. {
  462. if (obj < PDF_LIMIT)
  463. return NULL;
  464. if (obj->kind == PDF_INDIRECT)
  465. return REF(obj)->doc;
  466. if (obj->kind == PDF_ARRAY)
  467. return ARRAY(obj)->doc;
  468. if (obj->kind == PDF_DICT)
  469. return DICT(obj)->doc;
  470. return NULL;
  471. }
  472. /*
  473. This implementation will do to provide the required
  474. API change in advance of the rewrite to use weak references
  475. in the next version.
  476. */
  477. pdf_document *pdf_pin_document(fz_context *ctx, pdf_obj *obj)
  478. {
  479. return pdf_keep_document(ctx, pdf_get_bound_document(ctx, obj));
  480. }
  481. int pdf_objcmp_resolve(fz_context *ctx, pdf_obj *a, pdf_obj *b)
  482. {
  483. RESOLVE(a);
  484. RESOLVE(b);
  485. return pdf_objcmp(ctx, a, b);
  486. }
  487. static int
  488. do_objcmp(fz_context *ctx, pdf_obj *a, pdf_obj *b, int check_streams)
  489. {
  490. int i, j;
  491. if (a == b)
  492. return 0;
  493. /* a or b is null, true, or false */
  494. if (a <= PDF_FALSE || b <= PDF_FALSE)
  495. return 1;
  496. /* a is a constant name */
  497. if (a < PDF_LIMIT)
  498. {
  499. if (b < PDF_LIMIT)
  500. return a != b;
  501. if (b->kind != PDF_NAME)
  502. return 1;
  503. return strcmp(PDF_NAME_LIST[(intptr_t)a], NAME(b)->n);
  504. }
  505. /* b is a constant name */
  506. if (b < PDF_LIMIT)
  507. {
  508. if (a->kind != PDF_NAME)
  509. return 1;
  510. return strcmp(NAME(a)->n, PDF_NAME_LIST[(intptr_t)b]);
  511. }
  512. /* both a and b are allocated objects */
  513. if (a->kind != b->kind)
  514. return 1;
  515. switch (a->kind)
  516. {
  517. case PDF_INT:
  518. return NUM(a)->u.i - NUM(b)->u.i;
  519. case PDF_REAL:
  520. if (NUM(a)->u.f < NUM(b)->u.f)
  521. return -1;
  522. if (NUM(a)->u.f > NUM(b)->u.f)
  523. return 1;
  524. return 0;
  525. case PDF_STRING:
  526. if (STRING(a)->len < STRING(b)->len)
  527. {
  528. if (memcmp(STRING(a)->buf, STRING(b)->buf, STRING(a)->len) <= 0)
  529. return -1;
  530. return 1;
  531. }
  532. if (STRING(a)->len > STRING(b)->len)
  533. {
  534. if (memcmp(STRING(a)->buf, STRING(b)->buf, STRING(b)->len) >= 0)
  535. return 1;
  536. return -1;
  537. }
  538. return memcmp(STRING(a)->buf, STRING(b)->buf, STRING(a)->len);
  539. case PDF_NAME:
  540. return strcmp(NAME(a)->n, NAME(b)->n);
  541. case PDF_INDIRECT:
  542. if (REF(a)->num == REF(b)->num)
  543. return REF(a)->gen - REF(b)->gen;
  544. return REF(a)->num - REF(b)->num;
  545. case PDF_ARRAY:
  546. if (ARRAY(a)->len != ARRAY(b)->len)
  547. return ARRAY(a)->len - ARRAY(b)->len;
  548. for (i = 0; i < ARRAY(a)->len; i++)
  549. if (pdf_objcmp(ctx, ARRAY(a)->items[i], ARRAY(b)->items[i]))
  550. return 1;
  551. return 0;
  552. case PDF_DICT:
  553. if (DICT(a)->len != DICT(b)->len)
  554. return DICT(a)->len - DICT(b)->len;
  555. if ((a->flags & b->flags) & PDF_FLAGS_SORTED)
  556. {
  557. /* Both a and b are sorted. Easy. */
  558. for (i = 0; i < DICT(a)->len; i++)
  559. {
  560. if (pdf_objcmp(ctx, DICT(a)->items[i].k, DICT(b)->items[i].k))
  561. return 1;
  562. if (pdf_objcmp(ctx, DICT(a)->items[i].v, DICT(b)->items[i].v))
  563. return 1;
  564. }
  565. }
  566. else
  567. {
  568. /* Either a or b is not sorted. We need to work harder. */
  569. int len = DICT(a)->len;
  570. for (i = 0; i < len; i++)
  571. {
  572. pdf_obj *key = DICT(a)->items[i].k;
  573. pdf_obj *val = DICT(a)->items[i].v;
  574. for (j = 0; j < len; j++)
  575. {
  576. if (pdf_objcmp(ctx, key, DICT(b)->items[j].k) == 0 &&
  577. pdf_objcmp(ctx, val, DICT(b)->items[j].v) == 0)
  578. break; /* Match */
  579. }
  580. if (j == len)
  581. return 1;
  582. }
  583. }
  584. /* Dicts are identical, but if they are streams, we can only be sure
  585. * they are identical if the stream contents match. If '!check_streams',
  586. * then don't test for identical stream contents - only match if a == b.
  587. * Otherwise, do the full, painful, comparison. */
  588. {
  589. /* Slightly convoluted to know if something is a stream. */
  590. pdf_document *doc = DICT(a)->doc;
  591. int ap = pdf_obj_parent_num(ctx, a);
  592. int bp;
  593. int a_is_stream = 0;
  594. pdf_xref_entry *entrya = pdf_get_xref_entry_no_change(ctx, doc, ap);
  595. pdf_xref_entry *entryb;
  596. if (entrya != NULL && entrya->obj == a && pdf_obj_num_is_stream(ctx, doc, ap))
  597. {
  598. /* It's a stream, and we know a != b from above. */
  599. if (!check_streams)
  600. return 1; /* mismatch */
  601. a_is_stream = 1;
  602. }
  603. bp = pdf_obj_parent_num(ctx, b);
  604. entryb = pdf_get_xref_entry_no_change(ctx, doc, bp);
  605. if (entryb != NULL && entryb->obj == b && pdf_obj_num_is_stream(ctx, doc, bp))
  606. {
  607. /* It's a stream, and we know a != b from above. So mismatch. */
  608. if (!check_streams || !a_is_stream)
  609. return 1; /* mismatch */
  610. }
  611. else
  612. {
  613. /* b is not a stream. We match, iff a is not a stream. */
  614. return a_is_stream;
  615. }
  616. /* So, if we get here, we know check_streams is true, and that both
  617. * a and b are streams. */
  618. {
  619. fz_buffer *sa = NULL;
  620. fz_buffer *sb = NULL;
  621. int differ = 1;
  622. fz_var(sa);
  623. fz_var(sb);
  624. fz_try(ctx)
  625. {
  626. unsigned char *dataa, *datab;
  627. size_t lena, lenb;
  628. sa = pdf_load_raw_stream_number(ctx, doc, ap);
  629. sb = pdf_load_raw_stream_number(ctx, doc, bp);
  630. lena = fz_buffer_storage(ctx, sa, &dataa);
  631. lenb = fz_buffer_storage(ctx, sb, &datab);
  632. if (lena == lenb && memcmp(dataa, datab, lena) == 0)
  633. differ = 0;
  634. }
  635. fz_always(ctx)
  636. {
  637. fz_drop_buffer(ctx, sa);
  638. fz_drop_buffer(ctx, sb);
  639. }
  640. fz_catch(ctx)
  641. {
  642. fz_rethrow(ctx);
  643. }
  644. return differ;
  645. }
  646. }
  647. }
  648. return 1;
  649. }
  650. int
  651. pdf_objcmp(fz_context *ctx, pdf_obj *a, pdf_obj *b)
  652. {
  653. return do_objcmp(ctx, a, b, 0);
  654. }
  655. int
  656. pdf_objcmp_deep(fz_context *ctx, pdf_obj *a, pdf_obj *b)
  657. {
  658. return do_objcmp(ctx, a, b, 1);
  659. }
  660. int pdf_name_eq(fz_context *ctx, pdf_obj *a, pdf_obj *b)
  661. {
  662. RESOLVE(a);
  663. RESOLVE(b);
  664. if (a <= PDF_FALSE || b <= PDF_FALSE)
  665. return 0;
  666. if (a < PDF_LIMIT || b < PDF_LIMIT)
  667. return (a == b);
  668. if (a->kind == PDF_NAME && b->kind == PDF_NAME)
  669. return !strcmp(NAME(a)->n, NAME(b)->n);
  670. return 0;
  671. }
  672. static char *
  673. pdf_objkindstr(pdf_obj *obj)
  674. {
  675. if (obj == PDF_NULL)
  676. return "null";
  677. if (obj == PDF_TRUE || obj == PDF_FALSE)
  678. return "boolean";
  679. if (obj < PDF_LIMIT)
  680. return "name";
  681. switch (obj->kind)
  682. {
  683. case PDF_INT: return "integer";
  684. case PDF_REAL: return "real";
  685. case PDF_STRING: return "string";
  686. case PDF_NAME: return "name";
  687. case PDF_ARRAY: return "array";
  688. case PDF_DICT: return "dictionary";
  689. case PDF_INDIRECT: return "reference";
  690. }
  691. return "<unknown>";
  692. }
  693. pdf_obj *
  694. pdf_new_array(fz_context *ctx, pdf_document *doc, int initialcap)
  695. {
  696. pdf_obj_array *obj;
  697. int i;
  698. if (doc == NULL)
  699. fz_throw(ctx, FZ_ERROR_ARGUMENT, "cannot create array without a document");
  700. obj = Memento_label(fz_malloc(ctx, sizeof(pdf_obj_array)), "pdf_obj(array)");
  701. obj->super.refs = 1;
  702. obj->super.kind = PDF_ARRAY;
  703. obj->super.flags = 0;
  704. obj->doc = doc;
  705. obj->parent_num = 0;
  706. obj->len = 0;
  707. obj->cap = initialcap > 1 ? initialcap : 6;
  708. fz_try(ctx)
  709. {
  710. obj->items = Memento_label(fz_malloc_array(ctx, obj->cap, pdf_obj*), "pdf_array_items");
  711. }
  712. fz_catch(ctx)
  713. {
  714. fz_free(ctx, obj);
  715. fz_rethrow(ctx);
  716. }
  717. for (i = 0; i < obj->cap; i++)
  718. obj->items[i] = NULL;
  719. return &obj->super;
  720. }
  721. static void
  722. pdf_array_grow(fz_context *ctx, pdf_obj_array *obj)
  723. {
  724. int i;
  725. int new_cap = (obj->cap * 3) / 2;
  726. obj->items = fz_realloc_array(ctx, obj->items, new_cap, pdf_obj*);
  727. obj->cap = new_cap;
  728. for (i = obj->len ; i < obj->cap; i++)
  729. obj->items[i] = NULL;
  730. }
  731. pdf_obj *
  732. pdf_copy_array(fz_context *ctx, pdf_obj *obj)
  733. {
  734. pdf_document *doc;
  735. pdf_obj *arr;
  736. int i;
  737. int n;
  738. RESOLVE(obj);
  739. if (!OBJ_IS_ARRAY(obj))
  740. fz_throw(ctx, FZ_ERROR_ARGUMENT, "not an array (%s)", pdf_objkindstr(obj));
  741. doc = ARRAY(obj)->doc;
  742. n = pdf_array_len(ctx, obj);
  743. arr = pdf_new_array(ctx, doc, n);
  744. fz_try(ctx)
  745. for (i = 0; i < n; i++)
  746. pdf_array_push(ctx, arr, pdf_array_get(ctx, obj, i));
  747. fz_catch(ctx)
  748. {
  749. pdf_drop_obj(ctx, arr);
  750. fz_rethrow(ctx);
  751. }
  752. return arr;
  753. }
  754. int
  755. pdf_array_len(fz_context *ctx, pdf_obj *obj)
  756. {
  757. RESOLVE(obj);
  758. if (!OBJ_IS_ARRAY(obj))
  759. return 0;
  760. return ARRAY(obj)->len;
  761. }
  762. pdf_obj *
  763. pdf_array_get(fz_context *ctx, pdf_obj *obj, int i)
  764. {
  765. RESOLVE(obj);
  766. if (!OBJ_IS_ARRAY(obj))
  767. return NULL;
  768. if (i < 0 || i >= ARRAY(obj)->len)
  769. return NULL;
  770. return ARRAY(obj)->items[i];
  771. }
  772. /* Call this to enable journalling on a given document. */
  773. void pdf_enable_journal(fz_context *ctx, pdf_document *doc)
  774. {
  775. if (ctx == NULL || doc == NULL)
  776. return;
  777. if (doc->journal == NULL)
  778. doc->journal = fz_malloc_struct(ctx, pdf_journal);
  779. }
  780. static void
  781. discard_fragments(fz_context *ctx, pdf_journal_fragment *head)
  782. {
  783. while (head)
  784. {
  785. pdf_journal_fragment *next = head->next;
  786. pdf_drop_obj(ctx, head->inactive);
  787. fz_drop_buffer(ctx, head->stream);
  788. fz_free(ctx, head);
  789. head = next;
  790. }
  791. }
  792. static void
  793. discard_journal_entries(fz_context *ctx, pdf_journal_entry **journal_entry)
  794. {
  795. pdf_journal_entry *entry = *journal_entry;
  796. if (entry == NULL)
  797. return;
  798. *journal_entry = NULL;
  799. while (entry)
  800. {
  801. pdf_journal_entry *next = entry->next;
  802. discard_fragments(ctx, entry->head);
  803. fz_free(ctx, entry->title);
  804. fz_free(ctx, entry);
  805. entry = next;
  806. }
  807. }
  808. static void
  809. new_entry(fz_context *ctx, pdf_document *doc, char *operation)
  810. {
  811. fz_try(ctx)
  812. {
  813. pdf_journal_entry *entry;
  814. /* We create a new entry, and link it into the middle of
  815. * the chain. If we actually come to put anything into
  816. * it later, then the call to pdf_add_journal_fragment
  817. * during that addition will discard everything in the
  818. * history that follows it. */
  819. entry = fz_malloc_struct(ctx, pdf_journal_entry);
  820. if (doc->journal->current == NULL)
  821. {
  822. entry->prev = NULL;
  823. entry->next = doc->journal->head;
  824. doc->journal->head = entry;
  825. }
  826. else
  827. {
  828. entry->prev = doc->journal->current;
  829. entry->next = doc->journal->current->next;
  830. if (doc->journal->current->next)
  831. doc->journal->current->next->prev = entry;
  832. doc->journal->current->next = entry;
  833. }
  834. doc->journal->current = entry;
  835. entry->title = operation;
  836. }
  837. fz_catch(ctx)
  838. {
  839. fz_free(ctx, operation);
  840. fz_rethrow(ctx);
  841. }
  842. }
  843. /* Call this to start an operation. Undo/redo works at 'operation'
  844. * granularity. Nested operations are all counted within the outermost
  845. * operation. Any modification performed on a journalled PDF without an
  846. * operation having been started will throw an error. */
  847. static void
  848. do_begin_operation(fz_context *ctx, pdf_document *doc, const char *operation_)
  849. {
  850. char *operation;
  851. /* If we aren't journalling this doc, just give up now. */
  852. if (ctx == NULL || doc == NULL || doc->journal == NULL)
  853. return;
  854. /* Always increment nesting. */
  855. doc->journal->nesting++;
  856. operation = operation_ ? fz_strdup(ctx, operation_) : NULL;
  857. #ifdef PDF_DEBUG_JOURNAL
  858. fz_write_printf(ctx, fz_stddbg(ctx), "Beginning: (->%d) %s\n", doc->journal->nesting, operation ? operation : "<implicit>");
  859. #endif
  860. fz_try(ctx)
  861. {
  862. pdf_journal_entry *entry;
  863. /* We create a new entry, and link it into the middle of
  864. * the chain. If we actually come to put anything into
  865. * it later, then the call to pdf_add_journal_fragment
  866. * during that addition will discard everything in the
  867. * history that follows it. */
  868. entry = fz_malloc_struct(ctx, pdf_journal_entry);
  869. if (doc->journal->pending_tail == NULL)
  870. {
  871. entry->prev = NULL;
  872. entry->next = doc->journal->pending;
  873. doc->journal->pending = entry;
  874. }
  875. else
  876. {
  877. entry->prev = doc->journal->pending_tail;
  878. entry->next = doc->journal->pending_tail->next;
  879. if (doc->journal->pending_tail->next)
  880. doc->journal->pending_tail->next->prev = entry;
  881. doc->journal->pending_tail->next = entry;
  882. }
  883. doc->journal->pending_tail = entry;
  884. entry->title = operation;
  885. }
  886. fz_catch(ctx)
  887. {
  888. doc->journal->nesting--;
  889. fz_free(ctx, operation);
  890. fz_rethrow(ctx);
  891. }
  892. }
  893. void pdf_begin_operation(fz_context *ctx, pdf_document *doc, const char *operation)
  894. {
  895. if (operation == NULL)
  896. fz_throw(ctx, FZ_ERROR_ARGUMENT, "All operations must be named");
  897. do_begin_operation(ctx, doc, operation);
  898. }
  899. void pdf_begin_implicit_operation(fz_context *ctx, pdf_document *doc)
  900. {
  901. do_begin_operation(ctx, doc, NULL);
  902. }
  903. void pdf_drop_journal(fz_context *ctx, pdf_journal *journal)
  904. {
  905. if (ctx == NULL || journal == NULL)
  906. return;
  907. discard_journal_entries(ctx, &journal->head);
  908. /* Shouldn't be any pending ones, but be safe. */
  909. discard_journal_entries(ctx, &journal->pending);
  910. fz_free(ctx, journal);
  911. }
  912. #ifdef PDF_DEBUG_JOURNAL
  913. static void
  914. dump_changes(fz_context *ctx, pdf_document *doc, pdf_journal_entry *entry)
  915. {
  916. pdf_journal_fragment *frag;
  917. if (entry == NULL || entry->changed_since_last_dumped == 0)
  918. return;
  919. for (frag = entry->head; frag; frag = frag->next)
  920. {
  921. pdf_obj *obj;
  922. fz_write_printf(ctx, fz_stddbg(ctx), "Changing obj %d:\n", frag->obj_num);
  923. pdf_debug_obj(ctx, frag->inactive);
  924. fz_write_printf(ctx, fz_stddbg(ctx), " To:\n");
  925. obj = pdf_load_object(ctx, doc, frag->obj_num);
  926. pdf_debug_obj(ctx, obj);
  927. pdf_drop_obj(ctx, obj);
  928. }
  929. entry->changed_since_last_dumped = 0;
  930. }
  931. #endif
  932. /* We build up journal entries as being a list of changes (fragments) that
  933. * happen all together as part of a single step. When we reach pdf_end_operation
  934. * we have all the changes that have happened during this operation in a list
  935. * that basically boils down to being:
  936. *
  937. * change object x from being A to the value in the xref.
  938. * change object y from being B to the value in the xref.
  939. * change object z from being C to the value in the xref.
  940. * etc.
  941. *
  942. * The idea is that we can undo, or redo by stepping through that list.
  943. * Every object can only be mentioned once in a fragment (otherwise we
  944. * get very confused when undoing and redoing).
  945. *
  946. * When we come to glue 2 entries together (as happens when we end a
  947. * nested or implicit operation), we need to be sure that the 2 entries
  948. * don't both mention the same object.
  949. *
  950. * Imagine we've edited a text field from being empty to containing
  951. * 'he' by typing each char at a time:
  952. *
  953. * Entry 1:
  954. * change object x from being ''.
  955. * Entry 2 (implicit):
  956. * change object x from being 'h'.
  957. *
  958. * with current xref entry for x being 'he'.
  959. *
  960. * When we come to combine the two, we can't simply go to:
  961. *
  962. * change object x from being ''.
  963. * change object x from being 'h'.
  964. *
  965. * If we 'undo' that, however, because we run forwards through the list for
  966. * both undo and redo, we get it wrong.
  967. *
  968. * First we replace 'he' by ''.
  969. * Then we replace '' by 'h'.
  970. *
  971. * i.e. leaving us only partly undone.
  972. *
  973. * Either we need to run in different directions for undo and redo, or we need to
  974. * resolve the changes down to a single change for each object. Given that we don't
  975. * really want more than one change for each object in each changeset (needless memory
  976. * etc), let's resolve the changesets.
  977. **/
  978. static void resolve_undo(fz_context *ctx, pdf_journal_entry *entry)
  979. {
  980. pdf_journal_fragment *start, *current;
  981. pdf_journal_fragment *tail = NULL;
  982. /* Slightly nasty that this is n^2, but any alternative involves
  983. * sorting. Shouldn't be huge lists anyway. */
  984. for (start = entry->head; start; start = start->next)
  985. {
  986. pdf_journal_fragment *next;
  987. tail = start;
  988. for (current = start->next; current; current = next)
  989. {
  990. next = current->next;
  991. if (start->obj_num == current->obj_num)
  992. {
  993. pdf_drop_obj(ctx, current->inactive);
  994. fz_drop_buffer(ctx, current->stream);
  995. /* start->newobj should not change */
  996. /* Now drop current */
  997. if (next)
  998. next->prev = current->prev;
  999. current->prev->next = next;
  1000. fz_free(ctx, current);
  1001. }
  1002. }
  1003. }
  1004. entry->tail = tail;
  1005. }
  1006. /* Call this to end an operation. */
  1007. void pdf_end_operation(fz_context *ctx, pdf_document *doc)
  1008. {
  1009. pdf_journal_entry *entry;
  1010. if (ctx == NULL || doc == NULL || doc->journal == NULL)
  1011. return;
  1012. /* Decrement the operation nesting count. */
  1013. if (--doc->journal->nesting > 0)
  1014. {
  1015. /* We need to move the contents of doc->pending_tail down to
  1016. * be on doc->pending_tail->prev. i.e. we combine fragments
  1017. * as these operations become one. */
  1018. entry = doc->journal->pending_tail;
  1019. /* An implicit operation before we start the file can result in us getting here
  1020. * with no entry at all! */
  1021. if (entry && entry->prev)
  1022. {
  1023. if (entry->tail == NULL)
  1024. {
  1025. /* Nothing to move. */
  1026. }
  1027. else if (entry->prev->tail == NULL)
  1028. {
  1029. /* Nothing where we want to move it. */
  1030. entry->prev->head = entry->head;
  1031. entry->prev->tail = entry->tail;
  1032. }
  1033. else
  1034. {
  1035. /* Append one list to the other. */
  1036. entry->prev->tail->next = entry->head;
  1037. entry->head->prev = entry->prev->tail;
  1038. entry->prev->tail = entry->tail;
  1039. /* And resolve any clashing objects */
  1040. resolve_undo(ctx, entry->prev);
  1041. }
  1042. #ifdef PDF_DEBUG_JOURNAL
  1043. fz_write_printf(ctx, fz_stddbg(ctx), "Ending! (->%d) \"%s\" <= \"%s\"\n", doc->journal->nesting,
  1044. entry->prev->title ? entry->prev->title : "<implicit>",
  1045. entry->title ? entry->title : "<implicit>");
  1046. #endif
  1047. doc->journal->pending_tail = entry->prev;
  1048. entry->prev->next = NULL;
  1049. fz_free(ctx, entry->title);
  1050. fz_free(ctx, entry);
  1051. }
  1052. else
  1053. {
  1054. #ifdef PDF_DEBUG_JOURNAL
  1055. fz_write_printf(ctx, fz_stddbg(ctx), "Ending! (->%d) no entry\n", doc->journal->nesting);
  1056. #endif
  1057. }
  1058. return;
  1059. }
  1060. /* Now, check to see whether we have actually stored any changes
  1061. * (fragments) into our entry. If we have, we need to move these
  1062. * changes from pending onto current. */
  1063. entry = doc->journal->pending;
  1064. assert(entry);
  1065. /* We really ought to have just a single pending entry at this point,
  1066. * but implicit operations when we've just loaded a file can mean
  1067. * that we don't have an entry at all. */
  1068. if (entry == NULL)
  1069. {
  1070. /* Never happens! */
  1071. }
  1072. else if (entry->head == NULL)
  1073. {
  1074. /* Didn't actually change anything! Remove the empty entry. */
  1075. #ifdef PDF_DEBUG_JOURNAL
  1076. fz_write_printf(ctx, fz_stddbg(ctx), "Ending Empty!\n");
  1077. #endif
  1078. discard_journal_entries(ctx, &doc->journal->pending);
  1079. }
  1080. else if (entry->title != NULL)
  1081. {
  1082. /* Explicit operation. Move the entry off the pending list. */
  1083. assert(entry->next == NULL);
  1084. if (doc->journal->current)
  1085. {
  1086. doc->journal->current->next = entry;
  1087. entry->prev = doc->journal->current;
  1088. doc->journal->current = entry;
  1089. }
  1090. else
  1091. {
  1092. doc->journal->head = entry;
  1093. doc->journal->current = entry;
  1094. }
  1095. #ifdef PDF_DEBUG_JOURNAL
  1096. fz_write_printf(ctx, fz_stddbg(ctx), "Ending!\n");
  1097. #endif
  1098. }
  1099. else if (doc->journal->current == NULL)
  1100. {
  1101. /* Implicit operation, with no previous one. */
  1102. #ifdef PDF_DEBUG_JOURNAL
  1103. fz_write_printf(ctx, fz_stddbg(ctx), "Ending implicit with no previous!\n");
  1104. #endif
  1105. /* Just drop the record of the changes. */
  1106. discard_journal_entries(ctx, &doc->journal->pending);
  1107. }
  1108. else
  1109. {
  1110. /* Implicit operation. Roll these changes into the previous one.*/
  1111. #ifdef PDF_DEBUG_JOURNAL
  1112. fz_write_printf(ctx, fz_stddbg(ctx), "Ending implicit!\n");
  1113. #endif
  1114. doc->journal->current->tail->next = entry->head;
  1115. entry->head->prev = doc->journal->current->tail;
  1116. doc->journal->current->tail = entry->tail;
  1117. entry->head = NULL;
  1118. entry->tail = NULL;
  1119. fz_free(ctx, entry->title);
  1120. fz_free(ctx, entry);
  1121. /* And resolve any clashing objects */
  1122. resolve_undo(ctx, doc->journal->current);
  1123. }
  1124. doc->journal->pending = NULL;
  1125. doc->journal->pending_tail = NULL;
  1126. }
  1127. /* Call this to find out how many undo/redo steps there are, and the
  1128. * current position we are within those. 0 = original document,
  1129. * *steps = final edited version. */
  1130. int pdf_undoredo_state(fz_context *ctx, pdf_document *doc, int *steps)
  1131. {
  1132. int i, c;
  1133. pdf_journal_entry *entry;
  1134. if (ctx == NULL || doc == NULL || doc->journal == NULL)
  1135. {
  1136. *steps = 0;
  1137. return 0;
  1138. }
  1139. if (doc->journal->pending != NULL || doc->journal->nesting > 0)
  1140. fz_throw(ctx, FZ_ERROR_ARGUMENT, "Can't undo/redo during an operation");
  1141. i = 0;
  1142. c = 0;
  1143. for (entry = doc->journal->head; entry != NULL; entry = entry->next)
  1144. {
  1145. i++;
  1146. if (entry == doc->journal->current)
  1147. c = i;
  1148. }
  1149. *steps = i;
  1150. return c;
  1151. }
  1152. int pdf_can_undo(fz_context *ctx, pdf_document *doc)
  1153. {
  1154. int steps, step;
  1155. step = pdf_undoredo_state(ctx, doc, &steps);
  1156. return step > 0;
  1157. }
  1158. int pdf_can_redo(fz_context *ctx, pdf_document *doc)
  1159. {
  1160. int steps, step;
  1161. step = pdf_undoredo_state(ctx, doc, &steps);
  1162. return step != steps;
  1163. }
  1164. /* Call this to find the title of the operation within the undo state. */
  1165. const char *pdf_undoredo_step(fz_context *ctx, pdf_document *doc, int step)
  1166. {
  1167. pdf_journal_entry *entry;
  1168. if (ctx == NULL || doc == NULL || doc->journal == NULL)
  1169. return NULL;
  1170. if (doc->journal->pending != NULL || doc->journal->nesting > 0)
  1171. fz_throw(ctx, FZ_ERROR_ARGUMENT, "Can't undo/redo during an operation");
  1172. for (entry = doc->journal->head; step > 0 && entry != NULL; step--, entry = entry->next);
  1173. if (step != 0 || entry == NULL)
  1174. return NULL;
  1175. return entry->title;
  1176. }
  1177. static void
  1178. swap_fragments(fz_context *ctx, pdf_document *doc, pdf_journal_entry *entry)
  1179. {
  1180. pdf_journal_fragment *frag;
  1181. #ifdef PDF_DEBUG_JOURNAL
  1182. entry->changed_since_last_dumped = 1;
  1183. #endif
  1184. if (doc->local_xref_nesting != 0)
  1185. fz_throw(ctx, FZ_ERROR_ARGUMENT, "Can't undo/redo within an operation");
  1186. pdf_drop_local_xref_and_resources(ctx, doc);
  1187. for (frag = entry->head; frag != NULL; frag = frag->next)
  1188. {
  1189. pdf_xref_entry *xre;
  1190. pdf_obj *old;
  1191. fz_buffer *obuf;
  1192. int type;
  1193. xre = pdf_get_incremental_xref_entry(ctx, doc, frag->obj_num);
  1194. old = xre->obj;
  1195. obuf = xre->stm_buf;
  1196. xre->obj = frag->inactive;
  1197. type = xre->type;
  1198. xre->type = frag->newobj ? 0 : 'o';
  1199. frag->newobj = type == 0;
  1200. xre->stm_buf = frag->stream;
  1201. frag->inactive = old;
  1202. frag->stream = obuf;
  1203. }
  1204. }
  1205. /* Abandon an operation - unwind back to the previous begin. */
  1206. void pdf_abandon_operation(fz_context *ctx, pdf_document *doc)
  1207. {
  1208. pdf_journal_entry *entry;
  1209. if (ctx == NULL || doc == NULL || doc->journal == NULL)
  1210. return;
  1211. if (doc->journal->nesting == 0)
  1212. fz_throw(ctx, FZ_ERROR_ARGUMENT, "Can't abandon a non-existent operation!");
  1213. doc->journal->nesting--;
  1214. entry = doc->journal->pending_tail;
  1215. assert(entry);
  1216. /* Undo the changes we are about the discard. */
  1217. swap_fragments(ctx, doc, entry);
  1218. /* And discard entry. */
  1219. if (entry->prev == NULL)
  1220. {
  1221. doc->journal->pending = NULL;
  1222. doc->journal->pending_tail = NULL;
  1223. }
  1224. else
  1225. {
  1226. doc->journal->pending_tail = entry->prev;
  1227. entry->prev->next = NULL;
  1228. entry->prev = NULL;
  1229. }
  1230. #ifdef PDF_DEBUG_JOURNAL
  1231. fz_write_printf(ctx, fz_stddbg(ctx), "Abandoning!\n");
  1232. #endif
  1233. discard_journal_entries(ctx, &entry);
  1234. }
  1235. /* Move backwards in the undo history. Throws an error if we are at the
  1236. * start. Any edits to the document at this point will discard all
  1237. * subsequent history. */
  1238. void pdf_undo(fz_context *ctx, pdf_document *doc)
  1239. {
  1240. pdf_journal_entry *entry;
  1241. pdf_journal_fragment *frag;
  1242. if (ctx == NULL || doc == NULL)
  1243. return;
  1244. if (doc->journal == NULL)
  1245. fz_throw(ctx, FZ_ERROR_ARGUMENT, "Cannot undo on unjournaled PDF");
  1246. if (doc->journal->nesting != 0)
  1247. fz_throw(ctx, FZ_ERROR_ARGUMENT, "Can't undo during an operation!");
  1248. entry = doc->journal->current;
  1249. if (entry == NULL)
  1250. fz_throw(ctx, FZ_ERROR_ARGUMENT, "Already at start of history");
  1251. #ifdef PDF_DEBUG_JOURNAL
  1252. fz_write_printf(ctx, fz_stddbg(ctx), "Undo!\n");
  1253. #endif
  1254. doc->journal->current = entry->prev;
  1255. swap_fragments(ctx, doc, entry);
  1256. // nuke all caches
  1257. pdf_drop_page_tree_internal(ctx, doc);
  1258. pdf_sync_open_pages(ctx, doc);
  1259. for (frag = entry->head; frag; frag = frag->next)
  1260. pdf_purge_object_from_store(ctx, doc, frag->obj_num);
  1261. }
  1262. /* Move forwards in the undo history. Throws an error if we are at the
  1263. * end. */
  1264. void pdf_redo(fz_context *ctx, pdf_document *doc)
  1265. {
  1266. pdf_journal_entry *entry;
  1267. pdf_journal_fragment *frag;
  1268. if (ctx == NULL || doc == NULL)
  1269. return;
  1270. if (doc->journal == NULL)
  1271. fz_throw(ctx, FZ_ERROR_ARGUMENT, "Cannot redo on unjournaled PDF");
  1272. if (doc->journal->nesting != 0)
  1273. fz_throw(ctx, FZ_ERROR_ARGUMENT, "Can't redo during an operation!");
  1274. #ifdef PDF_DEBUG_JOURNAL
  1275. fz_write_printf(ctx, fz_stddbg(ctx), "Redo!\n");
  1276. #endif
  1277. entry = doc->journal->current;
  1278. if (entry == NULL)
  1279. {
  1280. /* If journal->current is null then everything has been undone. */
  1281. /* Go to the first change in journal->head if it exists. */
  1282. entry = doc->journal->head;
  1283. }
  1284. else
  1285. {
  1286. entry = entry->next;
  1287. }
  1288. if (entry == NULL)
  1289. fz_throw(ctx, FZ_ERROR_ARGUMENT, "Already at end of history");
  1290. // nuke all caches
  1291. pdf_drop_page_tree_internal(ctx, doc);
  1292. pdf_sync_open_pages(ctx, doc);
  1293. for (frag = entry->head; frag; frag = frag->next)
  1294. pdf_purge_object_from_store(ctx, doc, frag->obj_num);
  1295. doc->journal->current = entry;
  1296. swap_fragments(ctx, doc, entry);
  1297. }
  1298. void pdf_discard_journal(fz_context *ctx, pdf_journal *journal)
  1299. {
  1300. if (ctx == NULL || journal == NULL)
  1301. return;
  1302. discard_journal_entries(ctx, &journal->head);
  1303. /* Should be NULL, but belt and braces. */
  1304. discard_journal_entries(ctx, &journal->pending);
  1305. journal->head = NULL;
  1306. journal->current = NULL;
  1307. journal->pending = NULL;
  1308. journal->pending_tail = NULL;
  1309. }
  1310. static void
  1311. pdf_fingerprint_file(fz_context *ctx, pdf_document *doc, unsigned char digest[16], int i)
  1312. {
  1313. fz_md5 state;
  1314. fz_md5_init(&state);
  1315. fz_md5_update_int64(&state, doc->num_xref_sections-i);
  1316. for (; i < doc->num_xref_sections; i++)
  1317. {
  1318. pdf_xref_subsec *subsec = doc->xref_sections[i].subsec;
  1319. fz_md5_update_int64(&state, doc->xref_sections[i].num_objects);
  1320. while (subsec)
  1321. {
  1322. fz_md5_update_int64(&state, subsec->start);
  1323. fz_md5_update_int64(&state, subsec->len);
  1324. subsec = subsec->next;
  1325. }
  1326. }
  1327. fz_md5_final(&state, digest);
  1328. }
  1329. void
  1330. pdf_serialise_journal(fz_context *ctx, pdf_document *doc, fz_output *out)
  1331. {
  1332. pdf_journal_entry *entry;
  1333. int currentpos = 0;
  1334. unsigned char digest[16];
  1335. int i;
  1336. int nis = doc->num_incremental_sections;
  1337. pdf_fingerprint_file(ctx, doc, digest, nis);
  1338. if (!pdf_has_unsaved_changes(ctx, doc))
  1339. nis = 0;
  1340. fz_write_printf(ctx, out, "%!MuPDF-Journal-100\n");
  1341. fz_write_string(ctx, out, "\njournal\n<<\n");
  1342. fz_write_printf(ctx, out, "/NumSections %d\n", nis);
  1343. fz_write_printf(ctx, out, "/FileSize %ld\n", doc->file_size);
  1344. fz_write_printf(ctx, out, "/Fingerprint <");
  1345. for (i = 0; i < 16; i++)
  1346. fz_write_printf(ctx, out, "%02x", digest[i]);
  1347. fz_write_printf(ctx, out, ">\n");
  1348. if (doc->journal->current != NULL)
  1349. for (entry = doc->journal->head; entry != NULL; entry = entry->next)
  1350. {
  1351. currentpos++;
  1352. if (entry == doc->journal->current)
  1353. break;
  1354. }
  1355. fz_write_printf(ctx, out, "/HistoryPos %d\n", currentpos);
  1356. fz_write_string(ctx, out, ">>\n");
  1357. for (entry = doc->journal->head; entry != NULL; entry = entry->next)
  1358. {
  1359. pdf_journal_fragment *frag;
  1360. fz_write_printf(ctx, out, "entry\n%(\n", entry->title);
  1361. for (frag = entry->head; frag != NULL; frag = frag->next)
  1362. {
  1363. if (frag->newobj)
  1364. {
  1365. fz_write_printf(ctx, out, "%d 0 newobj\n", frag->obj_num);
  1366. continue;
  1367. }
  1368. fz_write_printf(ctx, out, "%d 0 obj\n", frag->obj_num);
  1369. pdf_print_encrypted_obj(ctx, out, frag->inactive, 1, 0, NULL, frag->obj_num, 0, NULL);
  1370. if (frag->stream)
  1371. {
  1372. fz_write_printf(ctx, out, "\nstream\n");
  1373. fz_write_data(ctx, out, frag->stream->data, frag->stream->len);
  1374. fz_write_string(ctx, out, "\nendstream");
  1375. }
  1376. fz_write_string(ctx, out, "\nendobj\n");
  1377. }
  1378. }
  1379. fz_write_printf(ctx, out, "endjournal\n");
  1380. }
  1381. void
  1382. pdf_add_journal_fragment(fz_context *ctx, pdf_document *doc, int parent, pdf_obj *copy, fz_buffer *copy_stream, int newobj)
  1383. {
  1384. pdf_journal_entry *entry;
  1385. pdf_journal_fragment *frag;
  1386. if (doc->journal == NULL)
  1387. return;
  1388. entry = doc->journal->pending_tail;
  1389. /* We must be in an operation. */
  1390. assert(entry != NULL);
  1391. if (entry == NULL)
  1392. fz_throw(ctx, FZ_ERROR_ARGUMENT, "Can't add a journal fragment absent an operation");
  1393. /* This should never happen, as we should always be appending to the end of
  1394. * the pending list. */
  1395. assert(entry->next == NULL);
  1396. if (entry->next)
  1397. {
  1398. discard_journal_entries(ctx, &entry->next);
  1399. doc->journal->pending_tail = NULL;
  1400. }
  1401. #ifdef PDF_DEBUG_JOURNAL
  1402. entry->changed_since_last_dumped = 1;
  1403. #endif
  1404. fz_try(ctx)
  1405. {
  1406. frag = fz_malloc_struct(ctx, pdf_journal_fragment);
  1407. frag->obj_num = parent;
  1408. if (entry->tail == NULL)
  1409. {
  1410. frag->prev = NULL;
  1411. entry->head = frag;
  1412. }
  1413. else
  1414. {
  1415. frag->prev = entry->tail;
  1416. entry->tail->next = frag;
  1417. }
  1418. entry->tail = frag;
  1419. frag->newobj = newobj;
  1420. frag->inactive = copy;
  1421. frag->stream = copy_stream;
  1422. }
  1423. fz_catch(ctx)
  1424. fz_rethrow(ctx);
  1425. }
  1426. void pdf_deserialise_journal(fz_context *ctx, pdf_document *doc, fz_stream *stm)
  1427. {
  1428. int num, version, c, nis, pos;
  1429. pdf_obj *obj = NULL, *fingerprint_obj;
  1430. fz_buffer *buffer;
  1431. unsigned char digest[16];
  1432. int64_t file_size;
  1433. int digests_match = 0;
  1434. pdf_token tok;
  1435. if (!doc || !stm)
  1436. return;
  1437. if (doc->journal)
  1438. fz_throw(ctx, FZ_ERROR_ARGUMENT, "Can't load a journal over another one");
  1439. if (fz_skip_string(ctx, stm, "%!MuPDF-Journal-"))
  1440. fz_throw(ctx, FZ_ERROR_FORMAT, "Bad journal format");
  1441. fz_var(obj);
  1442. fz_var(digests_match);
  1443. fz_try(ctx)
  1444. {
  1445. version = 0;
  1446. while (1)
  1447. {
  1448. c = fz_peek_byte(ctx, stm);
  1449. if (c < '0' || c > '9')
  1450. break;
  1451. version = (version*10) + c - '0';
  1452. (void)fz_read_byte(ctx, stm);
  1453. }
  1454. if (version != 100)
  1455. fz_throw(ctx, FZ_ERROR_FORMAT, "Bad journal format");
  1456. fz_skip_space(ctx, stm);
  1457. if (fz_skip_string(ctx, stm, "journal\n"))
  1458. fz_throw(ctx, FZ_ERROR_FORMAT, "Bad journal format");
  1459. tok = pdf_lex(ctx, stm, &doc->lexbuf.base);
  1460. if (tok != PDF_TOK_OPEN_DICT)
  1461. fz_throw(ctx, FZ_ERROR_FORMAT, "Bad journal format");
  1462. obj = pdf_parse_dict(ctx, doc, stm, &doc->lexbuf.base);
  1463. nis = pdf_dict_get_int(ctx, obj, PDF_NAME(NumSections));
  1464. if (nis < 0 || nis > doc->num_xref_sections)
  1465. fz_throw(ctx, FZ_ERROR_FORMAT, "Bad journal format");
  1466. pdf_fingerprint_file(ctx, doc, digest, nis);
  1467. file_size = pdf_dict_get_int(ctx, obj, PDF_NAME(FileSize));
  1468. fingerprint_obj = pdf_dict_get(ctx, obj, PDF_NAME(Fingerprint));
  1469. if (pdf_to_str_len(ctx, fingerprint_obj) != 16)
  1470. fz_throw(ctx, FZ_ERROR_FORMAT, "Bad journal fingerprint");
  1471. digests_match = (memcmp(pdf_to_str_buf(ctx, fingerprint_obj), digest, 16) == 0);
  1472. pos = pdf_dict_get_int(ctx, obj, PDF_NAME(HistoryPos));
  1473. }
  1474. fz_always(ctx)
  1475. {
  1476. pdf_drop_obj(ctx, obj);
  1477. }
  1478. fz_catch(ctx)
  1479. {
  1480. fz_rethrow(ctx);
  1481. }
  1482. if (!digests_match)
  1483. return;
  1484. if (doc->file_size < file_size)
  1485. return;
  1486. doc->journal = fz_malloc_struct(ctx, pdf_journal);
  1487. while (1)
  1488. {
  1489. int newobj;
  1490. fz_skip_space(ctx, stm);
  1491. if (fz_skip_string(ctx, stm, "entry\n") == 0)
  1492. {
  1493. /* Read the fragment title. */
  1494. char *title;
  1495. tok = pdf_lex(ctx, stm, &doc->lexbuf.base);
  1496. if (tok != PDF_TOK_STRING)
  1497. fz_throw(ctx, FZ_ERROR_FORMAT, "Bad string in journal");
  1498. title = fz_malloc(ctx, doc->lexbuf.base.len+1);
  1499. memcpy(title, doc->lexbuf.base.buffer, doc->lexbuf.base.len);
  1500. title[doc->lexbuf.base.len] = 0;
  1501. new_entry(ctx, doc, title);
  1502. continue;
  1503. }
  1504. if (fz_skip_string(ctx, stm, /*en*/"djournal") == 0)
  1505. break;
  1506. if (doc->journal->current == NULL)
  1507. fz_throw(ctx, FZ_ERROR_FORMAT, "Badly formed journal");
  1508. /* Read the object/stream for the next fragment. */
  1509. obj = pdf_parse_journal_obj(ctx, doc, stm, &num, &buffer, &newobj);
  1510. pdf_add_journal_fragment(ctx, doc, num, obj, buffer, newobj);
  1511. }
  1512. fz_skip_space(ctx, stm);
  1513. doc->journal->current = NULL;
  1514. if (pos > 0)
  1515. {
  1516. if (doc->journal->head == NULL)
  1517. fz_throw(ctx, FZ_ERROR_FORMAT, "Badly formed journal");
  1518. doc->journal->current = doc->journal->head;
  1519. while (--pos)
  1520. {
  1521. doc->journal->current = doc->journal->current->next;
  1522. if (doc->journal->current == NULL)
  1523. break;
  1524. }
  1525. }
  1526. doc->file_size = file_size;
  1527. /* We're about to make the last xref an incremental one. All incremental
  1528. * ones MUST be solid, but the snapshot might not have saved it as such,
  1529. * so solidify it now. */
  1530. pdf_ensure_solid_xref(ctx, doc, pdf_xref_len(ctx, doc));
  1531. doc->num_incremental_sections = nis;
  1532. if (nis > 0)
  1533. {
  1534. /* Ditch the trailer object out of the xref. Keep the direct
  1535. * trailer reference. */
  1536. pdf_delete_object(ctx, doc, pdf_obj_parent_num(ctx, doc->xref_sections[0].trailer));
  1537. pdf_set_obj_parent(ctx, doc->xref_sections[0].trailer, 0);
  1538. }
  1539. }
  1540. static void prepare_object_for_alteration(fz_context *ctx, pdf_obj *obj, pdf_obj *val)
  1541. {
  1542. pdf_document *doc, *val_doc;
  1543. int parent;
  1544. pdf_journal_fragment *frag;
  1545. pdf_journal_entry *entry;
  1546. pdf_obj *copy = NULL;
  1547. pdf_obj *orig;
  1548. fz_buffer *copy_stream = NULL;
  1549. int was_empty;
  1550. /*
  1551. obj should be a dict or an array. We don't care about
  1552. any other types, as they aren't 'containers'.
  1553. */
  1554. if (obj < PDF_LIMIT)
  1555. return;
  1556. switch (obj->kind)
  1557. {
  1558. case PDF_DICT:
  1559. doc = DICT(obj)->doc;
  1560. parent = DICT(obj)->parent_num;
  1561. break;
  1562. case PDF_ARRAY:
  1563. doc = ARRAY(obj)->doc;
  1564. parent = ARRAY(obj)->parent_num;
  1565. break;
  1566. default:
  1567. return;
  1568. }
  1569. assert(doc != NULL);
  1570. /* Do we need to drop the page maps? */
  1571. if (doc->rev_page_map || doc->fwd_page_map)
  1572. {
  1573. if (doc->non_structural_change)
  1574. {
  1575. /* No need to drop the reverse page map on a non-structural change. */
  1576. }
  1577. else if (parent == 0)
  1578. {
  1579. /* This object isn't linked into the document - can't change the
  1580. * pagemap. */
  1581. }
  1582. else if (doc->local_xref && doc->local_xref_nesting > 0)
  1583. {
  1584. /* We have a local_xref and it's in force. By convention, we
  1585. * never do structural changes in local_xrefs. */
  1586. }
  1587. else
  1588. pdf_drop_page_tree_internal(ctx, doc);
  1589. }
  1590. if (val)
  1591. {
  1592. val_doc = pdf_get_bound_document(ctx, val);
  1593. if (val_doc && val_doc != doc)
  1594. fz_throw(ctx, FZ_ERROR_ARGUMENT, "container and item belong to different documents");
  1595. }
  1596. /*
  1597. The newly linked object needs to record the parent_num.
  1598. */
  1599. if (parent != 0)
  1600. pdf_set_obj_parent(ctx, val, parent);
  1601. /*
  1602. parent_num == 0 while an object is being parsed from the file.
  1603. No further action is necessary.
  1604. */
  1605. if (parent == 0 || doc->save_in_progress || doc->repair_in_progress)
  1606. return;
  1607. if (doc->journal && doc->journal->nesting == 0)
  1608. fz_throw(ctx, FZ_ERROR_ARGUMENT, "Can't alter an object other than in an operation");
  1609. if (doc->local_xref)
  1610. {
  1611. /* We have a local_xref. If it's in force, then we're
  1612. * ready for alteration already. */
  1613. if (doc->local_xref_nesting > 0)
  1614. {
  1615. pdf_xref_ensure_local_object(ctx, doc, parent);
  1616. return;
  1617. }
  1618. else
  1619. {
  1620. /* The local xref isn't in force, and we're about
  1621. * to edit the document. This invalidates it, so
  1622. * throw it away. */
  1623. pdf_drop_local_xref_and_resources(ctx, doc);
  1624. }
  1625. }
  1626. // Empty store of items keyed on the object being changed.
  1627. if (parent != 0)
  1628. pdf_purge_object_from_store(ctx, doc, parent);
  1629. entry = NULL;
  1630. if (doc->journal)
  1631. {
  1632. /* We are about to add a fragment. Everything after 'current' in the
  1633. * history must be thrown away. If current is NULL, then *everything*
  1634. * must be thrown away. */
  1635. discard_journal_entries(ctx, doc->journal->current ? &doc->journal->current->next : &doc->journal->head);
  1636. /* We should be collating into a pending block. */
  1637. entry = doc->journal->pending_tail;
  1638. assert(entry);
  1639. /* If we've already stashed a value for this object in this fragment,
  1640. * we don't need to stash another one. It'll only confuse us later. */
  1641. for (frag = entry->head; frag != NULL; frag = frag->next)
  1642. if (frag->obj_num == parent)
  1643. {
  1644. entry = NULL;
  1645. break; /* Already stashed this one! */
  1646. }
  1647. }
  1648. /*
  1649. We need to ensure that the containing hierarchy of objects
  1650. has been moved to the incremental xref section.
  1651. */
  1652. was_empty = pdf_xref_ensure_incremental_object(ctx, doc, parent);
  1653. /* If we're not journalling, or we've already stashed an 'old' value for this
  1654. * object, just exit now. */
  1655. if (entry == NULL)
  1656. return;
  1657. /* Load the 'old' value and store it in a fragment. */
  1658. orig = pdf_load_object(ctx, doc, parent);
  1659. fz_var(copy);
  1660. fz_var(copy_stream);
  1661. fz_try(ctx)
  1662. {
  1663. if (was_empty)
  1664. {
  1665. /* was_empty = 1 iff, the the entry in the incremental xref was empty,
  1666. * and we copied any older value for that object forwards from an old xref.
  1667. * When we undo, we just want to blank the one in the incremental section.
  1668. * Effectively this is a "new object". */
  1669. copy = NULL;
  1670. copy_stream = NULL;
  1671. }
  1672. else
  1673. {
  1674. copy = pdf_deep_copy_obj(ctx, orig);
  1675. pdf_set_obj_parent(ctx, copy, parent);
  1676. if (pdf_obj_num_is_stream(ctx, doc, parent))
  1677. copy_stream = pdf_load_raw_stream_number(ctx, doc, parent);
  1678. }
  1679. pdf_add_journal_fragment(ctx, doc, parent, copy, copy_stream, was_empty);
  1680. }
  1681. fz_always(ctx)
  1682. {
  1683. pdf_drop_obj(ctx, orig);
  1684. }
  1685. fz_catch(ctx)
  1686. {
  1687. fz_drop_buffer(ctx, copy_stream);
  1688. pdf_drop_obj(ctx, copy);
  1689. fz_rethrow(ctx);
  1690. }
  1691. }
  1692. void
  1693. pdf_array_put(fz_context *ctx, pdf_obj *obj, int i, pdf_obj *item)
  1694. {
  1695. RESOLVE(obj);
  1696. if (!OBJ_IS_ARRAY(obj))
  1697. fz_throw(ctx, FZ_ERROR_ARGUMENT, "not an array (%s)", pdf_objkindstr(obj));
  1698. if (i == ARRAY(obj)->len)
  1699. {
  1700. pdf_array_push(ctx, obj, item);
  1701. return;
  1702. }
  1703. if (i < 0 || i > ARRAY(obj)->len)
  1704. fz_throw(ctx, FZ_ERROR_ARGUMENT, "index out of bounds");
  1705. prepare_object_for_alteration(ctx, obj, item);
  1706. pdf_drop_obj(ctx, ARRAY(obj)->items[i]);
  1707. ARRAY(obj)->items[i] = pdf_keep_obj(ctx, item);
  1708. }
  1709. void
  1710. pdf_array_put_drop(fz_context *ctx, pdf_obj *obj, int i, pdf_obj *item)
  1711. {
  1712. fz_try(ctx)
  1713. pdf_array_put(ctx, obj, i, item);
  1714. fz_always(ctx)
  1715. pdf_drop_obj(ctx, item);
  1716. fz_catch(ctx)
  1717. fz_rethrow(ctx);
  1718. }
  1719. void
  1720. pdf_array_push(fz_context *ctx, pdf_obj *obj, pdf_obj *item)
  1721. {
  1722. RESOLVE(obj);
  1723. if (!OBJ_IS_ARRAY(obj))
  1724. fz_throw(ctx, FZ_ERROR_ARGUMENT, "not an array (%s)", pdf_objkindstr(obj));
  1725. prepare_object_for_alteration(ctx, obj, item);
  1726. if (ARRAY(obj)->len + 1 > ARRAY(obj)->cap)
  1727. pdf_array_grow(ctx, ARRAY(obj));
  1728. ARRAY(obj)->items[ARRAY(obj)->len] = pdf_keep_obj(ctx, item);
  1729. ARRAY(obj)->len++;
  1730. }
  1731. void
  1732. pdf_array_push_drop(fz_context *ctx, pdf_obj *obj, pdf_obj *item)
  1733. {
  1734. fz_try(ctx)
  1735. pdf_array_push(ctx, obj, item);
  1736. fz_always(ctx)
  1737. pdf_drop_obj(ctx, item);
  1738. fz_catch(ctx)
  1739. fz_rethrow(ctx);
  1740. }
  1741. void
  1742. pdf_array_insert(fz_context *ctx, pdf_obj *obj, pdf_obj *item, int i)
  1743. {
  1744. RESOLVE(obj);
  1745. if (!OBJ_IS_ARRAY(obj))
  1746. fz_throw(ctx, FZ_ERROR_ARGUMENT, "not an array (%s)", pdf_objkindstr(obj));
  1747. if (i < 0 || i > ARRAY(obj)->len)
  1748. fz_throw(ctx, FZ_ERROR_ARGUMENT, "index out of bounds");
  1749. prepare_object_for_alteration(ctx, obj, item);
  1750. if (ARRAY(obj)->len + 1 > ARRAY(obj)->cap)
  1751. pdf_array_grow(ctx, ARRAY(obj));
  1752. memmove(ARRAY(obj)->items + i + 1, ARRAY(obj)->items + i, (ARRAY(obj)->len - i) * sizeof(pdf_obj*));
  1753. ARRAY(obj)->items[i] = pdf_keep_obj(ctx, item);
  1754. ARRAY(obj)->len++;
  1755. }
  1756. void
  1757. pdf_array_insert_drop(fz_context *ctx, pdf_obj *obj, pdf_obj *item, int i)
  1758. {
  1759. fz_try(ctx)
  1760. pdf_array_insert(ctx, obj, item, i);
  1761. fz_always(ctx)
  1762. pdf_drop_obj(ctx, item);
  1763. fz_catch(ctx)
  1764. fz_rethrow(ctx);
  1765. }
  1766. void
  1767. pdf_array_delete(fz_context *ctx, pdf_obj *obj, int i)
  1768. {
  1769. RESOLVE(obj);
  1770. if (!OBJ_IS_ARRAY(obj))
  1771. fz_throw(ctx, FZ_ERROR_ARGUMENT, "not an array (%s)", pdf_objkindstr(obj));
  1772. if (i < 0 || i >= ARRAY(obj)->len)
  1773. fz_throw(ctx, FZ_ERROR_ARGUMENT, "index out of bounds");
  1774. prepare_object_for_alteration(ctx, obj, NULL);
  1775. pdf_drop_obj(ctx, ARRAY(obj)->items[i]);
  1776. ARRAY(obj)->items[i] = 0;
  1777. ARRAY(obj)->len--;
  1778. memmove(ARRAY(obj)->items + i, ARRAY(obj)->items + i + 1, (ARRAY(obj)->len - i) * sizeof(pdf_obj*));
  1779. }
  1780. int
  1781. pdf_array_contains(fz_context *ctx, pdf_obj *arr, pdf_obj *obj)
  1782. {
  1783. int i, len;
  1784. len = pdf_array_len(ctx, arr);
  1785. for (i = 0; i < len; i++)
  1786. if (!pdf_objcmp(ctx, pdf_array_get(ctx, arr, i), obj))
  1787. return 1;
  1788. return 0;
  1789. }
  1790. int
  1791. pdf_array_find(fz_context *ctx, pdf_obj *arr, pdf_obj *obj)
  1792. {
  1793. int i, len;
  1794. len = pdf_array_len(ctx, arr);
  1795. for (i = 0; i < len; i++)
  1796. if (!pdf_objcmp(ctx, pdf_array_get(ctx, arr, i), obj))
  1797. return i;
  1798. return -1;
  1799. }
  1800. pdf_obj *pdf_new_point(fz_context *ctx, pdf_document *doc, fz_point point)
  1801. {
  1802. pdf_obj *arr = pdf_new_array(ctx, doc, 2);
  1803. fz_try(ctx)
  1804. {
  1805. pdf_array_push_real(ctx, arr, point.x);
  1806. pdf_array_push_real(ctx, arr, point.y);
  1807. }
  1808. fz_catch(ctx)
  1809. {
  1810. pdf_drop_obj(ctx, arr);
  1811. fz_rethrow(ctx);
  1812. }
  1813. return arr;
  1814. }
  1815. pdf_obj *pdf_new_rect(fz_context *ctx, pdf_document *doc, fz_rect rect)
  1816. {
  1817. pdf_obj *arr = pdf_new_array(ctx, doc, 4);
  1818. fz_try(ctx)
  1819. {
  1820. pdf_array_push_real(ctx, arr, rect.x0);
  1821. pdf_array_push_real(ctx, arr, rect.y0);
  1822. pdf_array_push_real(ctx, arr, rect.x1);
  1823. pdf_array_push_real(ctx, arr, rect.y1);
  1824. }
  1825. fz_catch(ctx)
  1826. {
  1827. pdf_drop_obj(ctx, arr);
  1828. fz_rethrow(ctx);
  1829. }
  1830. return arr;
  1831. }
  1832. pdf_obj *pdf_new_matrix(fz_context *ctx, pdf_document *doc, fz_matrix mtx)
  1833. {
  1834. pdf_obj *arr = pdf_new_array(ctx, doc, 6);
  1835. fz_try(ctx)
  1836. {
  1837. pdf_array_push_real(ctx, arr, mtx.a);
  1838. pdf_array_push_real(ctx, arr, mtx.b);
  1839. pdf_array_push_real(ctx, arr, mtx.c);
  1840. pdf_array_push_real(ctx, arr, mtx.d);
  1841. pdf_array_push_real(ctx, arr, mtx.e);
  1842. pdf_array_push_real(ctx, arr, mtx.f);
  1843. }
  1844. fz_catch(ctx)
  1845. {
  1846. pdf_drop_obj(ctx, arr);
  1847. fz_rethrow(ctx);
  1848. }
  1849. return arr;
  1850. }
  1851. pdf_obj *pdf_new_date(fz_context *ctx, pdf_document *doc, int64_t time)
  1852. {
  1853. char s[40];
  1854. if (!pdf_format_date(ctx, time, s, nelem(s)))
  1855. return NULL;
  1856. return pdf_new_string(ctx, s, strlen(s));
  1857. }
  1858. /* dicts may only have names as keys! */
  1859. static int keyvalcmp(const void *ap, const void *bp)
  1860. {
  1861. const struct keyval *a = ap;
  1862. const struct keyval *b = bp;
  1863. const char *an;
  1864. const char *bn;
  1865. /* We should never get a->k == NULL or b->k == NULL. If we
  1866. * do, then they match. */
  1867. if (a->k < PDF_LIMIT)
  1868. an = PDF_NAME_LIST[(intptr_t)a->k];
  1869. else if (a->k >= PDF_LIMIT && a->k->kind == PDF_NAME)
  1870. an = NAME(a->k)->n;
  1871. else
  1872. return 0;
  1873. if (b->k < PDF_LIMIT)
  1874. bn = PDF_NAME_LIST[(intptr_t)b->k];
  1875. else if (b->k >= PDF_LIMIT && b->k->kind == PDF_NAME)
  1876. bn = NAME(b->k)->n;
  1877. else
  1878. return 0;
  1879. return strcmp(an, bn);
  1880. }
  1881. pdf_obj *
  1882. pdf_new_dict(fz_context *ctx, pdf_document *doc, int initialcap)
  1883. {
  1884. pdf_obj_dict *obj;
  1885. int i;
  1886. if (doc == NULL)
  1887. fz_throw(ctx, FZ_ERROR_ARGUMENT, "cannot create dictionary without a document");
  1888. obj = Memento_label(fz_malloc(ctx, sizeof(pdf_obj_dict)), "pdf_obj(dict)");
  1889. obj->super.refs = 1;
  1890. obj->super.kind = PDF_DICT;
  1891. obj->super.flags = 0;
  1892. obj->doc = doc;
  1893. obj->parent_num = 0;
  1894. obj->len = 0;
  1895. obj->cap = initialcap > 1 ? initialcap : 10;
  1896. fz_try(ctx)
  1897. {
  1898. DICT(obj)->items = Memento_label(fz_malloc_array(ctx, DICT(obj)->cap, struct keyval), "dict_items");
  1899. }
  1900. fz_catch(ctx)
  1901. {
  1902. fz_free(ctx, obj);
  1903. fz_rethrow(ctx);
  1904. }
  1905. for (i = 0; i < DICT(obj)->cap; i++)
  1906. {
  1907. DICT(obj)->items[i].k = NULL;
  1908. DICT(obj)->items[i].v = NULL;
  1909. }
  1910. return &obj->super;
  1911. }
  1912. static void
  1913. pdf_dict_grow(fz_context *ctx, pdf_obj *obj)
  1914. {
  1915. int i;
  1916. int new_cap = (DICT(obj)->cap * 3) / 2;
  1917. DICT(obj)->items = fz_realloc_array(ctx, DICT(obj)->items, new_cap, struct keyval);
  1918. DICT(obj)->cap = new_cap;
  1919. for (i = DICT(obj)->len; i < DICT(obj)->cap; i++)
  1920. {
  1921. DICT(obj)->items[i].k = NULL;
  1922. DICT(obj)->items[i].v = NULL;
  1923. }
  1924. }
  1925. pdf_obj *
  1926. pdf_copy_dict(fz_context *ctx, pdf_obj *obj)
  1927. {
  1928. pdf_document *doc;
  1929. pdf_obj *dict;
  1930. int i, n;
  1931. RESOLVE(obj);
  1932. if (!OBJ_IS_DICT(obj))
  1933. fz_throw(ctx, FZ_ERROR_ARGUMENT, "not a dict (%s)", pdf_objkindstr(obj));
  1934. doc = DICT(obj)->doc;
  1935. n = pdf_dict_len(ctx, obj);
  1936. dict = pdf_new_dict(ctx, doc, n);
  1937. fz_try(ctx)
  1938. for (i = 0; i < n; i++)
  1939. pdf_dict_put(ctx, dict, pdf_dict_get_key(ctx, obj, i), pdf_dict_get_val(ctx, obj, i));
  1940. fz_catch(ctx)
  1941. {
  1942. pdf_drop_obj(ctx, dict);
  1943. fz_rethrow(ctx);
  1944. }
  1945. return dict;
  1946. }
  1947. int
  1948. pdf_dict_len(fz_context *ctx, pdf_obj *obj)
  1949. {
  1950. RESOLVE(obj);
  1951. if (!OBJ_IS_DICT(obj))
  1952. return 0;
  1953. return DICT(obj)->len;
  1954. }
  1955. pdf_obj *
  1956. pdf_dict_get_key(fz_context *ctx, pdf_obj *obj, int i)
  1957. {
  1958. RESOLVE(obj);
  1959. if (!OBJ_IS_DICT(obj))
  1960. return NULL;
  1961. if (i < 0 || i >= DICT(obj)->len)
  1962. return NULL;
  1963. return DICT(obj)->items[i].k;
  1964. }
  1965. pdf_obj *
  1966. pdf_dict_get_val(fz_context *ctx, pdf_obj *obj, int i)
  1967. {
  1968. RESOLVE(obj);
  1969. if (!OBJ_IS_DICT(obj))
  1970. return NULL;
  1971. if (i < 0 || i >= DICT(obj)->len)
  1972. return NULL;
  1973. return DICT(obj)->items[i].v;
  1974. }
  1975. void
  1976. pdf_dict_put_val_null(fz_context *ctx, pdf_obj *obj, int idx)
  1977. {
  1978. RESOLVE(obj);
  1979. if (!OBJ_IS_DICT(obj))
  1980. fz_throw(ctx, FZ_ERROR_ARGUMENT, "not a dict (%s)", pdf_objkindstr(obj));
  1981. if (idx < 0 || idx >= DICT(obj)->len)
  1982. fz_throw(ctx, FZ_ERROR_ARGUMENT, "index out of bounds");
  1983. prepare_object_for_alteration(ctx, obj, NULL);
  1984. pdf_drop_obj(ctx, DICT(obj)->items[idx].v);
  1985. DICT(obj)->items[idx].v = PDF_NULL;
  1986. }
  1987. /* Returns 0 <= i < len for key found. Returns -1-len < i <= -1 for key
  1988. * not found, but with insertion point -1-i. */
  1989. static int
  1990. pdf_dict_finds(fz_context *ctx, pdf_obj *obj, const char *key)
  1991. {
  1992. int len = DICT(obj)->len;
  1993. if ((obj->flags & PDF_FLAGS_SORTED) && len > 0)
  1994. {
  1995. int l = 0;
  1996. int r = len - 1;
  1997. if (strcmp(pdf_to_name(ctx, DICT(obj)->items[r].k), key) < 0)
  1998. {
  1999. return -1 - (r+1);
  2000. }
  2001. while (l <= r)
  2002. {
  2003. int m = (l + r) >> 1;
  2004. int c = -strcmp(pdf_to_name(ctx, DICT(obj)->items[m].k), key);
  2005. if (c < 0)
  2006. r = m - 1;
  2007. else if (c > 0)
  2008. l = m + 1;
  2009. else
  2010. return m;
  2011. }
  2012. return -1 - l;
  2013. }
  2014. else
  2015. {
  2016. int i;
  2017. for (i = 0; i < len; i++)
  2018. if (strcmp(pdf_to_name(ctx, DICT(obj)->items[i].k), key) == 0)
  2019. return i;
  2020. return -1 - len;
  2021. }
  2022. }
  2023. static int
  2024. pdf_dict_find(fz_context *ctx, pdf_obj *obj, pdf_obj *key)
  2025. {
  2026. int len = DICT(obj)->len;
  2027. if ((obj->flags & PDF_FLAGS_SORTED) && len > 0)
  2028. {
  2029. int l = 0;
  2030. int r = len - 1;
  2031. pdf_obj *k = DICT(obj)->items[r].k;
  2032. if (k == key || (k >= PDF_LIMIT && strcmp(NAME(k)->n, PDF_NAME_LIST[(intptr_t)key]) < 0))
  2033. {
  2034. return -1 - (r+1);
  2035. }
  2036. while (l <= r)
  2037. {
  2038. int m = (l + r) >> 1;
  2039. int c;
  2040. k = DICT(obj)->items[m].k;
  2041. c = (k < PDF_LIMIT ? (char *)key-(char *)k : -strcmp(NAME(k)->n, PDF_NAME_LIST[(intptr_t)key]));
  2042. if (c < 0)
  2043. r = m - 1;
  2044. else if (c > 0)
  2045. l = m + 1;
  2046. else
  2047. return m;
  2048. }
  2049. return -1 - l;
  2050. }
  2051. else
  2052. {
  2053. int i;
  2054. for (i = 0; i < len; i++)
  2055. {
  2056. pdf_obj *k = DICT(obj)->items[i].k;
  2057. if (k < PDF_LIMIT)
  2058. {
  2059. if (k == key)
  2060. return i;
  2061. }
  2062. else
  2063. {
  2064. if (!strcmp(PDF_NAME_LIST[(intptr_t)key], NAME(k)->n))
  2065. return i;
  2066. }
  2067. }
  2068. return -1 - len;
  2069. }
  2070. }
  2071. pdf_obj *
  2072. pdf_dict_gets(fz_context *ctx, pdf_obj *obj, const char *key)
  2073. {
  2074. int i;
  2075. RESOLVE(obj);
  2076. if (!OBJ_IS_DICT(obj))
  2077. return NULL;
  2078. if (!key)
  2079. return NULL;
  2080. i = pdf_dict_finds(ctx, obj, key);
  2081. if (i >= 0)
  2082. return DICT(obj)->items[i].v;
  2083. return NULL;
  2084. }
  2085. pdf_obj *
  2086. pdf_dict_getp(fz_context *ctx, pdf_obj *obj, const char *keys)
  2087. {
  2088. char buf[256];
  2089. char *k, *e;
  2090. RESOLVE(obj);
  2091. if (!OBJ_IS_DICT(obj))
  2092. return NULL;
  2093. if (strlen(keys)+1 > 256)
  2094. fz_throw(ctx, FZ_ERROR_ARGUMENT, "path too long");
  2095. strcpy(buf, keys);
  2096. e = buf;
  2097. while (*e && obj)
  2098. {
  2099. k = e;
  2100. while (*e != '/' && *e != '\0')
  2101. e++;
  2102. if (*e == '/')
  2103. {
  2104. *e = '\0';
  2105. e++;
  2106. }
  2107. obj = pdf_dict_gets(ctx, obj, k);
  2108. }
  2109. return obj;
  2110. }
  2111. pdf_obj *
  2112. pdf_dict_getl(fz_context *ctx, pdf_obj *obj, ...)
  2113. {
  2114. va_list keys;
  2115. pdf_obj *key;
  2116. va_start(keys, obj);
  2117. while (obj != NULL && (key = va_arg(keys, pdf_obj *)) != NULL)
  2118. {
  2119. obj = pdf_dict_get(ctx, obj, key);
  2120. }
  2121. va_end(keys);
  2122. return obj;
  2123. }
  2124. pdf_obj *
  2125. pdf_dict_get(fz_context *ctx, pdf_obj *obj, pdf_obj *key)
  2126. {
  2127. int i;
  2128. RESOLVE(obj);
  2129. if (!OBJ_IS_DICT(obj))
  2130. return NULL;
  2131. if (!OBJ_IS_NAME(key))
  2132. return NULL;
  2133. if (key < PDF_LIMIT)
  2134. i = pdf_dict_find(ctx, obj, key);
  2135. else
  2136. i = pdf_dict_finds(ctx, obj, pdf_to_name(ctx, key));
  2137. if (i >= 0)
  2138. return DICT(obj)->items[i].v;
  2139. return NULL;
  2140. }
  2141. pdf_obj *
  2142. pdf_dict_getsa(fz_context *ctx, pdf_obj *obj, const char *key, const char *abbrev)
  2143. {
  2144. pdf_obj *v;
  2145. v = pdf_dict_gets(ctx, obj, key);
  2146. if (v)
  2147. return v;
  2148. return pdf_dict_gets(ctx, obj, abbrev);
  2149. }
  2150. pdf_obj *
  2151. pdf_dict_geta(fz_context *ctx, pdf_obj *obj, pdf_obj *key, pdf_obj *abbrev)
  2152. {
  2153. pdf_obj *v;
  2154. /* ISO 32000-2:2020 (PDF 2.0) - abbreviated names take precedence. */
  2155. v = pdf_dict_get(ctx, obj, abbrev);
  2156. if (v)
  2157. return v;
  2158. return pdf_dict_get(ctx, obj, key);
  2159. }
  2160. static void
  2161. pdf_dict_get_put(fz_context *ctx, pdf_obj *obj, pdf_obj *key, pdf_obj *val, pdf_obj **old_val)
  2162. {
  2163. int i;
  2164. if (old_val)
  2165. *old_val = NULL;
  2166. RESOLVE(obj);
  2167. if (!OBJ_IS_DICT(obj))
  2168. fz_throw(ctx, FZ_ERROR_ARGUMENT, "not a dict (%s)", pdf_objkindstr(obj));
  2169. if (!OBJ_IS_NAME(key))
  2170. fz_throw(ctx, FZ_ERROR_ARGUMENT, "key is not a name (%s)", pdf_objkindstr(obj));
  2171. if (DICT(obj)->len > 100 && !(obj->flags & PDF_FLAGS_SORTED))
  2172. pdf_sort_dict(ctx, obj);
  2173. if (key < PDF_LIMIT)
  2174. i = pdf_dict_find(ctx, obj, key);
  2175. else
  2176. i = pdf_dict_finds(ctx, obj, pdf_to_name(ctx, key));
  2177. prepare_object_for_alteration(ctx, obj, val);
  2178. if (i >= 0 && i < DICT(obj)->len)
  2179. {
  2180. if (DICT(obj)->items[i].v != val)
  2181. {
  2182. pdf_obj *d = DICT(obj)->items[i].v;
  2183. DICT(obj)->items[i].v = pdf_keep_obj(ctx, val);
  2184. if (old_val)
  2185. *old_val = d;
  2186. else
  2187. pdf_drop_obj(ctx, d);
  2188. }
  2189. }
  2190. else
  2191. {
  2192. if (DICT(obj)->len + 1 > DICT(obj)->cap)
  2193. pdf_dict_grow(ctx, obj);
  2194. i = -1-i;
  2195. if ((obj->flags & PDF_FLAGS_SORTED) && DICT(obj)->len > 0)
  2196. memmove(&DICT(obj)->items[i + 1],
  2197. &DICT(obj)->items[i],
  2198. (DICT(obj)->len - i) * sizeof(struct keyval));
  2199. DICT(obj)->items[i].k = pdf_keep_obj(ctx, key);
  2200. DICT(obj)->items[i].v = pdf_keep_obj(ctx, val);
  2201. DICT(obj)->len ++;
  2202. }
  2203. }
  2204. void
  2205. pdf_dict_put(fz_context *ctx, pdf_obj *obj, pdf_obj *key, pdf_obj *val)
  2206. {
  2207. pdf_dict_get_put(ctx, obj, key, val, NULL);
  2208. }
  2209. void
  2210. pdf_dict_put_drop(fz_context *ctx, pdf_obj *obj, pdf_obj *key, pdf_obj *val)
  2211. {
  2212. fz_try(ctx)
  2213. pdf_dict_get_put(ctx, obj, key, val, NULL);
  2214. fz_always(ctx)
  2215. pdf_drop_obj(ctx, val);
  2216. fz_catch(ctx)
  2217. fz_rethrow(ctx);
  2218. }
  2219. void
  2220. pdf_dict_get_put_drop(fz_context *ctx, pdf_obj *obj, pdf_obj *key, pdf_obj *val, pdf_obj **old_val)
  2221. {
  2222. fz_try(ctx)
  2223. pdf_dict_get_put(ctx, obj, key, val, old_val);
  2224. fz_always(ctx)
  2225. pdf_drop_obj(ctx, val);
  2226. fz_catch(ctx)
  2227. fz_rethrow(ctx);
  2228. }
  2229. void
  2230. pdf_dict_puts(fz_context *ctx, pdf_obj *obj, const char *key, pdf_obj *val)
  2231. {
  2232. pdf_obj *keyobj;
  2233. RESOLVE(obj);
  2234. if (!OBJ_IS_DICT(obj))
  2235. fz_throw(ctx, FZ_ERROR_ARGUMENT, "not a dict (%s)", pdf_objkindstr(obj));
  2236. keyobj = pdf_new_name(ctx, key);
  2237. fz_try(ctx)
  2238. pdf_dict_put(ctx, obj, keyobj, val);
  2239. fz_always(ctx)
  2240. pdf_drop_obj(ctx, keyobj);
  2241. fz_catch(ctx)
  2242. fz_rethrow(ctx);
  2243. }
  2244. void
  2245. pdf_dict_puts_drop(fz_context *ctx, pdf_obj *obj, const char *key, pdf_obj *val)
  2246. {
  2247. pdf_obj *keyobj;
  2248. RESOLVE(obj);
  2249. if (!OBJ_IS_DICT(obj))
  2250. fz_throw(ctx, FZ_ERROR_ARGUMENT, "not a dict (%s)", pdf_objkindstr(obj));
  2251. keyobj = pdf_new_name(ctx, key);
  2252. fz_var(keyobj);
  2253. fz_try(ctx)
  2254. pdf_dict_put(ctx, obj, keyobj, val);
  2255. fz_always(ctx)
  2256. {
  2257. pdf_drop_obj(ctx, keyobj);
  2258. pdf_drop_obj(ctx, val);
  2259. }
  2260. fz_catch(ctx)
  2261. {
  2262. fz_rethrow(ctx);
  2263. }
  2264. }
  2265. void
  2266. pdf_dict_putp(fz_context *ctx, pdf_obj *obj, const char *keys, pdf_obj *val)
  2267. {
  2268. pdf_document *doc;
  2269. char buf[256];
  2270. char *k, *e;
  2271. pdf_obj *cobj = NULL;
  2272. RESOLVE(obj);
  2273. if (!OBJ_IS_DICT(obj))
  2274. fz_throw(ctx, FZ_ERROR_ARGUMENT, "not a dict (%s)", pdf_objkindstr(obj));
  2275. if (strlen(keys)+1 > 256)
  2276. fz_throw(ctx, FZ_ERROR_ARGUMENT, "path too long");
  2277. doc = DICT(obj)->doc;
  2278. strcpy(buf, keys);
  2279. e = buf;
  2280. while (*e)
  2281. {
  2282. k = e;
  2283. while (*e != '/' && *e != '\0')
  2284. e++;
  2285. if (*e == '/')
  2286. {
  2287. *e = '\0';
  2288. e++;
  2289. }
  2290. if (*e)
  2291. {
  2292. /* Not the last key in the key path. Create subdict if not already there. */
  2293. cobj = pdf_dict_gets(ctx, obj, k);
  2294. if (!pdf_is_dict(ctx, cobj))
  2295. {
  2296. cobj = pdf_new_dict(ctx, doc, 1);
  2297. fz_try(ctx)
  2298. pdf_dict_puts(ctx, obj, k, cobj);
  2299. fz_always(ctx)
  2300. pdf_drop_obj(ctx, cobj);
  2301. fz_catch(ctx)
  2302. fz_rethrow(ctx);
  2303. }
  2304. /* Move to subdict */
  2305. obj = cobj;
  2306. }
  2307. else
  2308. {
  2309. /* Last key. Use it to store the value */
  2310. /* Use val = NULL to request delete */
  2311. if (val)
  2312. pdf_dict_puts(ctx, obj, k, val);
  2313. else
  2314. pdf_dict_dels(ctx, obj, k);
  2315. }
  2316. }
  2317. }
  2318. void
  2319. pdf_dict_putp_drop(fz_context *ctx, pdf_obj *obj, const char *keys, pdf_obj *val)
  2320. {
  2321. fz_try(ctx)
  2322. pdf_dict_putp(ctx, obj, keys, val);
  2323. fz_always(ctx)
  2324. pdf_drop_obj(ctx, val);
  2325. fz_catch(ctx)
  2326. fz_rethrow(ctx);
  2327. }
  2328. static void
  2329. pdf_dict_vputl(fz_context *ctx, pdf_obj *obj, pdf_obj *val, va_list keys)
  2330. {
  2331. pdf_obj *key;
  2332. pdf_obj *next_key;
  2333. pdf_obj *next_obj;
  2334. pdf_document *doc;
  2335. RESOLVE(obj);
  2336. if (!OBJ_IS_DICT(obj))
  2337. fz_throw(ctx, FZ_ERROR_ARGUMENT, "not a dict (%s)", pdf_objkindstr(obj));
  2338. doc = DICT(obj)->doc;
  2339. key = va_arg(keys, pdf_obj *);
  2340. if (key == NULL)
  2341. return;
  2342. while ((next_key = va_arg(keys, pdf_obj *)) != NULL)
  2343. {
  2344. next_obj = pdf_dict_get(ctx, obj, key);
  2345. if (next_obj == NULL)
  2346. goto new_obj;
  2347. obj = next_obj;
  2348. key = next_key;
  2349. }
  2350. pdf_dict_put(ctx, obj, key, val);
  2351. return;
  2352. new_obj:
  2353. /* We have to create entries */
  2354. do
  2355. {
  2356. next_obj = pdf_new_dict(ctx, doc, 1);
  2357. pdf_dict_put_drop(ctx, obj, key, next_obj);
  2358. obj = next_obj;
  2359. key = next_key;
  2360. }
  2361. while ((next_key = va_arg(keys, pdf_obj *)) != NULL);
  2362. pdf_dict_put(ctx, obj, key, val);
  2363. return;
  2364. }
  2365. void
  2366. pdf_dict_putl(fz_context *ctx, pdf_obj *obj, pdf_obj *val, ...)
  2367. {
  2368. va_list keys;
  2369. va_start(keys, val);
  2370. fz_try(ctx)
  2371. pdf_dict_vputl(ctx, obj, val, keys);
  2372. fz_always(ctx)
  2373. va_end(keys);
  2374. fz_catch(ctx)
  2375. fz_rethrow(ctx);
  2376. }
  2377. void
  2378. pdf_dict_putl_drop(fz_context *ctx, pdf_obj *obj, pdf_obj *val, ...)
  2379. {
  2380. va_list keys;
  2381. va_start(keys, val);
  2382. fz_try(ctx)
  2383. pdf_dict_vputl(ctx, obj, val, keys);
  2384. fz_always(ctx)
  2385. {
  2386. pdf_drop_obj(ctx, val);
  2387. va_end(keys);
  2388. }
  2389. fz_catch(ctx)
  2390. fz_rethrow(ctx);
  2391. }
  2392. void
  2393. pdf_dict_dels(fz_context *ctx, pdf_obj *obj, const char *key)
  2394. {
  2395. int i;
  2396. RESOLVE(obj);
  2397. if (!OBJ_IS_DICT(obj))
  2398. fz_throw(ctx, FZ_ERROR_ARGUMENT, "not a dict (%s)", pdf_objkindstr(obj));
  2399. if (!key)
  2400. fz_throw(ctx, FZ_ERROR_ARGUMENT, "key is null");
  2401. prepare_object_for_alteration(ctx, obj, NULL);
  2402. i = pdf_dict_finds(ctx, obj, key);
  2403. if (i >= 0)
  2404. {
  2405. pdf_drop_obj(ctx, DICT(obj)->items[i].k);
  2406. pdf_drop_obj(ctx, DICT(obj)->items[i].v);
  2407. obj->flags &= ~PDF_FLAGS_SORTED;
  2408. DICT(obj)->items[i] = DICT(obj)->items[DICT(obj)->len-1];
  2409. DICT(obj)->len --;
  2410. }
  2411. }
  2412. void
  2413. pdf_dict_del(fz_context *ctx, pdf_obj *obj, pdf_obj *key)
  2414. {
  2415. if (!OBJ_IS_NAME(key))
  2416. fz_throw(ctx, FZ_ERROR_ARGUMENT, "key is not a name (%s)", pdf_objkindstr(key));
  2417. if (key < PDF_LIMIT)
  2418. pdf_dict_dels(ctx, obj, PDF_NAME_LIST[(intptr_t)key]);
  2419. else
  2420. pdf_dict_dels(ctx, obj, NAME(key)->n);
  2421. }
  2422. void
  2423. pdf_sort_dict(fz_context *ctx, pdf_obj *obj)
  2424. {
  2425. RESOLVE(obj);
  2426. if (!OBJ_IS_DICT(obj))
  2427. return;
  2428. if (!(obj->flags & PDF_FLAGS_SORTED))
  2429. {
  2430. qsort(DICT(obj)->items, DICT(obj)->len, sizeof(struct keyval), keyvalcmp);
  2431. obj->flags |= PDF_FLAGS_SORTED;
  2432. }
  2433. }
  2434. pdf_obj *
  2435. pdf_deep_copy_obj(fz_context *ctx, pdf_obj *obj)
  2436. {
  2437. if (obj < PDF_LIMIT)
  2438. {
  2439. return obj;
  2440. }
  2441. if (obj->kind == PDF_DICT)
  2442. {
  2443. pdf_document *doc = DICT(obj)->doc;
  2444. int n = pdf_dict_len(ctx, obj);
  2445. pdf_obj *dict = pdf_new_dict(ctx, doc, n);
  2446. int i;
  2447. fz_try(ctx)
  2448. for (i = 0; i < n; i++)
  2449. {
  2450. pdf_obj *obj_copy = pdf_deep_copy_obj(ctx, pdf_dict_get_val(ctx, obj, i));
  2451. pdf_dict_put_drop(ctx, dict, pdf_dict_get_key(ctx, obj, i), obj_copy);
  2452. }
  2453. fz_catch(ctx)
  2454. {
  2455. pdf_drop_obj(ctx, dict);
  2456. fz_rethrow(ctx);
  2457. }
  2458. DICT(dict)->parent_num = DICT(obj)->parent_num;
  2459. return dict;
  2460. }
  2461. else if (obj->kind == PDF_ARRAY)
  2462. {
  2463. pdf_document *doc = ARRAY(obj)->doc;
  2464. int n = pdf_array_len(ctx, obj);
  2465. pdf_obj *arr = pdf_new_array(ctx, doc, n);
  2466. int i;
  2467. fz_try(ctx)
  2468. for (i = 0; i < n; i++)
  2469. {
  2470. pdf_obj *obj_copy = pdf_deep_copy_obj(ctx, pdf_array_get(ctx, obj, i));
  2471. pdf_array_push_drop(ctx, arr, obj_copy);
  2472. }
  2473. fz_catch(ctx)
  2474. {
  2475. pdf_drop_obj(ctx, arr);
  2476. fz_rethrow(ctx);
  2477. }
  2478. ARRAY(arr)->parent_num = ARRAY(obj)->parent_num;
  2479. return arr;
  2480. }
  2481. else
  2482. {
  2483. return pdf_keep_obj(ctx, obj);
  2484. }
  2485. }
  2486. /* obj marking and unmarking functions - to avoid infinite recursions. */
  2487. int
  2488. pdf_obj_marked(fz_context *ctx, pdf_obj *obj)
  2489. {
  2490. RESOLVE(obj);
  2491. if (obj < PDF_LIMIT)
  2492. return 0;
  2493. return !!(obj->flags & PDF_FLAGS_MARKED);
  2494. }
  2495. int
  2496. pdf_mark_obj(fz_context *ctx, pdf_obj *obj)
  2497. {
  2498. int marked;
  2499. RESOLVE(obj);
  2500. if (obj < PDF_LIMIT)
  2501. return 0;
  2502. marked = !!(obj->flags & PDF_FLAGS_MARKED);
  2503. obj->flags |= PDF_FLAGS_MARKED;
  2504. return marked;
  2505. }
  2506. void
  2507. pdf_unmark_obj(fz_context *ctx, pdf_obj *obj)
  2508. {
  2509. RESOLVE(obj);
  2510. if (obj < PDF_LIMIT)
  2511. return;
  2512. obj->flags &= ~PDF_FLAGS_MARKED;
  2513. }
  2514. int
  2515. pdf_cycle(fz_context *ctx, pdf_cycle_list *here, pdf_cycle_list *up, pdf_obj *obj)
  2516. {
  2517. int num = pdf_to_num(ctx, obj);
  2518. if (num > 0)
  2519. {
  2520. pdf_cycle_list *x = up;
  2521. while (x)
  2522. {
  2523. if (x->num == num)
  2524. return 1;
  2525. x = x->up;
  2526. }
  2527. }
  2528. here->up = up;
  2529. here->num = num;
  2530. return 0;
  2531. }
  2532. pdf_mark_bits *
  2533. pdf_new_mark_bits(fz_context *ctx, pdf_document *doc)
  2534. {
  2535. int n = pdf_xref_len(ctx, doc);
  2536. int nb = (n + 7) >> 3;
  2537. pdf_mark_bits *marks = fz_calloc(ctx, offsetof(pdf_mark_bits, bits) + nb, 1);
  2538. marks->len = n;
  2539. return marks;
  2540. }
  2541. void
  2542. pdf_drop_mark_bits(fz_context *ctx, pdf_mark_bits *marks)
  2543. {
  2544. fz_free(ctx, marks);
  2545. }
  2546. void pdf_mark_bits_reset(fz_context *ctx, pdf_mark_bits *marks)
  2547. {
  2548. memset(marks->bits, 0, (marks->len + 7) >> 3);
  2549. }
  2550. int pdf_mark_bits_set(fz_context *ctx, pdf_mark_bits *marks, pdf_obj *obj)
  2551. {
  2552. int num = pdf_to_num(ctx, obj);
  2553. if (num > 0 && num < marks->len)
  2554. {
  2555. int x = num >> 3;
  2556. int m = 1 << (num & 7);
  2557. if (marks->bits[x] & m)
  2558. return 1;
  2559. marks->bits[x] |= m;
  2560. }
  2561. return 0;
  2562. }
  2563. void pdf_mark_bits_clear(fz_context *ctx, pdf_mark_bits *marks, pdf_obj *obj)
  2564. {
  2565. int num = pdf_to_num(ctx, obj);
  2566. if (num > 0 && num < marks->len)
  2567. {
  2568. int x = num >> 3;
  2569. int m = 0xff ^ (1 << (num & 7));
  2570. marks->bits[x] &= m;
  2571. }
  2572. }
  2573. int
  2574. pdf_mark_list_push(fz_context *ctx, pdf_mark_list *marks, pdf_obj *obj)
  2575. {
  2576. int num = pdf_to_num(ctx, obj);
  2577. int i;
  2578. /* If object is not an indirection, then no record to check.
  2579. * We must still push it to allow pops to stay in sync. */
  2580. if (num > 0)
  2581. {
  2582. /* Note: this is slow, if the mark list is expected to be big use pdf_mark_bits instead! */
  2583. for (i = 0; i < marks->len; ++i)
  2584. if (marks->list[i] == num)
  2585. return 1;
  2586. }
  2587. if (marks->len == marks->max)
  2588. {
  2589. int newsize = marks->max << 1;
  2590. if (marks->list == marks->local_list)
  2591. {
  2592. marks->list = fz_malloc_array(ctx, newsize, int);
  2593. memcpy(marks->list, marks->local_list, sizeof(marks->local_list));
  2594. }
  2595. else
  2596. marks->list = fz_realloc_array(ctx, marks->list, newsize, int);
  2597. marks->max = newsize;
  2598. }
  2599. marks->list[marks->len++] = num;
  2600. return 0;
  2601. }
  2602. void
  2603. pdf_mark_list_pop(fz_context *ctx, pdf_mark_list *marks)
  2604. {
  2605. --marks->len;
  2606. }
  2607. int
  2608. pdf_mark_list_check(fz_context *ctx, pdf_mark_list *marks, pdf_obj *obj)
  2609. {
  2610. if (pdf_mark_list_push(ctx, marks, obj))
  2611. return 1;
  2612. pdf_mark_list_pop(ctx, marks);
  2613. return 0;
  2614. }
  2615. void
  2616. pdf_mark_list_init(fz_context *ctx, pdf_mark_list *marks)
  2617. {
  2618. marks->len = 0;
  2619. marks->max = nelem(marks->local_list);
  2620. marks->list = marks->local_list;
  2621. }
  2622. void
  2623. pdf_mark_list_free(fz_context *ctx, pdf_mark_list *marks)
  2624. {
  2625. if (marks->list != marks->local_list)
  2626. fz_free(ctx, marks->list);
  2627. marks->len = 0;
  2628. marks->max = 0;
  2629. marks->list = NULL;
  2630. }
  2631. void
  2632. pdf_set_obj_memo(fz_context *ctx, pdf_obj *obj, int bit, int memo)
  2633. {
  2634. if (obj < PDF_LIMIT)
  2635. return;
  2636. bit <<= 1;
  2637. obj->flags |= PDF_FLAGS_MEMO_BASE << bit;
  2638. if (memo)
  2639. obj->flags |= PDF_FLAGS_MEMO_BASE_BOOL << bit;
  2640. else
  2641. obj->flags &= ~(PDF_FLAGS_MEMO_BASE_BOOL << bit);
  2642. }
  2643. int
  2644. pdf_obj_memo(fz_context *ctx, pdf_obj *obj, int bit, int *memo)
  2645. {
  2646. if (obj < PDF_LIMIT)
  2647. return 0;
  2648. bit <<= 1;
  2649. if (!(obj->flags & (PDF_FLAGS_MEMO_BASE<<bit)))
  2650. return 0;
  2651. *memo = !!(obj->flags & (PDF_FLAGS_MEMO_BASE_BOOL<<bit));
  2652. return 1;
  2653. }
  2654. /* obj dirty bit support. */
  2655. int pdf_obj_is_dirty(fz_context *ctx, pdf_obj *obj)
  2656. {
  2657. RESOLVE(obj);
  2658. if (obj < PDF_LIMIT)
  2659. return 0;
  2660. return !!(obj->flags & PDF_FLAGS_DIRTY);
  2661. }
  2662. void pdf_dirty_obj(fz_context *ctx, pdf_obj *obj)
  2663. {
  2664. RESOLVE(obj);
  2665. if (obj < PDF_LIMIT)
  2666. return;
  2667. obj->flags |= PDF_FLAGS_DIRTY;
  2668. }
  2669. void pdf_clean_obj(fz_context *ctx, pdf_obj *obj)
  2670. {
  2671. RESOLVE(obj);
  2672. if (obj < PDF_LIMIT)
  2673. return;
  2674. obj->flags &= ~PDF_FLAGS_DIRTY;
  2675. }
  2676. static void
  2677. pdf_drop_array(fz_context *ctx, pdf_obj *obj)
  2678. {
  2679. int i;
  2680. for (i = 0; i < DICT(obj)->len; i++)
  2681. pdf_drop_obj(ctx, ARRAY(obj)->items[i]);
  2682. fz_free(ctx, DICT(obj)->items);
  2683. fz_free(ctx, obj);
  2684. }
  2685. static void
  2686. pdf_drop_dict(fz_context *ctx, pdf_obj *obj)
  2687. {
  2688. int i;
  2689. for (i = 0; i < DICT(obj)->len; i++) {
  2690. pdf_drop_obj(ctx, DICT(obj)->items[i].k);
  2691. pdf_drop_obj(ctx, DICT(obj)->items[i].v);
  2692. }
  2693. fz_free(ctx, DICT(obj)->items);
  2694. fz_free(ctx, obj);
  2695. }
  2696. pdf_obj *
  2697. pdf_keep_obj(fz_context *ctx, pdf_obj *obj)
  2698. {
  2699. if (obj >= PDF_LIMIT)
  2700. return fz_keep_imp16(ctx, obj, &obj->refs);
  2701. return obj;
  2702. }
  2703. void
  2704. pdf_drop_obj(fz_context *ctx, pdf_obj *obj)
  2705. {
  2706. if (obj >= PDF_LIMIT)
  2707. {
  2708. if (fz_drop_imp16(ctx, obj, &obj->refs))
  2709. {
  2710. if (obj->kind == PDF_ARRAY)
  2711. pdf_drop_array(ctx, obj);
  2712. else if (obj->kind == PDF_DICT)
  2713. pdf_drop_dict(ctx, obj);
  2714. else if (obj->kind == PDF_STRING)
  2715. {
  2716. fz_free(ctx, STRING(obj)->text);
  2717. fz_free(ctx, obj);
  2718. }
  2719. else
  2720. fz_free(ctx, obj);
  2721. }
  2722. }
  2723. }
  2724. pdf_obj *
  2725. pdf_drop_singleton_obj(fz_context *ctx, pdf_obj *obj)
  2726. {
  2727. int drop;
  2728. /* If an object is < PDF_LIMIT, then it's a 'common' name or
  2729. * true or false. No point in dropping these as it
  2730. * won't save any memory. */
  2731. if (obj < PDF_LIMIT)
  2732. return obj;
  2733. /* See if it's a singleton object. We can only drop if
  2734. * it's a singleton object. If not, just exit leaving
  2735. * everything unchanged. */
  2736. fz_lock(ctx, FZ_LOCK_ALLOC);
  2737. drop = (obj->refs == 1);
  2738. fz_unlock(ctx, FZ_LOCK_ALLOC);
  2739. if (!drop)
  2740. return obj;
  2741. /* So drop the object! */
  2742. if (obj->kind == PDF_ARRAY)
  2743. pdf_drop_array(ctx, obj);
  2744. else if (obj->kind == PDF_DICT)
  2745. pdf_drop_dict(ctx, obj);
  2746. else if (obj->kind == PDF_STRING)
  2747. {
  2748. fz_free(ctx, STRING(obj)->text);
  2749. fz_free(ctx, obj);
  2750. }
  2751. else
  2752. fz_free(ctx, obj);
  2753. return NULL;
  2754. }
  2755. /*
  2756. Recurse through the object structure setting the node's parent_num to num.
  2757. parent_num is used when a subobject is to be changed during a document edit.
  2758. The whole containing hierarchy is moved to the incremental xref section, so
  2759. to be later written out as an incremental file update.
  2760. */
  2761. void
  2762. pdf_set_obj_parent(fz_context *ctx, pdf_obj *obj, int num)
  2763. {
  2764. int n, i;
  2765. if (obj < PDF_LIMIT)
  2766. return;
  2767. switch (obj->kind)
  2768. {
  2769. case PDF_ARRAY:
  2770. ARRAY(obj)->parent_num = num;
  2771. n = pdf_array_len(ctx, obj);
  2772. for (i = 0; i < n; i++)
  2773. pdf_set_obj_parent(ctx, pdf_array_get(ctx, obj, i), num);
  2774. break;
  2775. case PDF_DICT:
  2776. DICT(obj)->parent_num = num;
  2777. n = pdf_dict_len(ctx, obj);
  2778. for (i = 0; i < n; i++)
  2779. pdf_set_obj_parent(ctx, pdf_dict_get_val(ctx, obj, i), num);
  2780. break;
  2781. }
  2782. }
  2783. int pdf_obj_parent_num(fz_context *ctx, pdf_obj *obj)
  2784. {
  2785. if (obj < PDF_LIMIT)
  2786. return 0;
  2787. switch (obj->kind)
  2788. {
  2789. case PDF_INDIRECT:
  2790. return REF(obj)->num;
  2791. case PDF_ARRAY:
  2792. return ARRAY(obj)->parent_num;
  2793. case PDF_DICT:
  2794. return DICT(obj)->parent_num;
  2795. default:
  2796. return 0;
  2797. }
  2798. }
  2799. /* Pretty printing objects */
  2800. struct fmt
  2801. {
  2802. char *buf; /* original static buffer */
  2803. char *ptr; /* buffer we're writing to, maybe dynamically reallocated */
  2804. size_t cap;
  2805. size_t len;
  2806. int indent;
  2807. int tight;
  2808. int ascii;
  2809. int col;
  2810. int sep;
  2811. int last;
  2812. pdf_crypt *crypt;
  2813. int num;
  2814. int gen;
  2815. };
  2816. static void fmt_obj(fz_context *ctx, struct fmt *fmt, pdf_obj *obj);
  2817. static inline int iswhite(int ch)
  2818. {
  2819. return
  2820. ch == '\000' ||
  2821. ch == '\011' ||
  2822. ch == '\012' ||
  2823. ch == '\014' ||
  2824. ch == '\015' ||
  2825. ch == '\040';
  2826. }
  2827. static inline int isdelim(int ch)
  2828. {
  2829. return
  2830. ch == '(' || ch == ')' ||
  2831. ch == '<' || ch == '>' ||
  2832. ch == '[' || ch == ']' ||
  2833. ch == '{' || ch == '}' ||
  2834. ch == '/' ||
  2835. ch == '%';
  2836. }
  2837. static inline void fmt_putc(fz_context *ctx, struct fmt *fmt, int c)
  2838. {
  2839. if (fmt->sep && !isdelim(fmt->last) && !iswhite(fmt->last) && !isdelim(c) && !iswhite(c)) {
  2840. fmt->sep = 0;
  2841. fmt_putc(ctx, fmt, ' ');
  2842. }
  2843. fmt->sep = 0;
  2844. if (fmt->len >= fmt->cap)
  2845. {
  2846. fmt->cap *= 2;
  2847. if (fmt->buf == fmt->ptr)
  2848. {
  2849. fmt->ptr = Memento_label(fz_malloc(ctx, fmt->cap), "fmt_ptr");
  2850. memcpy(fmt->ptr, fmt->buf, fmt->len);
  2851. }
  2852. else
  2853. {
  2854. fmt->ptr = fz_realloc(ctx, fmt->ptr, fmt->cap);
  2855. }
  2856. }
  2857. fmt->ptr[fmt->len] = c;
  2858. if (c == '\n')
  2859. fmt->col = 0;
  2860. else
  2861. fmt->col ++;
  2862. fmt->len ++;
  2863. fmt->last = c;
  2864. }
  2865. static inline void fmt_indent(fz_context *ctx, struct fmt *fmt)
  2866. {
  2867. int i = fmt->indent;
  2868. while (i--) {
  2869. fmt_putc(ctx, fmt, ' ');
  2870. fmt_putc(ctx, fmt, ' ');
  2871. }
  2872. }
  2873. static inline void fmt_puts(fz_context *ctx, struct fmt *fmt, char *s)
  2874. {
  2875. while (*s)
  2876. fmt_putc(ctx, fmt, *s++);
  2877. }
  2878. static inline void fmt_sep(fz_context *ctx, struct fmt *fmt)
  2879. {
  2880. fmt->sep = 1;
  2881. }
  2882. static int is_binary_string(fz_context *ctx, pdf_obj *obj)
  2883. {
  2884. unsigned char *s = (unsigned char *)pdf_to_str_buf(ctx, obj);
  2885. size_t i, n = pdf_to_str_len(ctx, obj);
  2886. for (i = 0; i < n; ++i)
  2887. {
  2888. if (s[i] > 126) return 1;
  2889. if (s[i] < 32 && (s[i] != '\t' && s[i] != '\n' && s[i] != '\r')) return 1;
  2890. }
  2891. return 0;
  2892. }
  2893. static int is_longer_than_hex(fz_context *ctx, pdf_obj *obj)
  2894. {
  2895. unsigned char *s = (unsigned char *)pdf_to_str_buf(ctx, obj);
  2896. size_t i, n = pdf_to_str_len(ctx, obj);
  2897. size_t m = 0;
  2898. for (i = 0; i < n; ++i)
  2899. {
  2900. if (s[i] > 126)
  2901. m += 4;
  2902. else if (s[i] == 0)
  2903. m += 4;
  2904. else if (strchr("\n\r\t\b\f()\\", s[i]))
  2905. m += 2;
  2906. else if (s[i] < 32)
  2907. m += 4;
  2908. else
  2909. m += 1;
  2910. }
  2911. return m > (n * 2);
  2912. }
  2913. static void fmt_str_out(fz_context *ctx, void *fmt_, const unsigned char *s, size_t n)
  2914. {
  2915. struct fmt *fmt = (struct fmt *)fmt_;
  2916. int c;
  2917. size_t i;
  2918. for (i = 0; i < n; i++)
  2919. {
  2920. c = (unsigned char)s[i];
  2921. if (c == '\n')
  2922. fmt_puts(ctx, fmt, "\\n");
  2923. else if (c == '\r')
  2924. fmt_puts(ctx, fmt, "\\r");
  2925. else if (c == '\t')
  2926. fmt_puts(ctx, fmt, "\\t");
  2927. else if (c == '\b')
  2928. fmt_puts(ctx, fmt, "\\b");
  2929. else if (c == '\f')
  2930. fmt_puts(ctx, fmt, "\\f");
  2931. else if (c == '(')
  2932. fmt_puts(ctx, fmt, "\\(");
  2933. else if (c == ')')
  2934. fmt_puts(ctx, fmt, "\\)");
  2935. else if (c == '\\')
  2936. fmt_puts(ctx, fmt, "\\\\");
  2937. else if (c < 32 || c >= 127) {
  2938. fmt_putc(ctx, fmt, '\\');
  2939. fmt_putc(ctx, fmt, '0' + ((c / 64) & 7));
  2940. fmt_putc(ctx, fmt, '0' + ((c / 8) & 7));
  2941. fmt_putc(ctx, fmt, '0' + ((c) & 7));
  2942. }
  2943. else
  2944. fmt_putc(ctx, fmt, c);
  2945. }
  2946. }
  2947. static void fmt_str(fz_context *ctx, struct fmt *fmt, pdf_obj *obj)
  2948. {
  2949. unsigned char *s = (unsigned char *)pdf_to_str_buf(ctx, obj);
  2950. size_t n = pdf_to_str_len(ctx, obj);
  2951. fmt_putc(ctx, fmt, '(');
  2952. pdf_encrypt_data(ctx, fmt->crypt, fmt->num, fmt->gen, fmt_str_out, fmt, s, n);
  2953. fmt_putc(ctx, fmt, ')');
  2954. }
  2955. static void fmt_hex_out(fz_context *ctx, void *arg, const unsigned char *s, size_t n)
  2956. {
  2957. struct fmt *fmt = (struct fmt *)arg;
  2958. size_t i;
  2959. int b, c;
  2960. for (i = 0; i < n; i++) {
  2961. b = (unsigned char) s[i];
  2962. c = (b >> 4) & 0x0f;
  2963. fmt_putc(ctx, fmt, c < 0xA ? c + '0' : c + 'A' - 0xA);
  2964. c = (b) & 0x0f;
  2965. fmt_putc(ctx, fmt, c < 0xA ? c + '0' : c + 'A' - 0xA);
  2966. }
  2967. }
  2968. static void fmt_hex(fz_context *ctx, struct fmt *fmt, pdf_obj *obj)
  2969. {
  2970. unsigned char *s = (unsigned char *)pdf_to_str_buf(ctx, obj);
  2971. size_t n = pdf_to_str_len(ctx, obj);
  2972. fmt_putc(ctx, fmt, '<');
  2973. pdf_encrypt_data(ctx, fmt->crypt, fmt->num, fmt->gen, fmt_hex_out, fmt, s, n);
  2974. fmt_putc(ctx, fmt, '>');
  2975. }
  2976. static void fmt_name(fz_context *ctx, struct fmt *fmt, pdf_obj *obj)
  2977. {
  2978. unsigned char *s = (unsigned char *) pdf_to_name(ctx, obj);
  2979. int i, c;
  2980. fmt_putc(ctx, fmt, '/');
  2981. for (i = 0; s[i]; i++)
  2982. {
  2983. if (isdelim(s[i]) || iswhite(s[i]) ||
  2984. s[i] == '#' || s[i] < 32 || s[i] >= 127)
  2985. {
  2986. fmt_putc(ctx, fmt, '#');
  2987. c = (s[i] >> 4) & 0xf;
  2988. fmt_putc(ctx, fmt, c < 0xA ? c + '0' : c + 'A' - 0xA);
  2989. c = s[i] & 0xf;
  2990. fmt_putc(ctx, fmt, c < 0xA ? c + '0' : c + 'A' - 0xA);
  2991. }
  2992. else
  2993. {
  2994. fmt_putc(ctx, fmt, s[i]);
  2995. }
  2996. }
  2997. fmt->sep = 1;
  2998. }
  2999. static void fmt_array(fz_context *ctx, struct fmt *fmt, pdf_obj *obj)
  3000. {
  3001. int i, n;
  3002. n = pdf_array_len(ctx, obj);
  3003. if (fmt->tight) {
  3004. fmt_putc(ctx, fmt, '[');
  3005. for (i = 0; i < n; i++) {
  3006. fmt_obj(ctx, fmt, pdf_array_get(ctx, obj, i));
  3007. }
  3008. fmt_putc(ctx, fmt, ']');
  3009. }
  3010. else {
  3011. fmt_putc(ctx, fmt, '[');
  3012. fmt->indent ++;
  3013. for (i = 0; i < n; i++) {
  3014. if (fmt->col > 60) {
  3015. fmt_putc(ctx, fmt, '\n');
  3016. fmt_indent(ctx, fmt);
  3017. } else {
  3018. fmt_putc(ctx, fmt, ' ');
  3019. }
  3020. fmt_obj(ctx, fmt, pdf_array_get(ctx, obj, i));
  3021. }
  3022. fmt->indent --;
  3023. fmt_putc(ctx, fmt, ' ');
  3024. fmt_putc(ctx, fmt, ']');
  3025. fmt_sep(ctx, fmt);
  3026. }
  3027. }
  3028. static int is_signature(fz_context *ctx, pdf_obj *obj)
  3029. {
  3030. if (pdf_dict_get(ctx, obj, PDF_NAME(Type)) == PDF_NAME(Sig))
  3031. if (pdf_dict_get(ctx, obj, PDF_NAME(Contents)) && pdf_dict_get(ctx, obj, PDF_NAME(ByteRange)) && pdf_dict_get(ctx, obj, PDF_NAME(Filter)))
  3032. return 1;
  3033. return 0;
  3034. }
  3035. static void fmt_dict(fz_context *ctx, struct fmt *fmt, pdf_obj *obj)
  3036. {
  3037. int i, n;
  3038. pdf_obj *key, *val;
  3039. int skip = 0;
  3040. pdf_obj *type = pdf_dict_get(ctx, obj, PDF_NAME(Type));
  3041. n = pdf_dict_len(ctx, obj);
  3042. /* Open the dictionary.
  3043. * We spot /Type and /Subtype here so we can sent those first,
  3044. * in order. The hope is this will improve compression, because
  3045. * we'll be consistently sending those first. */
  3046. if (fmt->tight) {
  3047. fmt_puts(ctx, fmt, "<<");
  3048. if (type)
  3049. {
  3050. pdf_obj *subtype = pdf_dict_get(ctx, obj, PDF_NAME(Subtype));
  3051. fmt_obj(ctx, fmt, PDF_NAME(Type));
  3052. fmt_obj(ctx, fmt, type);
  3053. if (subtype)
  3054. {
  3055. fmt_obj(ctx, fmt, PDF_NAME(Subtype));
  3056. fmt_obj(ctx, fmt, subtype);
  3057. skip |= 2; /* Skip Subtype */
  3058. }
  3059. skip |= 1; /* Skip Type */
  3060. }
  3061. /* Now send all the key/value pairs except the ones we have decided to
  3062. * skip. */
  3063. for (i = 0; i < n; i++) {
  3064. key = pdf_dict_get_key(ctx, obj, i);
  3065. if (skip)
  3066. {
  3067. if ((skip & 1) != 0 && key == PDF_NAME(Type))
  3068. continue;
  3069. if ((skip & 2) != 0 && key == PDF_NAME(Subtype))
  3070. continue;
  3071. }
  3072. val = pdf_dict_get_val(ctx, obj, i);
  3073. fmt_obj(ctx, fmt, key);
  3074. if (key == PDF_NAME(Contents) && is_signature(ctx, obj))
  3075. {
  3076. pdf_crypt *crypt = fmt->crypt;
  3077. fz_try(ctx)
  3078. {
  3079. fmt->crypt = NULL;
  3080. fmt_obj(ctx, fmt, val);
  3081. }
  3082. fz_always(ctx)
  3083. fmt->crypt = crypt;
  3084. fz_catch(ctx)
  3085. fz_rethrow(ctx);
  3086. }
  3087. else
  3088. fmt_obj(ctx, fmt, val);
  3089. }
  3090. fmt_puts(ctx, fmt, ">>");
  3091. }
  3092. else /* Not tight, send it simply. */
  3093. {
  3094. fmt_puts(ctx, fmt, "<<\n");
  3095. fmt->indent ++;
  3096. for (i = 0; i < n; i++) {
  3097. key = pdf_dict_get_key(ctx, obj, i);
  3098. val = pdf_dict_get_val(ctx, obj, i);
  3099. fmt_indent(ctx, fmt);
  3100. fmt_obj(ctx, fmt, key);
  3101. fmt_putc(ctx, fmt, ' ');
  3102. if (!pdf_is_indirect(ctx, val) && pdf_is_array(ctx, val))
  3103. fmt->indent ++;
  3104. if (key == PDF_NAME(Contents) && is_signature(ctx, obj))
  3105. {
  3106. pdf_crypt *crypt = fmt->crypt;
  3107. fz_try(ctx)
  3108. {
  3109. fmt->crypt = NULL;
  3110. fmt_obj(ctx, fmt, val);
  3111. }
  3112. fz_always(ctx)
  3113. fmt->crypt = crypt;
  3114. fz_catch(ctx)
  3115. fz_rethrow(ctx);
  3116. }
  3117. else
  3118. fmt_obj(ctx, fmt, val);
  3119. fmt_putc(ctx, fmt, '\n');
  3120. if (!pdf_is_indirect(ctx, val) && pdf_is_array(ctx, val))
  3121. fmt->indent --;
  3122. }
  3123. fmt->indent --;
  3124. fmt_indent(ctx, fmt);
  3125. fmt_puts(ctx, fmt, ">>");
  3126. }
  3127. }
  3128. static void fmt_obj(fz_context *ctx, struct fmt *fmt, pdf_obj *obj)
  3129. {
  3130. char buf[256];
  3131. if (obj == PDF_NULL)
  3132. {
  3133. fmt_puts(ctx, fmt, "null");
  3134. fmt->sep = 1;
  3135. return;
  3136. }
  3137. else if (obj == PDF_TRUE)
  3138. {
  3139. fmt_puts(ctx, fmt, "true");
  3140. fmt->sep = 1;
  3141. return;
  3142. }
  3143. else if (obj == PDF_FALSE)
  3144. {
  3145. fmt_puts(ctx, fmt, "false");
  3146. fmt->sep = 1;
  3147. return;
  3148. }
  3149. else if (pdf_is_indirect(ctx, obj))
  3150. {
  3151. int n = pdf_to_num(ctx, obj);
  3152. int g = pdf_to_gen(ctx, obj);
  3153. fz_snprintf(buf, sizeof buf, "%d %d R", n, g);
  3154. fmt_puts(ctx, fmt, buf);
  3155. fmt->sep = 1;
  3156. return;
  3157. }
  3158. else if (pdf_is_int(ctx, obj))
  3159. {
  3160. fz_snprintf(buf, sizeof buf, "%ld", pdf_to_int64(ctx, obj));
  3161. fmt_puts(ctx, fmt, buf);
  3162. fmt->sep = 1;
  3163. return;
  3164. }
  3165. else if (pdf_is_real(ctx, obj))
  3166. {
  3167. float f = pdf_to_real(ctx, obj);
  3168. if (f == (int)f)
  3169. fz_snprintf(buf, sizeof buf, "%d", (int)f);
  3170. else
  3171. fz_snprintf(buf, sizeof buf, "%g", f);
  3172. fmt_puts(ctx, fmt, buf);
  3173. fmt->sep = 1;
  3174. return;
  3175. }
  3176. else if (pdf_is_string(ctx, obj))
  3177. {
  3178. unsigned char *str = (unsigned char *)pdf_to_str_buf(ctx, obj);
  3179. if (fmt->crypt
  3180. || (fmt->ascii && is_binary_string(ctx, obj))
  3181. || (str[0]==0xff && str[1]==0xfe)
  3182. || (str[0]==0xfe && str[1] == 0xff)
  3183. || is_longer_than_hex(ctx, obj)
  3184. )
  3185. fmt_hex(ctx, fmt, obj);
  3186. else
  3187. fmt_str(ctx, fmt, obj);
  3188. }
  3189. else if (pdf_is_name(ctx, obj))
  3190. fmt_name(ctx, fmt, obj);
  3191. else if (pdf_is_array(ctx, obj))
  3192. fmt_array(ctx, fmt, obj);
  3193. else if (pdf_is_dict(ctx, obj))
  3194. fmt_dict(ctx, fmt, obj);
  3195. else
  3196. fmt_puts(ctx, fmt, "<unknown object>");
  3197. }
  3198. static char *
  3199. pdf_sprint_encrypted_obj(fz_context *ctx, char *buf, size_t cap, size_t *len, pdf_obj *obj, int tight, int ascii, pdf_crypt *crypt, int num, int gen, int *sep)
  3200. {
  3201. struct fmt fmt;
  3202. fmt.indent = 0;
  3203. fmt.col = 0;
  3204. fmt.sep = sep ? *sep : 0;
  3205. fmt.last = 0;
  3206. if (!buf || cap == 0)
  3207. {
  3208. fmt.cap = 1024;
  3209. fmt.buf = NULL;
  3210. fmt.ptr = Memento_label(fz_malloc(ctx, fmt.cap), "fmt_buf");
  3211. }
  3212. else
  3213. {
  3214. fmt.cap = cap;
  3215. fmt.buf = buf;
  3216. fmt.ptr = buf;
  3217. }
  3218. fmt.tight = tight;
  3219. fmt.ascii = ascii;
  3220. fmt.len = 0;
  3221. fmt.crypt = crypt;
  3222. fmt.num = num;
  3223. fmt.gen = gen;
  3224. fz_try(ctx)
  3225. {
  3226. fmt_obj(ctx, &fmt, obj);
  3227. if (sep)
  3228. *sep = fmt.sep;
  3229. fmt.sep = 0;
  3230. fmt_putc(ctx, &fmt, 0);
  3231. }
  3232. fz_catch(ctx)
  3233. {
  3234. if (!buf || cap == 0)
  3235. fz_free(ctx, fmt.ptr);
  3236. fz_rethrow(ctx);
  3237. }
  3238. return *len = fmt.len-1, fmt.ptr;
  3239. }
  3240. char *
  3241. pdf_sprint_obj(fz_context *ctx, char *buf, size_t cap, size_t *len, pdf_obj *obj, int tight, int ascii)
  3242. {
  3243. return pdf_sprint_encrypted_obj(ctx, buf, cap, len, obj, tight, ascii, NULL, 0, 0, NULL);
  3244. }
  3245. void pdf_print_encrypted_obj(fz_context *ctx, fz_output *out, pdf_obj *obj, int tight, int ascii, pdf_crypt *crypt, int num, int gen, int *sep)
  3246. {
  3247. char buf[1024];
  3248. char *ptr;
  3249. size_t n;
  3250. ptr = pdf_sprint_encrypted_obj(ctx, buf, sizeof buf, &n, obj, tight, ascii, crypt, num, gen, sep);
  3251. fz_try(ctx)
  3252. fz_write_data(ctx, out, ptr, n);
  3253. fz_always(ctx)
  3254. if (ptr != buf)
  3255. fz_free(ctx, ptr);
  3256. fz_catch(ctx)
  3257. fz_rethrow(ctx);
  3258. }
  3259. void pdf_print_obj(fz_context *ctx, fz_output *out, pdf_obj *obj, int tight, int ascii)
  3260. {
  3261. pdf_print_encrypted_obj(ctx, out, obj, tight, ascii, NULL, 0, 0, NULL);
  3262. }
  3263. void pdf_debug_obj(fz_context *ctx, pdf_obj *obj)
  3264. {
  3265. pdf_print_obj(ctx, fz_stddbg(ctx), pdf_resolve_indirect(ctx, obj), 0, 0);
  3266. }
  3267. void pdf_debug_ref(fz_context *ctx, pdf_obj *obj)
  3268. {
  3269. fz_output *out = fz_stddbg(ctx);
  3270. pdf_print_obj(ctx, out, obj, 0, 0);
  3271. fz_write_byte(ctx, out, '\n');
  3272. }
  3273. int pdf_obj_refs(fz_context *ctx, pdf_obj *obj)
  3274. {
  3275. if (obj < PDF_LIMIT)
  3276. return 0;
  3277. return obj->refs;
  3278. }
  3279. /* Convenience functions */
  3280. /*
  3281. Uses Floyd's cycle finding algorithm, modified to avoid starting
  3282. the 'slow' pointer for a while.
  3283. https://www.geeksforgeeks.org/floyds-cycle-finding-algorithm/
  3284. */
  3285. pdf_obj *
  3286. pdf_dict_get_inheritable(fz_context *ctx, pdf_obj *node, pdf_obj *key)
  3287. {
  3288. pdf_obj *slow = node;
  3289. int halfbeat = 11; /* Don't start moving slow pointer for a while. */
  3290. while (node)
  3291. {
  3292. pdf_obj *val = pdf_dict_get(ctx, node, key);
  3293. if (val)
  3294. return val;
  3295. node = pdf_dict_get(ctx, node, PDF_NAME(Parent));
  3296. if (node == slow)
  3297. fz_throw(ctx, FZ_ERROR_FORMAT, "cycle in resources");
  3298. if (--halfbeat == 0)
  3299. {
  3300. slow = pdf_dict_get(ctx, slow, PDF_NAME(Parent));
  3301. halfbeat = 2;
  3302. }
  3303. }
  3304. return NULL;
  3305. }
  3306. pdf_obj *
  3307. pdf_dict_getp_inheritable(fz_context *ctx, pdf_obj *node, const char *path)
  3308. {
  3309. pdf_obj *slow = node;
  3310. int halfbeat = 11; /* Don't start moving slow pointer for a while. */
  3311. while (node)
  3312. {
  3313. pdf_obj *val = pdf_dict_getp(ctx, node, path);
  3314. if (val)
  3315. return val;
  3316. node = pdf_dict_get(ctx, node, PDF_NAME(Parent));
  3317. if (node == slow)
  3318. fz_throw(ctx, FZ_ERROR_FORMAT, "cycle in resources");
  3319. if (--halfbeat == 0)
  3320. {
  3321. slow = pdf_dict_get(ctx, slow, PDF_NAME(Parent));
  3322. halfbeat = 2;
  3323. }
  3324. }
  3325. return NULL;
  3326. }
  3327. pdf_obj *
  3328. pdf_dict_gets_inheritable(fz_context *ctx, pdf_obj *node, const char *key)
  3329. {
  3330. pdf_obj *slow = node;
  3331. int halfbeat = 11; /* Don't start moving slow pointer for a while. */
  3332. while (node)
  3333. {
  3334. pdf_obj *val = pdf_dict_gets(ctx, node, key);
  3335. if (val)
  3336. return val;
  3337. node = pdf_dict_get(ctx, node, PDF_NAME(Parent));
  3338. if (node == slow)
  3339. fz_throw(ctx, FZ_ERROR_FORMAT, "cycle in resources");
  3340. if (--halfbeat == 0)
  3341. {
  3342. slow = pdf_dict_get(ctx, slow, PDF_NAME(Parent));
  3343. halfbeat = 2;
  3344. }
  3345. }
  3346. return NULL;
  3347. }
  3348. void pdf_dict_put_bool(fz_context *ctx, pdf_obj *dict, pdf_obj *key, int x)
  3349. {
  3350. pdf_dict_put(ctx, dict, key, x ? PDF_TRUE : PDF_FALSE);
  3351. }
  3352. void pdf_dict_put_int(fz_context *ctx, pdf_obj *dict, pdf_obj *key, int64_t x)
  3353. {
  3354. pdf_dict_put_drop(ctx, dict, key, pdf_new_int(ctx, x));
  3355. }
  3356. void pdf_dict_put_real(fz_context *ctx, pdf_obj *dict, pdf_obj *key, double x)
  3357. {
  3358. pdf_dict_put_drop(ctx, dict, key, pdf_new_real(ctx, x));
  3359. }
  3360. void pdf_dict_put_name(fz_context *ctx, pdf_obj *dict, pdf_obj *key, const char *x)
  3361. {
  3362. pdf_dict_put_drop(ctx, dict, key, pdf_new_name(ctx, x));
  3363. }
  3364. void pdf_dict_put_string(fz_context *ctx, pdf_obj *dict, pdf_obj *key, const char *x, size_t n)
  3365. {
  3366. pdf_dict_put_drop(ctx, dict, key, pdf_new_string(ctx, x, n));
  3367. }
  3368. void pdf_dict_put_text_string(fz_context *ctx, pdf_obj *dict, pdf_obj *key, const char *x)
  3369. {
  3370. pdf_dict_put_drop(ctx, dict, key, pdf_new_text_string(ctx, x));
  3371. }
  3372. void pdf_dict_put_indirect(fz_context *ctx, pdf_obj *dict, pdf_obj *key, int num)
  3373. {
  3374. pdf_dict_put_drop(ctx, dict, key, pdf_new_indirect(ctx, pdf_get_bound_document(ctx, dict), num, 0));
  3375. }
  3376. void pdf_dict_put_point(fz_context *ctx, pdf_obj *dict, pdf_obj *key, fz_point x)
  3377. {
  3378. pdf_dict_put_drop(ctx, dict, key, pdf_new_point(ctx, pdf_get_bound_document(ctx, dict), x));
  3379. }
  3380. void pdf_dict_put_rect(fz_context *ctx, pdf_obj *dict, pdf_obj *key, fz_rect x)
  3381. {
  3382. pdf_dict_put_drop(ctx, dict, key, pdf_new_rect(ctx, pdf_get_bound_document(ctx, dict), x));
  3383. }
  3384. void pdf_dict_put_matrix(fz_context *ctx, pdf_obj *dict, pdf_obj *key, fz_matrix x)
  3385. {
  3386. pdf_dict_put_drop(ctx, dict, key, pdf_new_matrix(ctx, pdf_get_bound_document(ctx, dict), x));
  3387. }
  3388. void pdf_dict_put_date(fz_context *ctx, pdf_obj *dict, pdf_obj *key, int64_t time)
  3389. {
  3390. pdf_dict_put_drop(ctx, dict, key, pdf_new_date(ctx, pdf_get_bound_document(ctx, dict), time));
  3391. }
  3392. pdf_obj *pdf_dict_put_array(fz_context *ctx, pdf_obj *dict, pdf_obj *key, int initial)
  3393. {
  3394. pdf_obj *obj = pdf_new_array(ctx, pdf_get_bound_document(ctx, dict), initial);
  3395. pdf_dict_put_drop(ctx, dict, key, obj);
  3396. return obj;
  3397. }
  3398. pdf_obj *pdf_dict_put_dict(fz_context *ctx, pdf_obj *dict, pdf_obj *key, int initial)
  3399. {
  3400. pdf_obj *obj = pdf_new_dict(ctx, pdf_get_bound_document(ctx, dict), initial);
  3401. pdf_dict_put_drop(ctx, dict, key, obj);
  3402. return obj;
  3403. }
  3404. pdf_obj *pdf_dict_puts_dict(fz_context *ctx, pdf_obj *dict, const char *key, int initial)
  3405. {
  3406. pdf_obj *obj = pdf_new_dict(ctx, pdf_get_bound_document(ctx, dict), initial);
  3407. pdf_dict_puts_drop(ctx, dict, key, obj);
  3408. return obj;
  3409. }
  3410. void pdf_array_push_bool(fz_context *ctx, pdf_obj *array, int x)
  3411. {
  3412. pdf_array_push(ctx, array, x ? PDF_TRUE : PDF_FALSE);
  3413. }
  3414. void pdf_array_push_int(fz_context *ctx, pdf_obj *array, int64_t x)
  3415. {
  3416. pdf_array_push_drop(ctx, array, pdf_new_int(ctx, x));
  3417. }
  3418. void pdf_array_push_real(fz_context *ctx, pdf_obj *array, double x)
  3419. {
  3420. pdf_array_push_drop(ctx, array, pdf_new_real(ctx, x));
  3421. }
  3422. void pdf_array_push_name(fz_context *ctx, pdf_obj *array, const char *x)
  3423. {
  3424. pdf_array_push_drop(ctx, array, pdf_new_name(ctx, x));
  3425. }
  3426. void pdf_array_push_string(fz_context *ctx, pdf_obj *array, const char *x, size_t n)
  3427. {
  3428. pdf_array_push_drop(ctx, array, pdf_new_string(ctx, x, n));
  3429. }
  3430. void pdf_array_push_text_string(fz_context *ctx, pdf_obj *array, const char *x)
  3431. {
  3432. pdf_array_push_drop(ctx, array, pdf_new_text_string(ctx, x));
  3433. }
  3434. pdf_obj *pdf_array_push_array(fz_context *ctx, pdf_obj *array, int initial)
  3435. {
  3436. pdf_obj *obj = pdf_new_array(ctx, pdf_get_bound_document(ctx, array), initial);
  3437. pdf_array_push_drop(ctx, array, obj);
  3438. return obj;
  3439. }
  3440. pdf_obj *pdf_array_push_dict(fz_context *ctx, pdf_obj *array, int initial)
  3441. {
  3442. pdf_obj *obj = pdf_new_dict(ctx, pdf_get_bound_document(ctx, array), initial);
  3443. pdf_array_push_drop(ctx, array, obj);
  3444. return obj;
  3445. }
  3446. void pdf_array_put_bool(fz_context *ctx, pdf_obj *array, int i, int x)
  3447. {
  3448. pdf_array_put(ctx, array, i, x ? PDF_TRUE : PDF_FALSE);
  3449. }
  3450. void pdf_array_put_int(fz_context *ctx, pdf_obj *array, int i, int64_t x)
  3451. {
  3452. pdf_array_put_drop(ctx, array, i, pdf_new_int(ctx, x));
  3453. }
  3454. void pdf_array_put_real(fz_context *ctx, pdf_obj *array, int i, double x)
  3455. {
  3456. pdf_array_put_drop(ctx, array, i, pdf_new_real(ctx, x));
  3457. }
  3458. void pdf_array_put_name(fz_context *ctx, pdf_obj *array, int i, const char *x)
  3459. {
  3460. pdf_array_put_drop(ctx, array, i, pdf_new_name(ctx, x));
  3461. }
  3462. void pdf_array_put_string(fz_context *ctx, pdf_obj *array, int i, const char *x, size_t n)
  3463. {
  3464. pdf_array_put_drop(ctx, array, i, pdf_new_string(ctx, x, n));
  3465. }
  3466. void pdf_array_put_text_string(fz_context *ctx, pdf_obj *array, int i, const char *x)
  3467. {
  3468. pdf_array_put_drop(ctx, array, i, pdf_new_text_string(ctx, x));
  3469. }
  3470. pdf_obj *pdf_array_put_array(fz_context *ctx, pdf_obj *array, int i, int initial)
  3471. {
  3472. pdf_obj *obj = pdf_new_array(ctx, pdf_get_bound_document(ctx, array), initial);
  3473. pdf_array_put_drop(ctx, array, i, obj);
  3474. return obj;
  3475. }
  3476. pdf_obj *pdf_array_put_dict(fz_context *ctx, pdf_obj *array, int i, int initial)
  3477. {
  3478. pdf_obj *obj = pdf_new_dict(ctx, pdf_get_bound_document(ctx, array), initial);
  3479. pdf_array_put_drop(ctx, array, i, obj);
  3480. return obj;
  3481. }
  3482. int pdf_dict_get_bool(fz_context *ctx, pdf_obj *dict, pdf_obj *key)
  3483. {
  3484. return pdf_to_bool(ctx, pdf_dict_get(ctx, dict, key));
  3485. }
  3486. int pdf_dict_get_bool_default(fz_context *ctx, pdf_obj *dict, pdf_obj *key, int def)
  3487. {
  3488. return pdf_to_bool_default(ctx, pdf_dict_get(ctx, dict, key), def);
  3489. }
  3490. int pdf_dict_get_int(fz_context *ctx, pdf_obj *dict, pdf_obj *key)
  3491. {
  3492. return pdf_to_int(ctx, pdf_dict_get(ctx, dict, key));
  3493. }
  3494. int pdf_dict_get_int_default(fz_context *ctx, pdf_obj *dict, pdf_obj *key, int def)
  3495. {
  3496. return pdf_to_int_default(ctx, pdf_dict_get(ctx, dict, key), def);
  3497. }
  3498. int64_t pdf_dict_get_int64(fz_context *ctx, pdf_obj *dict, pdf_obj *key)
  3499. {
  3500. return pdf_to_int64(ctx, pdf_dict_get(ctx, dict, key));
  3501. }
  3502. float pdf_dict_get_real(fz_context *ctx, pdf_obj *dict, pdf_obj *key)
  3503. {
  3504. return pdf_to_real(ctx, pdf_dict_get(ctx, dict, key));
  3505. }
  3506. float pdf_dict_get_real_default(fz_context *ctx, pdf_obj *dict, pdf_obj *key, float def)
  3507. {
  3508. return pdf_to_real_default(ctx, pdf_dict_get(ctx, dict, key), def);
  3509. }
  3510. const char *pdf_dict_get_name(fz_context *ctx, pdf_obj *dict, pdf_obj *key)
  3511. {
  3512. return pdf_to_name(ctx, pdf_dict_get(ctx, dict, key));
  3513. }
  3514. const char *pdf_dict_get_string(fz_context *ctx, pdf_obj *dict, pdf_obj *key, size_t *sizep)
  3515. {
  3516. return pdf_to_string(ctx, pdf_dict_get(ctx, dict, key), sizep);
  3517. }
  3518. const char *pdf_dict_get_text_string(fz_context *ctx, pdf_obj *dict, pdf_obj *key)
  3519. {
  3520. return pdf_to_text_string(ctx, pdf_dict_get(ctx, dict, key));
  3521. }
  3522. const char *pdf_dict_get_text_string_opt(fz_context *ctx, pdf_obj *dict, pdf_obj *key)
  3523. {
  3524. pdf_obj *obj = pdf_dict_get(ctx, dict, key);
  3525. if (!pdf_is_string(ctx, obj))
  3526. return NULL;
  3527. return pdf_to_text_string(ctx, obj);
  3528. }
  3529. fz_point pdf_dict_get_point(fz_context *ctx, pdf_obj *dict, pdf_obj *key)
  3530. {
  3531. return pdf_to_point(ctx, pdf_dict_get(ctx, dict, key), 0);
  3532. }
  3533. fz_rect pdf_dict_get_rect(fz_context *ctx, pdf_obj *dict, pdf_obj *key)
  3534. {
  3535. return pdf_to_rect(ctx, pdf_dict_get(ctx, dict, key));
  3536. }
  3537. fz_matrix pdf_dict_get_matrix(fz_context *ctx, pdf_obj *dict, pdf_obj *key)
  3538. {
  3539. return pdf_to_matrix(ctx, pdf_dict_get(ctx, dict, key));
  3540. }
  3541. int pdf_dict_get_inheritable_bool(fz_context *ctx, pdf_obj *dict, pdf_obj *key)
  3542. {
  3543. return pdf_to_bool(ctx, pdf_dict_get_inheritable(ctx, dict, key));
  3544. }
  3545. int pdf_dict_get_inheritable_int(fz_context *ctx, pdf_obj *dict, pdf_obj *key)
  3546. {
  3547. return pdf_to_int(ctx, pdf_dict_get_inheritable(ctx, dict, key));
  3548. }
  3549. int64_t pdf_dict_get_inheritable_int64(fz_context *ctx, pdf_obj *dict, pdf_obj *key)
  3550. {
  3551. return pdf_to_int64(ctx, pdf_dict_get_inheritable(ctx, dict, key));
  3552. }
  3553. float pdf_dict_get_inheritable_real(fz_context *ctx, pdf_obj *dict, pdf_obj *key)
  3554. {
  3555. return pdf_to_real(ctx, pdf_dict_get_inheritable(ctx, dict, key));
  3556. }
  3557. const char *pdf_dict_get_inheritable_name(fz_context *ctx, pdf_obj *dict, pdf_obj *key)
  3558. {
  3559. return pdf_to_name(ctx, pdf_dict_get_inheritable(ctx, dict, key));
  3560. }
  3561. const char *pdf_dict_get_inheritable_string(fz_context *ctx, pdf_obj *dict, pdf_obj *key, size_t *sizep)
  3562. {
  3563. return pdf_to_string(ctx, pdf_dict_get_inheritable(ctx, dict, key), sizep);
  3564. }
  3565. const char *pdf_dict_get_inheritable_text_string(fz_context *ctx, pdf_obj *dict, pdf_obj *key)
  3566. {
  3567. return pdf_to_text_string(ctx, pdf_dict_get_inheritable(ctx, dict, key));
  3568. }
  3569. fz_rect pdf_dict_get_inheritable_rect(fz_context *ctx, pdf_obj *dict, pdf_obj *key)
  3570. {
  3571. return pdf_to_rect(ctx, pdf_dict_get_inheritable(ctx, dict, key));
  3572. }
  3573. fz_matrix pdf_dict_get_inheritable_matrix(fz_context *ctx, pdf_obj *dict, pdf_obj *key)
  3574. {
  3575. return pdf_to_matrix(ctx, pdf_dict_get_inheritable(ctx, dict, key));
  3576. }
  3577. int64_t pdf_dict_get_inheritable_date(fz_context *ctx, pdf_obj *dict, pdf_obj *key)
  3578. {
  3579. return pdf_to_date(ctx, pdf_dict_get_inheritable(ctx, dict, key));
  3580. }
  3581. int64_t pdf_dict_get_date(fz_context *ctx, pdf_obj *dict, pdf_obj *key)
  3582. {
  3583. return pdf_to_date(ctx, pdf_dict_get(ctx, dict, key));
  3584. }
  3585. int pdf_array_get_bool(fz_context *ctx, pdf_obj *array, int index)
  3586. {
  3587. return pdf_to_bool(ctx, pdf_array_get(ctx, array, index));
  3588. }
  3589. int pdf_array_get_int(fz_context *ctx, pdf_obj *array, int index)
  3590. {
  3591. return pdf_to_int(ctx, pdf_array_get(ctx, array, index));
  3592. }
  3593. float pdf_array_get_real(fz_context *ctx, pdf_obj *array, int index)
  3594. {
  3595. return pdf_to_real(ctx, pdf_array_get(ctx, array, index));
  3596. }
  3597. const char *pdf_array_get_name(fz_context *ctx, pdf_obj *array, int index)
  3598. {
  3599. return pdf_to_name(ctx, pdf_array_get(ctx, array, index));
  3600. }
  3601. const char *pdf_array_get_string(fz_context *ctx, pdf_obj *array, int index, size_t *sizep)
  3602. {
  3603. return pdf_to_string(ctx, pdf_array_get(ctx, array, index), sizep);
  3604. }
  3605. const char *pdf_array_get_text_string(fz_context *ctx, pdf_obj *array, int index)
  3606. {
  3607. return pdf_to_text_string(ctx, pdf_array_get(ctx, array, index));
  3608. }
  3609. fz_rect pdf_array_get_rect(fz_context *ctx, pdf_obj *array, int index)
  3610. {
  3611. return pdf_to_rect(ctx, pdf_array_get(ctx, array, index));
  3612. }
  3613. fz_matrix pdf_array_get_matrix(fz_context *ctx, pdf_obj *array, int index)
  3614. {
  3615. return pdf_to_matrix(ctx, pdf_array_get(ctx, array, index));
  3616. }
  3617. #ifndef NDEBUG
  3618. void pdf_verify_name_table_sanity(void)
  3619. {
  3620. int i;
  3621. for (i = PDF_ENUM_FALSE+1; i < PDF_ENUM_LIMIT-1; i++)
  3622. {
  3623. assert(strcmp(PDF_NAME_LIST[i], PDF_NAME_LIST[i+1]) < 0);
  3624. }
  3625. }
  3626. #endif