├── .gitignore ├── LICENSE.txt ├── Makefile ├── README.md ├── res ├── audio.wav ├── banner.png ├── cia.rsf └── icon.png └── source ├── am.c ├── am.h ├── main.c ├── parson.c ├── parson.h ├── ticket.c ├── ticket.h ├── titles.c └── titles.h /.gitignore: -------------------------------------------------------------------------------- 1 | *.smdh 2 | *.3dsx 3 | *.cia 4 | *.elf 5 | *.zip 6 | build 7 | res/icon.icn 8 | res/banner.bnr 9 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | #--------------------------------------------------------------------------------- 2 | .SUFFIXES: 3 | #--------------------------------------------------------------------------------- 4 | 5 | ifeq ($(strip $(DEVKITARM)),) 6 | $(error "Please set DEVKITARM in your environment. export DEVKITARM=devkitARM") 7 | endif 8 | 9 | TOPDIR ?= $(CURDIR) 10 | include $(DEVKITARM)/3ds_rules 11 | 12 | #--------------------------------------------------------------------------------- 13 | # TARGET is the name of the output 14 | # BUILD is the directory where object files & intermediate files will be placed 15 | # SOURCES is a list of directories containing source code 16 | # DATA is a list of directories containing data files 17 | # INCLUDES is a list of directories containing header files 18 | # 19 | # NO_SMDH: if set to anything, no SMDH file is generated. 20 | # ROMFS is the directory which contains the RomFS, relative to the Makefile (Optional) 21 | # APP_TITLE is the name of the app stored in the SMDH file (Optional) 22 | # APP_DESCRIPTION is the description of the app stored in the SMDH file (Optional) 23 | # APP_AUTHOR is the author of the app stored in the SMDH file (Optional) 24 | # ICON is the filename of the icon (.png), relative to the project folder. 25 | # If not set, it attempts to use one of the following (in this order): 26 | # - .png 27 | # - icon.png 28 | # - /default_icon.png 29 | #--------------------------------------------------------------------------------- 30 | TARGET := $(notdir $(CURDIR)) 31 | BUILD := build 32 | SOURCES := source 33 | DATA := data 34 | INCLUDES := include 35 | 36 | TITLE := 'Sleepmode Installer' 37 | AUTHOR := 'Reisyukaku' 38 | DESCRIP := 'downloads in your sleep' 39 | 40 | #--------------------------------------------------------------------------------- 41 | # options for code generation 42 | #--------------------------------------------------------------------------------- 43 | ARCH := -march=armv6k -mtune=mpcore -mfloat-abi=hard -mtp=soft 44 | 45 | CFLAGS := -g -Wall -O2 -mword-relocations \ 46 | -fomit-frame-pointer -ffunction-sections \ 47 | $(ARCH) 48 | 49 | CFLAGS += $(INCLUDE) -DARM11 -D_3DS -std=gnu11 50 | 51 | CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions -std=gnu++11 52 | 53 | ASFLAGS := -g $(ARCH) 54 | LDFLAGS = -specs=3dsx.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map) 55 | 56 | LIBS := -lctru -lm 57 | 58 | #--------------------------------------------------------------------------------- 59 | # list of directories containing libraries, this must be the top level containing 60 | # include and lib 61 | #--------------------------------------------------------------------------------- 62 | LIBDIRS := $(CTRULIB) 63 | 64 | 65 | #--------------------------------------------------------------------------------- 66 | # no real need to edit anything past this point unless you need to add additional 67 | # rules for different file extensions 68 | #--------------------------------------------------------------------------------- 69 | ifneq ($(BUILD),$(notdir $(CURDIR))) 70 | #--------------------------------------------------------------------------------- 71 | 72 | export OUTPUT := $(CURDIR)/$(TARGET) 73 | export TOPDIR := $(CURDIR) 74 | 75 | export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \ 76 | $(foreach dir,$(DATA),$(CURDIR)/$(dir)) 77 | 78 | export DEPSDIR := $(CURDIR)/$(BUILD) 79 | 80 | CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c))) 81 | CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp))) 82 | SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s))) 83 | PICAFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.v.pica))) 84 | SHLISTFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.shlist))) 85 | BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*))) 86 | 87 | #--------------------------------------------------------------------------------- 88 | # use CXX for linking C++ projects, CC for standard C 89 | #--------------------------------------------------------------------------------- 90 | ifeq ($(strip $(CPPFILES)),) 91 | #--------------------------------------------------------------------------------- 92 | export LD := $(CC) 93 | #--------------------------------------------------------------------------------- 94 | else 95 | #--------------------------------------------------------------------------------- 96 | export LD := $(CXX) 97 | #--------------------------------------------------------------------------------- 98 | endif 99 | #--------------------------------------------------------------------------------- 100 | 101 | export OFILES := $(addsuffix .o,$(BINFILES)) \ 102 | $(PICAFILES:.v.pica=.shbin.o) $(SHLISTFILES:.shlist=.shbin.o) \ 103 | $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o) 104 | 105 | export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \ 106 | $(foreach dir,$(LIBDIRS),-I$(dir)/include) \ 107 | -I$(CURDIR)/$(BUILD) 108 | 109 | export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib) 110 | 111 | ifeq ($(strip $(ICON)),) 112 | icons := $(wildcard *.png) 113 | ifneq (,$(findstring $(TARGET).png,$(icons))) 114 | export APP_ICON := $(TOPDIR)/$(TARGET).png 115 | else 116 | ifneq (,$(findstring icon.png,$(icons))) 117 | export APP_ICON := $(TOPDIR)/icon.png 118 | endif 119 | endif 120 | else 121 | export APP_ICON := $(TOPDIR)/$(ICON) 122 | endif 123 | 124 | ifeq ($(strip $(NO_SMDH)),) 125 | export _3DSXFLAGS += --smdh=$(CURDIR)/$(TARGET).smdh 126 | endif 127 | 128 | ifneq ($(ROMFS),) 129 | export _3DSXFLAGS += --romfs=$(CURDIR)/$(ROMFS) 130 | endif 131 | 132 | .PHONY: $(BUILD) clean all 133 | 134 | #--------------------------------------------------------------------------------- 135 | all: $(BUILD) cia 136 | 137 | $(BUILD): 138 | @[ -d $@ ] || mkdir -p $@ 139 | @$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile 140 | 141 | #--------------------------------------------------------------------------------- 142 | clean: 143 | @echo clean ... 144 | @rm -fr $(BUILD) $(TARGET).3dsx $(OUTPUT).smdh $(TARGET).elf $(TARGET).cia *.pem *.p12 res/*.icn res/*.bnr 145 | 146 | #--------------------------------------------------------------------------------- 147 | res/banner.bnr: res/banner.png res/audio.wav 148 | bannertool makebanner -o res/banner.bnr -i res/banner.png -a res/audio.wav 149 | 150 | res/icon.icn: res/icon.png 151 | bannertool makesmdh -o res/icon.icn -s $(TITLE) -l $(DESCRIP) \ 152 | -p $(AUTHOR) -i res/icon.png -f visible,nosavebackups 153 | 154 | cia: $(OUTPUT).elf res/banner.bnr res/icon.icn 155 | makerom -f cia -o $(OUTPUT).cia -elf $(OUTPUT).elf -rsf res/cia.rsf \ 156 | -banner res/banner.bnr -icon res/icon.icn -exefslogo -target t 157 | 158 | #--------------------------------------------------------------------------------- 159 | else 160 | 161 | DEPENDS := $(OFILES:.o=.d) 162 | 163 | #--------------------------------------------------------------------------------- 164 | # main targets 165 | #--------------------------------------------------------------------------------- 166 | ifeq ($(strip $(NO_SMDH)),) 167 | $(OUTPUT).3dsx : $(OUTPUT).elf $(OUTPUT).smdh 168 | else 169 | $(OUTPUT).3dsx : $(OUTPUT).elf 170 | endif 171 | 172 | $(OUTPUT).elf : $(OFILES) 173 | 174 | #--------------------------------------------------------------------------------- 175 | # you need a rule like this for each extension you use as binary data 176 | #--------------------------------------------------------------------------------- 177 | %.bin.o : %.bin 178 | #--------------------------------------------------------------------------------- 179 | @echo $(notdir $<) 180 | @$(bin2o) 181 | 182 | #--------------------------------------------------------------------------------- 183 | # rules for assembling GPU shaders 184 | #--------------------------------------------------------------------------------- 185 | define shader-as 186 | $(eval CURBIN := $(patsubst %.shbin.o,%.shbin,$(notdir $@))) 187 | picasso -o $(CURBIN) $1 188 | bin2s $(CURBIN) | $(AS) -o $@ 189 | echo "extern const u8" `(echo $(CURBIN) | sed -e 's/^\([0-9]\)/_\1/' | tr . _)`"_end[];" > `(echo $(CURBIN) | tr . _)`.h 190 | echo "extern const u8" `(echo $(CURBIN) | sed -e 's/^\([0-9]\)/_\1/' | tr . _)`"[];" >> `(echo $(CURBIN) | tr . _)`.h 191 | echo "extern const u32" `(echo $(CURBIN) | sed -e 's/^\([0-9]\)/_\1/' | tr . _)`_size";" >> `(echo $(CURBIN) | tr . _)`.h 192 | endef 193 | 194 | %.shbin.o : %.v.pica %.g.pica 195 | @echo $(notdir $^) 196 | @$(call shader-as,$^) 197 | 198 | %.shbin.o : %.v.pica 199 | @echo $(notdir $<) 200 | @$(call shader-as,$<) 201 | 202 | %.shbin.o : %.shlist 203 | @echo $(notdir $<) 204 | @$(call shader-as,$(foreach file,$(shell cat $<),$(dir $<)/$(file))) 205 | 206 | -include $(DEPENDS) 207 | 208 | #--------------------------------------------------------------------------------------- 209 | endif 210 | #--------------------------------------------------------------------------------------- 211 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Sleepmode Installer 2 | 3 | This adds titles for sleep mode download with nim. 4 | 5 | Enter titleID and watch it download. 6 | 7 | ## Compilation 8 | 9 | This requires commit e06c8e542feb7624d2e7ca2af0d41bcf5637d887 of ctrulib. 10 | 11 | To compile, run `make`. 12 | 13 | ## Installation 14 | 15 | Install the CIA build and run it. 16 | 17 | -------------------------------------------------------------------------------- /res/audio.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Reisyukaku/SleepmodeInstaller/88832ae99ef4e6e90a2cd95b76365763cac598ba/res/audio.wav -------------------------------------------------------------------------------- /res/banner.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Reisyukaku/SleepmodeInstaller/88832ae99ef4e6e90a2cd95b76365763cac598ba/res/banner.png -------------------------------------------------------------------------------- /res/cia.rsf: -------------------------------------------------------------------------------- 1 | BasicInfo: 2 | Title : SleepmodeInstaller 3 | ProductCode : sleepinstall 4 | Logo : Homebrew # Nintendo / Licensed / Distributed / iQue / iQueForSystem / Homebrew 5 | 6 | #RomFs: 7 | # Specifies the root path of the read only file system to include in the ROM. 8 | # RootPath : $(APP_ROMFS) 9 | 10 | TitleInfo: 11 | Category : Application 12 | UniqueId : 0xff1fb 13 | 14 | Option: 15 | UseOnSD : true # true if App is to be installed to SD 16 | FreeProductCode : true # Removes limitations on ProductCode 17 | MediaFootPadding : false # If true CCI files are created with padding 18 | EnableCrypt : false # Enables encryption for NCCH and CIA 19 | EnableCompress : true # Compresses where applicable (currently only exefs:/.code) 20 | 21 | AccessControlInfo: 22 | CoreVersion : 2 23 | 24 | # Exheader Format Version 25 | DescVersion : 2 26 | 27 | # Minimum Required Kernel Version (below is for 4.5.0) 28 | ReleaseKernelMajor : "02" 29 | ReleaseKernelMinor : "33" 30 | 31 | # ExtData 32 | UseExtSaveData : false # enables ExtData 33 | #ExtSaveDataId : 0x300 # only set this when the ID is different to the UniqueId 34 | 35 | # FS:USER Archive Access Permissions 36 | # Uncomment as required 37 | FileSystemAccess: 38 | - CategorySystemApplication 39 | #- CategoryHardwareCheck 40 | - CategoryFileSystemTool 41 | #- Debug 42 | #- TwlCardBackup 43 | #- TwlNandData 44 | #- Boss 45 | - DirectSdmc 46 | - Core 47 | #- CtrNandRo 48 | #- CtrNandRw 49 | #- CtrNandRoWrite 50 | #- CategorySystemSettings 51 | #- CardBoard 52 | #- ExportImportIvs 53 | #- DirectSdmcWrite 54 | #- SwitchCleanup 55 | #- SaveDataMove 56 | #- Shop 57 | #- Shell 58 | #- CategoryHomeMenu 59 | 60 | # Process Settings 61 | MemoryType : Application # Application/System/Base 62 | SystemMode : 64MB # 64MB(Default)/96MB/80MB/72MB/32MB 63 | IdealProcessor : 0 64 | AffinityMask : 1 65 | Priority : 16 66 | MaxCpu : 0 # Let system decide 67 | HandleTableSize : 0x200 68 | DisableDebug : false 69 | EnableForceDebug : false 70 | CanWriteSharedPage : true 71 | CanUsePrivilegedPriority : false 72 | CanUseNonAlphabetAndNumber : true 73 | PermitMainFunctionArgument : true 74 | CanShareDeviceMemory : true 75 | RunnableOnSleep : false 76 | SpecialMemoryArrange : true 77 | 78 | # New3DS Exclusive Process Settings 79 | SystemModeExt : Legacy # Legacy(Default)/124MB/178MB Legacy:Use Old3DS SystemMode 80 | CpuSpeed : 804MHz # 268MHz(Default)/804MHz 81 | EnableL2Cache : true # false(default)/true 82 | CanAccessCore2 : true 83 | 84 | # Virtual Address Mappings 85 | IORegisterMapping: 86 | - 1ff00000-1ff7ffff # DSP memory 87 | MemoryMapping: 88 | - 1f000000-1f5fffff:r # VRAM 89 | 90 | # Accessible SVCs, : 91 | SystemCallAccess: 92 | ArbitrateAddress: 34 93 | Break: 60 94 | CancelTimer: 28 95 | ClearEvent: 25 96 | ClearTimer: 29 97 | CloseHandle: 35 98 | ConnectToPort: 45 99 | ControlMemory: 1 100 | CreateAddressArbiter: 33 101 | CreateEvent: 23 102 | CreateMemoryBlock: 30 103 | CreateMutex: 19 104 | CreateSemaphore: 21 105 | CreateThread: 8 106 | CreateTimer: 26 107 | DuplicateHandle: 39 108 | ExitProcess: 3 109 | ExitThread: 9 110 | GetCurrentProcessorNumber: 17 111 | GetHandleInfo: 41 112 | GetProcessId: 53 113 | GetProcessIdOfThread: 54 114 | GetProcessIdealProcessor: 6 115 | GetProcessInfo: 43 116 | GetResourceLimit: 56 117 | GetResourceLimitCurrentValues: 58 118 | GetResourceLimitLimitValues: 57 119 | GetSystemInfo: 42 120 | GetSystemTick: 40 121 | GetThreadContext: 59 122 | GetThreadId: 55 123 | GetThreadIdealProcessor: 15 124 | GetThreadInfo: 44 125 | GetThreadPriority: 11 126 | MapMemoryBlock: 31 127 | OutputDebugString: 61 128 | QueryMemory: 2 129 | ReleaseMutex: 20 130 | ReleaseSemaphore: 22 131 | SendSyncRequest1: 46 132 | SendSyncRequest2: 47 133 | SendSyncRequest3: 48 134 | SendSyncRequest4: 49 135 | SendSyncRequest: 50 136 | SetThreadPriority: 12 137 | SetTimer: 27 138 | SignalEvent: 24 139 | SleepThread: 10 140 | UnmapMemoryBlock: 32 141 | WaitSynchronization1: 36 142 | WaitSynchronizationN: 37 143 | Backdoor: 123 144 | 145 | # Service List 146 | # Maximum 34 services (32 if firmware is prior to 9.3.0) 147 | ServiceAccessControl: 148 | - cfg:u 149 | - fs:USER 150 | - gsp::Gpu 151 | - hid:USER 152 | - ndm:u 153 | - pxi:dev 154 | - APT:U 155 | - ac:u 156 | - act:u 157 | - am:net 158 | - boss:U 159 | - cam:u 160 | - cecd:u 161 | - csnd:SND 162 | - frd:u 163 | - http:C 164 | - ir:USER 165 | - ir:u 166 | - ir:rst 167 | - ldr:ro 168 | - mic:u 169 | - news:u 170 | - nfc:u 171 | - nim:s 172 | - nim:u 173 | - nwm::UDS 174 | - ptm:u 175 | - qtm:u 176 | - ps:ps 177 | - y2r:u 178 | 179 | 180 | SystemControlInfo: 181 | SaveDataSize: $(APP_SAVE_SIZE) 182 | RemasterVersion: 0 183 | StackSize: 0x4000 184 | 185 | # Modules that run services listed above should be included below 186 | # Maximum 48 dependencies 187 | # If a module is listed that isn't present on the 3DS, the title will get stuck at the logo (3ds waves) 188 | # So act, nfc and qtm are commented for 4.x support. Uncomment if you need these. 189 | # : 190 | Dependency: 191 | ac: 0x0004013000002402 192 | #act: 0x0004013000003802 193 | am: 0x0004013000001502 194 | boss: 0x0004013000003402 195 | camera: 0x0004013000001602 196 | cecd: 0x0004013000002602 197 | cfg: 0x0004013000001702 198 | codec: 0x0004013000001802 199 | csnd: 0x0004013000002702 200 | dlp: 0x0004013000002802 201 | dsp: 0x0004013000001a02 202 | friends: 0x0004013000003202 203 | gpio: 0x0004013000001b02 204 | gsp: 0x0004013000001c02 205 | hid: 0x0004013000001d02 206 | http: 0x0004013000002902 207 | i2c: 0x0004013000001e02 208 | ir: 0x0004013000003302 209 | mcu: 0x0004013000001f02 210 | mic: 0x0004013000002002 211 | ndm: 0x0004013000002b02 212 | news: 0x0004013000003502 213 | #nfc: 0x0004013000004002 214 | nim: 0x0004013000002c02 215 | nwm: 0x0004013000002d02 216 | pdn: 0x0004013000002102 217 | ps: 0x0004013000003102 218 | ptm: 0x0004013000002202 219 | #qtm: 0x0004013020004202 220 | ro: 0x0004013000003702 221 | socket: 0x0004013000002e02 222 | spi: 0x0004013000002302 223 | ssl: 0x0004013000002f02 224 | -------------------------------------------------------------------------------- /res/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Reisyukaku/SleepmodeInstaller/88832ae99ef4e6e90a2cd95b76365763cac598ba/res/icon.png -------------------------------------------------------------------------------- /source/am.c: -------------------------------------------------------------------------------- 1 | /* 2 | * am.c 3 | * by Reisyukaku 4 | */ 5 | 6 | #include <3ds.h> 7 | #include "am.h" 8 | 9 | Result AM_GetNumTicketsOfProgram(u64 titleId, u32 *ntickets){ 10 | Result ret = 0; 11 | u32 *cmdbuf = getThreadCommandBuffer(); 12 | 13 | cmdbuf[0] = IPC_MakeHeader(0x81f, 2, 0); // 0x81f0080 14 | cmdbuf[1] = (u32)(titleId & 0xFFFFFFFF); 15 | cmdbuf[2] = (u32)((titleId >> 32) & 0xFFFFFFFF); 16 | 17 | if(R_FAILED(ret = svcSendSyncRequest(*(amGetSessionHandle())))) return ret; 18 | if(R_FAILED(ret = (Result)cmdbuf[1])) return ret; 19 | 20 | if(ntickets) *ntickets = cmdbuf[2]; 21 | 22 | return ret; 23 | } 24 | -------------------------------------------------------------------------------- /source/am.h: -------------------------------------------------------------------------------- 1 | /* 2 | * am.h 3 | * by Reisyukaku 4 | */ 5 | 6 | #ifndef INC_AM 7 | #define INC_AM 8 | 9 | Result AM_GetNumTicketsOfProgram(u64 titleId, u32 *ntickets); 10 | 11 | #endif 12 | -------------------------------------------------------------------------------- /source/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * nimtest.c 3 | * by Reisyukaku 4 | * 5 | * NIM test application for downloading titles in sleep mode. 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #include <3ds.h> 15 | #include "am.h" 16 | #include "ticket.h" 17 | #include "titles.h" 18 | 19 | static Result installTicket(u64 tid, unsigned char *encTitleKey){ 20 | unsigned char *cetk = malloc(sizeof(Cetk)); 21 | unsigned char dummySig[0x13C] = {0}; //doesnt matter 22 | unsigned char tikID[8] = {0}; //doesnt matter 23 | unsigned char eccPubKey[0x3C] = {0}; //doesnt matter 24 | BuildCetk(cetk, dummySig, tid, tikID, encTitleKey, eccPubKey, 0); 25 | 26 | Handle tik; 27 | Result res; 28 | u32 wb; 29 | res = AM_InstallTicketBegin(&tik); 30 | FSFILE_Write(tik, &wb, 0, cetk, sizeof(Cetk), 0); 31 | res |= AM_InstallTicketFinish(tik); 32 | 33 | return res; 34 | } 35 | 36 | static Result startInstall(u64 tid){ 37 | Result res; 38 | NIM_TitleConfig tc; 39 | u16 version; 40 | 41 | res = getTitleVersion(tid, &version); 42 | printf("Getting TMD version: %s (%lx)\n", R_FAILED(res) ? "\x1b[31mfailure\x1b[0m" : "\x1b[32msuccess\x1b[0m", res); 43 | 44 | NIMS_MakeTitleConfig(&tc, tid, version, 0, MEDIATYPE_SD); 45 | res = NIMS_RegisterTask(&tc, "(game name, read from samurai if available)", "(publisher)"); 46 | printf("Registering task: %s (%lx)\n", R_FAILED(res) ? "\x1b[31mfailure\x1b[0m" : "\x1b[32msuccess\x1b[0m", res); 47 | return res; 48 | } 49 | 50 | static SwkbdCallbackResult getTitleIDFromUser_cb(void *unused, const char **ppMessage, const char *text, size_t textlen){ 51 | static char errbuf[SWKBD_MAX_CALLBACK_MSG_LEN]; 52 | u64 tid; 53 | char *ep; 54 | 55 | tid = strtoull(text, &ep, 16); 56 | 57 | if(*ep != '\0' || errno != 0){ 58 | snprintf(errbuf, sizeof(errbuf), "Invalid title ID %s.", text); 59 | *ppMessage = errbuf; 60 | return SWKBD_CALLBACK_CONTINUE; 61 | } 62 | 63 | //tid must start with 0004 64 | if((tid & 0xFFFF000000000000ULL) >> 48 != 4){ 65 | snprintf(errbuf, sizeof(errbuf), "Invalid platform (TID did not start with 0004)."); 66 | *ppMessage = errbuf; 67 | return SWKBD_CALLBACK_CONTINUE; 68 | } 69 | 70 | //must be mediatype_sd: !(tidhigh & 0x10 [system bit]) 71 | if((tid >> 32) & 0x10){ 72 | snprintf(errbuf, sizeof(errbuf), "You cannot download system titles."); 73 | *ppMessage = errbuf; 74 | return SWKBD_CALLBACK_CONTINUE; 75 | } 76 | 77 | return SWKBD_CALLBACK_OK; 78 | } 79 | 80 | static int getTitleIDFromUser(u64 *tid){ 81 | static SwkbdState swkbd;//rip stack 82 | char tidbuf[17]; 83 | SwkbdButton button; 84 | 85 | swkbdInit(&swkbd, SWKBD_TYPE_QWERTY, 2, 16); 86 | swkbdSetValidation(&swkbd, SWKBD_FIXEDLEN, SWKBD_FILTER_CALLBACK, 16); 87 | swkbdSetFeatures(&swkbd, SWKBD_DARKEN_TOP_SCREEN|SWKBD_FIXED_WIDTH); 88 | swkbdSetHintText(&swkbd, "000400000fffff00"); 89 | swkbdSetInitialText(&swkbd, "000400000fffff00"); 90 | swkbdSetFilterCallback(&swkbd, getTitleIDFromUser_cb, NULL); 91 | //Defaults are: Left button is "Cancel", right button is "OK" 92 | 93 | button = swkbdInputText(&swkbd, tidbuf, sizeof(tidbuf)); 94 | 95 | if(button == SWKBD_BUTTON_NONE){ 96 | switch(swkbdGetResult(&swkbd)){ 97 | case SWKBD_INVALID_INPUT: 98 | printf("SWKBD LIBRARY USAGE ERROR!\n"); 99 | return -1; 100 | case SWKBD_OUTOFMEM: 101 | printf("SWKBD OOM!\n"); 102 | return -1; 103 | case SWKBD_POWERPRESSED: 104 | printf("Power button pressed; aborting.\n"); 105 | return -1; 106 | case SWKBD_BANNED_INPUT: 107 | printf("Invalid input; aborting.\n"); 108 | return -1; 109 | default: 110 | printf("UNEXPECTED SWKBD FAILURE!\n"); 111 | return -1; 112 | } 113 | }else if(button == SWKBD_BUTTON_LEFT){ 114 | //Cancel 115 | printf("Cancelled by user.\n"); 116 | return 1; 117 | } 118 | 119 | //Filter callback ensures we have valid input 120 | *tid = strtoull(tidbuf, NULL, 16); 121 | 122 | return 0; 123 | } 124 | 125 | static bool hasTicket(u64 tid) 126 | { 127 | u32 ntickets; 128 | Result res; 129 | 130 | if(R_FAILED(res = AM_GetNumTicketsOfProgram(tid, &ntickets))){ 131 | if(res == (Result)0xD8A083FAU) return false; //Title not found 132 | 133 | ERRF_ThrowResultWithMessage(res, "AM_GetNumTicketsOfProgram failed"); 134 | return false;//never reached 135 | } 136 | 137 | return (ntickets > 0); 138 | } 139 | 140 | void runInstaller(void){ 141 | Result res; 142 | static u8 nimBuf[0x20000]; 143 | u64 tid; 144 | 145 | printf( 146 | "sleep mode installer\n" 147 | "--------------------\n" 148 | ); 149 | 150 | //Get TID from user 151 | if(getTitleIDFromUser(&tid) == 0){ 152 | //Obtain ticket if needed 153 | if(!hasTicket(tid)){ 154 | unsigned char key[0x10]; 155 | printf("Ticket not found!\nTrying to download title key for %016llx\n", tid); 156 | 157 | res = getTitleKey(tid, key); 158 | printf("Obtaining titlekey: %s (%lx)\n", R_FAILED(res) ? "\x1b[31mfailure\x1b[0m" : "\x1b[32msuccess\x1b[0m", res); 159 | if(R_FAILED(res)) return; 160 | 161 | res = installTicket(tid, key); 162 | printf("Installing generated ticket: %s (%lx)\n", R_FAILED(res) ? "\x1b[31mfailure\x1b[0m" : "\x1b[32msuccess\x1b[0m", res); 163 | if(R_FAILED(res)) return; 164 | }else{ 165 | printf("Ticket found!\n"); 166 | } 167 | 168 | //Start install 169 | if(R_SUCCEEDED(res = nimsInit(nimBuf, sizeof(nimBuf)))){ 170 | switch(startInstall(tid)){ 171 | case 0xd8808040U: 172 | printf("\x1b[31mError: missing ticket\x1b[0m"); 173 | break; 174 | case 0xc820d005U: 175 | printf("\x1b[31mError: title already installed\x1b[0m"); 176 | break; 177 | case 0xc920d008U: 178 | printf("\x1b[31mError: TMD not found (wrong version?)\x1b[0m"); 179 | break; 180 | default: 181 | break; 182 | } 183 | }else{ 184 | printf("Unable to access nim:s: %08lx!\n", res); 185 | } 186 | } 187 | } 188 | 189 | int main(void){ 190 | //Init 191 | amInit(); 192 | httpcInit(0); 193 | gfxInitDefault(); 194 | consoleInit(GFX_TOP, NULL); 195 | 196 | //Run main routine 197 | runInstaller(); 198 | 199 | //Loop 200 | printf("Press START to exit.\n"); 201 | while(aptMainLoop()){ 202 | gspWaitForVBlank(); 203 | hidScanInput(); 204 | 205 | if(hidKeysDown() & KEY_START) break; 206 | 207 | gfxFlushBuffers(); 208 | gfxSwapBuffers(); 209 | } 210 | 211 | //Exit 212 | httpcExit(); 213 | amExit(); 214 | gfxExit(); 215 | nimsExit(); 216 | return 0; 217 | } 218 | -------------------------------------------------------------------------------- /source/parson.c: -------------------------------------------------------------------------------- 1 | /* 2 | Parson ( http://kgabis.github.com/parson/ ) 3 | Copyright (c) 2012 - 2016 Krzysztof Gabis 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | */ 23 | #ifdef _MSC_VER 24 | #ifndef _CRT_SECURE_NO_WARNINGS 25 | #define _CRT_SECURE_NO_WARNINGS 26 | #endif /* _CRT_SECURE_NO_WARNINGS */ 27 | #endif /* _MSC_VER */ 28 | 29 | #include "parson.h" 30 | 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | 38 | #define STARTING_CAPACITY 15 39 | #define ARRAY_MAX_CAPACITY 122880 /* 15*(2^13) */ 40 | #define OBJECT_MAX_CAPACITY 960 /* 15*(2^6) */ 41 | #define MAX_NESTING 19 42 | #define DOUBLE_SERIALIZATION_FORMAT "%f" 43 | 44 | #define SIZEOF_TOKEN(a) (sizeof(a) - 1) 45 | #define SKIP_CHAR(str) ((*str)++) 46 | #define SKIP_WHITESPACES(str) while (isspace(**str)) { SKIP_CHAR(str); } 47 | #define MAX(a, b) ((a) > (b) ? (a) : (b)) 48 | 49 | #undef malloc 50 | #undef free 51 | 52 | static JSON_Malloc_Function parson_malloc = malloc; 53 | static JSON_Free_Function parson_free = free; 54 | 55 | #define IS_CONT(b) (((unsigned char)(b) & 0xC0) == 0x80) /* is utf-8 continuation byte */ 56 | 57 | /* Type definitions */ 58 | typedef union json_value_value { 59 | char *string; 60 | double number; 61 | JSON_Object *object; 62 | JSON_Array *array; 63 | int boolean; 64 | int null; 65 | } JSON_Value_Value; 66 | 67 | struct json_value_t { 68 | JSON_Value *parent; 69 | JSON_Value_Type type; 70 | JSON_Value_Value value; 71 | }; 72 | 73 | struct json_object_t { 74 | JSON_Value *wrapping_value; 75 | char **names; 76 | JSON_Value **values; 77 | size_t count; 78 | size_t capacity; 79 | }; 80 | 81 | struct json_array_t { 82 | JSON_Value *wrapping_value; 83 | JSON_Value **items; 84 | size_t count; 85 | size_t capacity; 86 | }; 87 | 88 | /* Various */ 89 | static char * read_file(const char *filename); 90 | static void remove_comments(char *string, const char *start_token, const char *end_token); 91 | static char * parson_strndup(const char *string, size_t n); 92 | static char * parson_strdup(const char *string); 93 | static int is_utf16_hex(const unsigned char *string); 94 | static int num_bytes_in_utf8_sequence(unsigned char c); 95 | static int verify_utf8_sequence(const unsigned char *string, int *len); 96 | static int is_valid_utf8(const char *string, size_t string_len); 97 | static int is_decimal(const char *string, size_t length); 98 | 99 | /* JSON Object */ 100 | static JSON_Object * json_object_init(JSON_Value *wrapping_value); 101 | static JSON_Status json_object_add(JSON_Object *object, const char *name, JSON_Value *value); 102 | static JSON_Status json_object_resize(JSON_Object *object, size_t new_capacity); 103 | static JSON_Value * json_object_nget_value(const JSON_Object *object, const char *name, size_t n); 104 | static void json_object_free(JSON_Object *object); 105 | 106 | /* JSON Array */ 107 | static JSON_Array * json_array_init(JSON_Value *wrapping_value); 108 | static JSON_Status json_array_add(JSON_Array *array, JSON_Value *value); 109 | static JSON_Status json_array_resize(JSON_Array *array, size_t new_capacity); 110 | static void json_array_free(JSON_Array *array); 111 | 112 | /* JSON Value */ 113 | static JSON_Value * json_value_init_string_no_copy(char *string); 114 | 115 | /* Parser */ 116 | static JSON_Status skip_quotes(const char **string); 117 | static int parse_utf_16(const char **unprocessed, char **processed); 118 | static char * process_string(const char *input, size_t len); 119 | static char * get_quoted_string(const char **string); 120 | static JSON_Value * parse_object_value(const char **string, size_t nesting); 121 | static JSON_Value * parse_array_value(const char **string, size_t nesting); 122 | static JSON_Value * parse_string_value(const char **string); 123 | static JSON_Value * parse_boolean_value(const char **string); 124 | static JSON_Value * parse_number_value(const char **string); 125 | static JSON_Value * parse_null_value(const char **string); 126 | static JSON_Value * parse_value(const char **string, size_t nesting); 127 | 128 | /* Serialization */ 129 | static int json_serialize_to_buffer_r(const JSON_Value *value, char *buf, int level, int is_pretty, char *num_buf); 130 | static int json_serialize_string(const char *string, char *buf); 131 | static int append_indent(char *buf, int level); 132 | static int append_string(char *buf, const char *string); 133 | 134 | /* Various */ 135 | static char * parson_strndup(const char *string, size_t n) { 136 | char *output_string = (char*)parson_malloc(n + 1); 137 | if (!output_string) { 138 | return NULL; 139 | } 140 | output_string[n] = '\0'; 141 | strncpy(output_string, string, n); 142 | return output_string; 143 | } 144 | 145 | static char * parson_strdup(const char *string) { 146 | return parson_strndup(string, strlen(string)); 147 | } 148 | 149 | static int is_utf16_hex(const unsigned char *s) { 150 | return isxdigit(s[0]) && isxdigit(s[1]) && isxdigit(s[2]) && isxdigit(s[3]); 151 | } 152 | 153 | static int num_bytes_in_utf8_sequence(unsigned char c) { 154 | if (c == 0xC0 || c == 0xC1 || c > 0xF4 || IS_CONT(c)) { 155 | return 0; 156 | } else if ((c & 0x80) == 0) { /* 0xxxxxxx */ 157 | return 1; 158 | } else if ((c & 0xE0) == 0xC0) { /* 110xxxxx */ 159 | return 2; 160 | } else if ((c & 0xF0) == 0xE0) { /* 1110xxxx */ 161 | return 3; 162 | } else if ((c & 0xF8) == 0xF0) { /* 11110xxx */ 163 | return 4; 164 | } 165 | return 0; /* won't happen */ 166 | } 167 | 168 | static int verify_utf8_sequence(const unsigned char *string, int *len) { 169 | unsigned int cp = 0; 170 | *len = num_bytes_in_utf8_sequence(string[0]); 171 | 172 | if (*len == 1) { 173 | cp = string[0]; 174 | } else if (*len == 2 && IS_CONT(string[1])) { 175 | cp = string[0] & 0x1F; 176 | cp = (cp << 6) | (string[1] & 0x3F); 177 | } else if (*len == 3 && IS_CONT(string[1]) && IS_CONT(string[2])) { 178 | cp = ((unsigned char)string[0]) & 0xF; 179 | cp = (cp << 6) | (string[1] & 0x3F); 180 | cp = (cp << 6) | (string[2] & 0x3F); 181 | } else if (*len == 4 && IS_CONT(string[1]) && IS_CONT(string[2]) && IS_CONT(string[3])) { 182 | cp = string[0] & 0x7; 183 | cp = (cp << 6) | (string[1] & 0x3F); 184 | cp = (cp << 6) | (string[2] & 0x3F); 185 | cp = (cp << 6) | (string[3] & 0x3F); 186 | } else { 187 | return 0; 188 | } 189 | 190 | /* overlong encodings */ 191 | if ((cp < 0x80 && *len > 1) || 192 | (cp < 0x800 && *len > 2) || 193 | (cp < 0x10000 && *len > 3)) { 194 | return 0; 195 | } 196 | 197 | /* invalid unicode */ 198 | if (cp > 0x10FFFF) { 199 | return 0; 200 | } 201 | 202 | /* surrogate halves */ 203 | if (cp >= 0xD800 && cp <= 0xDFFF) { 204 | return 0; 205 | } 206 | 207 | return 1; 208 | } 209 | 210 | static int is_valid_utf8(const char *string, size_t string_len) { 211 | int len = 0; 212 | const char *string_end = string + string_len; 213 | while (string < string_end) { 214 | if (!verify_utf8_sequence((const unsigned char*)string, &len)) { 215 | return 0; 216 | } 217 | string += len; 218 | } 219 | return 1; 220 | } 221 | 222 | static int is_decimal(const char *string, size_t length) { 223 | if (length > 1 && string[0] == '0' && string[1] != '.') { 224 | return 0; 225 | } 226 | if (length > 2 && !strncmp(string, "-0", 2) && string[2] != '.') { 227 | return 0; 228 | } 229 | while (length--) { 230 | if (strchr("xX", string[length])) { 231 | return 0; 232 | } 233 | } 234 | return 1; 235 | } 236 | 237 | static char * read_file(const char * filename) { 238 | FILE *fp = fopen(filename, "r"); 239 | size_t file_size; 240 | long pos; 241 | char *file_contents; 242 | if (!fp) { 243 | return NULL; 244 | } 245 | fseek(fp, 0L, SEEK_END); 246 | pos = ftell(fp); 247 | if (pos < 0) { 248 | fclose(fp); 249 | return NULL; 250 | } 251 | file_size = pos; 252 | rewind(fp); 253 | file_contents = (char*)parson_malloc(sizeof(char) * (file_size + 1)); 254 | if (!file_contents) { 255 | fclose(fp); 256 | return NULL; 257 | } 258 | if (fread(file_contents, file_size, 1, fp) < 1) { 259 | if (ferror(fp)) { 260 | fclose(fp); 261 | parson_free(file_contents); 262 | return NULL; 263 | } 264 | } 265 | fclose(fp); 266 | file_contents[file_size] = '\0'; 267 | return file_contents; 268 | } 269 | 270 | static void remove_comments(char *string, const char *start_token, const char *end_token) { 271 | int in_string = 0, escaped = 0; 272 | size_t i; 273 | char *ptr = NULL, current_char; 274 | size_t start_token_len = strlen(start_token); 275 | size_t end_token_len = strlen(end_token); 276 | if (start_token_len == 0 || end_token_len == 0) { 277 | return; 278 | } 279 | while ((current_char = *string) != '\0') { 280 | if (current_char == '\\' && !escaped) { 281 | escaped = 1; 282 | string++; 283 | continue; 284 | } else if (current_char == '\"' && !escaped) { 285 | in_string = !in_string; 286 | } else if (!in_string && strncmp(string, start_token, start_token_len) == 0) { 287 | for(i = 0; i < start_token_len; i++) { 288 | string[i] = ' '; 289 | } 290 | string = string + start_token_len; 291 | ptr = strstr(string, end_token); 292 | if (!ptr) { 293 | return; 294 | } 295 | for (i = 0; i < (ptr - string) + end_token_len; i++) { 296 | string[i] = ' '; 297 | } 298 | string = ptr + end_token_len - 1; 299 | } 300 | escaped = 0; 301 | string++; 302 | } 303 | } 304 | 305 | /* JSON Object */ 306 | static JSON_Object * json_object_init(JSON_Value *wrapping_value) { 307 | JSON_Object *new_obj = (JSON_Object*)parson_malloc(sizeof(JSON_Object)); 308 | if (new_obj == NULL) { 309 | return NULL; 310 | } 311 | new_obj->wrapping_value = wrapping_value; 312 | new_obj->names = (char**)NULL; 313 | new_obj->values = (JSON_Value**)NULL; 314 | new_obj->capacity = 0; 315 | new_obj->count = 0; 316 | return new_obj; 317 | } 318 | 319 | static JSON_Status json_object_add(JSON_Object *object, const char *name, JSON_Value *value) { 320 | size_t index = 0; 321 | if (object == NULL || name == NULL || value == NULL) { 322 | return JSONFailure; 323 | } 324 | if (json_object_get_value(object, name) != NULL) { 325 | return JSONFailure; 326 | } 327 | if (object->count >= object->capacity) { 328 | size_t new_capacity = MAX(object->capacity * 2, STARTING_CAPACITY); 329 | if (new_capacity > OBJECT_MAX_CAPACITY) { 330 | return JSONFailure; 331 | } 332 | if (json_object_resize(object, new_capacity) == JSONFailure) { 333 | return JSONFailure; 334 | } 335 | } 336 | index = object->count; 337 | object->names[index] = parson_strdup(name); 338 | if (object->names[index] == NULL) { 339 | return JSONFailure; 340 | } 341 | value->parent = json_object_get_wrapping_value(object); 342 | object->values[index] = value; 343 | object->count++; 344 | return JSONSuccess; 345 | } 346 | 347 | static JSON_Status json_object_resize(JSON_Object *object, size_t new_capacity) { 348 | char **temp_names = NULL; 349 | JSON_Value **temp_values = NULL; 350 | 351 | if ((object->names == NULL && object->values != NULL) || 352 | (object->names != NULL && object->values == NULL) || 353 | new_capacity == 0) { 354 | return JSONFailure; /* Shouldn't happen */ 355 | } 356 | temp_names = (char**)parson_malloc(new_capacity * sizeof(char*)); 357 | if (temp_names == NULL) { 358 | return JSONFailure; 359 | } 360 | temp_values = (JSON_Value**)parson_malloc(new_capacity * sizeof(JSON_Value*)); 361 | if (temp_values == NULL) { 362 | parson_free(temp_names); 363 | return JSONFailure; 364 | } 365 | if (object->names != NULL && object->values != NULL && object->count > 0) { 366 | memcpy(temp_names, object->names, object->count * sizeof(char*)); 367 | memcpy(temp_values, object->values, object->count * sizeof(JSON_Value*)); 368 | } 369 | parson_free(object->names); 370 | parson_free(object->values); 371 | object->names = temp_names; 372 | object->values = temp_values; 373 | object->capacity = new_capacity; 374 | return JSONSuccess; 375 | } 376 | 377 | static JSON_Value * json_object_nget_value(const JSON_Object *object, const char *name, size_t n) { 378 | size_t i, name_length; 379 | for (i = 0; i < json_object_get_count(object); i++) { 380 | name_length = strlen(object->names[i]); 381 | if (name_length != n) { 382 | continue; 383 | } 384 | if (strncmp(object->names[i], name, n) == 0) { 385 | return object->values[i]; 386 | } 387 | } 388 | return NULL; 389 | } 390 | 391 | static void json_object_free(JSON_Object *object) { 392 | while(object->count--) { 393 | parson_free(object->names[object->count]); 394 | json_value_free(object->values[object->count]); 395 | } 396 | parson_free(object->names); 397 | parson_free(object->values); 398 | parson_free(object); 399 | } 400 | 401 | /* JSON Array */ 402 | static JSON_Array * json_array_init(JSON_Value *wrapping_value) { 403 | JSON_Array *new_array = (JSON_Array*)parson_malloc(sizeof(JSON_Array)); 404 | if (new_array == NULL) { 405 | return NULL; 406 | } 407 | new_array->wrapping_value = wrapping_value; 408 | new_array->items = (JSON_Value**)NULL; 409 | new_array->capacity = 0; 410 | new_array->count = 0; 411 | return new_array; 412 | } 413 | 414 | static JSON_Status json_array_add(JSON_Array *array, JSON_Value *value) { 415 | if (array->count >= array->capacity) { 416 | size_t new_capacity = MAX(array->capacity * 2, STARTING_CAPACITY); 417 | if (new_capacity > ARRAY_MAX_CAPACITY) { 418 | return JSONFailure; 419 | } 420 | if (json_array_resize(array, new_capacity) == JSONFailure) { 421 | return JSONFailure; 422 | } 423 | } 424 | value->parent = json_array_get_wrapping_value(array); 425 | array->items[array->count] = value; 426 | array->count++; 427 | return JSONSuccess; 428 | } 429 | 430 | static JSON_Status json_array_resize(JSON_Array *array, size_t new_capacity) { 431 | JSON_Value **new_items = NULL; 432 | if (new_capacity == 0) { 433 | return JSONFailure; 434 | } 435 | new_items = (JSON_Value**)parson_malloc(new_capacity * sizeof(JSON_Value*)); 436 | if (new_items == NULL) { 437 | return JSONFailure; 438 | } 439 | if (array->items != NULL && array->count > 0) { 440 | memcpy(new_items, array->items, array->count * sizeof(JSON_Value*)); 441 | } 442 | parson_free(array->items); 443 | array->items = new_items; 444 | array->capacity = new_capacity; 445 | return JSONSuccess; 446 | } 447 | 448 | static void json_array_free(JSON_Array *array) { 449 | while (array->count--) { 450 | json_value_free(array->items[array->count]); 451 | } 452 | parson_free(array->items); 453 | parson_free(array); 454 | } 455 | 456 | /* JSON Value */ 457 | static JSON_Value * json_value_init_string_no_copy(char *string) { 458 | JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); 459 | if (!new_value) { 460 | return NULL; 461 | } 462 | new_value->parent = NULL; 463 | new_value->type = JSONString; 464 | new_value->value.string = string; 465 | return new_value; 466 | } 467 | 468 | /* Parser */ 469 | static JSON_Status skip_quotes(const char **string) { 470 | if (**string != '\"') { 471 | return JSONFailure; 472 | } 473 | SKIP_CHAR(string); 474 | while (**string != '\"') { 475 | if (**string == '\0') { 476 | return JSONFailure; 477 | } else if (**string == '\\') { 478 | SKIP_CHAR(string); 479 | if (**string == '\0') { 480 | return JSONFailure; 481 | } 482 | } 483 | SKIP_CHAR(string); 484 | } 485 | SKIP_CHAR(string); 486 | return JSONSuccess; 487 | } 488 | 489 | static int parse_utf_16(const char **unprocessed, char **processed) { 490 | unsigned int cp, lead, trail; 491 | char *processed_ptr = *processed; 492 | const char *unprocessed_ptr = *unprocessed; 493 | unprocessed_ptr++; /* skips u */ 494 | if (!is_utf16_hex((const unsigned char*)unprocessed_ptr) || sscanf(unprocessed_ptr, "%4x", &cp) == EOF) { 495 | return JSONFailure; 496 | } 497 | if (cp < 0x80) { 498 | *processed_ptr = cp; /* 0xxxxxxx */ 499 | } else if (cp < 0x800) { 500 | *processed_ptr++ = ((cp >> 6) & 0x1F) | 0xC0; /* 110xxxxx */ 501 | *processed_ptr = ((cp ) & 0x3F) | 0x80; /* 10xxxxxx */ 502 | } else if (cp < 0xD800 || cp > 0xDFFF) { 503 | *processed_ptr++ = ((cp >> 12) & 0x0F) | 0xE0; /* 1110xxxx */ 504 | *processed_ptr++ = ((cp >> 6) & 0x3F) | 0x80; /* 10xxxxxx */ 505 | *processed_ptr = ((cp ) & 0x3F) | 0x80; /* 10xxxxxx */ 506 | } else if (cp >= 0xD800 && cp <= 0xDBFF) { /* lead surrogate (0xD800..0xDBFF) */ 507 | lead = cp; 508 | unprocessed_ptr += 4; /* should always be within the buffer, otherwise previous sscanf would fail */ 509 | if (*unprocessed_ptr++ != '\\' || *unprocessed_ptr++ != 'u' || /* starts with \u? */ 510 | !is_utf16_hex((const unsigned char*)unprocessed_ptr) || 511 | sscanf(unprocessed_ptr, "%4x", &trail) == EOF || 512 | trail < 0xDC00 || trail > 0xDFFF) { /* valid trail surrogate? (0xDC00..0xDFFF) */ 513 | return JSONFailure; 514 | } 515 | cp = ((((lead-0xD800)&0x3FF)<<10)|((trail-0xDC00)&0x3FF))+0x010000; 516 | *processed_ptr++ = (((cp >> 18) & 0x07) | 0xF0); /* 11110xxx */ 517 | *processed_ptr++ = (((cp >> 12) & 0x3F) | 0x80); /* 10xxxxxx */ 518 | *processed_ptr++ = (((cp >> 6) & 0x3F) | 0x80); /* 10xxxxxx */ 519 | *processed_ptr = (((cp ) & 0x3F) | 0x80); /* 10xxxxxx */ 520 | } else { /* trail surrogate before lead surrogate */ 521 | return JSONFailure; 522 | } 523 | unprocessed_ptr += 3; 524 | *processed = processed_ptr; 525 | *unprocessed = unprocessed_ptr; 526 | return JSONSuccess; 527 | } 528 | 529 | 530 | /* Copies and processes passed string up to supplied length. 531 | Example: "\u006Corem ipsum" -> lorem ipsum */ 532 | static char* process_string(const char *input, size_t len) { 533 | const char *input_ptr = input; 534 | size_t initial_size = (len + 1) * sizeof(char); 535 | size_t final_size = 0; 536 | char *output = (char*)parson_malloc(initial_size); 537 | char *output_ptr = output; 538 | char *resized_output = NULL; 539 | while ((*input_ptr != '\0') && (size_t)(input_ptr - input) < len) { 540 | if (*input_ptr == '\\') { 541 | input_ptr++; 542 | switch (*input_ptr) { 543 | case '\"': *output_ptr = '\"'; break; 544 | case '\\': *output_ptr = '\\'; break; 545 | case '/': *output_ptr = '/'; break; 546 | case 'b': *output_ptr = '\b'; break; 547 | case 'f': *output_ptr = '\f'; break; 548 | case 'n': *output_ptr = '\n'; break; 549 | case 'r': *output_ptr = '\r'; break; 550 | case 't': *output_ptr = '\t'; break; 551 | case 'u': 552 | if (parse_utf_16(&input_ptr, &output_ptr) == JSONFailure) { 553 | goto error; 554 | } 555 | break; 556 | default: 557 | goto error; 558 | } 559 | } else if ((unsigned char)*input_ptr < 0x20) { 560 | goto error; /* 0x00-0x19 are invalid characters for json string (http://www.ietf.org/rfc/rfc4627.txt) */ 561 | } else { 562 | *output_ptr = *input_ptr; 563 | } 564 | output_ptr++; 565 | input_ptr++; 566 | } 567 | *output_ptr = '\0'; 568 | /* resize to new length */ 569 | final_size = (size_t)(output_ptr-output) + 1; 570 | /* todo: don't resize if final_size == initial_size */ 571 | resized_output = (char*)parson_malloc(final_size); 572 | if (resized_output == NULL) { 573 | goto error; 574 | } 575 | memcpy(resized_output, output, final_size); 576 | parson_free(output); 577 | return resized_output; 578 | error: 579 | parson_free(output); 580 | return NULL; 581 | } 582 | 583 | /* Return processed contents of a string between quotes and 584 | skips passed argument to a matching quote. */ 585 | static char * get_quoted_string(const char **string) { 586 | const char *string_start = *string; 587 | size_t string_len = 0; 588 | JSON_Status status = skip_quotes(string); 589 | if (status != JSONSuccess) { 590 | return NULL; 591 | } 592 | string_len = *string - string_start - 2; /* length without quotes */ 593 | return process_string(string_start + 1, string_len); 594 | } 595 | 596 | static JSON_Value * parse_value(const char **string, size_t nesting) { 597 | if (nesting > MAX_NESTING) { 598 | return NULL; 599 | } 600 | SKIP_WHITESPACES(string); 601 | switch (**string) { 602 | case '{': 603 | return parse_object_value(string, nesting + 1); 604 | case '[': 605 | return parse_array_value(string, nesting + 1); 606 | case '\"': 607 | return parse_string_value(string); 608 | case 'f': case 't': 609 | return parse_boolean_value(string); 610 | case '-': 611 | case '0': case '1': case '2': case '3': case '4': 612 | case '5': case '6': case '7': case '8': case '9': 613 | return parse_number_value(string); 614 | case 'n': 615 | return parse_null_value(string); 616 | default: 617 | return NULL; 618 | } 619 | } 620 | 621 | static JSON_Value * parse_object_value(const char **string, size_t nesting) { 622 | JSON_Value *output_value = json_value_init_object(), *new_value = NULL; 623 | JSON_Object *output_object = json_value_get_object(output_value); 624 | char *new_key = NULL; 625 | if (output_value == NULL || **string != '{') { 626 | return NULL; 627 | } 628 | SKIP_CHAR(string); 629 | SKIP_WHITESPACES(string); 630 | if (**string == '}') { /* empty object */ 631 | SKIP_CHAR(string); 632 | return output_value; 633 | } 634 | while (**string != '\0') { 635 | new_key = get_quoted_string(string); 636 | SKIP_WHITESPACES(string); 637 | if (new_key == NULL || **string != ':') { 638 | json_value_free(output_value); 639 | return NULL; 640 | } 641 | SKIP_CHAR(string); 642 | new_value = parse_value(string, nesting); 643 | if (new_value == NULL) { 644 | parson_free(new_key); 645 | json_value_free(output_value); 646 | return NULL; 647 | } 648 | if(json_object_add(output_object, new_key, new_value) == JSONFailure) { 649 | parson_free(new_key); 650 | parson_free(new_value); 651 | json_value_free(output_value); 652 | return NULL; 653 | } 654 | parson_free(new_key); 655 | SKIP_WHITESPACES(string); 656 | if (**string != ',') { 657 | break; 658 | } 659 | SKIP_CHAR(string); 660 | SKIP_WHITESPACES(string); 661 | } 662 | SKIP_WHITESPACES(string); 663 | if (**string != '}' || /* Trim object after parsing is over */ 664 | json_object_resize(output_object, json_object_get_count(output_object)) == JSONFailure) { 665 | json_value_free(output_value); 666 | return NULL; 667 | } 668 | SKIP_CHAR(string); 669 | return output_value; 670 | } 671 | 672 | static JSON_Value * parse_array_value(const char **string, size_t nesting) { 673 | JSON_Value *output_value = json_value_init_array(), *new_array_value = NULL; 674 | JSON_Array *output_array = json_value_get_array(output_value); 675 | if (!output_value || **string != '[') { 676 | return NULL; 677 | } 678 | SKIP_CHAR(string); 679 | SKIP_WHITESPACES(string); 680 | if (**string == ']') { /* empty array */ 681 | SKIP_CHAR(string); 682 | return output_value; 683 | } 684 | while (**string != '\0') { 685 | new_array_value = parse_value(string, nesting); 686 | if (!new_array_value) { 687 | json_value_free(output_value); 688 | return NULL; 689 | } 690 | if (json_array_add(output_array, new_array_value) == JSONFailure) { 691 | parson_free(new_array_value); 692 | json_value_free(output_value); 693 | return NULL; 694 | } 695 | SKIP_WHITESPACES(string); 696 | if (**string != ',') { 697 | break; 698 | } 699 | SKIP_CHAR(string); 700 | SKIP_WHITESPACES(string); 701 | } 702 | SKIP_WHITESPACES(string); 703 | if (**string != ']' || /* Trim array after parsing is over */ 704 | json_array_resize(output_array, json_array_get_count(output_array)) == JSONFailure) { 705 | json_value_free(output_value); 706 | return NULL; 707 | } 708 | SKIP_CHAR(string); 709 | return output_value; 710 | } 711 | 712 | static JSON_Value * parse_string_value(const char **string) { 713 | JSON_Value *value = NULL; 714 | char *new_string = get_quoted_string(string); 715 | if (new_string == NULL) { 716 | return NULL; 717 | } 718 | value = json_value_init_string_no_copy(new_string); 719 | if (value == NULL) { 720 | parson_free(new_string); 721 | return NULL; 722 | } 723 | return value; 724 | } 725 | 726 | static JSON_Value * parse_boolean_value(const char **string) { 727 | size_t true_token_size = SIZEOF_TOKEN("true"); 728 | size_t false_token_size = SIZEOF_TOKEN("false"); 729 | if (strncmp("true", *string, true_token_size) == 0) { 730 | *string += true_token_size; 731 | return json_value_init_boolean(1); 732 | } else if (strncmp("false", *string, false_token_size) == 0) { 733 | *string += false_token_size; 734 | return json_value_init_boolean(0); 735 | } 736 | return NULL; 737 | } 738 | 739 | static JSON_Value * parse_number_value(const char **string) { 740 | char *end; 741 | double number = 0; 742 | errno = 0; 743 | number = strtod(*string, &end); 744 | if (errno || !is_decimal(*string, end - *string)) { 745 | return NULL; 746 | } 747 | *string = end; 748 | return json_value_init_number(number); 749 | } 750 | 751 | static JSON_Value * parse_null_value(const char **string) { 752 | size_t token_size = SIZEOF_TOKEN("null"); 753 | if (strncmp("null", *string, token_size) == 0) { 754 | *string += token_size; 755 | return json_value_init_null(); 756 | } 757 | return NULL; 758 | } 759 | 760 | /* Serialization */ 761 | #define APPEND_STRING(str) do { written = append_string(buf, (str));\ 762 | if (written < 0) { return -1; }\ 763 | if (buf != NULL) { buf += written; }\ 764 | written_total += written; } while(0) 765 | 766 | #define APPEND_INDENT(level) do { written = append_indent(buf, (level));\ 767 | if (written < 0) { return -1; }\ 768 | if (buf != NULL) { buf += written; }\ 769 | written_total += written; } while(0) 770 | 771 | static int json_serialize_to_buffer_r(const JSON_Value *value, char *buf, int level, int is_pretty, char *num_buf) 772 | { 773 | const char *key = NULL, *string = NULL; 774 | JSON_Value *temp_value = NULL; 775 | JSON_Array *array = NULL; 776 | JSON_Object *object = NULL; 777 | size_t i = 0, count = 0; 778 | double num = 0.0; 779 | int written = -1, written_total = 0; 780 | 781 | switch (json_value_get_type(value)) { 782 | case JSONArray: 783 | array = json_value_get_array(value); 784 | count = json_array_get_count(array); 785 | APPEND_STRING("["); 786 | if (count > 0 && is_pretty) { 787 | APPEND_STRING("\n"); 788 | } 789 | for (i = 0; i < count; i++) { 790 | if (is_pretty) { 791 | APPEND_INDENT(level+1); 792 | } 793 | temp_value = json_array_get_value(array, i); 794 | written = json_serialize_to_buffer_r(temp_value, buf, level+1, is_pretty, num_buf); 795 | if (written < 0) { 796 | return -1; 797 | } 798 | if (buf != NULL) { 799 | buf += written; 800 | } 801 | written_total += written; 802 | if (i < (count - 1)) { 803 | APPEND_STRING(","); 804 | } 805 | if (is_pretty) { 806 | APPEND_STRING("\n"); 807 | } 808 | } 809 | if (count > 0 && is_pretty) { 810 | APPEND_INDENT(level); 811 | } 812 | APPEND_STRING("]"); 813 | return written_total; 814 | case JSONObject: 815 | object = json_value_get_object(value); 816 | count = json_object_get_count(object); 817 | APPEND_STRING("{"); 818 | if (count > 0 && is_pretty) { 819 | APPEND_STRING("\n"); 820 | } 821 | for (i = 0; i < count; i++) { 822 | key = json_object_get_name(object, i); 823 | if (key == NULL) { 824 | return -1; 825 | } 826 | if (is_pretty) { 827 | APPEND_INDENT(level+1); 828 | } 829 | written = json_serialize_string(key, buf); 830 | if (written < 0) { 831 | return -1; 832 | } 833 | if (buf != NULL) { 834 | buf += written; 835 | } 836 | written_total += written; 837 | APPEND_STRING(":"); 838 | if (is_pretty) { 839 | APPEND_STRING(" "); 840 | } 841 | temp_value = json_object_get_value(object, key); 842 | written = json_serialize_to_buffer_r(temp_value, buf, level+1, is_pretty, num_buf); 843 | if (written < 0) { 844 | return -1; 845 | } 846 | if (buf != NULL) { 847 | buf += written; 848 | } 849 | written_total += written; 850 | if (i < (count - 1)) { 851 | APPEND_STRING(","); 852 | } 853 | if (is_pretty) { 854 | APPEND_STRING("\n"); 855 | } 856 | } 857 | if (count > 0 && is_pretty) { 858 | APPEND_INDENT(level); 859 | } 860 | APPEND_STRING("}"); 861 | return written_total; 862 | case JSONString: 863 | string = json_value_get_string(value); 864 | if (string == NULL) { 865 | return -1; 866 | } 867 | written = json_serialize_string(string, buf); 868 | if (written < 0) { 869 | return -1; 870 | } 871 | if (buf != NULL) { 872 | buf += written; 873 | } 874 | written_total += written; 875 | return written_total; 876 | case JSONBoolean: 877 | if (json_value_get_boolean(value)) { 878 | APPEND_STRING("true"); 879 | } else { 880 | APPEND_STRING("false"); 881 | } 882 | return written_total; 883 | case JSONNumber: 884 | num = json_value_get_number(value); 885 | if (buf != NULL) { 886 | num_buf = buf; 887 | } 888 | if (num == ((double)(int)num)) { /* check if num is integer */ 889 | written = sprintf(num_buf, "%d", (int)num); 890 | } else if (num == ((double)(unsigned int)num)) { 891 | written = sprintf(num_buf, "%u", (unsigned int)num); 892 | } else { 893 | written = sprintf(num_buf, DOUBLE_SERIALIZATION_FORMAT, num); 894 | } 895 | if (written < 0) { 896 | return -1; 897 | } 898 | if (buf != NULL) { 899 | buf += written; 900 | } 901 | written_total += written; 902 | return written_total; 903 | case JSONNull: 904 | APPEND_STRING("null"); 905 | return written_total; 906 | case JSONError: 907 | return -1; 908 | default: 909 | return -1; 910 | } 911 | } 912 | 913 | static int json_serialize_string(const char *string, char *buf) { 914 | size_t i = 0, len = strlen(string); 915 | char c = '\0'; 916 | int written = -1, written_total = 0; 917 | APPEND_STRING("\""); 918 | for (i = 0; i < len; i++) { 919 | c = string[i]; 920 | switch (c) { 921 | case '\"': APPEND_STRING("\\\""); break; 922 | case '\\': APPEND_STRING("\\\\"); break; 923 | case '/': APPEND_STRING("\\/"); break; /* to make json embeddable in xml\/html */ 924 | case '\b': APPEND_STRING("\\b"); break; 925 | case '\f': APPEND_STRING("\\f"); break; 926 | case '\n': APPEND_STRING("\\n"); break; 927 | case '\r': APPEND_STRING("\\r"); break; 928 | case '\t': APPEND_STRING("\\t"); break; 929 | case '\x00': APPEND_STRING("\\u0000"); break; 930 | case '\x01': APPEND_STRING("\\u0001"); break; 931 | case '\x02': APPEND_STRING("\\u0002"); break; 932 | case '\x03': APPEND_STRING("\\u0003"); break; 933 | case '\x04': APPEND_STRING("\\u0004"); break; 934 | case '\x05': APPEND_STRING("\\u0005"); break; 935 | case '\x06': APPEND_STRING("\\u0006"); break; 936 | case '\x07': APPEND_STRING("\\u0007"); break; 937 | /* '\x08' duplicate: '\b' */ 938 | /* '\x09' duplicate: '\t' */ 939 | /* '\x0a' duplicate: '\n' */ 940 | case '\x0b': APPEND_STRING("\\u000b"); break; 941 | /* '\x0c' duplicate: '\f' */ 942 | /* '\x0d' duplicate: '\r' */ 943 | case '\x0e': APPEND_STRING("\\u000e"); break; 944 | case '\x0f': APPEND_STRING("\\u000f"); break; 945 | case '\x10': APPEND_STRING("\\u0010"); break; 946 | case '\x11': APPEND_STRING("\\u0011"); break; 947 | case '\x12': APPEND_STRING("\\u0012"); break; 948 | case '\x13': APPEND_STRING("\\u0013"); break; 949 | case '\x14': APPEND_STRING("\\u0014"); break; 950 | case '\x15': APPEND_STRING("\\u0015"); break; 951 | case '\x16': APPEND_STRING("\\u0016"); break; 952 | case '\x17': APPEND_STRING("\\u0017"); break; 953 | case '\x18': APPEND_STRING("\\u0018"); break; 954 | case '\x19': APPEND_STRING("\\u0019"); break; 955 | case '\x1a': APPEND_STRING("\\u001a"); break; 956 | case '\x1b': APPEND_STRING("\\u001b"); break; 957 | case '\x1c': APPEND_STRING("\\u001c"); break; 958 | case '\x1d': APPEND_STRING("\\u001d"); break; 959 | case '\x1e': APPEND_STRING("\\u001e"); break; 960 | case '\x1f': APPEND_STRING("\\u001f"); break; 961 | default: 962 | if (buf != NULL) { 963 | buf[0] = c; 964 | buf += 1; 965 | } 966 | written_total += 1; 967 | break; 968 | } 969 | } 970 | APPEND_STRING("\""); 971 | return written_total; 972 | } 973 | 974 | static int append_indent(char *buf, int level) { 975 | int i; 976 | int written = -1, written_total = 0; 977 | for (i = 0; i < level; i++) { 978 | APPEND_STRING(" "); 979 | } 980 | return written_total; 981 | } 982 | 983 | static int append_string(char *buf, const char *string) { 984 | if (buf == NULL) { 985 | return (int)strlen(string); 986 | } 987 | return sprintf(buf, "%s", string); 988 | } 989 | 990 | #undef APPEND_STRING 991 | #undef APPEND_INDENT 992 | 993 | /* Parser API */ 994 | JSON_Value * json_parse_file(const char *filename) { 995 | char *file_contents = read_file(filename); 996 | JSON_Value *output_value = NULL; 997 | if (file_contents == NULL) { 998 | return NULL; 999 | } 1000 | output_value = json_parse_string(file_contents); 1001 | parson_free(file_contents); 1002 | return output_value; 1003 | } 1004 | 1005 | JSON_Value * json_parse_file_with_comments(const char *filename) { 1006 | char *file_contents = read_file(filename); 1007 | JSON_Value *output_value = NULL; 1008 | if (file_contents == NULL) { 1009 | return NULL; 1010 | } 1011 | output_value = json_parse_string_with_comments(file_contents); 1012 | parson_free(file_contents); 1013 | return output_value; 1014 | } 1015 | 1016 | JSON_Value * json_parse_string(const char *string) { 1017 | if (string == NULL) { 1018 | return NULL; 1019 | } 1020 | if (string[0] == '\xEF' && string[1] == '\xBB' && string[2] == '\xBF') { 1021 | string = string + 3; /* Support for UTF-8 BOM */ 1022 | } 1023 | return parse_value((const char**)&string, 0); 1024 | } 1025 | 1026 | JSON_Value * json_parse_string_with_comments(const char *string) { 1027 | JSON_Value *result = NULL; 1028 | char *string_mutable_copy = NULL, *string_mutable_copy_ptr = NULL; 1029 | string_mutable_copy = parson_strdup(string); 1030 | if (string_mutable_copy == NULL) { 1031 | return NULL; 1032 | } 1033 | remove_comments(string_mutable_copy, "/*", "*/"); 1034 | remove_comments(string_mutable_copy, "//", "\n"); 1035 | string_mutable_copy_ptr = string_mutable_copy; 1036 | result = parse_value((const char**)&string_mutable_copy_ptr, 0); 1037 | parson_free(string_mutable_copy); 1038 | return result; 1039 | } 1040 | 1041 | /* JSON Object API */ 1042 | 1043 | JSON_Value * json_object_get_value(const JSON_Object *object, const char *name) { 1044 | if (object == NULL || name == NULL) { 1045 | return NULL; 1046 | } 1047 | return json_object_nget_value(object, name, strlen(name)); 1048 | } 1049 | 1050 | const char * json_object_get_string(const JSON_Object *object, const char *name) { 1051 | return json_value_get_string(json_object_get_value(object, name)); 1052 | } 1053 | 1054 | double json_object_get_number(const JSON_Object *object, const char *name) { 1055 | return json_value_get_number(json_object_get_value(object, name)); 1056 | } 1057 | 1058 | JSON_Object * json_object_get_object(const JSON_Object *object, const char *name) { 1059 | return json_value_get_object(json_object_get_value(object, name)); 1060 | } 1061 | 1062 | JSON_Array * json_object_get_array(const JSON_Object *object, const char *name) { 1063 | return json_value_get_array(json_object_get_value(object, name)); 1064 | } 1065 | 1066 | int json_object_get_boolean(const JSON_Object *object, const char *name) { 1067 | return json_value_get_boolean(json_object_get_value(object, name)); 1068 | } 1069 | 1070 | JSON_Value * json_object_dotget_value(const JSON_Object *object, const char *name) { 1071 | const char *dot_position = strchr(name, '.'); 1072 | if (!dot_position) { 1073 | return json_object_get_value(object, name); 1074 | } 1075 | object = json_value_get_object(json_object_nget_value(object, name, dot_position - name)); 1076 | return json_object_dotget_value(object, dot_position + 1); 1077 | } 1078 | 1079 | const char * json_object_dotget_string(const JSON_Object *object, const char *name) { 1080 | return json_value_get_string(json_object_dotget_value(object, name)); 1081 | } 1082 | 1083 | double json_object_dotget_number(const JSON_Object *object, const char *name) { 1084 | return json_value_get_number(json_object_dotget_value(object, name)); 1085 | } 1086 | 1087 | JSON_Object * json_object_dotget_object(const JSON_Object *object, const char *name) { 1088 | return json_value_get_object(json_object_dotget_value(object, name)); 1089 | } 1090 | 1091 | JSON_Array * json_object_dotget_array(const JSON_Object *object, const char *name) { 1092 | return json_value_get_array(json_object_dotget_value(object, name)); 1093 | } 1094 | 1095 | int json_object_dotget_boolean(const JSON_Object *object, const char *name) { 1096 | return json_value_get_boolean(json_object_dotget_value(object, name)); 1097 | } 1098 | 1099 | size_t json_object_get_count(const JSON_Object *object) { 1100 | return object ? object->count : 0; 1101 | } 1102 | 1103 | const char * json_object_get_name(const JSON_Object *object, size_t index) { 1104 | if (object == NULL || index >= json_object_get_count(object)) { 1105 | return NULL; 1106 | } 1107 | return object->names[index]; 1108 | } 1109 | 1110 | JSON_Value * json_object_get_value_at(const JSON_Object *object, size_t index) { 1111 | if (object == NULL || index >= json_object_get_count(object)) { 1112 | return NULL; 1113 | } 1114 | return object->values[index]; 1115 | } 1116 | 1117 | JSON_Value *json_object_get_wrapping_value(const JSON_Object *object) { 1118 | return object->wrapping_value; 1119 | } 1120 | 1121 | int json_object_has_value (const JSON_Object *object, const char *name) { 1122 | return json_object_get_value(object, name) != NULL; 1123 | } 1124 | 1125 | int json_object_has_value_of_type(const JSON_Object *object, const char *name, JSON_Value_Type type) { 1126 | JSON_Value *val = json_object_get_value(object, name); 1127 | return val != NULL && json_value_get_type(val) == type; 1128 | } 1129 | 1130 | int json_object_dothas_value (const JSON_Object *object, const char *name) { 1131 | return json_object_dotget_value(object, name) != NULL; 1132 | } 1133 | 1134 | int json_object_dothas_value_of_type(const JSON_Object *object, const char *name, JSON_Value_Type type) { 1135 | JSON_Value *val = json_object_dotget_value(object, name); 1136 | return val != NULL && json_value_get_type(val) == type; 1137 | } 1138 | 1139 | /* JSON Array API */ 1140 | JSON_Value * json_array_get_value(const JSON_Array *array, size_t index) { 1141 | if (array == NULL || index >= json_array_get_count(array)) { 1142 | return NULL; 1143 | } 1144 | return array->items[index]; 1145 | } 1146 | 1147 | const char * json_array_get_string(const JSON_Array *array, size_t index) { 1148 | return json_value_get_string(json_array_get_value(array, index)); 1149 | } 1150 | 1151 | double json_array_get_number(const JSON_Array *array, size_t index) { 1152 | return json_value_get_number(json_array_get_value(array, index)); 1153 | } 1154 | 1155 | JSON_Object * json_array_get_object(const JSON_Array *array, size_t index) { 1156 | return json_value_get_object(json_array_get_value(array, index)); 1157 | } 1158 | 1159 | JSON_Array * json_array_get_array(const JSON_Array *array, size_t index) { 1160 | return json_value_get_array(json_array_get_value(array, index)); 1161 | } 1162 | 1163 | int json_array_get_boolean(const JSON_Array *array, size_t index) { 1164 | return json_value_get_boolean(json_array_get_value(array, index)); 1165 | } 1166 | 1167 | size_t json_array_get_count(const JSON_Array *array) { 1168 | return array ? array->count : 0; 1169 | } 1170 | 1171 | JSON_Value * json_array_get_wrapping_value(const JSON_Array *array) { 1172 | return array->wrapping_value; 1173 | } 1174 | 1175 | /* JSON Value API */ 1176 | JSON_Value_Type json_value_get_type(const JSON_Value *value) { 1177 | return value ? value->type : JSONError; 1178 | } 1179 | 1180 | JSON_Object * json_value_get_object(const JSON_Value *value) { 1181 | return json_value_get_type(value) == JSONObject ? value->value.object : NULL; 1182 | } 1183 | 1184 | JSON_Array * json_value_get_array(const JSON_Value *value) { 1185 | return json_value_get_type(value) == JSONArray ? value->value.array : NULL; 1186 | } 1187 | 1188 | const char * json_value_get_string(const JSON_Value *value) { 1189 | return json_value_get_type(value) == JSONString ? value->value.string : NULL; 1190 | } 1191 | 1192 | double json_value_get_number(const JSON_Value *value) { 1193 | return json_value_get_type(value) == JSONNumber ? value->value.number : 0; 1194 | } 1195 | 1196 | int json_value_get_boolean(const JSON_Value *value) { 1197 | return json_value_get_type(value) == JSONBoolean ? value->value.boolean : -1; 1198 | } 1199 | 1200 | JSON_Value * json_value_get_parent (const JSON_Value *value) { 1201 | return value ? value->parent : NULL; 1202 | } 1203 | 1204 | void json_value_free(JSON_Value *value) { 1205 | switch (json_value_get_type(value)) { 1206 | case JSONObject: 1207 | json_object_free(value->value.object); 1208 | break; 1209 | case JSONString: 1210 | parson_free(value->value.string); 1211 | break; 1212 | case JSONArray: 1213 | json_array_free(value->value.array); 1214 | break; 1215 | default: 1216 | break; 1217 | } 1218 | parson_free(value); 1219 | } 1220 | 1221 | JSON_Value * json_value_init_object(void) { 1222 | JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); 1223 | if (!new_value) { 1224 | return NULL; 1225 | } 1226 | new_value->parent = NULL; 1227 | new_value->type = JSONObject; 1228 | new_value->value.object = json_object_init(new_value); 1229 | if (!new_value->value.object) { 1230 | parson_free(new_value); 1231 | return NULL; 1232 | } 1233 | return new_value; 1234 | } 1235 | 1236 | JSON_Value * json_value_init_array(void) { 1237 | JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); 1238 | if (!new_value) { 1239 | return NULL; 1240 | } 1241 | new_value->parent = NULL; 1242 | new_value->type = JSONArray; 1243 | new_value->value.array = json_array_init(new_value); 1244 | if (!new_value->value.array) { 1245 | parson_free(new_value); 1246 | return NULL; 1247 | } 1248 | return new_value; 1249 | } 1250 | 1251 | JSON_Value * json_value_init_string(const char *string) { 1252 | char *copy = NULL; 1253 | JSON_Value *value; 1254 | size_t string_len = 0; 1255 | if (string == NULL) { 1256 | return NULL; 1257 | } 1258 | string_len = strlen(string); 1259 | if (!is_valid_utf8(string, string_len)) { 1260 | return NULL; 1261 | } 1262 | copy = parson_strndup(string, string_len); 1263 | if (copy == NULL) { 1264 | return NULL; 1265 | } 1266 | value = json_value_init_string_no_copy(copy); 1267 | if (value == NULL) { 1268 | parson_free(copy); 1269 | } 1270 | return value; 1271 | } 1272 | 1273 | JSON_Value * json_value_init_number(double number) { 1274 | JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); 1275 | if (!new_value) { 1276 | return NULL; 1277 | } 1278 | new_value->parent = NULL; 1279 | new_value->type = JSONNumber; 1280 | new_value->value.number = number; 1281 | return new_value; 1282 | } 1283 | 1284 | JSON_Value * json_value_init_boolean(int boolean) { 1285 | JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); 1286 | if (!new_value) { 1287 | return NULL; 1288 | } 1289 | new_value->parent = NULL; 1290 | new_value->type = JSONBoolean; 1291 | new_value->value.boolean = boolean ? 1 : 0; 1292 | return new_value; 1293 | } 1294 | 1295 | JSON_Value * json_value_init_null(void) { 1296 | JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); 1297 | if (!new_value) { 1298 | return NULL; 1299 | } 1300 | new_value->parent = NULL; 1301 | new_value->type = JSONNull; 1302 | return new_value; 1303 | } 1304 | 1305 | JSON_Value * json_value_deep_copy(const JSON_Value *value) { 1306 | size_t i = 0; 1307 | JSON_Value *return_value = NULL, *temp_value_copy = NULL, *temp_value = NULL; 1308 | const char *temp_string = NULL, *temp_key = NULL; 1309 | char *temp_string_copy = NULL; 1310 | JSON_Array *temp_array = NULL, *temp_array_copy = NULL; 1311 | JSON_Object *temp_object = NULL, *temp_object_copy = NULL; 1312 | 1313 | switch (json_value_get_type(value)) { 1314 | case JSONArray: 1315 | temp_array = json_value_get_array(value); 1316 | return_value = json_value_init_array(); 1317 | if (return_value == NULL) { 1318 | return NULL; 1319 | } 1320 | temp_array_copy = json_value_get_array(return_value); 1321 | for (i = 0; i < json_array_get_count(temp_array); i++) { 1322 | temp_value = json_array_get_value(temp_array, i); 1323 | temp_value_copy = json_value_deep_copy(temp_value); 1324 | if (temp_value_copy == NULL) { 1325 | json_value_free(return_value); 1326 | return NULL; 1327 | } 1328 | if (json_array_add(temp_array_copy, temp_value_copy) == JSONFailure) { 1329 | json_value_free(return_value); 1330 | json_value_free(temp_value_copy); 1331 | return NULL; 1332 | } 1333 | } 1334 | return return_value; 1335 | case JSONObject: 1336 | temp_object = json_value_get_object(value); 1337 | return_value = json_value_init_object(); 1338 | if (return_value == NULL) { 1339 | return NULL; 1340 | } 1341 | temp_object_copy = json_value_get_object(return_value); 1342 | for (i = 0; i < json_object_get_count(temp_object); i++) { 1343 | temp_key = json_object_get_name(temp_object, i); 1344 | temp_value = json_object_get_value(temp_object, temp_key); 1345 | temp_value_copy = json_value_deep_copy(temp_value); 1346 | if (temp_value_copy == NULL) { 1347 | json_value_free(return_value); 1348 | return NULL; 1349 | } 1350 | if (json_object_add(temp_object_copy, temp_key, temp_value_copy) == JSONFailure) { 1351 | json_value_free(return_value); 1352 | json_value_free(temp_value_copy); 1353 | return NULL; 1354 | } 1355 | } 1356 | return return_value; 1357 | case JSONBoolean: 1358 | return json_value_init_boolean(json_value_get_boolean(value)); 1359 | case JSONNumber: 1360 | return json_value_init_number(json_value_get_number(value)); 1361 | case JSONString: 1362 | temp_string = json_value_get_string(value); 1363 | if (temp_string == NULL) { 1364 | return NULL; 1365 | } 1366 | temp_string_copy = parson_strdup(temp_string); 1367 | if (temp_string_copy == NULL) { 1368 | return NULL; 1369 | } 1370 | return_value = json_value_init_string_no_copy(temp_string_copy); 1371 | if (return_value == NULL) { 1372 | parson_free(temp_string_copy); 1373 | } 1374 | return return_value; 1375 | case JSONNull: 1376 | return json_value_init_null(); 1377 | case JSONError: 1378 | return NULL; 1379 | default: 1380 | return NULL; 1381 | } 1382 | } 1383 | 1384 | size_t json_serialization_size(const JSON_Value *value) { 1385 | char num_buf[1100]; /* recursively allocating buffer on stack is a bad idea, so let's do it only once */ 1386 | int res = json_serialize_to_buffer_r(value, NULL, 0, 0, num_buf); 1387 | return res < 0 ? 0 : (size_t)(res + 1); 1388 | } 1389 | 1390 | JSON_Status json_serialize_to_buffer(const JSON_Value *value, char *buf, size_t buf_size_in_bytes) { 1391 | int written = -1; 1392 | size_t needed_size_in_bytes = json_serialization_size(value); 1393 | if (needed_size_in_bytes == 0 || buf_size_in_bytes < needed_size_in_bytes) { 1394 | return JSONFailure; 1395 | } 1396 | written = json_serialize_to_buffer_r(value, buf, 0, 0, NULL); 1397 | if (written < 0) { 1398 | return JSONFailure; 1399 | } 1400 | return JSONSuccess; 1401 | } 1402 | 1403 | JSON_Status json_serialize_to_file(const JSON_Value *value, const char *filename) { 1404 | JSON_Status return_code = JSONSuccess; 1405 | FILE *fp = NULL; 1406 | char *serialized_string = json_serialize_to_string(value); 1407 | if (serialized_string == NULL) { 1408 | return JSONFailure; 1409 | } 1410 | fp = fopen (filename, "w"); 1411 | if (fp == NULL) { 1412 | json_free_serialized_string(serialized_string); 1413 | return JSONFailure; 1414 | } 1415 | if (fputs(serialized_string, fp) == EOF) { 1416 | return_code = JSONFailure; 1417 | } 1418 | if (fclose(fp) == EOF) { 1419 | return_code = JSONFailure; 1420 | } 1421 | json_free_serialized_string(serialized_string); 1422 | return return_code; 1423 | } 1424 | 1425 | char * json_serialize_to_string(const JSON_Value *value) { 1426 | JSON_Status serialization_result = JSONFailure; 1427 | size_t buf_size_bytes = json_serialization_size(value); 1428 | char *buf = NULL; 1429 | if (buf_size_bytes == 0) { 1430 | return NULL; 1431 | } 1432 | buf = (char*)parson_malloc(buf_size_bytes); 1433 | if (buf == NULL) { 1434 | return NULL; 1435 | } 1436 | serialization_result = json_serialize_to_buffer(value, buf, buf_size_bytes); 1437 | if (serialization_result == JSONFailure) { 1438 | json_free_serialized_string(buf); 1439 | return NULL; 1440 | } 1441 | return buf; 1442 | } 1443 | 1444 | size_t json_serialization_size_pretty(const JSON_Value *value) { 1445 | char num_buf[1100]; /* recursively allocating buffer on stack is a bad idea, so let's do it only once */ 1446 | int res = json_serialize_to_buffer_r(value, NULL, 0, 1, num_buf); 1447 | return res < 0 ? 0 : (size_t)(res + 1); 1448 | } 1449 | 1450 | JSON_Status json_serialize_to_buffer_pretty(const JSON_Value *value, char *buf, size_t buf_size_in_bytes) { 1451 | int written = -1; 1452 | size_t needed_size_in_bytes = json_serialization_size_pretty(value); 1453 | if (needed_size_in_bytes == 0 || buf_size_in_bytes < needed_size_in_bytes) { 1454 | return JSONFailure; 1455 | } 1456 | written = json_serialize_to_buffer_r(value, buf, 0, 1, NULL); 1457 | if (written < 0) { 1458 | return JSONFailure; 1459 | } 1460 | return JSONSuccess; 1461 | } 1462 | 1463 | JSON_Status json_serialize_to_file_pretty(const JSON_Value *value, const char *filename) { 1464 | JSON_Status return_code = JSONSuccess; 1465 | FILE *fp = NULL; 1466 | char *serialized_string = json_serialize_to_string_pretty(value); 1467 | if (serialized_string == NULL) { 1468 | return JSONFailure; 1469 | } 1470 | fp = fopen (filename, "w"); 1471 | if (fp == NULL) { 1472 | json_free_serialized_string(serialized_string); 1473 | return JSONFailure; 1474 | } 1475 | if (fputs(serialized_string, fp) == EOF) { 1476 | return_code = JSONFailure; 1477 | } 1478 | if (fclose(fp) == EOF) { 1479 | return_code = JSONFailure; 1480 | } 1481 | json_free_serialized_string(serialized_string); 1482 | return return_code; 1483 | } 1484 | 1485 | char * json_serialize_to_string_pretty(const JSON_Value *value) { 1486 | JSON_Status serialization_result = JSONFailure; 1487 | size_t buf_size_bytes = json_serialization_size_pretty(value); 1488 | char *buf = NULL; 1489 | if (buf_size_bytes == 0) { 1490 | return NULL; 1491 | } 1492 | buf = (char*)parson_malloc(buf_size_bytes); 1493 | if (buf == NULL) { 1494 | return NULL; 1495 | } 1496 | serialization_result = json_serialize_to_buffer_pretty(value, buf, buf_size_bytes); 1497 | if (serialization_result == JSONFailure) { 1498 | json_free_serialized_string(buf); 1499 | return NULL; 1500 | } 1501 | return buf; 1502 | } 1503 | 1504 | void json_free_serialized_string(char *string) { 1505 | parson_free(string); 1506 | } 1507 | 1508 | JSON_Status json_array_remove(JSON_Array *array, size_t ix) { 1509 | JSON_Value *temp_value = NULL; 1510 | size_t last_element_ix = 0; 1511 | if (array == NULL || ix >= json_array_get_count(array)) { 1512 | return JSONFailure; 1513 | } 1514 | last_element_ix = json_array_get_count(array) - 1; 1515 | json_value_free(json_array_get_value(array, ix)); 1516 | if (ix != last_element_ix) { /* Replace value with one from the end of array */ 1517 | temp_value = json_array_get_value(array, last_element_ix); 1518 | if (temp_value == NULL) { 1519 | return JSONFailure; 1520 | } 1521 | array->items[ix] = temp_value; 1522 | } 1523 | array->count -= 1; 1524 | return JSONSuccess; 1525 | } 1526 | 1527 | JSON_Status json_array_replace_value(JSON_Array *array, size_t ix, JSON_Value *value) { 1528 | if (array == NULL || value == NULL || value->parent != NULL || ix >= json_array_get_count(array)) { 1529 | return JSONFailure; 1530 | } 1531 | json_value_free(json_array_get_value(array, ix)); 1532 | value->parent = json_array_get_wrapping_value(array); 1533 | array->items[ix] = value; 1534 | return JSONSuccess; 1535 | } 1536 | 1537 | JSON_Status json_array_replace_string(JSON_Array *array, size_t i, const char* string) { 1538 | JSON_Value *value = json_value_init_string(string); 1539 | if (value == NULL) { 1540 | return JSONFailure; 1541 | } 1542 | if (json_array_replace_value(array, i, value) == JSONFailure) { 1543 | json_value_free(value); 1544 | return JSONFailure; 1545 | } 1546 | return JSONSuccess; 1547 | } 1548 | 1549 | JSON_Status json_array_replace_number(JSON_Array *array, size_t i, double number) { 1550 | JSON_Value *value = json_value_init_number(number); 1551 | if (value == NULL) { 1552 | return JSONFailure; 1553 | } 1554 | if (json_array_replace_value(array, i, value) == JSONFailure) { 1555 | json_value_free(value); 1556 | return JSONFailure; 1557 | } 1558 | return JSONSuccess; 1559 | } 1560 | 1561 | JSON_Status json_array_replace_boolean(JSON_Array *array, size_t i, int boolean) { 1562 | JSON_Value *value = json_value_init_boolean(boolean); 1563 | if (value == NULL) { 1564 | return JSONFailure; 1565 | } 1566 | if (json_array_replace_value(array, i, value) == JSONFailure) { 1567 | json_value_free(value); 1568 | return JSONFailure; 1569 | } 1570 | return JSONSuccess; 1571 | } 1572 | 1573 | JSON_Status json_array_replace_null(JSON_Array *array, size_t i) { 1574 | JSON_Value *value = json_value_init_null(); 1575 | if (value == NULL) { 1576 | return JSONFailure; 1577 | } 1578 | if (json_array_replace_value(array, i, value) == JSONFailure) { 1579 | json_value_free(value); 1580 | return JSONFailure; 1581 | } 1582 | return JSONSuccess; 1583 | } 1584 | 1585 | JSON_Status json_array_clear(JSON_Array *array) { 1586 | size_t i = 0; 1587 | if (array == NULL) { 1588 | return JSONFailure; 1589 | } 1590 | for (i = 0; i < json_array_get_count(array); i++) { 1591 | json_value_free(json_array_get_value(array, i)); 1592 | } 1593 | array->count = 0; 1594 | return JSONSuccess; 1595 | } 1596 | 1597 | JSON_Status json_array_append_value(JSON_Array *array, JSON_Value *value) { 1598 | if (array == NULL || value == NULL || value->parent != NULL) { 1599 | return JSONFailure; 1600 | } 1601 | return json_array_add(array, value); 1602 | } 1603 | 1604 | JSON_Status json_array_append_string(JSON_Array *array, const char *string) { 1605 | JSON_Value *value = json_value_init_string(string); 1606 | if (value == NULL) { 1607 | return JSONFailure; 1608 | } 1609 | if (json_array_append_value(array, value) == JSONFailure) { 1610 | json_value_free(value); 1611 | return JSONFailure; 1612 | } 1613 | return JSONSuccess; 1614 | } 1615 | 1616 | JSON_Status json_array_append_number(JSON_Array *array, double number) { 1617 | JSON_Value *value = json_value_init_number(number); 1618 | if (value == NULL) { 1619 | return JSONFailure; 1620 | } 1621 | if (json_array_append_value(array, value) == JSONFailure) { 1622 | json_value_free(value); 1623 | return JSONFailure; 1624 | } 1625 | return JSONSuccess; 1626 | } 1627 | 1628 | JSON_Status json_array_append_boolean(JSON_Array *array, int boolean) { 1629 | JSON_Value *value = json_value_init_boolean(boolean); 1630 | if (value == NULL) { 1631 | return JSONFailure; 1632 | } 1633 | if (json_array_append_value(array, value) == JSONFailure) { 1634 | json_value_free(value); 1635 | return JSONFailure; 1636 | } 1637 | return JSONSuccess; 1638 | } 1639 | 1640 | JSON_Status json_array_append_null(JSON_Array *array) { 1641 | JSON_Value *value = json_value_init_null(); 1642 | if (value == NULL) { 1643 | return JSONFailure; 1644 | } 1645 | if (json_array_append_value(array, value) == JSONFailure) { 1646 | json_value_free(value); 1647 | return JSONFailure; 1648 | } 1649 | return JSONSuccess; 1650 | } 1651 | 1652 | JSON_Status json_object_set_value(JSON_Object *object, const char *name, JSON_Value *value) { 1653 | size_t i = 0; 1654 | JSON_Value *old_value; 1655 | if (object == NULL || name == NULL || value == NULL || value->parent != NULL) { 1656 | return JSONFailure; 1657 | } 1658 | old_value = json_object_get_value(object, name); 1659 | if (old_value != NULL) { /* free and overwrite old value */ 1660 | json_value_free(old_value); 1661 | for (i = 0; i < json_object_get_count(object); i++) { 1662 | if (strcmp(object->names[i], name) == 0) { 1663 | value->parent = json_object_get_wrapping_value(object); 1664 | object->values[i] = value; 1665 | return JSONSuccess; 1666 | } 1667 | } 1668 | } 1669 | /* add new key value pair */ 1670 | return json_object_add(object, name, value); 1671 | } 1672 | 1673 | JSON_Status json_object_set_string(JSON_Object *object, const char *name, const char *string) { 1674 | return json_object_set_value(object, name, json_value_init_string(string)); 1675 | } 1676 | 1677 | JSON_Status json_object_set_number(JSON_Object *object, const char *name, double number) { 1678 | return json_object_set_value(object, name, json_value_init_number(number)); 1679 | } 1680 | 1681 | JSON_Status json_object_set_boolean(JSON_Object *object, const char *name, int boolean) { 1682 | return json_object_set_value(object, name, json_value_init_boolean(boolean)); 1683 | } 1684 | 1685 | JSON_Status json_object_set_null(JSON_Object *object, const char *name) { 1686 | return json_object_set_value(object, name, json_value_init_null()); 1687 | } 1688 | 1689 | JSON_Status json_object_dotset_value(JSON_Object *object, const char *name, JSON_Value *value) { 1690 | const char *dot_pos = NULL; 1691 | char *current_name = NULL; 1692 | JSON_Object *temp_obj = NULL; 1693 | JSON_Value *new_value = NULL; 1694 | if (value == NULL || name == NULL || value == NULL) { 1695 | return JSONFailure; 1696 | } 1697 | dot_pos = strchr(name, '.'); 1698 | if (dot_pos == NULL) { 1699 | return json_object_set_value(object, name, value); 1700 | } else { 1701 | current_name = parson_strndup(name, dot_pos - name); 1702 | temp_obj = json_object_get_object(object, current_name); 1703 | if (temp_obj == NULL) { 1704 | new_value = json_value_init_object(); 1705 | if (new_value == NULL) { 1706 | parson_free(current_name); 1707 | return JSONFailure; 1708 | } 1709 | if (json_object_add(object, current_name, new_value) == JSONFailure) { 1710 | json_value_free(new_value); 1711 | parson_free(current_name); 1712 | return JSONFailure; 1713 | } 1714 | temp_obj = json_object_get_object(object, current_name); 1715 | } 1716 | parson_free(current_name); 1717 | return json_object_dotset_value(temp_obj, dot_pos + 1, value); 1718 | } 1719 | } 1720 | 1721 | JSON_Status json_object_dotset_string(JSON_Object *object, const char *name, const char *string) { 1722 | JSON_Value *value = json_value_init_string(string); 1723 | if (value == NULL) { 1724 | return JSONFailure; 1725 | } 1726 | if (json_object_dotset_value(object, name, value) == JSONFailure) { 1727 | json_value_free(value); 1728 | return JSONFailure; 1729 | } 1730 | return JSONSuccess; 1731 | } 1732 | 1733 | JSON_Status json_object_dotset_number(JSON_Object *object, const char *name, double number) { 1734 | JSON_Value *value = json_value_init_number(number); 1735 | if (value == NULL) { 1736 | return JSONFailure; 1737 | } 1738 | if (json_object_dotset_value(object, name, value) == JSONFailure) { 1739 | json_value_free(value); 1740 | return JSONFailure; 1741 | } 1742 | return JSONSuccess; 1743 | } 1744 | 1745 | JSON_Status json_object_dotset_boolean(JSON_Object *object, const char *name, int boolean) { 1746 | JSON_Value *value = json_value_init_boolean(boolean); 1747 | if (value == NULL) { 1748 | return JSONFailure; 1749 | } 1750 | if (json_object_dotset_value(object, name, value) == JSONFailure) { 1751 | json_value_free(value); 1752 | return JSONFailure; 1753 | } 1754 | return JSONSuccess; 1755 | } 1756 | 1757 | JSON_Status json_object_dotset_null(JSON_Object *object, const char *name) { 1758 | JSON_Value *value = json_value_init_null(); 1759 | if (value == NULL) { 1760 | return JSONFailure; 1761 | } 1762 | if (json_object_dotset_value(object, name, value) == JSONFailure) { 1763 | json_value_free(value); 1764 | return JSONFailure; 1765 | } 1766 | return JSONSuccess; 1767 | } 1768 | 1769 | JSON_Status json_object_remove(JSON_Object *object, const char *name) { 1770 | size_t i = 0, last_item_index = 0; 1771 | if (object == NULL || json_object_get_value(object, name) == NULL) { 1772 | return JSONFailure; 1773 | } 1774 | last_item_index = json_object_get_count(object) - 1; 1775 | for (i = 0; i < json_object_get_count(object); i++) { 1776 | if (strcmp(object->names[i], name) == 0) { 1777 | parson_free(object->names[i]); 1778 | json_value_free(object->values[i]); 1779 | if (i != last_item_index) { /* Replace key value pair with one from the end */ 1780 | object->names[i] = object->names[last_item_index]; 1781 | object->values[i] = object->values[last_item_index]; 1782 | } 1783 | object->count -= 1; 1784 | return JSONSuccess; 1785 | } 1786 | } 1787 | return JSONFailure; /* No execution path should end here */ 1788 | } 1789 | 1790 | JSON_Status json_object_dotremove(JSON_Object *object, const char *name) { 1791 | const char *dot_pos = strchr(name, '.'); 1792 | char *current_name = NULL; 1793 | JSON_Object *temp_obj = NULL; 1794 | if (dot_pos == NULL) { 1795 | return json_object_remove(object, name); 1796 | } else { 1797 | current_name = parson_strndup(name, dot_pos - name); 1798 | temp_obj = json_object_get_object(object, current_name); 1799 | if (temp_obj == NULL) { 1800 | parson_free(current_name); 1801 | return JSONFailure; 1802 | } 1803 | parson_free(current_name); 1804 | return json_object_dotremove(temp_obj, dot_pos + 1); 1805 | } 1806 | } 1807 | 1808 | JSON_Status json_object_clear(JSON_Object *object) { 1809 | size_t i = 0; 1810 | if (object == NULL) { 1811 | return JSONFailure; 1812 | } 1813 | for (i = 0; i < json_object_get_count(object); i++) { 1814 | parson_free(object->names[i]); 1815 | json_value_free(object->values[i]); 1816 | } 1817 | object->count = 0; 1818 | return JSONSuccess; 1819 | } 1820 | 1821 | JSON_Status json_validate(const JSON_Value *schema, const JSON_Value *value) { 1822 | JSON_Value *temp_schema_value = NULL, *temp_value = NULL; 1823 | JSON_Array *schema_array = NULL, *value_array = NULL; 1824 | JSON_Object *schema_object = NULL, *value_object = NULL; 1825 | JSON_Value_Type schema_type = JSONError, value_type = JSONError; 1826 | const char *key = NULL; 1827 | size_t i = 0, count = 0; 1828 | if (schema == NULL || value == NULL) { 1829 | return JSONFailure; 1830 | } 1831 | schema_type = json_value_get_type(schema); 1832 | value_type = json_value_get_type(value); 1833 | if (schema_type != value_type && schema_type != JSONNull) { /* null represents all values */ 1834 | return JSONFailure; 1835 | } 1836 | switch (schema_type) { 1837 | case JSONArray: 1838 | schema_array = json_value_get_array(schema); 1839 | value_array = json_value_get_array(value); 1840 | count = json_array_get_count(schema_array); 1841 | if (count == 0) { 1842 | return JSONSuccess; /* Empty array allows all types */ 1843 | } 1844 | /* Get first value from array, rest is ignored */ 1845 | temp_schema_value = json_array_get_value(schema_array, 0); 1846 | for (i = 0; i < json_array_get_count(value_array); i++) { 1847 | temp_value = json_array_get_value(value_array, i); 1848 | if (json_validate(temp_schema_value, temp_value) == JSONFailure) { 1849 | return JSONFailure; 1850 | } 1851 | } 1852 | return JSONSuccess; 1853 | case JSONObject: 1854 | schema_object = json_value_get_object(schema); 1855 | value_object = json_value_get_object(value); 1856 | count = json_object_get_count(schema_object); 1857 | if (count == 0) { 1858 | return JSONSuccess; /* Empty object allows all objects */ 1859 | } else if (json_object_get_count(value_object) < count) { 1860 | return JSONFailure; /* Tested object mustn't have less name-value pairs than schema */ 1861 | } 1862 | for (i = 0; i < count; i++) { 1863 | key = json_object_get_name(schema_object, i); 1864 | temp_schema_value = json_object_get_value(schema_object, key); 1865 | temp_value = json_object_get_value(value_object, key); 1866 | if (temp_value == NULL) { 1867 | return JSONFailure; 1868 | } 1869 | if (json_validate(temp_schema_value, temp_value) == JSONFailure) { 1870 | return JSONFailure; 1871 | } 1872 | } 1873 | return JSONSuccess; 1874 | case JSONString: case JSONNumber: case JSONBoolean: case JSONNull: 1875 | return JSONSuccess; /* equality already tested before switch */ 1876 | case JSONError: default: 1877 | return JSONFailure; 1878 | } 1879 | } 1880 | 1881 | JSON_Status json_value_equals(const JSON_Value *a, const JSON_Value *b) { 1882 | JSON_Object *a_object = NULL, *b_object = NULL; 1883 | JSON_Array *a_array = NULL, *b_array = NULL; 1884 | const char *a_string = NULL, *b_string = NULL; 1885 | const char *key = NULL; 1886 | size_t a_count = 0, b_count = 0, i = 0; 1887 | JSON_Value_Type a_type, b_type; 1888 | a_type = json_value_get_type(a); 1889 | b_type = json_value_get_type(b); 1890 | if (a_type != b_type) { 1891 | return 0; 1892 | } 1893 | switch (a_type) { 1894 | case JSONArray: 1895 | a_array = json_value_get_array(a); 1896 | b_array = json_value_get_array(b); 1897 | a_count = json_array_get_count(a_array); 1898 | b_count = json_array_get_count(b_array); 1899 | if (a_count != b_count) { 1900 | return 0; 1901 | } 1902 | for (i = 0; i < a_count; i++) { 1903 | if (!json_value_equals(json_array_get_value(a_array, i), 1904 | json_array_get_value(b_array, i))) { 1905 | return 0; 1906 | } 1907 | } 1908 | return 1; 1909 | case JSONObject: 1910 | a_object = json_value_get_object(a); 1911 | b_object = json_value_get_object(b); 1912 | a_count = json_object_get_count(a_object); 1913 | b_count = json_object_get_count(b_object); 1914 | if (a_count != b_count) { 1915 | return 0; 1916 | } 1917 | for (i = 0; i < a_count; i++) { 1918 | key = json_object_get_name(a_object, i); 1919 | if (!json_value_equals(json_object_get_value(a_object, key), 1920 | json_object_get_value(b_object, key))) { 1921 | return 0; 1922 | } 1923 | } 1924 | return 1; 1925 | case JSONString: 1926 | a_string = json_value_get_string(a); 1927 | b_string = json_value_get_string(b); 1928 | if (a_string == NULL || b_string == NULL) { 1929 | return 0; /* shouldn't happen */ 1930 | } 1931 | return strcmp(a_string, b_string) == 0; 1932 | case JSONBoolean: 1933 | return json_value_get_boolean(a) == json_value_get_boolean(b); 1934 | case JSONNumber: 1935 | return fabs(json_value_get_number(a) - json_value_get_number(b)) < 0.000001; /* EPSILON */ 1936 | case JSONError: 1937 | return 1; 1938 | case JSONNull: 1939 | return 1; 1940 | default: 1941 | return 1; 1942 | } 1943 | } 1944 | 1945 | JSON_Value_Type json_type(const JSON_Value *value) { 1946 | return json_value_get_type(value); 1947 | } 1948 | 1949 | JSON_Object * json_object (const JSON_Value *value) { 1950 | return json_value_get_object(value); 1951 | } 1952 | 1953 | JSON_Array * json_array (const JSON_Value *value) { 1954 | return json_value_get_array(value); 1955 | } 1956 | 1957 | const char * json_string (const JSON_Value *value) { 1958 | return json_value_get_string(value); 1959 | } 1960 | 1961 | double json_number (const JSON_Value *value) { 1962 | return json_value_get_number(value); 1963 | } 1964 | 1965 | int json_boolean(const JSON_Value *value) { 1966 | return json_value_get_boolean(value); 1967 | } 1968 | 1969 | void json_set_allocation_functions(JSON_Malloc_Function malloc_fun, JSON_Free_Function free_fun) { 1970 | parson_malloc = malloc_fun; 1971 | parson_free = free_fun; 1972 | } 1973 | -------------------------------------------------------------------------------- /source/parson.h: -------------------------------------------------------------------------------- 1 | /* 2 | Parson ( http://kgabis.github.com/parson/ ) 3 | Copyright (c) 2012 - 2016 Krzysztof Gabis 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | */ 23 | 24 | #ifndef parson_parson_h 25 | #define parson_parson_h 26 | 27 | #ifdef __cplusplus 28 | extern "C" 29 | { 30 | #endif 31 | 32 | #include /* size_t */ 33 | 34 | /* Types and enums */ 35 | typedef struct json_object_t JSON_Object; 36 | typedef struct json_array_t JSON_Array; 37 | typedef struct json_value_t JSON_Value; 38 | 39 | enum json_value_type { 40 | JSONError = -1, 41 | JSONNull = 1, 42 | JSONString = 2, 43 | JSONNumber = 3, 44 | JSONObject = 4, 45 | JSONArray = 5, 46 | JSONBoolean = 6 47 | }; 48 | typedef int JSON_Value_Type; 49 | 50 | enum json_result_t { 51 | JSONSuccess = 0, 52 | JSONFailure = -1 53 | }; 54 | typedef int JSON_Status; 55 | 56 | typedef void * (*JSON_Malloc_Function)(size_t); 57 | typedef void (*JSON_Free_Function)(void *); 58 | 59 | /* Call only once, before calling any other function from parson API. If not called, malloc and free 60 | from stdlib will be used for all allocations */ 61 | void json_set_allocation_functions(JSON_Malloc_Function malloc_fun, JSON_Free_Function free_fun); 62 | 63 | /* Parses first JSON value in a file, returns NULL in case of error */ 64 | JSON_Value * json_parse_file(const char *filename); 65 | 66 | /* Parses first JSON value in a file and ignores comments (/ * * / and //), 67 | returns NULL in case of error */ 68 | JSON_Value * json_parse_file_with_comments(const char *filename); 69 | 70 | /* Parses first JSON value in a string, returns NULL in case of error */ 71 | JSON_Value * json_parse_string(const char *string); 72 | 73 | /* Parses first JSON value in a string and ignores comments (/ * * / and //), 74 | returns NULL in case of error */ 75 | JSON_Value * json_parse_string_with_comments(const char *string); 76 | 77 | /* Serialization */ 78 | size_t json_serialization_size(const JSON_Value *value); /* returns 0 on fail */ 79 | JSON_Status json_serialize_to_buffer(const JSON_Value *value, char *buf, size_t buf_size_in_bytes); 80 | JSON_Status json_serialize_to_file(const JSON_Value *value, const char *filename); 81 | char * json_serialize_to_string(const JSON_Value *value); 82 | 83 | /* Pretty serialization */ 84 | size_t json_serialization_size_pretty(const JSON_Value *value); /* returns 0 on fail */ 85 | JSON_Status json_serialize_to_buffer_pretty(const JSON_Value *value, char *buf, size_t buf_size_in_bytes); 86 | JSON_Status json_serialize_to_file_pretty(const JSON_Value *value, const char *filename); 87 | char * json_serialize_to_string_pretty(const JSON_Value *value); 88 | 89 | void json_free_serialized_string(char *string); /* frees string from json_serialize_to_string and json_serialize_to_string_pretty */ 90 | 91 | /* Comparing */ 92 | int json_value_equals(const JSON_Value *a, const JSON_Value *b); 93 | 94 | /* Validation 95 | This is *NOT* JSON Schema. It validates json by checking if object have identically 96 | named fields with matching types. 97 | For example schema {"name":"", "age":0} will validate 98 | {"name":"Joe", "age":25} and {"name":"Joe", "age":25, "gender":"m"}, 99 | but not {"name":"Joe"} or {"name":"Joe", "age":"Cucumber"}. 100 | In case of arrays, only first value in schema is checked against all values in tested array. 101 | Empty objects ({}) validate all objects, empty arrays ([]) validate all arrays, 102 | null validates values of every type. 103 | */ 104 | JSON_Status json_validate(const JSON_Value *schema, const JSON_Value *value); 105 | 106 | /* 107 | * JSON Object 108 | */ 109 | JSON_Value * json_object_get_value (const JSON_Object *object, const char *name); 110 | const char * json_object_get_string (const JSON_Object *object, const char *name); 111 | JSON_Object * json_object_get_object (const JSON_Object *object, const char *name); 112 | JSON_Array * json_object_get_array (const JSON_Object *object, const char *name); 113 | double json_object_get_number (const JSON_Object *object, const char *name); /* returns 0 on fail */ 114 | int json_object_get_boolean(const JSON_Object *object, const char *name); /* returns -1 on fail */ 115 | 116 | /* dotget functions enable addressing values with dot notation in nested objects, 117 | just like in structs or c++/java/c# objects (e.g. objectA.objectB.value). 118 | Because valid names in JSON can contain dots, some values may be inaccessible 119 | this way. */ 120 | JSON_Value * json_object_dotget_value (const JSON_Object *object, const char *name); 121 | const char * json_object_dotget_string (const JSON_Object *object, const char *name); 122 | JSON_Object * json_object_dotget_object (const JSON_Object *object, const char *name); 123 | JSON_Array * json_object_dotget_array (const JSON_Object *object, const char *name); 124 | double json_object_dotget_number (const JSON_Object *object, const char *name); /* returns 0 on fail */ 125 | int json_object_dotget_boolean(const JSON_Object *object, const char *name); /* returns -1 on fail */ 126 | 127 | /* Functions to get available names */ 128 | size_t json_object_get_count (const JSON_Object *object); 129 | const char * json_object_get_name (const JSON_Object *object, size_t index); 130 | JSON_Value * json_object_get_value_at(const JSON_Object *object, size_t index); 131 | JSON_Value * json_object_get_wrapping_value(const JSON_Object *object); 132 | 133 | /* Functions to check if object has a value with a specific name. Returned value is 1 if object has 134 | * a value and 0 if it doesn't. dothas functions behave exactly like dotget functions. */ 135 | int json_object_has_value (const JSON_Object *object, const char *name); 136 | int json_object_has_value_of_type(const JSON_Object *object, const char *name, JSON_Value_Type type); 137 | 138 | int json_object_dothas_value (const JSON_Object *object, const char *name); 139 | int json_object_dothas_value_of_type(const JSON_Object *object, const char *name, JSON_Value_Type type); 140 | 141 | /* Creates new name-value pair or frees and replaces old value with a new one. 142 | * json_object_set_value does not copy passed value so it shouldn't be freed afterwards. */ 143 | JSON_Status json_object_set_value(JSON_Object *object, const char *name, JSON_Value *value); 144 | JSON_Status json_object_set_string(JSON_Object *object, const char *name, const char *string); 145 | JSON_Status json_object_set_number(JSON_Object *object, const char *name, double number); 146 | JSON_Status json_object_set_boolean(JSON_Object *object, const char *name, int boolean); 147 | JSON_Status json_object_set_null(JSON_Object *object, const char *name); 148 | 149 | /* Works like dotget functions, but creates whole hierarchy if necessary. 150 | * json_object_dotset_value does not copy passed value so it shouldn't be freed afterwards. */ 151 | JSON_Status json_object_dotset_value(JSON_Object *object, const char *name, JSON_Value *value); 152 | JSON_Status json_object_dotset_string(JSON_Object *object, const char *name, const char *string); 153 | JSON_Status json_object_dotset_number(JSON_Object *object, const char *name, double number); 154 | JSON_Status json_object_dotset_boolean(JSON_Object *object, const char *name, int boolean); 155 | JSON_Status json_object_dotset_null(JSON_Object *object, const char *name); 156 | 157 | /* Frees and removes name-value pair */ 158 | JSON_Status json_object_remove(JSON_Object *object, const char *name); 159 | 160 | /* Works like dotget function, but removes name-value pair only on exact match. */ 161 | JSON_Status json_object_dotremove(JSON_Object *object, const char *key); 162 | 163 | /* Removes all name-value pairs in object */ 164 | JSON_Status json_object_clear(JSON_Object *object); 165 | 166 | /* 167 | *JSON Array 168 | */ 169 | JSON_Value * json_array_get_value (const JSON_Array *array, size_t index); 170 | const char * json_array_get_string (const JSON_Array *array, size_t index); 171 | JSON_Object * json_array_get_object (const JSON_Array *array, size_t index); 172 | JSON_Array * json_array_get_array (const JSON_Array *array, size_t index); 173 | double json_array_get_number (const JSON_Array *array, size_t index); /* returns 0 on fail */ 174 | int json_array_get_boolean(const JSON_Array *array, size_t index); /* returns -1 on fail */ 175 | size_t json_array_get_count (const JSON_Array *array); 176 | JSON_Value * json_array_get_wrapping_value(const JSON_Array *array); 177 | 178 | /* Frees and removes value at given index, does nothing and returns JSONFailure if index doesn't exist. 179 | * Order of values in array may change during execution. */ 180 | JSON_Status json_array_remove(JSON_Array *array, size_t i); 181 | 182 | /* Frees and removes from array value at given index and replaces it with given one. 183 | * Does nothing and returns JSONFailure if index doesn't exist. 184 | * json_array_replace_value does not copy passed value so it shouldn't be freed afterwards. */ 185 | JSON_Status json_array_replace_value(JSON_Array *array, size_t i, JSON_Value *value); 186 | JSON_Status json_array_replace_string(JSON_Array *array, size_t i, const char* string); 187 | JSON_Status json_array_replace_number(JSON_Array *array, size_t i, double number); 188 | JSON_Status json_array_replace_boolean(JSON_Array *array, size_t i, int boolean); 189 | JSON_Status json_array_replace_null(JSON_Array *array, size_t i); 190 | 191 | /* Frees and removes all values from array */ 192 | JSON_Status json_array_clear(JSON_Array *array); 193 | 194 | /* Appends new value at the end of array. 195 | * json_array_append_value does not copy passed value so it shouldn't be freed afterwards. */ 196 | JSON_Status json_array_append_value(JSON_Array *array, JSON_Value *value); 197 | JSON_Status json_array_append_string(JSON_Array *array, const char *string); 198 | JSON_Status json_array_append_number(JSON_Array *array, double number); 199 | JSON_Status json_array_append_boolean(JSON_Array *array, int boolean); 200 | JSON_Status json_array_append_null(JSON_Array *array); 201 | 202 | /* 203 | *JSON Value 204 | */ 205 | JSON_Value * json_value_init_object (void); 206 | JSON_Value * json_value_init_array (void); 207 | JSON_Value * json_value_init_string (const char *string); /* copies passed string */ 208 | JSON_Value * json_value_init_number (double number); 209 | JSON_Value * json_value_init_boolean(int boolean); 210 | JSON_Value * json_value_init_null (void); 211 | JSON_Value * json_value_deep_copy (const JSON_Value *value); 212 | void json_value_free (JSON_Value *value); 213 | 214 | JSON_Value_Type json_value_get_type (const JSON_Value *value); 215 | JSON_Object * json_value_get_object (const JSON_Value *value); 216 | JSON_Array * json_value_get_array (const JSON_Value *value); 217 | const char * json_value_get_string (const JSON_Value *value); 218 | double json_value_get_number (const JSON_Value *value); 219 | int json_value_get_boolean(const JSON_Value *value); 220 | JSON_Value * json_value_get_parent (const JSON_Value *value); 221 | 222 | /* Same as above, but shorter */ 223 | JSON_Value_Type json_type (const JSON_Value *value); 224 | JSON_Object * json_object (const JSON_Value *value); 225 | JSON_Array * json_array (const JSON_Value *value); 226 | const char * json_string (const JSON_Value *value); 227 | double json_number (const JSON_Value *value); 228 | int json_boolean(const JSON_Value *value); 229 | 230 | #ifdef __cplusplus 231 | } 232 | #endif 233 | 234 | #endif 235 | -------------------------------------------------------------------------------- /source/ticket.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ticket.c 3 | * by Reisyukaku 4 | * 5 | * Handles tickets and things. 6 | */ 7 | 8 | #include "ticket.h" 9 | 10 | void BuildCetk(unsigned char *out, const u8 *sig, const unsigned long long titleID, const u8 *tikID, const u8 *encKey, const u8 *eccPubKey, const u16 version){ 11 | Cetk *tik = calloc(1, sizeof(Cetk)); 12 | tik->sigType = 0x04000100U; 13 | memcpy(&tik->signature, sig, 0x13C); 14 | u64 tidBE = ENDIAN_SWAP_u64(titleID); 15 | 16 | TikData *tikData = calloc(1, sizeof(TikData)); 17 | memcpy(&tikData->issuer, "Root-CA00000003-XS0000000c", 0x1B); 18 | memcpy(&tikData->eccPubKey, eccPubKey, 0x3C); 19 | tikData->version = 1; 20 | tikData->tikTitleVersion = ((version & 0xFF00) >> 8) | (version << 8); 21 | tikData->audit = 1; 22 | memcpy(&tikData->contIndex, contIndex, 0xAC); //Get contindex 23 | memcpy(&tikData->titleKey, encKey, 0x10); 24 | memcpy(&tikData->titleID, &tidBE, 8); 25 | memcpy(&tikData->ticketID, tikID, 8); 26 | tikData->commKeyIndex = 0; 27 | tik->ticketData = *tikData; 28 | memcpy(&tik->chainSig, chainSig, 0x700); 29 | 30 | memcpy(out, tik, sizeof(Cetk)); 31 | free(tikData); 32 | free(tik); 33 | } -------------------------------------------------------------------------------- /source/ticket.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ticket.h 3 | * by Reisyukaku 4 | */ 5 | 6 | #ifndef INC_TICKET 7 | #define INC_TICKET 8 | 9 | #include <3ds.h> 10 | #include 11 | #include 12 | #include 13 | 14 | #define ENDIAN_SWAP_u64(val) ((u64) ( \ 15 | (((u64) (val) & (u64) 0x00000000000000ff) << 56) | \ 16 | (((u64) (val) & (u64) 0x000000000000ff00) << 40) | \ 17 | (((u64) (val) & (u64) 0x0000000000ff0000) << 24) | \ 18 | (((u64) (val) & (u64) 0x00000000ff000000) << 8) | \ 19 | (((u64) (val) & (u64) 0x000000ff00000000) >> 8) | \ 20 | (((u64) (val) & (u64) 0x0000ff0000000000) >> 24) | \ 21 | (((u64) (val) & (u64) 0x00ff000000000000) >> 40) | \ 22 | (((u64) (val) & (u64) 0xff00000000000000) >> 56))) 23 | 24 | typedef struct{ 25 | u8 issuer[0x40]; 26 | u8 eccPubKey[0x3C]; 27 | u8 version; 28 | u8 CaCrlVersion; 29 | u8 SigCrlVersion; 30 | u8 titleKey[0x10]; 31 | u8 reserved; 32 | u8 ticketID[8]; 33 | u8 consoleID[4]; 34 | u8 titleID[8]; 35 | u8 reserved2[2]; 36 | u16 tikTitleVersion; 37 | u8 reserved3[8]; 38 | u8 licType; 39 | u8 commKeyIndex; 40 | u8 reserved4[0x2A]; 41 | u8 accntID[4]; 42 | u8 reserved5; 43 | u8 audit; 44 | u8 reserved6[0x42]; 45 | u8 limits[0x40]; 46 | u8 contIndex[0xAC]; 47 | } TikData; 48 | 49 | typedef struct{ 50 | unsigned sigType; 51 | u8 signature[0x13C]; 52 | TikData ticketData; 53 | u8 chainSig[0x700]; 54 | } Cetk; 55 | 56 | static unsigned char contIndex[0xAC] = { 57 | 0x00, 0x01, 0x00, 0x14, 0x00, 0x00, 0x00, 0xAC, 0x00, 0x00, 0x00, 0x14, 58 | 0x00, 0x01, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x28, 59 | 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x84, 0x00, 0x00, 0x00, 0x84, 60 | 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 61 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 62 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 63 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 64 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 65 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 66 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 67 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 68 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 69 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 70 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 71 | 0x00, 0x00, 0x00, 0x00 72 | }; 73 | 74 | static unsigned char chainSig[0x700] = { 75 | 0x00, 0x01, 0x00, 0x04, 0x63, 0x80, 0x5A, 0x35, 0x1A, 0x43, 0x7B, 0xA2, 76 | 0x43, 0x19, 0xBB, 0x3A, 0x77, 0x7B, 0x7A, 0xF3, 0x5E, 0x72, 0x4B, 0x15, 77 | 0x0A, 0x06, 0x39, 0x6C, 0x5F, 0xEC, 0x38, 0x45, 0xB1, 0x88, 0x76, 0x26, 78 | 0x8D, 0x5E, 0xDA, 0xE6, 0x2F, 0x14, 0xBA, 0x02, 0xFA, 0xD6, 0xFC, 0x3B, 79 | 0x2B, 0xBE, 0x87, 0x07, 0x63, 0x8E, 0x55, 0xBF, 0x05, 0x5A, 0xFC, 0xFC, 80 | 0xB3, 0x47, 0x69, 0x11, 0x89, 0xDB, 0x1C, 0xAF, 0x4B, 0x43, 0x76, 0x62, 81 | 0x3E, 0x30, 0x89, 0x0A, 0x9D, 0x3B, 0xBB, 0x3E, 0x50, 0xBD, 0xF7, 0xA6, 82 | 0xC0, 0xF7, 0xF8, 0xBB, 0x0D, 0xB5, 0x6A, 0xBB, 0xC6, 0xC3, 0x50, 0xC8, 83 | 0x88, 0xBB, 0x9D, 0xF0, 0x9B, 0xD1, 0x30, 0x64, 0x60, 0x69, 0xDD, 0x34, 84 | 0x67, 0xA7, 0x00, 0xEB, 0xDC, 0xF9, 0x8C, 0xB0, 0xF7, 0x93, 0x0E, 0x81, 85 | 0xFE, 0x98, 0xD9, 0x72, 0x45, 0x8B, 0x94, 0x7E, 0x59, 0xE2, 0xBE, 0x4E, 86 | 0x91, 0x2D, 0x75, 0xCA, 0x1B, 0x8E, 0x2E, 0xF4, 0x6D, 0x73, 0xB1, 0x6B, 87 | 0x35, 0xB5, 0x67, 0x0D, 0x63, 0x2D, 0x51, 0x38, 0x53, 0x28, 0x19, 0x1D, 88 | 0x9D, 0xAE, 0x8D, 0xC6, 0x61, 0xCC, 0xEF, 0xA4, 0xAB, 0xE2, 0xF3, 0xB0, 89 | 0x4C, 0x7B, 0xE2, 0x71, 0xB5, 0xF9, 0x2C, 0xFA, 0x55, 0xCD, 0x88, 0x8B, 90 | 0x72, 0xCC, 0xBE, 0x67, 0xFA, 0xDF, 0xEF, 0x6B, 0x53, 0x3C, 0x45, 0xD8, 91 | 0xCB, 0xDF, 0xB2, 0x76, 0x41, 0x46, 0xD6, 0xC2, 0x6F, 0x27, 0x16, 0xC5, 92 | 0x07, 0xF3, 0xF4, 0x44, 0x66, 0xA3, 0x15, 0xD2, 0x77, 0xF2, 0x89, 0xDA, 93 | 0xFD, 0xD5, 0x50, 0xCF, 0xA4, 0x9B, 0xEA, 0xCA, 0xC9, 0x7B, 0xE5, 0x46, 94 | 0x0E, 0xED, 0x9B, 0xFB, 0x04, 0xA9, 0xDA, 0x19, 0x58, 0xD9, 0x2A, 0x20, 95 | 0x8A, 0xAC, 0xC1, 0xF4, 0x8E, 0xE9, 0x14, 0xD8, 0x8A, 0xD7, 0x41, 0xD5, 96 | 0x5B, 0x9B, 0x64, 0x22, 0xD8, 0xAF, 0xAE, 0xC7, 0x00, 0x00, 0x00, 0x00, 97 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 98 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 99 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 100 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 101 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x52, 0x6F, 0x6F, 0x74, 102 | 0x2D, 0x43, 0x41, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x34, 0x00, 103 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 104 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 105 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 106 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 107 | 0x00, 0x00, 0x00, 0x01, 0x58, 0x53, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 108 | 0x30, 0x39, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 109 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 110 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 111 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 112 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0xA3, 0x47, 0xA4, 113 | 0xC0, 0x84, 0x4C, 0xEB, 0x7E, 0xB0, 0xCF, 0xF0, 0xAE, 0xB7, 0x77, 0x69, 114 | 0x85, 0x93, 0xE4, 0x99, 0x5A, 0x95, 0x4E, 0x58, 0x17, 0x38, 0xCE, 0xD6, 115 | 0x81, 0xB0, 0xBD, 0x77, 0x09, 0xE7, 0xF8, 0x9A, 0xDF, 0xAD, 0x05, 0x48, 116 | 0x83, 0xF6, 0xC3, 0xFD, 0xDF, 0x7B, 0x83, 0xE0, 0x0C, 0x26, 0x81, 0x54, 117 | 0x43, 0x29, 0xEA, 0x82, 0x6C, 0x89, 0xF0, 0xA6, 0x74, 0x42, 0x86, 0x4D, 118 | 0x32, 0x60, 0x32, 0x7D, 0xA7, 0x7A, 0x13, 0x40, 0x66, 0x59, 0xDA, 0x3E, 119 | 0x41, 0x6B, 0x27, 0x94, 0x03, 0x4F, 0xAA, 0x22, 0x9D, 0xD5, 0x54, 0x52, 120 | 0xDB, 0x27, 0x0A, 0x6A, 0xA2, 0x3D, 0x19, 0xB1, 0x66, 0x1B, 0x19, 0x7D, 121 | 0xAB, 0xC7, 0x0E, 0x88, 0x17, 0x91, 0xA1, 0x2A, 0xB4, 0x3C, 0x6C, 0xCB, 122 | 0xF5, 0xAA, 0x7C, 0x3A, 0xDD, 0x36, 0xFB, 0x35, 0x71, 0x7B, 0x20, 0x01, 123 | 0x59, 0x00, 0xD6, 0xF6, 0x90, 0x39, 0x35, 0x41, 0x31, 0xF8, 0xC1, 0xC0, 124 | 0x57, 0x3A, 0x35, 0x18, 0x58, 0x90, 0xB1, 0xAD, 0x9A, 0x0E, 0xEC, 0xE0, 125 | 0xF4, 0x7A, 0x7D, 0xA5, 0x27, 0x48, 0xC9, 0x72, 0xAB, 0x0D, 0x08, 0x7B, 126 | 0x62, 0x35, 0x40, 0x91, 0x14, 0x2B, 0xB1, 0x1D, 0x1A, 0xFA, 0xF9, 0xCD, 127 | 0x5C, 0x17, 0x13, 0x53, 0x52, 0x71, 0xCA, 0xE2, 0x2A, 0x78, 0xB1, 0x7F, 128 | 0x4A, 0xCD, 0x59, 0xD8, 0xBA, 0x1D, 0x7D, 0x70, 0x5F, 0x78, 0x1B, 0x9F, 129 | 0x9D, 0x37, 0x18, 0x8E, 0xD7, 0xCD, 0x0D, 0x49, 0x57, 0x74, 0x69, 0x88, 130 | 0x3A, 0x6B, 0x8E, 0x4E, 0x1B, 0x85, 0xDD, 0xBE, 0x39, 0x45, 0x05, 0x89, 131 | 0x56, 0x12, 0x97, 0x59, 0x9A, 0x09, 0xA4, 0xC8, 0x2D, 0x2F, 0xF5, 0xCF, 132 | 0xB4, 0x73, 0x70, 0xDB, 0x58, 0x1E, 0xB2, 0x4E, 0x77, 0x6F, 0xA4, 0x7E, 133 | 0x62, 0xDF, 0xB7, 0x05, 0xE8, 0x80, 0x42, 0x5C, 0xB8, 0x78, 0x87, 0x97, 134 | 0x7F, 0x66, 0x2C, 0x5F, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 135 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 136 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 137 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 138 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 139 | 0x00, 0x01, 0x00, 0x03, 0x19, 0x49, 0x42, 0x9D, 0x1E, 0x58, 0xA6, 0x2E, 140 | 0x7E, 0x8B, 0x56, 0xD1, 0xB7, 0x6A, 0xE3, 0x02, 0xFD, 0x8B, 0x97, 0x49, 141 | 0x1F, 0x77, 0x87, 0x45, 0xF7, 0x53, 0x88, 0xC4, 0xDD, 0x0B, 0xEB, 0x1D, 142 | 0xF1, 0x22, 0xFB, 0x96, 0x42, 0x15, 0x14, 0x97, 0x76, 0x4A, 0x53, 0xCF, 143 | 0x78, 0x15, 0x18, 0x45, 0xE4, 0x2C, 0xA8, 0xFD, 0xE4, 0x86, 0xFD, 0x2A, 144 | 0x4F, 0x53, 0xF8, 0xA1, 0xBA, 0x00, 0x8A, 0x74, 0x85, 0xFF, 0x73, 0xB3, 145 | 0xBF, 0x7E, 0x3C, 0x98, 0x07, 0x29, 0xD0, 0x65, 0x6B, 0x69, 0x32, 0x19, 146 | 0xAD, 0xE8, 0x35, 0xEB, 0x5F, 0xFF, 0xFC, 0xCB, 0x7C, 0xBB, 0x5E, 0x30, 147 | 0x7F, 0xE0, 0x68, 0x8B, 0x88, 0x8E, 0xF2, 0xD2, 0x05, 0x3F, 0xB7, 0xE7, 148 | 0x91, 0xE9, 0x85, 0xFD, 0x15, 0xEF, 0x10, 0xD7, 0x9C, 0xCA, 0x88, 0xD6, 149 | 0xBB, 0x15, 0xE8, 0xE4, 0x71, 0x4A, 0x98, 0xEE, 0x09, 0xBF, 0x7B, 0x8A, 150 | 0xF0, 0x53, 0x23, 0x2B, 0x64, 0x50, 0xE6, 0xD5, 0xFD, 0xFF, 0xC2, 0x0A, 151 | 0x6D, 0x1E, 0xA6, 0xA2, 0x38, 0x12, 0xE1, 0x01, 0x45, 0x25, 0xD5, 0x6D, 152 | 0x40, 0x82, 0x70, 0x3B, 0x86, 0x98, 0x69, 0x59, 0xA7, 0x3C, 0xD1, 0xA1, 153 | 0x43, 0x64, 0xD2, 0xC2, 0xDA, 0xEA, 0x96, 0xB0, 0x95, 0xF7, 0x6C, 0x46, 154 | 0xE4, 0xFF, 0x41, 0x55, 0x46, 0x5E, 0x70, 0xEF, 0x1E, 0xD3, 0x10, 0x53, 155 | 0xD9, 0x70, 0x11, 0xE0, 0x10, 0xCC, 0x93, 0xE7, 0x91, 0x40, 0x13, 0x68, 156 | 0x7F, 0xA3, 0xA8, 0x02, 0x99, 0x6D, 0x1E, 0x55, 0x7B, 0x1C, 0xCC, 0x7A, 157 | 0x7E, 0x8F, 0x58, 0x65, 0xC1, 0x74, 0x2E, 0x28, 0xE2, 0x6D, 0xEF, 0x38, 158 | 0xA9, 0x3A, 0xB5, 0xD8, 0x2D, 0x43, 0xEC, 0xCC, 0xBF, 0x0B, 0xEF, 0x22, 159 | 0xE1, 0xFD, 0x57, 0xE2, 0x86, 0x43, 0x33, 0x58, 0x2F, 0xED, 0xEA, 0xBC, 160 | 0x01, 0x2F, 0x98, 0x6D, 0xDF, 0xC3, 0xE9, 0x44, 0x79, 0x73, 0x47, 0x03, 161 | 0x08, 0x45, 0x5B, 0xDC, 0x57, 0xAA, 0x17, 0x0B, 0x84, 0x42, 0x7F, 0x73, 162 | 0xA2, 0x9B, 0x48, 0xF6, 0xDA, 0x13, 0x5F, 0x66, 0xC7, 0x45, 0xC1, 0x42, 163 | 0xA8, 0x4A, 0xFB, 0x0E, 0x6A, 0x5E, 0xED, 0x85, 0xD7, 0xB9, 0x71, 0x99, 164 | 0x36, 0xF8, 0xCE, 0x2B, 0x62, 0x1F, 0x39, 0x5F, 0x40, 0xDC, 0x03, 0xBE, 165 | 0xF8, 0x85, 0x4C, 0x11, 0x17, 0xFF, 0x0C, 0x12, 0x86, 0x41, 0xCC, 0x78, 166 | 0x43, 0xB9, 0x7B, 0x43, 0x46, 0xDB, 0x22, 0x6F, 0x60, 0x26, 0xAC, 0xB5, 167 | 0x6C, 0x27, 0x8B, 0x8E, 0x0E, 0xA7, 0x9A, 0x2D, 0x65, 0xEF, 0x79, 0x8E, 168 | 0x10, 0x78, 0xAD, 0x80, 0xED, 0x4B, 0x96, 0x04, 0xD2, 0xF0, 0x8B, 0x2C, 169 | 0xD6, 0x4A, 0x23, 0xA3, 0xDB, 0x27, 0x08, 0x33, 0xB4, 0x02, 0xF8, 0x08, 170 | 0x51, 0xF3, 0x5B, 0xED, 0x3E, 0xE4, 0x57, 0x7C, 0x66, 0x60, 0xFB, 0xF1, 171 | 0x6D, 0x94, 0x13, 0xE0, 0x9C, 0x91, 0x7A, 0x49, 0xD4, 0x2C, 0x6D, 0xA3, 172 | 0x75, 0xBC, 0x27, 0xF0, 0x23, 0x0D, 0xB9, 0x8F, 0x89, 0x73, 0xAB, 0x02, 173 | 0x7B, 0x52, 0x2C, 0xD5, 0x7E, 0xC0, 0x3D, 0x25, 0xE8, 0xB3, 0xFC, 0x34, 174 | 0x94, 0xC9, 0x7F, 0xB1, 0x08, 0xFE, 0x18, 0xC6, 0x8A, 0x43, 0x36, 0xE4, 175 | 0x6C, 0x26, 0xB6, 0xF2, 0x80, 0xD2, 0x7E, 0x34, 0xBE, 0x28, 0x7C, 0x3E, 176 | 0x46, 0x87, 0xBC, 0x9D, 0x77, 0x6B, 0x76, 0xD9, 0x28, 0xD1, 0xB6, 0x35, 177 | 0x2E, 0xC0, 0x34, 0x7D, 0x72, 0x94, 0xAA, 0x93, 0x60, 0x26, 0x8D, 0x26, 178 | 0xF5, 0xF6, 0x52, 0x06, 0x4A, 0xF2, 0x40, 0xD7, 0xD0, 0x0C, 0x7C, 0x5E, 179 | 0xA3, 0xC3, 0x2D, 0xE6, 0x2D, 0x9B, 0x5C, 0x4B, 0x4C, 0xAB, 0x6F, 0xD7, 180 | 0xBD, 0x37, 0x1D, 0x57, 0xC2, 0x16, 0x60, 0x95, 0x91, 0x0E, 0x4A, 0xD8, 181 | 0xE9, 0xED, 0x18, 0x1E, 0xF7, 0x61, 0x93, 0x61, 0x53, 0x89, 0x2D, 0x77, 182 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 183 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 184 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 185 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 186 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 187 | 0x52, 0x6F, 0x6F, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 188 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 189 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 190 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 191 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 192 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x43, 0x41, 0x30, 0x30, 193 | 0x30, 0x30, 0x30, 0x30, 0x30, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 194 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 195 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 196 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 197 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 198 | 0x81, 0x12, 0x2A, 0x46, 0xC9, 0xCC, 0x2D, 0xC4, 0xDF, 0x29, 0x30, 0xE4, 199 | 0xDF, 0x3F, 0x8C, 0x70, 0xA0, 0x78, 0x94, 0x87, 0x75, 0xAD, 0x5E, 0x9A, 200 | 0xA6, 0x04, 0xC5, 0xB4, 0xD8, 0xEA, 0xFF, 0x2A, 0xA1, 0xD2, 0x14, 0x67, 201 | 0x65, 0x64, 0xEF, 0xCA, 0x28, 0xCC, 0x00, 0x15, 0x45, 0x54, 0xA1, 0xA3, 202 | 0xEA, 0x13, 0x79, 0xE9, 0xE6, 0xCA, 0xAC, 0xED, 0x15, 0x93, 0xFE, 0x88, 203 | 0xD8, 0x9A, 0xC6, 0xB8, 0xAC, 0xCC, 0xAB, 0x6E, 0x20, 0x7C, 0xEB, 0x7C, 204 | 0xCA, 0x29, 0x80, 0x9E, 0x29, 0x80, 0x44, 0x06, 0x62, 0xB7, 0xD4, 0x38, 205 | 0x2A, 0x15, 0xDA, 0x43, 0x08, 0x57, 0x45, 0xA9, 0xAA, 0xE5, 0x9A, 0xA0, 206 | 0x5B, 0xDB, 0x32, 0xF6, 0x68, 0x69, 0xA2, 0xDD, 0x42, 0x95, 0x38, 0x6C, 207 | 0x87, 0xEC, 0xDD, 0x35, 0x08, 0xA2, 0xCF, 0x60, 0xD0, 0x1E, 0x23, 0xEC, 208 | 0x2F, 0xE6, 0x98, 0xF4, 0x70, 0xD6, 0x00, 0x15, 0x49, 0xA2, 0xF0, 0x67, 209 | 0x59, 0x13, 0x1E, 0x53, 0x4C, 0x70, 0x06, 0x05, 0x7D, 0xEF, 0x1D, 0x18, 210 | 0xA8, 0x3F, 0x0A, 0xC7, 0x9C, 0xFE, 0x80, 0xFF, 0x5A, 0x91, 0xF2, 0xBE, 211 | 0xD4, 0xA0, 0x83, 0x70, 0x61, 0x19, 0x0A, 0x03, 0x29, 0x90, 0x21, 0x65, 212 | 0x40, 0x3C, 0x9A, 0x90, 0x8F, 0xB6, 0x15, 0x73, 0x9F, 0x3C, 0xE3, 0x3B, 213 | 0xF1, 0xBA, 0xEA, 0x16, 0xC2, 0x5B, 0xCE, 0xD7, 0x96, 0x3F, 0xAC, 0xC9, 214 | 0xD2, 0x4D, 0x9C, 0x0A, 0xD7, 0x6F, 0xC0, 0x20, 0xB2, 0xC4, 0xB8, 0x4C, 215 | 0x10, 0xA7, 0x41, 0xA2, 0xCC, 0x7D, 0x9B, 0xAC, 0x3A, 0xAC, 0xCC, 0xA3, 216 | 0x52, 0x9B, 0xAC, 0x31, 0x6A, 0x9A, 0xA7, 0x5D, 0x2A, 0x26, 0xC7, 0xD7, 217 | 0xD2, 0x88, 0xCB, 0xA4, 0x66, 0xC5, 0xFE, 0x5F, 0x45, 0x4A, 0xE6, 0x79, 218 | 0x74, 0x4A, 0x90, 0xA1, 0x57, 0x72, 0xDB, 0x3B, 0x0E, 0x47, 0xA4, 0x9A, 219 | 0xF0, 0x31, 0xD1, 0x6D, 0xBE, 0xAB, 0x33, 0x2B, 0x00, 0x01, 0x00, 0x01, 220 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 221 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 222 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 223 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 224 | 0x00, 0x00, 0x00, 0x00 225 | }; 226 | 227 | void BuildCetk(unsigned char *out, const u8 *sig, const unsigned long long titleID, const u8 *tikID, const u8 *encKey, const u8 *eccPubKey, const u16 version); 228 | 229 | #endif -------------------------------------------------------------------------------- /source/titles.c: -------------------------------------------------------------------------------- 1 | /* 2 | * titlekeys.c 3 | * by Reisyukaku 4 | */ 5 | #include <3ds.h> 6 | #include 7 | #include 8 | #include 9 | #include "parson.h" 10 | #include "titles.h" 11 | 12 | static Result downloadTitleKeys(u64 tid, char **json){ 13 | httpcContext ctx; 14 | Result res = 0; 15 | u32 status = 0; 16 | bool require_close = false; 17 | u32 nr_total; 18 | u32 nr = 0; 19 | size_t allocated; 20 | unsigned char *buf = NULL; 21 | unsigned char *newbuf = NULL; 22 | 23 | if(R_FAILED(res = httpcOpenContext(&ctx, HTTPC_METHOD_GET, "http://3ds.titlekeys.gq/json_enc", 1))) goto clean; 24 | require_close = true; 25 | 26 | if(R_FAILED(res = httpcSetSSLOpt(&ctx, SSLCOPT_DisableVerify))) goto clean; 27 | if(R_FAILED(res = httpcAddRequestHeaderField(&ctx, "User-Agent", "nimtest/1.0"))) goto clean; 28 | if(R_FAILED(res = httpcSetKeepAlive(&ctx, HTTPC_KEEPALIVE_DISABLED))) goto clean; 29 | 30 | if(R_FAILED(res = httpcBeginRequest(&ctx))) goto clean; 31 | if(R_FAILED(res = httpcGetResponseStatusCodeTimeout(&ctx, &status, 30000000000))) goto clean;//30s timeout 32 | 33 | if(status != 200){ 34 | res = tkey_FileNotFoundResult; 35 | goto clean; 36 | } 37 | 38 | if((buf = malloc(0x1000)) == NULL){ 39 | res = tkey_OutOfMemoryResult; 40 | goto clean; 41 | } 42 | allocated = 0x1000; 43 | 44 | nr_total = 0; 45 | 46 | do{ 47 | res = httpcDownloadData(&ctx, buf+nr_total, 0x1000, &nr); 48 | nr_total += nr; 49 | 50 | if(res == HTTPC_RESULTCODE_DOWNLOADPENDING){ 51 | if((newbuf = realloc(buf, nr_total + 0x1000)) == NULL){ 52 | res = tkey_OutOfMemoryResult; 53 | goto clean; 54 | } 55 | buf = newbuf; 56 | allocated += 0x1000; 57 | } 58 | }while(res == HTTPC_RESULTCODE_DOWNLOADPENDING); 59 | 60 | if(R_FAILED(res)) goto clean; 61 | 62 | if(nr_total == allocated){//no space for the '\0' that parson requires 63 | if((newbuf = realloc(buf, nr_total + 1)) == NULL){ 64 | res = tkey_OutOfMemoryResult; 65 | goto clean; 66 | } 67 | buf = newbuf; 68 | } 69 | 70 | buf[nr_total] = '\0'; 71 | 72 | clean: 73 | if(require_close) httpcCloseContext(&ctx); 74 | 75 | if(R_FAILED(res)){ 76 | if(buf) free(buf); 77 | }else{ 78 | *json = (char *)buf; 79 | } 80 | 81 | return res; 82 | } 83 | 84 | static inline bool is_valid_hex_char(int c) 85 | { 86 | return ( 87 | (c >= 'A' && c <= 'F') || 88 | (c >= 'a' && c <= 'f') || 89 | (c >= '0' && c <= '9') 90 | ); 91 | } 92 | 93 | static inline u8 get_hex_char_value(int c) 94 | { 95 | if(c >= 'A' && c <= 'F'){ 96 | return (u8)((c - 'A') + 0xA); 97 | }else if (c >= 'a' && c <= 'f'){ 98 | return (u8)((c - 'a') + 0xA); 99 | }else{ 100 | return (u8)(c - '0'); 101 | } 102 | } 103 | 104 | static int parse_hex(const char *in, unsigned char *out, size_t outlen) 105 | { 106 | size_t inlen; 107 | 108 | if((inlen = strlen(in)) & 1) return -1; 109 | if(outlen < inlen / 2) return -1; 110 | 111 | for(size_t i = 0; i < inlen; i += 2){ 112 | if (!is_valid_hex_char(in[i]) || !is_valid_hex_char(in[i + 1])) return -1; 113 | 114 | out[i / 2] = (unsigned char)((get_hex_char_value(in[i]) << 4) | get_hex_char_value(in[i + 1])); 115 | } 116 | 117 | return 0; 118 | } 119 | 120 | static Result getTitleKeyFromJSON(u64 tid, char *buf, unsigned char *key){ 121 | Result res = 0; 122 | JSON_Value *root; 123 | JSON_Array *keyinfos; 124 | JSON_Object *title; 125 | u64 tmptid; 126 | 127 | root = json_parse_string(buf); 128 | 129 | if((keyinfos = json_array(root)) == NULL){ 130 | res = tkey_JSONParseFailureResult; 131 | goto clean; 132 | } 133 | 134 | for(size_t i = 0; i < json_array_get_count(keyinfos); ++i){ 135 | //skipping error checking for performance 136 | title = json_array_get_object(keyinfos, i); 137 | tmptid = strtoull(json_object_get_string(title, "titleID"), NULL, 16); 138 | if(tmptid != tid) 139 | continue; 140 | 141 | if(parse_hex(json_object_get_string(title, "encTitleKey"), key, 0x10) != 0){ 142 | res = tkey_JSONMissingTitleKeyResult; 143 | goto clean; 144 | } 145 | 146 | goto clean; //success 147 | } 148 | 149 | res = tkey_TitleKeyNotInDBResult; //failure 150 | 151 | clean: 152 | json_value_free(root); 153 | 154 | return res; 155 | } 156 | 157 | Result getTitleKey(u64 tid, unsigned char *key){ 158 | Result res = 0; 159 | char *buf = NULL; 160 | 161 | if((R_FAILED(res = downloadTitleKeys(tid, &buf)))) goto clean; 162 | if((R_FAILED(res = getTitleKeyFromJSON(tid, buf, key)))) goto clean; 163 | 164 | clean: 165 | if(buf) free(buf); 166 | 167 | return res; 168 | } 169 | 170 | //this is fucking dumb, downloading a tmd to download a tmd, but registertask requires the correct tmd version 171 | Result getTitleVersion(u64 tid, u16 *version){ 172 | static char urlbuf[0x49]; 173 | static unsigned char tmdbuf[0x300]; //Dont need the whole thing 174 | httpcContext ctx; 175 | Result res = 0; 176 | u32 nr; 177 | u32 status; 178 | bool require_close = false; 179 | 180 | snprintf(urlbuf, sizeof(urlbuf), "http://ccs.cdn.c.shop.nintendowifi.net/ccs/download/%016llx/tmd", tid); 181 | if(R_FAILED(res = httpcOpenContext(&ctx, HTTPC_METHOD_GET, urlbuf, 1))) goto clean; 182 | require_close = true; 183 | 184 | if(R_FAILED(res = httpcAddRequestHeaderField(&ctx, "User-Agent", "CTR/P/1.0.0/r61631"))) goto clean;//taken from nim, seems legit(TM) 185 | if(R_FAILED(res = httpcSetKeepAlive(&ctx, HTTPC_KEEPALIVE_DISABLED))) goto clean; 186 | if(R_FAILED(res = httpcBeginRequest(&ctx))) goto clean; 187 | if(R_FAILED(res = httpcGetResponseStatusCodeTimeout(&ctx, &status, 30000000000))) goto clean;//30s timeout 188 | 189 | if(status != 200){ 190 | res = tmd_FileNotFoundResult; 191 | goto clean; 192 | } 193 | 194 | httpcDownloadData(&ctx, tmdbuf, sizeof(tmdbuf), &nr); //will hang httpcCloseContext if remote sends > sizeof(tidbuf), but this never happens? 195 | 196 | *version = (tmdbuf[0x1dc] << 8) | tmdbuf[0x1dd]; 197 | 198 | printf("TMD version: %u\n", *version); 199 | 200 | clean: 201 | if(require_close) httpcCloseContext(&ctx); 202 | 203 | return res; 204 | } 205 | -------------------------------------------------------------------------------- /source/titles.h: -------------------------------------------------------------------------------- 1 | /* 2 | * titlekeys.h 3 | * by Reisyukaku 4 | */ 5 | 6 | #ifndef INC_TITLES 7 | #define INC_TITLES 8 | 9 | static const Result tkey_OutOfMemoryResult = MAKERESULT(RL_STATUS, RS_OUTOFRESOURCE, RM_APPLICATION, RD_OUT_OF_MEMORY); 10 | static const Result tkey_FileNotFoundResult = MAKERESULT(RL_PERMANENT, RS_NOTFOUND, RM_APPLICATION, 1); 11 | static const Result tkey_JSONParseFailureResult = MAKERESULT(RL_PERMANENT, RS_CANCELED, RM_APPLICATION, 2); 12 | static const Result tkey_JSONMissingTitleKeyResult = MAKERESULT(RL_PERMANENT, RS_NOTFOUND, RM_APPLICATION, 3); 13 | static const Result tkey_TitleKeyNotInDBResult = MAKERESULT(RL_PERMANENT, RS_NOTFOUND, RM_APPLICATION, 4); 14 | static const Result tmd_FileNotFoundResult = MAKERESULT(RL_PERMANENT, RS_NOTFOUND, RM_APPLICATION, 5); 15 | 16 | Result getTitleKey(u64 tid, unsigned char *key); 17 | Result getTitleVersion(u64 tid, u16 *version); 18 | 19 | #endif 20 | --------------------------------------------------------------------------------