├── .editorconfig ├── .vs ├── base-parallel.vcxproj ├── base-parallel.vcxproj.filters └── base-parallel.vcxproj.user ├── Directory.Build.props ├── LICENSE.txt ├── README.md ├── appveyor.yml ├── base-parallel.sln ├── rename.sh └── src ├── base-parallel.c └── msapi_utf8.h /.editorconfig: -------------------------------------------------------------------------------- 1 | # indicate this is the root of the project 2 | root = true 3 | 4 | [*] 5 | indent_style = tab 6 | indent_size = 4 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | charset = utf-8 10 | -------------------------------------------------------------------------------- /.vs/base-parallel.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | ARM64 7 | 8 | 9 | Debug 10 | Win32 11 | 12 | 13 | Release 14 | ARM64 15 | 16 | 17 | Release 18 | Win32 19 | 20 | 21 | Debug 22 | x64 23 | 24 | 25 | Release 26 | x64 27 | 28 | 29 | 30 | 16.0 31 | {6C2BED99-5A0A-42A2-AEBE-66717FA92232} 32 | Win32Proj 33 | baseconsole 34 | 35 | 36 | 37 | Application 38 | true 39 | Unicode 40 | v142 41 | 42 | 43 | Application 44 | false 45 | true 46 | Unicode 47 | v142 48 | 49 | 50 | Application 51 | true 52 | Unicode 53 | v142 54 | 55 | 56 | Application 57 | true 58 | Unicode 59 | v142 60 | 61 | 62 | Application 63 | false 64 | true 65 | Unicode 66 | v142 67 | 68 | 69 | Application 70 | false 71 | true 72 | Unicode 73 | v142 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | <_ProjectFileVersion>10.0.30319.1 100 | $(SolutionDir)arm64\$(Configuration)\ 101 | $(SolutionDir)arm64\$(Configuration)\$(ProjectName)\ 102 | $(SolutionDir)arm64\$(Configuration)\ 103 | $(SolutionDir)arm64\$(Configuration)\$(ProjectName)\ 104 | $(SolutionDir)x86\$(Configuration)\ 105 | $(SolutionDir)x86\$(Configuration)\$(ProjectName)\ 106 | $(SolutionDir)x86\$(Configuration)\ 107 | $(SolutionDir)x86\$(Configuration)\$(ProjectName)\ 108 | $(SolutionDir)x64\$(Configuration)\ 109 | $(SolutionDir)x64\$(Configuration)\$(ProjectName)\ 110 | $(SolutionDir)x64\$(Configuration)\ 111 | $(SolutionDir)x64\$(Configuration)\$(ProjectName)\ 112 | false 113 | false 114 | false 115 | false 116 | false 117 | false 118 | 119 | 120 | true 121 | 122 | 123 | true 124 | 125 | 126 | true 127 | 128 | 129 | false 130 | 131 | 132 | false 133 | 134 | 135 | false 136 | 137 | 138 | 139 | /DAPP_VERSION=$(AppVersion) %(AdditionalOptions) 140 | 141 | 142 | 143 | 144 | 145 | 146 | Level3 147 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 148 | false 149 | CompileAsC 150 | MultiThreadedDebug 151 | 152 | 153 | Console 154 | true 155 | 156 | 157 | 158 | 159 | 160 | 161 | Level3 162 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions) 163 | false 164 | CompileAsC 165 | MultiThreadedDebug 166 | 167 | 168 | Console 169 | true 170 | 171 | 172 | 173 | 174 | 175 | 176 | Level3 177 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions) 178 | false 179 | CompileAsC 180 | MultiThreadedDebug 181 | 182 | 183 | Console 184 | true 185 | 186 | 187 | 188 | 189 | 190 | 191 | Level3 192 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 193 | false 194 | CompileAsC 195 | MultiThreaded 196 | 197 | 198 | Console 199 | true 200 | true 201 | true 202 | 203 | 204 | 205 | 206 | 207 | 208 | Level3 209 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 210 | false 211 | CompileAsC 212 | MultiThreaded 213 | 214 | 215 | Console 216 | true 217 | true 218 | true 219 | 220 | 221 | 222 | 223 | 224 | 225 | Level3 226 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 227 | false 228 | CompileAsC 229 | MultiThreaded 230 | 231 | 232 | Console 233 | true 234 | true 235 | true 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | -------------------------------------------------------------------------------- /.vs/base-parallel.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | 14 | 15 | Source Files 16 | 17 | 18 | 19 | 20 | Header Files 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /.vs/base-parallel.vcxproj.user: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /Directory.Build.props: -------------------------------------------------------------------------------- 1 | 2 | 3 | false 4 | 5 | 6 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | base-parallel: A win32 parallel task template 2 | ============================================= 3 | 4 | [![Build status](https://img.shields.io/appveyor/ci/pbatard/base-parallel.svg?style=flat-square)](https://ci.appveyor.com/project/pbatard/base-parallel) 5 | [![Release](https://img.shields.io/github/release-pre/pbatard/base-parallel.svg?style=flat-square)](https://github.com/pbatard/base-parallel/releases) 6 | [![Github stats](https://img.shields.io/github/downloads/pbatard/base-parallel/total.svg?style=flat-square)](https://github.com/pbatard/base-parallel/releases) 7 | [![Licence](https://img.shields.io/badge/license-GPLv3-blue.svg?style=flat-square)](https://www.gnu.org/licenses/gpl-3.0.en.html) 8 | 9 | A base sample for running a CPU-heavy task against multiple core/CPUs. -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | image: Visual Studio 2019 2 | 3 | skip_non_tags: true 4 | 5 | environment: 6 | FRIENDLY_NAME: Base Parallel 7 | 8 | build_script: 9 | ps: |- 10 | msbuild "$env:APPVEYOR_PROJECT_NAME.sln" /m /p:Configuration=Release /p:Platform=x86 /p:AppVersion=$env:APPVEYOR_REPO_TAG_NAME /logger:"C:\Program Files\AppVeyor\BuildAgent\Appveyor.MSBuildLogger.dll" 11 | 12 | after_build: 13 | ps: |- 14 | # Make sure you use quotes around variables below! 15 | 7z a "$env:APPVEYOR_PROJECT_NAME.zip" ".\x86\Release\*.exe" LICENSE.txt 16 | Get-FileHash "$env:APPVEYOR_PROJECT_NAME.zip" -Algorithm SHA256 | Format-List 17 | 18 | artifacts: 19 | - path: $(APPVEYOR_PROJECT_NAME).zip 20 | name: $(FRIENDLY_NAME) 21 | 22 | deploy: 23 | release: $(APPVEYOR_REPO_TAG_NAME) 24 | description: $(FRIENDLY_NAME) $(APPVEYOR_REPO_TAG_NAME) 25 | provider: GitHub 26 | auth_token: 27 | secure: w5YuQOim+G+U7FxxrL0BH6t0trCWKCs9DMZlF4xqF2XGC6SymzwaJrPWrKeeJHPK 28 | artifact: /.*\.zip/ 29 | draft: false 30 | prerelease: false 31 | -------------------------------------------------------------------------------- /base-parallel.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.29926.136 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "base-parallel", ".vs\base-parallel.vcxproj", "{6C2BED99-5A0A-42A2-AEBE-66717FA92232}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|ARM64 = Debug|ARM64 11 | Debug|x64 = Debug|x64 12 | Debug|x86 = Debug|x86 13 | Release|ARM64 = Release|ARM64 14 | Release|x64 = Release|x64 15 | Release|x86 = Release|x86 16 | EndGlobalSection 17 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 18 | {6C2BED99-5A0A-42A2-AEBE-66717FA92232}.Debug|ARM64.ActiveCfg = Debug|ARM64 19 | {6C2BED99-5A0A-42A2-AEBE-66717FA92232}.Debug|ARM64.Build.0 = Debug|ARM64 20 | {6C2BED99-5A0A-42A2-AEBE-66717FA92232}.Debug|x64.ActiveCfg = Debug|x64 21 | {6C2BED99-5A0A-42A2-AEBE-66717FA92232}.Debug|x64.Build.0 = Debug|x64 22 | {6C2BED99-5A0A-42A2-AEBE-66717FA92232}.Debug|x86.ActiveCfg = Debug|Win32 23 | {6C2BED99-5A0A-42A2-AEBE-66717FA92232}.Debug|x86.Build.0 = Debug|Win32 24 | {6C2BED99-5A0A-42A2-AEBE-66717FA92232}.Release|ARM64.ActiveCfg = Release|ARM64 25 | {6C2BED99-5A0A-42A2-AEBE-66717FA92232}.Release|ARM64.Build.0 = Release|ARM64 26 | {6C2BED99-5A0A-42A2-AEBE-66717FA92232}.Release|x64.ActiveCfg = Release|x64 27 | {6C2BED99-5A0A-42A2-AEBE-66717FA92232}.Release|x64.Build.0 = Release|x64 28 | {6C2BED99-5A0A-42A2-AEBE-66717FA92232}.Release|x86.ActiveCfg = Release|Win32 29 | {6C2BED99-5A0A-42A2-AEBE-66717FA92232}.Release|x86.Build.0 = Release|Win32 30 | EndGlobalSection 31 | GlobalSection(SolutionProperties) = preSolution 32 | HideSolutionNode = FALSE 33 | EndGlobalSection 34 | GlobalSection(ExtensibilityGlobals) = postSolution 35 | SolutionGuid = {7994FC0C-9D7C-4AD7-855B-C3525FD8709A} 36 | EndGlobalSection 37 | EndGlobal 38 | -------------------------------------------------------------------------------- /rename.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | type -P sed &>/dev/null || { echo "sed command not found. Aborting." >&2; exit 1; } 4 | type -P git &>/dev/null || { echo "git command not found. Aborting." >&2; exit 1; } 5 | 6 | if [ ! -n "$1" ]; then 7 | echo "You must provide the new project name (eg. 'my-project')" 8 | exit 1 9 | else 10 | NEW=$1 11 | fi 12 | 13 | if [[ $new =~ "_" ]]; then 14 | echo "A project names with underscore(s) is not suitable for AppVeyor" 15 | exit 1 16 | fi 17 | 18 | ORG=$(ls *.sln) 19 | ORG="${ORG%.*}" 20 | echo "Renaming '$ORG' to '$NEW'..." 21 | 22 | sed -b -i "s/$ORG/$NEW/g" $ORG.sln README.md .vs/*.vcxproj* src/*.c 23 | git mv $ORG.sln $NEW.sln 24 | git mv .vs/$ORG.vcxproj .vs/$NEW.vcxproj 25 | git mv .vs/$ORG.vcxproj.filters .vs/$NEW.vcxproj.filters 26 | git mv .vs/$ORG.vcxproj.user .vs/$NEW.vcxproj.user 27 | git mv src/$ORG.c src/$NEW.c 28 | # Delete existing tags 29 | git tag | xargs git tag -d 30 | 31 | echo "Do not forget to change FRIENDLY_NAME in AppVeyor.yml and update README.md." 32 | -------------------------------------------------------------------------------- /src/base-parallel.c: -------------------------------------------------------------------------------- 1 | /* 2 | * base-parallel - A base console application for multithreaded parallel processing 3 | * 4 | * Copyright © 2020 Pete Batard 5 | * 6 | * This program is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program. If not, see . 18 | */ 19 | 20 | #ifdef _DEBUG 21 | #define _CRTDBG_MAP_ALLOC 22 | #include 23 | #include 24 | #endif 25 | 26 | #define WIN32_LEAN_AND_MEAN 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | #include "msapi_utf8.h" 33 | 34 | #pragma warning(disable: 6258) // I know what I'm using TerminateThread for 35 | 36 | #define _STRINGIFY(x) #x 37 | #define STRINGIFY(x) _STRINGIFY(x) 38 | 39 | #ifndef APP_VERSION 40 | #define APP_VERSION_STR "[DEV]" 41 | #else 42 | #define APP_VERSION_STR STRINGIFY(APP_VERSION) 43 | #endif 44 | 45 | // Maximum amount of time we allow for a thread (ms). 46 | // Set it to INFINITE to wait forever 47 | #define WAIT_TIME 15000 48 | // If you don't want to use all the logical processors 49 | // You can define a number of "spare threads" here. 50 | #define SPARE_THREADS 0 51 | // Number of iterations for our dummy loop 52 | #define MAX_ITERATIONS 100 53 | 54 | // The following is used to indicate cancellation 55 | volatile BOOL cancel_requested = FALSE; 56 | 57 | // Global thread variables 58 | DWORD num_threads = 0; 59 | DWORD_PTR* thread_affinity = NULL; 60 | HANDLE *data_ready = NULL, *thread_ready = NULL; 61 | uint32_t* thread_data = NULL; 62 | 63 | static __inline char* appname(const char* path) 64 | { 65 | static char appname[128]; 66 | _splitpath_s(path, NULL, 0, NULL, 0, appname, sizeof(appname), NULL, 0); 67 | return appname; 68 | } 69 | 70 | static __inline uint8_t popcnt64(register uint64_t u) 71 | { 72 | u = (u & 0x5555555555555555) + ((u >> 1) & 0x5555555555555555); 73 | u = (u & 0x3333333333333333) + ((u >> 2) & 0x3333333333333333); 74 | u = (u & 0x0f0f0f0f0f0f0f0f) + ((u >> 4) & 0x0f0f0f0f0f0f0f0f); 75 | u = (u & 0x00ff00ff00ff00ff) + ((u >> 8) & 0x00ff00ff00ff00ff); 76 | u = (u & 0x0000ffff0000ffff) + ((u >> 16) & 0x0000ffff0000ffff); 77 | u = (u & 0x00000000ffffffff) + ((u >> 32) & 0x00000000ffffffff); 78 | return (uint8_t)u; 79 | } 80 | 81 | /* 82 | * This call (loosely) detects the number of logical processors on the 83 | * system, sets the number of threads accordingly and then affixes each 84 | * thread (affinity) to a specific logical processor. 85 | * Note: This doesn't support more than 64 logical processors... 86 | */ 87 | BOOL SetThreadAffinity(void) 88 | { 89 | DWORD i; 90 | DWORD_PTR affinity, dummy; 91 | 92 | num_threads = 0; 93 | if (!GetProcessAffinityMask(GetCurrentProcess(), &affinity, &dummy)) 94 | return FALSE; 95 | 96 | num_threads = popcnt64(affinity); 97 | if (num_threads <= SPARE_THREADS) 98 | return FALSE; 99 | num_threads -= SPARE_THREADS; 100 | 101 | thread_affinity = calloc((size_t)num_threads, sizeof(DWORD_PTR)); 102 | if (thread_affinity == NULL) { 103 | fprintf(stderr, "Could not alloc thread_affinity.\n"); 104 | return FALSE; 105 | } 106 | 107 | // Spread the affinity evenly (1 thread per logical processor) 108 | for (i = 0; i < num_threads; i++) { 109 | thread_affinity[i] |= affinity & (-1LL * affinity); 110 | affinity ^= affinity & (-1LL * affinity); 111 | } 112 | return TRUE; 113 | } 114 | 115 | // Individual thread for the task that is to be executed in parallel 116 | DWORD WINAPI ParallelTaskThread(void* param) 117 | { 118 | uint32_t i = (uint32_t)(uintptr_t)param; 119 | do { 120 | // Signal that we're ready to service requests 121 | if (!SetEvent(thread_ready[i])) { 122 | printf("Failed to signal readiness for thread #%02d\n", i); 123 | return 1; 124 | } 125 | 126 | // Wait for requests 127 | if (WaitForSingleObject(data_ready[i], WAIT_TIME) != WAIT_OBJECT_0) { 128 | printf("Failed to get data ready event for thread #%02d\n", i); 129 | return 1; 130 | } 131 | 132 | // Check for exit condition 133 | if (thread_data[i] == 0) { 134 | printf("Thread #%02d exiting\n", i); 135 | return 0; 136 | } 137 | 138 | // Process data 139 | printf("Thread #%02d received data %d\n", i, thread_data[i]); 140 | 141 | // Dummy loop 142 | for (uint32_t j = 0; (j < 25) && (!cancel_requested); j++) 143 | Sleep(100); 144 | 145 | } while (1); 146 | } 147 | 148 | DWORD WINAPI ControlThread(void* param) 149 | { 150 | DWORD r = 1; 151 | HANDLE* task_thread; 152 | 153 | if ((num_threads == 0) || (thread_affinity == NULL)) 154 | ExitThread(r); 155 | 156 | task_thread = calloc(num_threads, sizeof(HANDLE)); 157 | data_ready = calloc(num_threads, sizeof(HANDLE)); 158 | thread_ready = calloc(num_threads, sizeof(HANDLE)); 159 | thread_data = calloc(num_threads, sizeof(uint32_t)); 160 | if ((task_thread == NULL) || (data_ready == NULL) || (thread_ready == NULL) || (thread_data == NULL)) { 161 | fprintf(stderr, "Alloc error.\n"); 162 | goto out; 163 | } 164 | 165 | printf("Creating %d threads...\n", num_threads); 166 | 167 | for (uint32_t i = 0; i < num_threads; i++) { 168 | // NB: Can't use a single manual-reset event for data_ready as we 169 | // wouldn't be able to ensure the event is reset before the thread 170 | // gets into its next wait loop 171 | data_ready[i] = CreateEvent(NULL, FALSE, FALSE, NULL); 172 | thread_ready[i] = CreateEvent(NULL, FALSE, FALSE, NULL); 173 | if ((data_ready[i] == NULL) || (thread_ready[i] == NULL)) { 174 | printf("Unable to create checksum thread event\n"); 175 | goto out; 176 | } 177 | task_thread[i] = CreateThread(NULL, 0, ParallelTaskThread, (LPVOID)(uintptr_t)i, 0, NULL); 178 | if (task_thread[i] == NULL) { 179 | printf("Unable to start thread #%02d\n", i); 180 | goto out; 181 | } 182 | SetThreadPriority(task_thread[i], THREAD_PRIORITY_ABOVE_NORMAL); 183 | if (thread_affinity[i] != 0) 184 | SetThreadAffinityMask(task_thread[i], thread_affinity[i]); 185 | } 186 | 187 | for (uint32_t iteration = 1; (iteration <= MAX_ITERATIONS) && !cancel_requested; iteration++) { 188 | // Wait for threads to signal they are ready to process data 189 | DWORD i = WaitForMultipleObjects(num_threads, thread_ready, FALSE, WAIT_TIME); 190 | if (i >= WAIT_OBJECT_0 + num_threads) { 191 | printf("Failed to wait on individual task thread\n"); 192 | goto out; 193 | } 194 | // Populate some data 195 | thread_data[i] = iteration; 196 | // Signal the waiting threads 197 | if (!SetEvent(data_ready[i])) { 198 | printf("Could not signal thread #%02d\n", i); 199 | goto out; 200 | } 201 | } 202 | 203 | // If we just exited the call, give some time for the last thread to read the data 204 | Sleep(250); 205 | 206 | // Clear data and signal all the threads to exit 207 | memset(thread_data, 0, sizeof(uint32_t) * num_threads); 208 | for (DWORD i = 0; i < num_threads; i++) 209 | SetEvent(data_ready[i]); 210 | 211 | // Wait for thread exit 212 | if (WaitForMultipleObjects(num_threads, task_thread, TRUE, WAIT_TIME) == WAIT_OBJECT_0) { 213 | memset(task_thread, 0, sizeof(HANDLE) * num_threads); 214 | } else { 215 | printf("Threads did not finalize\n"); 216 | goto out; 217 | } 218 | r = 0; 219 | 220 | out: 221 | for (uint32_t i = 0; i < num_threads; i++) { 222 | if (task_thread[i] != NULL) 223 | TerminateThread(task_thread[i], 1); 224 | if (data_ready[i] != NULL) 225 | CloseHandle(data_ready[i]); 226 | if (thread_ready[i] != NULL) 227 | CloseHandle(thread_ready[i]); 228 | } 229 | free(data_ready); 230 | free(thread_ready); 231 | free(task_thread); 232 | free(thread_data); 233 | ExitThread(r); 234 | } 235 | 236 | // Important: If debugging this in Visual Studio, you will get an 237 | // exception when pressing Ctrl-C as the debugger is set to break 238 | // then. See https://stackoverflow.com/a/13207212/1069307. 239 | static BOOL WINAPI ConsoleHandler(DWORD dwCtrlType) 240 | { 241 | switch (dwCtrlType) { 242 | case CTRL_C_EVENT: 243 | printf("Ctrl-C received\n"); 244 | cancel_requested = TRUE; 245 | return TRUE; 246 | default: 247 | return FALSE; 248 | } 249 | } 250 | 251 | int main_utf8(int argc, char** argv) 252 | { 253 | int r = 1; 254 | HANDLE control_thread; 255 | 256 | fprintf(stderr, "%s %s © 2020 Pete Batard \n\n", appname(argv[0]), APP_VERSION_STR); 257 | fprintf(stderr, "This program is free software; you can redistribute it and/or modify it under \n"); 258 | fprintf(stderr, "the terms of the GNU General Public License as published by the Free Software \n"); 259 | fprintf(stderr, "Foundation; either version 3 of the License or any later version.\n\n"); 260 | fprintf(stderr, "Official project and latest downloads at: https://github.com/pbatard/base-parallel\n\n"); 261 | 262 | if (!SetThreadAffinity()) { 263 | fprintf(stderr, "Could not set thread_affinity.\n"); 264 | goto out; 265 | } 266 | 267 | control_thread = CreateThread(NULL, 0, ControlThread, NULL, 0, NULL); 268 | if (control_thread == NULL) { 269 | fprintf(stderr, "Could not create control thread.\n"); 270 | goto out; 271 | } 272 | SetThreadPriority(control_thread, THREAD_PRIORITY_ABOVE_NORMAL); 273 | SetConsoleCtrlHandler((PHANDLER_ROUTINE)ConsoleHandler, TRUE); 274 | if (WaitForSingleObject(control_thread, INFINITE) != WAIT_OBJECT_0) { 275 | fprintf(stderr, "Control thread exited unexpectedly.\n"); 276 | goto out; 277 | } 278 | r = 0; 279 | 280 | out: 281 | free(thread_affinity); 282 | return r; 283 | } 284 | 285 | int wmain(int argc, wchar_t** argv16) 286 | { 287 | SetConsoleOutputCP(CP_UTF8); 288 | char** argv = calloc(argc, sizeof(char*)); 289 | if (argv == NULL) 290 | return -1; 291 | for (int i = 0; i < argc; i++) 292 | argv[i] = wchar_to_utf8(argv16[i]); 293 | int r = main_utf8(argc, argv); 294 | for (int i = 0; i < argc; i++) 295 | free(argv[i]); 296 | free(argv); 297 | #ifdef _DEBUG 298 | _CrtDumpMemoryLeaks(); 299 | #endif 300 | return r; 301 | } 302 | -------------------------------------------------------------------------------- /src/msapi_utf8.h: -------------------------------------------------------------------------------- 1 | /* 2 | * MSAPI_UTF8: Common API calls using UTF-8 strings 3 | * Compensating for what Microsoft should have done a long long time ago. 4 | * Also see https://utf8everywhere.org 5 | * 6 | * Copyright © 2010-2020 Pete Batard 7 | * 8 | * This library is free software; you can redistribute it and/or 9 | * modify it under the terms of the GNU Lesser General Public 10 | * License as published by the Free Software Foundation; either 11 | * version 3 of the License, or (at your option) any later version. 12 | * 13 | * This library is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 | * Lesser General Public License for more details. 17 | * 18 | * You should have received a copy of the GNU Lesser General Public 19 | * License along with this library; if not, write to the Free Software 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 21 | */ 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | 40 | #pragma once 41 | #if defined(_MSC_VER) 42 | // disable VS2012 Code Analysis warnings that are intentional 43 | #pragma warning(disable: 6387) // Don't care about bad params 44 | #endif 45 | 46 | #ifdef __cplusplus 47 | extern "C" { 48 | #endif 49 | 50 | #define _LTEXT(txt) L##txt 51 | #define LTEXT(txt) _LTEXT(txt) 52 | 53 | #define wchar_to_utf8_no_alloc(wsrc, dest, dest_size) \ 54 | WideCharToMultiByte(CP_UTF8, 0, wsrc, -1, dest, dest_size, NULL, NULL) 55 | #define utf8_to_wchar_no_alloc(src, wdest, wdest_size) \ 56 | MultiByteToWideChar(CP_UTF8, 0, src, -1, wdest, wdest_size) 57 | #define Edit_ReplaceSelU(hCtrl, str) ((void)SendMessageLU(hCtrl, EM_REPLACESEL, (WPARAM)FALSE, str)) 58 | #define ComboBox_AddStringU(hCtrl, str) ((int)(DWORD)SendMessageLU(hCtrl, CB_ADDSTRING, (WPARAM)FALSE, str)) 59 | #define ComboBox_InsertStringU(hCtrl, index, str) ((int)(DWORD)SendMessageLU(hCtrl, CB_INSERTSTRING, (WPARAM)index, str)) 60 | #define ComboBox_GetTextU(hCtrl, str, max_str) GetWindowTextU(hCtrl, str, max_str) 61 | #define GetSaveFileNameU(p) GetOpenSaveFileNameU(p, TRUE) 62 | #define GetOpenFileNameU(p) GetOpenSaveFileNameU(p, FALSE) 63 | #define ListView_SetItemTextU(hwndLV,i,iSubItem_,pszText_) { LVITEMW _ms_wlvi; _ms_wlvi.iSubItem = iSubItem_; \ 64 | _ms_wlvi.pszText = utf8_to_wchar(pszText_); \ 65 | SNDMSG((hwndLV),LVM_SETITEMTEXTW,(WPARAM)(i),(LPARAM)&_ms_wlvi); sfree(_ms_wlvi.pszText);} 66 | 67 | // Never ever use isdigit() or isspace(), etc. on UTF-8 strings! 68 | // These calls take an int and char is signed so MS compilers will produce an assert error on anything that's > 0x80 69 | #define isasciiU(c) isascii((unsigned char)(c)) 70 | #define iscntrlU(c) iscntrl((unsigned char)(c)) 71 | #define isdigitU(c) isdigit((unsigned char)(c)) 72 | #define isspaceU(c) isspace((unsigned char)(c)) 73 | #define isxdigitU(c) isxdigit((unsigned char)(c)) 74 | // NB: other issomething() calls are not implemented as they may require multibyte UTF-8 sequences to be converted 75 | 76 | #define sfree(p) do {if (p != NULL) {free((void*)(p)); p = NULL;}} while(0) 77 | #define wconvert(p) wchar_t* w ## p = utf8_to_wchar(p) 78 | #define walloc(p, size) wchar_t* w ## p = (p == NULL)?NULL:(wchar_t*)calloc(size, sizeof(wchar_t)) 79 | #define wfree(p) sfree(w ## p) 80 | 81 | /* 82 | * Converts an UTF-16 string to UTF8 (allocate returned string) 83 | * Returns NULL on error 84 | */ 85 | static __inline char* wchar_to_utf8(const wchar_t* wstr) 86 | { 87 | int size = 0; 88 | char* str = NULL; 89 | 90 | // Convert the empty string too 91 | if (wstr[0] == 0) 92 | return (char*)calloc(1, 1); 93 | 94 | // Find out the size we need to allocate for our converted string 95 | size = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL); 96 | if (size <= 1) // An empty string would be size 1 97 | return NULL; 98 | 99 | if ((str = (char*)calloc(size, 1)) == NULL) 100 | return NULL; 101 | 102 | if (wchar_to_utf8_no_alloc(wstr, str, size) != size) { 103 | sfree(str); 104 | return NULL; 105 | } 106 | 107 | return str; 108 | } 109 | 110 | /* 111 | * Converts an UTF8 string to UTF-16 (allocate returned string) 112 | * Returns NULL on error 113 | */ 114 | static __inline wchar_t* utf8_to_wchar(const char* str) 115 | { 116 | int size = 0; 117 | wchar_t* wstr = NULL; 118 | 119 | if (str == NULL) 120 | return NULL; 121 | 122 | // Convert the empty string too 123 | if (str[0] == 0) 124 | return (wchar_t*)calloc(1, sizeof(wchar_t)); 125 | 126 | // Find out the size we need to allocate for our converted string 127 | size = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0); 128 | if (size <= 1) // An empty string would be size 1 129 | return NULL; 130 | 131 | if ((wstr = (wchar_t*)calloc(size, sizeof(wchar_t))) == NULL) 132 | return NULL; 133 | 134 | if (utf8_to_wchar_no_alloc(str, wstr, size) != size) { 135 | sfree(wstr); 136 | return NULL; 137 | } 138 | return wstr; 139 | } 140 | 141 | /* 142 | * Converts an non NUL-terminated UTF-16 string of length len to UTF8 (allocate returned string) 143 | * Returns NULL on error 144 | */ 145 | static __inline char* wchar_len_to_utf8(const wchar_t* wstr, int wlen) 146 | { 147 | int size = 0; 148 | char* str = NULL; 149 | 150 | // Find out the size we need to allocate for our converted string 151 | size = WideCharToMultiByte(CP_UTF8, 0, wstr, wlen, NULL, 0, NULL, NULL); 152 | if (size <= 1) // An empty string would be size 1 153 | return NULL; 154 | 155 | if ((str = (char*)calloc(size, 1)) == NULL) 156 | return NULL; 157 | 158 | if (WideCharToMultiByte(CP_UTF8, 0, wstr, wlen, str, size, NULL, NULL) != size) { 159 | sfree(str); 160 | return NULL; 161 | } 162 | 163 | return str; 164 | } 165 | 166 | static __inline DWORD FormatMessageU(DWORD dwFlags, LPCVOID lpSource, DWORD dwMessageId, 167 | DWORD dwLanguageId, char* lpBuffer, DWORD nSize, va_list *Arguments) 168 | { 169 | DWORD ret = 0, err = ERROR_INVALID_DATA; 170 | // coverity[returned_null] 171 | walloc(lpBuffer, nSize); 172 | ret = FormatMessageW(dwFlags, lpSource, dwMessageId, dwLanguageId, wlpBuffer, nSize, Arguments); 173 | err = GetLastError(); 174 | if ((ret != 0) && ((ret = wchar_to_utf8_no_alloc(wlpBuffer, lpBuffer, nSize)) == 0)) { 175 | err = GetLastError(); 176 | ret = 0; 177 | } 178 | wfree(lpBuffer); 179 | SetLastError(err); 180 | return ret; 181 | } 182 | 183 | // SendMessage, with LPARAM as UTF-8 string 184 | static __inline LRESULT SendMessageLU(HWND hWnd, UINT Msg, WPARAM wParam, const char* lParam) 185 | { 186 | LRESULT ret = FALSE; 187 | DWORD err = ERROR_INVALID_DATA; 188 | wconvert(lParam); 189 | ret = SendMessageW(hWnd, Msg, wParam, (LPARAM)wlParam); 190 | err = GetLastError(); 191 | wfree(lParam); 192 | SetLastError(err); 193 | return ret; 194 | } 195 | 196 | static __inline int DrawTextExU(HDC hDC, LPCSTR lpchText, int nCount, LPRECT lpRect, UINT uFormat, LPDRAWTEXTPARAMS lpDTParams) 197 | { 198 | int ret; 199 | DWORD err = ERROR_INVALID_DATA; 200 | wconvert(lpchText); 201 | ret = DrawTextExW(hDC, wlpchText, nCount, lpRect, uFormat, lpDTParams); 202 | err = GetLastError(); 203 | wfree(lpchText); 204 | SetLastError(err); 205 | return ret; 206 | } 207 | 208 | static __inline BOOL SHGetPathFromIDListU(LPCITEMIDLIST pidl, char* pszPath) 209 | { 210 | BOOL ret = FALSE; 211 | DWORD err = ERROR_INVALID_DATA; 212 | // coverity[returned_null] 213 | walloc(pszPath, MAX_PATH); 214 | ret = SHGetPathFromIDListW(pidl, wpszPath); 215 | err = GetLastError(); 216 | if ((ret) && (wchar_to_utf8_no_alloc(wpszPath, pszPath, MAX_PATH) == 0)) { 217 | err = GetLastError(); 218 | ret = FALSE; 219 | } 220 | wfree(pszPath); 221 | SetLastError(err); 222 | return ret; 223 | } 224 | 225 | static __inline HWND CreateWindowU(char* lpClassName, char* lpWindowName, 226 | DWORD dwStyle, int x, int y, int nWidth, int nHeight, HWND hWndParent, 227 | HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam) 228 | { 229 | HWND ret = NULL; 230 | DWORD err = ERROR_INVALID_DATA; 231 | wconvert(lpClassName); 232 | wconvert(lpWindowName); 233 | ret = CreateWindowW(wlpClassName, wlpWindowName, dwStyle, x, y, nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam); 234 | err = GetLastError(); 235 | wfree(lpClassName); 236 | wfree(lpWindowName); 237 | SetLastError(err); 238 | return ret; 239 | } 240 | 241 | static __inline HWND CreateWindowExU(DWORD dwExStyle, char* lpClassName, char* lpWindowName, 242 | DWORD dwStyle, int x, int y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, 243 | HINSTANCE hInstance, LPVOID lpParam) 244 | { 245 | HWND ret = NULL; 246 | DWORD err = ERROR_INVALID_DATA; 247 | wconvert(lpClassName); 248 | wconvert(lpWindowName); 249 | ret = CreateWindowExW(dwExStyle, wlpClassName, wlpWindowName, dwStyle, x, y, nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam); 250 | err = GetLastError(); 251 | wfree(lpClassName); 252 | wfree(lpWindowName); 253 | SetLastError(err); 254 | return ret; 255 | } 256 | 257 | static __inline int MessageBoxU(HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType) 258 | { 259 | int ret; 260 | DWORD err = ERROR_INVALID_DATA; 261 | wconvert(lpText); 262 | wconvert(lpCaption); 263 | ret = MessageBoxW(hWnd, wlpText, wlpCaption, uType); 264 | err = GetLastError(); 265 | wfree(lpText); 266 | wfree(lpCaption); 267 | SetLastError(err); 268 | return ret; 269 | } 270 | 271 | static __inline int MessageBoxExU(HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType, WORD wLanguageId) 272 | { 273 | int ret; 274 | DWORD err = ERROR_INVALID_DATA; 275 | wconvert(lpText); 276 | wconvert(lpCaption); 277 | ret = MessageBoxExW(hWnd, wlpText, wlpCaption, uType, wLanguageId); 278 | err = GetLastError(); 279 | wfree(lpText); 280 | wfree(lpCaption); 281 | SetLastError(err); 282 | return ret; 283 | } 284 | 285 | static __inline int LoadStringU(HINSTANCE hInstance, UINT uID, LPSTR lpBuffer, int nBufferMax) 286 | { 287 | int ret; 288 | DWORD err = ERROR_INVALID_DATA; 289 | if (nBufferMax == 0) { 290 | // read-only pointer to resource mode is not supported 291 | SetLastError(ERROR_INVALID_PARAMETER); 292 | return 0; 293 | } 294 | // coverity[returned_null] 295 | walloc(lpBuffer, nBufferMax); 296 | ret = LoadStringW(hInstance, uID, wlpBuffer, nBufferMax); 297 | err = GetLastError(); 298 | if ((ret > 0) && ((ret = wchar_to_utf8_no_alloc(wlpBuffer, lpBuffer, nBufferMax)) == 0)) { 299 | err = GetLastError(); 300 | } 301 | wfree(lpBuffer); 302 | SetLastError(err); 303 | return ret; 304 | } 305 | 306 | static __inline HMODULE LoadLibraryU(LPCSTR lpFileName) 307 | { 308 | HMODULE ret; 309 | DWORD err = ERROR_INVALID_DATA; 310 | wconvert(lpFileName); 311 | ret = LoadLibraryW(wlpFileName); 312 | err = GetLastError(); 313 | wfree(lpFileName); 314 | SetLastError(err); 315 | return ret; 316 | } 317 | 318 | static __inline int DrawTextU(HDC hDC, LPCSTR lpText, int nCount, LPRECT lpRect, UINT uFormat) 319 | { 320 | int ret; 321 | DWORD err = ERROR_INVALID_DATA; 322 | wconvert(lpText); 323 | ret = DrawTextW(hDC, wlpText, nCount, lpRect, uFormat); 324 | err = GetLastError(); 325 | wfree(lpText); 326 | SetLastError(err); 327 | return ret; 328 | } 329 | 330 | static __inline int GetWindowTextU(HWND hWnd, char* lpString, int nMaxCount) 331 | { 332 | int ret = 0; 333 | DWORD err = ERROR_INVALID_DATA; 334 | // Handle the empty string as GetWindowTextW() returns 0 then 335 | if ((lpString != NULL) && (nMaxCount > 0)) 336 | lpString[0] = 0; 337 | // coverity[returned_null] 338 | walloc(lpString, nMaxCount); 339 | ret = GetWindowTextW(hWnd, wlpString, nMaxCount); 340 | err = GetLastError(); 341 | // coverity[var_deref_model] 342 | if ( (ret != 0) && ((ret = wchar_to_utf8_no_alloc(wlpString, lpString, nMaxCount)) == 0) ) { 343 | err = GetLastError(); 344 | } 345 | wfree(lpString); 346 | SetLastError(err); 347 | return ret; 348 | } 349 | 350 | static __inline BOOL SetWindowTextU(HWND hWnd, const char* lpString) 351 | { 352 | BOOL ret = FALSE; 353 | DWORD err = ERROR_INVALID_DATA; 354 | wconvert(lpString); 355 | ret = SetWindowTextW(hWnd, wlpString); 356 | err = GetLastError(); 357 | wfree(lpString); 358 | SetLastError(err); 359 | return ret; 360 | } 361 | 362 | static __inline int GetWindowTextLengthU(HWND hWnd) 363 | { 364 | int ret = 0; 365 | DWORD err = ERROR_INVALID_DATA; 366 | wchar_t* wbuf = NULL; 367 | char* buf = NULL; 368 | 369 | ret = GetWindowTextLengthW(hWnd); 370 | err = GetLastError(); 371 | if (ret == 0) goto out; 372 | wbuf = (wchar_t* )calloc(ret, sizeof(wchar_t)); 373 | err = GetLastError(); 374 | if (wbuf == NULL) { 375 | err = ERROR_OUTOFMEMORY; ret = 0; goto out; 376 | } 377 | ret = GetWindowTextW(hWnd, wbuf, ret); 378 | err = GetLastError(); 379 | if (ret == 0) goto out; 380 | buf = wchar_to_utf8(wbuf); 381 | err = GetLastError(); 382 | if (buf == NULL) { 383 | err = ERROR_OUTOFMEMORY; ret = 0; goto out; 384 | } 385 | ret = (int)strlen(buf) + 2; // GetDlgItemText seems to add a character 386 | err = GetLastError(); 387 | out: 388 | sfree(wbuf); 389 | sfree(buf); 390 | SetLastError(err); 391 | return ret; 392 | } 393 | 394 | static __inline UINT GetDlgItemTextU(HWND hDlg, int nIDDlgItem, char* lpString, int nMaxCount) 395 | { 396 | UINT ret = 0; 397 | DWORD err = ERROR_INVALID_DATA; 398 | // coverity[returned_null] 399 | walloc(lpString, nMaxCount); 400 | ret = GetDlgItemTextW(hDlg, nIDDlgItem, wlpString, nMaxCount); 401 | err = GetLastError(); 402 | if ((ret != 0) && ((ret = wchar_to_utf8_no_alloc(wlpString, lpString, nMaxCount)) == 0)) { 403 | err = GetLastError(); 404 | } 405 | wfree(lpString); 406 | SetLastError(err); 407 | return ret; 408 | } 409 | 410 | static __inline BOOL SetDlgItemTextU(HWND hDlg, int nIDDlgItem, const char* lpString) 411 | { 412 | BOOL ret = FALSE; 413 | DWORD err = ERROR_INVALID_DATA; 414 | wconvert(lpString); 415 | ret = SetDlgItemTextW(hDlg, nIDDlgItem, wlpString); 416 | err = GetLastError(); 417 | wfree(lpString); 418 | SetLastError(err); 419 | return ret; 420 | } 421 | 422 | static __inline BOOL InsertMenuU(HMENU hMenu, UINT uPosition, UINT uFlags, UINT_PTR uIDNewItem, const char* lpNewItem) 423 | { 424 | BOOL ret = FALSE; 425 | DWORD err = ERROR_INVALID_DATA; 426 | wconvert(lpNewItem); 427 | ret = InsertMenuW(hMenu, uPosition, uFlags, uIDNewItem, wlpNewItem); 428 | err = GetLastError(); 429 | wfree(lpNewItem); 430 | SetLastError(err); 431 | return ret; 432 | } 433 | 434 | static __inline int ComboBox_GetLBTextU(HWND hCtrl, int index, char* lpString) 435 | { 436 | int size; 437 | DWORD err = ERROR_INVALID_DATA; 438 | wchar_t* wlpString; 439 | if (lpString == NULL) 440 | return CB_ERR; 441 | size = (int)SendMessageW(hCtrl, CB_GETLBTEXTLEN, (WPARAM)index, (LPARAM)0); 442 | if (size < 0) 443 | return size; 444 | wlpString = (wchar_t*)calloc((size_t)size + 1, sizeof(wchar_t)); 445 | size = (int)SendMessageW(hCtrl, CB_GETLBTEXT, (WPARAM)index, (LPARAM)wlpString); 446 | err = GetLastError(); 447 | if (size > 0) 448 | wchar_to_utf8_no_alloc(wlpString, lpString, size+1); 449 | wfree(lpString); 450 | SetLastError(err); 451 | return size; 452 | } 453 | 454 | static __inline DWORD CharUpperBuffU(char* lpString, DWORD len) 455 | { 456 | DWORD ret; 457 | wchar_t *wlpString = (wchar_t*)calloc(len, sizeof(wchar_t)); 458 | if (wlpString == NULL) 459 | return 0; 460 | utf8_to_wchar_no_alloc(lpString, wlpString, len); 461 | ret = CharUpperBuffW(wlpString, len); 462 | wchar_to_utf8_no_alloc(wlpString, lpString, len); 463 | free(wlpString); 464 | return ret; 465 | } 466 | 467 | static __inline HANDLE CreateFileU(const char* lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, 468 | LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, 469 | DWORD dwFlagsAndAttributes, HANDLE hTemplateFile) 470 | { 471 | HANDLE ret = INVALID_HANDLE_VALUE; 472 | DWORD err = ERROR_INVALID_DATA; 473 | wconvert(lpFileName); 474 | ret = CreateFileW(wlpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, 475 | dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile); 476 | err = GetLastError(); 477 | wfree(lpFileName); 478 | SetLastError(err); 479 | return ret; 480 | } 481 | 482 | static __inline BOOL CreateDirectoryU(const char* lpPathName, LPSECURITY_ATTRIBUTES lpSecurityAttributes) 483 | { 484 | BOOL ret = FALSE; 485 | DWORD err = ERROR_INVALID_DATA; 486 | wconvert(lpPathName); 487 | ret = CreateDirectoryW(wlpPathName, lpSecurityAttributes); 488 | err = GetLastError(); 489 | wfree(lpPathName); 490 | SetLastError(err); 491 | return ret; 492 | } 493 | 494 | static __inline BOOL CopyFileU(const char* lpExistingFileName, const char* lpNewFileName, BOOL bFailIfExists) 495 | { 496 | BOOL ret = FALSE; 497 | DWORD err = ERROR_INVALID_DATA; 498 | wconvert(lpExistingFileName); 499 | wconvert(lpNewFileName); 500 | ret = CopyFileW(wlpExistingFileName, wlpNewFileName, bFailIfExists); 501 | err = GetLastError(); 502 | wfree(lpExistingFileName); 503 | wfree(lpNewFileName); 504 | SetLastError(err); 505 | return ret; 506 | } 507 | 508 | static __inline BOOL DeleteFileU(const char* lpFileName) 509 | { 510 | BOOL ret = FALSE; 511 | DWORD err = ERROR_INVALID_DATA; 512 | wconvert(lpFileName); 513 | ret = DeleteFileW(wlpFileName); 514 | err = GetLastError(); 515 | wfree(lpFileName); 516 | SetLastError(err); 517 | return ret; 518 | } 519 | 520 | static __inline BOOL PathFileExistsU(char* szPath) 521 | { 522 | BOOL ret; 523 | wconvert(szPath); 524 | ret = PathFileExistsW(wszPath); 525 | wfree(szPath); 526 | return ret; 527 | } 528 | 529 | static __inline int PathGetDriveNumberU(char* lpPath) 530 | { 531 | int ret = 0; 532 | DWORD err = ERROR_INVALID_DATA; 533 | wconvert(lpPath); 534 | ret = PathGetDriveNumberW(wlpPath); 535 | err = GetLastError(); 536 | wfree(lpPath); 537 | SetLastError(err); 538 | return ret; 539 | } 540 | 541 | // This one is tricky since we can't blindly convert a 542 | // UTF-16 position to a UTF-8 one. So we do it manually. 543 | static __inline const char* PathFindFileNameU(const char* szPath) 544 | { 545 | size_t i; 546 | if (szPath == NULL) 547 | return NULL; 548 | for (i = strlen(szPath); i != 0; i--) { 549 | if ((szPath[i] == '/') || (szPath[i] == '\\')) { 550 | i++; 551 | break; 552 | } 553 | } 554 | return &szPath[i]; 555 | } 556 | 557 | // This function differs from regular GetTextExtentPoint in that it uses a zero terminated string 558 | static __inline BOOL GetTextExtentPointU(HDC hdc, const char* lpString, LPSIZE lpSize) 559 | { 560 | BOOL ret = FALSE; 561 | DWORD err = ERROR_INVALID_DATA; 562 | wconvert(lpString); 563 | if (wlpString == NULL) 564 | return FALSE; 565 | ret = GetTextExtentPoint32W(hdc, wlpString, (int)wcslen(wlpString), lpSize); 566 | err = GetLastError(); 567 | wfree(lpString); 568 | SetLastError(err); 569 | return ret; 570 | } 571 | 572 | static __inline DWORD GetCurrentDirectoryU(DWORD nBufferLength, char* lpBuffer) 573 | { 574 | DWORD ret = 0, err = ERROR_INVALID_DATA; 575 | // coverity[returned_null] 576 | walloc(lpBuffer, nBufferLength); 577 | ret = GetCurrentDirectoryW(nBufferLength, wlpBuffer); 578 | err = GetLastError(); 579 | if ((ret != 0) && ((ret = wchar_to_utf8_no_alloc(wlpBuffer, lpBuffer, nBufferLength)) == 0)) { 580 | err = GetLastError(); 581 | } 582 | wfree(lpBuffer); 583 | SetLastError(err); 584 | return ret; 585 | } 586 | 587 | static __inline UINT GetSystemDirectoryU(char* lpBuffer, UINT uSize) 588 | { 589 | UINT ret = 0, err = ERROR_INVALID_DATA; 590 | // coverity[returned_null] 591 | walloc(lpBuffer, uSize); 592 | ret = GetSystemDirectoryW(wlpBuffer, uSize); 593 | err = GetLastError(); 594 | if ((ret != 0) && ((ret = wchar_to_utf8_no_alloc(wlpBuffer, lpBuffer, uSize)) == 0)) { 595 | err = GetLastError(); 596 | } 597 | wfree(lpBuffer); 598 | SetLastError(err); 599 | return ret; 600 | } 601 | 602 | static __inline UINT GetSystemWindowsDirectoryU(char* lpBuffer, UINT uSize) 603 | { 604 | UINT ret = 0, err = ERROR_INVALID_DATA; 605 | // coverity[returned_null] 606 | walloc(lpBuffer, uSize); 607 | ret = GetSystemWindowsDirectoryW(wlpBuffer, uSize); 608 | err = GetLastError(); 609 | if ((ret != 0) && ((ret = wchar_to_utf8_no_alloc(wlpBuffer, lpBuffer, uSize)) == 0)) { 610 | err = GetLastError(); 611 | } 612 | wfree(lpBuffer); 613 | SetLastError(err); 614 | return ret; 615 | } 616 | 617 | static __inline DWORD GetTempPathU(DWORD nBufferLength, char* lpBuffer) 618 | { 619 | DWORD ret = 0, err = ERROR_INVALID_DATA; 620 | // coverity[returned_null] 621 | walloc(lpBuffer, nBufferLength); 622 | ret = GetTempPathW(nBufferLength, wlpBuffer); 623 | err = GetLastError(); 624 | if ((ret != 0) && ((ret = wchar_to_utf8_no_alloc(wlpBuffer, lpBuffer, nBufferLength)) == 0)) { 625 | err = GetLastError(); 626 | } 627 | wfree(lpBuffer); 628 | SetLastError(err); 629 | return ret; 630 | } 631 | 632 | static __inline DWORD GetTempFileNameU(char* lpPathName, char* lpPrefixString, UINT uUnique, char* lpTempFileName) 633 | { 634 | DWORD ret = 0, err = ERROR_INVALID_DATA; 635 | wconvert(lpPathName); 636 | wconvert(lpPrefixString); 637 | // coverity[returned_null] 638 | walloc(lpTempFileName, MAX_PATH); 639 | ret = GetTempFileNameW(wlpPathName, wlpPrefixString, uUnique, wlpTempFileName); 640 | err = GetLastError(); 641 | if ((ret != 0) && ((ret = wchar_to_utf8_no_alloc(wlpTempFileName, lpTempFileName, MAX_PATH)) == 0)) { 642 | err = GetLastError(); 643 | } 644 | wfree(lpTempFileName); 645 | wfree(lpPrefixString); 646 | wfree(lpPathName); 647 | SetLastError(err); 648 | return ret; 649 | } 650 | 651 | static __inline DWORD GetModuleFileNameU(HMODULE hModule, char* lpFilename, DWORD nSize) 652 | { 653 | DWORD ret = 0, err = ERROR_INVALID_DATA; 654 | // coverity[returned_null] 655 | walloc(lpFilename, nSize); 656 | ret = GetModuleFileNameW(hModule, wlpFilename, nSize); 657 | err = GetLastError(); 658 | if ((ret != 0) && ((ret = wchar_to_utf8_no_alloc(wlpFilename, lpFilename, nSize)) == 0)) { 659 | err = GetLastError(); 660 | } 661 | wfree(lpFilename); 662 | SetLastError(err); 663 | return ret; 664 | } 665 | 666 | static __inline DWORD GetModuleFileNameExU(HANDLE hProcess, HMODULE hModule, char* lpFilename, DWORD nSize) 667 | { 668 | DWORD ret = 0, err = ERROR_INVALID_DATA; 669 | // coverity[returned_null] 670 | walloc(lpFilename, nSize); 671 | ret = GetModuleFileNameExW(hProcess, hModule, wlpFilename, nSize); 672 | err = GetLastError(); 673 | if ((ret != 0) 674 | && ((ret = wchar_to_utf8_no_alloc(wlpFilename, lpFilename, nSize)) == 0)) { 675 | err = GetLastError(); 676 | } 677 | wfree(lpFilename); 678 | SetLastError(err); 679 | return ret; 680 | } 681 | 682 | static __inline DWORD GetFullPathNameU(const char* lpFileName, DWORD nBufferLength, char* lpBuffer, char** lpFilePart) 683 | { 684 | DWORD ret = 0, err = ERROR_INVALID_DATA; 685 | wchar_t* wlpFilePart; 686 | wconvert(lpFileName); 687 | // coverity[returned_null] 688 | walloc(lpBuffer, nBufferLength); 689 | 690 | // lpFilePart is not supported 691 | if (lpFilePart != NULL) goto out; 692 | 693 | ret = GetFullPathNameW(wlpFileName, nBufferLength, wlpBuffer, &wlpFilePart); 694 | err = GetLastError(); 695 | if ((ret != 0) && ((ret = wchar_to_utf8_no_alloc(wlpBuffer, lpBuffer, nBufferLength)) == 0)) { 696 | err = GetLastError(); 697 | } 698 | 699 | out: 700 | wfree(lpBuffer); 701 | wfree(lpFileName); 702 | SetLastError(err); 703 | return ret; 704 | } 705 | 706 | static __inline DWORD GetFileAttributesU(const char* lpFileName) 707 | { 708 | DWORD ret = 0xFFFFFFFF, err = ERROR_INVALID_DATA; 709 | wconvert(lpFileName); 710 | // Unlike Microsoft's version, ours doesn't fail if the string is quoted 711 | if ((wlpFileName[0] == L'"') && (wlpFileName[wcslen(wlpFileName) - 1] == L'"')) { 712 | wlpFileName[wcslen(wlpFileName) - 1] = 0; 713 | ret = GetFileAttributesW(&wlpFileName[1]); 714 | } else { 715 | ret = GetFileAttributesW(wlpFileName); 716 | } 717 | err = GetLastError(); 718 | wfree(lpFileName); 719 | SetLastError(err); 720 | return ret; 721 | } 722 | 723 | static __inline BOOL SetFileAttributesU(const char* lpFileName, DWORD dwFileAttributes) 724 | { 725 | BOOL ret = FALSE, err = ERROR_INVALID_DATA; 726 | wconvert(lpFileName); 727 | // Unlike Microsoft's version, ours doesn't fail if the string is quoted 728 | if ((wlpFileName[0] == L'"') && (wlpFileName[wcslen(wlpFileName) - 1] == L'"')) { 729 | wlpFileName[wcslen(wlpFileName) - 1] = 0; 730 | ret = SetFileAttributesW(&wlpFileName[1], dwFileAttributes); 731 | } else { 732 | ret = SetFileAttributesW(wlpFileName, dwFileAttributes); 733 | } 734 | err = GetLastError(); 735 | wfree(lpFileName); 736 | SetLastError(err); 737 | return ret; 738 | } 739 | 740 | static __inline int SHCreateDirectoryExU(HWND hwnd, const char* pszPath, SECURITY_ATTRIBUTES *psa) 741 | { 742 | int ret = ERROR_INVALID_DATA; 743 | DWORD err = ERROR_INVALID_DATA; 744 | wconvert(pszPath); 745 | ret = SHCreateDirectoryExW(hwnd, wpszPath, psa); 746 | err = GetLastError(); 747 | wfree(pszPath); 748 | SetLastError(err); 749 | return ret; 750 | } 751 | 752 | static __inline int SHDeleteDirectoryExU(HWND hwnd, const char* pszPath, FILEOP_FLAGS fFlags) 753 | { 754 | int ret; 755 | // String needs to be double NULL terminated, so we just use the length of the UTF-8 string 756 | // which is always expected to be larger than our UTF-16 one, and add 2 chars for good measure. 757 | size_t wpszPath_len = strlen(pszPath) + 2; 758 | // coverity[returned_null] 759 | walloc(pszPath, wpszPath_len); 760 | SHFILEOPSTRUCTW shfo = { hwnd, FO_DELETE, wpszPath, NULL, fFlags, FALSE, NULL, NULL }; 761 | utf8_to_wchar_no_alloc(pszPath, wpszPath, (int)wpszPath_len); 762 | // FOF_SILENT | FOF_NOERRORUI | FOF_NOCONFIRMATION, 763 | ret = SHFileOperationW(&shfo); 764 | wfree(pszPath); 765 | return ret; 766 | } 767 | 768 | static __inline BOOL ShellExecuteExU(SHELLEXECUTEINFOA* lpExecInfo) 769 | { 770 | BOOL ret = FALSE; 771 | DWORD err = ERROR_INVALID_DATA; 772 | SHELLEXECUTEINFOW wExecInfo; 773 | 774 | // Because we're lazy, we'll assume that the A and W structs inherently have the same size 775 | if (lpExecInfo->cbSize != sizeof(SHELLEXECUTEINFOW)) { 776 | SetLastError(ERROR_BAD_LENGTH); return FALSE; 777 | } 778 | memcpy(&wExecInfo, lpExecInfo, lpExecInfo->cbSize); 779 | wExecInfo.lpVerb = utf8_to_wchar(lpExecInfo->lpVerb); 780 | wExecInfo.lpFile = utf8_to_wchar(lpExecInfo->lpFile); 781 | wExecInfo.lpParameters = utf8_to_wchar(lpExecInfo->lpParameters); 782 | wExecInfo.lpDirectory = utf8_to_wchar(lpExecInfo->lpDirectory); 783 | if (wExecInfo.fMask & SEE_MASK_CLASSNAME) { 784 | wExecInfo.lpClass = utf8_to_wchar(lpExecInfo->lpClass); 785 | } else { 786 | wExecInfo.lpClass = NULL; 787 | } 788 | ret = ShellExecuteExW(&wExecInfo); 789 | err = GetLastError(); 790 | // Copy the returned values back 791 | lpExecInfo->hInstApp = wExecInfo.hInstApp; 792 | lpExecInfo->hProcess = wExecInfo.hProcess; 793 | sfree(wExecInfo.lpVerb); 794 | sfree(wExecInfo.lpFile); 795 | sfree(wExecInfo.lpParameters); 796 | sfree(wExecInfo.lpDirectory); 797 | sfree(wExecInfo.lpClass); 798 | SetLastError(err); 799 | return ret; 800 | } 801 | 802 | // Doesn't support LPSTARTUPINFOEX struct 803 | static __inline BOOL CreateProcessU(const char* lpApplicationName, const char* lpCommandLine, LPSECURITY_ATTRIBUTES lpProcessAttributes, 804 | LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles, DWORD dwCreationFlags, 805 | LPVOID lpEnvironment, const char* lpCurrentDirectory, LPSTARTUPINFOA lpStartupInfo, 806 | LPPROCESS_INFORMATION lpProcessInformation) 807 | { 808 | BOOL ret = FALSE; 809 | DWORD err = ERROR_INVALID_DATA; 810 | STARTUPINFOW wStartupInfo; 811 | wconvert(lpApplicationName); 812 | wconvert(lpCommandLine); 813 | wconvert(lpCurrentDirectory); 814 | 815 | // Because we're lazy, we'll assume that the A and W structs inherently have the same size 816 | // Also prevents the use of STARTUPINFOEX 817 | if (lpStartupInfo->cb != sizeof(STARTUPINFOW)) { 818 | err = ERROR_BAD_LENGTH; goto out; 819 | } 820 | memcpy(&wStartupInfo, lpStartupInfo, lpStartupInfo->cb); 821 | wStartupInfo.lpDesktop = utf8_to_wchar(lpStartupInfo->lpDesktop); 822 | wStartupInfo.lpTitle = utf8_to_wchar(lpStartupInfo->lpTitle); 823 | ret = CreateProcessW(wlpApplicationName, wlpCommandLine, lpProcessAttributes, lpThreadAttributes, bInheritHandles, 824 | dwCreationFlags, lpEnvironment, wlpCurrentDirectory, &wStartupInfo, lpProcessInformation); 825 | err = GetLastError(); 826 | sfree(wStartupInfo.lpDesktop); 827 | sfree(wStartupInfo.lpTitle); 828 | out: 829 | wfree(lpApplicationName); 830 | wfree(lpCommandLine); 831 | wfree(lpCurrentDirectory); 832 | SetLastError(err); 833 | return ret; 834 | } 835 | 836 | // NOTE: when used, nFileOffset & nFileExtension MUST be provided 837 | // in number of Unicode characters, NOT number of UTF-8 bytes 838 | static __inline BOOL WINAPI GetOpenSaveFileNameU(LPOPENFILENAMEA lpofn, BOOL save) 839 | { 840 | BOOL ret = FALSE; 841 | DWORD err = ERROR_INVALID_DATA; 842 | size_t i, len; 843 | OPENFILENAMEW wofn; 844 | memset(&wofn, 0, sizeof(wofn)); 845 | wofn.lStructSize = sizeof(wofn); 846 | wofn.hwndOwner = lpofn->hwndOwner; 847 | wofn.hInstance = lpofn->hInstance; 848 | 849 | // No support for custom filters 850 | if (lpofn->lpstrCustomFilter != NULL) goto out; 851 | 852 | // Count on Microsoft to use an moronic scheme for filters 853 | // that relies on NULL separators and double NULL terminators 854 | if (lpofn->lpstrFilter != NULL) { 855 | // Replace the NULLs by something that can be converted 856 | for (i=0; ; i++) { 857 | if (lpofn->lpstrFilter[i] == 0) { 858 | ((char*)lpofn->lpstrFilter)[i] = '\r'; 859 | if (lpofn->lpstrFilter[i+1] == 0) { 860 | break; 861 | } 862 | } 863 | } 864 | wofn.lpstrFilter = utf8_to_wchar(lpofn->lpstrFilter); 865 | // And revert 866 | len = wcslen(wofn.lpstrFilter); // don't use in the loop as it would be reevaluated 867 | for (i=0; ilpstrFilter); 873 | for (i=0; ilpstrFilter[i] == '\r') { 875 | ((char*)lpofn->lpstrFilter)[i] = 0; 876 | } 877 | } 878 | } else { 879 | wofn.lpstrFilter = NULL; 880 | } 881 | wofn.nMaxCustFilter = lpofn->nMaxCustFilter; 882 | wofn.nFilterIndex = lpofn->nFilterIndex; 883 | wofn.lpstrFile = (LPWSTR)calloc(lpofn->nMaxFile, sizeof(wchar_t)); 884 | utf8_to_wchar_no_alloc(lpofn->lpstrFile, wofn.lpstrFile, lpofn->nMaxFile); 885 | wofn.nMaxFile = lpofn->nMaxFile; 886 | wofn.lpstrFileTitle = (LPWSTR)calloc(lpofn->nMaxFileTitle, sizeof(wchar_t)); 887 | utf8_to_wchar_no_alloc(lpofn->lpstrFileTitle, wofn.lpstrFileTitle, lpofn->nMaxFileTitle); 888 | wofn.nMaxFileTitle = lpofn->nMaxFileTitle; 889 | wofn.lpstrInitialDir = utf8_to_wchar(lpofn->lpstrInitialDir); 890 | wofn.lpstrTitle = utf8_to_wchar(lpofn->lpstrTitle); 891 | wofn.Flags = lpofn->Flags; 892 | wofn.nFileOffset = lpofn->nFileOffset; 893 | wofn.nFileExtension = lpofn->nFileExtension; 894 | wofn.lpstrDefExt = utf8_to_wchar(lpofn->lpstrDefExt); 895 | wofn.lCustData = lpofn->lCustData; 896 | wofn.lpfnHook = lpofn->lpfnHook; 897 | wofn.lpTemplateName = utf8_to_wchar(lpofn->lpTemplateName); 898 | wofn.pvReserved = lpofn->pvReserved; 899 | wofn.dwReserved = lpofn->dwReserved; 900 | wofn.FlagsEx = lpofn->FlagsEx; 901 | 902 | if (save) { 903 | ret = GetSaveFileNameW(&wofn); 904 | } else { 905 | ret = GetOpenFileNameW(&wofn); 906 | } 907 | err = GetLastError(); 908 | if ( (ret) 909 | && ( (wchar_to_utf8_no_alloc(wofn.lpstrFile, lpofn->lpstrFile, lpofn->nMaxFile) == 0) 910 | || (wchar_to_utf8_no_alloc(wofn.lpstrFileTitle, lpofn->lpstrFileTitle, lpofn->nMaxFileTitle) == 0) ) ) { 911 | err = GetLastError(); 912 | ret = FALSE; 913 | } 914 | out: 915 | sfree(wofn.lpstrDefExt); 916 | sfree(wofn.lpstrFile); 917 | sfree(wofn.lpstrFileTitle); 918 | sfree(wofn.lpstrFilter); 919 | sfree(wofn.lpstrInitialDir); 920 | sfree(wofn.lpstrTitle); 921 | sfree(wofn.lpTemplateName); 922 | SetLastError(err); 923 | return ret; 924 | } 925 | 926 | extern BOOL WINAPI UpdateDriverForPlugAndPlayDevicesW(HWND hwndParent, LPCWSTR HardwareId, 927 | LPCWSTR FullInfPath, DWORD InstallFlags, PBOOL bRebootRequired); 928 | 929 | static __inline BOOL UpdateDriverForPlugAndPlayDevicesU(HWND hwndParent, const char* HardwareId, const char* FullInfPath, 930 | DWORD InstallFlags, PBOOL bRebootRequired) 931 | { 932 | BOOL ret = FALSE; 933 | DWORD err = ERROR_INVALID_DATA; 934 | wconvert(HardwareId); 935 | wconvert(FullInfPath); 936 | ret = UpdateDriverForPlugAndPlayDevicesW(hwndParent, wHardwareId, wFullInfPath, InstallFlags, bRebootRequired); 937 | err = GetLastError(); 938 | wfree(HardwareId); 939 | wfree(FullInfPath); 940 | SetLastError(err); 941 | return ret; 942 | } 943 | 944 | static __inline BOOL SetupCopyOEMInfU(const char* SourceInfFileName, const char* OEMSourceMediaLocation, DWORD OEMSourceMediaType, 945 | DWORD CopyStyle, char* DestinationInfFileName, DWORD DestinationInfFileNameSize, 946 | PDWORD RequiredSize, PTSTR DestinationInfFileNameComponent) 947 | { 948 | BOOL ret = FALSE; 949 | DWORD err = ERROR_INVALID_DATA; 950 | wconvert(SourceInfFileName); 951 | wconvert(OEMSourceMediaLocation); 952 | // coverity[returned_null] 953 | walloc(DestinationInfFileName, DestinationInfFileNameSize); 954 | 955 | // DestinationInfFileNameComponent is not supported 956 | if (DestinationInfFileNameComponent != NULL) goto out; 957 | 958 | ret = SetupCopyOEMInfW(wSourceInfFileName, wOEMSourceMediaLocation, OEMSourceMediaType, CopyStyle, 959 | wDestinationInfFileName, DestinationInfFileNameSize, RequiredSize, NULL); 960 | err = GetLastError(); 961 | if ((ret != FALSE) && ((ret = wchar_to_utf8_no_alloc(wDestinationInfFileName, DestinationInfFileName, DestinationInfFileNameSize)) == 0)) { 962 | err = GetLastError(); 963 | } 964 | out: 965 | wfree(SourceInfFileName); 966 | wfree(OEMSourceMediaLocation); 967 | wfree(DestinationInfFileName); 968 | SetLastError(err); 969 | return ret; 970 | } 971 | 972 | static __inline int _chdirU(const char *dirname) 973 | { 974 | int ret; 975 | wconvert(dirname); 976 | ret = _wchdir(wdirname); 977 | wfree(dirname); 978 | return ret; 979 | } 980 | 981 | #if defined(_WIN32_WINNT) && (_WIN32_WINNT <= 0x501) 982 | static __inline FILE* fopenU(const char* filename, const char* mode) 983 | { 984 | FILE* ret = NULL; 985 | wconvert(filename); 986 | wconvert(mode); 987 | ret = _wfopen(wfilename, wmode); 988 | wfree(filename); 989 | wfree(mode); 990 | return ret; 991 | } 992 | 993 | static __inline int _openU(const char *filename, int oflag, int pmode) 994 | { 995 | int ret = -1; 996 | wconvert(filename); 997 | ret = _wopen(wfilename, oflag, pmode); 998 | wfree(filename); 999 | return ret; 1000 | } 1001 | #else 1002 | static __inline FILE* fopenU(const char* filename, const char* mode) 1003 | { 1004 | FILE* ret = NULL; 1005 | wconvert(filename); 1006 | wconvert(mode); 1007 | _wfopen_s(&ret, wfilename, wmode); 1008 | wfree(filename); 1009 | wfree(mode); 1010 | return ret; 1011 | } 1012 | 1013 | static __inline int _openU(const char *filename, int oflag , int pmode) 1014 | { 1015 | int ret = -1; 1016 | int shflag = _SH_DENYNO; 1017 | wconvert(filename); 1018 | // Try to match the share flag to the oflag 1019 | if ((oflag & 0x03) == _O_RDONLY) 1020 | shflag = _SH_DENYWR; 1021 | else if ((oflag & 0x03) == _O_WRONLY) 1022 | shflag = _SH_DENYRD; 1023 | _wsopen_s(&ret, wfilename, oflag, shflag, pmode); 1024 | wfree(filename); 1025 | return ret; 1026 | } 1027 | #endif 1028 | 1029 | static __inline int _unlinkU(const char* path) 1030 | { 1031 | int ret; 1032 | wconvert(path); 1033 | ret = _wunlink(wpath); 1034 | wfree(path); 1035 | return ret; 1036 | } 1037 | 1038 | static __inline int _stat64U(const char *path, struct __stat64 *buffer) 1039 | { 1040 | int ret; 1041 | wconvert(path); 1042 | ret = _wstat64(wpath, buffer); 1043 | wfree(path); 1044 | return ret; 1045 | } 1046 | 1047 | static __inline int _accessU(const char* path, int mode) 1048 | { 1049 | int ret; 1050 | wconvert(path); 1051 | ret = _waccess(wpath, mode); 1052 | wfree(path); 1053 | return ret; 1054 | } 1055 | 1056 | static __inline const char* _filenameU(const char* path) 1057 | { 1058 | int i; 1059 | if (path == NULL) 1060 | return NULL; 1061 | for (i = (int)strlen(path) - 1; i >= 0; i--) 1062 | if ((path[i] == '/') || (path[i] == '\\')) 1063 | return &path[i + 1]; 1064 | return path; 1065 | } 1066 | 1067 | // returned UTF-8 string must be freed 1068 | static __inline char* getenvU(const char* varname) 1069 | { 1070 | wconvert(varname); 1071 | char* ret = NULL; 1072 | wchar_t* wbuf = NULL; 1073 | // _wgetenv() is *BROKEN* in MS compilers => use GetEnvironmentVariableW() 1074 | DWORD dwSize = GetEnvironmentVariableW(wvarname, wbuf, 0); 1075 | wbuf = (wchar_t*)calloc(dwSize, sizeof(wchar_t)); 1076 | if (wbuf == NULL) { 1077 | wfree(varname); 1078 | return NULL; 1079 | } 1080 | dwSize = GetEnvironmentVariableW(wvarname, wbuf, dwSize); 1081 | if (dwSize != 0) 1082 | ret = wchar_to_utf8(wbuf); 1083 | free(wbuf); 1084 | wfree(varname); 1085 | return ret; 1086 | } 1087 | 1088 | static __inline int _mkdirU(const char* dirname) 1089 | { 1090 | wconvert(dirname); 1091 | int ret; 1092 | ret = _wmkdir(wdirname); 1093 | wfree(dirname); 1094 | return ret; 1095 | } 1096 | 1097 | // This version of _mkdirU creates all needed directories along the way 1098 | static __inline int _mkdirExU(const char* dirname) 1099 | { 1100 | int ret = -1, trailing_slash = -1; 1101 | size_t i, len; 1102 | wconvert(dirname); 1103 | len = wcslen(wdirname); 1104 | while (trailing_slash && (len > 0)) { 1105 | if ((wdirname[len - 1] == '\\') || (wdirname[len - 1] == '/')) 1106 | wdirname[--len] = 0; 1107 | else 1108 | trailing_slash = 0; 1109 | } 1110 | for (i = 0; i < len; i++) 1111 | if ((wdirname[i] == '\\') || (wdirname[i] == '/')) 1112 | wdirname[i] = 0; 1113 | for (i = 0; i < len; ) { 1114 | if ((_wmkdir(wdirname) < 0) && (errno != EEXIST) && (errno != EACCES)) 1115 | goto out; 1116 | i = wcslen(wdirname); 1117 | wdirname[i] = '\\'; 1118 | } 1119 | ret = 0; 1120 | out: 1121 | wfree(dirname); 1122 | return ret; 1123 | } 1124 | 1125 | static __inline int _rmdirU(const char* dirname) 1126 | { 1127 | wconvert(dirname); 1128 | int ret; 1129 | ret = _wrmdir(wdirname); 1130 | wfree(dirname); 1131 | return ret; 1132 | } 1133 | 1134 | static __inline BOOL MoveFileU(const char* lpExistingFileName, const char* lpNewFileName) 1135 | { 1136 | wconvert(lpExistingFileName); 1137 | wconvert(lpNewFileName); 1138 | BOOL ret = MoveFileW(wlpExistingFileName, wlpNewFileName); 1139 | wfree(lpNewFileName); 1140 | wfree(lpExistingFileName); 1141 | return ret; 1142 | } 1143 | 1144 | // The following expects PropertyBuffer to contain a single Unicode string 1145 | static __inline BOOL SetupDiGetDeviceRegistryPropertyU(HDEVINFO DeviceInfoSet, PSP_DEVINFO_DATA DeviceInfoData, 1146 | DWORD Property, PDWORD PropertyRegDataType, PBYTE PropertyBuffer, DWORD PropertyBufferSize, PDWORD RequiredSize) 1147 | { 1148 | BOOL ret = FALSE; 1149 | DWORD err = ERROR_INVALID_DATA; 1150 | // coverity[returned_null] 1151 | walloc(PropertyBuffer, PropertyBufferSize); 1152 | 1153 | ret = SetupDiGetDeviceRegistryPropertyW(DeviceInfoSet, DeviceInfoData, Property, 1154 | PropertyRegDataType, (PBYTE)wPropertyBuffer, PropertyBufferSize, RequiredSize); 1155 | err = GetLastError(); 1156 | if ((ret != 0) && (wchar_to_utf8_no_alloc(wPropertyBuffer, 1157 | (char*)(uintptr_t)PropertyBuffer, PropertyBufferSize) == 0)) { 1158 | err = GetLastError(); 1159 | ret = FALSE; 1160 | } 1161 | wfree(PropertyBuffer); 1162 | SetLastError(err); 1163 | return ret; 1164 | } 1165 | 1166 | static __inline BOOL GetVolumeInformationU(LPCSTR lpRootPathName, LPSTR lpVolumeNameBuffer, 1167 | DWORD nVolumeNameSize, LPDWORD lpVolumeSerialNumber, LPDWORD lpMaximumComponentLength, 1168 | LPDWORD lpFileSystemFlags, LPSTR lpFileSystemNameBuffer, DWORD nFileSystemNameSize) 1169 | { 1170 | BOOL ret = FALSE; 1171 | DWORD err = ERROR_INVALID_DATA; 1172 | wconvert(lpRootPathName); 1173 | // coverity[returned_null] 1174 | walloc(lpVolumeNameBuffer, nVolumeNameSize); 1175 | // coverity[returned_null] 1176 | walloc(lpFileSystemNameBuffer, nFileSystemNameSize); 1177 | 1178 | ret = GetVolumeInformationW(wlpRootPathName, wlpVolumeNameBuffer, nVolumeNameSize, 1179 | lpVolumeSerialNumber, lpMaximumComponentLength, lpFileSystemFlags, 1180 | wlpFileSystemNameBuffer, nFileSystemNameSize); 1181 | err = GetLastError(); 1182 | if (ret) { 1183 | if ( ((lpVolumeNameBuffer != NULL) && (wchar_to_utf8_no_alloc(wlpVolumeNameBuffer, 1184 | lpVolumeNameBuffer, nVolumeNameSize) == 0)) 1185 | || ((lpFileSystemNameBuffer != NULL) && (wchar_to_utf8_no_alloc(wlpFileSystemNameBuffer, 1186 | lpFileSystemNameBuffer, nFileSystemNameSize) == 0)) ) { 1187 | err = GetLastError(); 1188 | ret = FALSE; 1189 | } 1190 | } 1191 | wfree(lpVolumeNameBuffer); 1192 | wfree(lpFileSystemNameBuffer); 1193 | wfree(lpRootPathName); 1194 | SetLastError(err); 1195 | return ret; 1196 | } 1197 | 1198 | static __inline DWORD MapFileAndCheckSumU(const char* Filename, PDWORD HeaderSum, PDWORD CheckSum) 1199 | { 1200 | DWORD ret; 1201 | wconvert(Filename); 1202 | ret = MapFileAndCheckSumW(wFilename, HeaderSum, CheckSum); 1203 | wfree(Filename); 1204 | return ret; 1205 | } 1206 | 1207 | #ifdef __cplusplus 1208 | } 1209 | #endif 1210 | --------------------------------------------------------------------------------