├── .gitignore ├── COPYING ├── arid.cpp ├── control ├── control.sh ├── ios.sh ├── ldid.cpp ├── lookup2.c ├── make.sh ├── sha1.c ├── sha1.h └── version.sh /.gitignore: -------------------------------------------------------------------------------- 1 | ldid 2 | ldid.arm 3 | ldid.deb 4 | debs 5 | _ 6 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /arid.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "minimal/mapping.h" 4 | 5 | struct ar_hdr { 6 | char ar_name[16]; 7 | char ar_date[12]; 8 | char ar_uid[6]; 9 | char ar_gid[6]; 10 | char ar_mode[8]; 11 | char ar_size[10]; 12 | #define ARFMAG "`\n" 13 | char ar_fmag[2]; 14 | }; 15 | 16 | int main(int argc, char *argv[]) { 17 | size_t size; 18 | _assert(argc == 2); 19 | uint8_t *data = reinterpret_cast(map(argv[1], 0, _not(size_t), &size, false)); 20 | data += 8; 21 | uint8_t *end = data + size; 22 | while (end - data >= sizeof(struct ar_hdr)) { 23 | struct ar_hdr *head = reinterpret_cast(data); 24 | memset(head->ar_date + 1, ' ', sizeof(head->ar_date) - 1); 25 | head->ar_date[0] = '0'; 26 | size_t length = strtoul(head->ar_size, NULL, 10); 27 | data += length + sizeof(struct ar_hdr); 28 | } 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /control: -------------------------------------------------------------------------------- 1 | Package: ldid 2 | Priority: optional 3 | Section: Development 4 | Maintainer: Jay Freeman (saurik) 5 | Architecture: iphoneos-arm 6 | Version: 7 | Description: psuedo-codesign Mach-O files 8 | Name: Link Identity Editor 9 | Author: Jay Freeman (saurik) 10 | Depiction: http://cydia.saurik.com/info/ldid/ 11 | Depends: odcctools 12 | Tag: purpose::console, role::developer 13 | -------------------------------------------------------------------------------- /control.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | dir=$1 3 | dir=${dir:=_} 4 | sed -e "s@^\(Version:.*\)@\1$(./version.sh)@" control 5 | echo "Installed-Size: $(du -s "${dir}" | cut -f 1)" 6 | -------------------------------------------------------------------------------- /ios.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e -x 4 | 5 | cycc -i2.0 -m10.5 -oldid.arm -- ldid.cpp sha1.c -x c lookup2.c -I . 6 | 7 | rm -rf _ 8 | mkdir -p _/usr/bin 9 | cp -a ldid.arm _/usr/bin 10 | mkdir -p _/DEBIAN 11 | ./control.sh _ >_/DEBIAN/control 12 | mkdir -p debs 13 | ln -sf debs/ldid_$(./version.sh)_iphoneos-arm.deb ldid.deb 14 | dpkg-deb -b _ ldid.deb 15 | readlink ldid.deb 16 | -------------------------------------------------------------------------------- /ldid.cpp: -------------------------------------------------------------------------------- 1 | /* ldid - (Mach-O) Link-Loader Identity Editor 2 | * Copyright (C) 2007-2012 Jay Freeman (saurik) 3 | */ 4 | 5 | /* GNU Lesser General Public License, Version 3 {{{ */ 6 | /* This program is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program. If not, see . 18 | **/ 19 | /* }}} */ 20 | 21 | #include "minimal/stdlib.h" 22 | #include "minimal/string.h" 23 | #include "minimal/mapping.h" 24 | 25 | #include "sha1.h" 26 | 27 | #include 28 | #include 29 | #include 30 | 31 | #include 32 | #include 33 | #include 34 | 35 | struct fat_header { 36 | uint32_t magic; 37 | uint32_t nfat_arch; 38 | } _packed; 39 | 40 | #define FAT_MAGIC 0xcafebabe 41 | #define FAT_CIGAM 0xbebafeca 42 | 43 | struct fat_arch { 44 | uint32_t cputype; 45 | uint32_t cpusubtype; 46 | uint32_t offset; 47 | uint32_t size; 48 | uint32_t align; 49 | } _packed; 50 | 51 | struct mach_header { 52 | uint32_t magic; 53 | uint32_t cputype; 54 | uint32_t cpusubtype; 55 | uint32_t filetype; 56 | uint32_t ncmds; 57 | uint32_t sizeofcmds; 58 | uint32_t flags; 59 | } _packed; 60 | 61 | #define MH_MAGIC 0xfeedface 62 | #define MH_CIGAM 0xcefaedfe 63 | 64 | #define MH_MAGIC_64 0xfeedfacf 65 | #define MH_CIGAM_64 0xcffaedfe 66 | 67 | #define MH_DYLDLINK 0x4 68 | 69 | #define MH_EXECUTE 0x2 70 | #define MH_DYLIB 0x6 71 | #define MH_BUNDLE 0x8 72 | #define MH_DYLIB_STUB 0x9 73 | 74 | struct load_command { 75 | uint32_t cmd; 76 | uint32_t cmdsize; 77 | } _packed; 78 | 79 | #define LC_REQ_DYLD uint32_t(0x80000000) 80 | 81 | #define LC_SEGMENT uint32_t(0x01) 82 | #define LC_SYMTAB uint32_t(0x02) 83 | #define LC_DYSYMTAB uint32_t(0x0b) 84 | #define LC_LOAD_DYLIB uint32_t(0x0c) 85 | #define LC_ID_DYLIB uint32_t(0x0d) 86 | #define LC_SEGMENT_64 uint32_t(0x19) 87 | #define LC_UUID uint32_t(0x1b) 88 | #define LC_CODE_SIGNATURE uint32_t(0x1d) 89 | #define LC_SEGMENT_SPLIT_INFO uint32_t(0x1e) 90 | #define LC_REEXPORT_DYLIB uint32_t(0x1f | LC_REQ_DYLD) 91 | #define LC_ENCRYPTION_INFO uint32_t(0x21) 92 | #define LC_DYLD_INFO uint32_t(0x22) 93 | #define LC_DYLD_INFO_ONLY uint32_t(0x22 | LC_REQ_DYLD) 94 | 95 | struct dylib { 96 | uint32_t name; 97 | uint32_t timestamp; 98 | uint32_t current_version; 99 | uint32_t compatibility_version; 100 | } _packed; 101 | 102 | struct dylib_command { 103 | uint32_t cmd; 104 | uint32_t cmdsize; 105 | struct dylib dylib; 106 | } _packed; 107 | 108 | struct uuid_command { 109 | uint32_t cmd; 110 | uint32_t cmdsize; 111 | uint8_t uuid[16]; 112 | } _packed; 113 | 114 | struct symtab_command { 115 | uint32_t cmd; 116 | uint32_t cmdsize; 117 | uint32_t symoff; 118 | uint32_t nsyms; 119 | uint32_t stroff; 120 | uint32_t strsize; 121 | } _packed; 122 | 123 | struct dyld_info_command { 124 | uint32_t cmd; 125 | uint32_t cmdsize; 126 | uint32_t rebase_off; 127 | uint32_t rebase_size; 128 | uint32_t bind_off; 129 | uint32_t bind_size; 130 | uint32_t weak_bind_off; 131 | uint32_t weak_bind_size; 132 | uint32_t lazy_bind_off; 133 | uint32_t lazy_bind_size; 134 | uint32_t export_off; 135 | uint32_t export_size; 136 | } _packed; 137 | 138 | struct dysymtab_command { 139 | uint32_t cmd; 140 | uint32_t cmdsize; 141 | uint32_t ilocalsym; 142 | uint32_t nlocalsym; 143 | uint32_t iextdefsym; 144 | uint32_t nextdefsym; 145 | uint32_t iundefsym; 146 | uint32_t nundefsym; 147 | uint32_t tocoff; 148 | uint32_t ntoc; 149 | uint32_t modtaboff; 150 | uint32_t nmodtab; 151 | uint32_t extrefsymoff; 152 | uint32_t nextrefsyms; 153 | uint32_t indirectsymoff; 154 | uint32_t nindirectsyms; 155 | uint32_t extreloff; 156 | uint32_t nextrel; 157 | uint32_t locreloff; 158 | uint32_t nlocrel; 159 | } _packed; 160 | 161 | struct dylib_table_of_contents { 162 | uint32_t symbol_index; 163 | uint32_t module_index; 164 | } _packed; 165 | 166 | struct dylib_module { 167 | uint32_t module_name; 168 | uint32_t iextdefsym; 169 | uint32_t nextdefsym; 170 | uint32_t irefsym; 171 | uint32_t nrefsym; 172 | uint32_t ilocalsym; 173 | uint32_t nlocalsym; 174 | uint32_t iextrel; 175 | uint32_t nextrel; 176 | uint32_t iinit_iterm; 177 | uint32_t ninit_nterm; 178 | uint32_t objc_module_info_addr; 179 | uint32_t objc_module_info_size; 180 | } _packed; 181 | 182 | struct dylib_reference { 183 | uint32_t isym:24; 184 | uint32_t flags:8; 185 | } _packed; 186 | 187 | struct relocation_info { 188 | int32_t r_address; 189 | uint32_t r_symbolnum:24; 190 | uint32_t r_pcrel:1; 191 | uint32_t r_length:2; 192 | uint32_t r_extern:1; 193 | uint32_t r_type:4; 194 | } _packed; 195 | 196 | struct nlist { 197 | union { 198 | char *n_name; 199 | int32_t n_strx; 200 | } n_un; 201 | 202 | uint8_t n_type; 203 | uint8_t n_sect; 204 | uint8_t n_desc; 205 | uint32_t n_value; 206 | } _packed; 207 | 208 | struct segment_command { 209 | uint32_t cmd; 210 | uint32_t cmdsize; 211 | char segname[16]; 212 | uint32_t vmaddr; 213 | uint32_t vmsize; 214 | uint32_t fileoff; 215 | uint32_t filesize; 216 | uint32_t maxprot; 217 | uint32_t initprot; 218 | uint32_t nsects; 219 | uint32_t flags; 220 | } _packed; 221 | 222 | struct segment_command_64 { 223 | uint32_t cmd; 224 | uint32_t cmdsize; 225 | char segname[16]; 226 | uint64_t vmaddr; 227 | uint64_t vmsize; 228 | uint64_t fileoff; 229 | uint64_t filesize; 230 | uint32_t maxprot; 231 | uint32_t initprot; 232 | uint32_t nsects; 233 | uint32_t flags; 234 | } _packed; 235 | 236 | struct section { 237 | char sectname[16]; 238 | char segname[16]; 239 | uint32_t addr; 240 | uint32_t size; 241 | uint32_t offset; 242 | uint32_t align; 243 | uint32_t reloff; 244 | uint32_t nreloc; 245 | uint32_t flags; 246 | uint32_t reserved1; 247 | uint32_t reserved2; 248 | } _packed; 249 | 250 | struct section_64 { 251 | char sectname[16]; 252 | char segname[16]; 253 | uint64_t addr; 254 | uint64_t size; 255 | uint32_t offset; 256 | uint32_t align; 257 | uint32_t reloff; 258 | uint32_t nreloc; 259 | uint32_t flags; 260 | uint32_t reserved1; 261 | uint32_t reserved2; 262 | } _packed; 263 | 264 | struct linkedit_data_command { 265 | uint32_t cmd; 266 | uint32_t cmdsize; 267 | uint32_t dataoff; 268 | uint32_t datasize; 269 | } _packed; 270 | 271 | struct encryption_info_command { 272 | uint32_t cmd; 273 | uint32_t cmdsize; 274 | uint32_t cryptoff; 275 | uint32_t cryptsize; 276 | uint32_t cryptid; 277 | } _packed; 278 | 279 | uint16_t Swap_(uint16_t value) { 280 | return 281 | ((value >> 8) & 0x00ff) | 282 | ((value << 8) & 0xff00); 283 | } 284 | 285 | uint32_t Swap_(uint32_t value) { 286 | value = ((value >> 8) & 0x00ff00ff) | 287 | ((value << 8) & 0xff00ff00); 288 | value = ((value >> 16) & 0x0000ffff) | 289 | ((value << 16) & 0xffff0000); 290 | return value; 291 | } 292 | 293 | int16_t Swap_(int16_t value) { 294 | return Swap_(static_cast(value)); 295 | } 296 | 297 | int32_t Swap_(int32_t value) { 298 | return Swap_(static_cast(value)); 299 | } 300 | 301 | bool little_(true); 302 | 303 | uint16_t Swap(uint16_t value) { 304 | return little_ ? Swap_(value) : value; 305 | } 306 | 307 | uint32_t Swap(uint32_t value) { 308 | return little_ ? Swap_(value) : value; 309 | } 310 | 311 | int16_t Swap(int16_t value) { 312 | return Swap(static_cast(value)); 313 | } 314 | 315 | int32_t Swap(int32_t value) { 316 | return Swap(static_cast(value)); 317 | } 318 | 319 | template 320 | class Pointer; 321 | 322 | class Data { 323 | private: 324 | void *base_; 325 | size_t size_; 326 | 327 | protected: 328 | bool swapped_; 329 | 330 | public: 331 | Data(void *base, size_t size) : 332 | base_(base), 333 | size_(size), 334 | swapped_(false) 335 | { 336 | } 337 | 338 | uint16_t Swap(uint16_t value) const { 339 | return swapped_ ? Swap_(value) : value; 340 | } 341 | 342 | uint32_t Swap(uint32_t value) const { 343 | return swapped_ ? Swap_(value) : value; 344 | } 345 | 346 | int16_t Swap(int16_t value) const { 347 | return Swap(static_cast(value)); 348 | } 349 | 350 | int32_t Swap(int32_t value) const { 351 | return Swap(static_cast(value)); 352 | } 353 | 354 | void *GetBase() const { 355 | return base_; 356 | } 357 | 358 | size_t GetSize() const { 359 | return size_; 360 | } 361 | }; 362 | 363 | class MachHeader : 364 | public Data 365 | { 366 | private: 367 | bool bits64_; 368 | 369 | struct mach_header *mach_header_; 370 | struct load_command *load_command_; 371 | 372 | public: 373 | MachHeader(void *base, size_t size) : 374 | Data(base, size) 375 | { 376 | mach_header_ = (mach_header *) base; 377 | 378 | switch (Swap(mach_header_->magic)) { 379 | case MH_CIGAM: 380 | swapped_ = !swapped_; 381 | case MH_MAGIC: 382 | bits64_ = false; 383 | break; 384 | 385 | case MH_CIGAM_64: 386 | swapped_ = !swapped_; 387 | case MH_MAGIC_64: 388 | bits64_ = true; 389 | break; 390 | 391 | default: 392 | _assert(false); 393 | } 394 | 395 | void *post = mach_header_ + 1; 396 | if (bits64_) 397 | post = (uint32_t *) post + 1; 398 | load_command_ = (struct load_command *) post; 399 | 400 | _assert( 401 | Swap(mach_header_->filetype) == MH_EXECUTE || 402 | Swap(mach_header_->filetype) == MH_DYLIB || 403 | Swap(mach_header_->filetype) == MH_BUNDLE 404 | ); 405 | } 406 | 407 | struct mach_header *operator ->() const { 408 | return mach_header_; 409 | } 410 | 411 | uint32_t GetCPUType() const { 412 | return Swap(mach_header_->cputype); 413 | } 414 | 415 | uint16_t GetCPUSubtype() const { 416 | return Swap(mach_header_->cpusubtype) & 0xff; 417 | } 418 | 419 | std::vector GetLoadCommands() const { 420 | std::vector load_commands; 421 | 422 | struct load_command *load_command = load_command_; 423 | for (uint32_t cmd = 0; cmd != Swap(mach_header_->ncmds); ++cmd) { 424 | load_commands.push_back(load_command); 425 | load_command = (struct load_command *) ((uint8_t *) load_command + Swap(load_command->cmdsize)); 426 | } 427 | 428 | return load_commands; 429 | } 430 | 431 | std::vector GetSegments(const char *segment_name) const { 432 | std::vector segment_commands; 433 | 434 | _foreach (load_command, GetLoadCommands()) { 435 | if (Swap(load_command->cmd) == LC_SEGMENT) { 436 | segment_command *segment_command = reinterpret_cast(load_command); 437 | if (strncmp(segment_command->segname, segment_name, 16) == 0) 438 | segment_commands.push_back(segment_command); 439 | } 440 | } 441 | 442 | return segment_commands; 443 | } 444 | 445 | std::vector GetSegments64(const char *segment_name) { 446 | std::vector segment_commands; 447 | 448 | _foreach (load_command, GetLoadCommands()) { 449 | if (Swap(load_command->cmd) == LC_SEGMENT_64) { 450 | segment_command_64 *segment_command = reinterpret_cast(load_command); 451 | if (strncmp(segment_command->segname, segment_name, 16) == 0) 452 | segment_commands.push_back(segment_command); 453 | } 454 | } 455 | 456 | return segment_commands; 457 | } 458 | 459 | std::vector
GetSections(const char *segment_name, const char *section_name) const { 460 | std::vector
sections; 461 | 462 | _foreach (segment, GetSegments(segment_name)) { 463 | section *section = (struct section *) (segment + 1); 464 | 465 | uint32_t sect; 466 | for (sect = 0; sect != Swap(segment->nsects); ++sect) { 467 | if (strncmp(section->sectname, section_name, 16) == 0) 468 | sections.push_back(section); 469 | ++section; 470 | } 471 | } 472 | 473 | return sections; 474 | } 475 | 476 | template 477 | Pointer GetPointer(uint32_t address, const char *segment_name = NULL) const { 478 | load_command *load_command = (struct load_command *) (mach_header_ + 1); 479 | uint32_t cmd; 480 | 481 | for (cmd = 0; cmd != Swap(mach_header_->ncmds); ++cmd) { 482 | if (Swap(load_command->cmd) == LC_SEGMENT) { 483 | segment_command *segment_command = (struct segment_command *) load_command; 484 | if (segment_name != NULL && strncmp(segment_command->segname, segment_name, 16) != 0) 485 | goto next_command; 486 | 487 | section *sections = (struct section *) (segment_command + 1); 488 | 489 | uint32_t sect; 490 | for (sect = 0; sect != Swap(segment_command->nsects); ++sect) { 491 | section *section = §ions[sect]; 492 | //printf("%s %u %p %p %u\n", segment_command->segname, sect, address, section->addr, section->size); 493 | if (address >= Swap(section->addr) && address < Swap(section->addr) + Swap(section->size)) { 494 | //printf("0x%.8x %s\n", address, segment_command->segname); 495 | return Pointer(this, reinterpret_cast(address - Swap(section->addr) + Swap(section->offset) + (char *) mach_header_)); 496 | } 497 | } 498 | } 499 | 500 | next_command: 501 | load_command = (struct load_command *) ((char *) load_command + Swap(load_command->cmdsize)); 502 | } 503 | 504 | return Pointer(this); 505 | } 506 | 507 | template 508 | Pointer GetOffset(uint32_t offset) { 509 | return Pointer(this, reinterpret_cast(offset + (uint8_t *) mach_header_)); 510 | } 511 | }; 512 | 513 | class FatMachHeader : 514 | public MachHeader 515 | { 516 | private: 517 | fat_arch *fat_arch_; 518 | 519 | public: 520 | FatMachHeader(void *base, size_t size, fat_arch *fat_arch) : 521 | MachHeader(base, size), 522 | fat_arch_(fat_arch) 523 | { 524 | } 525 | 526 | fat_arch *GetFatArch() const { 527 | return fat_arch_; 528 | } 529 | }; 530 | 531 | class FatHeader : 532 | public Data 533 | { 534 | private: 535 | fat_header *fat_header_; 536 | std::vector mach_headers_; 537 | 538 | public: 539 | FatHeader(void *base, size_t size) : 540 | Data(base, size) 541 | { 542 | fat_header_ = reinterpret_cast(base); 543 | 544 | if (Swap(fat_header_->magic) == FAT_CIGAM) { 545 | swapped_ = !swapped_; 546 | goto fat; 547 | } else if (Swap(fat_header_->magic) != FAT_MAGIC) { 548 | fat_header_ = NULL; 549 | mach_headers_.push_back(FatMachHeader(base, size, NULL)); 550 | } else fat: { 551 | size_t fat_narch = Swap(fat_header_->nfat_arch); 552 | fat_arch *fat_arch = reinterpret_cast(fat_header_ + 1); 553 | size_t arch; 554 | for (arch = 0; arch != fat_narch; ++arch) { 555 | uint32_t arch_offset = Swap(fat_arch->offset); 556 | uint32_t arch_size = Swap(fat_arch->size); 557 | mach_headers_.push_back(FatMachHeader((uint8_t *) base + arch_offset, arch_size, fat_arch)); 558 | ++fat_arch; 559 | } 560 | } 561 | } 562 | 563 | std::vector &GetMachHeaders() { 564 | return mach_headers_; 565 | } 566 | 567 | bool IsFat() const { 568 | return fat_header_ != NULL; 569 | } 570 | 571 | struct fat_header *operator ->() const { 572 | return fat_header_; 573 | } 574 | }; 575 | 576 | FatHeader Map(const char *path, bool ro = false) { 577 | size_t size; 578 | void *base(map(path, 0, _not(size_t), &size, ro)); 579 | return FatHeader(base, size); 580 | } 581 | 582 | template 583 | class Pointer { 584 | private: 585 | const MachHeader *framework_; 586 | const Target_ *pointer_; 587 | 588 | public: 589 | Pointer(const MachHeader *framework = NULL, const Target_ *pointer = NULL) : 590 | framework_(framework), 591 | pointer_(pointer) 592 | { 593 | } 594 | 595 | operator const Target_ *() const { 596 | return pointer_; 597 | } 598 | 599 | const Target_ *operator ->() const { 600 | return pointer_; 601 | } 602 | 603 | Pointer &operator ++() { 604 | ++pointer_; 605 | return *this; 606 | } 607 | 608 | template 609 | Value_ Swap(Value_ value) { 610 | return framework_->Swap(value); 611 | } 612 | }; 613 | 614 | #define CSMAGIC_CODEDIRECTORY uint32_t(0xfade0c02) 615 | #define CSMAGIC_EMBEDDED_SIGNATURE uint32_t(0xfade0cc0) 616 | #define CSMAGIC_ENTITLEMENTS uint32_t(0xfade7171) 617 | 618 | #define CSSLOT_CODEDIRECTORY uint32_t(0) 619 | #define CSSLOT_REQUIREMENTS uint32_t(2) 620 | #define CSSLOT_ENTITLEMENTS uint32_t(5) 621 | 622 | struct BlobIndex { 623 | uint32_t type; 624 | uint32_t offset; 625 | } _packed; 626 | 627 | struct Blob { 628 | uint32_t magic; 629 | uint32_t length; 630 | } _packed; 631 | 632 | struct SuperBlob { 633 | struct Blob blob; 634 | uint32_t count; 635 | struct BlobIndex index[]; 636 | } _packed; 637 | 638 | struct CodeDirectory { 639 | struct Blob blob; 640 | uint32_t version; 641 | uint32_t flags; 642 | uint32_t hashOffset; 643 | uint32_t identOffset; 644 | uint32_t nSpecialSlots; 645 | uint32_t nCodeSlots; 646 | uint32_t codeLimit; 647 | uint8_t hashSize; 648 | uint8_t hashType; 649 | uint8_t spare1; 650 | uint8_t pageSize; 651 | uint32_t spare2; 652 | } _packed; 653 | 654 | extern "C" uint32_t hash(uint8_t *k, uint32_t length, uint32_t initval); 655 | 656 | void sha1(uint8_t *hash, uint8_t *data, size_t size) { 657 | SHA1Context context; 658 | SHA1Reset(&context); 659 | SHA1Input(&context, data, size); 660 | SHA1Result(&context, hash); 661 | } 662 | 663 | struct CodesignAllocation { 664 | uint32_t type_; 665 | uint16_t subtype_; 666 | size_t size_; 667 | 668 | CodesignAllocation(uint32_t type, uint16_t subtype, size_t size) : 669 | type_(type), 670 | subtype_(subtype), 671 | size_(size) 672 | { 673 | } 674 | }; 675 | 676 | int main(int argc, const char *argv[]) { 677 | union { 678 | uint16_t word; 679 | uint8_t byte[2]; 680 | } endian = {1}; 681 | 682 | little_ = endian.byte[0]; 683 | 684 | bool flag_R(false); 685 | bool flag_r(false); 686 | 687 | bool flag_t(false); 688 | bool flag_p(false); 689 | bool flag_u(false); 690 | bool flag_e(false); 691 | 692 | bool flag_T(false); 693 | 694 | bool flag_S(false); 695 | bool flag_s(false); 696 | 697 | bool flag_O(false); 698 | 699 | bool flag_D(false); 700 | bool flag_d(false); 701 | 702 | bool flag_A(false); 703 | bool flag_a(false); 704 | 705 | uint32_t flag_CPUType(_not(uint32_t)); 706 | uint32_t flag_CPUSubtype(_not(uint32_t)); 707 | 708 | bool timeh(false); 709 | uint32_t timev(0); 710 | 711 | const void *xmld(NULL); 712 | size_t xmls(0); 713 | 714 | uintptr_t noffset(_not(uintptr_t)); 715 | uintptr_t woffset(_not(uintptr_t)); 716 | 717 | std::vector files; 718 | 719 | if (argc == 1) { 720 | fprintf(stderr, "usage: %s -S[entitlements.xml] \n", argv[0]); 721 | fprintf(stderr, " %s -e MobileSafari\n", argv[0]); 722 | fprintf(stderr, " %s -S cat\n", argv[0]); 723 | fprintf(stderr, " %s -Stfp.xml gdb\n", argv[0]); 724 | exit(0); 725 | } 726 | 727 | for (int argi(1); argi != argc; ++argi) 728 | if (argv[argi][0] != '-') 729 | files.push_back(argv[argi]); 730 | else switch (argv[argi][1]) { 731 | case 'R': flag_R = true; break; 732 | case 'r': flag_r = true; break; 733 | 734 | case 't': flag_t = true; break; 735 | case 'u': flag_u = true; break; 736 | case 'p': flag_p = true; break; 737 | case 'e': flag_e = true; break; 738 | case 'O': flag_O = true; break; 739 | 740 | case 'D': flag_D = true; break; 741 | case 'd': flag_d = true; break; 742 | 743 | case 'a': flag_a = true; break; 744 | 745 | case 'A': 746 | flag_A = true; 747 | if (argv[argi][2] != '\0') { 748 | const char *cpu = argv[argi] + 2; 749 | const char *colon = strchr(cpu, ':'); 750 | _assert(colon != NULL); 751 | char *arge; 752 | flag_CPUType = strtoul(cpu, &arge, 0); 753 | _assert(arge == colon); 754 | flag_CPUSubtype = strtoul(colon + 1, &arge, 0); 755 | _assert(arge == argv[argi] + strlen(argv[argi])); 756 | } 757 | break; 758 | 759 | case 's': 760 | _assert(!flag_S); 761 | flag_s = true; 762 | break; 763 | 764 | case 'S': 765 | _assert(!flag_s); 766 | flag_S = true; 767 | if (argv[argi][2] != '\0') { 768 | const char *xml = argv[argi] + 2; 769 | xmld = map(xml, 0, _not(size_t), &xmls, true); 770 | } 771 | break; 772 | 773 | case 'T': { 774 | flag_T = true; 775 | if (argv[argi][2] == '-') 776 | timeh = true; 777 | else { 778 | char *arge; 779 | timev = strtoul(argv[argi] + 2, &arge, 0); 780 | _assert(arge == argv[argi] + strlen(argv[argi])); 781 | } 782 | } break; 783 | 784 | case 'n': { 785 | char *arge; 786 | noffset = strtoul(argv[argi] + 2, &arge, 0); 787 | _assert(arge == argv[argi] + strlen(argv[argi])); 788 | } break; 789 | 790 | case 'w': { 791 | char *arge; 792 | woffset = strtoul(argv[argi] + 2, &arge, 0); 793 | _assert(arge == argv[argi] + strlen(argv[argi])); 794 | } break; 795 | 796 | default: 797 | goto usage; 798 | break; 799 | } 800 | 801 | if (files.empty()) usage: { 802 | exit(0); 803 | } 804 | 805 | size_t filei(0), filee(0); 806 | _foreach (file, files) try { 807 | const char *path(file.c_str()); 808 | const char *base = strrchr(path, '/'); 809 | char *temp(NULL), *dir; 810 | 811 | if (base != NULL) 812 | dir = strndup_(path, base++ - path + 1); 813 | else { 814 | dir = strdup(""); 815 | base = path; 816 | } 817 | 818 | if (flag_r) { 819 | uint32_t clip(0); { 820 | FatHeader fat_header(Map(path)); 821 | _foreach (mach_header, fat_header.GetMachHeaders()) { 822 | if (flag_A) { 823 | if (mach_header.GetCPUType() != flag_CPUType) 824 | continue; 825 | if (mach_header.GetCPUSubtype() != flag_CPUSubtype) 826 | continue; 827 | } 828 | 829 | mach_header->flags = mach_header.Swap(mach_header.Swap(mach_header->flags) | MH_DYLDLINK); 830 | 831 | uint32_t size(_not(uint32_t)); { 832 | _foreach (load_command, mach_header.GetLoadCommands()) { 833 | switch (mach_header.Swap(load_command->cmd)) { 834 | case LC_CODE_SIGNATURE: { 835 | struct linkedit_data_command *signature = reinterpret_cast(load_command); 836 | memset(reinterpret_cast(mach_header.GetBase()) + mach_header.Swap(signature->dataoff), 0, mach_header.Swap(signature->datasize)); 837 | memset(signature, 0, sizeof(struct linkedit_data_command)); 838 | 839 | mach_header->ncmds = mach_header.Swap(mach_header.Swap(mach_header->ncmds) - 1); 840 | mach_header->sizeofcmds = mach_header.Swap(uint32_t(mach_header.Swap(mach_header->sizeofcmds) - sizeof(struct linkedit_data_command))); 841 | } break; 842 | 843 | case LC_SYMTAB: { 844 | struct symtab_command *symtab = reinterpret_cast(load_command); 845 | size = mach_header.Swap(symtab->stroff) + mach_header.Swap(symtab->strsize); 846 | } break; 847 | } 848 | } 849 | } 850 | 851 | _assert(size != _not(uint32_t)); 852 | 853 | _foreach (segment, const_cast(mach_header).GetSegments("__LINKEDIT")) { 854 | segment->filesize -= mach_header.GetSize() - size; 855 | 856 | if (fat_arch *fat_arch = mach_header.GetFatArch()) { 857 | fat_arch->size = fat_header.Swap(size); 858 | clip = std::max(clip, fat_header.Swap(fat_arch->offset) + size); 859 | } else 860 | clip = std::max(clip, size); 861 | } 862 | 863 | _foreach (segment, const_cast(mach_header).GetSegments64("__LINKEDIT")) { 864 | segment->filesize -= mach_header.GetSize() - size; 865 | 866 | if (fat_arch *fat_arch = mach_header.GetFatArch()) { 867 | fat_arch->size = fat_header.Swap(size); 868 | clip = std::max(clip, fat_header.Swap(fat_arch->offset) + size); 869 | } else 870 | clip = std::max(clip, size); 871 | } 872 | } 873 | } 874 | 875 | if (clip != 0) 876 | _syscall(truncate(path, clip)); 877 | } 878 | 879 | if (flag_S) { 880 | asprintf(&temp, "%s.%s.cs", dir, base); 881 | const char *allocate = getenv("CODESIGN_ALLOCATE"); 882 | if (allocate == NULL) 883 | allocate = "codesign_allocate"; 884 | 885 | std::vector allocations; { 886 | FatHeader fat_header(Map(path)); 887 | _foreach (mach_header, fat_header.GetMachHeaders()) { 888 | if (flag_A) { 889 | if (mach_header.GetCPUType() != flag_CPUType) 890 | continue; 891 | if (mach_header.GetCPUSubtype() != flag_CPUSubtype) 892 | continue; 893 | } 894 | 895 | mach_header->flags = mach_header.Swap(mach_header.Swap(mach_header->flags) | MH_DYLDLINK); 896 | 897 | size_t size(_not(size_t)); { 898 | _foreach (load_command, mach_header.GetLoadCommands()) { 899 | uint32_t cmd(mach_header.Swap(load_command->cmd)); 900 | if (cmd == LC_CODE_SIGNATURE) { 901 | struct linkedit_data_command *signature = reinterpret_cast(load_command); 902 | size = mach_header.Swap(signature->dataoff); 903 | _assert(size < mach_header.GetSize()); 904 | break; 905 | } 906 | } 907 | 908 | if (size == _not(size_t)) 909 | size = mach_header.GetSize(); 910 | } 911 | 912 | allocations.push_back(CodesignAllocation(mach_header.GetCPUType(), mach_header.GetCPUSubtype(), size)); 913 | } 914 | } 915 | 916 | if (!allocations.empty()) { 917 | 918 | pid_t pid = fork(); 919 | _syscall(pid); 920 | if (pid == 0) { 921 | // XXX: this leaks memory, but it doesn't really matter 922 | std::vector args; 923 | char *arg; 924 | 925 | args.push_back(allocate); 926 | 927 | args.push_back("-i"); 928 | args.push_back(path); 929 | 930 | _foreach (allocation, allocations) { 931 | args.push_back("-A"); 932 | 933 | asprintf(&arg, "%u", allocation.type_); 934 | args.push_back(arg); 935 | 936 | asprintf(&arg, "%u", allocation.subtype_); 937 | args.push_back(arg); 938 | 939 | size_t alloc(0); 940 | alloc += sizeof(struct SuperBlob); 941 | uint32_t special(0); 942 | 943 | special = std::max(special, CSSLOT_CODEDIRECTORY); 944 | alloc += sizeof(struct BlobIndex); 945 | alloc += sizeof(struct CodeDirectory); 946 | alloc += strlen(base) + 1; 947 | 948 | special = std::max(special, CSSLOT_REQUIREMENTS); 949 | alloc += sizeof(struct BlobIndex); 950 | alloc += 0xc; 951 | 952 | if (xmld != NULL) { 953 | special = std::max(special, CSSLOT_ENTITLEMENTS); 954 | alloc += sizeof(struct BlobIndex); 955 | alloc += sizeof(struct Blob); 956 | alloc += xmls; 957 | } 958 | 959 | size_t normal((allocation.size_ + 0x1000 - 1) / 0x1000); 960 | alloc += (special + normal) * 0x14; 961 | 962 | alloc += 15; 963 | alloc /= 16; 964 | alloc *= 16; 965 | 966 | asprintf(&arg, "%zu", alloc); 967 | args.push_back(arg); 968 | } 969 | 970 | args.push_back("-o"); 971 | args.push_back(temp); 972 | 973 | args.push_back(NULL); 974 | 975 | if (false) { 976 | printf("run:"); 977 | _foreach (arg, args) 978 | printf(" %s", arg); 979 | printf("\n"); 980 | } 981 | 982 | execvp(allocate, (char **) &args[0]); 983 | _assert(false); 984 | } 985 | 986 | int status; 987 | _syscall(waitpid(pid, &status, 0)); 988 | _assert(WIFEXITED(status)); 989 | _assert(WEXITSTATUS(status) == 0); 990 | 991 | } 992 | } 993 | 994 | if (flag_p) 995 | printf("path%zu='%s'\n", filei, file.c_str()); 996 | 997 | FatHeader fat_header(Map(temp == NULL ? path : temp, !(flag_R | flag_T | flag_s | flag_S | flag_O | flag_D))); 998 | struct linkedit_data_command *signature(NULL); 999 | 1000 | _foreach (mach_header, fat_header.GetMachHeaders()) { 1001 | if (flag_A) { 1002 | if (mach_header.GetCPUType() != flag_CPUType) 1003 | continue; 1004 | if (mach_header.GetCPUSubtype() != flag_CPUSubtype) 1005 | continue; 1006 | } 1007 | 1008 | if (flag_a) 1009 | printf("cpu=0x%x:0x%x\n", mach_header.GetCPUType(), mach_header.GetCPUSubtype()); 1010 | 1011 | if (flag_d) { 1012 | if (struct fat_arch *fat_arch = mach_header.GetFatArch()) 1013 | printf("offset=0x%x\n", Swap(fat_arch->offset)); 1014 | else 1015 | printf("offset=0x0\n"); 1016 | } 1017 | 1018 | if (woffset != _not(uintptr_t)) { 1019 | Pointer wvalue(mach_header.GetPointer(woffset)); 1020 | if (wvalue == NULL) 1021 | printf("(null) %p\n", reinterpret_cast(woffset)); 1022 | else 1023 | printf("0x%.08x\n", *wvalue); 1024 | } 1025 | 1026 | if (noffset != _not(uintptr_t)) 1027 | printf("%s\n", &*mach_header.GetPointer(noffset)); 1028 | 1029 | if (flag_d) 1030 | _foreach(segment, mach_header.GetSegments("__TEXT")) { 1031 | printf("vmaddr=0x%x\n", mach_header.Swap(segment->vmaddr)); 1032 | printf("fileoff=0x%x\n", mach_header.Swap(segment->fileoff)); 1033 | } 1034 | 1035 | if (flag_O) { 1036 | _foreach(section, mach_header.GetSections("__TEXT", "__text")) 1037 | section->addr = mach_header.Swap(0); 1038 | } 1039 | 1040 | _foreach (load_command, mach_header.GetLoadCommands()) { 1041 | uint32_t cmd(mach_header.Swap(load_command->cmd)); 1042 | 1043 | if (flag_R && cmd == LC_REEXPORT_DYLIB) 1044 | load_command->cmd = mach_header.Swap(LC_LOAD_DYLIB); 1045 | else if (cmd == LC_CODE_SIGNATURE) 1046 | signature = reinterpret_cast(load_command); 1047 | else if (cmd == LC_UUID) { 1048 | volatile struct uuid_command *uuid_command(reinterpret_cast(load_command)); 1049 | 1050 | if (flag_u) { 1051 | printf("uuid%zu=%.2x%.2x%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x%.2x%.2x%.2x%.2x\n", filei, 1052 | uuid_command->uuid[ 0], uuid_command->uuid[ 1], uuid_command->uuid[ 2], uuid_command->uuid[ 3], 1053 | uuid_command->uuid[ 4], uuid_command->uuid[ 5], uuid_command->uuid[ 6], uuid_command->uuid[ 7], 1054 | uuid_command->uuid[ 8], uuid_command->uuid[ 9], uuid_command->uuid[10], uuid_command->uuid[11], 1055 | uuid_command->uuid[12], uuid_command->uuid[13], uuid_command->uuid[14], uuid_command->uuid[15] 1056 | ); 1057 | } 1058 | } else if (cmd == LC_ID_DYLIB) { 1059 | volatile struct dylib_command *dylib_command(reinterpret_cast(load_command)); 1060 | 1061 | if (flag_t) 1062 | printf("time%zu=0x%.8x\n", filei, mach_header.Swap(dylib_command->dylib.timestamp)); 1063 | 1064 | if (flag_T) { 1065 | uint32_t timed; 1066 | 1067 | if (!timeh) 1068 | timed = timev; 1069 | else { 1070 | dylib_command->dylib.timestamp = 0; 1071 | timed = hash(reinterpret_cast(mach_header.GetBase()), mach_header.GetSize(), timev); 1072 | } 1073 | 1074 | dylib_command->dylib.timestamp = mach_header.Swap(timed); 1075 | } 1076 | } else if (cmd == LC_ENCRYPTION_INFO) { 1077 | volatile struct encryption_info_command *encryption_info_command(reinterpret_cast(load_command)); 1078 | 1079 | if (flag_D) 1080 | encryption_info_command->cryptid = mach_header.Swap(0); 1081 | 1082 | if (flag_d) { 1083 | printf("cryptoff=0x%x\n", mach_header.Swap(encryption_info_command->cryptoff)); 1084 | printf("cryptsize=0x%x\n", mach_header.Swap(encryption_info_command->cryptsize)); 1085 | printf("cryptid=0x%x\n", mach_header.Swap(encryption_info_command->cryptid)); 1086 | } 1087 | } 1088 | } 1089 | 1090 | if (flag_e) { 1091 | _assert(signature != NULL); 1092 | 1093 | uint32_t data = mach_header.Swap(signature->dataoff); 1094 | 1095 | uint8_t *top = reinterpret_cast(mach_header.GetBase()); 1096 | uint8_t *blob = top + data; 1097 | struct SuperBlob *super = reinterpret_cast(blob); 1098 | 1099 | for (size_t index(0); index != Swap(super->count); ++index) 1100 | if (Swap(super->index[index].type) == CSSLOT_ENTITLEMENTS) { 1101 | uint32_t begin = Swap(super->index[index].offset); 1102 | struct Blob *entitlements = reinterpret_cast(blob + begin); 1103 | fwrite(entitlements + 1, 1, Swap(entitlements->length) - sizeof(struct Blob), stdout); 1104 | } 1105 | } 1106 | 1107 | if (flag_s) { 1108 | _assert(signature != NULL); 1109 | 1110 | uint32_t data = mach_header.Swap(signature->dataoff); 1111 | 1112 | uint8_t *top = reinterpret_cast(mach_header.GetBase()); 1113 | uint8_t *blob = top + data; 1114 | struct SuperBlob *super = reinterpret_cast(blob); 1115 | 1116 | for (size_t index(0); index != Swap(super->count); ++index) 1117 | if (Swap(super->index[index].type) == CSSLOT_CODEDIRECTORY) { 1118 | uint32_t begin = Swap(super->index[index].offset); 1119 | struct CodeDirectory *directory = reinterpret_cast(blob + begin); 1120 | 1121 | uint8_t (*hashes)[20] = reinterpret_cast(blob + begin + Swap(directory->hashOffset)); 1122 | uint32_t pages = Swap(directory->nCodeSlots); 1123 | 1124 | if (pages != 1) 1125 | for (size_t i = 0; i != pages - 1; ++i) 1126 | sha1(hashes[i], top + 0x1000 * i, 0x1000); 1127 | if (pages != 0) 1128 | sha1(hashes[pages - 1], top + 0x1000 * (pages - 1), ((data - 1) % 0x1000) + 1); 1129 | } 1130 | } 1131 | 1132 | if (flag_S) { 1133 | _assert(signature != NULL); 1134 | 1135 | uint32_t data = mach_header.Swap(signature->dataoff); 1136 | uint32_t size = mach_header.Swap(signature->datasize); 1137 | 1138 | uint8_t *top = reinterpret_cast(mach_header.GetBase()); 1139 | uint8_t *blob = top + data; 1140 | struct SuperBlob *super = reinterpret_cast(blob); 1141 | super->blob.magic = Swap(CSMAGIC_EMBEDDED_SIGNATURE); 1142 | 1143 | uint32_t count = xmld == NULL ? 2 : 3; 1144 | uint32_t offset = sizeof(struct SuperBlob) + count * sizeof(struct BlobIndex); 1145 | 1146 | super->index[0].type = Swap(CSSLOT_CODEDIRECTORY); 1147 | super->index[0].offset = Swap(offset); 1148 | 1149 | uint32_t begin = offset; 1150 | struct CodeDirectory *directory = reinterpret_cast(blob + begin); 1151 | offset += sizeof(struct CodeDirectory); 1152 | 1153 | directory->blob.magic = Swap(CSMAGIC_CODEDIRECTORY); 1154 | directory->version = Swap(uint32_t(0x00020001)); 1155 | directory->flags = Swap(uint32_t(0)); 1156 | directory->codeLimit = Swap(data); 1157 | directory->hashSize = 0x14; 1158 | directory->hashType = 0x01; 1159 | directory->spare1 = 0x00; 1160 | directory->pageSize = 0x0c; 1161 | directory->spare2 = Swap(uint32_t(0)); 1162 | 1163 | directory->identOffset = Swap(offset - begin); 1164 | strcpy(reinterpret_cast(blob + offset), base); 1165 | offset += strlen(base) + 1; 1166 | 1167 | uint32_t special = xmld == NULL ? CSSLOT_REQUIREMENTS : CSSLOT_ENTITLEMENTS; 1168 | directory->nSpecialSlots = Swap(special); 1169 | 1170 | uint8_t (*hashes)[20] = reinterpret_cast(blob + offset); 1171 | memset(hashes, 0, sizeof(*hashes) * special); 1172 | 1173 | offset += sizeof(*hashes) * special; 1174 | hashes += special; 1175 | 1176 | uint32_t pages = (data + 0x1000 - 1) / 0x1000; 1177 | directory->nCodeSlots = Swap(pages); 1178 | 1179 | if (pages != 1) 1180 | for (size_t i = 0; i != pages - 1; ++i) 1181 | sha1(hashes[i], top + 0x1000 * i, 0x1000); 1182 | if (pages != 0) 1183 | sha1(hashes[pages - 1], top + 0x1000 * (pages - 1), ((data - 1) % 0x1000) + 1); 1184 | 1185 | directory->hashOffset = Swap(offset - begin); 1186 | offset += sizeof(*hashes) * pages; 1187 | directory->blob.length = Swap(offset - begin); 1188 | 1189 | super->index[1].type = Swap(CSSLOT_REQUIREMENTS); 1190 | super->index[1].offset = Swap(offset); 1191 | 1192 | memcpy(blob + offset, "\xfa\xde\x0c\x01\x00\x00\x00\x0c\x00\x00\x00\x00", 0xc); 1193 | offset += 0xc; 1194 | 1195 | if (xmld != NULL) { 1196 | super->index[2].type = Swap(CSSLOT_ENTITLEMENTS); 1197 | super->index[2].offset = Swap(offset); 1198 | 1199 | uint32_t begin = offset; 1200 | struct Blob *entitlements = reinterpret_cast(blob + begin); 1201 | offset += sizeof(struct Blob); 1202 | 1203 | memcpy(blob + offset, xmld, xmls); 1204 | offset += xmls; 1205 | 1206 | entitlements->magic = Swap(CSMAGIC_ENTITLEMENTS); 1207 | entitlements->length = Swap(offset - begin); 1208 | } 1209 | 1210 | for (size_t index(0); index != count; ++index) { 1211 | uint32_t type = Swap(super->index[index].type); 1212 | if (type != 0 && type <= special) { 1213 | uint32_t offset = Swap(super->index[index].offset); 1214 | struct Blob *local = (struct Blob *) (blob + offset); 1215 | sha1((uint8_t *) (hashes - type), (uint8_t *) local, Swap(local->length)); 1216 | } 1217 | } 1218 | 1219 | super->count = Swap(count); 1220 | super->blob.length = Swap(offset); 1221 | 1222 | if (offset > size) { 1223 | fprintf(stderr, "offset (%u) > size (%u)\n", offset, size); 1224 | _assert(false); 1225 | } //else fprintf(stderr, "offset (%zu) <= size (%zu)\n", offset, size); 1226 | 1227 | memset(blob + offset, 0, size - offset); 1228 | } 1229 | } 1230 | 1231 | if (flag_S) { 1232 | uint8_t *top = reinterpret_cast(fat_header.GetBase()); 1233 | size_t size = fat_header.GetSize(); 1234 | 1235 | char *copy; 1236 | asprintf(©, "%s.%s.cp", dir, base); 1237 | FILE *file = fopen(copy, "w+"); 1238 | size_t writ = fwrite(top, 1, size, file); 1239 | _assert(writ == size); 1240 | fclose(file); 1241 | 1242 | _syscall(unlink(temp)); 1243 | free(temp); 1244 | temp = copy; 1245 | } 1246 | 1247 | if (temp != NULL) { 1248 | struct stat info; 1249 | _syscall(stat(path, &info)); 1250 | _syscall(chown(temp, info.st_uid, info.st_gid)); 1251 | _syscall(chmod(temp, info.st_mode)); 1252 | _syscall(unlink(path)); 1253 | _syscall(rename(temp, path)); 1254 | free(temp); 1255 | } 1256 | 1257 | free(dir); 1258 | ++filei; 1259 | } catch (const char *) { 1260 | ++filee; 1261 | ++filei; 1262 | } 1263 | 1264 | return filee; 1265 | } 1266 | -------------------------------------------------------------------------------- /lookup2.c: -------------------------------------------------------------------------------- 1 | /* 2 | -------------------------------------------------------------------- 3 | lookup2.c, by Bob Jenkins, December 1996, Public Domain. 4 | hash(), hash2(), hash3, and mix() are externally useful functions. 5 | Routines to test the hash are included if SELF_TEST is defined. 6 | You can use this free for any purpose. It has no warranty. 7 | -------------------------------------------------------------------- 8 | */ 9 | #include 10 | #include 11 | #include 12 | typedef unsigned long int ub4; /* unsigned 4-byte quantities */ 13 | typedef unsigned char ub1; 14 | 15 | #define hashsize(n) ((ub4)1<<(n)) 16 | #define hashmask(n) (hashsize(n)-1) 17 | 18 | /* 19 | -------------------------------------------------------------------- 20 | mix -- mix 3 32-bit values reversibly. 21 | For every delta with one or two bit set, and the deltas of all three 22 | high bits or all three low bits, whether the original value of a,b,c 23 | is almost all zero or is uniformly distributed, 24 | * If mix() is run forward or backward, at least 32 bits in a,b,c 25 | have at least 1/4 probability of changing. 26 | * If mix() is run forward, every bit of c will change between 1/3 and 27 | 2/3 of the time. (Well, 22/100 and 78/100 for some 2-bit deltas.) 28 | mix() was built out of 36 single-cycle latency instructions in a 29 | structure that could supported 2x parallelism, like so: 30 | a -= b; 31 | a -= c; x = (c>>13); 32 | b -= c; a ^= x; 33 | b -= a; x = (a<<8); 34 | c -= a; b ^= x; 35 | c -= b; x = (b>>13); 36 | ... 37 | Unfortunately, superscalar Pentiums and Sparcs can't take advantage 38 | of that parallelism. They've also turned some of those single-cycle 39 | latency instructions into multi-cycle latency instructions. Still, 40 | this is the fastest good hash I could find. There were about 2^^68 41 | to choose from. I only looked at a billion or so. 42 | -------------------------------------------------------------------- 43 | */ 44 | #define mix(a,b,c) \ 45 | { \ 46 | a -= b; a -= c; a ^= (c>>13); \ 47 | b -= c; b -= a; b ^= (a<<8); \ 48 | c -= a; c -= b; c ^= (b>>13); \ 49 | a -= b; a -= c; a ^= (c>>12); \ 50 | b -= c; b -= a; b ^= (a<<16); \ 51 | c -= a; c -= b; c ^= (b>>5); \ 52 | a -= b; a -= c; a ^= (c>>3); \ 53 | b -= c; b -= a; b ^= (a<<10); \ 54 | c -= a; c -= b; c ^= (b>>15); \ 55 | } 56 | 57 | /* same, but slower, works on systems that might have 8 byte ub4's */ 58 | #define mix2(a,b,c) \ 59 | { \ 60 | a -= b; a -= c; a ^= (c>>13); \ 61 | b -= c; b -= a; b ^= (a<< 8); \ 62 | c -= a; c -= b; c ^= ((b&0xffffffff)>>13); \ 63 | a -= b; a -= c; a ^= ((c&0xffffffff)>>12); \ 64 | b -= c; b -= a; b = (b ^ (a<<16)) & 0xffffffff; \ 65 | c -= a; c -= b; c = (c ^ (b>> 5)) & 0xffffffff; \ 66 | a -= b; a -= c; a = (a ^ (c>> 3)) & 0xffffffff; \ 67 | b -= c; b -= a; b = (b ^ (a<<10)) & 0xffffffff; \ 68 | c -= a; c -= b; c = (c ^ (b>>15)) & 0xffffffff; \ 69 | } 70 | 71 | /* 72 | -------------------------------------------------------------------- 73 | hash() -- hash a variable-length key into a 32-bit value 74 | k : the key (the unaligned variable-length array of bytes) 75 | len : the length of the key, counting by bytes 76 | level : can be any 4-byte value 77 | Returns a 32-bit value. Every bit of the key affects every bit of 78 | the return value. Every 1-bit and 2-bit delta achieves avalanche. 79 | About 36+6len instructions. 80 | 81 | The best hash table sizes are powers of 2. There is no need to do 82 | mod a prime (mod is sooo slow!). If you need less than 32 bits, 83 | use a bitmask. For example, if you need only 10 bits, do 84 | h = (h & hashmask(10)); 85 | In which case, the hash table should have hashsize(10) elements. 86 | 87 | If you are hashing n strings (ub1 **)k, do it like this: 88 | for (i=0, h=0; i= 12) 113 | { 114 | a += (k[0] +((ub4)k[1]<<8) +((ub4)k[2]<<16) +((ub4)k[3]<<24)); 115 | b += (k[4] +((ub4)k[5]<<8) +((ub4)k[6]<<16) +((ub4)k[7]<<24)); 116 | c += (k[8] +((ub4)k[9]<<8) +((ub4)k[10]<<16)+((ub4)k[11]<<24)); 117 | mix(a,b,c); 118 | k += 12; len -= 12; 119 | } 120 | 121 | /*------------------------------------- handle the last 11 bytes */ 122 | c += length; 123 | switch(len) /* all the case statements fall through */ 124 | { 125 | case 11: c+=((ub4)k[10]<<24); 126 | case 10: c+=((ub4)k[9]<<16); 127 | case 9 : c+=((ub4)k[8]<<8); 128 | /* the first byte of c is reserved for the length */ 129 | case 8 : b+=((ub4)k[7]<<24); 130 | case 7 : b+=((ub4)k[6]<<16); 131 | case 6 : b+=((ub4)k[5]<<8); 132 | case 5 : b+=k[4]; 133 | case 4 : a+=((ub4)k[3]<<24); 134 | case 3 : a+=((ub4)k[2]<<16); 135 | case 2 : a+=((ub4)k[1]<<8); 136 | case 1 : a+=k[0]; 137 | /* case 0: nothing left to add */ 138 | } 139 | mix(a,b,c); 140 | /*-------------------------------------------- report the result */ 141 | return c; 142 | } 143 | 144 | 145 | /* 146 | -------------------------------------------------------------------- 147 | This works on all machines. hash2() is identical to hash() on 148 | little-endian machines, except that the length has to be measured 149 | in ub4s instead of bytes. It is much faster than hash(). It 150 | requires 151 | -- that the key be an array of ub4's, and 152 | -- that all your machines have the same endianness, and 153 | -- that the length be the number of ub4's in the key 154 | -------------------------------------------------------------------- 155 | */ 156 | ub4 hash2( k, length, initval) 157 | register ub4 *k; /* the key */ 158 | register ub4 length; /* the length of the key, in ub4s */ 159 | register ub4 initval; /* the previous hash, or an arbitrary value */ 160 | { 161 | register ub4 a,b,c,len; 162 | 163 | /* Set up the internal state */ 164 | len = length; 165 | a = b = 0x9e3779b9; /* the golden ratio; an arbitrary value */ 166 | c = initval; /* the previous hash value */ 167 | 168 | /*---------------------------------------- handle most of the key */ 169 | while (len >= 3) 170 | { 171 | a += k[0]; 172 | b += k[1]; 173 | c += k[2]; 174 | mix(a,b,c); 175 | k += 3; len -= 3; 176 | } 177 | 178 | /*-------------------------------------- handle the last 2 ub4's */ 179 | c += length; 180 | switch(len) /* all the case statements fall through */ 181 | { 182 | /* c is reserved for the length */ 183 | case 2 : b+=k[1]; 184 | case 1 : a+=k[0]; 185 | /* case 0: nothing left to add */ 186 | } 187 | mix(a,b,c); 188 | /*-------------------------------------------- report the result */ 189 | return c; 190 | } 191 | 192 | /* 193 | -------------------------------------------------------------------- 194 | This is identical to hash() on little-endian machines (like Intel 195 | x86s or VAXen). It gives nondeterministic results on big-endian 196 | machines. It is faster than hash(), but a little slower than 197 | hash2(), and it requires 198 | -- that all your machines be little-endian 199 | -------------------------------------------------------------------- 200 | */ 201 | 202 | ub4 hash3( k, length, initval) 203 | register ub1 *k; /* the key */ 204 | register ub4 length; /* the length of the key */ 205 | register ub4 initval; /* the previous hash, or an arbitrary value */ 206 | { 207 | register ub4 a,b,c,len; 208 | 209 | /* Set up the internal state */ 210 | len = length; 211 | a = b = 0x9e3779b9; /* the golden ratio; an arbitrary value */ 212 | c = initval; /* the previous hash value */ 213 | 214 | /*---------------------------------------- handle most of the key */ 215 | if (((ub4)k)&3) 216 | { 217 | while (len >= 12) /* unaligned */ 218 | { 219 | a += (k[0] +((ub4)k[1]<<8) +((ub4)k[2]<<16) +((ub4)k[3]<<24)); 220 | b += (k[4] +((ub4)k[5]<<8) +((ub4)k[6]<<16) +((ub4)k[7]<<24)); 221 | c += (k[8] +((ub4)k[9]<<8) +((ub4)k[10]<<16)+((ub4)k[11]<<24)); 222 | mix(a,b,c); 223 | k += 12; len -= 12; 224 | } 225 | } 226 | else 227 | { 228 | while (len >= 12) /* aligned */ 229 | { 230 | a += *(ub4 *)(k+0); 231 | b += *(ub4 *)(k+4); 232 | c += *(ub4 *)(k+8); 233 | mix(a,b,c); 234 | k += 12; len -= 12; 235 | } 236 | } 237 | 238 | /*------------------------------------- handle the last 11 bytes */ 239 | c += length; 240 | switch(len) /* all the case statements fall through */ 241 | { 242 | case 11: c+=((ub4)k[10]<<24); 243 | case 10: c+=((ub4)k[9]<<16); 244 | case 9 : c+=((ub4)k[8]<<8); 245 | /* the first byte of c is reserved for the length */ 246 | case 8 : b+=((ub4)k[7]<<24); 247 | case 7 : b+=((ub4)k[6]<<16); 248 | case 6 : b+=((ub4)k[5]<<8); 249 | case 5 : b+=k[4]; 250 | case 4 : a+=((ub4)k[3]<<24); 251 | case 3 : a+=((ub4)k[2]<<16); 252 | case 2 : a+=((ub4)k[1]<<8); 253 | case 1 : a+=k[0]; 254 | /* case 0: nothing left to add */ 255 | } 256 | mix(a,b,c); 257 | /*-------------------------------------------- report the result */ 258 | return c; 259 | } 260 | 261 | 262 | 263 | #ifdef SELF_TEST 264 | 265 | /* used for timings */ 266 | void driver1() 267 | { 268 | ub4 buf[256]; 269 | ub4 i; 270 | ub4 h=0; 271 | 272 | for (i=0; i<256; ++i) 273 | { 274 | h = hash(buf,i,h); 275 | } 276 | } 277 | 278 | /* check that every input bit changes every output bit half the time */ 279 | #define HASHSTATE 1 280 | #define HASHLEN 1 281 | #define MAXPAIR 80 282 | #define MAXLEN 70 283 | void driver2() 284 | { 285 | ub1 qa[MAXLEN+1], qb[MAXLEN+2], *a = &qa[0], *b = &qb[1]; 286 | ub4 c[HASHSTATE], d[HASHSTATE], i, j=0, k, l, m, z; 287 | ub4 e[HASHSTATE],f[HASHSTATE],g[HASHSTATE],h[HASHSTATE]; 288 | ub4 x[HASHSTATE],y[HASHSTATE]; 289 | ub4 hlen; 290 | 291 | printf("No more than %d trials should ever be needed \n",MAXPAIR/2); 292 | for (hlen=0; hlen < MAXLEN; ++hlen) 293 | { 294 | z=0; 295 | for (i=0; i>(8-j)); 312 | c[0] = hash(a, hlen, m); 313 | b[i] ^= ((k+1)<>(8-j)); 315 | d[0] = hash(b, hlen, m); 316 | /* check every bit is 1, 0, set, and not set at least once */ 317 | for (l=0; lz) z=k; 330 | if (k==MAXPAIR) 331 | { 332 | printf("Some bit didn't change: "); 333 | printf("%.8lx %.8lx %.8lx %.8lx %.8lx %.8lx ", 334 | e[0],f[0],g[0],h[0],x[0],y[0]); 335 | printf("i %ld j %ld m %ld len %ld\n",i,j,m,hlen); 336 | } 337 | if (z==MAXPAIR) goto done; 338 | } 339 | } 340 | } 341 | done: 342 | if (z < MAXPAIR) 343 | { 344 | printf("Mix success %2ld bytes %2ld initvals ",i,m); 345 | printf("required %ld trials\n",z/2); 346 | } 347 | } 348 | printf("\n"); 349 | } 350 | 351 | /* Check for reading beyond the end of the buffer and alignment problems */ 352 | void driver3() 353 | { 354 | ub1 buf[MAXLEN+20], *b; 355 | ub4 len; 356 | ub1 q[] = "This is the time for all good men to come to the aid of their country"; 357 | ub1 qq[] = "xThis is the time for all good men to come to the aid of their country"; 358 | ub1 qqq[] = "xxThis is the time for all good men to come to the aid of their country"; 359 | ub1 qqqq[] = "xxxThis is the time for all good men to come to the aid of their country"; 360 | ub4 h,i,j,ref,x,y; 361 | 362 | printf("Endianness. These should all be the same:\n"); 363 | printf("%.8lx\n", hash(q, sizeof(q)-1, (ub4)0)); 364 | printf("%.8lx\n", hash(qq+1, sizeof(q)-1, (ub4)0)); 365 | printf("%.8lx\n", hash(qqq+2, sizeof(q)-1, (ub4)0)); 366 | printf("%.8lx\n", hash(qqqq+3, sizeof(q)-1, (ub4)0)); 367 | printf("\n"); 368 | for (h=0, b=buf+1; h<8; ++h, ++b) 369 | { 370 | for (i=0; i/dev/null; then 14 | flags+=(-arch "${arch}") 15 | fi 16 | done 17 | 18 | set -x 19 | g++ "${flags[@]}" -o ldid ldid.cpp -I. -x c lookup2.c sha1.c 20 | -------------------------------------------------------------------------------- /sha1.c: -------------------------------------------------------------------------------- 1 | /* 2 | * sha1.c 3 | * 4 | * Description: 5 | * This file implements the Secure Hashing Algorithm 1 as 6 | * defined in FIPS PUB 180-1 published April 17, 1995. 7 | * 8 | * The SHA-1, produces a 160-bit message digest for a given 9 | * data stream. It should take about 2**n steps to find a 10 | * message with the same digest as a given message and 11 | * 2**(n/2) to find any two messages with the same digest, 12 | * when n is the digest size in bits. Therefore, this 13 | * algorithm can serve as a means of providing a 14 | * "fingerprint" for a message. 15 | * 16 | * Portability Issues: 17 | * SHA-1 is defined in terms of 32-bit "words". This code 18 | * uses (included via "sha1.h" to define 32 and 8 19 | * bit unsigned integer types. If your C compiler does not 20 | * support 32 bit unsigned integers, this code is not 21 | * appropriate. 22 | * 23 | * Caveats: 24 | * SHA-1 is designed to work with messages less than 2^64 bits 25 | * long. Although SHA-1 allows a message digest to be generated 26 | * for messages of any number of bits less than 2^64, this 27 | * implementation only works with messages with a length that is 28 | * a multiple of the size of an 8-bit character. 29 | * 30 | */ 31 | 32 | #include "sha1.h" 33 | 34 | /* 35 | * Define the SHA1 circular left shift macro 36 | */ 37 | #define SHA1CircularShift(bits,word) \ 38 | (((word) << (bits)) | ((word) >> (32-(bits)))) 39 | 40 | /* Local Function Prototyptes */ 41 | void SHA1PadMessage(SHA1Context *); 42 | void SHA1ProcessMessageBlock(SHA1Context *); 43 | 44 | /* 45 | * SHA1Reset 46 | * 47 | * Description: 48 | * This function will initialize the SHA1Context in preparation 49 | * for computing a new SHA1 message digest. 50 | * 51 | * Parameters: 52 | * context: [in/out] 53 | * The context to reset. 54 | * 55 | * Returns: 56 | * sha Error Code. 57 | * 58 | */ 59 | int SHA1Reset(SHA1Context *context) 60 | { 61 | if (!context) 62 | { 63 | return shaNull; 64 | } 65 | 66 | context->Length_Low = 0; 67 | context->Length_High = 0; 68 | context->Message_Block_Index = 0; 69 | 70 | context->Intermediate_Hash[0] = 0x67452301; 71 | context->Intermediate_Hash[1] = 0xEFCDAB89; 72 | context->Intermediate_Hash[2] = 0x98BADCFE; 73 | context->Intermediate_Hash[3] = 0x10325476; 74 | context->Intermediate_Hash[4] = 0xC3D2E1F0; 75 | 76 | context->Computed = 0; 77 | context->Corrupted = 0; 78 | 79 | return shaSuccess; 80 | } 81 | 82 | /* 83 | * SHA1Result 84 | * 85 | * Description: 86 | * This function will return the 160-bit message digest into the 87 | * Message_Digest array provided by the caller. 88 | * NOTE: The first octet of hash is stored in the 0th element, 89 | * the last octet of hash in the 19th element. 90 | * 91 | * Parameters: 92 | * context: [in/out] 93 | * The context to use to calculate the SHA-1 hash. 94 | * Message_Digest: [out] 95 | * Where the digest is returned. 96 | * 97 | * Returns: 98 | * sha Error Code. 99 | * 100 | */ 101 | int SHA1Result( SHA1Context *context, 102 | uint8_t Message_Digest[SHA1HashSize]) 103 | { 104 | int i; 105 | 106 | if (!context || !Message_Digest) 107 | { 108 | return shaNull; 109 | } 110 | 111 | if (context->Corrupted) 112 | { 113 | return context->Corrupted; 114 | } 115 | 116 | if (!context->Computed) 117 | { 118 | SHA1PadMessage(context); 119 | for(i=0; i<64; ++i) 120 | { 121 | /* message may be sensitive, clear it out */ 122 | context->Message_Block[i] = 0; 123 | } 124 | context->Length_Low = 0; /* and clear length */ 125 | context->Length_High = 0; 126 | context->Computed = 1; 127 | 128 | } 129 | 130 | for(i = 0; i < SHA1HashSize; ++i) 131 | { 132 | Message_Digest[i] = context->Intermediate_Hash[i>>2] 133 | >> 8 * ( 3 - ( i & 0x03 ) ); 134 | } 135 | 136 | return shaSuccess; 137 | } 138 | 139 | /* 140 | * SHA1Input 141 | * 142 | * Description: 143 | * This function accepts an array of octets as the next portion 144 | * of the message. 145 | * 146 | * Parameters: 147 | * context: [in/out] 148 | * The SHA context to update 149 | * message_array: [in] 150 | * An array of characters representing the next portion of 151 | * the message. 152 | * length: [in] 153 | * The length of the message in message_array 154 | * 155 | * Returns: 156 | * sha Error Code. 157 | * 158 | */ 159 | int SHA1Input( SHA1Context *context, 160 | const uint8_t *message_array, 161 | unsigned length) 162 | { 163 | if (!length) 164 | { 165 | return shaSuccess; 166 | } 167 | 168 | if (!context || !message_array) 169 | { 170 | return shaNull; 171 | } 172 | 173 | if (context->Computed) 174 | { 175 | context->Corrupted = shaStateError; 176 | 177 | return shaStateError; 178 | } 179 | 180 | if (context->Corrupted) 181 | { 182 | return context->Corrupted; 183 | } 184 | while(length-- && !context->Corrupted) 185 | { 186 | context->Message_Block[context->Message_Block_Index++] = 187 | (*message_array & 0xFF); 188 | 189 | context->Length_Low += 8; 190 | if (context->Length_Low == 0) 191 | { 192 | context->Length_High++; 193 | if (context->Length_High == 0) 194 | { 195 | /* Message is too long */ 196 | context->Corrupted = 1; 197 | } 198 | } 199 | 200 | if (context->Message_Block_Index == 64) 201 | { 202 | SHA1ProcessMessageBlock(context); 203 | } 204 | 205 | message_array++; 206 | } 207 | 208 | return shaSuccess; 209 | } 210 | 211 | /* 212 | * SHA1ProcessMessageBlock 213 | * 214 | * Description: 215 | * This function will process the next 512 bits of the message 216 | * stored in the Message_Block array. 217 | * 218 | * Parameters: 219 | * None. 220 | * 221 | * Returns: 222 | * Nothing. 223 | * 224 | * Comments: 225 | 226 | * Many of the variable names in this code, especially the 227 | * single character names, were used because those were the 228 | * names used in the publication. 229 | * 230 | * 231 | */ 232 | void SHA1ProcessMessageBlock(SHA1Context *context) 233 | { 234 | const uint32_t K[] = { /* Constants defined in SHA-1 */ 235 | 0x5A827999, 236 | 0x6ED9EBA1, 237 | 0x8F1BBCDC, 238 | 0xCA62C1D6 239 | }; 240 | int t; /* Loop counter */ 241 | uint32_t temp; /* Temporary word value */ 242 | uint32_t W[80]; /* Word sequence */ 243 | uint32_t A, B, C, D, E; /* Word buffers */ 244 | 245 | /* 246 | * Initialize the first 16 words in the array W 247 | */ 248 | for(t = 0; t < 16; t++) 249 | { 250 | W[t] = context->Message_Block[t * 4] << 24; 251 | W[t] |= context->Message_Block[t * 4 + 1] << 16; 252 | W[t] |= context->Message_Block[t * 4 + 2] << 8; 253 | W[t] |= context->Message_Block[t * 4 + 3]; 254 | } 255 | 256 | for(t = 16; t < 80; t++) 257 | { 258 | W[t] = SHA1CircularShift(1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]); 259 | } 260 | 261 | A = context->Intermediate_Hash[0]; 262 | B = context->Intermediate_Hash[1]; 263 | C = context->Intermediate_Hash[2]; 264 | D = context->Intermediate_Hash[3]; 265 | E = context->Intermediate_Hash[4]; 266 | 267 | for(t = 0; t < 20; t++) 268 | { 269 | temp = SHA1CircularShift(5,A) + 270 | ((B & C) | ((~B) & D)) + E + W[t] + K[0]; 271 | E = D; 272 | D = C; 273 | C = SHA1CircularShift(30,B); 274 | 275 | B = A; 276 | A = temp; 277 | } 278 | 279 | for(t = 20; t < 40; t++) 280 | { 281 | temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[1]; 282 | E = D; 283 | D = C; 284 | C = SHA1CircularShift(30,B); 285 | B = A; 286 | A = temp; 287 | } 288 | 289 | for(t = 40; t < 60; t++) 290 | { 291 | temp = SHA1CircularShift(5,A) + 292 | ((B & C) | (B & D) | (C & D)) + E + W[t] + K[2]; 293 | E = D; 294 | D = C; 295 | C = SHA1CircularShift(30,B); 296 | B = A; 297 | A = temp; 298 | } 299 | 300 | for(t = 60; t < 80; t++) 301 | { 302 | temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[3]; 303 | E = D; 304 | D = C; 305 | C = SHA1CircularShift(30,B); 306 | B = A; 307 | A = temp; 308 | } 309 | 310 | context->Intermediate_Hash[0] += A; 311 | context->Intermediate_Hash[1] += B; 312 | context->Intermediate_Hash[2] += C; 313 | context->Intermediate_Hash[3] += D; 314 | context->Intermediate_Hash[4] += E; 315 | 316 | context->Message_Block_Index = 0; 317 | } 318 | 319 | /* 320 | * SHA1PadMessage 321 | * 322 | 323 | * Description: 324 | * According to the standard, the message must be padded to an even 325 | * 512 bits. The first padding bit must be a '1'. The last 64 326 | * bits represent the length of the original message. All bits in 327 | * between should be 0. This function will pad the message 328 | * according to those rules by filling the Message_Block array 329 | * accordingly. It will also call the ProcessMessageBlock function 330 | * provided appropriately. When it returns, it can be assumed that 331 | * the message digest has been computed. 332 | * 333 | * Parameters: 334 | * context: [in/out] 335 | * The context to pad 336 | * ProcessMessageBlock: [in] 337 | * The appropriate SHA*ProcessMessageBlock function 338 | * Returns: 339 | * Nothing. 340 | * 341 | */ 342 | 343 | void SHA1PadMessage(SHA1Context *context) 344 | { 345 | /* 346 | * Check to see if the current message block is too small to hold 347 | * the initial padding bits and length. If so, we will pad the 348 | * block, process it, and then continue padding into a second 349 | * block. 350 | */ 351 | if (context->Message_Block_Index > 55) 352 | { 353 | context->Message_Block[context->Message_Block_Index++] = 0x80; 354 | while(context->Message_Block_Index < 64) 355 | { 356 | context->Message_Block[context->Message_Block_Index++] = 0; 357 | } 358 | 359 | SHA1ProcessMessageBlock(context); 360 | 361 | while(context->Message_Block_Index < 56) 362 | { 363 | context->Message_Block[context->Message_Block_Index++] = 0; 364 | } 365 | } 366 | else 367 | { 368 | context->Message_Block[context->Message_Block_Index++] = 0x80; 369 | while(context->Message_Block_Index < 56) 370 | { 371 | 372 | context->Message_Block[context->Message_Block_Index++] = 0; 373 | } 374 | } 375 | 376 | /* 377 | * Store the message length as the last 8 octets 378 | */ 379 | context->Message_Block[56] = context->Length_High >> 24; 380 | context->Message_Block[57] = context->Length_High >> 16; 381 | context->Message_Block[58] = context->Length_High >> 8; 382 | context->Message_Block[59] = context->Length_High; 383 | context->Message_Block[60] = context->Length_Low >> 24; 384 | context->Message_Block[61] = context->Length_Low >> 16; 385 | context->Message_Block[62] = context->Length_Low >> 8; 386 | context->Message_Block[63] = context->Length_Low; 387 | 388 | SHA1ProcessMessageBlock(context); 389 | } 390 | -------------------------------------------------------------------------------- /sha1.h: -------------------------------------------------------------------------------- 1 | /* 2 | * sha1.h 3 | * 4 | * Description: 5 | * This is the header file for code which implements the Secure 6 | * Hashing Algorithm 1 as defined in FIPS PUB 180-1 published 7 | * April 17, 1995. 8 | * 9 | * Many of the variable names in this code, especially the 10 | * single character names, were used because those were the names 11 | * used in the publication. 12 | * 13 | * Please read the file sha1.c for more information. 14 | * 15 | */ 16 | 17 | #ifndef _SHA1_H_ 18 | #define _SHA1_H_ 19 | 20 | #include 21 | 22 | #ifndef _SHA_enum_ 23 | #define _SHA_enum_ 24 | enum 25 | { 26 | shaSuccess = 0, 27 | shaNull, /* Null pointer parameter */ 28 | shaInputTooLong, /* input data too long */ 29 | shaStateError /* called Input after Result */ 30 | }; 31 | #endif 32 | #define SHA1HashSize 20 33 | 34 | /* 35 | * This structure will hold context information for the SHA-1 36 | * hashing operation 37 | */ 38 | typedef struct SHA1Context 39 | { 40 | uint32_t Intermediate_Hash[SHA1HashSize/4]; /* Message Digest */ 41 | 42 | uint32_t Length_Low; /* Message length in bits */ 43 | uint32_t Length_High; /* Message length in bits */ 44 | 45 | /* Index into message block array */ 46 | int_least16_t Message_Block_Index; 47 | uint8_t Message_Block[64]; /* 512-bit message blocks */ 48 | 49 | int Computed; /* Is the digest computed? */ 50 | int Corrupted; /* Is the message digest corrupted? */ 51 | } SHA1Context; 52 | 53 | /* 54 | * Function Prototypes 55 | */ 56 | 57 | int SHA1Reset( SHA1Context *); 58 | int SHA1Input( SHA1Context *, 59 | const uint8_t *, 60 | unsigned int); 61 | int SHA1Result( SHA1Context *, 62 | uint8_t Message_Digest[SHA1HashSize]); 63 | 64 | #endif 65 | -------------------------------------------------------------------------------- /version.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo "$(git describe --tags --dirty="+" --match="v*" | sed -e 's@-\([^-]*\)-\([^-]*\)$@+\1.\2@;s@^v@1:@')" 3 | --------------------------------------------------------------------------------