├── .DS_Store ├── .gitignore ├── .vscode └── settings.json ├── LICENSE ├── Makefile ├── README.md ├── sd_card └── config │ └── sys-ftpd │ ├── config.ini │ ├── connect.mp3 │ ├── disconnect.mp3 │ ├── pauseoff.mp3 │ └── pauseon.mp3 ├── source ├── console.c ├── console.h ├── ftp.c ├── ftp.h ├── led.c ├── led.h ├── main.c ├── minIni.c ├── minIni.h ├── minIni │ ├── minGlue-FatFs.h │ ├── minGlue-ccs.h │ ├── minGlue-efsl.h │ ├── minGlue-ffs.h │ ├── minGlue-mdd.h │ ├── minGlue-stdio.h │ ├── minGlue.h │ └── wxMinIni.h ├── mp3.c ├── mp3.h ├── util.c └── util.h └── sys-ftpd.json /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jakibaki/sys-ftpd/6e4993f1288ead176287103d6200646d727af810/.DS_Store -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | build 2 | *.elf 3 | *.npdm 4 | *.nso 5 | *.nsp 6 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "__locale": "c", 4 | "stdlib.h": "c", 5 | "inet.h": "c", 6 | "__bit_reference": "c", 7 | "bitset": "c", 8 | "chrono": "c", 9 | "dirent.h": "c", 10 | "stdio.h": "c", 11 | "string.h": "c", 12 | "mp3.h": "c", 13 | "__string": "c", 14 | "string": "c", 15 | "string_view": "c", 16 | "ios": "c", 17 | "ftp.h": "c", 18 | "util.h": "c" 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /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 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | #--------------------------------------------------------------------------------- 2 | .SUFFIXES: 3 | #--------------------------------------------------------------------------------- 4 | 5 | ifeq ($(strip $(DEVKITPRO)),) 6 | $(error "Please set DEVKITPRO in your environment. export DEVKITPRO=/devkitpro") 7 | endif 8 | 9 | TOPDIR ?= $(CURDIR) 10 | include $(DEVKITPRO)/libnx/switch_rules 11 | 12 | #--------------------------------------------------------------------------------- 13 | # TARGET is the name of the output 14 | # BUILD is the directory where object files & intermediate files will be placed 15 | # SOURCES is a list of directories containing source code 16 | # DATA is a list of directories containing data files 17 | # INCLUDES is a list of directories containing header files 18 | # EXEFS_SRC is the optional input directory containing data copied into exefs, if anything this normally should only contain "main.npdm". 19 | #--------------------------------------------------------------------------------- 20 | TARGET := sys-ftpd 21 | BUILD := build 22 | SOURCES := source 23 | DATA := data 24 | INCLUDES := include 25 | EXEFS_SRC := exefs_src 26 | ROMFS := romfs 27 | 28 | #--------------------------------------------------------------------------------- 29 | # options for code generation 30 | #--------------------------------------------------------------------------------- 31 | ARCH := -march=armv8-a+crc+crypto -mtune=cortex-a57 -mtp=soft -fPIE 32 | 33 | CFLAGS := -g -Wall -O2 -ffunction-sections \ 34 | $(ARCH) $(DEFINES) 35 | 36 | CFLAGS += $(INCLUDE) -D__SWITCH__ 37 | 38 | CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions -std=gnu++11 39 | 40 | ASFLAGS := -g $(ARCH) 41 | LDFLAGS = -specs=$(DEVKITPRO)/libnx/switch.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map) 42 | 43 | LIBS := -lnx -lmpg123 -lm 44 | 45 | #--------------------------------------------------------------------------------- 46 | # list of directories containing libraries, this must be the top level containing 47 | # include and lib 48 | #--------------------------------------------------------------------------------- 49 | LIBDIRS := $(PORTLIBS) $(LIBNX) 50 | 51 | 52 | #--------------------------------------------------------------------------------- 53 | # no real need to edit anything past this point unless you need to add additional 54 | # rules for different file extensions 55 | #--------------------------------------------------------------------------------- 56 | ifneq ($(BUILD),$(notdir $(CURDIR))) 57 | #--------------------------------------------------------------------------------- 58 | 59 | export OUTPUT := $(CURDIR)/$(TARGET) 60 | export TOPDIR := $(CURDIR) 61 | 62 | export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \ 63 | $(foreach dir,$(DATA),$(CURDIR)/$(dir)) 64 | 65 | export DEPSDIR := $(CURDIR)/$(BUILD) 66 | 67 | CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c))) 68 | CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp))) 69 | SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s))) 70 | BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*))) 71 | 72 | #--------------------------------------------------------------------------------- 73 | # use CXX for linking C++ projects, CC for standard C 74 | #--------------------------------------------------------------------------------- 75 | ifeq ($(strip $(CPPFILES)),) 76 | #--------------------------------------------------------------------------------- 77 | export LD := $(CC) 78 | #--------------------------------------------------------------------------------- 79 | else 80 | #--------------------------------------------------------------------------------- 81 | export LD := $(CXX) 82 | #--------------------------------------------------------------------------------- 83 | endif 84 | #--------------------------------------------------------------------------------- 85 | 86 | export OFILES_BIN := $(addsuffix .o,$(BINFILES)) 87 | export OFILES_SRC := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o) 88 | export OFILES := $(OFILES_BIN) $(OFILES_SRC) 89 | export HFILES_BIN := $(addsuffix .h,$(subst .,_,$(BINFILES))) 90 | 91 | export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \ 92 | $(foreach dir,$(LIBDIRS),-I$(dir)/include) \ 93 | -I$(CURDIR)/$(BUILD) 94 | 95 | export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib) 96 | 97 | export BUILD_EXEFS_SRC := $(TOPDIR)/$(EXEFS_SRC) 98 | 99 | ifeq ($(strip $(CONFIG_JSON)),) 100 | jsons := $(wildcard *.json) 101 | ifneq (,$(findstring $(TARGET).json,$(jsons))) 102 | export APP_JSON := $(TOPDIR)/$(TARGET).json 103 | else 104 | ifneq (,$(findstring config.json,$(jsons))) 105 | export APP_JSON := $(TOPDIR)/config.json 106 | endif 107 | endif 108 | else 109 | export APP_JSON := $(TOPDIR)/$(CONFIG_JSON) 110 | endif 111 | 112 | .PHONY: $(BUILD) clean all 113 | 114 | #--------------------------------------------------------------------------------- 115 | all: $(BUILD) 116 | 117 | $(BUILD): 118 | @[ -d $@ ] || mkdir -p $@ 119 | @$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile 120 | 121 | #--------------------------------------------------------------------------------- 122 | clean: 123 | @echo clean ... 124 | @rm -fr $(BUILD) $(TARGET).nsp $(TARGET).npdm $(TARGET).nso $(TARGET).elf 125 | 126 | #--------------------------------------------------------------------------------- 127 | else 128 | .PHONY: all 129 | 130 | DEPENDS := $(OFILES:.o=.d) 131 | 132 | #--------------------------------------------------------------------------------- 133 | # main targets 134 | #--------------------------------------------------------------------------------- 135 | all : $(OUTPUT).nsp 136 | 137 | ifeq ($(strip $(APP_JSON)),) 138 | $(OUTPUT).nsp : $(OUTPUT).nso 139 | else 140 | $(OUTPUT).nsp : $(OUTPUT).nso $(OUTPUT).npdm 141 | endif 142 | 143 | $(OUTPUT).nso : $(OUTPUT).elf 144 | 145 | $(OUTPUT).elf : $(OFILES) 146 | 147 | $(OFILES_SRC) : $(HFILES_BIN) 148 | 149 | #--------------------------------------------------------------------------------- 150 | # you need a rule like this for each extension you use as binary data 151 | #--------------------------------------------------------------------------------- 152 | %.bin.o : %.bin 153 | #--------------------------------------------------------------------------------- 154 | @echo $(notdir $<) 155 | @$(bin2o) 156 | 157 | -include $(DEPENDS) 158 | 159 | #--------------------------------------------------------------------------------------- 160 | endif 161 | #--------------------------------------------------------------------------------------- 162 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # sys-ftpd 2 | 3 | Put the contents of the sd_card folder in the root of your sd-card or else the Module won't work! 4 | 5 | This is a nintendo-switch sysmodule which runs an ftpd-server in the background (on port 5000 with no auth). 6 | 7 | The ftpd is basically copy paste from [this](https://github.com/DavidBuchanan314/ftpd) ftpd-fork. 8 | 9 | Put the contents of the sd_card folder in the root of your sd-card or else the sound effects won't work! 10 | 11 | Hotkeys: To help with security while there is are no login credentials, debugging, or otherwise, you can toggle the state of the server using the (+) + (-) + (X) button combination. 12 | 13 | Sound effects from: 14 | https://freesound.org/people/jens.enk/sounds/434610/ 15 | https://freesound.org/people/jens.enk/sounds/434611/ 16 | https://freesound.org/people/rhodesmas/sounds/322897/ 17 | https://freesound.org/people/rhodesmas/sounds/322895/ 18 | 19 | 20 | --- 21 | 22 | Config Example (`/config/sys-ftpd/config.ini`): 23 | 24 | ``` 25 | [User] 26 | user:=ftpd 27 | 28 | [Password] 29 | password:=12345 30 | 31 | [Port] 32 | port:=5000 33 | 34 | [Anonymous] 35 | anonymous:=1 36 | ;if anonymous:=1 no login and password are needed! 37 | ;if anonymous:=0 must set user:= and Password! 38 | ``` 39 | -------------------------------------------------------------------------------- /sd_card/config/sys-ftpd/config.ini: -------------------------------------------------------------------------------- 1 | [User] 2 | user:=ftpd 3 | 4 | [Password] 5 | password:=12345 6 | 7 | [Port] 8 | port:=5000 9 | 10 | [Anonymous] 11 | anonymous:=1 12 | #if anonymous:=1 no login and password needed ! 13 | #if anonymous:=0 must set user:= and Password:= ! 14 | 15 | -------------------------------------------------------------------------------- /sd_card/config/sys-ftpd/connect.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jakibaki/sys-ftpd/6e4993f1288ead176287103d6200646d727af810/sd_card/config/sys-ftpd/connect.mp3 -------------------------------------------------------------------------------- /sd_card/config/sys-ftpd/disconnect.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jakibaki/sys-ftpd/6e4993f1288ead176287103d6200646d727af810/sd_card/config/sys-ftpd/disconnect.mp3 -------------------------------------------------------------------------------- /sd_card/config/sys-ftpd/pauseoff.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jakibaki/sys-ftpd/6e4993f1288ead176287103d6200646d727af810/sd_card/config/sys-ftpd/pauseoff.mp3 -------------------------------------------------------------------------------- /sd_card/config/sys-ftpd/pauseon.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jakibaki/sys-ftpd/6e4993f1288ead176287103d6200646d727af810/sd_card/config/sys-ftpd/pauseon.mp3 -------------------------------------------------------------------------------- /source/console.c: -------------------------------------------------------------------------------- 1 | // This file is under the terms of the unlicense (https://github.com/DavidBuchanan314/ftpd/blob/master/LICENSE) 2 | 3 | #define ENABLE_LOGGING 1 4 | #include "console.h" 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | /* this is a lot easier when you have a real console */ 13 | 14 | int should_log = 0; 15 | 16 | void 17 | console_init(void) 18 | { 19 | } 20 | 21 | void 22 | console_set_status(const char *fmt, ...) 23 | { 24 | 25 | } 26 | 27 | void 28 | console_print(const char *fmt, ...) 29 | { 30 | if(should_log) { 31 | stdout = stderr = fopen("/config/sys-ftpd/logs/ftpd.log", "a"); 32 | va_list ap; 33 | va_start(ap, fmt); 34 | vprintf(fmt, ap); 35 | va_end(ap); 36 | fclose(stdout); 37 | } 38 | } 39 | 40 | void 41 | debug_print(const char *fmt, ...) 42 | { 43 | if(should_log) { 44 | stdout = stderr = fopen("/config/sys-ftpd/logs/ftpd.log", "a"); 45 | #ifdef ENABLE_LOGGING 46 | va_list ap; 47 | va_start(ap, fmt); 48 | vfprintf(stderr, fmt, ap); 49 | va_end(ap); 50 | #endif 51 | fclose(stdout); 52 | } 53 | } 54 | 55 | void console_render(void) 56 | { 57 | } 58 | -------------------------------------------------------------------------------- /source/console.h: -------------------------------------------------------------------------------- 1 | // This file is under the terms of the unlicense (https://github.com/DavidBuchanan314/ftpd/blob/master/LICENSE) 2 | 3 | #pragma once 4 | 5 | #ifdef _3DS 6 | #include <3ds.h> 7 | #define ESC(x) "\x1b[" #x 8 | #define RESET ESC(0m) 9 | #define BLACK ESC(30m) 10 | #define RED ESC(31;1m) 11 | #define GREEN ESC(32;1m) 12 | #define YELLOW ESC(33;1m) 13 | #define BLUE ESC(34;1m) 14 | #define MAGENTA ESC(35;1m) 15 | #define CYAN ESC(36;1m) 16 | #define WHITE ESC(37;1m) 17 | #else 18 | #define ESC(x) 19 | #define RESET 20 | #define BLACK 21 | #define RED 22 | #define GREEN 23 | #define YELLOW 24 | #define BLUE 25 | #define MAGENTA 26 | #define CYAN 27 | #define WHITE 28 | #endif 29 | 30 | extern int should_log; 31 | 32 | void console_init(void); 33 | 34 | __attribute__((format(printf,1,2))) 35 | void console_set_status(const char *fmt, ...); 36 | 37 | __attribute__((format(printf,1,2))) 38 | void console_print(const char *fmt, ...); 39 | 40 | __attribute__((format(printf,1,2))) 41 | void debug_print(const char *fmt, ...); 42 | 43 | void console_render(void); -------------------------------------------------------------------------------- /source/ftp.h: -------------------------------------------------------------------------------- 1 | // This file is under the terms of the unlicense (https://github.com/DavidBuchanan314/ftpd/blob/master/LICENSE) 2 | 3 | #pragma once 4 | 5 | /*! Loop status */ 6 | typedef enum 7 | { 8 | LOOP_CONTINUE, /*!< Continue looping */ 9 | LOOP_RESTART, /*!< Reinitialize */ 10 | LOOP_EXIT, /*!< Terminate looping */ 11 | } loop_status_t; 12 | 13 | void ftp_pre_init(void); 14 | int ftp_init(void); 15 | loop_status_t ftp_loop(void); 16 | void ftp_exit(void); 17 | void ftp_post_exit(void); 18 | -------------------------------------------------------------------------------- /source/led.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "util.h" 5 | 6 | void flash_led_connect() 7 | { 8 | HidsysNotificationLedPattern pattern; 9 | memset(&pattern, 0, sizeof(pattern)); 10 | 11 | // Setup Breathing effect pattern data. 12 | pattern.baseMiniCycleDuration = 0x8; // 100ms. 13 | pattern.totalMiniCycles = 0x2; // 3 mini cycles. Last one 12.5ms. 14 | pattern.totalFullCycles = 0x0; // Repeat forever. 15 | pattern.startIntensity = 0x2; // 13%. 16 | 17 | pattern.miniCycles[0].ledIntensity = 0xF; // 100%. 18 | pattern.miniCycles[0].transitionSteps = 0xF; // 15 steps. Transition time 1.5s. 19 | pattern.miniCycles[0].finalStepDuration = 0x0; // Forced 12.5ms. 20 | pattern.miniCycles[1].ledIntensity = 0x2; // 13%. 21 | pattern.miniCycles[1].transitionSteps = 0xF; // 15 steps. Transition time 1.5s. 22 | pattern.miniCycles[1].finalStepDuration = 0x0; // Forced 12.5ms. 23 | 24 | u64 uniquePadIds[5] = {0}; 25 | 26 | size_t total_entries = 0; 27 | 28 | Result rc = hidsysGetUniquePadIds(uniquePadIds, 5, &total_entries); 29 | if (R_FAILED(rc) && rc != MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer)) 30 | fatalLater(rc); 31 | 32 | for (int i = 0; i < total_entries; i++) 33 | hidsysSetNotificationLedPattern(&pattern, uniquePadIds[i]); 34 | } 35 | 36 | void flash_led_disconnect() 37 | { 38 | HidsysNotificationLedPattern pattern; 39 | memset(&pattern, 0, sizeof(pattern)); 40 | 41 | u64 uniquePadIds[2]; 42 | memset(uniquePadIds, 0, sizeof(uniquePadIds)); 43 | 44 | size_t total_entries = 0; 45 | 46 | Result rc = hidsysGetUniquePadsFromNpad(hidGetHandheldMode() ? CONTROLLER_HANDHELD : CONTROLLER_PLAYER_1, uniquePadIds, 2, &total_entries); 47 | if (R_FAILED(rc) && rc != MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer)) 48 | fatalLater(rc); 49 | 50 | for (int i = 0; i < total_entries; i++) 51 | hidsysSetNotificationLedPattern(&pattern, uniquePadIds[i]); 52 | } 53 | -------------------------------------------------------------------------------- /source/led.h: -------------------------------------------------------------------------------- 1 | void flash_led_connect(); 2 | void flash_led_disconnect(); -------------------------------------------------------------------------------- /source/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include "console.h" 10 | #include "ftp.h" 11 | #include 12 | 13 | // only for mkdir, used when creating the "logs" directory 14 | #include 15 | 16 | #include 17 | 18 | #include "util.h" 19 | 20 | #include "mp3.h" 21 | 22 | #define TITLE_ID 0x420000000000000E 23 | #define HEAP_SIZE 0x000540000 24 | 25 | // we aren't an applet 26 | u32 __nx_applet_type = AppletType_None; 27 | 28 | // setup a fake heap 29 | char fake_heap[HEAP_SIZE]; 30 | 31 | // we override libnx internals to do a minimal init 32 | void __libnx_initheap(void) 33 | { 34 | extern char *fake_heap_start; 35 | extern char *fake_heap_end; 36 | 37 | // setup newlib fake heap 38 | fake_heap_start = fake_heap; 39 | fake_heap_end = fake_heap + HEAP_SIZE; 40 | } 41 | 42 | void __appInit(void) 43 | { 44 | Result rc; 45 | svcSleepThread(10000000000L); 46 | rc = smInitialize(); 47 | if (R_FAILED(rc)) 48 | fatalLater(rc); 49 | rc = fsInitialize(); 50 | if (R_FAILED(rc)) 51 | fatalLater(rc); 52 | rc = fsdevMountSdmc(); 53 | if (R_FAILED(rc)) 54 | fatalLater(rc); 55 | rc = timeInitialize(); 56 | if (R_FAILED(rc)) 57 | fatalLater(rc); 58 | rc = hidInitialize(); 59 | if (R_FAILED(rc)) 60 | fatalLater(rc); 61 | rc = hidsysInitialize(); 62 | if (R_FAILED(rc)) 63 | fatalLater(rc); 64 | rc = setsysInitialize(); 65 | if (R_SUCCEEDED(rc)) 66 | { 67 | SetSysFirmwareVersion fw; 68 | rc = setsysGetFirmwareVersion(&fw); 69 | if (R_SUCCEEDED(rc)) 70 | hosversionSet(MAKEHOSVERSION(fw.major, fw.minor, fw.micro)); 71 | setsysExit(); 72 | } 73 | } 74 | 75 | void __appExit(void) 76 | { 77 | fsdevUnmountAll(); 78 | fsExit(); 79 | smExit(); 80 | audoutExit(); 81 | timeExit(); 82 | hidExit(); 83 | hidsysExit(); 84 | } 85 | 86 | static loop_status_t loop(loop_status_t (*callback)(void)) 87 | { 88 | loop_status_t status = LOOP_CONTINUE; 89 | 90 | while (appletMainLoop()) 91 | { 92 | svcSleepThread(10000000L); 93 | status = callback(); 94 | console_render(); 95 | if (status != LOOP_CONTINUE) 96 | return status; 97 | if (isPaused()) 98 | return LOOP_RESTART; 99 | } 100 | return LOOP_EXIT; 101 | } 102 | 103 | void inputPoller() 104 | { 105 | while (appletMainLoop()) 106 | { 107 | svcSleepThread(1e+8L); 108 | hidScanInput(); 109 | u64 kDown = hidKeysDown(CONTROLLER_P1_AUTO); 110 | u64 kHeld = hidKeysHeld(CONTROLLER_P1_AUTO); 111 | 112 | if ((kDown & KEY_PLUS || kDown & KEY_MINUS || kDown & KEY_X) && (kHeld & KEY_PLUS && kHeld & KEY_MINUS && kHeld & KEY_X)) 113 | setPaused(!isPaused()); 114 | } 115 | } 116 | 117 | int main(int argc, char **argv) 118 | { 119 | (void)argc; 120 | (void)argv; 121 | 122 | FILE *should_log_file = fopen("/config/sys-ftpd/logs/ftpd_log_enabled", "r"); 123 | if (should_log_file != NULL) 124 | { 125 | should_log = true; 126 | fclose(should_log_file); 127 | 128 | mkdir("/config/sys-ftpd/logs", 0700); 129 | unlink("/config/sys-ftpd/logs/ftpd.log"); 130 | } 131 | 132 | mp3MutInit(); 133 | pauseInit(); 134 | Thread pauseThread; 135 | Result rc = threadCreate(&pauseThread, inputPoller, NULL, 0x4000, 49, 3); 136 | if (R_FAILED(rc)) 137 | fatalLater(rc); 138 | rc = threadStart(&pauseThread); 139 | if (R_FAILED(rc)) 140 | fatalLater(rc); 141 | 142 | loop_status_t status = LOOP_RESTART; 143 | 144 | ftp_pre_init(); 145 | while (status == LOOP_RESTART) 146 | { 147 | while (isPaused()) 148 | { 149 | svcSleepThread(1000000000L); 150 | } 151 | 152 | /* initialize ftp subsystem */ 153 | if (ftp_init() == 0) 154 | { 155 | /* ftp loop */ 156 | status = loop(ftp_loop); 157 | 158 | /* done with ftp */ 159 | ftp_exit(); 160 | } 161 | else 162 | status = LOOP_EXIT; 163 | } 164 | ftp_post_exit(); 165 | 166 | return 0; 167 | } 168 | -------------------------------------------------------------------------------- /source/minIni.c: -------------------------------------------------------------------------------- 1 | /* minIni - Multi-Platform INI file parser, suitable for embedded systems 2 | * 3 | * These routines are in part based on the article "Multiplatform .INI Files" 4 | * by Joseph J. Graf in the March 1994 issue of Dr. Dobb's Journal. 5 | * 6 | * Copyright (c) CompuPhase, 2008-2017 7 | * 8 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 9 | * use this file except in compliance with the License. You may obtain a copy 10 | * of the License at 11 | * 12 | * http://www.apache.org/licenses/LICENSE-2.0 13 | * 14 | * Unless required by applicable law or agreed to in writing, software 15 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 16 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 17 | * License for the specific language governing permissions and limitations 18 | * under the License. 19 | * 20 | * Version: $Id: minIni.c 53 2015-01-18 13:35:11Z thiadmer.riemersma@gmail.com $ 21 | */ 22 | 23 | #if (defined _UNICODE || defined __UNICODE__ || defined UNICODE) && !defined INI_ANSIONLY 24 | # if !defined UNICODE /* for Windows */ 25 | # define UNICODE 26 | # endif 27 | # if !defined _UNICODE /* for C library */ 28 | # define _UNICODE 29 | # endif 30 | #endif 31 | 32 | #define MININI_IMPLEMENTATION 33 | #include "minIni.h" 34 | #if defined NDEBUG 35 | #define assert(e) 36 | #else 37 | #include 38 | #endif 39 | 40 | #if !defined __T || defined INI_ANSIONLY 41 | #include 42 | #include 43 | #include 44 | #define TCHAR char 45 | #define __T(s) s 46 | #define _tcscat strcat 47 | #define _tcschr strchr 48 | #define _tcscmp strcmp 49 | #define _tcscpy strcpy 50 | #define _tcsicmp stricmp 51 | #define _tcslen strlen 52 | #define _tcsncmp strncmp 53 | #define _tcsncmp strncmp 54 | #define _tcsrchr strrchr 55 | #define _tcstol strtol 56 | #define _tcstod strtod 57 | #define _totupper toupper 58 | #define _stprintf sprintf 59 | #define _tfgets fgets 60 | #define _tfputs fputs 61 | #define _tfopen fopen 62 | #define _tremove remove 63 | #define _trename rename 64 | #endif 65 | 66 | #if defined __linux || defined __linux__ 67 | #define __LINUX__ 68 | #elif defined FREEBSD && !defined __FreeBSD__ 69 | #define __FreeBSD__ 70 | #elif defined(_MSC_VER) 71 | #pragma warning(disable: 4996) /* for Microsoft Visual C/C++ */ 72 | #endif 73 | #if !defined strncmp && !defined PORTABLE_strncmp 74 | #if defined __LINUX__ || defined __FreeBSD__ || defined __OpenBSD__ || defined __APPLE__ 75 | #define strncmp strncasecmp 76 | #endif 77 | #endif 78 | #if !defined _totupper 79 | #define _totupper toupper 80 | #endif 81 | 82 | #if !defined INI_LINETERM 83 | #if defined __LINUX__ || defined __FreeBSD__ || defined __OpenBSD__ || defined __APPLE__ 84 | #define INI_LINETERM __T("\n") 85 | #else 86 | #define INI_LINETERM __T("\r\n") 87 | #endif 88 | #endif 89 | #if !defined INI_FILETYPE 90 | #error Missing definition for INI_FILETYPE. 91 | #endif 92 | 93 | #if !defined sizearray 94 | #define sizearray(a) (sizeof(a) / sizeof((a)[0])) 95 | #endif 96 | 97 | enum quote_option { 98 | QUOTE_NONE, 99 | QUOTE_ENQUOTE, 100 | QUOTE_DEQUOTE, 101 | }; 102 | 103 | #if defined PORTABLE_strncmp 104 | int strncmp(const TCHAR *s1, const TCHAR *s2, size_t n) 105 | { 106 | while (n-- != 0 && (*s1 || *s2)) { 107 | register int c1, c2; 108 | c1 = *s1++; 109 | if ('a' <= c1 && c1 <= 'z') 110 | c1 += ('A' - 'a'); 111 | c2 = *s2++; 112 | if ('a' <= c2 && c2 <= 'z') 113 | c2 += ('A' - 'a'); 114 | if (c1 != c2) 115 | return c1 - c2; 116 | } /* while */ 117 | return 0; 118 | } 119 | #endif /* PORTABLE_strncmp */ 120 | 121 | static TCHAR *skipleading(const TCHAR *str) 122 | { 123 | assert(str != NULL); 124 | while ('\0' < *str && *str <= ' ') 125 | str++; 126 | return (TCHAR *)str; 127 | } 128 | 129 | static TCHAR *skiptrailing(const TCHAR *str, const TCHAR *base) 130 | { 131 | assert(str != NULL); 132 | assert(base != NULL); 133 | while (str > base && '\0' < *(str-1) && *(str-1) <= ' ') 134 | str--; 135 | return (TCHAR *)str; 136 | } 137 | 138 | static TCHAR *striptrailing(TCHAR *str) 139 | { 140 | TCHAR *ptr = skiptrailing(_tcschr(str, '\0'), str); 141 | assert(ptr != NULL); 142 | *ptr = '\0'; 143 | return str; 144 | } 145 | 146 | static TCHAR *ini_strncpy(TCHAR *dest, const TCHAR *source, size_t maxlen, enum quote_option option) 147 | { 148 | size_t d, s; 149 | 150 | assert(maxlen>0); 151 | assert(source != NULL && dest != NULL); 152 | assert((dest < source || (dest == source && option != QUOTE_ENQUOTE)) || dest > source + strlen(source)); 153 | if (option == QUOTE_ENQUOTE && maxlen < 3) 154 | option = QUOTE_NONE; /* cannot store two quotes and a terminating zero in less than 3 characters */ 155 | 156 | switch (option) { 157 | case QUOTE_NONE: 158 | for (d = 0; d < maxlen - 1 && source[d] != '\0'; d++) 159 | dest[d] = source[d]; 160 | assert(d < maxlen); 161 | dest[d] = '\0'; 162 | break; 163 | case QUOTE_ENQUOTE: 164 | d = 0; 165 | dest[d++] = '"'; 166 | for (s = 0; source[s] != '\0' && d < maxlen - 2; s++, d++) { 167 | if (source[s] == '"') { 168 | if (d >= maxlen - 3) 169 | break; /* no space to store the escape character plus the one that follows it */ 170 | dest[d++] = '\\'; 171 | } /* if */ 172 | dest[d] = source[s]; 173 | } /* for */ 174 | dest[d++] = '"'; 175 | dest[d] = '\0'; 176 | break; 177 | case QUOTE_DEQUOTE: 178 | for (d = s = 0; source[s] != '\0' && d < maxlen - 1; s++, d++) { 179 | if ((source[s] == '"' || source[s] == '\\') && source[s + 1] == '"') 180 | s++; 181 | dest[d] = source[s]; 182 | } /* for */ 183 | dest[d] = '\0'; 184 | break; 185 | default: 186 | assert(0); 187 | } /* switch */ 188 | 189 | return dest; 190 | } 191 | 192 | static TCHAR *cleanstring(TCHAR *string, enum quote_option *quotes) 193 | { 194 | int isstring; 195 | TCHAR *ep; 196 | 197 | assert(string != NULL); 198 | assert(quotes != NULL); 199 | 200 | /* Remove a trailing comment */ 201 | isstring = 0; 202 | for (ep = string; *ep != '\0' && ((*ep != ';' && *ep != '#') || isstring); ep++) { 203 | if (*ep == '"') { 204 | if (*(ep + 1) == '"') 205 | ep++; /* skip "" (both quotes) */ 206 | else 207 | isstring = !isstring; /* single quote, toggle isstring */ 208 | } else if (*ep == '\\' && *(ep + 1) == '"') { 209 | ep++; /* skip \" (both quotes */ 210 | } /* if */ 211 | } /* for */ 212 | assert(ep != NULL && (*ep == '\0' || *ep == ';' || *ep == '#')); 213 | *ep = '\0'; /* terminate at a comment */ 214 | striptrailing(string); 215 | /* Remove double quotes surrounding a value */ 216 | *quotes = QUOTE_NONE; 217 | if (*string == '"' && (ep = _tcschr(string, '\0')) != NULL && *(ep - 1) == '"') { 218 | string++; 219 | *--ep = '\0'; 220 | *quotes = QUOTE_DEQUOTE; /* this is a string, so remove escaped characters */ 221 | } /* if */ 222 | return string; 223 | } 224 | 225 | static int getkeystring(INI_FILETYPE *fp, const TCHAR *Section, const TCHAR *Key, 226 | int idxSection, int idxKey, TCHAR *Buffer, int BufferSize, 227 | INI_FILEPOS *mark) 228 | { 229 | TCHAR *sp, *ep; 230 | int len, idx; 231 | enum quote_option quotes; 232 | TCHAR LocalBuffer[INI_BUFFERSIZE]; 233 | 234 | assert(fp != NULL); 235 | /* Move through file 1 line at a time until a section is matched or EOF. If 236 | * parameter Section is NULL, only look at keys above the first section. If 237 | * idxSection is positive, copy the relevant section name. 238 | */ 239 | len = (Section != NULL) ? (int)_tcslen(Section) : 0; 240 | if (len > 0 || idxSection >= 0) { 241 | assert(idxSection >= 0 || Section != NULL); 242 | idx = -1; 243 | do { 244 | if (!ini_read(LocalBuffer, INI_BUFFERSIZE, fp)) 245 | return 0; 246 | sp = skipleading(LocalBuffer); 247 | ep = _tcsrchr(sp, ']'); 248 | } while (*sp != '[' || ep == NULL || 249 | (((int)(ep-sp-1) != len || Section == NULL || _tcsncmp(sp+1,Section,len) != 0) && ++idx != idxSection)); 250 | if (idxSection >= 0) { 251 | if (idx == idxSection) { 252 | assert(ep != NULL); 253 | assert(*ep == ']'); 254 | *ep = '\0'; 255 | ini_strncpy(Buffer, sp + 1, BufferSize, QUOTE_NONE); 256 | return 1; 257 | } /* if */ 258 | return 0; /* no more section found */ 259 | } /* if */ 260 | } /* if */ 261 | 262 | /* Now that the section has been found, find the entry. 263 | * Stop searching upon leaving the section's area. 264 | */ 265 | assert(Key != NULL || idxKey >= 0); 266 | len = (Key != NULL) ? (int)_tcslen(Key) : 0; 267 | idx = -1; 268 | do { 269 | if (mark != NULL) 270 | ini_tell(fp, mark); /* optionally keep the mark to the start of the line */ 271 | if (!ini_read(LocalBuffer,INI_BUFFERSIZE,fp) || *(sp = skipleading(LocalBuffer)) == '[') 272 | return 0; 273 | sp = skipleading(LocalBuffer); 274 | ep = _tcschr(sp, '='); /* Parse out the equal sign */ 275 | if (ep == NULL) 276 | ep = _tcschr(sp, ':'); 277 | } while (*sp == ';' || *sp == '#' || ep == NULL 278 | || ((len == 0 || (int)(skiptrailing(ep,sp)-sp) != len || _tcsncmp(sp,Key,len) != 0) && ++idx != idxKey)); 279 | if (idxKey >= 0) { 280 | if (idx == idxKey) { 281 | assert(ep != NULL); 282 | assert(*ep == '=' || *ep == ':'); 283 | *ep = '\0'; 284 | striptrailing(sp); 285 | ini_strncpy(Buffer, sp, BufferSize, QUOTE_NONE); 286 | return 1; 287 | } /* if */ 288 | return 0; /* no more key found (in this section) */ 289 | } /* if */ 290 | 291 | /* Copy up to BufferSize chars to buffer */ 292 | assert(ep != NULL); 293 | assert(*ep == '=' || *ep == ':'); 294 | sp = skipleading(ep + 1); 295 | sp = cleanstring(sp, "es); /* Remove a trailing comment */ 296 | ini_strncpy(Buffer, sp, BufferSize, quotes); 297 | return 1; 298 | } 299 | 300 | /** ini_gets() 301 | * \param Section the name of the section to search for 302 | * \param Key the name of the entry to find the value of 303 | * \param DefValue default string in the event of a failed read 304 | * \param Buffer a pointer to the buffer to copy into 305 | * \param BufferSize the maximum number of characters to copy 306 | * \param Filename the name and full path of the .ini file to read from 307 | * 308 | * \return the number of characters copied into the supplied buffer 309 | */ 310 | int ini_gets(const TCHAR *Section, const TCHAR *Key, const TCHAR *DefValue, 311 | TCHAR *Buffer, int BufferSize, const TCHAR *Filename) 312 | { 313 | INI_FILETYPE fp; 314 | int ok = 0; 315 | 316 | if (Buffer == NULL || BufferSize <= 0 || Key == NULL) 317 | return 0; 318 | if (ini_openread(Filename, &fp)) { 319 | ok = getkeystring(&fp, Section, Key, -1, -1, Buffer, BufferSize, NULL); 320 | (void)ini_close(&fp); 321 | } /* if */ 322 | if (!ok) 323 | ini_strncpy(Buffer, (DefValue != NULL) ? DefValue : __T(""), BufferSize, QUOTE_NONE); 324 | return (int)_tcslen(Buffer); 325 | } 326 | 327 | /** ini_getl() 328 | * \param Section the name of the section to search for 329 | * \param Key the name of the entry to find the value of 330 | * \param DefValue the default value in the event of a failed read 331 | * \param Filename the name of the .ini file to read from 332 | * 333 | * \return the value located at Key 334 | */ 335 | long ini_getl(const TCHAR *Section, const TCHAR *Key, long DefValue, const TCHAR *Filename) 336 | { 337 | TCHAR LocalBuffer[64]; 338 | int len = ini_gets(Section, Key, __T(""), LocalBuffer, sizearray(LocalBuffer), Filename); 339 | return (len == 0) ? DefValue 340 | : ((len >= 2 && _totupper((int)LocalBuffer[1]) == 'X') ? _tcstol(LocalBuffer, NULL, 16) 341 | : _tcstol(LocalBuffer, NULL, 10)); 342 | } 343 | 344 | #if defined INI_REAL 345 | /** ini_getf() 346 | * \param Section the name of the section to search for 347 | * \param Key the name of the entry to find the value of 348 | * \param DefValue the default value in the event of a failed read 349 | * \param Filename the name of the .ini file to read from 350 | * 351 | * \return the value located at Key 352 | */ 353 | INI_REAL ini_getf(const TCHAR *Section, const TCHAR *Key, INI_REAL DefValue, const TCHAR *Filename) 354 | { 355 | TCHAR LocalBuffer[64]; 356 | int len = ini_gets(Section, Key, __T(""), LocalBuffer, sizearray(LocalBuffer), Filename); 357 | return (len == 0) ? DefValue : ini_atof(LocalBuffer); 358 | } 359 | #endif 360 | 361 | /** ini_getbool() 362 | * \param Section the name of the section to search for 363 | * \param Key the name of the entry to find the value of 364 | * \param DefValue default value in the event of a failed read; it should 365 | * zero (0) or one (1). 366 | * \param Filename the name and full path of the .ini file to read from 367 | * 368 | * A true boolean is found if one of the following is matched: 369 | * - A string starting with 'y' or 'Y' 370 | * - A string starting with 't' or 'T' 371 | * - A string starting with '1' 372 | * 373 | * A false boolean is found if one of the following is matched: 374 | * - A string starting with 'n' or 'N' 375 | * - A string starting with 'f' or 'F' 376 | * - A string starting with '0' 377 | * 378 | * \return the true/false flag as interpreted at Key 379 | */ 380 | int ini_getbool(const TCHAR *Section, const TCHAR *Key, int DefValue, const TCHAR *Filename) 381 | { 382 | TCHAR LocalBuffer[2] = __T(""); 383 | int ret; 384 | 385 | ini_gets(Section, Key, __T(""), LocalBuffer, sizearray(LocalBuffer), Filename); 386 | LocalBuffer[0] = (TCHAR)_totupper((int)LocalBuffer[0]); 387 | if (LocalBuffer[0] == 'Y' || LocalBuffer[0] == '1' || LocalBuffer[0] == 'T') 388 | ret = 1; 389 | else if (LocalBuffer[0] == 'N' || LocalBuffer[0] == '0' || LocalBuffer[0] == 'F') 390 | ret = 0; 391 | else 392 | ret = DefValue; 393 | 394 | return(ret); 395 | } 396 | 397 | /** ini_getsection() 398 | * \param idx the zero-based sequence number of the section to return 399 | * \param Buffer a pointer to the buffer to copy into 400 | * \param BufferSize the maximum number of characters to copy 401 | * \param Filename the name and full path of the .ini file to read from 402 | * 403 | * \return the number of characters copied into the supplied buffer 404 | */ 405 | int ini_getsection(int idx, TCHAR *Buffer, int BufferSize, const TCHAR *Filename) 406 | { 407 | INI_FILETYPE fp; 408 | int ok = 0; 409 | 410 | if (Buffer == NULL || BufferSize <= 0 || idx < 0) 411 | return 0; 412 | if (ini_openread(Filename, &fp)) { 413 | ok = getkeystring(&fp, NULL, NULL, idx, -1, Buffer, BufferSize, NULL); 414 | (void)ini_close(&fp); 415 | } /* if */ 416 | if (!ok) 417 | *Buffer = '\0'; 418 | return (int)_tcslen(Buffer); 419 | } 420 | 421 | /** ini_getkey() 422 | * \param Section the name of the section to browse through, or NULL to 423 | * browse through the keys outside any section 424 | * \param idx the zero-based sequence number of the key to return 425 | * \param Buffer a pointer to the buffer to copy into 426 | * \param BufferSize the maximum number of characters to copy 427 | * \param Filename the name and full path of the .ini file to read from 428 | * 429 | * \return the number of characters copied into the supplied buffer 430 | */ 431 | int ini_getkey(const TCHAR *Section, int idx, TCHAR *Buffer, int BufferSize, const TCHAR *Filename) 432 | { 433 | INI_FILETYPE fp; 434 | int ok = 0; 435 | 436 | if (Buffer == NULL || BufferSize <= 0 || idx < 0) 437 | return 0; 438 | if (ini_openread(Filename, &fp)) { 439 | ok = getkeystring(&fp, Section, NULL, -1, idx, Buffer, BufferSize, NULL); 440 | (void)ini_close(&fp); 441 | } /* if */ 442 | if (!ok) 443 | *Buffer = '\0'; 444 | return (int)_tcslen(Buffer); 445 | } 446 | 447 | 448 | #if !defined INI_NOBROWSE 449 | /** ini_browse() 450 | * \param Callback a pointer to a function that will be called for every 451 | * setting in the INI file. 452 | * \param UserData arbitrary data, which the function passes on the 453 | * \c Callback function 454 | * \param Filename the name and full path of the .ini file to read from 455 | * 456 | * \return 1 on success, 0 on failure (INI file not found) 457 | * 458 | * \note The \c Callback function must return 1 to continue 459 | * browsing through the INI file, or 0 to stop. Even when the 460 | * callback stops the browsing, this function will return 1 461 | * (for success). 462 | */ 463 | int ini_browse(INI_CALLBACK Callback, void *UserData, const TCHAR *Filename) 464 | { 465 | TCHAR LocalBuffer[INI_BUFFERSIZE]; 466 | int lenSec, lenKey; 467 | enum quote_option quotes; 468 | INI_FILETYPE fp; 469 | 470 | if (Callback == NULL) 471 | return 0; 472 | if (!ini_openread(Filename, &fp)) 473 | return 0; 474 | 475 | LocalBuffer[0] = '\0'; /* copy an empty section in the buffer */ 476 | lenSec = (int)_tcslen(LocalBuffer) + 1; 477 | for ( ;; ) { 478 | TCHAR *sp, *ep; 479 | if (!ini_read(LocalBuffer + lenSec, INI_BUFFERSIZE - lenSec, &fp)) 480 | break; 481 | sp = skipleading(LocalBuffer + lenSec); 482 | /* ignore empty strings and comments */ 483 | if (*sp == '\0' || *sp == ';' || *sp == '#') 484 | continue; 485 | /* see whether we reached a new section */ 486 | ep = _tcsrchr(sp, ']'); 487 | if (*sp == '[' && ep != NULL) { 488 | *ep = '\0'; 489 | ini_strncpy(LocalBuffer, sp + 1, INI_BUFFERSIZE, QUOTE_NONE); 490 | lenSec = (int)_tcslen(LocalBuffer) + 1; 491 | continue; 492 | } /* if */ 493 | /* not a new section, test for a key/value pair */ 494 | ep = _tcschr(sp, '='); /* test for the equal sign or colon */ 495 | if (ep == NULL) 496 | ep = _tcschr(sp, ':'); 497 | if (ep == NULL) 498 | continue; /* invalid line, ignore */ 499 | *ep++ = '\0'; /* split the key from the value */ 500 | striptrailing(sp); 501 | ini_strncpy(LocalBuffer + lenSec, sp, INI_BUFFERSIZE - lenSec, QUOTE_NONE); 502 | lenKey = (int)_tcslen(LocalBuffer + lenSec) + 1; 503 | /* clean up the value */ 504 | sp = skipleading(ep); 505 | sp = cleanstring(sp, "es); /* Remove a trailing comment */ 506 | ini_strncpy(LocalBuffer + lenSec + lenKey, sp, INI_BUFFERSIZE - lenSec - lenKey, quotes); 507 | /* call the callback */ 508 | if (!Callback(LocalBuffer, LocalBuffer + lenSec, LocalBuffer + lenSec + lenKey, UserData)) 509 | break; 510 | } /* for */ 511 | 512 | (void)ini_close(&fp); 513 | return 1; 514 | } 515 | #endif /* INI_NOBROWSE */ 516 | 517 | #if ! defined INI_READONLY 518 | static void ini_tempname(TCHAR *dest, const TCHAR *source, int maxlength) 519 | { 520 | TCHAR *p; 521 | 522 | ini_strncpy(dest, source, maxlength, QUOTE_NONE); 523 | p = _tcsrchr(dest, '\0'); 524 | assert(p != NULL); 525 | *(p - 1) = '~'; 526 | } 527 | 528 | static enum quote_option check_enquote(const TCHAR *Value) 529 | { 530 | const TCHAR *p; 531 | 532 | /* run through the value, if it has trailing spaces, or '"', ';' or '#' 533 | * characters, enquote it 534 | */ 535 | assert(Value != NULL); 536 | for (p = Value; *p != '\0' && *p != '"' && *p != ';' && *p != '#'; p++) 537 | /* nothing */; 538 | return (*p != '\0' || (p > Value && *(p - 1) == ' ')) ? QUOTE_ENQUOTE : QUOTE_NONE; 539 | } 540 | 541 | static void writesection(TCHAR *LocalBuffer, const TCHAR *Section, INI_FILETYPE *fp) 542 | { 543 | if (Section != NULL && _tcslen(Section) > 0) { 544 | TCHAR *p; 545 | LocalBuffer[0] = '['; 546 | ini_strncpy(LocalBuffer + 1, Section, INI_BUFFERSIZE - 4, QUOTE_NONE); /* -1 for '[', -1 for ']', -2 for '\r\n' */ 547 | p = _tcsrchr(LocalBuffer, '\0'); 548 | assert(p != NULL); 549 | *p++ = ']'; 550 | _tcscpy(p, INI_LINETERM); /* copy line terminator (typically "\n") */ 551 | if (fp != NULL) 552 | (void)ini_write(LocalBuffer, fp); 553 | } /* if */ 554 | } 555 | 556 | static void writekey(TCHAR *LocalBuffer, const TCHAR *Key, const TCHAR *Value, INI_FILETYPE *fp) 557 | { 558 | TCHAR *p; 559 | enum quote_option option = check_enquote(Value); 560 | ini_strncpy(LocalBuffer, Key, INI_BUFFERSIZE - 3, QUOTE_NONE); /* -1 for '=', -2 for '\r\n' */ 561 | p = _tcsrchr(LocalBuffer, '\0'); 562 | assert(p != NULL); 563 | *p++ = '='; 564 | ini_strncpy(p, Value, INI_BUFFERSIZE - (p - LocalBuffer) - 2, option); /* -2 for '\r\n' */ 565 | p = _tcsrchr(LocalBuffer, '\0'); 566 | assert(p != NULL); 567 | _tcscpy(p, INI_LINETERM); /* copy line terminator (typically "\n") */ 568 | if (fp != NULL) 569 | (void)ini_write(LocalBuffer, fp); 570 | } 571 | 572 | static int cache_accum(const TCHAR *string, int *size, int max) 573 | { 574 | int len = (int)_tcslen(string); 575 | if (*size + len >= max) 576 | return 0; 577 | *size += len; 578 | return 1; 579 | } 580 | 581 | static int cache_flush(TCHAR *buffer, int *size, 582 | INI_FILETYPE *rfp, INI_FILETYPE *wfp, INI_FILEPOS *mark) 583 | { 584 | int terminator_len = (int)_tcslen(INI_LINETERM); 585 | int pos = 0; 586 | 587 | (void)ini_seek(rfp, mark); 588 | assert(buffer != NULL); 589 | buffer[0] = '\0'; 590 | assert(size != NULL); 591 | assert(*size <= INI_BUFFERSIZE); 592 | while (pos < *size) { 593 | (void)ini_read(buffer + pos, INI_BUFFERSIZE - pos, rfp); 594 | while (pos < *size && buffer[pos] != '\0') 595 | pos++; /* cannot use _tcslen() because buffer may not be zero-terminated */ 596 | } /* while */ 597 | if (buffer[0] != '\0') { 598 | assert(pos > 0 && pos <= INI_BUFFERSIZE); 599 | if (pos == INI_BUFFERSIZE) 600 | pos--; 601 | buffer[pos] = '\0'; /* force zero-termination (may be left unterminated in the above while loop) */ 602 | (void)ini_write(buffer, wfp); 603 | } 604 | ini_tell(rfp, mark); /* update mark */ 605 | *size = 0; 606 | /* return whether the buffer ended with a line termination */ 607 | return (pos > terminator_len) && (_tcscmp(buffer + pos - terminator_len, INI_LINETERM) == 0); 608 | } 609 | 610 | static int close_rename(INI_FILETYPE *rfp, INI_FILETYPE *wfp, const TCHAR *filename, TCHAR *buffer) 611 | { 612 | (void)ini_close(rfp); 613 | (void)ini_close(wfp); 614 | (void)ini_remove(filename); 615 | (void)ini_tempname(buffer, filename, INI_BUFFERSIZE); 616 | (void)ini_rename(buffer, filename); 617 | return 1; 618 | } 619 | 620 | /** ini_puts() 621 | * \param Section the name of the section to write the string in 622 | * \param Key the name of the entry to write, or NULL to erase all keys in the section 623 | * \param Value a pointer to the buffer the string, or NULL to erase the key 624 | * \param Filename the name and full path of the .ini file to write to 625 | * 626 | * \return 1 if successful, otherwise 0 627 | */ 628 | int ini_puts(const TCHAR *Section, const TCHAR *Key, const TCHAR *Value, const TCHAR *Filename) 629 | { 630 | INI_FILETYPE rfp; 631 | INI_FILETYPE wfp; 632 | INI_FILEPOS mark; 633 | INI_FILEPOS head, tail; 634 | TCHAR *sp, *ep; 635 | TCHAR LocalBuffer[INI_BUFFERSIZE]; 636 | int len, match, flag, cachelen; 637 | 638 | assert(Filename != NULL); 639 | if (!ini_openread(Filename, &rfp)) { 640 | /* If the .ini file doesn't exist, make a new file */ 641 | if (Key != NULL && Value != NULL) { 642 | if (!ini_openwrite(Filename, &wfp)) 643 | return 0; 644 | writesection(LocalBuffer, Section, &wfp); 645 | writekey(LocalBuffer, Key, Value, &wfp); 646 | (void)ini_close(&wfp); 647 | } /* if */ 648 | return 1; 649 | } /* if */ 650 | 651 | /* If parameters Key and Value are valid (so this is not an "erase" request) 652 | * and the setting already exists, there are two short-cuts to avoid rewriting 653 | * the INI file. 654 | */ 655 | if (Key != NULL && Value != NULL) { 656 | ini_tell(&rfp, &mark); 657 | match = getkeystring(&rfp, Section, Key, -1, -1, LocalBuffer, sizearray(LocalBuffer), &head); 658 | if (match) { 659 | /* if the current setting is identical to the one to write, there is 660 | * nothing to do. 661 | */ 662 | if (_tcscmp(LocalBuffer,Value) == 0) { 663 | (void)ini_close(&rfp); 664 | return 1; 665 | } /* if */ 666 | /* if the new setting has the same length as the current setting, and the 667 | * glue file permits file read/write access, we can modify in place. 668 | */ 669 | #if defined ini_openrewrite 670 | /* we already have the start of the (raw) line, get the end too */ 671 | ini_tell(&rfp, &tail); 672 | /* create new buffer (without writing it to file) */ 673 | writekey(LocalBuffer, Key, Value, NULL); 674 | if (_tcslen(LocalBuffer) == (size_t)(tail - head)) { 675 | /* length matches, close the file & re-open for read/write, then 676 | * write at the correct position 677 | */ 678 | (void)ini_close(&rfp); 679 | if (!ini_openrewrite(Filename, &wfp)) 680 | return 0; 681 | (void)ini_seek(&wfp, &head); 682 | (void)ini_write(LocalBuffer, &wfp); 683 | (void)ini_close(&wfp); 684 | return 1; 685 | } /* if */ 686 | #endif 687 | } /* if */ 688 | /* key not found, or different value & length -> proceed (but rewind the 689 | * input file first) 690 | */ 691 | (void)ini_seek(&rfp, &mark); 692 | } /* if */ 693 | 694 | /* Get a temporary file name to copy to. Use the existing name, but with 695 | * the last character set to a '~'. 696 | */ 697 | ini_tempname(LocalBuffer, Filename, INI_BUFFERSIZE); 698 | if (!ini_openwrite(LocalBuffer, &wfp)) { 699 | (void)ini_close(&rfp); 700 | return 0; 701 | } /* if */ 702 | (void)ini_tell(&rfp, &mark); 703 | cachelen = 0; 704 | 705 | /* Move through the file one line at a time until a section is 706 | * matched or until EOF. Copy to temp file as it is read. 707 | */ 708 | len = (Section != NULL) ? (int)_tcslen(Section) : 0; 709 | if (len > 0) { 710 | do { 711 | if (!ini_read(LocalBuffer, INI_BUFFERSIZE, &rfp)) { 712 | /* Failed to find section, so add one to the end */ 713 | flag = cache_flush(LocalBuffer, &cachelen, &rfp, &wfp, &mark); 714 | if (Key!=NULL && Value!=NULL) { 715 | if (!flag) 716 | (void)ini_write(INI_LINETERM, &wfp); /* force a new line behind the last line of the INI file */ 717 | writesection(LocalBuffer, Section, &wfp); 718 | writekey(LocalBuffer, Key, Value, &wfp); 719 | } /* if */ 720 | return close_rename(&rfp, &wfp, Filename, LocalBuffer); /* clean up and rename */ 721 | } /* if */ 722 | /* Copy the line from source to dest, but not if this is the section that 723 | * we are looking for and this section must be removed 724 | */ 725 | sp = skipleading(LocalBuffer); 726 | ep = _tcsrchr(sp, ']'); 727 | match = (*sp == '[' && ep != NULL && (int)(ep-sp-1) == len && _tcsncmp(sp + 1,Section,len) == 0); 728 | if (!match || Key != NULL) { 729 | if (!cache_accum(LocalBuffer, &cachelen, INI_BUFFERSIZE)) { 730 | cache_flush(LocalBuffer, &cachelen, &rfp, &wfp, &mark); 731 | (void)ini_read(LocalBuffer, INI_BUFFERSIZE, &rfp); 732 | cache_accum(LocalBuffer, &cachelen, INI_BUFFERSIZE); 733 | } /* if */ 734 | } /* if */ 735 | } while (!match); 736 | } /* if */ 737 | cache_flush(LocalBuffer, &cachelen, &rfp, &wfp, &mark); 738 | /* when deleting a section, the section head that was just found has not been 739 | * copied to the output file, but because this line was not "accumulated" in 740 | * the cache, the position in the input file was reset to the point just 741 | * before the section; this must now be skipped (again) 742 | */ 743 | if (Key == NULL) { 744 | (void)ini_read(LocalBuffer, INI_BUFFERSIZE, &rfp); 745 | (void)ini_tell(&rfp, &mark); 746 | } /* if */ 747 | 748 | /* Now that the section has been found, find the entry. Stop searching 749 | * upon leaving the section's area. Copy the file as it is read 750 | * and create an entry if one is not found. 751 | */ 752 | len = (Key != NULL) ? (int)_tcslen(Key) : 0; 753 | for( ;; ) { 754 | if (!ini_read(LocalBuffer, INI_BUFFERSIZE, &rfp)) { 755 | /* EOF without an entry so make one */ 756 | flag = cache_flush(LocalBuffer, &cachelen, &rfp, &wfp, &mark); 757 | if (Key!=NULL && Value!=NULL) { 758 | if (!flag) 759 | (void)ini_write(INI_LINETERM, &wfp); /* force a new line behind the last line of the INI file */ 760 | writekey(LocalBuffer, Key, Value, &wfp); 761 | } /* if */ 762 | return close_rename(&rfp, &wfp, Filename, LocalBuffer); /* clean up and rename */ 763 | } /* if */ 764 | sp = skipleading(LocalBuffer); 765 | ep = _tcschr(sp, '='); /* Parse out the equal sign */ 766 | if (ep == NULL) 767 | ep = _tcschr(sp, ':'); 768 | match = (ep != NULL && len > 0 && (int)(skiptrailing(ep,sp)-sp) == len && _tcsncmp(sp,Key,len) == 0); 769 | if ((Key != NULL && match) || *sp == '[') 770 | break; /* found the key, or found a new section */ 771 | /* copy other keys in the section */ 772 | if (Key == NULL) { 773 | (void)ini_tell(&rfp, &mark); /* we are deleting the entire section, so update the read position */ 774 | } else { 775 | if (!cache_accum(LocalBuffer, &cachelen, INI_BUFFERSIZE)) { 776 | cache_flush(LocalBuffer, &cachelen, &rfp, &wfp, &mark); 777 | (void)ini_read(LocalBuffer, INI_BUFFERSIZE, &rfp); 778 | cache_accum(LocalBuffer, &cachelen, INI_BUFFERSIZE); 779 | } /* if */ 780 | } /* if */ 781 | } /* for */ 782 | /* the key was found, or we just dropped on the next section (meaning that it 783 | * wasn't found); in both cases we need to write the key, but in the latter 784 | * case, we also need to write the line starting the new section after writing 785 | * the key 786 | */ 787 | flag = (*sp == '['); 788 | cache_flush(LocalBuffer, &cachelen, &rfp, &wfp, &mark); 789 | if (Key != NULL && Value != NULL) 790 | writekey(LocalBuffer, Key, Value, &wfp); 791 | /* cache_flush() reset the "read pointer" to the start of the line with the 792 | * previous key or the new section; read it again (because writekey() destroyed 793 | * the buffer) 794 | */ 795 | (void)ini_read(LocalBuffer, INI_BUFFERSIZE, &rfp); 796 | if (flag) { 797 | /* the new section heading needs to be copied to the output file */ 798 | cache_accum(LocalBuffer, &cachelen, INI_BUFFERSIZE); 799 | } else { 800 | /* forget the old key line */ 801 | (void)ini_tell(&rfp, &mark); 802 | } /* if */ 803 | /* Copy the rest of the INI file */ 804 | while (ini_read(LocalBuffer, INI_BUFFERSIZE, &rfp)) { 805 | if (!cache_accum(LocalBuffer, &cachelen, INI_BUFFERSIZE)) { 806 | cache_flush(LocalBuffer, &cachelen, &rfp, &wfp, &mark); 807 | (void)ini_read(LocalBuffer, INI_BUFFERSIZE, &rfp); 808 | cache_accum(LocalBuffer, &cachelen, INI_BUFFERSIZE); 809 | } /* if */ 810 | } /* while */ 811 | cache_flush(LocalBuffer, &cachelen, &rfp, &wfp, &mark); 812 | return close_rename(&rfp, &wfp, Filename, LocalBuffer); /* clean up and rename */ 813 | } 814 | 815 | /* Ansi C "itoa" based on Kernighan & Ritchie's "Ansi C" book. */ 816 | #define ABS(v) ((v) < 0 ? -(v) : (v)) 817 | 818 | static void strreverse(TCHAR *str) 819 | { 820 | int i, j; 821 | for (i = 0, j = (int)_tcslen(str) - 1; i < j; i++, j--) { 822 | TCHAR t = str[i]; 823 | str[i] = str[j]; 824 | str[j] = t; 825 | } /* for */ 826 | } 827 | 828 | static void long2str(long value, TCHAR *str) 829 | { 830 | int i = 0; 831 | long sign = value; 832 | 833 | /* generate digits in reverse order */ 834 | do { 835 | int n = (int)(value % 10); /* get next lowest digit */ 836 | str[i++] = (TCHAR)(ABS(n) + '0'); /* handle case of negative digit */ 837 | } while (value /= 10); /* delete the lowest digit */ 838 | if (sign < 0) 839 | str[i++] = '-'; 840 | str[i] = '\0'; 841 | 842 | strreverse(str); 843 | } 844 | 845 | /** ini_putl() 846 | * \param Section the name of the section to write the value in 847 | * \param Key the name of the entry to write 848 | * \param Value the value to write 849 | * \param Filename the name and full path of the .ini file to write to 850 | * 851 | * \return 1 if successful, otherwise 0 852 | */ 853 | int ini_putl(const TCHAR *Section, const TCHAR *Key, long Value, const TCHAR *Filename) 854 | { 855 | TCHAR LocalBuffer[32]; 856 | long2str(Value, LocalBuffer); 857 | return ini_puts(Section, Key, LocalBuffer, Filename); 858 | } 859 | 860 | #if defined INI_REAL 861 | /** ini_putf() 862 | * \param Section the name of the section to write the value in 863 | * \param Key the name of the entry to write 864 | * \param Value the value to write 865 | * \param Filename the name and full path of the .ini file to write to 866 | * 867 | * \return 1 if successful, otherwise 0 868 | */ 869 | int ini_putf(const TCHAR *Section, const TCHAR *Key, INI_REAL Value, const TCHAR *Filename) 870 | { 871 | TCHAR LocalBuffer[64]; 872 | ini_ftoa(LocalBuffer, Value); 873 | return ini_puts(Section, Key, LocalBuffer, Filename); 874 | } 875 | #endif /* INI_REAL */ 876 | #endif /* !INI_READONLY */ 877 | -------------------------------------------------------------------------------- /source/minIni.h: -------------------------------------------------------------------------------- 1 | /* minIni - Multi-Platform INI file parser, suitable for embedded systems 2 | * 3 | * Copyright (c) CompuPhase, 2008-2017 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 6 | * use this file except in compliance with the License. You may obtain a copy 7 | * of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 14 | * License for the specific language governing permissions and limitations 15 | * under the License. 16 | * 17 | * Version: $Id: minIni.h 53 2015-01-18 13:35:11Z thiadmer.riemersma@gmail.com $ 18 | */ 19 | #ifndef MININI_H 20 | #define MININI_H 21 | 22 | #include "minIni/minGlue.h" 23 | 24 | #if (defined _UNICODE || defined __UNICODE__ || defined UNICODE) && !defined INI_ANSIONLY 25 | #include 26 | #define mTCHAR TCHAR 27 | #else 28 | /* force TCHAR to be "char", but only for minIni */ 29 | #define mTCHAR char 30 | #endif 31 | 32 | #if !defined INI_BUFFERSIZE 33 | #define INI_BUFFERSIZE 512 34 | #endif 35 | 36 | #if defined __cplusplus 37 | extern "C" { 38 | #endif 39 | 40 | int ini_getbool(const mTCHAR *Section, const mTCHAR *Key, int DefValue, const mTCHAR *Filename); 41 | long ini_getl(const mTCHAR *Section, const mTCHAR *Key, long DefValue, const mTCHAR *Filename); 42 | int ini_gets(const mTCHAR *Section, const mTCHAR *Key, const mTCHAR *DefValue, mTCHAR *Buffer, int BufferSize, const mTCHAR *Filename); 43 | int ini_getsection(int idx, mTCHAR *Buffer, int BufferSize, const mTCHAR *Filename); 44 | int ini_getkey(const mTCHAR *Section, int idx, mTCHAR *Buffer, int BufferSize, const mTCHAR *Filename); 45 | 46 | #if defined INI_REAL 47 | INI_REAL ini_getf(const mTCHAR *Section, const mTCHAR *Key, INI_REAL DefValue, const mTCHAR *Filename); 48 | #endif 49 | 50 | #if !defined INI_READONLY 51 | int ini_putl(const mTCHAR *Section, const mTCHAR *Key, long Value, const mTCHAR *Filename); 52 | int ini_puts(const mTCHAR *Section, const mTCHAR *Key, const mTCHAR *Value, const mTCHAR *Filename); 53 | #if defined INI_REAL 54 | int ini_putf(const mTCHAR *Section, const mTCHAR *Key, INI_REAL Value, const mTCHAR *Filename); 55 | #endif 56 | #endif /* INI_READONLY */ 57 | 58 | #if !defined INI_NOBROWSE 59 | typedef int (*INI_CALLBACK)(const mTCHAR *Section, const mTCHAR *Key, const mTCHAR *Value, void *UserData); 60 | int ini_browse(INI_CALLBACK Callback, void *UserData, const mTCHAR *Filename); 61 | #endif /* INI_NOBROWSE */ 62 | 63 | #if defined __cplusplus 64 | } 65 | #endif 66 | 67 | 68 | #if defined __cplusplus 69 | 70 | #if defined __WXWINDOWS__ 71 | #include "wxMinIni.h" 72 | #else 73 | #include 74 | 75 | /* The C++ class in minIni.h was contributed by Steven Van Ingelgem. */ 76 | class minIni 77 | { 78 | public: 79 | minIni(const std::string& filename) : iniFilename(filename) 80 | { } 81 | 82 | bool getbool(const std::string& Section, const std::string& Key, bool DefValue=false) const 83 | { return ini_getbool(Section.c_str(), Key.c_str(), int(DefValue), iniFilename.c_str()) != 0; } 84 | 85 | long getl(const std::string& Section, const std::string& Key, long DefValue=0) const 86 | { return ini_getl(Section.c_str(), Key.c_str(), DefValue, iniFilename.c_str()); } 87 | 88 | int geti(const std::string& Section, const std::string& Key, int DefValue=0) const 89 | { return static_cast(this->getl(Section, Key, long(DefValue))); } 90 | 91 | std::string gets(const std::string& Section, const std::string& Key, const std::string& DefValue="") const 92 | { 93 | char buffer[INI_BUFFERSIZE]; 94 | ini_gets(Section.c_str(), Key.c_str(), DefValue.c_str(), buffer, INI_BUFFERSIZE, iniFilename.c_str()); 95 | return buffer; 96 | } 97 | 98 | std::string getsection(int idx) const 99 | { 100 | char buffer[INI_BUFFERSIZE]; 101 | ini_getsection(idx, buffer, INI_BUFFERSIZE, iniFilename.c_str()); 102 | return buffer; 103 | } 104 | 105 | std::string getkey(const std::string& Section, int idx) const 106 | { 107 | char buffer[INI_BUFFERSIZE]; 108 | ini_getkey(Section.c_str(), idx, buffer, INI_BUFFERSIZE, iniFilename.c_str()); 109 | return buffer; 110 | } 111 | 112 | #if defined INI_REAL 113 | INI_REAL getf(const std::string& Section, const std::string& Key, INI_REAL DefValue=0) const 114 | { return ini_getf(Section.c_str(), Key.c_str(), DefValue, iniFilename.c_str()); } 115 | #endif 116 | 117 | #if ! defined INI_READONLY 118 | bool put(const std::string& Section, const std::string& Key, long Value) 119 | { return ini_putl(Section.c_str(), Key.c_str(), Value, iniFilename.c_str()) != 0; } 120 | 121 | bool put(const std::string& Section, const std::string& Key, int Value) 122 | { return ini_putl(Section.c_str(), Key.c_str(), (long)Value, iniFilename.c_str()) != 0; } 123 | 124 | bool put(const std::string& Section, const std::string& Key, bool Value) 125 | { return ini_putl(Section.c_str(), Key.c_str(), (long)Value, iniFilename.c_str()) != 0; } 126 | 127 | bool put(const std::string& Section, const std::string& Key, const std::string& Value) 128 | { return ini_puts(Section.c_str(), Key.c_str(), Value.c_str(), iniFilename.c_str()) != 0; } 129 | 130 | bool put(const std::string& Section, const std::string& Key, const char* Value) 131 | { return ini_puts(Section.c_str(), Key.c_str(), Value, iniFilename.c_str()) != 0; } 132 | 133 | #if defined INI_REAL 134 | bool put(const std::string& Section, const std::string& Key, INI_REAL Value) 135 | { return ini_putf(Section.c_str(), Key.c_str(), Value, iniFilename.c_str()) != 0; } 136 | #endif 137 | 138 | bool del(const std::string& Section, const std::string& Key) 139 | { return ini_puts(Section.c_str(), Key.c_str(), 0, iniFilename.c_str()) != 0; } 140 | 141 | bool del(const std::string& Section) 142 | { return ini_puts(Section.c_str(), 0, 0, iniFilename.c_str()) != 0; } 143 | #endif 144 | 145 | #if !defined INI_NOBROWSE 146 | bool browse(INI_CALLBACK Callback, void *UserData) const 147 | { return ini_browse(Callback, UserData, iniFilename.c_str()) != 0; } 148 | #endif 149 | 150 | private: 151 | std::string iniFilename; 152 | }; 153 | 154 | #endif /* __WXWINDOWS__ */ 155 | #endif /* __cplusplus */ 156 | 157 | #endif /* MININI_H */ 158 | -------------------------------------------------------------------------------- /source/minIni/minGlue-FatFs.h: -------------------------------------------------------------------------------- 1 | /* Glue functions for the minIni library, based on the FatFs and Petit-FatFs 2 | * libraries, see http://elm-chan.org/fsw/ff/00index_e.html 3 | * 4 | * By CompuPhase, 2008-2012 5 | * This "glue file" is in the public domain. It is distributed without 6 | * warranties or conditions of any kind, either express or implied. 7 | * 8 | * (The FatFs and Petit-FatFs libraries are copyright by ChaN and licensed at 9 | * its own terms.) 10 | */ 11 | 12 | #define INI_BUFFERSIZE 256 /* maximum line length, maximum path length */ 13 | 14 | /* You must set _USE_STRFUNC to 1 or 2 in the include file ff.h (or tff.h) 15 | * to enable the "string functions" fgets() and fputs(). 16 | */ 17 | #include "ff.h" /* include tff.h for Tiny-FatFs */ 18 | 19 | #define INI_FILETYPE FIL 20 | #define ini_openread(filename,file) (f_open((file), (filename), FA_READ+FA_OPEN_EXISTING) == FR_OK) 21 | #define ini_openwrite(filename,file) (f_open((file), (filename), FA_WRITE+FA_CREATE_ALWAYS) == FR_OK) 22 | #define ini_close(file) (f_close(file) == FR_OK) 23 | #define ini_read(buffer,size,file) f_gets((buffer), (size),(file)) 24 | #define ini_write(buffer,file) f_puts((buffer), (file)) 25 | #define ini_remove(filename) (f_unlink(filename) == FR_OK) 26 | 27 | #define INI_FILEPOS DWORD 28 | #define ini_tell(file,pos) (*(pos) = f_tell((file))) 29 | #define ini_seek(file,pos) (f_lseek((file), *(pos)) == FR_OK) 30 | 31 | static int ini_rename(TCHAR *source, const TCHAR *dest) 32 | { 33 | /* Function f_rename() does not allow drive letters in the destination file */ 34 | char *drive = strchr(dest, ':'); 35 | drive = (drive == NULL) ? dest : drive + 1; 36 | return (f_rename(source, drive) == FR_OK); 37 | } 38 | -------------------------------------------------------------------------------- /source/minIni/minGlue-ccs.h: -------------------------------------------------------------------------------- 1 | /* minIni glue functions for FAT library by CCS, Inc. (as provided with their 2 | * PIC MCU compiler) 3 | * 4 | * By CompuPhase, 2011-2012 5 | * This "glue file" is in the public domain. It is distributed without 6 | * warranties or conditions of any kind, either express or implied. 7 | * 8 | * (The FAT library is copyright (c) 2007 Custom Computer Services, and 9 | * licensed at its own terms.) 10 | */ 11 | 12 | #define INI_BUFFERSIZE 256 /* maximum line length, maximum path length */ 13 | 14 | #ifndef FAT_PIC_C 15 | #error FAT library must be included before this module 16 | #endif 17 | #define const /* keyword not supported by CCS */ 18 | 19 | #define INI_FILETYPE FILE 20 | #define ini_openread(filename,file) (fatopen((filename), "r", (file)) == GOODEC) 21 | #define ini_openwrite(filename,file) (fatopen((filename), "w", (file)) == GOODEC) 22 | #define ini_close(file) (fatclose((file)) == 0) 23 | #define ini_read(buffer,size,file) (fatgets((buffer), (size), (file)) != NULL) 24 | #define ini_write(buffer,file) (fatputs((buffer), (file)) == GOODEC) 25 | #define ini_remove(filename) (rm_file((filename)) == 0) 26 | 27 | #define INI_FILEPOS fatpos_t 28 | #define ini_tell(file,pos) (fatgetpos((file), (pos)) == 0) 29 | #define ini_seek(file,pos) (fatsetpos((file), (pos)) == 0) 30 | 31 | #ifndef INI_READONLY 32 | /* CCS FAT library lacks a rename function, so instead we copy the file to the 33 | * new name and delete the old file 34 | */ 35 | static int ini_rename(char *source, char *dest) 36 | { 37 | FILE fr, fw; 38 | int n; 39 | 40 | if (fatopen(source, "r", &fr) != GOODEC) 41 | return 0; 42 | if (rm_file(dest) != 0) 43 | return 0; 44 | if (fatopen(dest, "w", &fw) != GOODEC) 45 | return 0; 46 | 47 | /* With some "insider knowledge", we can save some memory: the "source" 48 | * parameter holds a filename that was built from the "dest" parameter. It 49 | * was built in a local buffer with the size INI_BUFFERSIZE. We can reuse 50 | * this buffer for copying the file. 51 | */ 52 | while (n=fatread(source, 1, INI_BUFFERSIZE, &fr)) 53 | fatwrite(source, 1, n, &fw); 54 | 55 | fatclose(&fr); 56 | fatclose(&fw); 57 | 58 | /* Now we need to delete the source file. However, we have garbled the buffer 59 | * that held the filename of the source. So we need to build it again. 60 | */ 61 | ini_tempname(source, dest, INI_BUFFERSIZE); 62 | return rm_file(source) == 0; 63 | } 64 | #endif 65 | -------------------------------------------------------------------------------- /source/minIni/minGlue-efsl.h: -------------------------------------------------------------------------------- 1 | /* Glue functions for the minIni library, based on the EFS Library, see 2 | * http://www.efsl.be/ 3 | * 4 | * By CompuPhase, 2008-2012 5 | * This "glue file" is in the public domain. It is distributed without 6 | * warranties or conditions of any kind, either express or implied. 7 | * 8 | * (EFSL is copyright 2005-2006 Lennart Ysboodt and Michael De Nil, and 9 | * licensed under the GPL with an exception clause for static linking.) 10 | */ 11 | 12 | #define INI_BUFFERSIZE 256 /* maximum line length, maximum path length */ 13 | #define INI_LINETERM "\r\n" /* set line termination explicitly */ 14 | 15 | #include "efs.h" 16 | extern EmbeddedFileSystem g_efs; 17 | 18 | #define INI_FILETYPE EmbeddedFile 19 | #define ini_openread(filename,file) (file_fopen((file), &g_efs.myFs, (char*)(filename), 'r') == 0) 20 | #define ini_openwrite(filename,file) (file_fopen((file), &g_efs.myFs, (char*)(filename), 'w') == 0) 21 | #define ini_close(file) file_fclose(file) 22 | #define ini_read(buffer,size,file) (file_read((file), (size), (buffer)) > 0) 23 | #define ini_write(buffer,file) (file_write((file), strlen(buffer), (char*)(buffer)) > 0) 24 | #define ini_remove(filename) rmfile(&g_efs.myFs, (char*)(filename)) 25 | 26 | #define INI_FILEPOS euint32 27 | #define ini_tell(file,pos) (*(pos) = (file)->FilePtr)) 28 | #define ini_seek(file,pos) file_setpos((file), (*pos)) 29 | 30 | #if ! defined INI_READONLY 31 | /* EFSL lacks a rename function, so instead we copy the file to the new name 32 | * and delete the old file 33 | */ 34 | static int ini_rename(char *source, const char *dest) 35 | { 36 | EmbeddedFile fr, fw; 37 | int n; 38 | 39 | if (file_fopen(&fr, &g_efs.myFs, source, 'r') != 0) 40 | return 0; 41 | if (rmfile(&g_efs.myFs, (char*)dest) != 0) 42 | return 0; 43 | if (file_fopen(&fw, &g_efs.myFs, (char*)dest, 'w') != 0) 44 | return 0; 45 | 46 | /* With some "insider knowledge", we can save some memory: the "source" 47 | * parameter holds a filename that was built from the "dest" parameter. It 48 | * was built in buffer and this buffer has the size INI_BUFFERSIZE. We can 49 | * reuse this buffer for copying the file. 50 | */ 51 | while (n=file_read(&fr, INI_BUFFERSIZE, source)) 52 | file_write(&fw, n, source); 53 | 54 | file_fclose(&fr); 55 | file_fclose(&fw); 56 | 57 | /* Now we need to delete the source file. However, we have garbled the buffer 58 | * that held the filename of the source. So we need to build it again. 59 | */ 60 | ini_tempname(source, dest, INI_BUFFERSIZE); 61 | return rmfile(&g_efs.myFs, source) == 0; 62 | } 63 | #endif 64 | -------------------------------------------------------------------------------- /source/minIni/minGlue-ffs.h: -------------------------------------------------------------------------------- 1 | /* Glue functions for the minIni library, based on the "FAT Filing System" 2 | * library by embedded-code.com 3 | * 4 | * By CompuPhase, 2008-2012 5 | * This "glue file" is in the public domain. It is distributed without 6 | * warranties or conditions of any kind, either express or implied. 7 | * 8 | * (The "FAT Filing System" library itself is copyright embedded-code.com, and 9 | * licensed at its own terms.) 10 | */ 11 | 12 | #define INI_BUFFERSIZE 256 /* maximum line length, maximum path length */ 13 | #include 14 | 15 | #define INI_FILETYPE FFS_FILE* 16 | #define ini_openread(filename,file) ((*(file) = ffs_fopen((filename),"r")) != NULL) 17 | #define ini_openwrite(filename,file) ((*(file) = ffs_fopen((filename),"w")) != NULL) 18 | #define ini_close(file) (ffs_fclose(*(file)) == 0) 19 | #define ini_read(buffer,size,file) (ffs_fgets((buffer),(size),*(file)) != NULL) 20 | #define ini_write(buffer,file) (ffs_fputs((buffer),*(file)) >= 0) 21 | #define ini_rename(source,dest) (ffs_rename((source), (dest)) == 0) 22 | #define ini_remove(filename) (ffs_remove(filename) == 0) 23 | 24 | #define INI_FILEPOS long 25 | #define ini_tell(file,pos) (ffs_fgetpos(*(file), (pos)) == 0) 26 | #define ini_seek(file,pos) (ffs_fsetpos(*(file), (pos)) == 0) 27 | -------------------------------------------------------------------------------- /source/minIni/minGlue-mdd.h: -------------------------------------------------------------------------------- 1 | /* minIni glue functions for Microchip's "Memory Disk Drive" file system 2 | * library, as presented in Microchip application note AN1045. 3 | * 4 | * By CompuPhase, 2011-2014 5 | * This "glue file" is in the public domain. It is distributed without 6 | * warranties or conditions of any kind, either express or implied. 7 | * 8 | * (The "Microchip Memory Disk Drive File System" is copyright (c) Microchip 9 | * Technology Incorporated, and licensed at its own terms.) 10 | */ 11 | 12 | #define INI_BUFFERSIZE 256 /* maximum line length, maximum path length */ 13 | 14 | #include "MDD File System\fsio.h" 15 | #include 16 | 17 | #define INI_FILETYPE FSFILE* 18 | #define ini_openread(filename,file) ((*(file) = FSfopen((filename),FS_READ)) != NULL) 19 | #define ini_openwrite(filename,file) ((*(file) = FSfopen((filename),FS_WRITE)) != NULL) 20 | #define ini_openrewrite(filename,file) ((*(file) = fopen((filename),FS_READPLUS)) != NULL) 21 | #define ini_close(file) (FSfclose(*(file)) == 0) 22 | #define ini_write(buffer,file) (FSfwrite((buffer), 1, strlen(buffer), (*file)) > 0) 23 | #define ini_remove(filename) (FSremove((filename)) == 0) 24 | 25 | #define INI_FILEPOS long int 26 | #define ini_tell(file,pos) (*(pos) = FSftell(*(file))) 27 | #define ini_seek(file,pos) (FSfseek(*(file), *(pos), SEEK_SET) == 0) 28 | 29 | /* Since the Memory Disk Drive file system library reads only blocks of files, 30 | * the function to read a text line does so by "over-reading" a block of the 31 | * of the maximum size and truncating it behind the end-of-line. 32 | */ 33 | static int ini_read(char *buffer, int size, INI_FILETYPE *file) 34 | { 35 | size_t numread = size; 36 | char *eol; 37 | 38 | if ((numread = FSfread(buffer, 1, size, *file)) == 0) 39 | return 0; /* at EOF */ 40 | if ((eol = strchr(buffer, '\n')) == NULL) 41 | eol = strchr(buffer, '\r'); 42 | if (eol != NULL) { 43 | /* terminate the buffer */ 44 | *++eol = '\0'; 45 | /* "unread" the data that was read too much */ 46 | FSfseek(*file, - (int)(numread - (size_t)(eol - buffer)), SEEK_CUR); 47 | } /* if */ 48 | return 1; 49 | } 50 | 51 | #ifndef INI_READONLY 52 | static int ini_rename(const char *source, const char *dest) 53 | { 54 | FSFILE* ftmp = FSfopen((source), FS_READ); 55 | FSrename((dest), ftmp); 56 | return FSfclose(ftmp) == 0; 57 | } 58 | #endif 59 | -------------------------------------------------------------------------------- /source/minIni/minGlue-stdio.h: -------------------------------------------------------------------------------- 1 | /* Glue functions for the minIni library, based on the C/C++ stdio library 2 | * 3 | * Or better said: this file contains macros that maps the function interface 4 | * used by minIni to the standard C/C++ file I/O functions. 5 | * 6 | * By CompuPhase, 2008-2014 7 | * This "glue file" is in the public domain. It is distributed without 8 | * warranties or conditions of any kind, either express or implied. 9 | */ 10 | 11 | /* map required file I/O types and functions to the standard C library */ 12 | #include 13 | 14 | #define INI_FILETYPE FILE* 15 | #define ini_openread(filename,file) ((*(file) = fopen((filename),"rb")) != NULL) 16 | #define ini_openwrite(filename,file) ((*(file) = fopen((filename),"wb")) != NULL) 17 | #define ini_openrewrite(filename,file) ((*(file) = fopen((filename),"r+b")) != NULL) 18 | #define ini_close(file) (fclose(*(file)) == 0) 19 | #define ini_read(buffer,size,file) (fgets((buffer),(size),*(file)) != NULL) 20 | #define ini_write(buffer,file) (fputs((buffer),*(file)) >= 0) 21 | #define ini_rename(source,dest) (rename((source), (dest)) == 0) 22 | #define ini_remove(filename) (remove(filename) == 0) 23 | 24 | #define INI_FILEPOS long int 25 | #define ini_tell(file,pos) (*(pos) = ftell(*(file))) 26 | #define ini_seek(file,pos) (fseek(*(file), *(pos), SEEK_SET) == 0) 27 | 28 | /* for floating-point support, define additional types and functions */ 29 | #define INI_REAL float 30 | #define ini_ftoa(string,value) sprintf((string),"%f",(value)) 31 | #define ini_atof(string) (INI_REAL)strtod((string),NULL) 32 | -------------------------------------------------------------------------------- /source/minIni/minGlue.h: -------------------------------------------------------------------------------- 1 | /* Glue functions for the minIni library, based on the C/C++ stdio library 2 | * 3 | * Or better said: this file contains macros that maps the function interface 4 | * used by minIni to the standard C/C++ file I/O functions. 5 | * 6 | * By CompuPhase, 2008-2014 7 | * This "glue file" is in the public domain. It is distributed without 8 | * warranties or conditions of any kind, either express or implied. 9 | */ 10 | 11 | /* map required file I/O types and functions to the standard C library */ 12 | #include 13 | 14 | #define INI_FILETYPE FILE* 15 | #define ini_openread(filename,file) ((*(file) = fopen((filename),"rb")) != NULL) 16 | #define ini_openwrite(filename,file) ((*(file) = fopen((filename),"wb")) != NULL) 17 | #define ini_openrewrite(filename,file) ((*(file) = fopen((filename),"r+b")) != NULL) 18 | #define ini_close(file) (fclose(*(file)) == 0) 19 | #define ini_read(buffer,size,file) (fgets((buffer),(size),*(file)) != NULL) 20 | #define ini_write(buffer,file) (fputs((buffer),*(file)) >= 0) 21 | #define ini_rename(source,dest) (rename((source), (dest)) == 0) 22 | #define ini_remove(filename) (remove(filename) == 0) 23 | 24 | #define INI_FILEPOS long int 25 | #define ini_tell(file,pos) (*(pos) = ftell(*(file))) 26 | #define ini_seek(file,pos) (fseek(*(file), *(pos), SEEK_SET) == 0) 27 | 28 | /* for floating-point support, define additional types and functions */ 29 | #define INI_REAL float 30 | #define ini_ftoa(string,value) sprintf((string),"%f",(value)) 31 | #define ini_atof(string) (INI_REAL)strtod((string),NULL) 32 | -------------------------------------------------------------------------------- /source/minIni/wxMinIni.h: -------------------------------------------------------------------------------- 1 | /* minIni - Multi-Platform INI file parser, wxWidgets interface 2 | * 3 | * Copyright (c) CompuPhase, 2008-2012 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not 6 | * use this file except in compliance with the License. You may obtain a copy 7 | * of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 14 | * License for the specific language governing permissions and limitations 15 | * under the License. 16 | * 17 | * Version: $Id: wxMinIni.h 44 2012-01-04 15:52:56Z thiadmer.riemersma@gmail.com $ 18 | */ 19 | #ifndef WXMININI_H 20 | #define WXMININI_H 21 | 22 | #include 23 | #include "minIni.h" 24 | 25 | class minIni 26 | { 27 | public: 28 | minIni(const wxString& filename) : iniFilename(filename) 29 | { } 30 | 31 | bool getbool(const wxString& Section, const wxString& Key, bool DefValue=false) const 32 | { return ini_getbool(Section.utf8_str(), Key.utf8_str(), int(DefValue), iniFilename.utf8_str()) != 0; } 33 | 34 | long getl(const wxString& Section, const wxString& Key, long DefValue=0) const 35 | { return ini_getl(Section.utf8_str(), Key.utf8_str(), DefValue, iniFilename.utf8_str()); } 36 | 37 | int geti(const wxString& Section, const wxString& Key, int DefValue=0) const 38 | { return static_cast(ini_getl(Section.utf8_str(), Key.utf8_str(), (long)DefValue, iniFilename.utf8_str())); } 39 | 40 | wxString gets(const wxString& Section, const wxString& Key, const wxString& DefValue=wxT("")) const 41 | { 42 | char buffer[INI_BUFFERSIZE]; 43 | ini_gets(Section.utf8_str(), Key.utf8_str(), DefValue.utf8_str(), buffer, INI_BUFFERSIZE, iniFilename.utf8_str()); 44 | wxString result = wxString::FromUTF8(buffer); 45 | return result; 46 | } 47 | 48 | wxString getsection(int idx) const 49 | { 50 | char buffer[INI_BUFFERSIZE]; 51 | ini_getsection(idx, buffer, INI_BUFFERSIZE, iniFilename.utf8_str()); 52 | wxString result = wxString::FromUTF8(buffer); 53 | return result; 54 | } 55 | 56 | wxString getkey(const wxString& Section, int idx) const 57 | { 58 | char buffer[INI_BUFFERSIZE]; 59 | ini_getkey(Section.utf8_str(), idx, buffer, INI_BUFFERSIZE, iniFilename.utf8_str()); 60 | wxString result = wxString::FromUTF8(buffer); 61 | return result; 62 | } 63 | 64 | #if defined INI_REAL 65 | INI_REAL getf(const wxString& Section, wxString& Key, INI_REAL DefValue=0) const 66 | { return ini_getf(Section.utf8_str(), Key.utf8_str(), DefValue, iniFilename.utf8_str()); } 67 | #endif 68 | 69 | #if ! defined INI_READONLY 70 | bool put(const wxString& Section, const wxString& Key, long Value) const 71 | { return ini_putl(Section.utf8_str(), Key.utf8_str(), Value, iniFilename.utf8_str()) != 0; } 72 | 73 | bool put(const wxString& Section, const wxString& Key, int Value) const 74 | { return ini_putl(Section.utf8_str(), Key.utf8_str(), (long)Value, iniFilename.utf8_str()) != 0; } 75 | 76 | bool put(const wxString& Section, const wxString& Key, bool Value) const 77 | { return ini_putl(Section.utf8_str(), Key.utf8_str(), (long)Value, iniFilename.utf8_str()) != 0; } 78 | 79 | bool put(const wxString& Section, const wxString& Key, const wxString& Value) const 80 | { return ini_puts(Section.utf8_str(), Key.utf8_str(), Value.utf8_str(), iniFilename.utf8_str()) != 0; } 81 | 82 | bool put(const wxString& Section, const wxString& Key, const char* Value) const 83 | { return ini_puts(Section.utf8_str(), Key.utf8_str(), Value, iniFilename.utf8_str()) != 0; } 84 | 85 | #if defined INI_REAL 86 | bool put(const wxString& Section, const wxString& Key, INI_REAL Value) const 87 | { return ini_putf(Section.utf8_str(), Key.utf8_str(), Value, iniFilename.utf8_str()) != 0; } 88 | #endif 89 | 90 | bool del(const wxString& Section, const wxString& Key) const 91 | { return ini_puts(Section.utf8_str(), Key.utf8_str(), 0, iniFilename.utf8_str()) != 0; } 92 | 93 | bool del(const wxString& Section) const 94 | { return ini_puts(Section.utf8_str(), 0, 0, iniFilename.utf8_str()) != 0; } 95 | #endif 96 | 97 | private: 98 | wxString iniFilename; 99 | }; 100 | 101 | #endif /* WXMININI_H */ 102 | -------------------------------------------------------------------------------- /source/mp3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include "util.h" 9 | #include "console.h" 10 | 11 | #include 12 | 13 | 14 | #define BUF_COUNT 2 15 | 16 | static size_t buffSize; 17 | static mpg123_handle *mh = NULL; 18 | static uint32_t rate; 19 | static uint8_t channels; 20 | 21 | AudioOutBuffer* audout_released_buf; 22 | 23 | AudioOutBuffer audiobuf[BUF_COUNT]; 24 | u8* buffData[BUF_COUNT]; 25 | int curBuf = 0; 26 | #define swapbuf (curBuf = (curBuf+1)%(BUF_COUNT)) 27 | 28 | static Mutex mp3Mutex; 29 | 30 | void mp3MutInit() { 31 | mutexInit(&mp3Mutex); 32 | 33 | buffSize = 80256; 34 | for(int curBuf = 0; curBuf < BUF_COUNT; curBuf++) { 35 | buffData[curBuf] = memalign(0x1000, buffSize); 36 | } 37 | 38 | audoutInitialize(); 39 | audoutStartAudioOut(); 40 | } 41 | 42 | int initMp3(const char* file) 43 | { 44 | 45 | int err = 0; 46 | int encoding = 0; 47 | 48 | if((err = mpg123_init()) != MPG123_OK) 49 | return err; 50 | 51 | mpg123_pars* pars = mpg123_new_pars(&err); 52 | mpg123_par(pars, MPG123_FORCE_RATE, audoutGetSampleRate(), 0); 53 | mpg123_par(pars, MPG123_FORCE_STEREO, 1, 0); 54 | 55 | if((mh = mpg123_parnew(pars, NULL, &err)) == NULL) 56 | { 57 | printf("Error: %s\n", mpg123_plain_strerror(err)); 58 | return err; 59 | } 60 | 61 | 62 | 63 | if(mpg123_open(mh, file) != MPG123_OK || 64 | mpg123_getformat(mh, (long *) &rate, (int *) &channels, &encoding) != MPG123_OK) 65 | { 66 | printf("Trouble with mpg123: %s\n", mpg123_strerror(mh)); 67 | return -1; 68 | } 69 | 70 | /* 71 | * Ensure that this output format will not change (it might, when we allow 72 | * it). 73 | */ 74 | mpg123_format_none(mh); 75 | mpg123_format(mh, rate, channels, encoding); 76 | 77 | 78 | return 0; 79 | } 80 | 81 | /** 82 | * Get sampling rate of MP3 file. 83 | * 84 | * \return Sampling rate. 85 | */ 86 | uint32_t rateMp3(void) 87 | { 88 | return rate; 89 | } 90 | 91 | /** 92 | * Get number of channels of MP3 file. 93 | * 94 | * \return Number of channels for opened file. 95 | */ 96 | uint8_t channelMp3(void) 97 | { 98 | return channels; 99 | } 100 | 101 | /** 102 | * Decode part of open MP3 file. 103 | * 104 | * \param buffer Decoded output. 105 | * \return Samples read for each channel. 106 | */ 107 | uint64_t decodeMp3(void* buffer) 108 | { 109 | memset(buffer, 0, buffSize); 110 | size_t done = 0; 111 | mpg123_read(mh, buffer, buffSize, &done); 112 | return done / (sizeof(int16_t)); 113 | } 114 | 115 | /** 116 | * Free MP3 decoder. 117 | */ 118 | void exitMp3(void) 119 | { 120 | /*for(int curBuf = 0; curBuf < BUF_COUNT; curBuf++) { 121 | free(buffData[curBuf]); 122 | }*/ 123 | mpg123_close(mh); 124 | mpg123_delete(mh); 125 | mpg123_exit(); 126 | } 127 | 128 | int fillBuf() { 129 | int count = decodeMp3(buffData[curBuf]); 130 | if(count == 0) 131 | return count; 132 | audiobuf[curBuf].next = 0; 133 | audiobuf[curBuf].buffer = buffData[curBuf]; 134 | audiobuf[curBuf].buffer_size = buffSize; 135 | audiobuf[curBuf].data_size = buffSize; 136 | audiobuf[curBuf].data_offset = 0; 137 | audoutAppendAudioOutBuffer(&audiobuf[curBuf]); 138 | swapbuf; 139 | return count; 140 | } 141 | 142 | 143 | void playMp3(char* file) { 144 | mutexLock(&mp3Mutex); 145 | 146 | initMp3(file); 147 | 148 | u32 released_count = 0; 149 | int toPlayCount = 0; 150 | for(int curBuf = 0; curBuf < BUF_COUNT/2; curBuf++) 151 | toPlayCount += fillBuf() > 0; 152 | 153 | 154 | int lastFill = 1; 155 | while(appletMainLoop() && lastFill) 156 | { 157 | for(int curBuf = 0; curBuf < BUF_COUNT/2; curBuf++) { 158 | lastFill = fillBuf(); 159 | toPlayCount += lastFill > 0; 160 | } 161 | for(int curBuf = 0; curBuf < BUF_COUNT/2 && toPlayCount--; curBuf++) 162 | audoutWaitPlayFinish(&audout_released_buf, &released_count, 1000000000L); 163 | 164 | } 165 | 166 | while(toPlayCount--) 167 | audoutWaitPlayFinish(&audout_released_buf, &released_count, 1000000000L); 168 | 169 | exitMp3(); 170 | mutexUnlock(&mp3Mutex); 171 | } -------------------------------------------------------------------------------- /source/mp3.h: -------------------------------------------------------------------------------- 1 | void playMp3(char* file); 2 | void mp3MutInit(); -------------------------------------------------------------------------------- /source/util.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | #include "mp3.h" 10 | 11 | void fatalLater(Result err) 12 | { 13 | Handle srv; 14 | 15 | while (R_FAILED(smGetServiceOriginal(&srv, smEncodeName("fatal:u")))) 16 | { 17 | // wait one sec and retry 18 | svcSleepThread(1000000000L); 19 | } 20 | 21 | // fatal is here time, fatal like a boss 22 | IpcCommand c; 23 | ipcInitialize(&c); 24 | ipcSendPid(&c); 25 | struct 26 | { 27 | u64 magic; 28 | u64 cmd_id; 29 | u64 result; 30 | u64 unknown; 31 | } * raw; 32 | 33 | raw = ipcPrepareHeader(&c, sizeof(*raw)); 34 | 35 | raw->magic = SFCI_MAGIC; 36 | raw->cmd_id = 1; 37 | raw->result = err; 38 | raw->unknown = 0; 39 | 40 | ipcDispatch(srv); 41 | svcCloseHandle(srv); 42 | } 43 | 44 | 45 | bool paused = false; 46 | static Mutex pausedMutex; 47 | void pauseInit() { 48 | mutexInit(&pausedMutex); 49 | mutexLock(&pausedMutex); 50 | FILE *should_pause_file = fopen("/config/sys-ftpd/ftpd_paused", "r"); 51 | if (should_pause_file != NULL) { 52 | paused = true; 53 | fclose(should_pause_file); 54 | } 55 | mutexUnlock(&pausedMutex); 56 | } 57 | 58 | 59 | bool isPaused() { 60 | mutexLock(&pausedMutex); 61 | bool ret = paused; 62 | mutexUnlock(&pausedMutex); 63 | return ret; 64 | } 65 | 66 | void setPaused(bool newPaused) { 67 | mutexLock(&pausedMutex); 68 | paused = newPaused; 69 | if(paused) { 70 | FILE *should_pause_file = fopen("/config/sys-ftpd/ftpd_paused", "w"); 71 | fclose(should_pause_file); 72 | playMp3("/config/sys-ftpd/pauseon.mp3"); 73 | } else { 74 | unlink("/config/sys-ftpd/ftpd_paused"); 75 | playMp3("/config/sys-ftpd/pauseoff.mp3"); 76 | } 77 | mutexUnlock(&pausedMutex); 78 | } 79 | -------------------------------------------------------------------------------- /source/util.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void fatalLater(Result err); 4 | void pauseInit(); 5 | bool isPaused(); 6 | void setPaused(bool newPaused); -------------------------------------------------------------------------------- /sys-ftpd.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sys-ftpd", 3 | "title_id": "0x420000000000000E", 4 | "title_id_range_min": "0x420000000000000E", 5 | "title_id_range_max": "0x420000000000000E", 6 | "main_thread_stack_size": "0x00024000", 7 | "main_thread_priority": 49, 8 | "default_cpu_id": 3, 9 | "process_category": 0, 10 | "is_retail": true, 11 | "pool_partition": 2, 12 | "is_64_bit": true, 13 | "address_space_type": 3, 14 | "filesystem_access": { 15 | "permissions": "0xffffffffffffffff" 16 | }, 17 | "service_access": ["*"], 18 | "service_host": ["*"], 19 | "kernel_capabilities": [{ 20 | "type": "kernel_flags", 21 | "value": { 22 | "highest_thread_priority": 63, 23 | "lowest_thread_priority": 24, 24 | "lowest_cpu_id": 3, 25 | "highest_cpu_id": 3 26 | } 27 | }, { 28 | "type": "syscalls", 29 | "value": { 30 | "svcUnknown": "0x00", 31 | "svcSetHeapSize": "0x01", 32 | "svcSetMemoryPermission": "0x02", 33 | "svcSetMemoryAttribute": "0x03", 34 | "svcMapMemory": "0x04", 35 | "svcUnmapMemory": "0x05", 36 | "svcQueryMemory": "0x06", 37 | "svcExitProcess": "0x07", 38 | "svcCreateThread": "0x08", 39 | "svcStartThread": "0x09", 40 | "svcExitThread": "0x0a", 41 | "svcSleepThread": "0x0b", 42 | "svcGetThreadPriority": "0x0c", 43 | "svcSetThreadPriority": "0x0d", 44 | "svcGetThreadCoreMask": "0x0e", 45 | "svcSetThreadCoreMask": "0x0f", 46 | "svcGetCurrentProcessorNumber": "0x10", 47 | "svcSignalEvent": "0x11", 48 | "svcClearEvent": "0x12", 49 | "svcMapSharedMemory": "0x13", 50 | "svcUnmapSharedMemory": "0x14", 51 | "svcCreateTransferMemory": "0x15", 52 | "svcCloseHandle": "0x16", 53 | "svcResetSignal": "0x17", 54 | "svcWaitSynchronization": "0x18", 55 | "svcCancelSynchronization": "0x19", 56 | "svcArbitrateLock": "0x1a", 57 | "svcArbitrateUnlock": "0x1b", 58 | "svcWaitProcessWideKeyAtomic": "0x1c", 59 | "svcSignalProcessWideKey": "0x1d", 60 | "svcGetSystemTick": "0x1e", 61 | "svcConnectToNamedPort": "0x1f", 62 | "svcSendSyncRequestLight": "0x20", 63 | "svcSendSyncRequest": "0x21", 64 | "svcSendSyncRequestWithUserBuffer": "0x22", 65 | "svcSendAsyncRequestWithUserBuffer": "0x23", 66 | "svcGetProcessId": "0x24", 67 | "svcGetThreadId": "0x25", 68 | "svcBreak": "0x26", 69 | "svcOutputDebugString": "0x27", 70 | "svcReturnFromException": "0x28", 71 | "svcGetInfo": "0x29", 72 | "svcFlushEntireDataCache": "0x2a", 73 | "svcFlushDataCache": "0x2b", 74 | "svcMapPhysicalMemory": "0x2c", 75 | "svcUnmapPhysicalMemory": "0x2d", 76 | "svcGetFutureThreadInfo": "0x2e", 77 | "svcGetLastThreadInfo": "0x2f", 78 | "svcGetResourceLimitLimitValue": "0x30", 79 | "svcGetResourceLimitCurrentValue": "0x31", 80 | "svcSetThreadActivity": "0x32", 81 | "svcGetThreadContext3": "0x33", 82 | "svcWaitForAddress": "0x34", 83 | "svcSignalToAddress": "0x35", 84 | "svcUnknown": "0x36", 85 | "svcUnknown": "0x37", 86 | "svcUnknown": "0x38", 87 | "svcUnknown": "0x39", 88 | "svcUnknown": "0x3a", 89 | "svcUnknown": "0x3b", 90 | "svcDumpInfo": "0x3c", 91 | "svcDumpInfoNew": "0x3d", 92 | "svcUnknown": "0x3e", 93 | "svcUnknown": "0x3f", 94 | "svcCreateSession": "0x40", 95 | "svcAcceptSession": "0x41", 96 | "svcReplyAndReceiveLight": "0x42", 97 | "svcReplyAndReceive": "0x43", 98 | "svcReplyAndReceiveWithUserBuffer": "0x44", 99 | "svcCreateEvent": "0x45", 100 | "svcUnknown": "0x46", 101 | "svcUnknown": "0x47", 102 | "svcMapPhysicalMemoryUnsafe": "0x48", 103 | "svcUnmapPhysicalMemoryUnsafe": "0x49", 104 | "svcSetUnsafeLimit": "0x4a", 105 | "svcCreateCodeMemory": "0x4b", 106 | "svcControlCodeMemory": "0x4c", 107 | "svcSleepSystem": "0x4d", 108 | "svcReadWriteRegister": "0x4e", 109 | "svcSetProcessActivity": "0x4f", 110 | "svcCreateSharedMemory": "0x50", 111 | "svcMapTransferMemory": "0x51", 112 | "svcUnmapTransferMemory": "0x52", 113 | "svcCreateInterruptEvent": "0x53", 114 | "svcQueryPhysicalAddress": "0x54", 115 | "svcQueryIoMapping": "0x55", 116 | "svcCreateDeviceAddressSpace": "0x56", 117 | "svcAttachDeviceAddressSpace": "0x57", 118 | "svcDetachDeviceAddressSpace": "0x58", 119 | "svcMapDeviceAddressSpaceByForce": "0x59", 120 | "svcMapDeviceAddressSpaceAligned": "0x5a", 121 | "svcMapDeviceAddressSpace": "0x5b", 122 | "svcUnmapDeviceAddressSpace": "0x5c", 123 | "svcInvalidateProcessDataCache": "0x5d", 124 | "svcStoreProcessDataCache": "0x5e", 125 | "svcFlushProcessDataCache": "0x5f", 126 | "svcDebugActiveProcess": "0x60", 127 | "svcBreakDebugProcess": "0x61", 128 | "svcTerminateDebugProcess": "0x62", 129 | "svcGetDebugEvent": "0x63", 130 | "svcContinueDebugEvent": "0x64", 131 | "svcGetProcessList": "0x65", 132 | "svcGetThreadList": "0x66", 133 | "svcGetDebugThreadContext": "0x67", 134 | "svcSetDebugThreadContext": "0x68", 135 | "svcQueryDebugProcessMemory": "0x69", 136 | "svcReadDebugProcessMemory": "0x6a", 137 | "svcWriteDebugProcessMemory": "0x6b", 138 | "svcSetHardwareBreakPoint": "0x6c", 139 | "svcGetDebugThreadParam": "0x6d", 140 | "svcUnknown": "0x6e", 141 | "svcGetSystemInfo": "0x6f", 142 | "svcCreatePort": "0x70", 143 | "svcManageNamedPort": "0x71", 144 | "svcConnectToPort": "0x72", 145 | "svcSetProcessMemoryPermission": "0x73", 146 | "svcMapProcessMemory": "0x74", 147 | "svcUnmapProcessMemory": "0x75", 148 | "svcQueryProcessMemory": "0x76", 149 | "svcMapProcessCodeMemory": "0x77", 150 | "svcUnmapProcessCodeMemory": "0x78", 151 | "svcCreateProcess": "0x79", 152 | "svcStartProcess": "0x7a", 153 | "svcTerminateProcess": "0x7b", 154 | "svcGetProcessInfo": "0x7c", 155 | "svcCreateResourceLimit": "0x7d", 156 | "svcSetResourceLimitLimitValue": "0x7e", 157 | "svcCallSecureMonitor": "0x7f" 158 | } 159 | }, { 160 | "type": "min_kernel_version", 161 | "value": "0x0060" 162 | }, { 163 | "type": "handle_table_size", 164 | "value": 1023 165 | }, { 166 | "type": "debug_flags", 167 | "value": { 168 | "allow_debug": false, 169 | "force_debug": true 170 | } 171 | }] 172 | } --------------------------------------------------------------------------------