├── .gitignore ├── .gitmodules ├── LICENSE ├── LICENSE-quirc.md ├── Makefile ├── README.md ├── resources ├── app.rsf ├── audio.wav ├── banner.png └── icon.png └── source ├── buttons.cpp ├── decode.c ├── dir.cpp ├── download.cpp ├── draw.cpp ├── fs.cpp ├── gfx.cpp ├── identify.c ├── include ├── buttons.h ├── common.h ├── dir.h ├── download.hpp ├── draw.h ├── extract_zip.hpp ├── fs.hpp ├── gfx.hpp ├── qrdec.h ├── quirc.h ├── quirc_internal.h ├── sort.h └── util.hpp ├── main.cpp ├── qrdec.cpp ├── quirc.c ├── sort.cpp ├── util.cpp ├── version_db.c ├── xplorer.cpp └── zip.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | #exclude 2 | 3 | /build/* 4 | /output/3ds-arm/multidownload++/* 5 | /output/3ds-arm/* 6 | output/3ds-arm/multidownload.3ds 7 | /resources/banner.bin 8 | /resources/icon.bin 9 | *.elf -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "buildtools"] 2 | path = buildtools 3 | url = https://github.com/Steveice10/buildtools.git 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 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 | {one line to give the program's name and a brief idea of what it does.} 635 | Copyright (C) {year} {name of author} 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 | {project} Copyright (C) {year} {fullname} 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 | -------------------------------------------------------------------------------- /LICENSE-quirc.md: -------------------------------------------------------------------------------- 1 | quirc -- QR-code recognition library 2 | Copyright (C) 2010-2012 Daniel Beer 3 | 4 | Permission to use, copy, modify, and/or distribute this software for 5 | any purpose with or without fee is hereby granted, provided that the 6 | above copyright notice and this permission notice appear in all 7 | copies. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 10 | WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 11 | WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 12 | AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 13 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 14 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 15 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 16 | PERFORMANCE OF THIS SOFTWARE. 17 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # This Makefile is an edited version of the file found here: 2 | 3 | # https://github.com/Steveice10/FBI and has been released under MIT Licence as 4 | 5 | # follows: 6 | 7 | # 8 | 9 | # Copyright © 2015 Steveice10 10 | 11 | # 12 | 13 | # Permission is hereby granted, free of charge, to any person obtaining a copy 14 | 15 | # of this software and associated documentation files (the "Software"), to deal 16 | 17 | # in the Software without restriction, including without limitation the rights 18 | 19 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 20 | 21 | # copies of the Software, and to permit persons to whom the Software is 22 | 23 | # furnished to do so, subject to the following conditions: 24 | 25 | # 26 | 27 | # The above copyright notice and this permission notice shall be included in 28 | 29 | # all copies or substantial portions of the Software. 30 | 31 | # 32 | 33 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 34 | 35 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 36 | 37 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 38 | 39 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 40 | 41 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 42 | 43 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 44 | 45 | # SOFTWARE. 46 | 47 | 48 | 49 | # TARGET # 50 | 51 | 52 | 53 | TARGET := 3DS 54 | 55 | LIBRARY := 0 56 | 57 | 58 | 59 | ifeq ($(TARGET),3DS) 60 | 61 | ifeq ($(strip $(DEVKITPRO)),) 62 | 63 | $(error "Please set DEVKITPRO in your environment. export DEVKITPRO=devkitPro") 64 | 65 | endif 66 | 67 | 68 | 69 | ifeq ($(strip $(DEVKITARM)),) 70 | 71 | $(error "Please set DEVKITARM in your environment. export DEVKITARM=devkitARM") 72 | 73 | endif 74 | 75 | endif 76 | 77 | 78 | 79 | # COMMON CONFIGURATION # 80 | 81 | 82 | 83 | NAME := Multidownload++ 84 | 85 | 86 | 87 | BUILD_DIR := build 88 | 89 | OUTPUT_DIR := output 90 | 91 | INCLUDE_DIRS := source/include 92 | 93 | SOURCE_DIRS := source 94 | 95 | 96 | 97 | EXTRA_OUTPUT_FILES := 98 | 99 | 100 | 101 | LIBRARY_DIRS := $(DEVKITPRO)/libctru 102 | 103 | LIBRARIES := archive z ctru m 104 | 105 | 106 | 107 | BUILD_FLAGS := -Wall -Wextra 108 | 109 | RUN_FLAGS := 110 | 111 | VERSION_MAJOR := 3 112 | 113 | VERSION_MINOR := 2 114 | 115 | VERSION_MICRO := 0 116 | 117 | 118 | 119 | # 3DS CONFIGURATION # 120 | 121 | 122 | 123 | TITLE := $(NAME) 124 | 125 | DESCRIPTION := Download files straight onto your 3ds! 126 | 127 | AUTHOR := Kartik 128 | 129 | PRODUCT_CODE := CTR-E-DOWN 130 | 131 | UNIQUE_ID := 0x1030 132 | 133 | SYSTEM_MODE := 64MB 134 | 135 | SYSTEM_MODE_EXT := Legacy 136 | 137 | CPU_SPEED := 804MHz 138 | 139 | ROMFS_DIR := romfs 140 | 141 | BANNER_AUDIO := resources/audio.wav 142 | 143 | BANNER_IMAGE := resources/banner.png 144 | 145 | ICON := resources/icon.png 146 | 147 | 148 | 149 | # INTERNAL # 150 | 151 | 152 | 153 | include buildtools/make_base -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Multidownload++ 2 | 3 | Rewrite of original Multidownload in c++.Has better compatibility with sites ,a nicer UI , an included file-explorer and less errors. 4 | 5 | TODO:- 6 | Zip Support 7 | Mega & adf.ly support 8 | -------------------------------------------------------------------------------- /resources/app.rsf: -------------------------------------------------------------------------------- 1 | BasicInfo: 2 | Title : "Multidownload++" 3 | CompanyCode : "00" 4 | ProductCode : "CTR-E-DOWN" 5 | ContentType : Application 6 | Logo : Nintendo # Nintendo / Licensed / Distributed / iQue / iQueForSystem 7 | 8 | TitleInfo: 9 | UniqueId : 0x1030 10 | Category : Application 11 | 12 | CardInfo: 13 | MediaSize : 128MB # 128MB / 256MB / 512MB / 1GB / 2GB / 4GB 14 | MediaType : Card1 # Card1 / Card2 15 | CardDevice : NorFlash # NorFlash(Pick this if you use savedata) / None 16 | 17 | 18 | Option: 19 | UseOnSD : true # true if App is to be installed to SD 20 | FreeProductCode : true # Removes limitations on ProductCode 21 | MediaFootPadding : false # If true CCI files are created with padding 22 | EnableCrypt : false # Enables encryption for NCCH and CIA 23 | EnableCompress : true # Compresses exefs code 24 | 25 | AccessControlInfo: 26 | #UseExtSaveData : true 27 | #ExtSaveDataId: 0xff3ff 28 | #UseExtendedSaveDataAccessControl: true 29 | #AccessibleSaveDataIds: [0x101, 0x202, 0x303, 0x404, 0x505, 0x606] 30 | 31 | SystemControlInfo: 32 | SaveDataSize: 128KB 33 | RemasterVersion: 0 34 | StackSize: 0x40000 35 | 36 | # DO NOT EDIT BELOW HERE OR PROGRAMS WILL NOT LAUNCH (most likely) 37 | 38 | AccessControlInfo: 39 | FileSystemAccess: 40 | - Debug 41 | - DirectSdmc 42 | - DirectSdmcWrite 43 | 44 | IdealProcessor : 0 45 | AffinityMask : 1 46 | 47 | Priority : 16 48 | 49 | MaxCpu : 0x9E # Default 50 | DisableDebug : false 51 | EnableForceDebug : false 52 | CanWriteSharedPage : false 53 | CanUsePrivilegedPriority : false 54 | CanUseNonAlphabetAndNumber : false 55 | PermitMainFunctionArgument : false 56 | CanShareDeviceMemory : false 57 | RunnableOnSleep : false 58 | SpecialMemoryArrange : false 59 | CoreVersion : 2 60 | DescVersion : 2 61 | 62 | ReleaseKernelMajor : "02" 63 | ReleaseKernelMinor : "33" 64 | MemoryType : Application 65 | HandleTableSize: 512 66 | 67 | SystemModeExt : Legacy # Legacy(Default)/124MB/178MB Legacy:Use Old3DS SystemMode 68 | CpuSpeed : 804MHz # 268MHz(Default)/804MHz 69 | EnableL2Cache : true # false(default)/true 70 | CanAccessCore2 : true 71 | 72 | IORegisterMapping: 73 | - 1ff50000-1ff57fff 74 | - 1ff70000-1ff77fff 75 | MemoryMapping: 76 | - 1f000000-1f5fffff:r 77 | SystemCallAccess: 78 | ArbitrateAddress: 34 79 | Break: 60 80 | CancelTimer: 28 81 | ClearEvent: 25 82 | ClearTimer: 29 83 | CloseHandle: 35 84 | ConnectToPort: 45 85 | ControlMemory: 1 86 | CreateAddressArbiter: 33 87 | CreateEvent: 23 88 | CreateMemoryBlock: 30 89 | CreateMutex: 19 90 | CreateSemaphore: 21 91 | CreateThread: 8 92 | CreateTimer: 26 93 | DuplicateHandle: 39 94 | ExitProcess: 3 95 | ExitThread: 9 96 | GetCurrentProcessorNumber: 17 97 | GetHandleInfo: 41 98 | GetProcessId: 53 99 | GetProcessIdOfThread: 54 100 | GetProcessIdealProcessor: 6 101 | GetProcessInfo: 43 102 | GetResourceLimit: 56 103 | GetResourceLimitCurrentValues: 58 104 | GetResourceLimitLimitValues: 57 105 | GetSystemInfo: 42 106 | GetSystemTick: 40 107 | GetThreadContext: 59 108 | GetThreadId: 55 109 | GetThreadIdealProcessor: 15 110 | GetThreadInfo: 44 111 | GetThreadPriority: 11 112 | MapMemoryBlock: 31 113 | OutputDebugString: 61 114 | QueryMemory: 2 115 | ReleaseMutex: 20 116 | ReleaseSemaphore: 22 117 | SendSyncRequest1: 46 118 | SendSyncRequest2: 47 119 | SendSyncRequest3: 48 120 | SendSyncRequest4: 49 121 | SendSyncRequest: 50 122 | SetThreadPriority: 12 123 | SetTimer: 27 124 | SignalEvent: 24 125 | SleepThread: 10 126 | UnmapMemoryBlock: 32 127 | WaitSynchronization1: 36 128 | WaitSynchronizationN: 37 129 | InterruptNumbers: 130 | ServiceAccessControl: 131 | - APT:U 132 | - $hioFIO 133 | - $hostio0 134 | - $hostio1 135 | - ac:u 136 | - boss:U 137 | - cam:u 138 | - cecd:u 139 | - cfg:u 140 | - dlp:FKCL 141 | - dlp:SRVR 142 | - dsp::DSP 143 | - frd:u 144 | - fs:USER 145 | - gsp::Gpu 146 | - hid:USER 147 | - http:C 148 | - mic:u 149 | - ndm:u 150 | - news:s 151 | - nwm::UDS 152 | - ptm:u 153 | - pxi:dev 154 | - soc:U 155 | - gsp::Lcd 156 | - y2r:u 157 | - ldr:ro 158 | - ir:USER 159 | - ir:u 160 | - csnd:SND 161 | - ir:rst 162 | - ns:s 163 | 164 | SystemControlInfo: 165 | Dependency: 166 | ac: 0x0004013000002402L 167 | am: 0x0004013000001502L 168 | boss: 0x0004013000003402L 169 | camera: 0x0004013000001602L 170 | cecd: 0x0004013000002602L 171 | cfg: 0x0004013000001702L 172 | codec: 0x0004013000001802L 173 | csnd: 0x0004013000002702L 174 | dlp: 0x0004013000002802L 175 | dsp: 0x0004013000001a02L 176 | friends: 0x0004013000003202L 177 | gpio: 0x0004013000001b02L 178 | gsp: 0x0004013000001c02L 179 | hid: 0x0004013000001d02L 180 | http: 0x0004013000002902L 181 | i2c: 0x0004013000001e02L 182 | ir: 0x0004013000003302L 183 | mcu: 0x0004013000001f02L 184 | mic: 0x0004013000002002L 185 | ndm: 0x0004013000002b02L 186 | news: 0x0004013000003502L 187 | nim: 0x0004013000002c02L 188 | nwm: 0x0004013000002d02L 189 | pdn: 0x0004013000002102L 190 | ps: 0x0004013000003102L 191 | ptm: 0x0004013000002202L 192 | ro: 0x0004013000003702L 193 | socket: 0x0004013000002e02L 194 | spi: 0x0004013000002302L 195 | -------------------------------------------------------------------------------- /resources/audio.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hax0kartik/Multidownload/786a841d4ab2fa4222825832797ba71bb4b12651/resources/audio.wav -------------------------------------------------------------------------------- /resources/banner.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hax0kartik/Multidownload/786a841d4ab2fa4222825832797ba71bb4b12651/resources/banner.png -------------------------------------------------------------------------------- /resources/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hax0kartik/Multidownload/786a841d4ab2fa4222825832797ba71bb4b12651/resources/icon.png -------------------------------------------------------------------------------- /source/buttons.cpp: -------------------------------------------------------------------------------- 1 | #include "buttons.h" 2 | #include "dir.h" 3 | #include "draw.h" 4 | 5 | void up(void) { 6 | if (size_of_file_array == 0){ ; } 7 | 8 | // if selected is 0 and there is no scroll, skip to the bottom 9 | else if (selected+scroll == 0) { 10 | // If there is a need for scroll 11 | if (size_of_file_array > MAX_FILES_ON_SCREEN) { 12 | // arrays are 0 indexed, so max files -1 = highest index of array shown 13 | selected = MAX_FILES_ON_SCREEN-1; 14 | // scroll will be max size it can be 15 | scroll = size_of_file_array-MAX_FILES_ON_SCREEN; 16 | } 17 | else { selected = size_of_file_array-1; } 18 | } 19 | 20 | else if (scroll > 0) { scroll--; } 21 | 22 | else { selected--; } 23 | } 24 | 25 | void down(void) { 26 | if (size_of_file_array == 0){ ; } 27 | 28 | // If selected+scroll are at the largest index of the file array (arrays are 0 indexed!) 29 | else if (selected+scroll == size_of_file_array-1) { 30 | selected = 0; 31 | scroll = 0; 32 | } 33 | 34 | // If selected = highest index and selected+scroll < the max index for the array 35 | else if ((selected == MAX_FILES_ON_SCREEN-1) && (selected+scroll < size_of_file_array-1)) { scroll++; } 36 | 37 | else { selected++; } 38 | } 39 | 40 | void left(void) { 41 | if (size_of_file_array == 0){ ; } 42 | // If selected+scroll are at the largest index of the file array (arrays are 0 indexed!) 43 | else { 44 | selected = 0; 45 | scroll = 0; 46 | } 47 | } 48 | 49 | void right(void){ 50 | if (size_of_file_array == 0){ ; } 51 | else { 52 | // If there is a need for scroll 53 | if (size_of_file_array > MAX_FILES_ON_SCREEN) { 54 | // arrays are 0 indexed, so max files -1 = highest index of array shown 55 | selected = MAX_FILES_ON_SCREEN-1; 56 | // scroll will be max size it can be 57 | scroll = size_of_file_array-MAX_FILES_ON_SCREEN; 58 | } 59 | else { selected = size_of_file_array-1; } 60 | } 61 | } 62 | 63 | void a_pressed(void) { 64 | consoleSelect(&debugscreen); 65 | 66 | if (size_of_file_array == 0){ ; } 67 | 68 | else { 69 | // If it is actually a directory 70 | if (!isfile_arr[selected+scroll]) { 71 | printf("path is dir\n"); 72 | strcat(current_path, file_arr[selected+scroll]); 73 | strcat(current_path, "/"); 74 | printf("\x1b[32mnew path: %s\x1b[0m\n", current_path); 75 | get_all_in_dir(current_path); 76 | consoleSelect(&topScreen); 77 | printf("\x1b[2J"); 78 | print_all_values_in_filear(); 79 | } 80 | } 81 | 82 | } 83 | 84 | void b_pressed(void) { 85 | consoleSelect(&debugscreen); 86 | 87 | if (!strcmp(current_path, "sdmc:/")) { printf("\x1b[32mcurrently in sdmc:/\x1b[0m\n"); } 88 | 89 | // move up a directory 90 | else { get_ud(); } 91 | consoleSelect(&topScreen); 92 | printf("\x1b[2J"); 93 | } 94 | -------------------------------------------------------------------------------- /source/decode.c: -------------------------------------------------------------------------------- 1 | /* quirc -- QR-code recognition library 2 | * Copyright (C) 2010-2012 Daniel Beer 3 | * 4 | * Permission to use, copy, modify, and/or distribute this software for any 5 | * purpose with or without fee is hereby granted, provided that the above 6 | * copyright notice and this permission notice appear in all copies. 7 | * 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | */ 16 | 17 | #include "quirc_internal.h" 18 | 19 | #include 20 | #include 21 | 22 | #define MAX_POLY 64 23 | 24 | /************************************************************************ 25 | * Galois fields 26 | */ 27 | 28 | struct galois_field { 29 | int p; 30 | const uint8_t *log; 31 | const uint8_t *exp; 32 | }; 33 | 34 | static const uint8_t gf16_exp[16] = { 35 | 0x01, 0x02, 0x04, 0x08, 0x03, 0x06, 0x0c, 0x0b, 36 | 0x05, 0x0a, 0x07, 0x0e, 0x0f, 0x0d, 0x09, 0x01 37 | }; 38 | 39 | static const uint8_t gf16_log[16] = { 40 | 0x00, 0x0f, 0x01, 0x04, 0x02, 0x08, 0x05, 0x0a, 41 | 0x03, 0x0e, 0x09, 0x07, 0x06, 0x0d, 0x0b, 0x0c 42 | }; 43 | 44 | static const struct galois_field gf16 = { 45 | .p = 15, 46 | .log = gf16_log, 47 | .exp = gf16_exp 48 | }; 49 | 50 | static const uint8_t gf256_exp[256] = { 51 | 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 52 | 0x1d, 0x3a, 0x74, 0xe8, 0xcd, 0x87, 0x13, 0x26, 53 | 0x4c, 0x98, 0x2d, 0x5a, 0xb4, 0x75, 0xea, 0xc9, 54 | 0x8f, 0x03, 0x06, 0x0c, 0x18, 0x30, 0x60, 0xc0, 55 | 0x9d, 0x27, 0x4e, 0x9c, 0x25, 0x4a, 0x94, 0x35, 56 | 0x6a, 0xd4, 0xb5, 0x77, 0xee, 0xc1, 0x9f, 0x23, 57 | 0x46, 0x8c, 0x05, 0x0a, 0x14, 0x28, 0x50, 0xa0, 58 | 0x5d, 0xba, 0x69, 0xd2, 0xb9, 0x6f, 0xde, 0xa1, 59 | 0x5f, 0xbe, 0x61, 0xc2, 0x99, 0x2f, 0x5e, 0xbc, 60 | 0x65, 0xca, 0x89, 0x0f, 0x1e, 0x3c, 0x78, 0xf0, 61 | 0xfd, 0xe7, 0xd3, 0xbb, 0x6b, 0xd6, 0xb1, 0x7f, 62 | 0xfe, 0xe1, 0xdf, 0xa3, 0x5b, 0xb6, 0x71, 0xe2, 63 | 0xd9, 0xaf, 0x43, 0x86, 0x11, 0x22, 0x44, 0x88, 64 | 0x0d, 0x1a, 0x34, 0x68, 0xd0, 0xbd, 0x67, 0xce, 65 | 0x81, 0x1f, 0x3e, 0x7c, 0xf8, 0xed, 0xc7, 0x93, 66 | 0x3b, 0x76, 0xec, 0xc5, 0x97, 0x33, 0x66, 0xcc, 67 | 0x85, 0x17, 0x2e, 0x5c, 0xb8, 0x6d, 0xda, 0xa9, 68 | 0x4f, 0x9e, 0x21, 0x42, 0x84, 0x15, 0x2a, 0x54, 69 | 0xa8, 0x4d, 0x9a, 0x29, 0x52, 0xa4, 0x55, 0xaa, 70 | 0x49, 0x92, 0x39, 0x72, 0xe4, 0xd5, 0xb7, 0x73, 71 | 0xe6, 0xd1, 0xbf, 0x63, 0xc6, 0x91, 0x3f, 0x7e, 72 | 0xfc, 0xe5, 0xd7, 0xb3, 0x7b, 0xf6, 0xf1, 0xff, 73 | 0xe3, 0xdb, 0xab, 0x4b, 0x96, 0x31, 0x62, 0xc4, 74 | 0x95, 0x37, 0x6e, 0xdc, 0xa5, 0x57, 0xae, 0x41, 75 | 0x82, 0x19, 0x32, 0x64, 0xc8, 0x8d, 0x07, 0x0e, 76 | 0x1c, 0x38, 0x70, 0xe0, 0xdd, 0xa7, 0x53, 0xa6, 77 | 0x51, 0xa2, 0x59, 0xb2, 0x79, 0xf2, 0xf9, 0xef, 78 | 0xc3, 0x9b, 0x2b, 0x56, 0xac, 0x45, 0x8a, 0x09, 79 | 0x12, 0x24, 0x48, 0x90, 0x3d, 0x7a, 0xf4, 0xf5, 80 | 0xf7, 0xf3, 0xfb, 0xeb, 0xcb, 0x8b, 0x0b, 0x16, 81 | 0x2c, 0x58, 0xb0, 0x7d, 0xfa, 0xe9, 0xcf, 0x83, 82 | 0x1b, 0x36, 0x6c, 0xd8, 0xad, 0x47, 0x8e, 0x01 83 | }; 84 | 85 | static const uint8_t gf256_log[256] = { 86 | 0x00, 0xff, 0x01, 0x19, 0x02, 0x32, 0x1a, 0xc6, 87 | 0x03, 0xdf, 0x33, 0xee, 0x1b, 0x68, 0xc7, 0x4b, 88 | 0x04, 0x64, 0xe0, 0x0e, 0x34, 0x8d, 0xef, 0x81, 89 | 0x1c, 0xc1, 0x69, 0xf8, 0xc8, 0x08, 0x4c, 0x71, 90 | 0x05, 0x8a, 0x65, 0x2f, 0xe1, 0x24, 0x0f, 0x21, 91 | 0x35, 0x93, 0x8e, 0xda, 0xf0, 0x12, 0x82, 0x45, 92 | 0x1d, 0xb5, 0xc2, 0x7d, 0x6a, 0x27, 0xf9, 0xb9, 93 | 0xc9, 0x9a, 0x09, 0x78, 0x4d, 0xe4, 0x72, 0xa6, 94 | 0x06, 0xbf, 0x8b, 0x62, 0x66, 0xdd, 0x30, 0xfd, 95 | 0xe2, 0x98, 0x25, 0xb3, 0x10, 0x91, 0x22, 0x88, 96 | 0x36, 0xd0, 0x94, 0xce, 0x8f, 0x96, 0xdb, 0xbd, 97 | 0xf1, 0xd2, 0x13, 0x5c, 0x83, 0x38, 0x46, 0x40, 98 | 0x1e, 0x42, 0xb6, 0xa3, 0xc3, 0x48, 0x7e, 0x6e, 99 | 0x6b, 0x3a, 0x28, 0x54, 0xfa, 0x85, 0xba, 0x3d, 100 | 0xca, 0x5e, 0x9b, 0x9f, 0x0a, 0x15, 0x79, 0x2b, 101 | 0x4e, 0xd4, 0xe5, 0xac, 0x73, 0xf3, 0xa7, 0x57, 102 | 0x07, 0x70, 0xc0, 0xf7, 0x8c, 0x80, 0x63, 0x0d, 103 | 0x67, 0x4a, 0xde, 0xed, 0x31, 0xc5, 0xfe, 0x18, 104 | 0xe3, 0xa5, 0x99, 0x77, 0x26, 0xb8, 0xb4, 0x7c, 105 | 0x11, 0x44, 0x92, 0xd9, 0x23, 0x20, 0x89, 0x2e, 106 | 0x37, 0x3f, 0xd1, 0x5b, 0x95, 0xbc, 0xcf, 0xcd, 107 | 0x90, 0x87, 0x97, 0xb2, 0xdc, 0xfc, 0xbe, 0x61, 108 | 0xf2, 0x56, 0xd3, 0xab, 0x14, 0x2a, 0x5d, 0x9e, 109 | 0x84, 0x3c, 0x39, 0x53, 0x47, 0x6d, 0x41, 0xa2, 110 | 0x1f, 0x2d, 0x43, 0xd8, 0xb7, 0x7b, 0xa4, 0x76, 111 | 0xc4, 0x17, 0x49, 0xec, 0x7f, 0x0c, 0x6f, 0xf6, 112 | 0x6c, 0xa1, 0x3b, 0x52, 0x29, 0x9d, 0x55, 0xaa, 113 | 0xfb, 0x60, 0x86, 0xb1, 0xbb, 0xcc, 0x3e, 0x5a, 114 | 0xcb, 0x59, 0x5f, 0xb0, 0x9c, 0xa9, 0xa0, 0x51, 115 | 0x0b, 0xf5, 0x16, 0xeb, 0x7a, 0x75, 0x2c, 0xd7, 116 | 0x4f, 0xae, 0xd5, 0xe9, 0xe6, 0xe7, 0xad, 0xe8, 117 | 0x74, 0xd6, 0xf4, 0xea, 0xa8, 0x50, 0x58, 0xaf 118 | }; 119 | 120 | const static struct galois_field gf256 = { 121 | .p = 255, 122 | .log = gf256_log, 123 | .exp = gf256_exp 124 | }; 125 | 126 | /************************************************************************ 127 | * Polynomial operations 128 | */ 129 | 130 | static void poly_add(uint8_t *dst, const uint8_t *src, uint8_t c, 131 | int shift, const struct galois_field *gf) 132 | { 133 | int i; 134 | int log_c = gf->log[c]; 135 | 136 | if (!c) 137 | return; 138 | 139 | for (i = 0; i < MAX_POLY; i++) { 140 | int p = i + shift; 141 | uint8_t v = src[i]; 142 | 143 | if (p < 0 || p >= MAX_POLY) 144 | continue; 145 | if (!v) 146 | continue; 147 | 148 | dst[p] ^= gf->exp[(gf->log[v] + log_c) % gf->p]; 149 | } 150 | } 151 | 152 | static uint8_t poly_eval(const uint8_t *s, uint8_t x, 153 | const struct galois_field *gf) 154 | { 155 | int i; 156 | uint8_t sum = 0; 157 | uint8_t log_x = gf->log[x]; 158 | 159 | if (!x) 160 | return s[0]; 161 | 162 | for (i = 0; i < MAX_POLY; i++) { 163 | uint8_t c = s[i]; 164 | 165 | if (!c) 166 | continue; 167 | 168 | sum ^= gf->exp[(gf->log[c] + log_x * i) % gf->p]; 169 | } 170 | 171 | return sum; 172 | } 173 | 174 | /************************************************************************ 175 | * Berlekamp-Massey algorithm for finding error locator polynomials. 176 | */ 177 | 178 | static void berlekamp_massey(const uint8_t *s, int N, 179 | const struct galois_field *gf, 180 | uint8_t *sigma) 181 | { 182 | uint8_t C[MAX_POLY]; 183 | uint8_t B[MAX_POLY]; 184 | int L = 0; 185 | int m = 1; 186 | uint8_t b = 1; 187 | int n; 188 | 189 | memset(B, 0, sizeof(B)); 190 | memset(C, 0, sizeof(C)); 191 | B[0] = 1; 192 | C[0] = 1; 193 | 194 | for (n = 0; n < N; n++) { 195 | uint8_t d = s[n]; 196 | uint8_t mult; 197 | int i; 198 | 199 | for (i = 1; i <= L; i++) { 200 | if (!(C[i] && s[n - i])) 201 | continue; 202 | 203 | d ^= gf->exp[(gf->log[C[i]] + 204 | gf->log[s[n - i]]) % 205 | gf->p]; 206 | } 207 | 208 | mult = gf->exp[(gf->p - gf->log[b] + gf->log[d]) % gf->p]; 209 | 210 | if (!d) { 211 | m++; 212 | } else if (L * 2 <= n) { 213 | uint8_t T[MAX_POLY]; 214 | 215 | memcpy(T, C, sizeof(T)); 216 | poly_add(C, B, mult, m, gf); 217 | memcpy(B, T, sizeof(B)); 218 | L = n + 1 - L; 219 | b = d; 220 | m = 1; 221 | } else { 222 | poly_add(C, B, mult, m, gf); 223 | m++; 224 | } 225 | } 226 | 227 | memcpy(sigma, C, MAX_POLY); 228 | } 229 | 230 | /************************************************************************ 231 | * Code stream error correction 232 | * 233 | * Generator polynomial for GF(2^8) is x^8 + x^4 + x^3 + x^2 + 1 234 | */ 235 | 236 | static int block_syndromes(const uint8_t *data, int bs, int npar, uint8_t *s) 237 | { 238 | int nonzero = 0; 239 | int i; 240 | 241 | memset(s, 0, MAX_POLY); 242 | 243 | for (i = 0; i < npar; i++) { 244 | int j; 245 | 246 | for (j = 0; j < bs; j++) { 247 | uint8_t c = data[bs - j - 1]; 248 | 249 | if (!c) 250 | continue; 251 | 252 | s[i] ^= gf256_exp[((int)gf256_log[c] + 253 | i * j) % 255]; 254 | } 255 | 256 | if (s[i]) 257 | nonzero = 1; 258 | } 259 | 260 | return nonzero; 261 | } 262 | 263 | static void eloc_poly(uint8_t *omega, 264 | const uint8_t *s, const uint8_t *sigma, 265 | int npar) 266 | { 267 | int i; 268 | 269 | memset(omega, 0, MAX_POLY); 270 | 271 | for (i = 0; i < npar; i++) { 272 | const uint8_t a = sigma[i]; 273 | const uint8_t log_a = gf256_log[a]; 274 | int j; 275 | 276 | if (!a) 277 | continue; 278 | 279 | for (j = 0; j + 1 < MAX_POLY; j++) { 280 | const uint8_t b = s[j + 1]; 281 | 282 | if (i + j >= npar) 283 | break; 284 | 285 | if (!b) 286 | continue; 287 | 288 | omega[i + j] ^= 289 | gf256_exp[(log_a + gf256_log[b]) % 255]; 290 | } 291 | } 292 | } 293 | 294 | static quirc_decode_error_t correct_block(uint8_t *data, 295 | const struct quirc_rs_params *ecc) 296 | { 297 | int npar = ecc->bs - ecc->dw; 298 | uint8_t s[MAX_POLY]; 299 | uint8_t sigma[MAX_POLY]; 300 | uint8_t sigma_deriv[MAX_POLY]; 301 | uint8_t omega[MAX_POLY]; 302 | int i; 303 | 304 | /* Compute syndrome vector */ 305 | if (!block_syndromes(data, ecc->bs, npar, s)) 306 | return QUIRC_SUCCESS; 307 | 308 | berlekamp_massey(s, npar, &gf256, sigma); 309 | 310 | /* Compute derivative of sigma */ 311 | memset(sigma_deriv, 0, MAX_POLY); 312 | for (i = 0; i + 1 < MAX_POLY; i += 2) 313 | sigma_deriv[i] = sigma[i + 1]; 314 | 315 | /* Compute error evaluator polynomial */ 316 | eloc_poly(omega, s, sigma, npar - 1); 317 | 318 | /* Find error locations and magnitudes */ 319 | for (i = 0; i < ecc->bs; i++) { 320 | uint8_t xinv = gf256_exp[255 - i]; 321 | 322 | if (!poly_eval(sigma, xinv, &gf256)) { 323 | uint8_t sd_x = poly_eval(sigma_deriv, xinv, &gf256); 324 | uint8_t omega_x = poly_eval(omega, xinv, &gf256); 325 | uint8_t error = gf256_exp[(255 - gf256_log[sd_x] + 326 | gf256_log[omega_x]) % 255]; 327 | 328 | data[ecc->bs - i - 1] ^= error; 329 | } 330 | } 331 | 332 | if (block_syndromes(data, ecc->bs, npar, s)) 333 | return QUIRC_ERROR_DATA_ECC; 334 | 335 | return QUIRC_SUCCESS; 336 | } 337 | 338 | /************************************************************************ 339 | * Format value error correction 340 | * 341 | * Generator polynomial for GF(2^4) is x^4 + x + 1 342 | */ 343 | 344 | #define FORMAT_MAX_ERROR 3 345 | #define FORMAT_SYNDROMES (FORMAT_MAX_ERROR * 2) 346 | #define FORMAT_BITS 15 347 | 348 | static int format_syndromes(uint16_t u, uint8_t *s) 349 | { 350 | int i; 351 | int nonzero = 0; 352 | 353 | memset(s, 0, MAX_POLY); 354 | 355 | for (i = 0; i < FORMAT_SYNDROMES; i++) { 356 | int j; 357 | 358 | s[i] = 0; 359 | for (j = 0; j < FORMAT_BITS; j++) 360 | if (u & (1 << j)) 361 | s[i] ^= gf16_exp[((i + 1) * j) % 15]; 362 | 363 | if (s[i]) 364 | nonzero = 1; 365 | } 366 | 367 | return nonzero; 368 | } 369 | 370 | static quirc_decode_error_t correct_format(uint16_t *f_ret) 371 | { 372 | uint16_t u = *f_ret; 373 | int i; 374 | uint8_t s[MAX_POLY]; 375 | uint8_t sigma[MAX_POLY]; 376 | 377 | /* Evaluate U (received codeword) at each of alpha_1 .. alpha_6 378 | * to get S_1 .. S_6 (but we index them from 0). 379 | */ 380 | if (!format_syndromes(u, s)) 381 | return QUIRC_SUCCESS; 382 | 383 | berlekamp_massey(s, FORMAT_SYNDROMES, &gf16, sigma); 384 | 385 | /* Now, find the roots of the polynomial */ 386 | for (i = 0; i < 15; i++) 387 | if (!poly_eval(sigma, gf16_exp[15 - i], &gf16)) 388 | u ^= (1 << i); 389 | 390 | if (format_syndromes(u, s)) 391 | return QUIRC_ERROR_FORMAT_ECC; 392 | 393 | *f_ret = u; 394 | return QUIRC_SUCCESS; 395 | } 396 | 397 | /************************************************************************ 398 | * Decoder algorithm 399 | */ 400 | 401 | struct datastream { 402 | uint8_t raw[QUIRC_MAX_PAYLOAD]; 403 | int data_bits; 404 | int ptr; 405 | 406 | uint8_t data[QUIRC_MAX_PAYLOAD]; 407 | }; 408 | 409 | static inline int grid_bit(const struct quirc_code *code, int x, int y) 410 | { 411 | int p = y * code->size + x; 412 | 413 | return (code->cell_bitmap[p >> 3] >> (p & 7)) & 1; 414 | } 415 | 416 | static quirc_decode_error_t read_format(const struct quirc_code *code, 417 | struct quirc_data *data, int which) 418 | { 419 | int i; 420 | uint16_t format = 0; 421 | uint16_t fdata; 422 | quirc_decode_error_t err; 423 | 424 | if (which) { 425 | for (i = 0; i < 7; i++) 426 | format = (format << 1) | 427 | grid_bit(code, 8, code->size - 1 - i); 428 | for (i = 0; i < 8; i++) 429 | format = (format << 1) | 430 | grid_bit(code, code->size - 8 + i, 8); 431 | } else { 432 | static const int xs[15] = { 433 | 8, 8, 8, 8, 8, 8, 8, 8, 7, 5, 4, 3, 2, 1, 0 434 | }; 435 | static const int ys[15] = { 436 | 0, 1, 2, 3, 4, 5, 7, 8, 8, 8, 8, 8, 8, 8, 8 437 | }; 438 | 439 | for (i = 14; i >= 0; i--) 440 | format = (format << 1) | grid_bit(code, xs[i], ys[i]); 441 | } 442 | 443 | format ^= 0x5412; 444 | 445 | err = correct_format(&format); 446 | if (err) 447 | return err; 448 | 449 | fdata = format >> 10; 450 | data->ecc_level = fdata >> 3; 451 | data->mask = fdata & 7; 452 | 453 | return QUIRC_SUCCESS; 454 | } 455 | 456 | static int mask_bit(int mask, int i, int j) 457 | { 458 | switch (mask) { 459 | case 0: return !((i + j) % 2); 460 | case 1: return !(i % 2); 461 | case 2: return !(j % 3); 462 | case 3: return !((i + j) % 3); 463 | case 4: return !(((i / 2) + (j / 3)) % 2); 464 | case 5: return !((i * j) % 2 + (i * j) % 3); 465 | case 6: return !(((i * j) % 2 + (i * j) % 3) % 2); 466 | case 7: return !(((i * j) % 3 + (i + j) % 2) % 2); 467 | } 468 | 469 | return 0; 470 | } 471 | 472 | static int reserved_cell(int version, int i, int j) 473 | { 474 | const struct quirc_version_info *ver = &quirc_version_db[version]; 475 | int size = version * 4 + 17; 476 | int ai = -1, aj = -1, a; 477 | 478 | /* Finder + format: top left */ 479 | if (i < 9 && j < 9) 480 | return 1; 481 | 482 | /* Finder + format: bottom left */ 483 | if (i + 8 >= size && j < 9) 484 | return 1; 485 | 486 | /* Finder + format: top right */ 487 | if (i < 9 && j + 8 >= size) 488 | return 1; 489 | 490 | /* Exclude timing patterns */ 491 | if (i == 6 || j == 6) 492 | return 1; 493 | 494 | /* Exclude version info, if it exists. Version info sits adjacent to 495 | * the top-right and bottom-left finders in three rows, bounded by 496 | * the timing pattern. 497 | */ 498 | if (version >= 7) { 499 | if (i < 6 && j + 11 >= size) 500 | return 1; 501 | if (i + 11 >= size && j < 6) 502 | return 1; 503 | } 504 | 505 | /* Exclude alignment patterns */ 506 | for (a = 0; a < QUIRC_MAX_ALIGNMENT && ver->apat[a]; a++) { 507 | int p = ver->apat[a]; 508 | 509 | if (abs(p - i) < 3) 510 | ai = a; 511 | if (abs(p - j) < 3) 512 | aj = a; 513 | } 514 | 515 | if (ai >= 0 && aj >= 0) { 516 | a--; 517 | if (ai > 0 && ai < a) 518 | return 1; 519 | if (aj > 0 && aj < a) 520 | return 1; 521 | if (aj == a && ai == a) 522 | return 1; 523 | } 524 | 525 | return 0; 526 | } 527 | 528 | static void read_bit(const struct quirc_code *code, 529 | struct quirc_data *data, 530 | struct datastream *ds, int i, int j) 531 | { 532 | int bitpos = ds->data_bits & 7; 533 | int bytepos = ds->data_bits >> 3; 534 | int v = grid_bit(code, j, i); 535 | 536 | if (mask_bit(data->mask, i, j)) 537 | v ^= 1; 538 | 539 | if (v) 540 | ds->raw[bytepos] |= (0x80 >> bitpos); 541 | 542 | ds->data_bits++; 543 | } 544 | 545 | static void read_data(const struct quirc_code *code, 546 | struct quirc_data *data, 547 | struct datastream *ds) 548 | { 549 | int y = code->size - 1; 550 | int x = code->size - 1; 551 | int dir = -1; 552 | 553 | while (x > 0) { 554 | if (x == 6) 555 | x--; 556 | 557 | if (!reserved_cell(data->version, y, x)) 558 | read_bit(code, data, ds, y, x); 559 | 560 | if (!reserved_cell(data->version, y, x - 1)) 561 | read_bit(code, data, ds, y, x - 1); 562 | 563 | y += dir; 564 | if (y < 0 || y >= code->size) { 565 | dir = -dir; 566 | x -= 2; 567 | y += dir; 568 | } 569 | } 570 | } 571 | 572 | static quirc_decode_error_t codestream_ecc(struct quirc_data *data, 573 | struct datastream *ds) 574 | { 575 | const struct quirc_version_info *ver = 576 | &quirc_version_db[data->version]; 577 | const struct quirc_rs_params *sb_ecc = &ver->ecc[data->ecc_level]; 578 | struct quirc_rs_params lb_ecc; 579 | const int lb_count = 580 | (ver->data_bytes - sb_ecc->bs * sb_ecc->ns) / (sb_ecc->bs + 1); 581 | const int bc = lb_count + sb_ecc->ns; 582 | const int ecc_offset = sb_ecc->dw * bc + lb_count; 583 | int dst_offset = 0; 584 | int i; 585 | 586 | memcpy(&lb_ecc, sb_ecc, sizeof(lb_ecc)); 587 | lb_ecc.dw++; 588 | lb_ecc.bs++; 589 | 590 | for (i = 0; i < bc; i++) { 591 | uint8_t *dst = ds->data + dst_offset; 592 | const struct quirc_rs_params *ecc = 593 | (i < sb_ecc->ns) ? sb_ecc : &lb_ecc; 594 | const int num_ec = ecc->bs - ecc->dw; 595 | quirc_decode_error_t err; 596 | int j; 597 | 598 | for (j = 0; j < ecc->dw; j++) 599 | dst[j] = ds->raw[j * bc + i]; 600 | for (j = 0; j < num_ec; j++) 601 | dst[ecc->dw + j] = ds->raw[ecc_offset + j * bc + i]; 602 | 603 | err = correct_block(dst, ecc); 604 | if (err) 605 | return err; 606 | 607 | dst_offset += ecc->dw; 608 | } 609 | 610 | ds->data_bits = dst_offset * 8; 611 | 612 | return QUIRC_SUCCESS; 613 | } 614 | 615 | static inline int bits_remaining(const struct datastream *ds) 616 | { 617 | return ds->data_bits - ds->ptr; 618 | } 619 | 620 | static int take_bits(struct datastream *ds, int len) 621 | { 622 | int ret = 0; 623 | 624 | while (len && (ds->ptr < ds->data_bits)) { 625 | uint8_t b = ds->data[ds->ptr >> 3]; 626 | int bitpos = ds->ptr & 7; 627 | 628 | ret <<= 1; 629 | if ((b << bitpos) & 0x80) 630 | ret |= 1; 631 | 632 | ds->ptr++; 633 | len--; 634 | } 635 | 636 | return ret; 637 | } 638 | 639 | static int numeric_tuple(struct quirc_data *data, 640 | struct datastream *ds, 641 | int bits, int digits) 642 | { 643 | int tuple; 644 | int i; 645 | 646 | if (bits_remaining(ds) < bits) 647 | return -1; 648 | 649 | tuple = take_bits(ds, bits); 650 | 651 | for (i = digits - 1; i >= 0; i--) { 652 | data->payload[data->payload_len + i] = tuple % 10 + '0'; 653 | tuple /= 10; 654 | } 655 | 656 | data->payload_len += digits; 657 | return 0; 658 | } 659 | 660 | static quirc_decode_error_t decode_numeric(struct quirc_data *data, 661 | struct datastream *ds) 662 | { 663 | int bits = 14; 664 | int count; 665 | 666 | if (data->version < 10) 667 | bits = 10; 668 | else if (data->version < 27) 669 | bits = 12; 670 | 671 | count = take_bits(ds, bits); 672 | if (data->payload_len + count + 1 > QUIRC_MAX_PAYLOAD) 673 | return QUIRC_ERROR_DATA_OVERFLOW; 674 | 675 | while (count >= 3) { 676 | if (numeric_tuple(data, ds, 10, 3) < 0) 677 | return QUIRC_ERROR_DATA_UNDERFLOW; 678 | count -= 3; 679 | } 680 | 681 | if (count >= 2) { 682 | if (numeric_tuple(data, ds, 7, 2) < 0) 683 | return QUIRC_ERROR_DATA_UNDERFLOW; 684 | count -= 2; 685 | } 686 | 687 | if (count) { 688 | if (numeric_tuple(data, ds, 4, 1) < 0) 689 | return QUIRC_ERROR_DATA_UNDERFLOW; 690 | count--; 691 | } 692 | 693 | return QUIRC_SUCCESS; 694 | } 695 | 696 | static int alpha_tuple(struct quirc_data *data, 697 | struct datastream *ds, 698 | int bits, int digits) 699 | { 700 | int tuple; 701 | int i; 702 | 703 | if (bits_remaining(ds) < bits) 704 | return -1; 705 | 706 | tuple = take_bits(ds, bits); 707 | 708 | for (i = 0; i < digits; i++) { 709 | static const char *alpha_map = 710 | "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:"; 711 | 712 | data->payload[data->payload_len + digits - i - 1] = 713 | alpha_map[tuple % 45]; 714 | tuple /= 45; 715 | } 716 | 717 | data->payload_len += digits; 718 | return 0; 719 | } 720 | 721 | static quirc_decode_error_t decode_alpha(struct quirc_data *data, 722 | struct datastream *ds) 723 | { 724 | int bits = 13; 725 | int count; 726 | 727 | if (data->version < 10) 728 | bits = 9; 729 | else if (data->version < 27) 730 | bits = 11; 731 | 732 | count = take_bits(ds, bits); 733 | if (data->payload_len + count + 1 > QUIRC_MAX_PAYLOAD) 734 | return QUIRC_ERROR_DATA_OVERFLOW; 735 | 736 | while (count >= 2) { 737 | if (alpha_tuple(data, ds, 11, 2) < 0) 738 | return QUIRC_ERROR_DATA_UNDERFLOW; 739 | count -= 2; 740 | } 741 | 742 | if (count) { 743 | if (alpha_tuple(data, ds, 6, 1) < 0) 744 | return QUIRC_ERROR_DATA_UNDERFLOW; 745 | count--; 746 | } 747 | 748 | return QUIRC_SUCCESS; 749 | } 750 | 751 | static quirc_decode_error_t decode_byte(struct quirc_data *data, 752 | struct datastream *ds) 753 | { 754 | int bits = 16; 755 | int count; 756 | int i; 757 | 758 | if (data->version < 10) 759 | bits = 8; 760 | 761 | count = take_bits(ds, bits); 762 | if (data->payload_len + count + 1 > QUIRC_MAX_PAYLOAD) 763 | return QUIRC_ERROR_DATA_OVERFLOW; 764 | if (bits_remaining(ds) < count * 8) 765 | return QUIRC_ERROR_DATA_UNDERFLOW; 766 | 767 | for (i = 0; i < count; i++) 768 | data->payload[data->payload_len++] = take_bits(ds, 8); 769 | 770 | return QUIRC_SUCCESS; 771 | } 772 | 773 | static quirc_decode_error_t decode_kanji(struct quirc_data *data, 774 | struct datastream *ds) 775 | { 776 | int bits = 12; 777 | int count; 778 | int i; 779 | 780 | if (data->version < 10) 781 | bits = 8; 782 | else if (data->version < 27) 783 | bits = 10; 784 | 785 | count = take_bits(ds, bits); 786 | if (data->payload_len + count * 2 + 1 > QUIRC_MAX_PAYLOAD) 787 | return QUIRC_ERROR_DATA_OVERFLOW; 788 | if (bits_remaining(ds) < count * 13) 789 | return QUIRC_ERROR_DATA_UNDERFLOW; 790 | 791 | for (i = 0; i < count; i++) { 792 | int d = take_bits(ds, 13); 793 | uint16_t sjw; 794 | 795 | if (d + 0x8140 >= 0x9ffc) 796 | sjw = d + 0x8140; 797 | else 798 | sjw = d + 0xc140; 799 | 800 | data->payload[data->payload_len++] = sjw >> 8; 801 | data->payload[data->payload_len++] = sjw & 0xff; 802 | } 803 | 804 | return QUIRC_SUCCESS; 805 | } 806 | 807 | static quirc_decode_error_t decode_eci(struct quirc_data *data, 808 | struct datastream *ds) 809 | { 810 | if (bits_remaining(ds) < 8) 811 | return QUIRC_ERROR_DATA_UNDERFLOW; 812 | 813 | data->eci = take_bits(ds, 8); 814 | 815 | if ((data->eci & 0xc0) == 0x80) { 816 | if (bits_remaining(ds) < 8) 817 | return QUIRC_ERROR_DATA_UNDERFLOW; 818 | 819 | data->eci = (data->eci << 8) | take_bits(ds, 8); 820 | } else if ((data->eci & 0xe0) == 0xc0) { 821 | if (bits_remaining(ds) < 16) 822 | return QUIRC_ERROR_DATA_UNDERFLOW; 823 | 824 | data->eci = (data->eci << 16) | take_bits(ds, 16); 825 | } 826 | 827 | return QUIRC_SUCCESS; 828 | } 829 | 830 | static quirc_decode_error_t decode_payload(struct quirc_data *data, 831 | struct datastream *ds) 832 | { 833 | while (bits_remaining(ds) >= 4) { 834 | quirc_decode_error_t err = QUIRC_SUCCESS; 835 | int type = take_bits(ds, 4); 836 | 837 | switch (type) { 838 | case QUIRC_DATA_TYPE_NUMERIC: 839 | err = decode_numeric(data, ds); 840 | break; 841 | 842 | case QUIRC_DATA_TYPE_ALPHA: 843 | err = decode_alpha(data, ds); 844 | break; 845 | 846 | case QUIRC_DATA_TYPE_BYTE: 847 | err = decode_byte(data, ds); 848 | break; 849 | 850 | case QUIRC_DATA_TYPE_KANJI: 851 | err = decode_kanji(data, ds); 852 | break; 853 | 854 | case 7: 855 | err = decode_eci(data, ds); 856 | break; 857 | 858 | default: 859 | goto done; 860 | } 861 | 862 | if (err) 863 | return err; 864 | 865 | if (!(type & (type - 1)) && (type > data->data_type)) 866 | data->data_type = type; 867 | } 868 | done: 869 | 870 | /* Add nul terminator to all payloads */ 871 | if (data->payload_len >= sizeof(data->payload)) 872 | data->payload_len--; 873 | data->payload[data->payload_len] = 0; 874 | 875 | return QUIRC_SUCCESS; 876 | } 877 | 878 | quirc_decode_error_t quirc_decode(const struct quirc_code *code, 879 | struct quirc_data *data) 880 | { 881 | quirc_decode_error_t err; 882 | struct datastream ds; 883 | 884 | if ((code->size - 17) % 4) 885 | return QUIRC_ERROR_INVALID_GRID_SIZE; 886 | 887 | memset(data, 0, sizeof(*data)); 888 | memset(&ds, 0, sizeof(ds)); 889 | 890 | data->version = (code->size - 17) / 4; 891 | 892 | if (data->version < 1 || 893 | data->version > QUIRC_MAX_VERSION) 894 | return QUIRC_ERROR_INVALID_VERSION; 895 | 896 | /* Read format information -- try both locations */ 897 | err = read_format(code, data, 0); 898 | if (err) 899 | err = read_format(code, data, 1); 900 | if (err) 901 | return err; 902 | 903 | read_data(code, data, &ds); 904 | err = codestream_ecc(data, &ds); 905 | if (err) 906 | return err; 907 | 908 | err = decode_payload(data, &ds); 909 | if (err) 910 | return err; 911 | 912 | return QUIRC_SUCCESS; 913 | } 914 | -------------------------------------------------------------------------------- /source/dir.cpp: -------------------------------------------------------------------------------- 1 | #include "dir.h" 2 | #include "sort.h" 3 | #include 4 | // Gets the upper directory of the current_path 5 | void get_ud(void) { 6 | consoleSelect(&debugscreen); 7 | 8 | char path_to_iterate[MAX_PATH_SIZE]; 9 | strcpy(path_to_iterate, current_path); 10 | 11 | char looking_for[] = "/"; 12 | char *token; 13 | char dummy1[MAX_PATH_SIZE]; 14 | char dummy2[MAX_PATH_SIZE]; 15 | 16 | // Sometimes there is random stuff in here for some reason, so clean both before starting 17 | strcpy(dummy1, ""); 18 | strcpy(dummy2, ""); 19 | 20 | token = strtok(path_to_iterate, looking_for); 21 | 22 | while( token != NULL ) { 23 | // add token (for example "sdmc:" into dummy1 24 | strcat(dummy1, token); 25 | strcat(dummy1, "/"); 26 | 27 | if(strstr(dummy1, current_path) != NULL) { 28 | // dummy2 happens after this, so will have 1 less token 29 | strcpy(current_path, dummy2); 30 | break; 31 | } 32 | 33 | strcat(dummy2, token); 34 | strcat(dummy2, "/"); 35 | 36 | // get the next token288342890277634069 37 | token = strtok(NULL, looking_for); 38 | } 39 | printf("\x1b[32mnew path: %s\x1b[0m\n", current_path); 40 | } 41 | 42 | // Fills file array with all files in a given directory 43 | void get_all_in_dir(char dir_to_show[]) { 44 | // Bottom screen is for debug info. Anything printed in this func will print to bottom screen 45 | consoleSelect(&debugscreen); 46 | 47 | // 2 of each for 2 iterations 48 | DIR *d; 49 | DIR *nd; 50 | d = opendir(dir_to_show); 51 | nd = opendir(dir_to_show); 52 | 53 | if (d) { 54 | // Get rid of strings in file name array 55 | if(current_file[0]!=0) 56 | for (int i = 0; i < size_of_file_array; i++) { free(file_arr[i]); } 57 | 58 | struct dirent *dir; 59 | struct dirent *ndir; 60 | int count = 0; 61 | selected = 0; 62 | scroll = 0; 63 | 64 | // While readdir returns something other than NULL. The variable dir will change each loop 65 | while ((dir = readdir(d)) != NULL) { 66 | // Count how many files there are 67 | count++; 68 | } 69 | 70 | size_of_file_array = count; 71 | count = 0; 72 | 73 | // Create a 2D string array using malloc 74 | // Create an array of pointers the size of the amount of files in the chosen directory 75 | file_arr =(char**)realloc(file_arr, (size_of_file_array+1) * sizeof(char*)); 76 | // Set each pointer inside the array to point to a char 77 | for (int i = 0; i < size_of_file_array; i++) { file_arr[i] =(char*) malloc((MAX_DIR_NAME_SIZE+1) * sizeof(char)); } 78 | isfile_arr =(char*) realloc(isfile_arr, (size_of_file_array+1) * sizeof(bool)); 79 | 80 | // Iterate over dir again, this time adding filenames to created 2D array 81 | while ((ndir = readdir(nd)) != NULL) { 82 | // Get d_name from the dir struct and copy into array 83 | strcpy(file_arr[count], ndir->d_name); 84 | // If d_type is a file 85 | isfile_arr[count] = (ndir->d_type == 8); 86 | count++; 87 | } 88 | } 89 | closedir(d); 90 | closedir(nd); 91 | bubble_sort_files(); 92 | } -------------------------------------------------------------------------------- /source/download.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "download.hpp" 3 | #include 4 | #include 5 | #include <3ds.h> 6 | #include "fs.hpp" 7 | #include "gfx.hpp" 8 | #include "util.hpp" 9 | #include "extract_zip.hpp" 10 | #include 11 | using namespace std; 12 | Result http_download(string url,string loca) 13 | { 14 | Result ret = 0; 15 | u32 statuscode=0; 16 | u32 contentsize=0, readsize=0, size=0x1000; 17 | char a[2048]; 18 | string strNew; 19 | httpcContext context; 20 | extern PrintConsole top,bottom; 21 | consoleSelect(&top); 22 | cout<<"\x1b[2J"; 23 | pBar bar; 24 | fs out; 25 | consoleSelect(&bottom); 26 | cout<<"\x1b[2J"; 27 | bar.length_set(contentsize); 28 | bar.update(0); 29 | bar.print(); 30 | consoleSelect(&top); 31 | cout<<"\x1b[33;1mDownloading : \x1b[37;1m"<= 301 && statuscode <= 303) || (statuscode >= 307 && statuscode <= 308)) 65 | { 66 | char newurl[1024]; 67 | httpcGetResponseHeader(&context, (char*)"Location", newurl, 1024); 68 | httpcCloseContext(&context); 69 | return http_download(newurl,loca); 70 | } 71 | if (statuscode != 200) 72 | return 1; 73 | ret=httpcGetResponseHeader(&context, (char*)"Content-Disposition", a, 2048); 74 | if(ret!=0) 75 | { 76 | int pos = url.find_last_of("/"); 77 | strNew = url.substr(pos+1); 78 | } 79 | else 80 | { 81 | string contents(a,strlen(a)); 82 | int pos = contents.find("="); 83 | strNew = contents.substr (pos+1); 84 | } 85 | if(strNew.find_first_of("\"")!= string::npos) 86 | { 87 | unsigned first=strNew.find_first_of("\""); 88 | unsigned last=strNew.find_last_of("\""); 89 | strNew = strNew.substr(first+1,last-first-1); 90 | } 91 | cout <<"\x1b[33;1mFileName : \x1b[37;1m"<")!=string::npos||strNew.find("|")!=string::npos 105 | ||strNew.find("%")!=string::npos||strNew.empty()) 106 | { 107 | strNew = tl(1,"Please enter the filename along with the extension.Eg:-multi.zip,k.3dsx,file.pdf,etc.","OK"); 108 | cout< 0) { 15 | if (scroll > 0) { printf("/\\\n"); } // Print up arrow showing you can scroll up 16 | else { printf(" \n"); } 17 | // Print directories first 18 | for (i=0; i MAX_FILES_ON_SCREEN) && (selected+scroll != size_of_file_array-1) ) { printf("\n\n\\/ "); }else printf("\n\n "); 36 | } 37 | else { printf("\n\n\t\t\x1b[47;30m- Folder is empty -\x1b[0m"); } 38 | } -------------------------------------------------------------------------------- /source/fs.cpp: -------------------------------------------------------------------------------- 1 | #include "fs.hpp" 2 | using namespace std; 3 | void fs::openfile(string location) 4 | { 5 | fp = fopen(location.c_str(),"w+b"); 6 | } 7 | 8 | void fs::writefile(const char *buffer, size_t size) 9 | { 10 | fwrite(buffer,size,1,fp); 11 | } 12 | void fs::writefile(string location, string buffer) 13 | { 14 | fs::openfile(location); 15 | fs::writefile(buffer.c_str(), buffer.size()); 16 | fs::closefile(); 17 | } 18 | string fs::readfile(string place) 19 | { 20 | ifstream input_file(place); 21 | if (!input_file.is_open()) 22 | { 23 | input_file.close();// check for successful opening 24 | return "error"; 25 | } 26 | string buffer; 27 | input_file >> buffer; 28 | input_file.close(); 29 | return buffer; 30 | } 31 | void fs::closefile() 32 | { 33 | fclose(fp); 34 | } -------------------------------------------------------------------------------- /source/gfx.cpp: -------------------------------------------------------------------------------- 1 | #include "gfx.hpp" 2 | #include "fs.hpp" 3 | #include 4 | #include 5 | #include <3ds.h> 6 | #include 7 | using namespace std; 8 | string ab; 9 | void menu() 10 | { 11 | extern PrintConsole top,bottom; 12 | consoleInit(GFX_BOTTOM, &bottom); 13 | consoleInit(GFX_TOP, &top); 14 | consoleSelect(&top); 15 | draw_border(GFX_TOP,0,255,255); 16 | draw_border(GFX_BOTTOM,255,255,0); 17 | consoleSetWindow(&top, 1, 1, 47, 28); 18 | consoleSetWindow(&bottom, 1, 1, 38, 28); 19 | cout<<"\x1b[14;12H\x1b[1;37mWelcome to Multidownload++"< 3 | * 4 | * Permission to use, copy, modify, and/or distribute this software for any 5 | * purpose with or without fee is hereby granted, provided that the above 6 | * copyright notice and this permission notice appear in all copies. 7 | * 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | */ 16 | 17 | #include 18 | #include 19 | #include 20 | #include "quirc_internal.h" 21 | 22 | /************************************************************************ 23 | * Linear algebra routines 24 | */ 25 | 26 | static int line_intersect(const struct quirc_point *p0, 27 | const struct quirc_point *p1, 28 | const struct quirc_point *q0, 29 | const struct quirc_point *q1, 30 | struct quirc_point *r) 31 | { 32 | /* (a, b) is perpendicular to line p */ 33 | int a = -(p1->y - p0->y); 34 | int b = p1->x - p0->x; 35 | 36 | /* (c, d) is perpendicular to line q */ 37 | int c = -(q1->y - q0->y); 38 | int d = q1->x - q0->x; 39 | 40 | /* e and f are dot products of the respective vectors with p and q */ 41 | int e = a * p1->x + b * p1->y; 42 | int f = c * q1->x + d * q1->y; 43 | 44 | /* Now we need to solve: 45 | * [a b] [rx] [e] 46 | * [c d] [ry] = [f] 47 | * 48 | * We do this by inverting the matrix and applying it to (e, f): 49 | * [ d -b] [e] [rx] 50 | * 1/det [-c a] [f] = [ry] 51 | */ 52 | int det = (a * d) - (b * c); 53 | 54 | if (!det) 55 | return 0; 56 | 57 | r->x = (d * e - b * f) / det; 58 | r->y = (-c * e + a * f) / det; 59 | 60 | return 1; 61 | } 62 | 63 | static void perspective_setup(double *c, 64 | const struct quirc_point *rect, 65 | double w, double h) 66 | { 67 | double x0 = rect[0].x; 68 | double y0 = rect[0].y; 69 | double x1 = rect[1].x; 70 | double y1 = rect[1].y; 71 | double x2 = rect[2].x; 72 | double y2 = rect[2].y; 73 | double x3 = rect[3].x; 74 | double y3 = rect[3].y; 75 | 76 | double wden = w * (x2*y3 - x3*y2 + (x3-x2)*y1 + x1*(y2-y3)); 77 | double hden = h * (x2*y3 + x1*(y2-y3) - x3*y2 + (x3-x2)*y1); 78 | 79 | c[0] = (x1*(x2*y3-x3*y2) + x0*(-x2*y3+x3*y2+(x2-x3)*y1) + 80 | x1*(x3-x2)*y0) / wden; 81 | c[1] = -(x0*(x2*y3+x1*(y2-y3)-x2*y1) - x1*x3*y2 + x2*x3*y1 82 | + (x1*x3-x2*x3)*y0) / hden; 83 | c[2] = x0; 84 | c[3] = (y0*(x1*(y3-y2)-x2*y3+x3*y2) + y1*(x2*y3-x3*y2) + 85 | x0*y1*(y2-y3)) / wden; 86 | c[4] = (x0*(y1*y3-y2*y3) + x1*y2*y3 - x2*y1*y3 + 87 | y0*(x3*y2-x1*y2+(x2-x3)*y1)) / hden; 88 | c[5] = y0; 89 | c[6] = (x1*(y3-y2) + x0*(y2-y3) + (x2-x3)*y1 + (x3-x2)*y0) / wden; 90 | c[7] = (-x2*y3 + x1*y3 + x3*y2 + x0*(y1-y2) - x3*y1 + (x2-x1)*y0) / 91 | hden; 92 | } 93 | 94 | static void perspective_map(const double *c, 95 | double u, double v, struct quirc_point *ret) 96 | { 97 | double den = c[6]*u + c[7]*v + 1.0; 98 | double x = (c[0]*u + c[1]*v + c[2]) / den; 99 | double y = (c[3]*u + c[4]*v + c[5]) / den; 100 | 101 | ret->x = rint(x); 102 | ret->y = rint(y); 103 | } 104 | 105 | static void perspective_unmap(const double *c, 106 | const struct quirc_point *in, 107 | double *u, double *v) 108 | { 109 | double x = in->x; 110 | double y = in->y; 111 | double den = -c[0]*c[7]*y + c[1]*c[6]*y + (c[3]*c[7]-c[4]*c[6])*x + 112 | c[0]*c[4] - c[1]*c[3]; 113 | 114 | *u = -(c[1]*(y-c[5]) - c[2]*c[7]*y + (c[5]*c[7]-c[4])*x + c[2]*c[4]) / 115 | den; 116 | *v = (c[0]*(y-c[5]) - c[2]*c[6]*y + (c[5]*c[6]-c[3])*x + c[2]*c[3]) / 117 | den; 118 | } 119 | 120 | /************************************************************************ 121 | * Span-based floodfill routine 122 | */ 123 | 124 | #define FLOOD_FILL_MAX_DEPTH 4096 125 | 126 | typedef void (*span_func_t)(void *user_data, int y, int left, int right); 127 | 128 | static void flood_fill_seed(struct quirc *q, int x, int y, int from, int to, 129 | span_func_t func, void *user_data, 130 | int depth) 131 | { 132 | int left = x; 133 | int right = x; 134 | int i; 135 | quirc_pixel_t *row = q->pixels + y * q->w; 136 | 137 | if (depth >= FLOOD_FILL_MAX_DEPTH) 138 | return; 139 | 140 | while (left > 0 && row[left - 1] == from) 141 | left--; 142 | 143 | while (right < q->w - 1 && row[right + 1] == from) 144 | right++; 145 | 146 | /* Fill the extent */ 147 | for (i = left; i <= right; i++) 148 | row[i] = to; 149 | 150 | if (func) 151 | func(user_data, y, left, right); 152 | 153 | /* Seed new flood-fills */ 154 | if (y > 0) { 155 | row = q->pixels + (y - 1) * q->w; 156 | 157 | for (i = left; i <= right; i++) 158 | if (row[i] == from) 159 | flood_fill_seed(q, i, y - 1, from, to, 160 | func, user_data, depth + 1); 161 | } 162 | 163 | if (y < q->h - 1) { 164 | row = q->pixels + (y + 1) * q->w; 165 | 166 | for (i = left; i <= right; i++) 167 | if (row[i] == from) 168 | flood_fill_seed(q, i, y + 1, from, to, 169 | func, user_data, depth + 1); 170 | } 171 | } 172 | 173 | /************************************************************************ 174 | * Adaptive thresholding 175 | */ 176 | 177 | #define THRESHOLD_S_MIN 1 178 | #define THRESHOLD_S_DEN 8 179 | #define THRESHOLD_T 5 180 | 181 | static void threshold(struct quirc *q) 182 | { 183 | int x, y; 184 | int avg_w = 0; 185 | int avg_u = 0; 186 | int threshold_s = q->w / THRESHOLD_S_DEN; 187 | quirc_pixel_t *row = q->pixels; 188 | 189 | /* 190 | * Ensure a sane, non-zero value for threshold_s. 191 | * 192 | * threshold_s can be zero if the image width is small. We need to avoid 193 | * SIGFPE as it will be used as divisor. 194 | */ 195 | if (threshold_s < THRESHOLD_S_MIN) 196 | threshold_s = THRESHOLD_S_MIN; 197 | 198 | for (y = 0; y < q->h; y++) { 199 | int row_average[q->w]; 200 | 201 | memset(row_average, 0, sizeof(row_average)); 202 | 203 | for (x = 0; x < q->w; x++) { 204 | int w, u; 205 | 206 | if (y & 1) { 207 | w = x; 208 | u = q->w - 1 - x; 209 | } else { 210 | w = q->w - 1 - x; 211 | u = x; 212 | } 213 | 214 | avg_w = (avg_w * (threshold_s - 1)) / 215 | threshold_s + row[w]; 216 | avg_u = (avg_u * (threshold_s - 1)) / 217 | threshold_s + row[u]; 218 | 219 | row_average[w] += avg_w; 220 | row_average[u] += avg_u; 221 | } 222 | 223 | for (x = 0; x < q->w; x++) { 224 | if (row[x] < row_average[x] * 225 | (100 - THRESHOLD_T) / (200 * threshold_s)) 226 | row[x] = QUIRC_PIXEL_BLACK; 227 | else 228 | row[x] = QUIRC_PIXEL_WHITE; 229 | } 230 | 231 | row += q->w; 232 | } 233 | } 234 | 235 | static void area_count(void *user_data, int y, int left, int right) 236 | { 237 | ((struct quirc_region *)user_data)->count += right - left + 1; 238 | } 239 | 240 | static int region_code(struct quirc *q, int x, int y) 241 | { 242 | int pixel; 243 | struct quirc_region *box; 244 | int region; 245 | 246 | if (x < 0 || y < 0 || x >= q->w || y >= q->h) 247 | return -1; 248 | 249 | pixel = q->pixels[y * q->w + x]; 250 | 251 | if (pixel >= QUIRC_PIXEL_REGION) 252 | return pixel; 253 | 254 | if (pixel == QUIRC_PIXEL_WHITE) 255 | return -1; 256 | 257 | if (q->num_regions >= QUIRC_MAX_REGIONS) 258 | return -1; 259 | 260 | region = q->num_regions; 261 | box = &q->regions[q->num_regions++]; 262 | 263 | memset(box, 0, sizeof(*box)); 264 | 265 | box->seed.x = x; 266 | box->seed.y = y; 267 | box->capstone = -1; 268 | 269 | flood_fill_seed(q, x, y, pixel, region, area_count, box, 0); 270 | 271 | return region; 272 | } 273 | 274 | struct polygon_score_data { 275 | struct quirc_point ref; 276 | 277 | int scores[4]; 278 | struct quirc_point *corners; 279 | }; 280 | 281 | static void find_one_corner(void *user_data, int y, int left, int right) 282 | { 283 | struct polygon_score_data *psd = 284 | (struct polygon_score_data *)user_data; 285 | int xs[2] = {left, right}; 286 | int dy = y - psd->ref.y; 287 | int i; 288 | 289 | for (i = 0; i < 2; i++) { 290 | int dx = xs[i] - psd->ref.x; 291 | int d = dx * dx + dy * dy; 292 | 293 | if (d > psd->scores[0]) { 294 | psd->scores[0] = d; 295 | psd->corners[0].x = xs[i]; 296 | psd->corners[0].y = y; 297 | } 298 | } 299 | } 300 | 301 | static void find_other_corners(void *user_data, int y, int left, int right) 302 | { 303 | struct polygon_score_data *psd = 304 | (struct polygon_score_data *)user_data; 305 | int xs[2] = {left, right}; 306 | int i; 307 | 308 | for (i = 0; i < 2; i++) { 309 | int up = xs[i] * psd->ref.x + y * psd->ref.y; 310 | int right = xs[i] * -psd->ref.y + y * psd->ref.x; 311 | int scores[4] = {up, right, -up, -right}; 312 | int j; 313 | 314 | for (j = 0; j < 4; j++) { 315 | if (scores[j] > psd->scores[j]) { 316 | psd->scores[j] = scores[j]; 317 | psd->corners[j].x = xs[i]; 318 | psd->corners[j].y = y; 319 | } 320 | } 321 | } 322 | } 323 | 324 | static void find_region_corners(struct quirc *q, 325 | int rcode, const struct quirc_point *ref, 326 | struct quirc_point *corners) 327 | { 328 | struct quirc_region *region = &q->regions[rcode]; 329 | struct polygon_score_data psd; 330 | int i; 331 | 332 | memset(&psd, 0, sizeof(psd)); 333 | psd.corners = corners; 334 | 335 | memcpy(&psd.ref, ref, sizeof(psd.ref)); 336 | psd.scores[0] = -1; 337 | flood_fill_seed(q, region->seed.x, region->seed.y, 338 | rcode, QUIRC_PIXEL_BLACK, 339 | find_one_corner, &psd, 0); 340 | 341 | psd.ref.x = psd.corners[0].x - psd.ref.x; 342 | psd.ref.y = psd.corners[0].y - psd.ref.y; 343 | 344 | for (i = 0; i < 4; i++) 345 | memcpy(&psd.corners[i], ®ion->seed, 346 | sizeof(psd.corners[i])); 347 | 348 | i = region->seed.x * psd.ref.x + region->seed.y * psd.ref.y; 349 | psd.scores[0] = i; 350 | psd.scores[2] = -i; 351 | i = region->seed.x * -psd.ref.y + region->seed.y * psd.ref.x; 352 | psd.scores[1] = i; 353 | psd.scores[3] = -i; 354 | 355 | flood_fill_seed(q, region->seed.x, region->seed.y, 356 | QUIRC_PIXEL_BLACK, rcode, 357 | find_other_corners, &psd, 0); 358 | } 359 | 360 | static void record_capstone(struct quirc *q, int ring, int stone) 361 | { 362 | struct quirc_region *stone_reg = &q->regions[stone]; 363 | struct quirc_region *ring_reg = &q->regions[ring]; 364 | struct quirc_capstone *capstone; 365 | int cs_index; 366 | 367 | if (q->num_capstones >= QUIRC_MAX_CAPSTONES) 368 | return; 369 | 370 | cs_index = q->num_capstones; 371 | capstone = &q->capstones[q->num_capstones++]; 372 | 373 | memset(capstone, 0, sizeof(*capstone)); 374 | 375 | capstone->qr_grid = -1; 376 | capstone->ring = ring; 377 | capstone->stone = stone; 378 | stone_reg->capstone = cs_index; 379 | ring_reg->capstone = cs_index; 380 | 381 | /* Find the corners of the ring */ 382 | find_region_corners(q, ring, &stone_reg->seed, capstone->corners); 383 | 384 | /* Set up the perspective transform and find the center */ 385 | perspective_setup(capstone->c, capstone->corners, 7.0, 7.0); 386 | perspective_map(capstone->c, 3.5, 3.5, &capstone->center); 387 | } 388 | 389 | static void test_capstone(struct quirc *q, int x, int y, int *pb) 390 | { 391 | int ring_right = region_code(q, x - pb[4], y); 392 | int stone = region_code(q, x - pb[4] - pb[3] - pb[2], y); 393 | int ring_left = region_code(q, x - pb[4] - pb[3] - 394 | pb[2] - pb[1] - pb[0], 395 | y); 396 | struct quirc_region *stone_reg; 397 | struct quirc_region *ring_reg; 398 | int ratio; 399 | 400 | if (ring_left < 0 || ring_right < 0 || stone < 0) 401 | return; 402 | 403 | /* Left and ring of ring should be connected */ 404 | if (ring_left != ring_right) 405 | return; 406 | 407 | /* Ring should be disconnected from stone */ 408 | if (ring_left == stone) 409 | return; 410 | 411 | stone_reg = &q->regions[stone]; 412 | ring_reg = &q->regions[ring_left]; 413 | 414 | /* Already detected */ 415 | if (stone_reg->capstone >= 0 || ring_reg->capstone >= 0) 416 | return; 417 | 418 | /* Ratio should ideally be 37.5 */ 419 | ratio = stone_reg->count * 100 / ring_reg->count; 420 | if (ratio < 10 || ratio > 70) 421 | return; 422 | 423 | record_capstone(q, ring_left, stone); 424 | } 425 | 426 | static void finder_scan(struct quirc *q, int y) 427 | { 428 | quirc_pixel_t *row = q->pixels + y * q->w; 429 | int x; 430 | int last_color; 431 | int run_length = 0; 432 | int run_count = 0; 433 | int pb[5]; 434 | 435 | memset(pb, 0, sizeof(pb)); 436 | for (x = 0; x < q->w; x++) { 437 | int color = row[x] ? 1 : 0; 438 | 439 | if (x && color != last_color) { 440 | memmove(pb, pb + 1, sizeof(pb[0]) * 4); 441 | pb[4] = run_length; 442 | run_length = 0; 443 | run_count++; 444 | 445 | if (!color && run_count >= 5) { 446 | static int check[5] = {1, 1, 3, 1, 1}; 447 | int avg, err; 448 | int i; 449 | int ok = 1; 450 | 451 | avg = (pb[0] + pb[1] + pb[3] + pb[4]) / 4; 452 | err = avg * 3 / 4; 453 | 454 | for (i = 0; i < 5; i++) 455 | if (pb[i] < check[i] * avg - err || 456 | pb[i] > check[i] * avg + err) 457 | ok = 0; 458 | 459 | if (ok) 460 | test_capstone(q, x, y, pb); 461 | } 462 | } 463 | 464 | run_length++; 465 | last_color = color; 466 | } 467 | } 468 | 469 | static void find_alignment_pattern(struct quirc *q, int index) 470 | { 471 | struct quirc_grid *qr = &q->grids[index]; 472 | struct quirc_capstone *c0 = &q->capstones[qr->caps[0]]; 473 | struct quirc_capstone *c2 = &q->capstones[qr->caps[2]]; 474 | struct quirc_point a; 475 | struct quirc_point b; 476 | struct quirc_point c; 477 | int size_estimate; 478 | int step_size = 1; 479 | int dir = 0; 480 | double u, v; 481 | 482 | /* Grab our previous estimate of the alignment pattern corner */ 483 | memcpy(&b, &qr->align, sizeof(b)); 484 | 485 | /* Guess another two corners of the alignment pattern so that we 486 | * can estimate its size. 487 | */ 488 | perspective_unmap(c0->c, &b, &u, &v); 489 | perspective_map(c0->c, u, v + 1.0, &a); 490 | perspective_unmap(c2->c, &b, &u, &v); 491 | perspective_map(c2->c, u + 1.0, v, &c); 492 | 493 | size_estimate = abs((a.x - b.x) * -(c.y - b.y) + 494 | (a.y - b.y) * (c.x - b.x)); 495 | 496 | /* Spiral outwards from the estimate point until we find something 497 | * roughly the right size. Don't look too far from the estimate 498 | * point. 499 | */ 500 | while (step_size * step_size < size_estimate * 100) { 501 | static const int dx_map[] = {1, 0, -1, 0}; 502 | static const int dy_map[] = {0, -1, 0, 1}; 503 | int i; 504 | 505 | for (i = 0; i < step_size; i++) { 506 | int code = region_code(q, b.x, b.y); 507 | 508 | if (code >= 0) { 509 | struct quirc_region *reg = &q->regions[code]; 510 | 511 | if (reg->count >= size_estimate / 2 && 512 | reg->count <= size_estimate * 2) { 513 | qr->align_region = code; 514 | return; 515 | } 516 | } 517 | 518 | b.x += dx_map[dir]; 519 | b.y += dy_map[dir]; 520 | } 521 | 522 | dir = (dir + 1) % 4; 523 | if (!(dir & 1)) 524 | step_size++; 525 | } 526 | } 527 | 528 | static void find_leftmost_to_line(void *user_data, int y, int left, int right) 529 | { 530 | struct polygon_score_data *psd = 531 | (struct polygon_score_data *)user_data; 532 | int xs[2] = {left, right}; 533 | int i; 534 | 535 | for (i = 0; i < 2; i++) { 536 | int d = -psd->ref.y * xs[i] + psd->ref.x * y; 537 | 538 | if (d < psd->scores[0]) { 539 | psd->scores[0] = d; 540 | psd->corners[0].x = xs[i]; 541 | psd->corners[0].y = y; 542 | } 543 | } 544 | } 545 | 546 | /* Do a Bresenham scan from one point to another and count the number 547 | * of black/white transitions. 548 | */ 549 | static int timing_scan(const struct quirc *q, 550 | const struct quirc_point *p0, 551 | const struct quirc_point *p1) 552 | { 553 | int n = p1->x - p0->x; 554 | int d = p1->y - p0->y; 555 | int x = p0->x; 556 | int y = p0->y; 557 | int *dom, *nondom; 558 | int dom_step; 559 | int nondom_step; 560 | int a = 0; 561 | int i; 562 | int run_length = 0; 563 | int count = 0; 564 | 565 | if (p0->x < 0 || p0->y < 0 || p0->x >= q->w || p0->y >= q->h) 566 | return -1; 567 | if (p1->x < 0 || p1->y < 0 || p1->x >= q->w || p1->y >= q->h) 568 | return -1; 569 | 570 | if (abs(n) > abs(d)) { 571 | int swap = n; 572 | 573 | n = d; 574 | d = swap; 575 | 576 | dom = &x; 577 | nondom = &y; 578 | } else { 579 | dom = &y; 580 | nondom = &x; 581 | } 582 | 583 | if (n < 0) { 584 | n = -n; 585 | nondom_step = -1; 586 | } else { 587 | nondom_step = 1; 588 | } 589 | 590 | if (d < 0) { 591 | d = -d; 592 | dom_step = -1; 593 | } else { 594 | dom_step = 1; 595 | } 596 | 597 | x = p0->x; 598 | y = p0->y; 599 | for (i = 0; i <= d; i++) { 600 | int pixel; 601 | 602 | if (y < 0 || y >= q->h || x < 0 || x >= q->w) 603 | break; 604 | 605 | pixel = q->pixels[y * q->w + x]; 606 | 607 | if (pixel) { 608 | if (run_length >= 2) 609 | count++; 610 | run_length = 0; 611 | } else { 612 | run_length++; 613 | } 614 | 615 | a += n; 616 | *dom += dom_step; 617 | if (a >= d) { 618 | *nondom += nondom_step; 619 | a -= d; 620 | } 621 | } 622 | 623 | return count; 624 | } 625 | 626 | /* Try the measure the timing pattern for a given QR code. This does 627 | * not require the global perspective to have been set up, but it 628 | * does require that the capstone corners have been set to their 629 | * canonical rotation. 630 | * 631 | * For each capstone, we find a point in the middle of the ring band 632 | * which is nearest the centre of the code. Using these points, we do 633 | * a horizontal and a vertical timing scan. 634 | */ 635 | static int measure_timing_pattern(struct quirc *q, int index) 636 | { 637 | struct quirc_grid *qr = &q->grids[index]; 638 | int i; 639 | int scan; 640 | int ver; 641 | int size; 642 | 643 | for (i = 0; i < 3; i++) { 644 | static const double us[] = {6.5, 6.5, 0.5}; 645 | static const double vs[] = {0.5, 6.5, 6.5}; 646 | struct quirc_capstone *cap = &q->capstones[qr->caps[i]]; 647 | 648 | perspective_map(cap->c, us[i], vs[i], &qr->tpep[i]); 649 | } 650 | 651 | qr->hscan = timing_scan(q, &qr->tpep[1], &qr->tpep[2]); 652 | qr->vscan = timing_scan(q, &qr->tpep[1], &qr->tpep[0]); 653 | 654 | scan = qr->hscan; 655 | if (qr->vscan > scan) 656 | scan = qr->vscan; 657 | 658 | /* If neither scan worked, we can't go any further. */ 659 | if (scan < 0) 660 | return -1; 661 | 662 | /* Choose the nearest allowable grid size */ 663 | size = scan * 2 + 13; 664 | ver = (size - 15) / 4; 665 | qr->grid_size = ver * 4 + 17; 666 | 667 | return 0; 668 | } 669 | 670 | /* Read a cell from a grid using the currently set perspective 671 | * transform. Returns +/- 1 for black/white, 0 for cells which are 672 | * out of image bounds. 673 | */ 674 | static int read_cell(const struct quirc *q, int index, int x, int y) 675 | { 676 | const struct quirc_grid *qr = &q->grids[index]; 677 | struct quirc_point p; 678 | 679 | perspective_map(qr->c, x + 0.5, y + 0.5, &p); 680 | if (p.y < 0 || p.y >= q->h || p.x < 0 || p.x >= q->w) 681 | return 0; 682 | 683 | return q->pixels[p.y * q->w + p.x] ? 1 : -1; 684 | } 685 | 686 | static int fitness_cell(const struct quirc *q, int index, int x, int y) 687 | { 688 | const struct quirc_grid *qr = &q->grids[index]; 689 | int score = 0; 690 | int u, v; 691 | 692 | for (v = 0; v < 3; v++) 693 | for (u = 0; u < 3; u++) { 694 | static const double offsets[] = {0.3, 0.5, 0.7}; 695 | struct quirc_point p; 696 | 697 | perspective_map(qr->c, x + offsets[u], 698 | y + offsets[v], &p); 699 | if (p.y < 0 || p.y >= q->h || p.x < 0 || p.x >= q->w) 700 | continue; 701 | 702 | if (q->pixels[p.y * q->w + p.x]) 703 | score++; 704 | else 705 | score--; 706 | } 707 | 708 | return score; 709 | } 710 | 711 | static int fitness_ring(const struct quirc *q, int index, int cx, int cy, 712 | int radius) 713 | { 714 | int i; 715 | int score = 0; 716 | 717 | for (i = 0; i < radius * 2; i++) { 718 | score += fitness_cell(q, index, cx - radius + i, cy - radius); 719 | score += fitness_cell(q, index, cx - radius, cy + radius - i); 720 | score += fitness_cell(q, index, cx + radius, cy - radius + i); 721 | score += fitness_cell(q, index, cx + radius - i, cy + radius); 722 | } 723 | 724 | return score; 725 | } 726 | 727 | static int fitness_apat(const struct quirc *q, int index, int cx, int cy) 728 | { 729 | return fitness_cell(q, index, cx, cy) - 730 | fitness_ring(q, index, cx, cy, 1) + 731 | fitness_ring(q, index, cx, cy, 2); 732 | } 733 | 734 | static int fitness_capstone(const struct quirc *q, int index, int x, int y) 735 | { 736 | x += 3; 737 | y += 3; 738 | 739 | return fitness_cell(q, index, x, y) + 740 | fitness_ring(q, index, x, y, 1) - 741 | fitness_ring(q, index, x, y, 2) + 742 | fitness_ring(q, index, x, y, 3); 743 | } 744 | 745 | /* Compute a fitness score for the currently configured perspective 746 | * transform, using the features we expect to find by scanning the 747 | * grid. 748 | */ 749 | static int fitness_all(const struct quirc *q, int index) 750 | { 751 | const struct quirc_grid *qr = &q->grids[index]; 752 | int version = (qr->grid_size - 17) / 4; 753 | const struct quirc_version_info *info = &quirc_version_db[version]; 754 | int score = 0; 755 | int i, j; 756 | int ap_count; 757 | 758 | /* Check the timing pattern */ 759 | for (i = 0; i < qr->grid_size - 14; i++) { 760 | int expect = (i & 1) ? 1 : -1; 761 | 762 | score += fitness_cell(q, index, i + 7, 6) * expect; 763 | score += fitness_cell(q, index, 6, i + 7) * expect; 764 | } 765 | 766 | /* Check capstones */ 767 | score += fitness_capstone(q, index, 0, 0); 768 | score += fitness_capstone(q, index, qr->grid_size - 7, 0); 769 | score += fitness_capstone(q, index, 0, qr->grid_size - 7); 770 | 771 | if (version < 0 || version > QUIRC_MAX_VERSION) 772 | return score; 773 | 774 | /* Check alignment patterns */ 775 | ap_count = 0; 776 | while (info->apat[ap_count]) 777 | ap_count++; 778 | 779 | for (i = 1; i + 1 < ap_count; i++) { 780 | score += fitness_apat(q, index, 6, info->apat[i]); 781 | score += fitness_apat(q, index, info->apat[i], 6); 782 | } 783 | 784 | for (i = 1; i < ap_count; i++) 785 | for (j = 1; j < ap_count; j++) 786 | score += fitness_apat(q, index, 787 | info->apat[i], info->apat[j]); 788 | 789 | return score; 790 | } 791 | 792 | static void jiggle_perspective(struct quirc *q, int index) 793 | { 794 | struct quirc_grid *qr = &q->grids[index]; 795 | int best = fitness_all(q, index); 796 | int pass; 797 | double adjustments[8]; 798 | int i; 799 | 800 | for (i = 0; i < 8; i++) 801 | adjustments[i] = qr->c[i] * 0.02; 802 | 803 | for (pass = 0; pass < 5; pass++) { 804 | for (i = 0; i < 16; i++) { 805 | int j = i >> 1; 806 | int test; 807 | double old = qr->c[j]; 808 | double step = adjustments[j]; 809 | double new; 810 | 811 | if (i & 1) 812 | new = old + step; 813 | else 814 | new = old - step; 815 | 816 | qr->c[j] = new; 817 | test = fitness_all(q, index); 818 | 819 | if (test > best) 820 | best = test; 821 | else 822 | qr->c[j] = old; 823 | } 824 | 825 | for (i = 0; i < 8; i++) 826 | adjustments[i] *= 0.5; 827 | } 828 | } 829 | 830 | /* Once the capstones are in place and an alignment point has been 831 | * chosen, we call this function to set up a grid-reading perspective 832 | * transform. 833 | */ 834 | static void setup_qr_perspective(struct quirc *q, int index) 835 | { 836 | struct quirc_grid *qr = &q->grids[index]; 837 | struct quirc_point rect[4]; 838 | 839 | /* Set up the perspective map for reading the grid */ 840 | memcpy(&rect[0], &q->capstones[qr->caps[1]].corners[0], 841 | sizeof(rect[0])); 842 | memcpy(&rect[1], &q->capstones[qr->caps[2]].corners[0], 843 | sizeof(rect[0])); 844 | memcpy(&rect[2], &qr->align, sizeof(rect[0])); 845 | memcpy(&rect[3], &q->capstones[qr->caps[0]].corners[0], 846 | sizeof(rect[0])); 847 | perspective_setup(qr->c, rect, qr->grid_size - 7, qr->grid_size - 7); 848 | 849 | jiggle_perspective(q, index); 850 | } 851 | 852 | /* Rotate the capstone with so that corner 0 is the leftmost with respect 853 | * to the given reference line. 854 | */ 855 | static void rotate_capstone(struct quirc_capstone *cap, 856 | const struct quirc_point *h0, 857 | const struct quirc_point *hd) 858 | { 859 | struct quirc_point copy[4]; 860 | int j; 861 | int best; 862 | int best_score; 863 | 864 | for (j = 0; j < 4; j++) { 865 | struct quirc_point *p = &cap->corners[j]; 866 | int score = (p->x - h0->x) * -hd->y + 867 | (p->y - h0->y) * hd->x; 868 | 869 | if (!j || score < best_score) { 870 | best = j; 871 | best_score = score; 872 | } 873 | } 874 | 875 | /* Rotate the capstone */ 876 | for (j = 0; j < 4; j++) 877 | memcpy(©[j], &cap->corners[(j + best) % 4], 878 | sizeof(copy[j])); 879 | memcpy(cap->corners, copy, sizeof(cap->corners)); 880 | perspective_setup(cap->c, cap->corners, 7.0, 7.0); 881 | } 882 | 883 | static void record_qr_grid(struct quirc *q, int a, int b, int c) 884 | { 885 | struct quirc_point h0, hd; 886 | int i; 887 | int qr_index; 888 | struct quirc_grid *qr; 889 | 890 | if (q->num_grids >= QUIRC_MAX_GRIDS) 891 | return; 892 | 893 | /* Construct the hypotenuse line from A to C. B should be to 894 | * the left of this line. 895 | */ 896 | memcpy(&h0, &q->capstones[a].center, sizeof(h0)); 897 | hd.x = q->capstones[c].center.x - q->capstones[a].center.x; 898 | hd.y = q->capstones[c].center.y - q->capstones[a].center.y; 899 | 900 | /* Make sure A-B-C is clockwise */ 901 | if ((q->capstones[b].center.x - h0.x) * -hd.y + 902 | (q->capstones[b].center.y - h0.y) * hd.x > 0) { 903 | int swap = a; 904 | 905 | a = c; 906 | c = swap; 907 | hd.x = -hd.x; 908 | hd.y = -hd.y; 909 | } 910 | 911 | /* Record the grid and its components */ 912 | qr_index = q->num_grids; 913 | qr = &q->grids[q->num_grids++]; 914 | 915 | memset(qr, 0, sizeof(*qr)); 916 | qr->caps[0] = a; 917 | qr->caps[1] = b; 918 | qr->caps[2] = c; 919 | qr->align_region = -1; 920 | 921 | /* Rotate each capstone so that corner 0 is top-left with respect 922 | * to the grid. 923 | */ 924 | for (i = 0; i < 3; i++) { 925 | struct quirc_capstone *cap = &q->capstones[qr->caps[i]]; 926 | 927 | rotate_capstone(cap, &h0, &hd); 928 | cap->qr_grid = qr_index; 929 | } 930 | 931 | /* Check the timing pattern. This doesn't require a perspective 932 | * transform. 933 | */ 934 | if (measure_timing_pattern(q, qr_index) < 0) 935 | goto fail; 936 | 937 | /* Make an estimate based for the alignment pattern based on extending 938 | * lines from capstones A and C. 939 | */ 940 | if (!line_intersect(&q->capstones[a].corners[0], 941 | &q->capstones[a].corners[1], 942 | &q->capstones[c].corners[0], 943 | &q->capstones[c].corners[3], 944 | &qr->align)) 945 | goto fail; 946 | 947 | /* On V2+ grids, we should use the alignment pattern. */ 948 | if (qr->grid_size > 21) { 949 | /* Try to find the actual location of the alignment pattern. */ 950 | find_alignment_pattern(q, qr_index); 951 | 952 | /* Find the point of the alignment pattern closest to the 953 | * top-left of the QR grid. 954 | */ 955 | if (qr->align_region >= 0) { 956 | struct polygon_score_data psd; 957 | struct quirc_region *reg = 958 | &q->regions[qr->align_region]; 959 | 960 | /* Start from some point inside the alignment pattern */ 961 | memcpy(&qr->align, ®->seed, sizeof(qr->align)); 962 | 963 | memcpy(&psd.ref, &hd, sizeof(psd.ref)); 964 | psd.corners = &qr->align; 965 | psd.scores[0] = -hd.y * qr->align.x + 966 | hd.x * qr->align.y; 967 | 968 | flood_fill_seed(q, reg->seed.x, reg->seed.y, 969 | qr->align_region, QUIRC_PIXEL_BLACK, 970 | NULL, NULL, 0); 971 | flood_fill_seed(q, reg->seed.x, reg->seed.y, 972 | QUIRC_PIXEL_BLACK, qr->align_region, 973 | find_leftmost_to_line, &psd, 0); 974 | } 975 | } 976 | 977 | setup_qr_perspective(q, qr_index); 978 | return; 979 | 980 | fail: 981 | /* We've been unable to complete setup for this grid. Undo what we've 982 | * recorded and pretend it never happened. 983 | */ 984 | for (i = 0; i < 3; i++) 985 | q->capstones[qr->caps[i]].qr_grid = -1; 986 | q->num_grids--; 987 | } 988 | 989 | struct neighbour { 990 | int index; 991 | double distance; 992 | }; 993 | 994 | struct neighbour_list { 995 | struct neighbour n[QUIRC_MAX_CAPSTONES]; 996 | int count; 997 | }; 998 | 999 | static void test_neighbours(struct quirc *q, int i, 1000 | const struct neighbour_list *hlist, 1001 | const struct neighbour_list *vlist) 1002 | { 1003 | int j, k; 1004 | double best_score = 0.0; 1005 | int best_h = -1, best_v = -1; 1006 | 1007 | /* Test each possible grouping */ 1008 | for (j = 0; j < hlist->count; j++) 1009 | for (k = 0; k < vlist->count; k++) { 1010 | const struct neighbour *hn = &hlist->n[j]; 1011 | const struct neighbour *vn = &vlist->n[k]; 1012 | double score = fabs(1.0 - hn->distance / vn->distance); 1013 | 1014 | if (score > 2.5) 1015 | continue; 1016 | 1017 | if (best_h < 0 || score < best_score) { 1018 | best_h = hn->index; 1019 | best_v = vn->index; 1020 | best_score = score; 1021 | } 1022 | } 1023 | 1024 | if (best_h < 0 || best_v < 0) 1025 | return; 1026 | 1027 | record_qr_grid(q, best_h, i, best_v); 1028 | } 1029 | 1030 | static void test_grouping(struct quirc *q, int i) 1031 | { 1032 | struct quirc_capstone *c1 = &q->capstones[i]; 1033 | int j; 1034 | struct neighbour_list hlist; 1035 | struct neighbour_list vlist; 1036 | 1037 | if (c1->qr_grid >= 0) 1038 | return; 1039 | 1040 | hlist.count = 0; 1041 | vlist.count = 0; 1042 | 1043 | /* Look for potential neighbours by examining the relative gradients 1044 | * from this capstone to others. 1045 | */ 1046 | for (j = 0; j < q->num_capstones; j++) { 1047 | struct quirc_capstone *c2 = &q->capstones[j]; 1048 | double u, v; 1049 | 1050 | if (i == j || c2->qr_grid >= 0) 1051 | continue; 1052 | 1053 | perspective_unmap(c1->c, &c2->center, &u, &v); 1054 | 1055 | u = fabs(u - 3.5); 1056 | v = fabs(v - 3.5); 1057 | 1058 | if (u < 0.2 * v) { 1059 | struct neighbour *n = &hlist.n[hlist.count++]; 1060 | 1061 | n->index = j; 1062 | n->distance = v; 1063 | } 1064 | 1065 | if (v < 0.2 * u) { 1066 | struct neighbour *n = &vlist.n[vlist.count++]; 1067 | 1068 | n->index = j; 1069 | n->distance = u; 1070 | } 1071 | } 1072 | 1073 | if (!(hlist.count && vlist.count)) 1074 | return; 1075 | 1076 | test_neighbours(q, i, &hlist, &vlist); 1077 | } 1078 | 1079 | static void pixels_setup(struct quirc *q) 1080 | { 1081 | if (sizeof(*q->image) == sizeof(*q->pixels)) { 1082 | q->pixels = (quirc_pixel_t *)q->image; 1083 | } else { 1084 | int x, y; 1085 | for (y = 0; y < q->h; y++) { 1086 | for (x = 0; x < q->w; x++) { 1087 | q->pixels[y * q->w + x] = q->image[y * q->w + x]; 1088 | } 1089 | } 1090 | } 1091 | } 1092 | 1093 | uint8_t *quirc_begin(struct quirc *q, int *w, int *h) 1094 | { 1095 | q->num_regions = QUIRC_PIXEL_REGION; 1096 | q->num_capstones = 0; 1097 | q->num_grids = 0; 1098 | 1099 | if (w) 1100 | *w = q->w; 1101 | if (h) 1102 | *h = q->h; 1103 | 1104 | return q->image; 1105 | } 1106 | 1107 | void quirc_end(struct quirc *q) 1108 | { 1109 | int i; 1110 | 1111 | pixels_setup(q); 1112 | threshold(q); 1113 | 1114 | for (i = 0; i < q->h; i++) 1115 | finder_scan(q, i); 1116 | 1117 | for (i = 0; i < q->num_capstones; i++) 1118 | test_grouping(q, i); 1119 | } 1120 | 1121 | void quirc_extract(const struct quirc *q, int index, 1122 | struct quirc_code *code) 1123 | { 1124 | const struct quirc_grid *qr = &q->grids[index]; 1125 | int y; 1126 | int i = 0; 1127 | 1128 | if (index < 0 || index > q->num_grids) 1129 | return; 1130 | 1131 | memset(code, 0, sizeof(*code)); 1132 | 1133 | perspective_map(qr->c, 0.0, 0.0, &code->corners[0]); 1134 | perspective_map(qr->c, qr->grid_size, 0.0, &code->corners[1]); 1135 | perspective_map(qr->c, qr->grid_size, qr->grid_size, 1136 | &code->corners[2]); 1137 | perspective_map(qr->c, 0.0, qr->grid_size, &code->corners[3]); 1138 | 1139 | code->size = qr->grid_size; 1140 | 1141 | for (y = 0; y < qr->grid_size; y++) { 1142 | int x; 1143 | 1144 | for (x = 0; x < qr->grid_size; x++) { 1145 | if (read_cell(q, index, x, y) > 0) 1146 | code->cell_bitmap[i >> 3] |= (1 << (i & 7)); 1147 | 1148 | i++; 1149 | } 1150 | } 1151 | } 1152 | -------------------------------------------------------------------------------- /source/include/buttons.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "common.h" 4 | 5 | void up(void); 6 | void down(void); 7 | void left(void); 8 | void right(void); 9 | void a_pressed(void); 10 | void b_pressed(void); 11 | -------------------------------------------------------------------------------- /source/include/common.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include // Basic functions 4 | #include // malloc and free 5 | #include // String manipulation 6 | #include // For opendir() and readdir() 7 | #include // rmdir() 8 | #include <3ds.h> // Main 3ds lib (libctru) 9 | #include 10 | 11 | /* 12 | Notes 13 | 14 | According to Wikipedia, the max FAT32 path length is 255 UTF-16 characters, so 255 * 2 = 510 (because the 16 in UTF-16 means 16 bits = 2 bytes) 15 | 16 | all malloc / realloc statements create the length needed +1, because there needs to be room for the end character 17 | 18 | Colours { red = error related, blue = button related, green = path related, white = other } 19 | */ 20 | 21 | extern char current_path[511]; 22 | extern char current_file[511]; 23 | extern int selected; 24 | extern char **file_arr; 25 | extern char **disp_file_arr; 26 | extern char *isfile_arr; 27 | extern std::string ab; 28 | extern int size_of_file_array; 29 | extern int scroll; 30 | extern PrintConsole topScreen; 31 | extern PrintConsole instructionscreen; 32 | extern PrintConsole debugscreen; 33 | extern int MAX_DIR_NAME_SIZE; 34 | extern int MAX_FILES_ON_SCREEN; 35 | extern int MAX_PATH_SIZE; 36 | int xplorer(void); -------------------------------------------------------------------------------- /source/include/dir.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "common.h" 4 | 5 | void get_ud(void); 6 | void get_all_in_dir(char dir_to_show[]); 7 | -------------------------------------------------------------------------------- /source/include/download.hpp: -------------------------------------------------------------------------------- 1 | #include <3ds.h> 2 | #include 3 | #include "gfx.hpp" 4 | Result http_download(std::string url,std::string loca); -------------------------------------------------------------------------------- /source/include/draw.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "common.h" 4 | 5 | void print_all_values_in_filear(void); 6 | -------------------------------------------------------------------------------- /source/include/extract_zip.hpp: -------------------------------------------------------------------------------- 1 | #include 2 | #ifndef extract_zip_hpp 3 | #define extarct_zip_hpp 4 | void extract(std::string location); 5 | #endif -------------------------------------------------------------------------------- /source/include/fs.hpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | class fs{ 7 | public: 8 | void openfile(string location); 9 | void writefile(const char *buffer, size_t size); 10 | void writefile(string location, string buffer); 11 | void closefile(); 12 | string readfile(string place); 13 | private: 14 | FILE *fp; 15 | }; 16 | -------------------------------------------------------------------------------- /source/include/gfx.hpp: -------------------------------------------------------------------------------- 1 | #include <3ds.h> 2 | #include 3 | #ifndef gfx_hpp 4 | #define gfx_hpp 5 | void drawpixel(u8* screen, u32 x,u32 y,u8 r,u8 g, u8 b); 6 | void drawline(u8 *screen,u32 x,u32 y,u8 r,u8 g, u8 b,int width,int j); 7 | void draw_border(gfxScreen_t screen,u8 r ,u8 g,u8 b); 8 | void menu(); 9 | class pBar { 10 | public: 11 | void update(double newProgress); 12 | void print(); 13 | void length_set(int size); 14 | std::string firstPartOfpBar = "[", 15 | lastPartOfpBar = "]", 16 | pBarFiller = "=", 17 | pBarUpdater = ">"; 18 | double neededProgress = 0; 19 | private: 20 | int amountOfFiller, 21 | pBarLength = 35, //I would recommend NOT changing this 22 | currUpdateVal = 0; //Do not change 23 | double currentProgress = 0; //Do not change 24 | }; 25 | #endif -------------------------------------------------------------------------------- /source/include/qrdec.h: -------------------------------------------------------------------------------- 1 | #ifndef QR_FILE 2 | #define QR_FILE 3 | char * qr_dec(); 4 | #endif 5 | -------------------------------------------------------------------------------- /source/include/quirc.h: -------------------------------------------------------------------------------- 1 | /* quirc -- QR-code recognition library 2 | * Copyright (C) 2010-2012 Daniel Beer 3 | * 4 | * Permission to use, copy, modify, and/or distribute this software for any 5 | * purpose with or without fee is hereby granted, provided that the above 6 | * copyright notice and this permission notice appear in all copies. 7 | * 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | */ 16 | 17 | #ifndef QUIRC_H_ 18 | #define QUIRC_H_ 19 | 20 | #include 21 | 22 | #ifdef __cplusplus 23 | extern "C" { 24 | #endif 25 | 26 | struct quirc; 27 | 28 | /* Obtain the library version string. */ 29 | const char *quirc_version(void); 30 | 31 | /* Construct a new QR-code recognizer. This function will return NULL 32 | * if sufficient memory could not be allocated. 33 | */ 34 | struct quirc *quirc_new(void); 35 | 36 | /* Destroy a QR-code recognizer. */ 37 | void quirc_destroy(struct quirc *q); 38 | 39 | /* Resize the QR-code recognizer. The size of an image must be 40 | * specified before codes can be analyzed. 41 | * 42 | * This function returns 0 on success, or -1 if sufficient memory could 43 | * not be allocated. 44 | */ 45 | int quirc_resize(struct quirc *q, int w, int h); 46 | 47 | /* These functions are used to process images for QR-code recognition. 48 | * quirc_begin() must first be called to obtain access to a buffer into 49 | * which the input image should be placed. Optionally, the current 50 | * width and height may be returned. 51 | * 52 | * After filling the buffer, quirc_end() should be called to process 53 | * the image for QR-code recognition. The locations and content of each 54 | * code may be obtained using accessor functions described below. 55 | */ 56 | uint8_t *quirc_begin(struct quirc *q, int *w, int *h); 57 | void quirc_end(struct quirc *q); 58 | 59 | /* This structure describes a location in the input image buffer. */ 60 | struct quirc_point { 61 | int x; 62 | int y; 63 | }; 64 | 65 | /* This enum describes the various decoder errors which may occur. */ 66 | typedef enum { 67 | QUIRC_SUCCESS = 0, 68 | QUIRC_ERROR_INVALID_GRID_SIZE, 69 | QUIRC_ERROR_INVALID_VERSION, 70 | QUIRC_ERROR_FORMAT_ECC, 71 | QUIRC_ERROR_DATA_ECC, 72 | QUIRC_ERROR_UNKNOWN_DATA_TYPE, 73 | QUIRC_ERROR_DATA_OVERFLOW, 74 | QUIRC_ERROR_DATA_UNDERFLOW 75 | } quirc_decode_error_t; 76 | 77 | /* Return a string error message for an error code. */ 78 | const char *quirc_strerror(quirc_decode_error_t err); 79 | 80 | /* Limits on the maximum size of QR-codes and their content. */ 81 | #define QUIRC_MAX_BITMAP 3917 82 | #define QUIRC_MAX_PAYLOAD 8896 83 | 84 | /* QR-code ECC types. */ 85 | #define QUIRC_ECC_LEVEL_M 0 86 | #define QUIRC_ECC_LEVEL_L 1 87 | #define QUIRC_ECC_LEVEL_H 2 88 | #define QUIRC_ECC_LEVEL_Q 3 89 | 90 | /* QR-code data types. */ 91 | #define QUIRC_DATA_TYPE_NUMERIC 1 92 | #define QUIRC_DATA_TYPE_ALPHA 2 93 | #define QUIRC_DATA_TYPE_BYTE 4 94 | #define QUIRC_DATA_TYPE_KANJI 8 95 | 96 | /* Common character encodings */ 97 | #define QUIRC_ECI_ISO_8859_1 1 98 | #define QUIRC_ECI_IBM437 2 99 | #define QUIRC_ECI_ISO_8859_2 4 100 | #define QUIRC_ECI_ISO_8859_3 5 101 | #define QUIRC_ECI_ISO_8859_4 6 102 | #define QUIRC_ECI_ISO_8859_5 7 103 | #define QUIRC_ECI_ISO_8859_6 8 104 | #define QUIRC_ECI_ISO_8859_7 9 105 | #define QUIRC_ECI_ISO_8859_8 10 106 | #define QUIRC_ECI_ISO_8859_9 11 107 | #define QUIRC_ECI_WINDOWS_874 13 108 | #define QUIRC_ECI_ISO_8859_13 15 109 | #define QUIRC_ECI_ISO_8859_15 17 110 | #define QUIRC_ECI_SHIFT_JIS 20 111 | #define QUIRC_ECI_UTF_8 26 112 | 113 | /* This structure is used to return information about detected QR codes 114 | * in the input image. 115 | */ 116 | struct quirc_code { 117 | /* The four corners of the QR-code, from top left, clockwise */ 118 | struct quirc_point corners[4]; 119 | 120 | /* The number of cells across in the QR-code. The cell bitmap 121 | * is a bitmask giving the actual values of cells. If the cell 122 | * at (x, y) is black, then the following bit is set: 123 | * 124 | * cell_bitmap[i << 3] & (1 << (i & 7)) 125 | * 126 | * where i = (y * size) + x. 127 | */ 128 | int size; 129 | uint8_t cell_bitmap[QUIRC_MAX_BITMAP]; 130 | }; 131 | 132 | /* This structure holds the decoded QR-code data */ 133 | struct quirc_data { 134 | /* Various parameters of the QR-code. These can mostly be 135 | * ignored if you only care about the data. 136 | */ 137 | int version; 138 | int ecc_level; 139 | int mask; 140 | 141 | /* This field is the highest-valued data type found in the QR 142 | * code. 143 | */ 144 | int data_type; 145 | 146 | /* Data payload. For the Kanji datatype, payload is encoded as 147 | * Shift-JIS. For all other datatypes, payload is ASCII text. 148 | */ 149 | uint8_t payload[QUIRC_MAX_PAYLOAD]; 150 | int payload_len; 151 | 152 | /* ECI assignment number */ 153 | uint32_t eci; 154 | }; 155 | 156 | /* Return the number of QR-codes identified in the last processed 157 | * image. 158 | */ 159 | int quirc_count(const struct quirc *q); 160 | 161 | /* Extract the QR-code specified by the given index. */ 162 | void quirc_extract(const struct quirc *q, int index, 163 | struct quirc_code *code); 164 | 165 | /* Decode a QR-code, returning the payload data. */ 166 | quirc_decode_error_t quirc_decode(const struct quirc_code *code, 167 | struct quirc_data *data); 168 | 169 | #ifdef __cplusplus 170 | } 171 | #endif 172 | 173 | #endif 174 | -------------------------------------------------------------------------------- /source/include/quirc_internal.h: -------------------------------------------------------------------------------- 1 | /* quirc -- QR-code recognition library 2 | * Copyright (C) 2010-2012 Daniel Beer 3 | * 4 | * Permission to use, copy, modify, and/or distribute this software for any 5 | * purpose with or without fee is hereby granted, provided that the above 6 | * copyright notice and this permission notice appear in all copies. 7 | * 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | */ 16 | 17 | #ifndef QUIRC_INTERNAL_H_ 18 | #define QUIRC_INTERNAL_H_ 19 | 20 | #include "quirc.h" 21 | 22 | #define QUIRC_PIXEL_WHITE 0 23 | #define QUIRC_PIXEL_BLACK 1 24 | #define QUIRC_PIXEL_REGION 2 25 | 26 | #ifndef QUIRC_MAX_REGIONS 27 | #define QUIRC_MAX_REGIONS 254 28 | #endif 29 | #define QUIRC_MAX_CAPSTONES 32 30 | #define QUIRC_MAX_GRIDS 8 31 | 32 | #define QUIRC_PERSPECTIVE_PARAMS 8 33 | 34 | #if QUIRC_MAX_REGIONS < UINT8_MAX 35 | typedef uint8_t quirc_pixel_t; 36 | #elif QUIRC_MAX_REGIONS < UINT16_MAX 37 | typedef uint16_t quirc_pixel_t; 38 | #else 39 | #error "QUIRC_MAX_REGIONS > 65534 is not supported" 40 | #endif 41 | 42 | struct quirc_region { 43 | struct quirc_point seed; 44 | int count; 45 | int capstone; 46 | }; 47 | 48 | struct quirc_capstone { 49 | int ring; 50 | int stone; 51 | 52 | struct quirc_point corners[4]; 53 | struct quirc_point center; 54 | double c[QUIRC_PERSPECTIVE_PARAMS]; 55 | 56 | int qr_grid; 57 | }; 58 | 59 | struct quirc_grid { 60 | /* Capstone indices */ 61 | int caps[3]; 62 | 63 | /* Alignment pattern region and corner */ 64 | int align_region; 65 | struct quirc_point align; 66 | 67 | /* Timing pattern endpoints */ 68 | struct quirc_point tpep[3]; 69 | int hscan; 70 | int vscan; 71 | 72 | /* Grid size and perspective transform */ 73 | int grid_size; 74 | double c[QUIRC_PERSPECTIVE_PARAMS]; 75 | }; 76 | 77 | struct quirc { 78 | uint8_t *image; 79 | quirc_pixel_t *pixels; 80 | int w; 81 | int h; 82 | 83 | int num_regions; 84 | struct quirc_region regions[QUIRC_MAX_REGIONS]; 85 | 86 | int num_capstones; 87 | struct quirc_capstone capstones[QUIRC_MAX_CAPSTONES]; 88 | 89 | int num_grids; 90 | struct quirc_grid grids[QUIRC_MAX_GRIDS]; 91 | }; 92 | 93 | /************************************************************************ 94 | * QR-code version information database 95 | */ 96 | 97 | #define QUIRC_MAX_VERSION 40 98 | #define QUIRC_MAX_ALIGNMENT 7 99 | 100 | struct quirc_rs_params { 101 | int bs; /* Small block size */ 102 | int dw; /* Small data words */ 103 | int ns; /* Number of small blocks */ 104 | }; 105 | 106 | struct quirc_version_info { 107 | int data_bytes; 108 | int apat[QUIRC_MAX_ALIGNMENT]; 109 | struct quirc_rs_params ecc[4]; 110 | }; 111 | 112 | extern const struct quirc_version_info quirc_version_db[QUIRC_MAX_VERSION + 1]; 113 | 114 | #endif 115 | -------------------------------------------------------------------------------- /source/include/sort.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "common.h" 4 | 5 | void bubble_sort_files(void); 6 | -------------------------------------------------------------------------------- /source/include/util.hpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | std::string tl(int buttons, std::string texgen, const char *right); -------------------------------------------------------------------------------- /source/main.cpp: -------------------------------------------------------------------------------- 1 | #include <3ds.h> 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "gfx.hpp" 7 | #include "fs.hpp" 8 | #include "download.hpp" 9 | #include "util.hpp" 10 | #include "common.h" 11 | #include "qrdec.h" 12 | u32 __stacksize__ =0x40000; 13 | 14 | using namespace std; 15 | touchPosition touch; 16 | PrintConsole top,bottom; 17 | bool touchInBox(touchPosition touch, int x, int y, int w, int h) 18 | { 19 | int tx=touch.px; 20 | int ty=touch.py; 21 | u32 kDown = hidKeysDown(); 22 | if (kDown & KEY_TOUCH && tx > x && tx < x+w && ty > y && ty < y+h) { 23 | return true; 24 | } else { 25 | return false; 26 | } 27 | } 28 | 29 | int main() 30 | { 31 | gfxInitDefault(); 32 | menu(); 33 | consoleSelect(&top); 34 | hidInit(); 35 | Result ret; 36 | httpcInit(0x1000); 37 | while (aptMainLoop()) { 38 | 39 | hidScanInput(); 40 | hidTouchRead(&touch); 41 | 42 | u32 kDown=hidKeysDown(); 43 | if (kDown & KEY_START) { 44 | break; 45 | } 46 | if(touchInBox(touch,5,25,300,25)) 47 | { 48 | string b=tl(2,"Enter URL","Download"); 49 | consoleSelect(&top); 50 | cout<<"\x1b[2J"; 51 | ret=http_download(b,ab); 52 | if(ret==0) 53 | cout<<"\x1b[33;1mDownload :\x1b[32;1m Success\x1b[37;1m"< 2 | #include 3 | #include 4 | #include 5 | #include <3ds.h> 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include "gfx.hpp" 12 | extern "C"{ 13 | #include "quirc.h" 14 | } 15 | #define WIDTH 400 16 | #define HEIGHT 240 17 | #define SCREEN_SIZE WIDTH * HEIGHT * 2 18 | #define BUF_SIZE SCREEN_SIZE * 2 19 | #define WAIT_TIMEOUT 300000000ULL 20 | char qurl[1024]; 21 | 22 | void writePictureToIntensityMap(void *fb, void *img, u16 width, u16 height){ 23 | u8 *fb_8 = (u8*) fb; 24 | u16 *img_16 = (u16*) img; 25 | for(u32 i = 0; i < width * height; i++){ 26 | u16 data = img_16[i]; 27 | uint8_t b = ((data >> 11) & 0x1F) << 3; 28 | uint8_t g = ((data >> 5) & 0x3F) << 2; 29 | uint8_t r = (data & 0x1F) << 3; 30 | fb_8[i] = (r + g + b) / 3; 31 | } 32 | } 33 | 34 | void bhm_line(void *fb,int x1,int y1,int x2,int y2,u32 c) 35 | { 36 | int x,y,dx,dy,dx1,dy1,px,py,xe,ye,i; 37 | dx=x2-x1; 38 | dy=y2-y1; 39 | dx1=fabs(dx); 40 | dy1=fabs(dy); 41 | px=2*dy1-dx1; 42 | py=2*dx1-dy1; 43 | if(dy1<=dx1){ 44 | if(dx>=0){ 45 | x=x1; 46 | y=y1; 47 | xe=x2; 48 | } else { 49 | x=x2; 50 | y=y2; 51 | xe=x1; 52 | } 53 | drawpixel((u8*)fb,x,y,255,255,255); 54 | for(i=0;x0 && dy>0)){ 60 | y=y+1; 61 | } else { 62 | y=y-1; 63 | } 64 | px=px+2*(dy1-dx1); 65 | } 66 | drawpixel((u8*)fb,x,y,255,255,255); 67 | } 68 | } else { 69 | if(dy>=0){ 70 | x=x1; 71 | y=y1; 72 | ye=y2; 73 | } else { 74 | x=x2; 75 | y=y2; 76 | ye=y1; 77 | } 78 | drawpixel((u8*)fb,x,y,255,255,255); 79 | for(i=0;y0 && dy>0)){ 85 | x=x+1; 86 | } else { 87 | x=x-1; 88 | } 89 | py=py+2*(dx1-dy1); 90 | } 91 | drawpixel((u8*)fb,x,y,255,255,255); 92 | } 93 | } 94 | } 95 | 96 | 97 | void cleanup() { 98 | camExit(); 99 | } 100 | 101 | void writePictureToFramebufferRGB565(void *fb, void *img, u16 x, u16 y, u16 width, u16 height) { 102 | u8 *fb_8 = (u8*) fb; 103 | u16 *img_16 = (u16*) img; 104 | int i, j, draw_x, draw_y; 105 | for(j = 0; j < height; j++) { 106 | for(i = 0; i < width; i++) { 107 | draw_y = y + height - j; 108 | draw_x = x + i; 109 | u16 data = img_16[j * width + i]; 110 | uint8_t b = ((data >> 11) & 0x1F) << 3; 111 | uint8_t g = ((data >> 5) & 0x3F) << 2; 112 | uint8_t r = (data & 0x1F) << 3; 113 | drawpixel(fb_8,i,j,r,g,b); 114 | } 115 | } 116 | } 117 | 118 | // TODO: Figure out how to use CAMU_GetStereoCameraCalibrationData 119 | void takePicture3D(u16 *buf){ 120 | u32 bufSize; 121 | Handle camReceiveEvent = 0; 122 | 123 | CAMU_GetMaxBytes(&bufSize, WIDTH, HEIGHT); 124 | CAMU_SetTransferBytes(PORT_CAM1, bufSize, WIDTH, HEIGHT); 125 | 126 | CAMU_ClearBuffer(PORT_CAM1); 127 | CAMU_StartCapture(PORT_CAM1); 128 | CAMU_SetReceiving(&camReceiveEvent, (u8*)buf, PORT_CAM1, WIDTH * HEIGHT * 2, (s16) bufSize); 129 | svcWaitSynchronization(camReceiveEvent, WAIT_TIMEOUT); 130 | 131 | svcCloseHandle(camReceiveEvent); 132 | //CAMU_Activate(SELECT_NONE); 133 | } 134 | 135 | char * qr_dec() 136 | { 137 | // Initializations 138 | // Save current stack frame for easy exit 139 | u32 kDown; 140 | u32 kHeld; 141 | printf("Initializing camera\n"); 142 | gfxSetScreenFormat(GFX_TOP,GSP_RGB565_OES); 143 | // Enable double buffering to remove screen tearing 144 | gfxFlushBuffers(); 145 | gfxSwapBuffers(); 146 | printf("camInit: 0x%08X\n", (unsigned int) camInit()); 147 | CAMU_Activate(SELECT_OUT1); 148 | printf("CAMU_SetSize: 0x%08X\n", (unsigned int) CAMU_SetSize(SELECT_OUT1, SIZE_CTR_TOP_LCD, CONTEXT_A)); 149 | printf("CAMU_SetOutputFormat: 0x%08X\n", (unsigned int) CAMU_SetOutputFormat(SELECT_OUT1, OUTPUT_RGB_565, CONTEXT_A)); 150 | CAMU_SetFrameRate(SELECT_OUT1, FRAME_RATE_30); 151 | printf("CAMU_SetNoiseFilter: 0x%08X\n", (unsigned int) CAMU_SetNoiseFilter(SELECT_OUT1, true)); 152 | printf("CAMU_SetAutoExposure: 0x%08X\n", (unsigned int) CAMU_SetAutoExposure(SELECT_OUT1, true)); 153 | printf("CAMU_SetAutoWhiteBalance: 0x%08X\n", (unsigned int) CAMU_SetAutoWhiteBalance(SELECT_OUT1, true)); 154 | //printf("CAMU_SetEffect: 0x%08X\n", (unsigned int) CAMU_SetEffect(SELECT_OUT1_OUT2, EFFECT_MONO, CONTEXT_A)); 155 | 156 | printf("CAMU_SetTrimming: 0x%08X\n", (unsigned int) CAMU_SetTrimming(PORT_CAM1, false)); 157 | //printf("CAMU_SetTrimmingParamsCenter: 0x%08X\n", (unsigned int) CAMU_SetTrimmingParamsCenter(PORT_CAM1, 512, 240, 512, 384)); 158 | struct quirc *qr; 159 | qr = quirc_new(); 160 | if (!qr){ 161 | printf("Failed to allocate memory"); 162 | return 0; 163 | } 164 | 165 | if (quirc_resize(qr, WIDTH, HEIGHT) < 0){ 166 | printf("Failed to allocate video memory"); 167 | return 0; 168 | } 169 | 170 | u16 *buf =(u16*) malloc(BUF_SIZE); 171 | 172 | gfxFlushBuffers(); 173 | gfxSwapBuffers(); 174 | struct quirc_data data; 175 | // Main loop 176 | while (aptMainLoop()) { 177 | // Read which buttons are currently pressed or not 178 | hidScanInput(); 179 | kDown = hidKeysDown(); 180 | // If START button is pressed, break loop and quit 181 | if (kDown & KEY_START) { 182 | break; 183 | } 184 | takePicture3D(buf); 185 | writePictureToFramebufferRGB565(gfxGetFramebuffer(GFX_TOP ,GFX_LEFT,NULL, NULL), buf, 0, 0, WIDTH, HEIGHT); 186 | int w=WIDTH, h=HEIGHT; 187 | u8 *image = (u8*)quirc_begin(qr, &w, &h); 188 | writePictureToIntensityMap(image, buf, WIDTH, HEIGHT); 189 | quirc_end(qr); 190 | 191 | int num_codes = quirc_count(qr); 192 | 193 | for (int i = 0; i < num_codes; i++){ 194 | struct quirc_code code; 195 | 196 | quirc_decode_error_t err; 197 | 198 | quirc_extract(qr, i, &code); 199 | 200 | err = quirc_decode(&code, &data); 201 | if (!err){ 202 | CAMU_Activate(SELECT_NONE); 203 | printf("\x1b[32;1mDECODE SUCCESSFUL %s\n\x1b[37;1m",data.payload); 204 | printf("\x1b[32;1mSuccesful 3\n\x1b[37;1m"); 205 | free(buf); 206 | printf("\x1b[32;1mSuccesful 4\n\x1b[37;1m"); 207 | cleanup(); 208 | quirc_destroy(qr); 209 | printf("\x1b[32;1mSuccesful 5\n\x1b[37;1m"); 210 | printf("\x1b[32;1mSuccesful 1\x1b[37;1m"); 211 | strcpy(qurl,(char*)data.payload); 212 | return qurl; 213 | } 214 | gspWaitForVBlank(); 215 | gfxSwapBuffers(); 216 | } 217 | } 218 | return "ERROR"; 219 | } -------------------------------------------------------------------------------- /source/quirc.c: -------------------------------------------------------------------------------- 1 | /* quirc -- QR-code recognition library 2 | * Copyright (C) 2010-2012 Daniel Beer 3 | * 4 | * Permission to use, copy, modify, and/or distribute this software for any 5 | * purpose with or without fee is hereby granted, provided that the above 6 | * copyright notice and this permission notice appear in all copies. 7 | * 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | */ 16 | 17 | #include 18 | #include 19 | #include "quirc_internal.h" 20 | 21 | const char *quirc_version(void) 22 | { 23 | return "1.0"; 24 | } 25 | 26 | struct quirc *quirc_new(void) 27 | { 28 | struct quirc *q = malloc(sizeof(*q)); 29 | 30 | if (!q) 31 | return NULL; 32 | 33 | memset(q, 0, sizeof(*q)); 34 | return q; 35 | } 36 | 37 | void quirc_destroy(struct quirc *q) 38 | { 39 | if (q->image) 40 | free(q->image); 41 | if (sizeof(*q->image) != sizeof(*q->pixels)) 42 | free(q->pixels); 43 | 44 | free(q); 45 | } 46 | 47 | int quirc_resize(struct quirc *q, int w, int h) 48 | { 49 | uint8_t *new_image = realloc(q->image, w * h); 50 | 51 | if (!new_image) 52 | return -1; 53 | 54 | if (sizeof(*q->image) != sizeof(*q->pixels)) { 55 | size_t new_size = w * h * sizeof(quirc_pixel_t); 56 | quirc_pixel_t *new_pixels = realloc(q->pixels, new_size); 57 | if (!new_pixels) 58 | return -1; 59 | q->pixels = new_pixels; 60 | } 61 | 62 | q->image = new_image; 63 | q->w = w; 64 | q->h = h; 65 | 66 | return 0; 67 | } 68 | 69 | int quirc_count(const struct quirc *q) 70 | { 71 | return q->num_grids; 72 | } 73 | 74 | static const char *const error_table[] = { 75 | [QUIRC_SUCCESS] = "Success", 76 | [QUIRC_ERROR_INVALID_GRID_SIZE] = "Invalid grid size", 77 | [QUIRC_ERROR_INVALID_VERSION] = "Invalid version", 78 | [QUIRC_ERROR_FORMAT_ECC] = "Format data ECC failure", 79 | [QUIRC_ERROR_DATA_ECC] = "ECC failure", 80 | [QUIRC_ERROR_UNKNOWN_DATA_TYPE] = "Unknown data type", 81 | [QUIRC_ERROR_DATA_OVERFLOW] = "Data overflow", 82 | [QUIRC_ERROR_DATA_UNDERFLOW] = "Data underflow" 83 | }; 84 | 85 | const char *quirc_strerror(quirc_decode_error_t err) 86 | { 87 | if (err >= 0 && err < sizeof(error_table) / sizeof(error_table[0])) 88 | return error_table[err]; 89 | 90 | return "Unknown error"; 91 | } 92 | -------------------------------------------------------------------------------- /source/sort.cpp: -------------------------------------------------------------------------------- 1 | #include "sort.h" 2 | 3 | // Sort given array (yay for bubble sort) 4 | void bubble_sort_files(void) { 5 | bool swapped = true; 6 | char temp[MAX_DIR_NAME_SIZE]; 7 | int inttemp; 8 | int pass; 9 | int pass2; 10 | 11 | for (pass=size_of_file_array-1; pass>0; pass--) { 12 | for (pass2=0; pass2 0) { 14 | swapped = true; 15 | 16 | // Swap values in file name array 17 | strcpy(temp, file_arr[pass2]); 18 | strcpy(file_arr[pass2], file_arr[pass2+1]); 19 | strcpy(file_arr[pass2+1], temp); 20 | 21 | // Swap values in isfile array 22 | inttemp = isfile_arr[pass2]; 23 | isfile_arr[pass2] = isfile_arr[pass2+1]; 24 | isfile_arr[pass2+1] = inttemp; 25 | 26 | } 27 | } 28 | if (!swapped) { break; } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /source/util.cpp: -------------------------------------------------------------------------------- 1 | #include "util.hpp" 2 | #include 3 | #include 4 | #include <3ds.h> 5 | using namespace std; 6 | char mybuf[1024]; 7 | string tl(int buttons,string texgen,const char * right) 8 | { 9 | SwkbdState swkbd; 10 | SwkbdButton button = SWKBD_BUTTON_NONE; 11 | swkbdInit(&swkbd, SWKBD_TYPE_NORMAL, buttons, -1); 12 | swkbdSetHintText(&swkbd, texgen.c_str()); 13 | if(buttons==2){ 14 | swkbdSetButton (&swkbd, SWKBD_BUTTON_LEFT,"Cancel", true); 15 | swkbdSetButton (&swkbd, SWKBD_BUTTON_RIGHT,right, true); 16 | } 17 | else 18 | swkbdSetButton (&swkbd, SWKBD_BUTTON_LEFT,right, true); 19 | swkbdSetValidation(&swkbd, SWKBD_NOTEMPTY_NOTBLANK, 0, 0); 20 | button = swkbdInputText(&swkbd, mybuf, sizeof(mybuf)); 21 | return mybuf; 22 | } -------------------------------------------------------------------------------- /source/version_db.c: -------------------------------------------------------------------------------- 1 | /* quirc -- QR-code recognition library 2 | * Copyright (C) 2010-2012 Daniel Beer 3 | * 4 | * Permission to use, copy, modify, and/or distribute this software for any 5 | * purpose with or without fee is hereby granted, provided that the above 6 | * copyright notice and this permission notice appear in all copies. 7 | * 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 | */ 16 | 17 | #include "quirc_internal.h" 18 | 19 | const struct quirc_version_info quirc_version_db[QUIRC_MAX_VERSION + 1] = { 20 | {0}, 21 | { /* Version 1 */ 22 | .data_bytes = 26, 23 | .apat = {0}, 24 | .ecc = { 25 | {.bs = 26, .dw = 16, .ns = 1}, 26 | {.bs = 26, .dw = 19, .ns = 1}, 27 | {.bs = 26, .dw = 9, .ns = 1}, 28 | {.bs = 26, .dw = 13, .ns = 1} 29 | } 30 | }, 31 | { /* Version 2 */ 32 | .data_bytes = 44, 33 | .apat = {6, 18, 0}, 34 | .ecc = { 35 | {.bs = 44, .dw = 28, .ns = 1}, 36 | {.bs = 44, .dw = 34, .ns = 1}, 37 | {.bs = 44, .dw = 16, .ns = 1}, 38 | {.bs = 44, .dw = 22, .ns = 1} 39 | } 40 | }, 41 | { /* Version 3 */ 42 | .data_bytes = 70, 43 | .apat = {6, 22, 0}, 44 | .ecc = { 45 | {.bs = 70, .dw = 44, .ns = 1}, 46 | {.bs = 70, .dw = 55, .ns = 1}, 47 | {.bs = 35, .dw = 13, .ns = 2}, 48 | {.bs = 35, .dw = 17, .ns = 2} 49 | } 50 | }, 51 | { /* Version 4 */ 52 | .data_bytes = 100, 53 | .apat = {6, 26, 0}, 54 | .ecc = { 55 | {.bs = 50, .dw = 32, .ns = 2}, 56 | {.bs = 100, .dw = 80, .ns = 1}, 57 | {.bs = 25, .dw = 9, .ns = 4}, 58 | {.bs = 50, .dw = 24, .ns = 2} 59 | } 60 | }, 61 | { /* Version 5 */ 62 | .data_bytes = 134, 63 | .apat = {6, 30, 0}, 64 | .ecc = { 65 | {.bs = 67, .dw = 43, .ns = 2}, 66 | {.bs = 134, .dw = 108, .ns = 1}, 67 | {.bs = 33, .dw = 11, .ns = 2}, 68 | {.bs = 33, .dw = 15, .ns = 2} 69 | } 70 | }, 71 | { /* Version 6 */ 72 | .data_bytes = 172, 73 | .apat = {6, 34, 0}, 74 | .ecc = { 75 | {.bs = 43, .dw = 27, .ns = 4}, 76 | {.bs = 86, .dw = 68, .ns = 2}, 77 | {.bs = 43, .dw = 15, .ns = 4}, 78 | {.bs = 43, .dw = 19, .ns = 4} 79 | } 80 | }, 81 | { /* Version 7 */ 82 | .data_bytes = 196, 83 | .apat = {6, 22, 38, 0}, 84 | .ecc = { 85 | {.bs = 49, .dw = 31, .ns = 4}, 86 | {.bs = 98, .dw = 78, .ns = 2}, 87 | {.bs = 39, .dw = 13, .ns = 4}, 88 | {.bs = 32, .dw = 14, .ns = 2} 89 | } 90 | }, 91 | { /* Version 8 */ 92 | .data_bytes = 242, 93 | .apat = {6, 24, 42, 0}, 94 | .ecc = { 95 | {.bs = 60, .dw = 38, .ns = 2}, 96 | {.bs = 121, .dw = 97, .ns = 2}, 97 | {.bs = 40, .dw = 14, .ns = 4}, 98 | {.bs = 40, .dw = 18, .ns = 4} 99 | } 100 | }, 101 | { /* Version 9 */ 102 | .data_bytes = 292, 103 | .apat = {6, 26, 46, 0}, 104 | .ecc = { 105 | {.bs = 58, .dw = 36, .ns = 3}, 106 | {.bs = 146, .dw = 116, .ns = 2}, 107 | {.bs = 36, .dw = 12, .ns = 4}, 108 | {.bs = 36, .dw = 16, .ns = 4} 109 | } 110 | }, 111 | { /* Version 10 */ 112 | .data_bytes = 346, 113 | .apat = {6, 28, 50, 0}, 114 | .ecc = { 115 | {.bs = 69, .dw = 43, .ns = 4}, 116 | {.bs = 86, .dw = 68, .ns = 2}, 117 | {.bs = 43, .dw = 15, .ns = 6}, 118 | {.bs = 43, .dw = 19, .ns = 6} 119 | } 120 | }, 121 | { /* Version 11 */ 122 | .data_bytes = 404, 123 | .apat = {6, 30, 54, 0}, 124 | .ecc = { 125 | {.bs = 80, .dw = 50, .ns = 1}, 126 | {.bs = 101, .dw = 81, .ns = 4}, 127 | {.bs = 36, .dw = 12, .ns = 3}, 128 | {.bs = 50, .dw = 22, .ns = 4} 129 | } 130 | }, 131 | { /* Version 12 */ 132 | .data_bytes = 466, 133 | .apat = {6, 32, 58, 0}, 134 | .ecc = { 135 | {.bs = 58, .dw = 36, .ns = 6}, 136 | {.bs = 116, .dw = 92, .ns = 2}, 137 | {.bs = 42, .dw = 14, .ns = 7}, 138 | {.bs = 46, .dw = 20, .ns = 4} 139 | } 140 | }, 141 | { /* Version 13 */ 142 | .data_bytes = 532, 143 | .apat = {6, 34, 62, 0}, 144 | .ecc = { 145 | {.bs = 59, .dw = 37, .ns = 8}, 146 | {.bs = 133, .dw = 107, .ns = 4}, 147 | {.bs = 33, .dw = 11, .ns = 12}, 148 | {.bs = 44, .dw = 20, .ns = 8} 149 | } 150 | }, 151 | { /* Version 14 */ 152 | .data_bytes = 581, 153 | .apat = {6, 26, 46, 66, 0}, 154 | .ecc = { 155 | {.bs = 64, .dw = 40, .ns = 4}, 156 | {.bs = 145, .dw = 115, .ns = 3}, 157 | {.bs = 36, .dw = 12, .ns = 11}, 158 | {.bs = 36, .dw = 16, .ns = 11} 159 | } 160 | }, 161 | { /* Version 15 */ 162 | .data_bytes = 655, 163 | .apat = {6, 26, 48, 70, 0}, 164 | .ecc = { 165 | {.bs = 65, .dw = 41, .ns = 5}, 166 | {.bs = 109, .dw = 87, .ns = 5}, 167 | {.bs = 36, .dw = 12, .ns = 11}, 168 | {.bs = 54, .dw = 24, .ns = 5} 169 | } 170 | }, 171 | { /* Version 16 */ 172 | .data_bytes = 733, 173 | .apat = {6, 26, 50, 74, 0}, 174 | .ecc = { 175 | {.bs = 73, .dw = 45, .ns = 7}, 176 | {.bs = 122, .dw = 98, .ns = 5}, 177 | {.bs = 45, .dw = 15, .ns = 3}, 178 | {.bs = 43, .dw = 19, .ns = 15} 179 | } 180 | }, 181 | { /* Version 17 */ 182 | .data_bytes = 815, 183 | .apat = {6, 30, 54, 78, 0}, 184 | .ecc = { 185 | {.bs = 74, .dw = 46, .ns = 10}, 186 | {.bs = 135, .dw = 107, .ns = 1}, 187 | {.bs = 42, .dw = 14, .ns = 2}, 188 | {.bs = 50, .dw = 22, .ns = 1} 189 | } 190 | }, 191 | { /* Version 18 */ 192 | .data_bytes = 901, 193 | .apat = {6, 30, 56, 82, 0}, 194 | .ecc = { 195 | {.bs = 69, .dw = 43, .ns = 9}, 196 | {.bs = 150, .dw = 120, .ns = 5}, 197 | {.bs = 42, .dw = 14, .ns = 2}, 198 | {.bs = 50, .dw = 22, .ns = 17} 199 | } 200 | }, 201 | { /* Version 19 */ 202 | .data_bytes = 991, 203 | .apat = {6, 30, 58, 86, 0}, 204 | .ecc = { 205 | {.bs = 70, .dw = 44, .ns = 3}, 206 | {.bs = 141, .dw = 113, .ns = 3}, 207 | {.bs = 39, .dw = 13, .ns = 9}, 208 | {.bs = 47, .dw = 21, .ns = 17} 209 | } 210 | }, 211 | { /* Version 20 */ 212 | .data_bytes = 1085, 213 | .apat = {6, 34, 62, 90, 0}, 214 | .ecc = { 215 | {.bs = 67, .dw = 41, .ns = 3}, 216 | {.bs = 135, .dw = 107, .ns = 3}, 217 | {.bs = 43, .dw = 15, .ns = 15}, 218 | {.bs = 54, .dw = 24, .ns = 15} 219 | } 220 | }, 221 | { /* Version 21 */ 222 | .data_bytes = 1156, 223 | .apat = {6, 28, 50, 72, 92, 0}, 224 | .ecc = { 225 | {.bs = 68, .dw = 42, .ns = 17}, 226 | {.bs = 144, .dw = 116, .ns = 4}, 227 | {.bs = 46, .dw = 16, .ns = 19}, 228 | {.bs = 50, .dw = 22, .ns = 17} 229 | } 230 | }, 231 | { /* Version 22 */ 232 | .data_bytes = 1258, 233 | .apat = {6, 26, 50, 74, 98, 0}, 234 | .ecc = { 235 | {.bs = 74, .dw = 46, .ns = 17}, 236 | {.bs = 139, .dw = 111, .ns = 2}, 237 | {.bs = 37, .dw = 13, .ns = 34}, 238 | {.bs = 54, .dw = 24, .ns = 7} 239 | } 240 | }, 241 | { /* Version 23 */ 242 | .data_bytes = 1364, 243 | .apat = {6, 30, 54, 78, 102, 0}, 244 | .ecc = { 245 | {.bs = 75, .dw = 47, .ns = 4}, 246 | {.bs = 151, .dw = 121, .ns = 4}, 247 | {.bs = 45, .dw = 15, .ns = 16}, 248 | {.bs = 54, .dw = 24, .ns = 11} 249 | } 250 | }, 251 | { /* Version 24 */ 252 | .data_bytes = 1474, 253 | .apat = {6, 28, 54, 80, 106, 0}, 254 | .ecc = { 255 | {.bs = 73, .dw = 45, .ns = 6}, 256 | {.bs = 147, .dw = 117, .ns = 6}, 257 | {.bs = 46, .dw = 16, .ns = 30}, 258 | {.bs = 54, .dw = 24, .ns = 11} 259 | } 260 | }, 261 | { /* Version 25 */ 262 | .data_bytes = 1588, 263 | .apat = {6, 32, 58, 84, 110, 0}, 264 | .ecc = { 265 | {.bs = 75, .dw = 47, .ns = 8}, 266 | {.bs = 132, .dw = 106, .ns = 8}, 267 | {.bs = 45, .dw = 15, .ns = 22}, 268 | {.bs = 54, .dw = 24, .ns = 7} 269 | } 270 | }, 271 | { /* Version 26 */ 272 | .data_bytes = 1706, 273 | .apat = {6, 30, 58, 86, 114, 0}, 274 | .ecc = { 275 | {.bs = 74, .dw = 46, .ns = 19}, 276 | {.bs = 142, .dw = 114, .ns = 10}, 277 | {.bs = 46, .dw = 16, .ns = 33}, 278 | {.bs = 50, .dw = 22, .ns = 28} 279 | } 280 | }, 281 | { /* Version 27 */ 282 | .data_bytes = 1828, 283 | .apat = {6, 34, 62, 90, 118, 0}, 284 | .ecc = { 285 | {.bs = 73, .dw = 45, .ns = 22}, 286 | {.bs = 152, .dw = 122, .ns = 8}, 287 | {.bs = 45, .dw = 15, .ns = 12}, 288 | {.bs = 53, .dw = 23, .ns = 8} 289 | } 290 | }, 291 | { /* Version 28 */ 292 | .data_bytes = 1921, 293 | .apat = {6, 26, 50, 74, 98, 122, 0}, 294 | .ecc = { 295 | {.bs = 73, .dw = 45, .ns = 3}, 296 | {.bs = 147, .dw = 117, .ns = 3}, 297 | {.bs = 45, .dw = 15, .ns = 11}, 298 | {.bs = 54, .dw = 24, .ns = 4} 299 | } 300 | }, 301 | { /* Version 29 */ 302 | .data_bytes = 2051, 303 | .apat = {6, 30, 54, 78, 102, 126, 0}, 304 | .ecc = { 305 | {.bs = 73, .dw = 45, .ns = 21}, 306 | {.bs = 146, .dw = 116, .ns = 7}, 307 | {.bs = 45, .dw = 15, .ns = 19}, 308 | {.bs = 53, .dw = 23, .ns = 1} 309 | } 310 | }, 311 | { /* Version 30 */ 312 | .data_bytes = 2185, 313 | .apat = {6, 26, 52, 78, 104, 130, 0}, 314 | .ecc = { 315 | {.bs = 75, .dw = 47, .ns = 19}, 316 | {.bs = 145, .dw = 115, .ns = 5}, 317 | {.bs = 45, .dw = 15, .ns = 23}, 318 | {.bs = 54, .dw = 24, .ns = 15} 319 | } 320 | }, 321 | { /* Version 31 */ 322 | .data_bytes = 2323, 323 | .apat = {6, 30, 56, 82, 108, 134, 0}, 324 | .ecc = { 325 | {.bs = 74, .dw = 46, .ns = 2}, 326 | {.bs = 145, .dw = 115, .ns = 13}, 327 | {.bs = 45, .dw = 15, .ns = 23}, 328 | {.bs = 54, .dw = 24, .ns = 42} 329 | } 330 | }, 331 | { /* Version 32 */ 332 | .data_bytes = 2465, 333 | .apat = {6, 34, 60, 86, 112, 138, 0}, 334 | .ecc = { 335 | {.bs = 74, .dw = 46, .ns = 10}, 336 | {.bs = 145, .dw = 115, .ns = 17}, 337 | {.bs = 45, .dw = 15, .ns = 19}, 338 | {.bs = 54, .dw = 24, .ns = 10} 339 | } 340 | }, 341 | { /* Version 33 */ 342 | .data_bytes = 2611, 343 | .apat = {6, 30, 58, 86, 114, 142, 0}, 344 | .ecc = { 345 | {.bs = 74, .dw = 46, .ns = 14}, 346 | {.bs = 145, .dw = 115, .ns = 17}, 347 | {.bs = 45, .dw = 15, .ns = 11}, 348 | {.bs = 54, .dw = 24, .ns = 29} 349 | } 350 | }, 351 | { /* Version 34 */ 352 | .data_bytes = 2761, 353 | .apat = {6, 34, 62, 90, 118, 146, 0}, 354 | .ecc = { 355 | {.bs = 74, .dw = 46, .ns = 14}, 356 | {.bs = 145, .dw = 115, .ns = 13}, 357 | {.bs = 46, .dw = 16, .ns = 59}, 358 | {.bs = 54, .dw = 24, .ns = 44} 359 | } 360 | }, 361 | { /* Version 35 */ 362 | .data_bytes = 2876, 363 | .apat = {6, 30, 54, 78, 102, 126, 150}, 364 | .ecc = { 365 | {.bs = 75, .dw = 47, .ns = 12}, 366 | {.bs = 151, .dw = 121, .ns = 12}, 367 | {.bs = 45, .dw = 15, .ns = 22}, 368 | {.bs = 54, .dw = 24, .ns = 39} 369 | } 370 | }, 371 | { /* Version 36 */ 372 | .data_bytes = 3034, 373 | .apat = {6, 24, 50, 76, 102, 128, 154}, 374 | .ecc = { 375 | {.bs = 75, .dw = 47, .ns = 6}, 376 | {.bs = 151, .dw = 121, .ns = 6}, 377 | {.bs = 45, .dw = 15, .ns = 2}, 378 | {.bs = 54, .dw = 24, .ns = 46} 379 | } 380 | }, 381 | { /* Version 37 */ 382 | .data_bytes = 3196, 383 | .apat = {6, 28, 54, 80, 106, 132, 158}, 384 | .ecc = { 385 | {.bs = 74, .dw = 46, .ns = 29}, 386 | {.bs = 152, .dw = 122, .ns = 17}, 387 | {.bs = 45, .dw = 15, .ns = 24}, 388 | {.bs = 54, .dw = 24, .ns = 49} 389 | } 390 | }, 391 | { /* Version 38 */ 392 | .data_bytes = 3362, 393 | .apat = {6, 32, 58, 84, 110, 136, 162}, 394 | .ecc = { 395 | {.bs = 74, .dw = 46, .ns = 13}, 396 | {.bs = 152, .dw = 122, .ns = 4}, 397 | {.bs = 45, .dw = 15, .ns = 42}, 398 | {.bs = 54, .dw = 24, .ns = 48} 399 | } 400 | }, 401 | { /* Version 39 */ 402 | .data_bytes = 3532, 403 | .apat = {6, 26, 54, 82, 110, 138, 166}, 404 | .ecc = { 405 | {.bs = 75, .dw = 47, .ns = 40}, 406 | {.bs = 147, .dw = 117, .ns = 20}, 407 | {.bs = 45, .dw = 15, .ns = 10}, 408 | {.bs = 54, .dw = 24, .ns = 43} 409 | } 410 | }, 411 | { /* Version 40 */ 412 | .data_bytes = 3706, 413 | .apat = {6, 30, 58, 86, 114, 142, 170}, 414 | .ecc = { 415 | {.bs = 75, .dw = 47, .ns = 18}, 416 | {.bs = 148, .dw = 118, .ns = 19}, 417 | {.bs = 45, .dw = 15, .ns = 20}, 418 | {.bs = 54, .dw = 24, .ns = 34} 419 | } 420 | } 421 | }; 422 | -------------------------------------------------------------------------------- /source/xplorer.cpp: -------------------------------------------------------------------------------- 1 | #include "common.h" 2 | #include "dir.h" 3 | #include "draw.h" 4 | #include "buttons.h" 5 | #include "gfx.hpp" 6 | // contains current path 7 | char current_path[511]; 8 | char current_file[511]; 9 | // selected file 10 | int selected = 0; 11 | // A pointer to an array which is full of pointers which will point to strings (only way to create a dynamic/resizable array) 12 | char **file_arr; 13 | // Same as file_arr, will be used "alongside" file_arr to check whether something is a file or directory 14 | char *isfile_arr; 15 | // Size of file name array 16 | int size_of_file_array; 17 | // Used to offset what is printed from the file array, to allow "scrolling" 18 | int scroll = 0; 19 | // One PrintConsole for each screen 20 | PrintConsole topScreen, instructionscreen, debugscreen; 21 | // Max len of file/directory name 22 | int MAX_DIR_NAME_SIZE = 261; 23 | // What it says on the tin 24 | int MAX_FILES_ON_SCREEN = 24; 25 | // ^ 26 | int MAX_PATH_SIZE = 511; 27 | 28 | int xplorer(void) { 29 | 30 | // Initialize console for both screens using the two different PrintConsole 31 | consoleInit(GFX_BOTTOM, &debugscreen); 32 | consoleInit(GFX_BOTTOM, &instructionscreen); 33 | consoleInit(GFX_TOP, &topScreen); 34 | draw_border(GFX_TOP,0,255,255); 35 | draw_border(GFX_BOTTOM,255,255,0); 36 | consoleSetWindow(&topScreen, 1, 1, 48, 28); 37 | consoleSetWindow(&instructionscreen, 1, 1, 38, 26); 38 | consoleSetWindow(&debugscreen, 1, 9, 40, 21); 39 | consoleSelect(&instructionscreen); 40 | printf("A - Change Directory/Select file\nB - go up a directory\nDPAD/Circle pad - up and down\nSELECT - use the current location as download location."); 41 | printf("\n--------------------------------------"); 42 | 43 | consoleSelect(&debugscreen); 44 | printf("Started xplorer...\n"); 45 | 46 | // Initial allocation for these 2 arrays 47 | file_arr = (char**)malloc(1 * sizeof(char*)); 48 | printf("Ok\n"); 49 | isfile_arr = (char*) malloc(1 * sizeof(bool)); 50 | printf("OK 1\n"); 51 | // For when it is first realloc(ed) 52 | size_of_file_array = 1; 53 | 54 | strcpy(current_path, "sdmc:/"); 55 | get_all_in_dir(current_path); 56 | printf("Ok 3"); 57 | // Fill file name array with file names 58 | print_all_values_in_filear(); // Print all in / 59 | u64 min=0; 60 | // Main loop 61 | while (aptMainLoop()) { 62 | 63 | hidScanInput(); 64 | 65 | u32 kDown = hidKeysDown(); 66 | u32 kHeld = hidKeysHeld(); 67 | if(kDown) 68 | min=osGetTime(); 69 | if ((kDown & KEY_UP)|((kHeld & KEY_UP)&&(osGetTime() - min>500))) { 70 | up(); 71 | print_all_values_in_filear(); 72 | } 73 | 74 | else if ((kDown & KEY_DOWN)||((kHeld & KEY_DOWN)&&(osGetTime() - min>650))) { 75 | down(); 76 | print_all_values_in_filear(); 77 | } 78 | 79 | else if (kDown & KEY_LEFT) { 80 | left(); 81 | print_all_values_in_filear(); 82 | } 83 | 84 | else if (kDown & KEY_RIGHT) { 85 | right(); 86 | print_all_values_in_filear(); 87 | } 88 | 89 | else if (kDown & KEY_A) { 90 | a_pressed(); 91 | if(current_file[0]!=0) 92 | break; 93 | } 94 | 95 | else if (kDown & KEY_B) { 96 | b_pressed(); 97 | get_all_in_dir(current_path); 98 | print_all_values_in_filear(); 99 | } 100 | 101 | else if(kDown & KEY_START) 102 | break; 103 | else if(kDown & KEY_SELECT) 104 | break; 105 | gfxSwapBuffers(); 106 | } 107 | 108 | // Clear up allocated memory 109 | for (int i=0; i 3 | #include 4 | #include <3ds.h> 5 | #include "extract_zip.hpp" 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | static int copy_data(struct archive *ar, struct archive *aw) 15 | { 16 | int r; 17 | const void *buff; 18 | size_t size; 19 | int64_t offset; 20 | 21 | for (;;) { 22 | r = archive_read_data_block(ar, &buff, &size, &offset); 23 | if (r == ARCHIVE_EOF) 24 | return (ARCHIVE_OK); 25 | if (r < ARCHIVE_OK) 26 | return (r); 27 | r = archive_write_data_block(aw, buff, size, offset); 28 | if (r < ARCHIVE_OK) { 29 | printf("%s\n", archive_error_string(aw)); 30 | return (r); 31 | } 32 | } 33 | } 34 | void extract(std::string filename) 35 | { 36 | struct archive *a; 37 | struct archive *ext; 38 | struct archive_entry *entry; 39 | int flags; 40 | int r; 41 | 42 | /* Select which attributes we want to restore. */ 43 | flags = ARCHIVE_EXTRACT_PERM; 44 | flags |= ARCHIVE_EXTRACT_ACL; 45 | flags |= ARCHIVE_EXTRACT_FFLAGS; 46 | 47 | a = archive_read_new(); 48 | archive_read_support_format_all(a); 49 | ext = archive_write_disk_new(); 50 | archive_write_disk_set_options(ext, flags); 51 | //archive_write_disk_set_standard_lookup(ext); 52 | if ((r = archive_read_open_filename(a, filename.c_str(), 10240))) 53 | printf("Couldn't find file"); 54 | for (;;) { 55 | r = archive_read_next_header(a, &entry); 56 | if (r == ARCHIVE_EOF) 57 | break; 58 | if(r == ARCHIVE_OK) 59 | { 60 | printf("%s\n",archive_entry_pathname(entry)); 61 | } 62 | if (r < ARCHIVE_OK) 63 | printf( "%s\n", archive_error_string(a)); 64 | if (r < ARCHIVE_WARN) 65 | return; 66 | r = archive_write_header(ext, entry); 67 | if (r < ARCHIVE_OK) 68 | printf("%s\n", archive_error_string(ext)); 69 | else if (archive_entry_size(entry) > 0) { 70 | r = copy_data(a, ext); 71 | if (r < ARCHIVE_OK) 72 | printf("%s\n", archive_error_string(ext)); 73 | if (r < ARCHIVE_WARN) 74 | return; 75 | } 76 | r = archive_write_finish_entry(ext); 77 | if (r < ARCHIVE_OK) 78 | printf("%s\n", archive_error_string(ext)); 79 | if (r < ARCHIVE_WARN) 80 | return; 81 | } 82 | archive_read_close(a); 83 | archive_read_free(a); 84 | archive_write_close(ext); 85 | archive_write_free(ext); 86 | return; 87 | } --------------------------------------------------------------------------------