zxing_cpp 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (332) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +13 -0
  3. data/.gitmodules +3 -0
  4. data/CHANGELOG.rdoc +6 -0
  5. data/Gemfile +3 -0
  6. data/Manifest.txt +331 -0
  7. data/README.rdoc +138 -0
  8. data/Rakefile +28 -0
  9. data/bin/zxd +87 -0
  10. data/bin/zxe +53 -0
  11. data/ext/zxing/extconf.rb +27 -0
  12. data/ext/zxing/zxing-cpp/.gitignore +4 -0
  13. data/ext/zxing/zxing-cpp/AUTHORS +115 -0
  14. data/ext/zxing/zxing-cpp/CMakeLists.txt +84 -0
  15. data/ext/zxing/zxing-cpp/COPYING +201 -0
  16. data/ext/zxing/zxing-cpp/NOTICE +65 -0
  17. data/ext/zxing/zxing-cpp/README.md +50 -0
  18. data/ext/zxing/zxing-cpp/cli/src/ImageReaderSource.cpp +112 -0
  19. data/ext/zxing/zxing-cpp/cli/src/ImageReaderSource.h +40 -0
  20. data/ext/zxing/zxing-cpp/cli/src/jpgd.cpp +3174 -0
  21. data/ext/zxing/zxing-cpp/cli/src/jpgd.h +319 -0
  22. data/ext/zxing/zxing-cpp/cli/src/lodepng.cpp +6261 -0
  23. data/ext/zxing/zxing-cpp/cli/src/lodepng.h +1695 -0
  24. data/ext/zxing/zxing-cpp/cli/src/main.cpp +297 -0
  25. data/ext/zxing/zxing-cpp/cmake/FindCPPUNIT.cmake +54 -0
  26. data/ext/zxing/zxing-cpp/cmake/FindIconv.cmake +57 -0
  27. data/ext/zxing/zxing-cpp/core/src/bigint/.gitignore +6 -0
  28. data/ext/zxing/zxing-cpp/core/src/bigint/BigInteger.cc +405 -0
  29. data/ext/zxing/zxing-cpp/core/src/bigint/BigInteger.hh +215 -0
  30. data/ext/zxing/zxing-cpp/core/src/bigint/BigIntegerAlgorithms.cc +70 -0
  31. data/ext/zxing/zxing-cpp/core/src/bigint/BigIntegerAlgorithms.hh +25 -0
  32. data/ext/zxing/zxing-cpp/core/src/bigint/BigIntegerLibrary.hh +8 -0
  33. data/ext/zxing/zxing-cpp/core/src/bigint/BigIntegerUtils.cc +50 -0
  34. data/ext/zxing/zxing-cpp/core/src/bigint/BigIntegerUtils.hh +72 -0
  35. data/ext/zxing/zxing-cpp/core/src/bigint/BigUnsigned.cc +697 -0
  36. data/ext/zxing/zxing-cpp/core/src/bigint/BigUnsigned.hh +418 -0
  37. data/ext/zxing/zxing-cpp/core/src/bigint/BigUnsignedInABase.cc +125 -0
  38. data/ext/zxing/zxing-cpp/core/src/bigint/BigUnsignedInABase.hh +122 -0
  39. data/ext/zxing/zxing-cpp/core/src/bigint/ChangeLog +146 -0
  40. data/ext/zxing/zxing-cpp/core/src/bigint/Makefile +73 -0
  41. data/ext/zxing/zxing-cpp/core/src/bigint/NumberlikeArray.hh +177 -0
  42. data/ext/zxing/zxing-cpp/core/src/bigint/README +71 -0
  43. data/ext/zxing/zxing-cpp/core/src/win32/zxing/iconv.h +14 -0
  44. data/ext/zxing/zxing-cpp/core/src/win32/zxing/stdint.h +247 -0
  45. data/ext/zxing/zxing-cpp/core/src/win32/zxing/win_iconv.c +2035 -0
  46. data/ext/zxing/zxing-cpp/core/src/zxing/BarcodeFormat.cpp +40 -0
  47. data/ext/zxing/zxing-cpp/core/src/zxing/BarcodeFormat.h +60 -0
  48. data/ext/zxing/zxing-cpp/core/src/zxing/Binarizer.cpp +45 -0
  49. data/ext/zxing/zxing-cpp/core/src/zxing/Binarizer.h +50 -0
  50. data/ext/zxing/zxing-cpp/core/src/zxing/BinaryBitmap.cpp +70 -0
  51. data/ext/zxing/zxing-cpp/core/src/zxing/BinaryBitmap.h +56 -0
  52. data/ext/zxing/zxing-cpp/core/src/zxing/ChecksumException.cpp +28 -0
  53. data/ext/zxing/zxing-cpp/core/src/zxing/ChecksumException.h +34 -0
  54. data/ext/zxing/zxing-cpp/core/src/zxing/DecodeHints.cpp +142 -0
  55. data/ext/zxing/zxing-cpp/core/src/zxing/DecodeHints.h +85 -0
  56. data/ext/zxing/zxing-cpp/core/src/zxing/Exception.cpp +43 -0
  57. data/ext/zxing/zxing-cpp/core/src/zxing/Exception.h +51 -0
  58. data/ext/zxing/zxing-cpp/core/src/zxing/FormatException.cpp +41 -0
  59. data/ext/zxing/zxing-cpp/core/src/zxing/FormatException.h +37 -0
  60. data/ext/zxing/zxing-cpp/core/src/zxing/IllegalStateException.h +35 -0
  61. data/ext/zxing/zxing-cpp/core/src/zxing/InvertedLuminanceSource.cpp +68 -0
  62. data/ext/zxing/zxing-cpp/core/src/zxing/InvertedLuminanceSource.h +48 -0
  63. data/ext/zxing/zxing-cpp/core/src/zxing/LuminanceSource.cpp +86 -0
  64. data/ext/zxing/zxing-cpp/core/src/zxing/LuminanceSource.h +59 -0
  65. data/ext/zxing/zxing-cpp/core/src/zxing/MultiFormatReader.cpp +124 -0
  66. data/ext/zxing/zxing-cpp/core/src/zxing/MultiFormatReader.h +48 -0
  67. data/ext/zxing/zxing-cpp/core/src/zxing/NotFoundException.h +35 -0
  68. data/ext/zxing/zxing-cpp/core/src/zxing/Reader.cpp +31 -0
  69. data/ext/zxing/zxing-cpp/core/src/zxing/Reader.h +40 -0
  70. data/ext/zxing/zxing-cpp/core/src/zxing/ReaderException.h +37 -0
  71. data/ext/zxing/zxing-cpp/core/src/zxing/Result.cpp +61 -0
  72. data/ext/zxing/zxing-cpp/core/src/zxing/Result.h +55 -0
  73. data/ext/zxing/zxing-cpp/core/src/zxing/ResultIO.cpp +34 -0
  74. data/ext/zxing/zxing-cpp/core/src/zxing/ResultPoint.cpp +108 -0
  75. data/ext/zxing/zxing-cpp/core/src/zxing/ResultPoint.h +55 -0
  76. data/ext/zxing/zxing-cpp/core/src/zxing/ResultPointCallback.cpp +26 -0
  77. data/ext/zxing/zxing-cpp/core/src/zxing/ResultPointCallback.h +39 -0
  78. data/ext/zxing/zxing-cpp/core/src/zxing/ZXing.h +133 -0
  79. data/ext/zxing/zxing-cpp/core/src/zxing/aztec/AztecDetectorResult.cpp +54 -0
  80. data/ext/zxing/zxing-cpp/core/src/zxing/aztec/AztecDetectorResult.h +48 -0
  81. data/ext/zxing/zxing-cpp/core/src/zxing/aztec/AztecReader.cpp +68 -0
  82. data/ext/zxing/zxing-cpp/core/src/zxing/aztec/AztecReader.h +49 -0
  83. data/ext/zxing/zxing-cpp/core/src/zxing/aztec/decoder/Decoder.cpp +489 -0
  84. data/ext/zxing/zxing-cpp/core/src/zxing/aztec/decoder/Decoder.h +69 -0
  85. data/ext/zxing/zxing-cpp/core/src/zxing/aztec/detector/Detector.cpp +548 -0
  86. data/ext/zxing/zxing-cpp/core/src/zxing/aztec/detector/Detector.h +92 -0
  87. data/ext/zxing/zxing-cpp/core/src/zxing/common/Array.h +170 -0
  88. data/ext/zxing/zxing-cpp/core/src/zxing/common/BitArray.cpp +155 -0
  89. data/ext/zxing/zxing-cpp/core/src/zxing/common/BitArray.h +81 -0
  90. data/ext/zxing/zxing-cpp/core/src/zxing/common/BitArrayIO.cpp +31 -0
  91. data/ext/zxing/zxing-cpp/core/src/zxing/common/BitMatrix.cpp +143 -0
  92. data/ext/zxing/zxing-cpp/core/src/zxing/common/BitMatrix.h +91 -0
  93. data/ext/zxing/zxing-cpp/core/src/zxing/common/BitSource.cpp +76 -0
  94. data/ext/zxing/zxing-cpp/core/src/zxing/common/BitSource.h +74 -0
  95. data/ext/zxing/zxing-cpp/core/src/zxing/common/CharacterSetECI.cpp +104 -0
  96. data/ext/zxing/zxing-cpp/core/src/zxing/common/CharacterSetECI.h +53 -0
  97. data/ext/zxing/zxing-cpp/core/src/zxing/common/Counted.h +140 -0
  98. data/ext/zxing/zxing-cpp/core/src/zxing/common/DecoderResult.cpp +46 -0
  99. data/ext/zxing/zxing-cpp/core/src/zxing/common/DecoderResult.h +51 -0
  100. data/ext/zxing/zxing-cpp/core/src/zxing/common/DetectorResult.cpp +39 -0
  101. data/ext/zxing/zxing-cpp/core/src/zxing/common/DetectorResult.h +43 -0
  102. data/ext/zxing/zxing-cpp/core/src/zxing/common/GlobalHistogramBinarizer.cpp +212 -0
  103. data/ext/zxing/zxing-cpp/core/src/zxing/common/GlobalHistogramBinarizer.h +48 -0
  104. data/ext/zxing/zxing-cpp/core/src/zxing/common/GreyscaleLuminanceSource.cpp +80 -0
  105. data/ext/zxing/zxing-cpp/core/src/zxing/common/GreyscaleLuminanceSource.h +53 -0
  106. data/ext/zxing/zxing-cpp/core/src/zxing/common/GreyscaleRotatedLuminanceSource.cpp +81 -0
  107. data/ext/zxing/zxing-cpp/core/src/zxing/common/GreyscaleRotatedLuminanceSource.h +46 -0
  108. data/ext/zxing/zxing-cpp/core/src/zxing/common/GridSampler.cpp +122 -0
  109. data/ext/zxing/zxing-cpp/core/src/zxing/common/GridSampler.h +45 -0
  110. data/ext/zxing/zxing-cpp/core/src/zxing/common/HybridBinarizer.cpp +226 -0
  111. data/ext/zxing/zxing-cpp/core/src/zxing/common/HybridBinarizer.h +67 -0
  112. data/ext/zxing/zxing-cpp/core/src/zxing/common/IllegalArgumentException.cpp +27 -0
  113. data/ext/zxing/zxing-cpp/core/src/zxing/common/IllegalArgumentException.h +36 -0
  114. data/ext/zxing/zxing-cpp/core/src/zxing/common/PerspectiveTransform.cpp +107 -0
  115. data/ext/zxing/zxing-cpp/core/src/zxing/common/PerspectiveTransform.h +49 -0
  116. data/ext/zxing/zxing-cpp/core/src/zxing/common/Point.h +47 -0
  117. data/ext/zxing/zxing-cpp/core/src/zxing/common/Str.cpp +61 -0
  118. data/ext/zxing/zxing-cpp/core/src/zxing/common/Str.h +51 -0
  119. data/ext/zxing/zxing-cpp/core/src/zxing/common/StringUtils.cpp +198 -0
  120. data/ext/zxing/zxing-cpp/core/src/zxing/common/StringUtils.h +52 -0
  121. data/ext/zxing/zxing-cpp/core/src/zxing/common/detector/JavaMath.h +43 -0
  122. data/ext/zxing/zxing-cpp/core/src/zxing/common/detector/MathUtils.h +57 -0
  123. data/ext/zxing/zxing-cpp/core/src/zxing/common/detector/MonochromeRectangleDetector.cpp +174 -0
  124. data/ext/zxing/zxing-cpp/core/src/zxing/common/detector/MonochromeRectangleDetector.h +62 -0
  125. data/ext/zxing/zxing-cpp/core/src/zxing/common/detector/WhiteRectangleDetector.cpp +330 -0
  126. data/ext/zxing/zxing-cpp/core/src/zxing/common/detector/WhiteRectangleDetector.h +59 -0
  127. data/ext/zxing/zxing-cpp/core/src/zxing/common/reedsolomon/GenericGF.cpp +150 -0
  128. data/ext/zxing/zxing-cpp/core/src/zxing/common/reedsolomon/GenericGF.h +73 -0
  129. data/ext/zxing/zxing-cpp/core/src/zxing/common/reedsolomon/GenericGFPoly.cpp +218 -0
  130. data/ext/zxing/zxing-cpp/core/src/zxing/common/reedsolomon/GenericGFPoly.h +56 -0
  131. data/ext/zxing/zxing-cpp/core/src/zxing/common/reedsolomon/ReedSolomonDecoder.cpp +174 -0
  132. data/ext/zxing/zxing-cpp/core/src/zxing/common/reedsolomon/ReedSolomonDecoder.h +49 -0
  133. data/ext/zxing/zxing-cpp/core/src/zxing/common/reedsolomon/ReedSolomonException.cpp +30 -0
  134. data/ext/zxing/zxing-cpp/core/src/zxing/common/reedsolomon/ReedSolomonException.h +33 -0
  135. data/ext/zxing/zxing-cpp/core/src/zxing/datamatrix/DataMatrixReader.cpp +54 -0
  136. data/ext/zxing/zxing-cpp/core/src/zxing/datamatrix/DataMatrixReader.h +45 -0
  137. data/ext/zxing/zxing-cpp/core/src/zxing/datamatrix/Version.cpp +199 -0
  138. data/ext/zxing/zxing-cpp/core/src/zxing/datamatrix/Version.h +87 -0
  139. data/ext/zxing/zxing-cpp/core/src/zxing/datamatrix/decoder/BitMatrixParser.cpp +361 -0
  140. data/ext/zxing/zxing-cpp/core/src/zxing/datamatrix/decoder/BitMatrixParser.h +59 -0
  141. data/ext/zxing/zxing-cpp/core/src/zxing/datamatrix/decoder/DataBlock.cpp +113 -0
  142. data/ext/zxing/zxing-cpp/core/src/zxing/datamatrix/decoder/DataBlock.h +49 -0
  143. data/ext/zxing/zxing-cpp/core/src/zxing/datamatrix/decoder/DecodedBitStreamParser.cpp +416 -0
  144. data/ext/zxing/zxing-cpp/core/src/zxing/datamatrix/decoder/DecodedBitStreamParser.h +104 -0
  145. data/ext/zxing/zxing-cpp/core/src/zxing/datamatrix/decoder/Decoder.cpp +93 -0
  146. data/ext/zxing/zxing-cpp/core/src/zxing/datamatrix/decoder/Decoder.h +49 -0
  147. data/ext/zxing/zxing-cpp/core/src/zxing/datamatrix/detector/CornerPoint.cpp +46 -0
  148. data/ext/zxing/zxing-cpp/core/src/zxing/datamatrix/detector/CornerPoint.h +43 -0
  149. data/ext/zxing/zxing-cpp/core/src/zxing/datamatrix/detector/Detector.cpp +446 -0
  150. data/ext/zxing/zxing-cpp/core/src/zxing/datamatrix/detector/Detector.h +94 -0
  151. data/ext/zxing/zxing-cpp/core/src/zxing/datamatrix/detector/DetectorException.cpp +23 -0
  152. data/ext/zxing/zxing-cpp/core/src/zxing/datamatrix/detector/DetectorException.h +23 -0
  153. data/ext/zxing/zxing-cpp/core/src/zxing/multi/ByQuadrantReader.cpp +75 -0
  154. data/ext/zxing/zxing-cpp/core/src/zxing/multi/ByQuadrantReader.h +42 -0
  155. data/ext/zxing/zxing-cpp/core/src/zxing/multi/GenericMultipleBarcodeReader.cpp +137 -0
  156. data/ext/zxing/zxing-cpp/core/src/zxing/multi/GenericMultipleBarcodeReader.h +51 -0
  157. data/ext/zxing/zxing-cpp/core/src/zxing/multi/MultipleBarcodeReader.cpp +29 -0
  158. data/ext/zxing/zxing-cpp/core/src/zxing/multi/MultipleBarcodeReader.h +41 -0
  159. data/ext/zxing/zxing-cpp/core/src/zxing/multi/qrcode/QRCodeMultiReader.cpp +58 -0
  160. data/ext/zxing/zxing-cpp/core/src/zxing/multi/qrcode/QRCodeMultiReader.h +36 -0
  161. data/ext/zxing/zxing-cpp/core/src/zxing/multi/qrcode/detector/MultiDetector.cpp +47 -0
  162. data/ext/zxing/zxing-cpp/core/src/zxing/multi/qrcode/detector/MultiDetector.h +37 -0
  163. data/ext/zxing/zxing-cpp/core/src/zxing/multi/qrcode/detector/MultiFinderPatternFinder.cpp +236 -0
  164. data/ext/zxing/zxing-cpp/core/src/zxing/multi/qrcode/detector/MultiFinderPatternFinder.h +47 -0
  165. data/ext/zxing/zxing-cpp/core/src/zxing/oned/CodaBarReader.cpp +340 -0
  166. data/ext/zxing/zxing-cpp/core/src/zxing/oned/CodaBarReader.h +57 -0
  167. data/ext/zxing/zxing-cpp/core/src/zxing/oned/Code128Reader.cpp +496 -0
  168. data/ext/zxing/zxing-cpp/core/src/zxing/oned/Code128Reader.h +48 -0
  169. data/ext/zxing/zxing-cpp/core/src/zxing/oned/Code39Reader.cpp +328 -0
  170. data/ext/zxing/zxing-cpp/core/src/zxing/oned/Code39Reader.h +63 -0
  171. data/ext/zxing/zxing-cpp/core/src/zxing/oned/Code93Reader.cpp +293 -0
  172. data/ext/zxing/zxing-cpp/core/src/zxing/oned/Code93Reader.h +58 -0
  173. data/ext/zxing/zxing-cpp/core/src/zxing/oned/EAN13Reader.cpp +85 -0
  174. data/ext/zxing/zxing-cpp/core/src/zxing/oned/EAN13Reader.h +49 -0
  175. data/ext/zxing/zxing-cpp/core/src/zxing/oned/EAN8Reader.cpp +65 -0
  176. data/ext/zxing/zxing-cpp/core/src/zxing/oned/EAN8Reader.h +47 -0
  177. data/ext/zxing/zxing-cpp/core/src/zxing/oned/ITFReader.cpp +337 -0
  178. data/ext/zxing/zxing-cpp/core/src/zxing/oned/ITFReader.h +54 -0
  179. data/ext/zxing/zxing-cpp/core/src/zxing/oned/MultiFormatOneDReader.cpp +96 -0
  180. data/ext/zxing/zxing-cpp/core/src/zxing/oned/MultiFormatOneDReader.h +38 -0
  181. data/ext/zxing/zxing-cpp/core/src/zxing/oned/MultiFormatUPCEANReader.cpp +110 -0
  182. data/ext/zxing/zxing-cpp/core/src/zxing/oned/MultiFormatUPCEANReader.h +41 -0
  183. data/ext/zxing/zxing-cpp/core/src/zxing/oned/OneDReader.cpp +227 -0
  184. data/ext/zxing/zxing-cpp/core/src/zxing/oned/OneDReader.h +81 -0
  185. data/ext/zxing/zxing-cpp/core/src/zxing/oned/OneDResultPoint.cpp +28 -0
  186. data/ext/zxing/zxing-cpp/core/src/zxing/oned/OneDResultPoint.h +35 -0
  187. data/ext/zxing/zxing-cpp/core/src/zxing/oned/UPCAReader.cpp +71 -0
  188. data/ext/zxing/zxing-cpp/core/src/zxing/oned/UPCAReader.h +50 -0
  189. data/ext/zxing/zxing-cpp/core/src/zxing/oned/UPCEANReader.cpp +309 -0
  190. data/ext/zxing/zxing-cpp/core/src/zxing/oned/UPCEANReader.h +88 -0
  191. data/ext/zxing/zxing-cpp/core/src/zxing/oned/UPCEReader.cpp +146 -0
  192. data/ext/zxing/zxing-cpp/core/src/zxing/oned/UPCEReader.h +47 -0
  193. data/ext/zxing/zxing-cpp/core/src/zxing/pdf417/PDF417Reader.cpp +170 -0
  194. data/ext/zxing/zxing-cpp/core/src/zxing/pdf417/PDF417Reader.h +49 -0
  195. data/ext/zxing/zxing-cpp/core/src/zxing/pdf417/decoder/BitMatrixParser.cpp +997 -0
  196. data/ext/zxing/zxing-cpp/core/src/zxing/pdf417/decoder/BitMatrixParser.h +84 -0
  197. data/ext/zxing/zxing-cpp/core/src/zxing/pdf417/decoder/DecodedBitStreamParser.cpp +563 -0
  198. data/ext/zxing/zxing-cpp/core/src/zxing/pdf417/decoder/DecodedBitStreamParser.h +84 -0
  199. data/ext/zxing/zxing-cpp/core/src/zxing/pdf417/decoder/Decoder.cpp +118 -0
  200. data/ext/zxing/zxing-cpp/core/src/zxing/pdf417/decoder/Decoder.h +62 -0
  201. data/ext/zxing/zxing-cpp/core/src/zxing/pdf417/decoder/ec/ErrorCorrection.cpp +214 -0
  202. data/ext/zxing/zxing-cpp/core/src/zxing/pdf417/decoder/ec/ErrorCorrection.h +71 -0
  203. data/ext/zxing/zxing-cpp/core/src/zxing/pdf417/decoder/ec/ModulusGF.cpp +120 -0
  204. data/ext/zxing/zxing-cpp/core/src/zxing/pdf417/decoder/ec/ModulusGF.h +72 -0
  205. data/ext/zxing/zxing-cpp/core/src/zxing/pdf417/decoder/ec/ModulusPoly.cpp +284 -0
  206. data/ext/zxing/zxing-cpp/core/src/zxing/pdf417/decoder/ec/ModulusPoly.h +68 -0
  207. data/ext/zxing/zxing-cpp/core/src/zxing/pdf417/detector/Detector.cpp +664 -0
  208. data/ext/zxing/zxing-cpp/core/src/zxing/pdf417/detector/Detector.h +106 -0
  209. data/ext/zxing/zxing-cpp/core/src/zxing/pdf417/detector/LinesSampler.cpp +714 -0
  210. data/ext/zxing/zxing-cpp/core/src/zxing/pdf417/detector/LinesSampler.h +122 -0
  211. data/ext/zxing/zxing-cpp/core/src/zxing/qrcode/ErrorCorrectionLevel.cpp +65 -0
  212. data/ext/zxing/zxing-cpp/core/src/zxing/qrcode/ErrorCorrectionLevel.h +52 -0
  213. data/ext/zxing/zxing-cpp/core/src/zxing/qrcode/FormatInformation.cpp +117 -0
  214. data/ext/zxing/zxing-cpp/core/src/zxing/qrcode/FormatInformation.h +54 -0
  215. data/ext/zxing/zxing-cpp/core/src/zxing/qrcode/QRCodeReader.cpp +52 -0
  216. data/ext/zxing/zxing-cpp/core/src/zxing/qrcode/QRCodeReader.h +48 -0
  217. data/ext/zxing/zxing-cpp/core/src/zxing/qrcode/Version.cpp +560 -0
  218. data/ext/zxing/zxing-cpp/core/src/zxing/qrcode/Version.h +85 -0
  219. data/ext/zxing/zxing-cpp/core/src/zxing/qrcode/decoder/BitMatrixParser.cpp +183 -0
  220. data/ext/zxing/zxing-cpp/core/src/zxing/qrcode/decoder/BitMatrixParser.h +56 -0
  221. data/ext/zxing/zxing-cpp/core/src/zxing/qrcode/decoder/DataBlock.cpp +118 -0
  222. data/ext/zxing/zxing-cpp/core/src/zxing/qrcode/decoder/DataBlock.h +50 -0
  223. data/ext/zxing/zxing-cpp/core/src/zxing/qrcode/decoder/DataMask.cpp +159 -0
  224. data/ext/zxing/zxing-cpp/core/src/zxing/qrcode/decoder/DataMask.h +50 -0
  225. data/ext/zxing/zxing-cpp/core/src/zxing/qrcode/decoder/DecodedBitStreamParser.cpp +425 -0
  226. data/ext/zxing/zxing-cpp/core/src/zxing/qrcode/decoder/DecodedBitStreamParser.h +72 -0
  227. data/ext/zxing/zxing-cpp/core/src/zxing/qrcode/decoder/Decoder.cpp +107 -0
  228. data/ext/zxing/zxing-cpp/core/src/zxing/qrcode/decoder/Decoder.h +46 -0
  229. data/ext/zxing/zxing-cpp/core/src/zxing/qrcode/decoder/Mode.cpp +90 -0
  230. data/ext/zxing/zxing-cpp/core/src/zxing/qrcode/decoder/Mode.h +57 -0
  231. data/ext/zxing/zxing-cpp/core/src/zxing/qrcode/detector/AlignmentPattern.cpp +47 -0
  232. data/ext/zxing/zxing-cpp/core/src/zxing/qrcode/detector/AlignmentPattern.h +45 -0
  233. data/ext/zxing/zxing-cpp/core/src/zxing/qrcode/detector/AlignmentPatternFinder.cpp +208 -0
  234. data/ext/zxing/zxing-cpp/core/src/zxing/qrcode/detector/AlignmentPatternFinder.h +68 -0
  235. data/ext/zxing/zxing-cpp/core/src/zxing/qrcode/detector/Detector.cpp +314 -0
  236. data/ext/zxing/zxing-cpp/core/src/zxing/qrcode/detector/Detector.h +69 -0
  237. data/ext/zxing/zxing-cpp/core/src/zxing/qrcode/detector/FinderPattern.cpp +69 -0
  238. data/ext/zxing/zxing-cpp/core/src/zxing/qrcode/detector/FinderPattern.h +48 -0
  239. data/ext/zxing/zxing-cpp/core/src/zxing/qrcode/detector/FinderPatternFinder.cpp +559 -0
  240. data/ext/zxing/zxing-cpp/core/src/zxing/qrcode/detector/FinderPatternFinder.h +76 -0
  241. data/ext/zxing/zxing-cpp/core/src/zxing/qrcode/detector/FinderPatternInfo.cpp +41 -0
  242. data/ext/zxing/zxing-cpp/core/src/zxing/qrcode/detector/FinderPatternInfo.h +47 -0
  243. data/ext/zxing/zxing-cpp/core/tests/src/TestRunner.cpp +30 -0
  244. data/ext/zxing/zxing-cpp/core/tests/src/common/BitArrayTest.cpp +216 -0
  245. data/ext/zxing/zxing-cpp/core/tests/src/common/BitArrayTest.h +61 -0
  246. data/ext/zxing/zxing-cpp/core/tests/src/common/BitMatrixTest.cpp +106 -0
  247. data/ext/zxing/zxing-cpp/core/tests/src/common/BitMatrixTest.h +55 -0
  248. data/ext/zxing/zxing-cpp/core/tests/src/common/BitSourceTest.cpp +49 -0
  249. data/ext/zxing/zxing-cpp/core/tests/src/common/BitSourceTest.h +42 -0
  250. data/ext/zxing/zxing-cpp/core/tests/src/common/CountedTest.cpp +58 -0
  251. data/ext/zxing/zxing-cpp/core/tests/src/common/CountedTest.h +46 -0
  252. data/ext/zxing/zxing-cpp/core/tests/src/common/PerspectiveTransformTest.cpp +69 -0
  253. data/ext/zxing/zxing-cpp/core/tests/src/common/PerspectiveTransformTest.h +47 -0
  254. data/ext/zxing/zxing-cpp/core/tests/src/common/reedsolomon/ReedSolomonTest.cpp +129 -0
  255. data/ext/zxing/zxing-cpp/core/tests/src/common/reedsolomon/ReedSolomonTest.h +62 -0
  256. data/ext/zxing/zxing-cpp/core/tests/src/qrcode/ErrorCorrectionLevelTest.cpp +47 -0
  257. data/ext/zxing/zxing-cpp/core/tests/src/qrcode/ErrorCorrectionLevelTest.h +45 -0
  258. data/ext/zxing/zxing-cpp/core/tests/src/qrcode/FormatInformationTest.cpp +88 -0
  259. data/ext/zxing/zxing-cpp/core/tests/src/qrcode/FormatInformationTest.h +47 -0
  260. data/ext/zxing/zxing-cpp/core/tests/src/qrcode/VersionTest.cpp +88 -0
  261. data/ext/zxing/zxing-cpp/core/tests/src/qrcode/VersionTest.h +49 -0
  262. data/ext/zxing/zxing-cpp/core/tests/src/qrcode/decoder/DataMaskTest.cpp +132 -0
  263. data/ext/zxing/zxing-cpp/core/tests/src/qrcode/decoder/DataMaskTest.h +91 -0
  264. data/ext/zxing/zxing-cpp/core/tests/src/qrcode/decoder/ModeTest.cpp +52 -0
  265. data/ext/zxing/zxing-cpp/core/tests/src/qrcode/decoder/ModeTest.h +47 -0
  266. data/ext/zxing/zxing.cc +224 -0
  267. data/lib/zxing.rb +50 -0
  268. data/lib/zxing/.gitignore +2 -0
  269. data/lib/zxing/aztec.rb +5 -0
  270. data/lib/zxing/aztec/aztec_reader.rb +14 -0
  271. data/lib/zxing/bad_image_exception.rb +4 -0
  272. data/lib/zxing/binarizer.rb +8 -0
  273. data/lib/zxing/binary_bitmap.rb +15 -0
  274. data/lib/zxing/checksum_exception.rb +4 -0
  275. data/lib/zxing/common.rb +7 -0
  276. data/lib/zxing/common/bit_matrix.rb +9 -0
  277. data/lib/zxing/common/hybrid_binarizer.rb +17 -0
  278. data/lib/zxing/common/illegal_argument_exception.rb +4 -0
  279. data/lib/zxing/datamatrix.rb +5 -0
  280. data/lib/zxing/datamatrix/data_matrix_reader.rb +14 -0
  281. data/lib/zxing/decodable.rb +11 -0
  282. data/lib/zxing/exception.rb +4 -0
  283. data/lib/zxing/ffi.rb +20 -0
  284. data/lib/zxing/ffi/aztec.rb +6 -0
  285. data/lib/zxing/ffi/aztec/aztec_reader.rb +9 -0
  286. data/lib/zxing/ffi/binarizer.rb +25 -0
  287. data/lib/zxing/ffi/binary_bitmap.rb +15 -0
  288. data/lib/zxing/ffi/common.rb +8 -0
  289. data/lib/zxing/ffi/common/bit_matrix.rb +12 -0
  290. data/lib/zxing/ffi/common/greyscale_luminance_source.rb +30 -0
  291. data/lib/zxing/ffi/common/hybrid_binarizer.rb +10 -0
  292. data/lib/zxing/ffi/datamatrix.rb +6 -0
  293. data/lib/zxing/ffi/datamatrix/data_matrix_reader.rb +9 -0
  294. data/lib/zxing/ffi/library.rb +102 -0
  295. data/lib/zxing/ffi/luminance_source.rb +18 -0
  296. data/lib/zxing/ffi/multi_format_reader.rb +9 -0
  297. data/lib/zxing/ffi/oned.rb +6 -0
  298. data/lib/zxing/ffi/oned/code_39_reader.rb +10 -0
  299. data/lib/zxing/ffi/qrcode.rb +8 -0
  300. data/lib/zxing/ffi/qrcode/decoder.rb +10 -0
  301. data/lib/zxing/ffi/qrcode/detector.rb +10 -0
  302. data/lib/zxing/ffi/reader.rb +58 -0
  303. data/lib/zxing/ffi/result.rb +23 -0
  304. data/lib/zxing/format_exception.rb +4 -0
  305. data/lib/zxing/illegal_argument_exception.rb +4 -0
  306. data/lib/zxing/image.rb +15 -0
  307. data/lib/zxing/luminance_source.rb +18 -0
  308. data/lib/zxing/multi_format_reader.rb +11 -0
  309. data/lib/zxing/not_found_exception.rb +4 -0
  310. data/lib/zxing/oned.rb +5 -0
  311. data/lib/zxing/oned/code_39_reader.rb +15 -0
  312. data/lib/zxing/qrcode.rb +8 -0
  313. data/lib/zxing/qrcode/decoder.rb +14 -0
  314. data/lib/zxing/qrcode/detector.rb +14 -0
  315. data/lib/zxing/qrcode/encoder.rb +8 -0
  316. data/lib/zxing/qrcode/encoder/byte_matrix.rb +18 -0
  317. data/lib/zxing/qrcode/encoder/encoder.rb +9 -0
  318. data/lib/zxing/qrcode/encoder/qrcode.rb +18 -0
  319. data/lib/zxing/reader.rb +8 -0
  320. data/lib/zxing/reader_exception.rb +4 -0
  321. data/lib/zxing/reed_solomon_exception.rb +2 -0
  322. data/lib/zxing/result.rb +18 -0
  323. data/lib/zxing/rmagick.rb +5 -0
  324. data/lib/zxing/rmagick/image.rb +104 -0
  325. data/lib/zxing/version.rb +33 -0
  326. data/test/qrcode.png +0 -0
  327. data/test/test_helper.rb +5 -0
  328. data/test/test_zxing.rb +58 -0
  329. data/test/vendor.rb +360 -0
  330. data/test/zxing/test_decodable.rb +38 -0
  331. data/zxing_cpp.gemspec +48 -0
  332. metadata +473 -0
@@ -0,0 +1,71 @@
1
+
2
+ C++ Big Integer Library
3
+ (see ChangeLog for version)
4
+
5
+ http://mattmccutchen.net/bigint/
6
+
7
+ Written and maintained by Matt McCutchen <matt@mattmccutchen.net>
8
+
9
+ You can use this library in a C++ program to do arithmetic on integers of size
10
+ limited only by your computer's memory. The library provides BigUnsigned and
11
+ BigInteger classes that represent nonnegative integers and signed integers,
12
+ respectively. Most of the C++ arithmetic operators are overloaded for these
13
+ classes, so big-integer calculations are as easy as:
14
+
15
+ #include "BigIntegerLibrary.hh"
16
+
17
+ BigInteger a = 65536;
18
+ cout << (a * a * a * a * a * a * a * a);
19
+
20
+ (prints 340282366920938463463374607431768211456)
21
+
22
+ The code in `sample.cc' demonstrates the most important features of the library.
23
+ To get started quickly, read the code and explanations in that file and run it.
24
+ If you want more detail or a feature not shown in `sample.cc', consult the
25
+ consult the actual header and source files, which are thoroughly commented.
26
+
27
+ This library emphasizes ease of use and clarity of implementation over speed;
28
+ some users will prefer GMP (http://swox.com/gmp/), which is faster. The code is
29
+ intended to be reasonably portable across computers and modern C++ compilers; in
30
+ particular, it uses whatever word size the computer provides (32-bit, 64-bit, or
31
+ otherwise).
32
+
33
+ Compiling programs that use the library
34
+ ---------------------------------------
35
+ The library consists of a folder full of C++ header files (`.hh') and source
36
+ files (`.cc'). Your own programs should `#include' the necessary header files
37
+ and link with the source files. A makefile that builds the sample program
38
+ (`sample.cc') is included; you can adapt it to replace the sample with your own
39
+ program.
40
+
41
+ Alternatively, you can use your own build system or IDE. In that case, you must
42
+ put the library header files where the compiler will find them and arrange to
43
+ have your program linked with the library source files; otherwise, you will get
44
+ errors about missing header files or "undefined references". To learn how to do
45
+ this, consult the documentation for the build system or IDE; don't bother asking
46
+ me. Adding all the library files to your project will work in many IDEs but may
47
+ not be the most desirable approach.
48
+
49
+ Resources
50
+ ---------
51
+ The library's Web site (above) provides links to released versions, the current
52
+ development version, and a mailing list for release announcements, questions,
53
+ bug reports, and other discussion of the library. I would be delighted to hear
54
+ from you if you like this library and/or find a good use for it.
55
+
56
+ Bugs and enhancements
57
+ ---------------------
58
+ The library has been tested by me and others but is by no means bug-free. If
59
+ you find a bug, please report it, whether it comes in the form of compiling
60
+ trouble, a mathematically inaccurate result, or a memory-management blooper
61
+ (since I use Java, these are altogether too common in my C++). I generally fix
62
+ all reported bugs. You are also welcome to request enhancements, but I am
63
+ unlikely to do substantial amounts of work on enhancements at this point.
64
+
65
+ Legal
66
+ -----
67
+ I, Matt McCutchen, the sole author of the original Big Integer Library, waive my
68
+ copyright to it, placing it in the public domain. The library comes with
69
+ absolutely no warranty.
70
+
71
+ ~~~~
@@ -0,0 +1,14 @@
1
+ #ifndef _LIBICONV_H
2
+ #define _LIBICONV_H
3
+ #include <stddef.h>
4
+ #ifdef __cplusplus
5
+ extern "C" {
6
+ #endif
7
+ typedef void* iconv_t;
8
+ iconv_t iconv_open(const char *tocode, const char *fromcode);
9
+ int iconv_close(iconv_t cd);
10
+ size_t iconv(iconv_t cd, char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft);
11
+ #ifdef __cplusplus
12
+ }
13
+ #endif
14
+ #endif//_LIBICONV_H
@@ -0,0 +1,247 @@
1
+ // ISO C9x compliant stdint.h for Microsoft Visual Studio
2
+ // Based on ISO/IEC 9899:TC2 Committee draft (May 6, 2005) WG14/N1124
3
+ //
4
+ // Copyright (c) 2006-2008 Alexander Chemeris
5
+ //
6
+ // Redistribution and use in source and binary forms, with or without
7
+ // modification, are permitted provided that the following conditions are met:
8
+ //
9
+ // 1. Redistributions of source code must retain the above copyright notice,
10
+ // this list of conditions and the following disclaimer.
11
+ //
12
+ // 2. Redistributions in binary form must reproduce the above copyright
13
+ // notice, this list of conditions and the following disclaimer in the
14
+ // documentation and/or other materials provided with the distribution.
15
+ //
16
+ // 3. The name of the author may be used to endorse or promote products
17
+ // derived from this software without specific prior written permission.
18
+ //
19
+ // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
20
+ // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21
+ // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
22
+ // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
+ // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24
+ // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25
+ // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26
+ // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27
+ // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28
+ // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
+ //
30
+ ///////////////////////////////////////////////////////////////////////////////
31
+
32
+ #ifndef _MSC_VER // [
33
+ #error "Use this header only with Microsoft Visual C++ compilers!"
34
+ #endif // _MSC_VER ]
35
+
36
+ #ifndef _MSC_STDINT_H_ // [
37
+ #define _MSC_STDINT_H_
38
+
39
+ #if _MSC_VER > 1000
40
+ #pragma once
41
+ #endif
42
+
43
+ #include <limits.h>
44
+
45
+ // For Visual Studio 6 in C++ mode and for many Visual Studio versions when
46
+ // compiling for ARM we should wrap <wchar.h> include with 'extern "C++" {}'
47
+ // or compiler give many errors like this:
48
+ // error C2733: second C linkage of overloaded function 'wmemchr' not allowed
49
+ #ifdef __cplusplus
50
+ extern "C" {
51
+ #endif
52
+ # include <wchar.h>
53
+ #ifdef __cplusplus
54
+ }
55
+ #endif
56
+
57
+ // Define _W64 macros to mark types changing their size, like intptr_t.
58
+ #ifndef _W64
59
+ # if !defined(__midl) && (defined(_X86_) || defined(_M_IX86)) && _MSC_VER >= 1300
60
+ # define _W64 __w64
61
+ # else
62
+ # define _W64
63
+ # endif
64
+ #endif
65
+
66
+
67
+ // 7.18.1 Integer types
68
+
69
+ // 7.18.1.1 Exact-width integer types
70
+
71
+ // Visual Studio 6 and Embedded Visual C++ 4 doesn't
72
+ // realize that, e.g. char has the same size as __int8
73
+ // so we give up on __intX for them.
74
+ #if (_MSC_VER < 1300)
75
+ typedef signed char int8_t;
76
+ typedef signed short int16_t;
77
+ typedef signed int int32_t;
78
+ typedef unsigned char uint8_t;
79
+ typedef unsigned short uint16_t;
80
+ typedef unsigned int uint32_t;
81
+ #else
82
+ typedef signed __int8 int8_t;
83
+ typedef signed __int16 int16_t;
84
+ typedef signed __int32 int32_t;
85
+ typedef unsigned __int8 uint8_t;
86
+ typedef unsigned __int16 uint16_t;
87
+ typedef unsigned __int32 uint32_t;
88
+ #endif
89
+ typedef signed __int64 int64_t;
90
+ typedef unsigned __int64 uint64_t;
91
+
92
+
93
+ // 7.18.1.2 Minimum-width integer types
94
+ typedef int8_t int_least8_t;
95
+ typedef int16_t int_least16_t;
96
+ typedef int32_t int_least32_t;
97
+ typedef int64_t int_least64_t;
98
+ typedef uint8_t uint_least8_t;
99
+ typedef uint16_t uint_least16_t;
100
+ typedef uint32_t uint_least32_t;
101
+ typedef uint64_t uint_least64_t;
102
+
103
+ // 7.18.1.3 Fastest minimum-width integer types
104
+ typedef int8_t int_fast8_t;
105
+ typedef int16_t int_fast16_t;
106
+ typedef int32_t int_fast32_t;
107
+ typedef int64_t int_fast64_t;
108
+ typedef uint8_t uint_fast8_t;
109
+ typedef uint16_t uint_fast16_t;
110
+ typedef uint32_t uint_fast32_t;
111
+ typedef uint64_t uint_fast64_t;
112
+
113
+ // 7.18.1.4 Integer types capable of holding object pointers
114
+ #ifdef _WIN64 // [
115
+ typedef signed __int64 intptr_t;
116
+ typedef unsigned __int64 uintptr_t;
117
+ #else // _WIN64 ][
118
+ typedef _W64 signed int intptr_t;
119
+ typedef _W64 unsigned int uintptr_t;
120
+ #endif // _WIN64 ]
121
+
122
+ // 7.18.1.5 Greatest-width integer types
123
+ typedef int64_t intmax_t;
124
+ typedef uint64_t uintmax_t;
125
+
126
+
127
+ // 7.18.2 Limits of specified-width integer types
128
+
129
+ #if !defined(__cplusplus) || defined(__STDC_LIMIT_MACROS) // [ See footnote 220 at page 257 and footnote 221 at page 259
130
+
131
+ // 7.18.2.1 Limits of exact-width integer types
132
+ #define INT8_MIN ((int8_t)_I8_MIN)
133
+ #define INT8_MAX _I8_MAX
134
+ #define INT16_MIN ((int16_t)_I16_MIN)
135
+ #define INT16_MAX _I16_MAX
136
+ #define INT32_MIN ((int32_t)_I32_MIN)
137
+ #define INT32_MAX _I32_MAX
138
+ #define INT64_MIN ((int64_t)_I64_MIN)
139
+ #define INT64_MAX _I64_MAX
140
+ #define UINT8_MAX _UI8_MAX
141
+ #define UINT16_MAX _UI16_MAX
142
+ #define UINT32_MAX _UI32_MAX
143
+ #define UINT64_MAX _UI64_MAX
144
+
145
+ // 7.18.2.2 Limits of minimum-width integer types
146
+ #define INT_LEAST8_MIN INT8_MIN
147
+ #define INT_LEAST8_MAX INT8_MAX
148
+ #define INT_LEAST16_MIN INT16_MIN
149
+ #define INT_LEAST16_MAX INT16_MAX
150
+ #define INT_LEAST32_MIN INT32_MIN
151
+ #define INT_LEAST32_MAX INT32_MAX
152
+ #define INT_LEAST64_MIN INT64_MIN
153
+ #define INT_LEAST64_MAX INT64_MAX
154
+ #define UINT_LEAST8_MAX UINT8_MAX
155
+ #define UINT_LEAST16_MAX UINT16_MAX
156
+ #define UINT_LEAST32_MAX UINT32_MAX
157
+ #define UINT_LEAST64_MAX UINT64_MAX
158
+
159
+ // 7.18.2.3 Limits of fastest minimum-width integer types
160
+ #define INT_FAST8_MIN INT8_MIN
161
+ #define INT_FAST8_MAX INT8_MAX
162
+ #define INT_FAST16_MIN INT16_MIN
163
+ #define INT_FAST16_MAX INT16_MAX
164
+ #define INT_FAST32_MIN INT32_MIN
165
+ #define INT_FAST32_MAX INT32_MAX
166
+ #define INT_FAST64_MIN INT64_MIN
167
+ #define INT_FAST64_MAX INT64_MAX
168
+ #define UINT_FAST8_MAX UINT8_MAX
169
+ #define UINT_FAST16_MAX UINT16_MAX
170
+ #define UINT_FAST32_MAX UINT32_MAX
171
+ #define UINT_FAST64_MAX UINT64_MAX
172
+
173
+ // 7.18.2.4 Limits of integer types capable of holding object pointers
174
+ #ifdef _WIN64 // [
175
+ # define INTPTR_MIN INT64_MIN
176
+ # define INTPTR_MAX INT64_MAX
177
+ # define UINTPTR_MAX UINT64_MAX
178
+ #else // _WIN64 ][
179
+ # define INTPTR_MIN INT32_MIN
180
+ # define INTPTR_MAX INT32_MAX
181
+ # define UINTPTR_MAX UINT32_MAX
182
+ #endif // _WIN64 ]
183
+
184
+ // 7.18.2.5 Limits of greatest-width integer types
185
+ #define INTMAX_MIN INT64_MIN
186
+ #define INTMAX_MAX INT64_MAX
187
+ #define UINTMAX_MAX UINT64_MAX
188
+
189
+ // 7.18.3 Limits of other integer types
190
+
191
+ #ifdef _WIN64 // [
192
+ # define PTRDIFF_MIN _I64_MIN
193
+ # define PTRDIFF_MAX _I64_MAX
194
+ #else // _WIN64 ][
195
+ # define PTRDIFF_MIN _I32_MIN
196
+ # define PTRDIFF_MAX _I32_MAX
197
+ #endif // _WIN64 ]
198
+
199
+ #define SIG_ATOMIC_MIN INT_MIN
200
+ #define SIG_ATOMIC_MAX INT_MAX
201
+
202
+ #ifndef SIZE_MAX // [
203
+ # ifdef _WIN64 // [
204
+ # define SIZE_MAX _UI64_MAX
205
+ # else // _WIN64 ][
206
+ # define SIZE_MAX _UI32_MAX
207
+ # endif // _WIN64 ]
208
+ #endif // SIZE_MAX ]
209
+
210
+ // WCHAR_MIN and WCHAR_MAX are also defined in <wchar.h>
211
+ #ifndef WCHAR_MIN // [
212
+ # define WCHAR_MIN 0
213
+ #endif // WCHAR_MIN ]
214
+ #ifndef WCHAR_MAX // [
215
+ # define WCHAR_MAX _UI16_MAX
216
+ #endif // WCHAR_MAX ]
217
+
218
+ #define WINT_MIN 0
219
+ #define WINT_MAX _UI16_MAX
220
+
221
+ #endif // __STDC_LIMIT_MACROS ]
222
+
223
+
224
+ // 7.18.4 Limits of other integer types
225
+
226
+ #if !defined(__cplusplus) || defined(__STDC_CONSTANT_MACROS) // [ See footnote 224 at page 260
227
+
228
+ // 7.18.4.1 Macros for minimum-width integer constants
229
+
230
+ #define INT8_C(val) val##i8
231
+ #define INT16_C(val) val##i16
232
+ #define INT32_C(val) val##i32
233
+ #define INT64_C(val) val##i64
234
+
235
+ #define UINT8_C(val) val##ui8
236
+ #define UINT16_C(val) val##ui16
237
+ #define UINT32_C(val) val##ui32
238
+ #define UINT64_C(val) val##ui64
239
+
240
+ // 7.18.4.2 Macros for greatest-width integer constants
241
+ #define INTMAX_C INT64_C
242
+ #define UINTMAX_C UINT64_C
243
+
244
+ #endif // __STDC_CONSTANT_MACROS ]
245
+
246
+
247
+ #endif // _MSC_STDINT_H_ ]
@@ -0,0 +1,2035 @@
1
+ /*
2
+ * iconv implementation using Win32 API to convert.
3
+ *
4
+ * This file is placed in the public domain.
5
+ */
6
+
7
+ /* for WC_NO_BEST_FIT_CHARS */
8
+ #ifndef WINVER
9
+ # define WINVER 0x0500
10
+ #endif
11
+
12
+ #define STRICT
13
+ #include <windows.h>
14
+ #include <errno.h>
15
+ #include <string.h>
16
+ #include <stdlib.h>
17
+
18
+ /* WORKAROUND: */
19
+ #ifndef UNDER_CE
20
+ #define GetProcAddressA GetProcAddress
21
+ #endif
22
+
23
+ #if 0
24
+ # define MAKE_EXE
25
+ # define MAKE_DLL
26
+ # define USE_LIBICONV_DLL
27
+ #endif
28
+
29
+ #if !defined(DEFAULT_LIBICONV_DLL)
30
+ # define DEFAULT_LIBICONV_DLL ""
31
+ #endif
32
+
33
+ #define MB_CHAR_MAX 16
34
+
35
+ #define UNICODE_MODE_BOM_DONE 1
36
+ #define UNICODE_MODE_SWAPPED 2
37
+
38
+ #define FLAG_USE_BOM 1
39
+ #define FLAG_TRANSLIT 2 /* //TRANSLIT */
40
+ #define FLAG_IGNORE 4 /* //IGNORE */
41
+
42
+ typedef unsigned char uchar;
43
+ typedef unsigned short ushort;
44
+ typedef unsigned int uint;
45
+
46
+ typedef void* iconv_t;
47
+
48
+ iconv_t iconv_open(const char *tocode, const char *fromcode);
49
+ int iconv_close(iconv_t cd);
50
+ size_t iconv(iconv_t cd, char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft);
51
+
52
+ /* libiconv interface for vim */
53
+ #if defined(MAKE_DLL)
54
+ int
55
+ iconvctl (iconv_t cd, int request, void* argument)
56
+ {
57
+ /* not supported */
58
+ return 0;
59
+ }
60
+ #endif
61
+
62
+ typedef struct compat_t compat_t;
63
+ typedef struct csconv_t csconv_t;
64
+ typedef struct rec_iconv_t rec_iconv_t;
65
+
66
+ typedef iconv_t (*f_iconv_open)(const char *tocode, const char *fromcode);
67
+ typedef int (*f_iconv_close)(iconv_t cd);
68
+ typedef size_t (*f_iconv)(iconv_t cd, char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft);
69
+ typedef int* (*f_errno)(void);
70
+ typedef int (*f_mbtowc)(csconv_t *cv, const uchar *buf, int bufsize, ushort *wbuf, int *wbufsize);
71
+ typedef int (*f_wctomb)(csconv_t *cv, ushort *wbuf, int wbufsize, uchar *buf, int bufsize);
72
+ typedef int (*f_mblen)(csconv_t *cv, const uchar *buf, int bufsize);
73
+ typedef int (*f_flush)(csconv_t *cv, uchar *buf, int bufsize);
74
+
75
+ #define COMPAT_IN 1
76
+ #define COMPAT_OUT 2
77
+
78
+ /* unicode mapping for compatibility with other conversion table. */
79
+ struct compat_t {
80
+ uint in;
81
+ uint out;
82
+ uint flag;
83
+ };
84
+
85
+ struct csconv_t {
86
+ int codepage;
87
+ int flags;
88
+ f_mbtowc mbtowc;
89
+ f_wctomb wctomb;
90
+ f_mblen mblen;
91
+ f_flush flush;
92
+ DWORD mode;
93
+ compat_t *compat;
94
+ };
95
+
96
+ struct rec_iconv_t {
97
+ iconv_t cd;
98
+ f_iconv_close iconv_close;
99
+ f_iconv iconv;
100
+ f_errno _errno;
101
+ csconv_t from;
102
+ csconv_t to;
103
+ #if defined(USE_LIBICONV_DLL)
104
+ HMODULE hlibiconv;
105
+ #endif
106
+ };
107
+
108
+ static int win_iconv_open(rec_iconv_t *cd, const char *tocode, const char *fromcode);
109
+ static int win_iconv_close(iconv_t cd);
110
+ static size_t win_iconv(iconv_t cd, char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft);
111
+
112
+ static int load_mlang();
113
+ static int make_csconv(const char *name, csconv_t *cv);
114
+ static int name_to_codepage(const char *name);
115
+ static uint utf16_to_ucs4(const ushort *wbuf);
116
+ static void ucs4_to_utf16(uint wc, ushort *wbuf, int *wbufsize);
117
+ static int mbtowc_flags(int codepage);
118
+ static int must_use_null_useddefaultchar(int codepage);
119
+ static char *strrstr(const char *str, const char *token);
120
+ static char *xstrndup(const char *s, size_t n);
121
+ static int seterror(int err);
122
+
123
+ #if defined(USE_LIBICONV_DLL)
124
+ static int libiconv_iconv_open(rec_iconv_t *cd, const char *tocode, const char *fromcode);
125
+ static PVOID MyImageDirectoryEntryToData(LPVOID Base, BOOLEAN MappedAsImage, USHORT DirectoryEntry, PULONG Size);
126
+ static HMODULE find_imported_module_by_funcname(HMODULE hModule, const char *funcname);
127
+
128
+ static HMODULE hwiniconv;
129
+ #endif
130
+
131
+ static int sbcs_mblen(csconv_t *cv, const uchar *buf, int bufsize);
132
+ static int dbcs_mblen(csconv_t *cv, const uchar *buf, int bufsize);
133
+ static int mbcs_mblen(csconv_t *cv, const uchar *buf, int bufsize);
134
+ static int utf8_mblen(csconv_t *cv, const uchar *buf, int bufsize);
135
+ static int eucjp_mblen(csconv_t *cv, const uchar *buf, int bufsize);
136
+
137
+ static int kernel_mbtowc(csconv_t *cv, const uchar *buf, int bufsize, ushort *wbuf, int *wbufsize);
138
+ static int kernel_wctomb(csconv_t *cv, ushort *wbuf, int wbufsize, uchar *buf, int bufsize);
139
+ static int mlang_mbtowc(csconv_t *cv, const uchar *buf, int bufsize, ushort *wbuf, int *wbufsize);
140
+ static int mlang_wctomb(csconv_t *cv, ushort *wbuf, int wbufsize, uchar *buf, int bufsize);
141
+ static int utf16_mbtowc(csconv_t *cv, const uchar *buf, int bufsize, ushort *wbuf, int *wbufsize);
142
+ static int utf16_wctomb(csconv_t *cv, ushort *wbuf, int wbufsize, uchar *buf, int bufsize);
143
+ static int utf32_mbtowc(csconv_t *cv, const uchar *buf, int bufsize, ushort *wbuf, int *wbufsize);
144
+ static int utf32_wctomb(csconv_t *cv, ushort *wbuf, int wbufsize, uchar *buf, int bufsize);
145
+ static int iso2022jp_mbtowc(csconv_t *cv, const uchar *buf, int bufsize, ushort *wbuf, int *wbufsize);
146
+ static int iso2022jp_wctomb(csconv_t *cv, ushort *wbuf, int wbufsize, uchar *buf, int bufsize);
147
+ static int iso2022jp_flush(csconv_t *cv, uchar *buf, int bufsize);
148
+
149
+ static struct {
150
+ int codepage;
151
+ const char *name;
152
+ } codepage_alias[] = {
153
+ {65001, "CP65001"},
154
+ {65001, "UTF8"},
155
+ {65001, "UTF-8"},
156
+
157
+ {1200, "CP1200"},
158
+ {1200, "UTF16LE"},
159
+ {1200, "UTF-16LE"},
160
+ {1200, "UCS2LE"},
161
+ {1200, "UCS-2LE"},
162
+
163
+ {1201, "CP1201"},
164
+ {1201, "UTF16BE"},
165
+ {1201, "UTF-16BE"},
166
+ {1201, "UCS2BE"},
167
+ {1201, "UCS-2BE"},
168
+ {1201, "unicodeFFFE"},
169
+
170
+ {12000, "CP12000"},
171
+ {12000, "UTF32LE"},
172
+ {12000, "UTF-32LE"},
173
+ {12000, "UCS4LE"},
174
+ {12000, "UCS-4LE"},
175
+
176
+ {12001, "CP12001"},
177
+ {12001, "UTF32BE"},
178
+ {12001, "UTF-32BE"},
179
+ {12001, "UCS4BE"},
180
+ {12001, "UCS-4BE"},
181
+
182
+ #ifndef GLIB_COMPILATION
183
+ /*
184
+ * Default is big endian.
185
+ * See rfc2781 4.3 Interpreting text labelled as UTF-16.
186
+ */
187
+ {1201, "UTF16"},
188
+ {1201, "UTF-16"},
189
+ {1201, "UCS2"},
190
+ {1201, "UCS-2"},
191
+ {12001, "UTF32"},
192
+ {12001, "UTF-32"},
193
+ {12001, "UCS-4"},
194
+ {12001, "UCS4"},
195
+ #else
196
+ /* Default is little endian, because the platform is */
197
+ {1200, "UTF16"},
198
+ {1200, "UTF-16"},
199
+ {1200, "UCS2"},
200
+ {1200, "UCS-2"},
201
+ {12000, "UTF32"},
202
+ {12000, "UTF-32"},
203
+ {12000, "UCS4"},
204
+ {12000, "UCS-4"},
205
+ #endif
206
+
207
+ /* copy from libiconv `iconv -l` */
208
+ /* !IsValidCodePage(367) */
209
+ {20127, "ANSI_X3.4-1968"},
210
+ {20127, "ANSI_X3.4-1986"},
211
+ {20127, "ASCII"},
212
+ {20127, "CP367"},
213
+ {20127, "IBM367"},
214
+ {20127, "ISO-IR-6"},
215
+ {20127, "ISO646-US"},
216
+ {20127, "ISO_646.IRV:1991"},
217
+ {20127, "US"},
218
+ {20127, "US-ASCII"},
219
+ {20127, "CSASCII"},
220
+
221
+ /* !IsValidCodePage(819) */
222
+ {1252, "CP819"},
223
+ {1252, "IBM819"},
224
+ {28591, "ISO-8859-1"},
225
+ {28591, "ISO-IR-100"},
226
+ {28591, "ISO8859-1"},
227
+ {28591, "ISO_8859-1"},
228
+ {28591, "ISO_8859-1:1987"},
229
+ {28591, "L1"},
230
+ {28591, "LATIN1"},
231
+ {28591, "CSISOLATIN1"},
232
+
233
+ {1250, "CP1250"},
234
+ {1250, "MS-EE"},
235
+ {1250, "WINDOWS-1250"},
236
+
237
+ {1251, "CP1251"},
238
+ {1251, "MS-CYRL"},
239
+ {1251, "WINDOWS-1251"},
240
+
241
+ {1252, "CP1252"},
242
+ {1252, "MS-ANSI"},
243
+ {1252, "WINDOWS-1252"},
244
+
245
+ {1253, "CP1253"},
246
+ {1253, "MS-GREEK"},
247
+ {1253, "WINDOWS-1253"},
248
+
249
+ {1254, "CP1254"},
250
+ {1254, "MS-TURK"},
251
+ {1254, "WINDOWS-1254"},
252
+
253
+ {1255, "CP1255"},
254
+ {1255, "MS-HEBR"},
255
+ {1255, "WINDOWS-1255"},
256
+
257
+ {1256, "CP1256"},
258
+ {1256, "MS-ARAB"},
259
+ {1256, "WINDOWS-1256"},
260
+
261
+ {1257, "CP1257"},
262
+ {1257, "WINBALTRIM"},
263
+ {1257, "WINDOWS-1257"},
264
+
265
+ {1258, "CP1258"},
266
+ {1258, "WINDOWS-1258"},
267
+
268
+ {850, "850"},
269
+ {850, "CP850"},
270
+ {850, "IBM850"},
271
+ {850, "CSPC850MULTILINGUAL"},
272
+
273
+ /* !IsValidCodePage(862) */
274
+ {862, "862"},
275
+ {862, "CP862"},
276
+ {862, "IBM862"},
277
+ {862, "CSPC862LATINHEBREW"},
278
+
279
+ {866, "866"},
280
+ {866, "CP866"},
281
+ {866, "IBM866"},
282
+ {866, "CSIBM866"},
283
+
284
+ /* !IsValidCodePage(154) */
285
+ {154, "CP154"},
286
+ {154, "CYRILLIC-ASIAN"},
287
+ {154, "PT154"},
288
+ {154, "PTCP154"},
289
+ {154, "CSPTCP154"},
290
+
291
+ /* !IsValidCodePage(1133) */
292
+ {1133, "CP1133"},
293
+ {1133, "IBM-CP1133"},
294
+
295
+ {874, "CP874"},
296
+ {874, "WINDOWS-874"},
297
+
298
+ /* !IsValidCodePage(51932) */
299
+ {51932, "CP51932"},
300
+ {51932, "MS51932"},
301
+ {51932, "WINDOWS-51932"},
302
+ {51932, "EUC-JP"},
303
+
304
+ {932, "CP932"},
305
+ {932, "MS932"},
306
+ {932, "SHIFFT_JIS"},
307
+ {932, "SHIFFT_JIS-MS"},
308
+ {932, "SJIS"},
309
+ {932, "SJIS-MS"},
310
+ {932, "SJIS-OPEN"},
311
+ {932, "SJIS-WIN"},
312
+ {932, "WINDOWS-31J"},
313
+ {932, "WINDOWS-932"},
314
+ {932, "CSWINDOWS31J"},
315
+
316
+ {50221, "CP50221"},
317
+ {50221, "ISO-2022-JP"},
318
+ {50221, "ISO-2022-JP-MS"},
319
+ {50221, "ISO2022-JP"},
320
+ {50221, "ISO2022-JP-MS"},
321
+ {50221, "MS50221"},
322
+ {50221, "WINDOWS-50221"},
323
+
324
+ {936, "CP936"},
325
+ {936, "GBK"},
326
+ {936, "MS936"},
327
+ {936, "WINDOWS-936"},
328
+
329
+ {950, "CP950"},
330
+ {950, "BIG5"},
331
+ {950, "BIG5HKSCS"},
332
+ {950, "BIG5-HKSCS"},
333
+
334
+ {949, "CP949"},
335
+ {949, "UHC"},
336
+ {949, "EUC-KR"},
337
+
338
+ {1361, "CP1361"},
339
+ {1361, "JOHAB"},
340
+
341
+ {437, "437"},
342
+ {437, "CP437"},
343
+ {437, "IBM437"},
344
+ {437, "CSPC8CODEPAGE437"},
345
+
346
+ {737, "CP737"},
347
+
348
+ {775, "CP775"},
349
+ {775, "IBM775"},
350
+ {775, "CSPC775BALTIC"},
351
+
352
+ {852, "852"},
353
+ {852, "CP852"},
354
+ {852, "IBM852"},
355
+ {852, "CSPCP852"},
356
+
357
+ /* !IsValidCodePage(853) */
358
+ {853, "CP853"},
359
+
360
+ {855, "855"},
361
+ {855, "CP855"},
362
+ {855, "IBM855"},
363
+ {855, "CSIBM855"},
364
+
365
+ {857, "857"},
366
+ {857, "CP857"},
367
+ {857, "IBM857"},
368
+ {857, "CSIBM857"},
369
+
370
+ /* !IsValidCodePage(858) */
371
+ {858, "CP858"},
372
+
373
+ {860, "860"},
374
+ {860, "CP860"},
375
+ {860, "IBM860"},
376
+ {860, "CSIBM860"},
377
+
378
+ {861, "861"},
379
+ {861, "CP-IS"},
380
+ {861, "CP861"},
381
+ {861, "IBM861"},
382
+ {861, "CSIBM861"},
383
+
384
+ {863, "863"},
385
+ {863, "CP863"},
386
+ {863, "IBM863"},
387
+ {863, "CSIBM863"},
388
+
389
+ {864, "CP864"},
390
+ {864, "IBM864"},
391
+ {864, "CSIBM864"},
392
+
393
+ {865, "865"},
394
+ {865, "CP865"},
395
+ {865, "IBM865"},
396
+ {865, "CSIBM865"},
397
+
398
+ {869, "869"},
399
+ {869, "CP-GR"},
400
+ {869, "CP869"},
401
+ {869, "IBM869"},
402
+ {869, "CSIBM869"},
403
+
404
+ /* !IsValidCodePage(1152) */
405
+ {1125, "CP1125"},
406
+
407
+ /*
408
+ * Code Page Identifiers
409
+ * http://msdn2.microsoft.com/en-us/library/ms776446.aspx
410
+ */
411
+ {37, "IBM037"}, /* IBM EBCDIC US-Canada */
412
+ {437, "IBM437"}, /* OEM United States */
413
+ {500, "IBM500"}, /* IBM EBCDIC International */
414
+ {708, "ASMO-708"}, /* Arabic (ASMO 708) */
415
+ /* 709 Arabic (ASMO-449+, BCON V4) */
416
+ /* 710 Arabic - Transparent Arabic */
417
+ {720, "DOS-720"}, /* Arabic (Transparent ASMO); Arabic (DOS) */
418
+ {737, "ibm737"}, /* OEM Greek (formerly 437G); Greek (DOS) */
419
+ {775, "ibm775"}, /* OEM Baltic; Baltic (DOS) */
420
+ {850, "ibm850"}, /* OEM Multilingual Latin 1; Western European (DOS) */
421
+ {852, "ibm852"}, /* OEM Latin 2; Central European (DOS) */
422
+ {855, "IBM855"}, /* OEM Cyrillic (primarily Russian) */
423
+ {857, "ibm857"}, /* OEM Turkish; Turkish (DOS) */
424
+ {858, "IBM00858"}, /* OEM Multilingual Latin 1 + Euro symbol */
425
+ {860, "IBM860"}, /* OEM Portuguese; Portuguese (DOS) */
426
+ {861, "ibm861"}, /* OEM Icelandic; Icelandic (DOS) */
427
+ {862, "DOS-862"}, /* OEM Hebrew; Hebrew (DOS) */
428
+ {863, "IBM863"}, /* OEM French Canadian; French Canadian (DOS) */
429
+ {864, "IBM864"}, /* OEM Arabic; Arabic (864) */
430
+ {865, "IBM865"}, /* OEM Nordic; Nordic (DOS) */
431
+ {866, "cp866"}, /* OEM Russian; Cyrillic (DOS) */
432
+ {869, "ibm869"}, /* OEM Modern Greek; Greek, Modern (DOS) */
433
+ {870, "IBM870"}, /* IBM EBCDIC Multilingual/ROECE (Latin 2); IBM EBCDIC Multilingual Latin 2 */
434
+ {874, "windows-874"}, /* ANSI/OEM Thai (same as 28605, ISO 8859-15); Thai (Windows) */
435
+ {875, "cp875"}, /* IBM EBCDIC Greek Modern */
436
+ {932, "shift_jis"}, /* ANSI/OEM Japanese; Japanese (Shift-JIS) */
437
+ {932, "shift-jis"}, /* alternative name for it */
438
+ {936, "gb2312"}, /* ANSI/OEM Simplified Chinese (PRC, Singapore); Chinese Simplified (GB2312) */
439
+ {949, "ks_c_5601-1987"}, /* ANSI/OEM Korean (Unified Hangul Code) */
440
+ {950, "big5"}, /* ANSI/OEM Traditional Chinese (Taiwan; Hong Kong SAR, PRC); Chinese Traditional (Big5) */
441
+ {950, "big5hkscs"}, /* ANSI/OEM Traditional Chinese (Hong Kong SAR); Chinese Traditional (Big5-HKSCS) */
442
+ {950, "big5-hkscs"}, /* alternative name for it */
443
+ {1026, "IBM1026"}, /* IBM EBCDIC Turkish (Latin 5) */
444
+ {1047, "IBM01047"}, /* IBM EBCDIC Latin 1/Open System */
445
+ {1140, "IBM01140"}, /* IBM EBCDIC US-Canada (037 + Euro symbol); IBM EBCDIC (US-Canada-Euro) */
446
+ {1141, "IBM01141"}, /* IBM EBCDIC Germany (20273 + Euro symbol); IBM EBCDIC (Germany-Euro) */
447
+ {1142, "IBM01142"}, /* IBM EBCDIC Denmark-Norway (20277 + Euro symbol); IBM EBCDIC (Denmark-Norway-Euro) */
448
+ {1143, "IBM01143"}, /* IBM EBCDIC Finland-Sweden (20278 + Euro symbol); IBM EBCDIC (Finland-Sweden-Euro) */
449
+ {1144, "IBM01144"}, /* IBM EBCDIC Italy (20280 + Euro symbol); IBM EBCDIC (Italy-Euro) */
450
+ {1145, "IBM01145"}, /* IBM EBCDIC Latin America-Spain (20284 + Euro symbol); IBM EBCDIC (Spain-Euro) */
451
+ {1146, "IBM01146"}, /* IBM EBCDIC United Kingdom (20285 + Euro symbol); IBM EBCDIC (UK-Euro) */
452
+ {1147, "IBM01147"}, /* IBM EBCDIC France (20297 + Euro symbol); IBM EBCDIC (France-Euro) */
453
+ {1148, "IBM01148"}, /* IBM EBCDIC International (500 + Euro symbol); IBM EBCDIC (International-Euro) */
454
+ {1149, "IBM01149"}, /* IBM EBCDIC Icelandic (20871 + Euro symbol); IBM EBCDIC (Icelandic-Euro) */
455
+ {1250, "windows-1250"}, /* ANSI Central European; Central European (Windows) */
456
+ {1251, "windows-1251"}, /* ANSI Cyrillic; Cyrillic (Windows) */
457
+ {1252, "windows-1252"}, /* ANSI Latin 1; Western European (Windows) */
458
+ {1253, "windows-1253"}, /* ANSI Greek; Greek (Windows) */
459
+ {1254, "windows-1254"}, /* ANSI Turkish; Turkish (Windows) */
460
+ {1255, "windows-1255"}, /* ANSI Hebrew; Hebrew (Windows) */
461
+ {1256, "windows-1256"}, /* ANSI Arabic; Arabic (Windows) */
462
+ {1257, "windows-1257"}, /* ANSI Baltic; Baltic (Windows) */
463
+ {1258, "windows-1258"}, /* ANSI/OEM Vietnamese; Vietnamese (Windows) */
464
+ {1361, "Johab"}, /* Korean (Johab) */
465
+ {10000, "macintosh"}, /* MAC Roman; Western European (Mac) */
466
+ {10001, "x-mac-japanese"}, /* Japanese (Mac) */
467
+ {10002, "x-mac-chinesetrad"}, /* MAC Traditional Chinese (Big5); Chinese Traditional (Mac) */
468
+ {10003, "x-mac-korean"}, /* Korean (Mac) */
469
+ {10004, "x-mac-arabic"}, /* Arabic (Mac) */
470
+ {10005, "x-mac-hebrew"}, /* Hebrew (Mac) */
471
+ {10006, "x-mac-greek"}, /* Greek (Mac) */
472
+ {10007, "x-mac-cyrillic"}, /* Cyrillic (Mac) */
473
+ {10008, "x-mac-chinesesimp"}, /* MAC Simplified Chinese (GB 2312); Chinese Simplified (Mac) */
474
+ {10010, "x-mac-romanian"}, /* Romanian (Mac) */
475
+ {10017, "x-mac-ukrainian"}, /* Ukrainian (Mac) */
476
+ {10021, "x-mac-thai"}, /* Thai (Mac) */
477
+ {10029, "x-mac-ce"}, /* MAC Latin 2; Central European (Mac) */
478
+ {10079, "x-mac-icelandic"}, /* Icelandic (Mac) */
479
+ {10081, "x-mac-turkish"}, /* Turkish (Mac) */
480
+ {10082, "x-mac-croatian"}, /* Croatian (Mac) */
481
+ {20000, "x-Chinese_CNS"}, /* CNS Taiwan; Chinese Traditional (CNS) */
482
+ {20001, "x-cp20001"}, /* TCA Taiwan */
483
+ {20002, "x_Chinese-Eten"}, /* Eten Taiwan; Chinese Traditional (Eten) */
484
+ {20003, "x-cp20003"}, /* IBM5550 Taiwan */
485
+ {20004, "x-cp20004"}, /* TeleText Taiwan */
486
+ {20005, "x-cp20005"}, /* Wang Taiwan */
487
+ {20105, "x-IA5"}, /* IA5 (IRV International Alphabet No. 5, 7-bit); Western European (IA5) */
488
+ {20106, "x-IA5-German"}, /* IA5 German (7-bit) */
489
+ {20107, "x-IA5-Swedish"}, /* IA5 Swedish (7-bit) */
490
+ {20108, "x-IA5-Norwegian"}, /* IA5 Norwegian (7-bit) */
491
+ {20127, "us-ascii"}, /* US-ASCII (7-bit) */
492
+ {20261, "x-cp20261"}, /* T.61 */
493
+ {20269, "x-cp20269"}, /* ISO 6937 Non-Spacing Accent */
494
+ {20273, "IBM273"}, /* IBM EBCDIC Germany */
495
+ {20277, "IBM277"}, /* IBM EBCDIC Denmark-Norway */
496
+ {20278, "IBM278"}, /* IBM EBCDIC Finland-Sweden */
497
+ {20280, "IBM280"}, /* IBM EBCDIC Italy */
498
+ {20284, "IBM284"}, /* IBM EBCDIC Latin America-Spain */
499
+ {20285, "IBM285"}, /* IBM EBCDIC United Kingdom */
500
+ {20290, "IBM290"}, /* IBM EBCDIC Japanese Katakana Extended */
501
+ {20297, "IBM297"}, /* IBM EBCDIC France */
502
+ {20420, "IBM420"}, /* IBM EBCDIC Arabic */
503
+ {20423, "IBM423"}, /* IBM EBCDIC Greek */
504
+ {20424, "IBM424"}, /* IBM EBCDIC Hebrew */
505
+ {20833, "x-EBCDIC-KoreanExtended"}, /* IBM EBCDIC Korean Extended */
506
+ {20838, "IBM-Thai"}, /* IBM EBCDIC Thai */
507
+ {20866, "koi8-r"}, /* Russian (KOI8-R); Cyrillic (KOI8-R) */
508
+ {20871, "IBM871"}, /* IBM EBCDIC Icelandic */
509
+ {20880, "IBM880"}, /* IBM EBCDIC Cyrillic Russian */
510
+ {20905, "IBM905"}, /* IBM EBCDIC Turkish */
511
+ {20924, "IBM00924"}, /* IBM EBCDIC Latin 1/Open System (1047 + Euro symbol) */
512
+ {20932, "EUC-JP"}, /* Japanese (JIS 0208-1990 and 0121-1990) */
513
+ {20936, "x-cp20936"}, /* Simplified Chinese (GB2312); Chinese Simplified (GB2312-80) */
514
+ {20949, "x-cp20949"}, /* Korean Wansung */
515
+ {21025, "cp1025"}, /* IBM EBCDIC Cyrillic Serbian-Bulgarian */
516
+ /* 21027 (deprecated) */
517
+ {21866, "koi8-u"}, /* Ukrainian (KOI8-U); Cyrillic (KOI8-U) */
518
+ {28591, "iso-8859-1"}, /* ISO 8859-1 Latin 1; Western European (ISO) */
519
+ {28591, "iso8859-1"}, /* ISO 8859-1 Latin 1; Western European (ISO) */
520
+ {28592, "iso-8859-2"}, /* ISO 8859-2 Central European; Central European (ISO) */
521
+ {28592, "iso8859-2"}, /* ISO 8859-2 Central European; Central European (ISO) */
522
+ {28593, "iso-8859-3"}, /* ISO 8859-3 Latin 3 */
523
+ {28593, "iso8859-3"}, /* ISO 8859-3 Latin 3 */
524
+ {28594, "iso-8859-4"}, /* ISO 8859-4 Baltic */
525
+ {28594, "iso8859-4"}, /* ISO 8859-4 Baltic */
526
+ {28595, "iso-8859-5"}, /* ISO 8859-5 Cyrillic */
527
+ {28595, "iso8859-5"}, /* ISO 8859-5 Cyrillic */
528
+ {28596, "iso-8859-6"}, /* ISO 8859-6 Arabic */
529
+ {28596, "iso8859-6"}, /* ISO 8859-6 Arabic */
530
+ {28597, "iso-8859-7"}, /* ISO 8859-7 Greek */
531
+ {28597, "iso8859-7"}, /* ISO 8859-7 Greek */
532
+ {28598, "iso-8859-8"}, /* ISO 8859-8 Hebrew; Hebrew (ISO-Visual) */
533
+ {28598, "iso8859-8"}, /* ISO 8859-8 Hebrew; Hebrew (ISO-Visual) */
534
+ {28599, "iso-8859-9"}, /* ISO 8859-9 Turkish */
535
+ {28599, "iso8859-9"}, /* ISO 8859-9 Turkish */
536
+ {28603, "iso-8859-13"}, /* ISO 8859-13 Estonian */
537
+ {28603, "iso8859-13"}, /* ISO 8859-13 Estonian */
538
+ {28605, "iso-8859-15"}, /* ISO 8859-15 Latin 9 */
539
+ {28605, "iso8859-15"}, /* ISO 8859-15 Latin 9 */
540
+ {29001, "x-Europa"}, /* Europa 3 */
541
+ {38598, "iso-8859-8-i"}, /* ISO 8859-8 Hebrew; Hebrew (ISO-Logical) */
542
+ {38598, "iso8859-8-i"}, /* ISO 8859-8 Hebrew; Hebrew (ISO-Logical) */
543
+ {50220, "iso-2022-jp"}, /* ISO 2022 Japanese with no halfwidth Katakana; Japanese (JIS) */
544
+ {50221, "csISO2022JP"}, /* ISO 2022 Japanese with halfwidth Katakana; Japanese (JIS-Allow 1 byte Kana) */
545
+ {50222, "iso-2022-jp"}, /* ISO 2022 Japanese JIS X 0201-1989; Japanese (JIS-Allow 1 byte Kana - SO/SI) */
546
+ {50225, "iso-2022-kr"}, /* ISO 2022 Korean */
547
+ {50225, "iso2022-kr"}, /* ISO 2022 Korean */
548
+ {50227, "x-cp50227"}, /* ISO 2022 Simplified Chinese; Chinese Simplified (ISO 2022) */
549
+ /* 50229 ISO 2022 Traditional Chinese */
550
+ /* 50930 EBCDIC Japanese (Katakana) Extended */
551
+ /* 50931 EBCDIC US-Canada and Japanese */
552
+ /* 50933 EBCDIC Korean Extended and Korean */
553
+ /* 50935 EBCDIC Simplified Chinese Extended and Simplified Chinese */
554
+ /* 50936 EBCDIC Simplified Chinese */
555
+ /* 50937 EBCDIC US-Canada and Traditional Chinese */
556
+ /* 50939 EBCDIC Japanese (Latin) Extended and Japanese */
557
+ {51932, "euc-jp"}, /* EUC Japanese */
558
+ {51936, "EUC-CN"}, /* EUC Simplified Chinese; Chinese Simplified (EUC) */
559
+ {51949, "euc-kr"}, /* EUC Korean */
560
+ /* 51950 EUC Traditional Chinese */
561
+ {52936, "hz-gb-2312"}, /* HZ-GB2312 Simplified Chinese; Chinese Simplified (HZ) */
562
+ {54936, "GB18030"}, /* Windows XP and later: GB18030 Simplified Chinese (4 byte); Chinese Simplified (GB18030) */
563
+ {57002, "x-iscii-de"}, /* ISCII Devanagari */
564
+ {57003, "x-iscii-be"}, /* ISCII Bengali */
565
+ {57004, "x-iscii-ta"}, /* ISCII Tamil */
566
+ {57005, "x-iscii-te"}, /* ISCII Telugu */
567
+ {57006, "x-iscii-as"}, /* ISCII Assamese */
568
+ {57007, "x-iscii-or"}, /* ISCII Oriya */
569
+ {57008, "x-iscii-ka"}, /* ISCII Kannada */
570
+ {57009, "x-iscii-ma"}, /* ISCII Malayalam */
571
+ {57010, "x-iscii-gu"}, /* ISCII Gujarati */
572
+ {57011, "x-iscii-pa"}, /* ISCII Punjabi */
573
+
574
+ {0, NULL}
575
+ };
576
+
577
+ /*
578
+ * SJIS SHIFTJIS table CP932 table
579
+ * ---- --------------------------- --------------------------------
580
+ * 5C U+00A5 YEN SIGN U+005C REVERSE SOLIDUS
581
+ * 7E U+203E OVERLINE U+007E TILDE
582
+ * 815C U+2014 EM DASH U+2015 HORIZONTAL BAR
583
+ * 815F U+005C REVERSE SOLIDUS U+FF3C FULLWIDTH REVERSE SOLIDUS
584
+ * 8160 U+301C WAVE DASH U+FF5E FULLWIDTH TILDE
585
+ * 8161 U+2016 DOUBLE VERTICAL LINE U+2225 PARALLEL TO
586
+ * 817C U+2212 MINUS SIGN U+FF0D FULLWIDTH HYPHEN-MINUS
587
+ * 8191 U+00A2 CENT SIGN U+FFE0 FULLWIDTH CENT SIGN
588
+ * 8192 U+00A3 POUND SIGN U+FFE1 FULLWIDTH POUND SIGN
589
+ * 81CA U+00AC NOT SIGN U+FFE2 FULLWIDTH NOT SIGN
590
+ *
591
+ * EUC-JP and ISO-2022-JP should be compatible with CP932.
592
+ *
593
+ * Kernel and MLang have different Unicode mapping table. Make sure
594
+ * which API is used.
595
+ */
596
+ static compat_t cp932_compat[] = {
597
+ {0x00A5, 0x005C, COMPAT_OUT},
598
+ {0x203E, 0x007E, COMPAT_OUT},
599
+ {0x2014, 0x2015, COMPAT_OUT},
600
+ {0x301C, 0xFF5E, COMPAT_OUT},
601
+ {0x2016, 0x2225, COMPAT_OUT},
602
+ {0x2212, 0xFF0D, COMPAT_OUT},
603
+ {0x00A2, 0xFFE0, COMPAT_OUT},
604
+ {0x00A3, 0xFFE1, COMPAT_OUT},
605
+ {0x00AC, 0xFFE2, COMPAT_OUT},
606
+ {0, 0, 0}
607
+ };
608
+
609
+ static compat_t cp20932_compat[] = {
610
+ {0x00A5, 0x005C, COMPAT_OUT},
611
+ {0x203E, 0x007E, COMPAT_OUT},
612
+ {0x2014, 0x2015, COMPAT_OUT},
613
+ {0xFF5E, 0x301C, COMPAT_OUT|COMPAT_IN},
614
+ {0x2225, 0x2016, COMPAT_OUT|COMPAT_IN},
615
+ {0xFF0D, 0x2212, COMPAT_OUT|COMPAT_IN},
616
+ {0xFFE0, 0x00A2, COMPAT_OUT|COMPAT_IN},
617
+ {0xFFE1, 0x00A3, COMPAT_OUT|COMPAT_IN},
618
+ {0xFFE2, 0x00AC, COMPAT_OUT|COMPAT_IN},
619
+ {0, 0, 0}
620
+ };
621
+
622
+ static compat_t *cp51932_compat = cp932_compat;
623
+
624
+ /* cp20932_compat for kernel. cp932_compat for mlang. */
625
+ static compat_t *cp5022x_compat = cp932_compat;
626
+
627
+ typedef HRESULT (WINAPI *CONVERTINETSTRING)(
628
+ LPDWORD lpdwMode,
629
+ DWORD dwSrcEncoding,
630
+ DWORD dwDstEncoding,
631
+ LPCSTR lpSrcStr,
632
+ LPINT lpnSrcSize,
633
+ LPBYTE lpDstStr,
634
+ LPINT lpnDstSize
635
+ );
636
+ typedef HRESULT (WINAPI *CONVERTINETMULTIBYTETOUNICODE)(
637
+ LPDWORD lpdwMode,
638
+ DWORD dwSrcEncoding,
639
+ LPCSTR lpSrcStr,
640
+ LPINT lpnMultiCharCount,
641
+ LPWSTR lpDstStr,
642
+ LPINT lpnWideCharCount
643
+ );
644
+ typedef HRESULT (WINAPI *CONVERTINETUNICODETOMULTIBYTE)(
645
+ LPDWORD lpdwMode,
646
+ DWORD dwEncoding,
647
+ LPCWSTR lpSrcStr,
648
+ LPINT lpnWideCharCount,
649
+ LPSTR lpDstStr,
650
+ LPINT lpnMultiCharCount
651
+ );
652
+ typedef HRESULT (WINAPI *ISCONVERTINETSTRINGAVAILABLE)(
653
+ DWORD dwSrcEncoding,
654
+ DWORD dwDstEncoding
655
+ );
656
+ typedef HRESULT (WINAPI *LCIDTORFC1766A)(
657
+ LCID Locale,
658
+ LPSTR pszRfc1766,
659
+ int nChar
660
+ );
661
+ typedef HRESULT (WINAPI *LCIDTORFC1766W)(
662
+ LCID Locale,
663
+ LPWSTR pszRfc1766,
664
+ int nChar
665
+ );
666
+ typedef HRESULT (WINAPI *RFC1766TOLCIDA)(
667
+ LCID *pLocale,
668
+ LPSTR pszRfc1766
669
+ );
670
+ typedef HRESULT (WINAPI *RFC1766TOLCIDW)(
671
+ LCID *pLocale,
672
+ LPWSTR pszRfc1766
673
+ );
674
+ static CONVERTINETSTRING ConvertINetString;
675
+ static CONVERTINETMULTIBYTETOUNICODE ConvertINetMultiByteToUnicode;
676
+ static CONVERTINETUNICODETOMULTIBYTE ConvertINetUnicodeToMultiByte;
677
+ static ISCONVERTINETSTRINGAVAILABLE IsConvertINetStringAvailable;
678
+ static LCIDTORFC1766A LcidToRfc1766A;
679
+ static RFC1766TOLCIDA Rfc1766ToLcidA;
680
+
681
+ static int
682
+ load_mlang()
683
+ {
684
+ HMODULE h;
685
+ if (ConvertINetString != NULL)
686
+ return TRUE;
687
+ h = LoadLibrary(TEXT("mlang.dll"));
688
+ if (!h)
689
+ return FALSE;
690
+ ConvertINetString = (CONVERTINETSTRING)GetProcAddressA(h, "ConvertINetString");
691
+ ConvertINetMultiByteToUnicode = (CONVERTINETMULTIBYTETOUNICODE)GetProcAddressA(h, "ConvertINetMultiByteToUnicode");
692
+ ConvertINetUnicodeToMultiByte = (CONVERTINETUNICODETOMULTIBYTE)GetProcAddressA(h, "ConvertINetUnicodeToMultiByte");
693
+ IsConvertINetStringAvailable = (ISCONVERTINETSTRINGAVAILABLE)GetProcAddressA(h, "IsConvertINetStringAvailable");
694
+ LcidToRfc1766A = (LCIDTORFC1766A)GetProcAddressA(h, "LcidToRfc1766A");
695
+ Rfc1766ToLcidA = (RFC1766TOLCIDA)GetProcAddressA(h, "Rfc1766ToLcidA");
696
+ return TRUE;
697
+ }
698
+
699
+ iconv_t
700
+ iconv_open(const char *tocode, const char *fromcode)
701
+ {
702
+ rec_iconv_t *cd;
703
+
704
+ cd = (rec_iconv_t *)calloc(1, sizeof(rec_iconv_t));
705
+ if (cd == NULL)
706
+ return (iconv_t)(-1);
707
+
708
+ #if defined(USE_LIBICONV_DLL)
709
+ errno = 0;
710
+ if (libiconv_iconv_open(cd, tocode, fromcode))
711
+ return (iconv_t)cd;
712
+ #endif
713
+
714
+ /* reset the errno to prevent reporting wrong error code.
715
+ * 0 for unsorted error. */
716
+ errno = 0;
717
+ if (win_iconv_open(cd, tocode, fromcode))
718
+ return (iconv_t)cd;
719
+
720
+ free(cd);
721
+
722
+ return (iconv_t)(-1);
723
+ }
724
+
725
+ int
726
+ iconv_close(iconv_t _cd)
727
+ {
728
+ rec_iconv_t *cd = (rec_iconv_t *)_cd;
729
+ int r = cd->iconv_close(cd->cd);
730
+ int e = *(cd->_errno());
731
+ #if defined(USE_LIBICONV_DLL)
732
+ if (cd->hlibiconv != NULL)
733
+ FreeLibrary(cd->hlibiconv);
734
+ #endif
735
+ free(cd);
736
+ errno = e;
737
+ return r;
738
+ }
739
+
740
+ size_t
741
+ iconv(iconv_t _cd, char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
742
+ {
743
+ rec_iconv_t *cd = (rec_iconv_t *)_cd;
744
+ size_t r = cd->iconv(cd->cd, inbuf, inbytesleft, outbuf, outbytesleft);
745
+ errno = *(cd->_errno());
746
+ return r;
747
+ }
748
+
749
+ static int
750
+ win_iconv_open(rec_iconv_t *cd, const char *tocode, const char *fromcode)
751
+ {
752
+ if (!make_csconv(fromcode, &cd->from) || !make_csconv(tocode, &cd->to))
753
+ return FALSE;
754
+ cd->iconv_close = win_iconv_close;
755
+ cd->iconv = win_iconv;
756
+ cd->_errno = _errno;
757
+ cd->cd = (iconv_t)cd;
758
+ return TRUE;
759
+ }
760
+
761
+ static int
762
+ win_iconv_close(iconv_t cd)
763
+ {
764
+ return 0;
765
+ }
766
+
767
+ static size_t
768
+ win_iconv(iconv_t _cd, char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
769
+ {
770
+ rec_iconv_t *cd = (rec_iconv_t *)_cd;
771
+ ushort wbuf[MB_CHAR_MAX]; /* enough room for one character */
772
+ int insize;
773
+ int outsize;
774
+ int wsize;
775
+ DWORD frommode;
776
+ DWORD tomode;
777
+ uint wc;
778
+ compat_t *cp;
779
+ int i;
780
+
781
+ if (inbuf == NULL || *inbuf == NULL)
782
+ {
783
+ if (outbuf != NULL && *outbuf != NULL && cd->to.flush != NULL)
784
+ {
785
+ tomode = cd->to.mode;
786
+ outsize = cd->to.flush(&cd->to, (uchar *)*outbuf, *outbytesleft);
787
+ if (outsize == -1)
788
+ {
789
+ if ((cd->to.flags & FLAG_IGNORE) && errno != E2BIG)
790
+ {
791
+ outsize = 0;
792
+ }
793
+ else
794
+ {
795
+ cd->to.mode = tomode;
796
+ return (size_t)(-1);
797
+ }
798
+ }
799
+ *outbuf += outsize;
800
+ *outbytesleft -= outsize;
801
+ }
802
+ cd->from.mode = 0;
803
+ cd->to.mode = 0;
804
+ return 0;
805
+ }
806
+
807
+ while (*inbytesleft != 0)
808
+ {
809
+ frommode = cd->from.mode;
810
+ tomode = cd->to.mode;
811
+ wsize = MB_CHAR_MAX;
812
+
813
+ insize = cd->from.mbtowc(&cd->from, (const uchar *)*inbuf, *inbytesleft, wbuf, &wsize);
814
+ if (insize == -1)
815
+ {
816
+ if (cd->to.flags & FLAG_IGNORE)
817
+ {
818
+ cd->from.mode = frommode;
819
+ insize = 1;
820
+ wsize = 0;
821
+ }
822
+ else
823
+ {
824
+ cd->from.mode = frommode;
825
+ return (size_t)(-1);
826
+ }
827
+ }
828
+
829
+ if (wsize == 0)
830
+ {
831
+ *inbuf += insize;
832
+ *inbytesleft -= insize;
833
+ continue;
834
+ }
835
+
836
+ if (cd->from.compat != NULL)
837
+ {
838
+ wc = utf16_to_ucs4(wbuf);
839
+ cp = cd->from.compat;
840
+ for (i = 0; cp[i].in != 0; ++i)
841
+ {
842
+ if ((cp[i].flag & COMPAT_IN) && cp[i].out == wc)
843
+ {
844
+ ucs4_to_utf16(cp[i].in, wbuf, &wsize);
845
+ break;
846
+ }
847
+ }
848
+ }
849
+
850
+ if (cd->to.compat != NULL)
851
+ {
852
+ wc = utf16_to_ucs4(wbuf);
853
+ cp = cd->to.compat;
854
+ for (i = 0; cp[i].in != 0; ++i)
855
+ {
856
+ if ((cp[i].flag & COMPAT_OUT) && cp[i].in == wc)
857
+ {
858
+ ucs4_to_utf16(cp[i].out, wbuf, &wsize);
859
+ break;
860
+ }
861
+ }
862
+ }
863
+
864
+ outsize = cd->to.wctomb(&cd->to, wbuf, wsize, (uchar *)*outbuf, *outbytesleft);
865
+ if (outsize == -1)
866
+ {
867
+ if ((cd->to.flags & FLAG_IGNORE) && errno != E2BIG)
868
+ {
869
+ cd->to.mode = tomode;
870
+ outsize = 0;
871
+ }
872
+ else
873
+ {
874
+ cd->from.mode = frommode;
875
+ cd->to.mode = tomode;
876
+ return (size_t)(-1);
877
+ }
878
+ }
879
+
880
+ *inbuf += insize;
881
+ *outbuf += outsize;
882
+ *inbytesleft -= insize;
883
+ *outbytesleft -= outsize;
884
+ }
885
+
886
+ return 0;
887
+ }
888
+
889
+ static int
890
+ make_csconv(const char *_name, csconv_t *cv)
891
+ {
892
+ CPINFO cpinfo;
893
+ int use_compat = TRUE;
894
+ int flag = 0;
895
+ char *name;
896
+ char *p;
897
+
898
+ name = xstrndup(_name, strlen(_name));
899
+ if (name == NULL)
900
+ return FALSE;
901
+
902
+ /* check for option "enc_name//opt1//opt2" */
903
+ while ((p = strrstr(name, "//")) != NULL)
904
+ {
905
+ if (_stricmp(p + 2, "nocompat") == 0)
906
+ use_compat = FALSE;
907
+ else if (_stricmp(p + 2, "translit") == 0)
908
+ flag |= FLAG_TRANSLIT;
909
+ else if (_stricmp(p + 2, "ignore") == 0)
910
+ flag |= FLAG_IGNORE;
911
+ *p = 0;
912
+ }
913
+
914
+ cv->mode = 0;
915
+ cv->flags = flag;
916
+ cv->mblen = NULL;
917
+ cv->flush = NULL;
918
+ cv->compat = NULL;
919
+ cv->codepage = name_to_codepage(name);
920
+ if (cv->codepage == 1200 || cv->codepage == 1201)
921
+ {
922
+ cv->mbtowc = utf16_mbtowc;
923
+ cv->wctomb = utf16_wctomb;
924
+ if (_stricmp(name, "UTF-16") == 0 || _stricmp(name, "UTF16") == 0 ||
925
+ _stricmp(name, "UCS-2") == 0 || _stricmp(name, "UCS2") == 0)
926
+ cv->flags |= FLAG_USE_BOM;
927
+ }
928
+ else if (cv->codepage == 12000 || cv->codepage == 12001)
929
+ {
930
+ cv->mbtowc = utf32_mbtowc;
931
+ cv->wctomb = utf32_wctomb;
932
+ if (_stricmp(name, "UTF-32") == 0 || _stricmp(name, "UTF32") == 0 ||
933
+ _stricmp(name, "UCS-4") == 0 || _stricmp(name, "UCS4") == 0)
934
+ cv->flags |= FLAG_USE_BOM;
935
+ }
936
+ else if (cv->codepage == 65001)
937
+ {
938
+ cv->mbtowc = kernel_mbtowc;
939
+ cv->wctomb = kernel_wctomb;
940
+ cv->mblen = utf8_mblen;
941
+ }
942
+ else if ((cv->codepage == 50220 || cv->codepage == 50221 || cv->codepage == 50222) && load_mlang())
943
+ {
944
+ cv->mbtowc = iso2022jp_mbtowc;
945
+ cv->wctomb = iso2022jp_wctomb;
946
+ cv->flush = iso2022jp_flush;
947
+ }
948
+ else if (cv->codepage == 51932 && load_mlang())
949
+ {
950
+ cv->mbtowc = mlang_mbtowc;
951
+ cv->wctomb = mlang_wctomb;
952
+ cv->mblen = eucjp_mblen;
953
+ }
954
+ else if (IsValidCodePage(cv->codepage)
955
+ && GetCPInfo(cv->codepage, &cpinfo) != 0)
956
+ {
957
+ cv->mbtowc = kernel_mbtowc;
958
+ cv->wctomb = kernel_wctomb;
959
+ if (cpinfo.MaxCharSize == 1)
960
+ cv->mblen = sbcs_mblen;
961
+ else if (cpinfo.MaxCharSize == 2)
962
+ cv->mblen = dbcs_mblen;
963
+ else
964
+ cv->mblen = mbcs_mblen;
965
+ }
966
+ else
967
+ {
968
+ /* not supported */
969
+ free(name);
970
+ errno = EINVAL;
971
+ return FALSE;
972
+ }
973
+
974
+ if (use_compat)
975
+ {
976
+ switch (cv->codepage)
977
+ {
978
+ case 932: cv->compat = cp932_compat; break;
979
+ case 20932: cv->compat = cp20932_compat; break;
980
+ case 51932: cv->compat = cp51932_compat; break;
981
+ case 50220: case 50221: case 50222: cv->compat = cp5022x_compat; break;
982
+ }
983
+ }
984
+
985
+ free(name);
986
+
987
+ return TRUE;
988
+ }
989
+
990
+ static int
991
+ name_to_codepage(const char *name)
992
+ {
993
+ int i;
994
+
995
+ if (*name == '\0' ||
996
+ strcmp(name, "char") == 0)
997
+ return GetACP();
998
+ else if (strcmp(name, "wchar_t") == 0)
999
+ return 1200;
1000
+ else if (_strnicmp(name, "cp", 2) == 0)
1001
+ return atoi(name + 2); /* CP123 */
1002
+ else if ('0' <= name[0] && name[0] <= '9')
1003
+ return atoi(name); /* 123 */
1004
+ else if (_strnicmp(name, "xx", 2) == 0)
1005
+ return atoi(name + 2); /* XX123 for debug */
1006
+
1007
+ for (i = 0; codepage_alias[i].name != NULL; ++i)
1008
+ if (_stricmp(name, codepage_alias[i].name) == 0)
1009
+ return codepage_alias[i].codepage;
1010
+ return -1;
1011
+ }
1012
+
1013
+ /*
1014
+ * http://www.faqs.org/rfcs/rfc2781.html
1015
+ */
1016
+ static uint
1017
+ utf16_to_ucs4(const ushort *wbuf)
1018
+ {
1019
+ uint wc = wbuf[0];
1020
+ if (0xD800 <= wbuf[0] && wbuf[0] <= 0xDBFF)
1021
+ wc = ((wbuf[0] & 0x3FF) << 10) + (wbuf[1] & 0x3FF) + 0x10000;
1022
+ return wc;
1023
+ }
1024
+
1025
+ static void
1026
+ ucs4_to_utf16(uint wc, ushort *wbuf, int *wbufsize)
1027
+ {
1028
+ if (wc < 0x10000)
1029
+ {
1030
+ wbuf[0] = wc;
1031
+ *wbufsize = 1;
1032
+ }
1033
+ else
1034
+ {
1035
+ wc -= 0x10000;
1036
+ wbuf[0] = 0xD800 | ((wc >> 10) & 0x3FF);
1037
+ wbuf[1] = 0xDC00 | (wc & 0x3FF);
1038
+ *wbufsize = 2;
1039
+ }
1040
+ }
1041
+
1042
+ /*
1043
+ * Check if codepage is one of those for which the dwFlags parameter
1044
+ * to MultiByteToWideChar() must be zero. Return zero or
1045
+ * MB_ERR_INVALID_CHARS. The docs in Platform SDK for for Windows
1046
+ * Server 2003 R2 claims that also codepage 65001 is one of these, but
1047
+ * that doesn't seem to be the case. The MSDN docs for MSVS2008 leave
1048
+ * out 65001 (UTF-8), and that indeed seems to be the case on XP, it
1049
+ * works fine to pass MB_ERR_INVALID_CHARS in dwFlags when converting
1050
+ * from UTF-8.
1051
+ */
1052
+ static int
1053
+ mbtowc_flags(int codepage)
1054
+ {
1055
+ return (codepage == 50220 || codepage == 50221 ||
1056
+ codepage == 50222 || codepage == 50225 ||
1057
+ codepage == 50227 || codepage == 50229 ||
1058
+ codepage == 52936 || codepage == 54936 ||
1059
+ (codepage >= 57002 && codepage <= 57011) ||
1060
+ codepage == 65000 || codepage == 42) ? 0 : MB_ERR_INVALID_CHARS;
1061
+ }
1062
+
1063
+ /*
1064
+ * Check if codepage is one those for which the lpUsedDefaultChar
1065
+ * parameter to WideCharToMultiByte() must be NULL. The docs in
1066
+ * Platform SDK for for Windows Server 2003 R2 claims that this is the
1067
+ * list below, while the MSDN docs for MSVS2008 claim that it is only
1068
+ * for 65000 (UTF-7) and 65001 (UTF-8). This time the earlier Platform
1069
+ * SDK seems to be correct, at least for XP.
1070
+ */
1071
+ static int
1072
+ must_use_null_useddefaultchar(int codepage)
1073
+ {
1074
+ return (codepage == 65000 || codepage == 65001 ||
1075
+ codepage == 50220 || codepage == 50221 ||
1076
+ codepage == 50222 || codepage == 50225 ||
1077
+ codepage == 50227 || codepage == 50229 ||
1078
+ codepage == 52936 || codepage == 54936 ||
1079
+ (codepage >= 57002 && codepage <= 57011) ||
1080
+ codepage == 42);
1081
+ }
1082
+
1083
+ static char *
1084
+ strrstr(const char *str, const char *token)
1085
+ {
1086
+ int len = strlen(token);
1087
+ const char *p = str + strlen(str);
1088
+
1089
+ while (str <= --p)
1090
+ if (p[0] == token[0] && strncmp(p, token, len) == 0)
1091
+ return (char *)p;
1092
+ return NULL;
1093
+ }
1094
+
1095
+ static char *
1096
+ xstrndup(const char *s, size_t n)
1097
+ {
1098
+ char *p;
1099
+
1100
+ p = (char *)malloc(n + 1);
1101
+ if (p == NULL)
1102
+ return NULL;
1103
+ memcpy(p, s, n);
1104
+ p[n] = '\0';
1105
+ return p;
1106
+ }
1107
+
1108
+ static int
1109
+ seterror(int err)
1110
+ {
1111
+ errno = err;
1112
+ return -1;
1113
+ }
1114
+
1115
+ #if defined(USE_LIBICONV_DLL)
1116
+ static int
1117
+ libiconv_iconv_open(rec_iconv_t *cd, const char *tocode, const char *fromcode)
1118
+ {
1119
+ HMODULE hlibiconv = NULL;
1120
+ HMODULE hmsvcrt = NULL;
1121
+ char *dllname;
1122
+ const char *p;
1123
+ const char *e;
1124
+ f_iconv_open _iconv_open;
1125
+
1126
+ /*
1127
+ * always try to load dll, so that we can switch dll in runtime.
1128
+ */
1129
+
1130
+ /* XXX: getenv() can't get variable set by SetEnvironmentVariable() */
1131
+ p = getenv("WINICONV_LIBICONV_DLL");
1132
+ if (p == NULL)
1133
+ p = DEFAULT_LIBICONV_DLL;
1134
+ /* parse comma separated value */
1135
+ for ( ; *p != 0; p = (*e == ',') ? e + 1 : e)
1136
+ {
1137
+ e = strchr(p, ',');
1138
+ if (p == e)
1139
+ continue;
1140
+ else if (e == NULL)
1141
+ e = p + strlen(p);
1142
+ dllname = xstrndup(p, e - p);
1143
+ if (dllname == NULL)
1144
+ return FALSE;
1145
+ hlibiconv = LoadLibraryA(dllname);
1146
+ free(dllname);
1147
+ if (hlibiconv != NULL)
1148
+ {
1149
+ if (hlibiconv == hwiniconv)
1150
+ {
1151
+ FreeLibrary(hlibiconv);
1152
+ hlibiconv = NULL;
1153
+ continue;
1154
+ }
1155
+ break;
1156
+ }
1157
+ }
1158
+
1159
+ if (hlibiconv == NULL)
1160
+ goto failed;
1161
+
1162
+ hmsvcrt = find_imported_module_by_funcname(hlibiconv, "_errno");
1163
+ if (hmsvcrt == NULL)
1164
+ goto failed;
1165
+
1166
+ _iconv_open = (f_iconv_open)GetProcAddressA(hlibiconv, "libiconv_open");
1167
+ if (_iconv_open == NULL)
1168
+ _iconv_open = (f_iconv_open)GetProcAddressA(hlibiconv, "iconv_open");
1169
+ cd->iconv_close = (f_iconv_close)GetProcAddressA(hlibiconv, "libiconv_close");
1170
+ if (cd->iconv_close == NULL)
1171
+ cd->iconv_close = (f_iconv_close)GetProcAddressA(hlibiconv, "iconv_close");
1172
+ cd->iconv = (f_iconv)GetProcAddressA(hlibiconv, "libiconv");
1173
+ if (cd->iconv == NULL)
1174
+ cd->iconv = (f_iconv)GetProcAddressA(hlibiconv, "iconv");
1175
+ cd->_errno = (f_errno)GetProcAddressA(hmsvcrt, "_errno");
1176
+ if (_iconv_open == NULL || cd->iconv_close == NULL
1177
+ || cd->iconv == NULL || cd->_errno == NULL)
1178
+ goto failed;
1179
+
1180
+ cd->cd = _iconv_open(tocode, fromcode);
1181
+ if (cd->cd == (iconv_t)(-1))
1182
+ goto failed;
1183
+
1184
+ cd->hlibiconv = hlibiconv;
1185
+ return TRUE;
1186
+
1187
+ failed:
1188
+ if (hlibiconv != NULL)
1189
+ FreeLibrary(hlibiconv);
1190
+ /* do not free hmsvcrt which is obtained by GetModuleHandle() */
1191
+ return FALSE;
1192
+ }
1193
+
1194
+ /*
1195
+ * Reference:
1196
+ * http://forums.belution.com/ja/vc/000/234/78s.shtml
1197
+ * http://nienie.com/~masapico/api_ImageDirectoryEntryToData.html
1198
+ *
1199
+ * The formal way is
1200
+ * imagehlp.h or dbghelp.h
1201
+ * imagehlp.lib or dbghelp.lib
1202
+ * ImageDirectoryEntryToData()
1203
+ */
1204
+ #define TO_DOS_HEADER(base) ((PIMAGE_DOS_HEADER)(base))
1205
+ #define TO_NT_HEADERS(base) ((PIMAGE_NT_HEADERS)((LPBYTE)(base) + TO_DOS_HEADER(base)->e_lfanew))
1206
+ static PVOID
1207
+ MyImageDirectoryEntryToData(LPVOID Base, BOOLEAN MappedAsImage, USHORT DirectoryEntry, PULONG Size)
1208
+ {
1209
+ /* TODO: MappedAsImage? */
1210
+ PIMAGE_DATA_DIRECTORY p;
1211
+ p = TO_NT_HEADERS(Base)->OptionalHeader.DataDirectory + DirectoryEntry;
1212
+ if (p->VirtualAddress == 0) {
1213
+ *Size = 0;
1214
+ return NULL;
1215
+ }
1216
+ *Size = p->Size;
1217
+ return (PVOID)((LPBYTE)Base + p->VirtualAddress);
1218
+ }
1219
+
1220
+ static HMODULE
1221
+ find_imported_module_by_funcname(HMODULE hModule, const char *funcname)
1222
+ {
1223
+ DWORD_PTR Base;
1224
+ ULONG Size;
1225
+ PIMAGE_IMPORT_DESCRIPTOR Imp;
1226
+ PIMAGE_THUNK_DATA Name; /* Import Name Table */
1227
+ PIMAGE_IMPORT_BY_NAME ImpName;
1228
+
1229
+ Base = (DWORD_PTR)hModule;
1230
+ Imp = (PIMAGE_IMPORT_DESCRIPTOR)MyImageDirectoryEntryToData(
1231
+ (LPVOID)Base,
1232
+ TRUE,
1233
+ IMAGE_DIRECTORY_ENTRY_IMPORT,
1234
+ &Size);
1235
+ if (Imp == NULL)
1236
+ return NULL;
1237
+ for ( ; Imp->OriginalFirstThunk != 0; ++Imp)
1238
+ {
1239
+ Name = (PIMAGE_THUNK_DATA)(Base + Imp->OriginalFirstThunk);
1240
+ for ( ; Name->u1.Ordinal != 0; ++Name)
1241
+ {
1242
+ if (!IMAGE_SNAP_BY_ORDINAL(Name->u1.Ordinal))
1243
+ {
1244
+ ImpName = (PIMAGE_IMPORT_BY_NAME)
1245
+ (Base + (DWORD_PTR)Name->u1.AddressOfData);
1246
+ if (strcmp((char *)ImpName->Name, funcname) == 0)
1247
+ return GetModuleHandleA((char *)(Base + Imp->Name));
1248
+ }
1249
+ }
1250
+ }
1251
+ return NULL;
1252
+ }
1253
+ #endif
1254
+
1255
+ static int
1256
+ sbcs_mblen(csconv_t *cv, const uchar *buf, int bufsize)
1257
+ {
1258
+ return 1;
1259
+ }
1260
+
1261
+ static int
1262
+ dbcs_mblen(csconv_t *cv, const uchar *buf, int bufsize)
1263
+ {
1264
+ int len = IsDBCSLeadByteEx(cv->codepage, buf[0]) ? 2 : 1;
1265
+ if (bufsize < len)
1266
+ return seterror(EINVAL);
1267
+ return len;
1268
+ }
1269
+
1270
+ static int
1271
+ mbcs_mblen(csconv_t *cv, const uchar *buf, int bufsize)
1272
+ {
1273
+ int len = 0;
1274
+
1275
+ if (cv->codepage == 54936) {
1276
+ if (buf[0] <= 0x7F) len = 1;
1277
+ else if (buf[0] >= 0x81 && buf[0] <= 0xFE &&
1278
+ bufsize >= 2 &&
1279
+ ((buf[1] >= 0x40 && buf[1] <= 0x7E) ||
1280
+ (buf[1] >= 0x80 && buf[1] <= 0xFE))) len = 2;
1281
+ else if (buf[0] >= 0x81 && buf[0] <= 0xFE &&
1282
+ bufsize >= 4 &&
1283
+ buf[1] >= 0x30 && buf[1] <= 0x39) len = 4;
1284
+ else
1285
+ return seterror(EINVAL);
1286
+ return len;
1287
+ }
1288
+ else
1289
+ return seterror(EINVAL);
1290
+ }
1291
+
1292
+ static int
1293
+ utf8_mblen(csconv_t *cv, const uchar *buf, int bufsize)
1294
+ {
1295
+ int len = 0;
1296
+
1297
+ if (buf[0] < 0x80) len = 1;
1298
+ else if ((buf[0] & 0xE0) == 0xC0) len = 2;
1299
+ else if ((buf[0] & 0xF0) == 0xE0) len = 3;
1300
+ else if ((buf[0] & 0xF8) == 0xF0) len = 4;
1301
+ else if ((buf[0] & 0xFC) == 0xF8) len = 5;
1302
+ else if ((buf[0] & 0xFE) == 0xFC) len = 6;
1303
+
1304
+ if (len == 0)
1305
+ return seterror(EILSEQ);
1306
+ else if (bufsize < len)
1307
+ return seterror(EINVAL);
1308
+ return len;
1309
+ }
1310
+
1311
+ static int
1312
+ eucjp_mblen(csconv_t *cv, const uchar *buf, int bufsize)
1313
+ {
1314
+ if (buf[0] < 0x80) /* ASCII */
1315
+ return 1;
1316
+ else if (buf[0] == 0x8E) /* JIS X 0201 */
1317
+ {
1318
+ if (bufsize < 2)
1319
+ return seterror(EINVAL);
1320
+ else if (!(0xA1 <= buf[1] && buf[1] <= 0xDF))
1321
+ return seterror(EILSEQ);
1322
+ return 2;
1323
+ }
1324
+ else if (buf[0] == 0x8F) /* JIS X 0212 */
1325
+ {
1326
+ if (bufsize < 3)
1327
+ return seterror(EINVAL);
1328
+ else if (!(0xA1 <= buf[1] && buf[1] <= 0xFE)
1329
+ || !(0xA1 <= buf[2] && buf[2] <= 0xFE))
1330
+ return seterror(EILSEQ);
1331
+ return 3;
1332
+ }
1333
+ else /* JIS X 0208 */
1334
+ {
1335
+ if (bufsize < 2)
1336
+ return seterror(EINVAL);
1337
+ else if (!(0xA1 <= buf[0] && buf[0] <= 0xFE)
1338
+ || !(0xA1 <= buf[1] && buf[1] <= 0xFE))
1339
+ return seterror(EILSEQ);
1340
+ return 2;
1341
+ }
1342
+ }
1343
+
1344
+ static int
1345
+ kernel_mbtowc(csconv_t *cv, const uchar *buf, int bufsize, ushort *wbuf, int *wbufsize)
1346
+ {
1347
+ int len;
1348
+
1349
+ len = cv->mblen(cv, buf, bufsize);
1350
+ if (len == -1)
1351
+ return -1;
1352
+ *wbufsize = MultiByteToWideChar(cv->codepage, mbtowc_flags (cv->codepage),
1353
+ (const char *)buf, len, (wchar_t *)wbuf, *wbufsize);
1354
+ if (*wbufsize == 0)
1355
+ return seterror(EILSEQ);
1356
+ return len;
1357
+ }
1358
+
1359
+ static int
1360
+ kernel_wctomb(csconv_t *cv, ushort *wbuf, int wbufsize, uchar *buf, int bufsize)
1361
+ {
1362
+ BOOL usedDefaultChar = 0;
1363
+ BOOL *p = NULL;
1364
+ int flags = 0;
1365
+ int len;
1366
+
1367
+ if (bufsize == 0)
1368
+ return seterror(E2BIG);
1369
+ if (!must_use_null_useddefaultchar(cv->codepage))
1370
+ {
1371
+ p = &usedDefaultChar;
1372
+ #ifdef WC_NO_BEST_FIT_CHARS
1373
+ if (!(cv->flags & FLAG_TRANSLIT))
1374
+ flags |= WC_NO_BEST_FIT_CHARS;
1375
+ #endif
1376
+ }
1377
+ len = WideCharToMultiByte(cv->codepage, flags,
1378
+ (const wchar_t *)wbuf, wbufsize, (char *)buf, bufsize, NULL, p);
1379
+ if (len == 0)
1380
+ {
1381
+ if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
1382
+ return seterror(E2BIG);
1383
+ return seterror(EILSEQ);
1384
+ }
1385
+ else if (usedDefaultChar && !(cv->flags & FLAG_TRANSLIT))
1386
+ return seterror(EILSEQ);
1387
+ else if (cv->mblen(cv, buf, len) != len) /* validate result */
1388
+ return seterror(EILSEQ);
1389
+ return len;
1390
+ }
1391
+
1392
+ /*
1393
+ * It seems that the mode (cv->mode) is fixnum.
1394
+ * For example, when converting iso-2022-jp(cp50221) to unicode:
1395
+ * in ascii sequence: mode=0xC42C0000
1396
+ * in jisx0208 sequence: mode=0xC42C0001
1397
+ * "C42C" is same for each convert session.
1398
+ * It should be: ((codepage-1)<<16)|state
1399
+ */
1400
+ static int
1401
+ mlang_mbtowc(csconv_t *cv, const uchar *buf, int bufsize, ushort *wbuf, int *wbufsize)
1402
+ {
1403
+ int len;
1404
+ int insize;
1405
+ HRESULT hr;
1406
+
1407
+ len = cv->mblen(cv, buf, bufsize);
1408
+ if (len == -1)
1409
+ return -1;
1410
+ insize = len;
1411
+ hr = ConvertINetMultiByteToUnicode(&cv->mode, cv->codepage,
1412
+ (const char *)buf, &insize, (wchar_t *)wbuf, wbufsize);
1413
+ if (hr != S_OK || insize != len)
1414
+ return seterror(EILSEQ);
1415
+ return len;
1416
+ }
1417
+
1418
+ static int
1419
+ mlang_wctomb(csconv_t *cv, ushort *wbuf, int wbufsize, uchar *buf, int bufsize)
1420
+ {
1421
+ char tmpbuf[MB_CHAR_MAX]; /* enough room for one character */
1422
+ int tmpsize = MB_CHAR_MAX;
1423
+ int insize = wbufsize;
1424
+ HRESULT hr;
1425
+
1426
+ hr = ConvertINetUnicodeToMultiByte(&cv->mode, cv->codepage,
1427
+ (const wchar_t *)wbuf, &wbufsize, tmpbuf, &tmpsize);
1428
+ if (hr != S_OK || insize != wbufsize)
1429
+ return seterror(EILSEQ);
1430
+ else if (bufsize < tmpsize)
1431
+ return seterror(E2BIG);
1432
+ else if (cv->mblen(cv, (uchar *)tmpbuf, tmpsize) != tmpsize)
1433
+ return seterror(EILSEQ);
1434
+ memcpy(buf, tmpbuf, tmpsize);
1435
+ return tmpsize;
1436
+ }
1437
+
1438
+ static int
1439
+ utf16_mbtowc(csconv_t *cv, const uchar *buf, int bufsize, ushort *wbuf, int *wbufsize)
1440
+ {
1441
+ int codepage = cv->codepage;
1442
+
1443
+ /* swap endian: 1200 <-> 1201 */
1444
+ if (cv->mode & UNICODE_MODE_SWAPPED)
1445
+ codepage ^= 1;
1446
+
1447
+ if (bufsize < 2)
1448
+ return seterror(EINVAL);
1449
+ if (codepage == 1200) /* little endian */
1450
+ wbuf[0] = (buf[1] << 8) | buf[0];
1451
+ else if (codepage == 1201) /* big endian */
1452
+ wbuf[0] = (buf[0] << 8) | buf[1];
1453
+
1454
+ if ((cv->flags & FLAG_USE_BOM) && !(cv->mode & UNICODE_MODE_BOM_DONE))
1455
+ {
1456
+ cv->mode |= UNICODE_MODE_BOM_DONE;
1457
+ if (wbuf[0] == 0xFFFE)
1458
+ {
1459
+ cv->mode |= UNICODE_MODE_SWAPPED;
1460
+ *wbufsize = 0;
1461
+ return 2;
1462
+ }
1463
+ else if (wbuf[0] == 0xFEFF)
1464
+ {
1465
+ *wbufsize = 0;
1466
+ return 2;
1467
+ }
1468
+ }
1469
+
1470
+ if (0xDC00 <= wbuf[0] && wbuf[0] <= 0xDFFF)
1471
+ return seterror(EILSEQ);
1472
+ if (0xD800 <= wbuf[0] && wbuf[0] <= 0xDBFF)
1473
+ {
1474
+ if (bufsize < 4)
1475
+ return seterror(EINVAL);
1476
+ if (codepage == 1200) /* little endian */
1477
+ wbuf[1] = (buf[3] << 8) | buf[2];
1478
+ else if (codepage == 1201) /* big endian */
1479
+ wbuf[1] = (buf[2] << 8) | buf[3];
1480
+ if (!(0xDC00 <= wbuf[1] && wbuf[1] <= 0xDFFF))
1481
+ return seterror(EILSEQ);
1482
+ *wbufsize = 2;
1483
+ return 4;
1484
+ }
1485
+ *wbufsize = 1;
1486
+ return 2;
1487
+ }
1488
+
1489
+ static int
1490
+ utf16_wctomb(csconv_t *cv, ushort *wbuf, int wbufsize, uchar *buf, int bufsize)
1491
+ {
1492
+ if ((cv->flags & FLAG_USE_BOM) && !(cv->mode & UNICODE_MODE_BOM_DONE))
1493
+ {
1494
+ int r;
1495
+
1496
+ cv->mode |= UNICODE_MODE_BOM_DONE;
1497
+ if (bufsize < 2)
1498
+ return seterror(E2BIG);
1499
+ if (cv->codepage == 1200) /* little endian */
1500
+ memcpy(buf, "\xFF\xFE", 2);
1501
+ else if (cv->codepage == 1201) /* big endian */
1502
+ memcpy(buf, "\xFE\xFF", 2);
1503
+
1504
+ r = utf16_wctomb(cv, wbuf, wbufsize, buf + 2, bufsize - 2);
1505
+ if (r == -1)
1506
+ return -1;
1507
+ return r + 2;
1508
+ }
1509
+
1510
+ if (bufsize < 2)
1511
+ return seterror(E2BIG);
1512
+ if (cv->codepage == 1200) /* little endian */
1513
+ {
1514
+ buf[0] = (wbuf[0] & 0x00FF);
1515
+ buf[1] = (wbuf[0] & 0xFF00) >> 8;
1516
+ }
1517
+ else if (cv->codepage == 1201) /* big endian */
1518
+ {
1519
+ buf[0] = (wbuf[0] & 0xFF00) >> 8;
1520
+ buf[1] = (wbuf[0] & 0x00FF);
1521
+ }
1522
+ if (0xD800 <= wbuf[0] && wbuf[0] <= 0xDBFF)
1523
+ {
1524
+ if (bufsize < 4)
1525
+ return seterror(E2BIG);
1526
+ if (cv->codepage == 1200) /* little endian */
1527
+ {
1528
+ buf[2] = (wbuf[1] & 0x00FF);
1529
+ buf[3] = (wbuf[1] & 0xFF00) >> 8;
1530
+ }
1531
+ else if (cv->codepage == 1201) /* big endian */
1532
+ {
1533
+ buf[2] = (wbuf[1] & 0xFF00) >> 8;
1534
+ buf[3] = (wbuf[1] & 0x00FF);
1535
+ }
1536
+ return 4;
1537
+ }
1538
+ return 2;
1539
+ }
1540
+
1541
+ static int
1542
+ utf32_mbtowc(csconv_t *cv, const uchar *buf, int bufsize, ushort *wbuf, int *wbufsize)
1543
+ {
1544
+ int codepage = cv->codepage;
1545
+ uint wc;
1546
+
1547
+ /* swap endian: 12000 <-> 12001 */
1548
+ if (cv->mode & UNICODE_MODE_SWAPPED)
1549
+ codepage ^= 1;
1550
+
1551
+ if (bufsize < 4)
1552
+ return seterror(EINVAL);
1553
+ if (codepage == 12000) /* little endian */
1554
+ wc = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
1555
+ else if (codepage == 12001) /* big endian */
1556
+ wc = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
1557
+
1558
+ if ((cv->flags & FLAG_USE_BOM) && !(cv->mode & UNICODE_MODE_BOM_DONE))
1559
+ {
1560
+ cv->mode |= UNICODE_MODE_BOM_DONE;
1561
+ if (wc == 0xFFFE0000)
1562
+ {
1563
+ cv->mode |= UNICODE_MODE_SWAPPED;
1564
+ *wbufsize = 0;
1565
+ return 4;
1566
+ }
1567
+ else if (wc == 0x0000FEFF)
1568
+ {
1569
+ *wbufsize = 0;
1570
+ return 4;
1571
+ }
1572
+ }
1573
+
1574
+ if ((0xD800 <= wc && wc <= 0xDFFF) || 0x10FFFF < wc)
1575
+ return seterror(EILSEQ);
1576
+ ucs4_to_utf16(wc, wbuf, wbufsize);
1577
+ return 4;
1578
+ }
1579
+
1580
+ static int
1581
+ utf32_wctomb(csconv_t *cv, ushort *wbuf, int wbufsize, uchar *buf, int bufsize)
1582
+ {
1583
+ uint wc;
1584
+
1585
+ if ((cv->flags & FLAG_USE_BOM) && !(cv->mode & UNICODE_MODE_BOM_DONE))
1586
+ {
1587
+ int r;
1588
+
1589
+ cv->mode |= UNICODE_MODE_BOM_DONE;
1590
+ if (bufsize < 4)
1591
+ return seterror(E2BIG);
1592
+ if (cv->codepage == 12000) /* little endian */
1593
+ memcpy(buf, "\xFF\xFE\x00\x00", 4);
1594
+ else if (cv->codepage == 12001) /* big endian */
1595
+ memcpy(buf, "\x00\x00\xFE\xFF", 4);
1596
+
1597
+ r = utf32_wctomb(cv, wbuf, wbufsize, buf + 4, bufsize - 4);
1598
+ if (r == -1)
1599
+ return -1;
1600
+ return r + 4;
1601
+ }
1602
+
1603
+ if (bufsize < 4)
1604
+ return seterror(E2BIG);
1605
+ wc = utf16_to_ucs4(wbuf);
1606
+ if (cv->codepage == 12000) /* little endian */
1607
+ {
1608
+ buf[0] = wc & 0x000000FF;
1609
+ buf[1] = (wc & 0x0000FF00) >> 8;
1610
+ buf[2] = (wc & 0x00FF0000) >> 16;
1611
+ buf[3] = (wc & 0xFF000000) >> 24;
1612
+ }
1613
+ else if (cv->codepage == 12001) /* big endian */
1614
+ {
1615
+ buf[0] = (wc & 0xFF000000) >> 24;
1616
+ buf[1] = (wc & 0x00FF0000) >> 16;
1617
+ buf[2] = (wc & 0x0000FF00) >> 8;
1618
+ buf[3] = wc & 0x000000FF;
1619
+ }
1620
+ return 4;
1621
+ }
1622
+
1623
+ /*
1624
+ * 50220: ISO 2022 Japanese with no halfwidth Katakana; Japanese (JIS)
1625
+ * 50221: ISO 2022 Japanese with halfwidth Katakana; Japanese (JIS-Allow
1626
+ * 1 byte Kana)
1627
+ * 50222: ISO 2022 Japanese JIS X 0201-1989; Japanese (JIS-Allow 1 byte
1628
+ * Kana - SO/SI)
1629
+ *
1630
+ * MultiByteToWideChar() and WideCharToMultiByte() behave differently
1631
+ * depending on Windows version. On XP, WideCharToMultiByte() doesn't
1632
+ * terminate result sequence with ascii escape. But Vista does.
1633
+ * Use MLang instead.
1634
+ */
1635
+
1636
+ #define ISO2022_MODE(cs, shift) (((cs) << 8) | (shift))
1637
+ #define ISO2022_MODE_CS(mode) (((mode) >> 8) & 0xFF)
1638
+ #define ISO2022_MODE_SHIFT(mode) ((mode) & 0xFF)
1639
+
1640
+ #define ISO2022_SI 0
1641
+ #define ISO2022_SO 1
1642
+
1643
+ /* shift in */
1644
+ static const char iso2022_SI_seq[] = "\x0F";
1645
+ /* shift out */
1646
+ static const char iso2022_SO_seq[] = "\x0E";
1647
+
1648
+ typedef struct iso2022_esc_t iso2022_esc_t;
1649
+ struct iso2022_esc_t {
1650
+ const char *esc;
1651
+ int esc_len;
1652
+ int len;
1653
+ int cs;
1654
+ };
1655
+
1656
+ #define ISO2022JP_CS_ASCII 0
1657
+ #define ISO2022JP_CS_JISX0201_ROMAN 1
1658
+ #define ISO2022JP_CS_JISX0201_KANA 2
1659
+ #define ISO2022JP_CS_JISX0208_1978 3
1660
+ #define ISO2022JP_CS_JISX0208_1983 4
1661
+ #define ISO2022JP_CS_JISX0212 5
1662
+
1663
+ static iso2022_esc_t iso2022jp_esc[] = {
1664
+ {"\x1B\x28\x42", 3, 1, ISO2022JP_CS_ASCII},
1665
+ {"\x1B\x28\x4A", 3, 1, ISO2022JP_CS_JISX0201_ROMAN},
1666
+ {"\x1B\x28\x49", 3, 1, ISO2022JP_CS_JISX0201_KANA},
1667
+ {"\x1B\x24\x40", 3, 2, ISO2022JP_CS_JISX0208_1983}, /* unify 1978 with 1983 */
1668
+ {"\x1B\x24\x42", 3, 2, ISO2022JP_CS_JISX0208_1983},
1669
+ {"\x1B\x24\x28\x44", 4, 2, ISO2022JP_CS_JISX0212},
1670
+ {NULL, 0, 0, 0}
1671
+ };
1672
+
1673
+ static int
1674
+ iso2022jp_mbtowc(csconv_t *cv, const uchar *buf, int bufsize, ushort *wbuf, int *wbufsize)
1675
+ {
1676
+ iso2022_esc_t *iesc = iso2022jp_esc;
1677
+ char tmp[MB_CHAR_MAX];
1678
+ int insize;
1679
+ HRESULT hr;
1680
+ DWORD dummy = 0;
1681
+ int len;
1682
+ int esc_len;
1683
+ int cs;
1684
+ int shift;
1685
+ int i;
1686
+
1687
+ if (buf[0] == 0x1B)
1688
+ {
1689
+ for (i = 0; iesc[i].esc != NULL; ++i)
1690
+ {
1691
+ esc_len = iesc[i].esc_len;
1692
+ if (bufsize < esc_len)
1693
+ {
1694
+ if (strncmp((char *)buf, iesc[i].esc, bufsize) == 0)
1695
+ return seterror(EINVAL);
1696
+ }
1697
+ else
1698
+ {
1699
+ if (strncmp((char *)buf, iesc[i].esc, esc_len) == 0)
1700
+ {
1701
+ cv->mode = ISO2022_MODE(iesc[i].cs, ISO2022_SI);
1702
+ *wbufsize = 0;
1703
+ return esc_len;
1704
+ }
1705
+ }
1706
+ }
1707
+ /* not supported escape sequence */
1708
+ return seterror(EILSEQ);
1709
+ }
1710
+ else if (buf[0] == iso2022_SO_seq[0])
1711
+ {
1712
+ cv->mode = ISO2022_MODE(ISO2022_MODE_CS(cv->mode), ISO2022_SO);
1713
+ *wbufsize = 0;
1714
+ return 1;
1715
+ }
1716
+ else if (buf[0] == iso2022_SI_seq[0])
1717
+ {
1718
+ cv->mode = ISO2022_MODE(ISO2022_MODE_CS(cv->mode), ISO2022_SI);
1719
+ *wbufsize = 0;
1720
+ return 1;
1721
+ }
1722
+
1723
+ cs = ISO2022_MODE_CS(cv->mode);
1724
+ shift = ISO2022_MODE_SHIFT(cv->mode);
1725
+
1726
+ /* reset the mode for informal sequence */
1727
+ if (buf[0] < 0x20)
1728
+ {
1729
+ cs = ISO2022JP_CS_ASCII;
1730
+ shift = ISO2022_SI;
1731
+ }
1732
+
1733
+ len = iesc[cs].len;
1734
+ if (bufsize < len)
1735
+ return seterror(EINVAL);
1736
+ for (i = 0; i < len; ++i)
1737
+ if (!(buf[i] < 0x80))
1738
+ return seterror(EILSEQ);
1739
+ esc_len = iesc[cs].esc_len;
1740
+ memcpy(tmp, iesc[cs].esc, esc_len);
1741
+ if (shift == ISO2022_SO)
1742
+ {
1743
+ memcpy(tmp + esc_len, iso2022_SO_seq, 1);
1744
+ esc_len += 1;
1745
+ }
1746
+ memcpy(tmp + esc_len, buf, len);
1747
+
1748
+ if ((cv->codepage == 50220 || cv->codepage == 50221
1749
+ || cv->codepage == 50222) && shift == ISO2022_SO)
1750
+ {
1751
+ /* XXX: shift-out cannot be used for mbtowc (both kernel and
1752
+ * mlang) */
1753
+ esc_len = iesc[ISO2022JP_CS_JISX0201_KANA].esc_len;
1754
+ memcpy(tmp, iesc[ISO2022JP_CS_JISX0201_KANA].esc, esc_len);
1755
+ memcpy(tmp + esc_len, buf, len);
1756
+ }
1757
+
1758
+ insize = len + esc_len;
1759
+ hr = ConvertINetMultiByteToUnicode(&dummy, cv->codepage,
1760
+ (const char *)tmp, &insize, (wchar_t *)wbuf, wbufsize);
1761
+ if (hr != S_OK || insize != len + esc_len)
1762
+ return seterror(EILSEQ);
1763
+
1764
+ /* Check for conversion error. Assuming defaultChar is 0x3F. */
1765
+ /* ascii should be converted from ascii */
1766
+ if (wbuf[0] == buf[0]
1767
+ && cv->mode != ISO2022_MODE(ISO2022JP_CS_ASCII, ISO2022_SI))
1768
+ return seterror(EILSEQ);
1769
+
1770
+ /* reset the mode for informal sequence */
1771
+ if (cv->mode != ISO2022_MODE(cs, shift))
1772
+ cv->mode = ISO2022_MODE(cs, shift);
1773
+
1774
+ return len;
1775
+ }
1776
+
1777
+ static int
1778
+ iso2022jp_wctomb(csconv_t *cv, ushort *wbuf, int wbufsize, uchar *buf, int bufsize)
1779
+ {
1780
+ iso2022_esc_t *iesc = iso2022jp_esc;
1781
+ char tmp[MB_CHAR_MAX];
1782
+ int tmpsize = MB_CHAR_MAX;
1783
+ int insize = wbufsize;
1784
+ HRESULT hr;
1785
+ DWORD dummy = 0;
1786
+ int len;
1787
+ int esc_len;
1788
+ int cs;
1789
+ int shift;
1790
+ int i;
1791
+
1792
+ /*
1793
+ * MultiByte = [escape sequence] + character + [escape sequence]
1794
+ *
1795
+ * Whether trailing escape sequence is added depends on which API is
1796
+ * used (kernel or MLang, and its version).
1797
+ */
1798
+ hr = ConvertINetUnicodeToMultiByte(&dummy, cv->codepage,
1799
+ (const wchar_t *)wbuf, &wbufsize, tmp, &tmpsize);
1800
+ if (hr != S_OK || insize != wbufsize)
1801
+ return seterror(EILSEQ);
1802
+ else if (bufsize < tmpsize)
1803
+ return seterror(E2BIG);
1804
+
1805
+ if (tmpsize == 1)
1806
+ {
1807
+ cs = ISO2022JP_CS_ASCII;
1808
+ esc_len = 0;
1809
+ }
1810
+ else
1811
+ {
1812
+ for (i = 1; iesc[i].esc != NULL; ++i)
1813
+ {
1814
+ esc_len = iesc[i].esc_len;
1815
+ if (strncmp(tmp, iesc[i].esc, esc_len) == 0)
1816
+ {
1817
+ cs = iesc[i].cs;
1818
+ break;
1819
+ }
1820
+ }
1821
+ if (iesc[i].esc == NULL)
1822
+ /* not supported escape sequence */
1823
+ return seterror(EILSEQ);
1824
+ }
1825
+
1826
+ shift = ISO2022_SI;
1827
+ if (tmp[esc_len] == iso2022_SO_seq[0])
1828
+ {
1829
+ shift = ISO2022_SO;
1830
+ esc_len += 1;
1831
+ }
1832
+
1833
+ len = iesc[cs].len;
1834
+
1835
+ /* Check for converting error. Assuming defaultChar is 0x3F. */
1836
+ /* ascii should be converted from ascii */
1837
+ if (cs == ISO2022JP_CS_ASCII && !(wbuf[0] < 0x80))
1838
+ return seterror(EILSEQ);
1839
+ else if (tmpsize < esc_len + len)
1840
+ return seterror(EILSEQ);
1841
+
1842
+ if (cv->mode == ISO2022_MODE(cs, shift))
1843
+ {
1844
+ /* remove escape sequence */
1845
+ if (esc_len != 0)
1846
+ memmove(tmp, tmp + esc_len, len);
1847
+ esc_len = 0;
1848
+ }
1849
+ else
1850
+ {
1851
+ if (cs == ISO2022JP_CS_ASCII)
1852
+ {
1853
+ esc_len = iesc[ISO2022JP_CS_ASCII].esc_len;
1854
+ memmove(tmp + esc_len, tmp, len);
1855
+ memcpy(tmp, iesc[ISO2022JP_CS_ASCII].esc, esc_len);
1856
+ }
1857
+ if (ISO2022_MODE_SHIFT(cv->mode) == ISO2022_SO)
1858
+ {
1859
+ /* shift-in before changing to other mode */
1860
+ memmove(tmp + 1, tmp, len + esc_len);
1861
+ memcpy(tmp, iso2022_SI_seq, 1);
1862
+ esc_len += 1;
1863
+ }
1864
+ }
1865
+
1866
+ if (bufsize < len + esc_len)
1867
+ return seterror(E2BIG);
1868
+ memcpy(buf, tmp, len + esc_len);
1869
+ cv->mode = ISO2022_MODE(cs, shift);
1870
+ return len + esc_len;
1871
+ }
1872
+
1873
+ static int
1874
+ iso2022jp_flush(csconv_t *cv, uchar *buf, int bufsize)
1875
+ {
1876
+ iso2022_esc_t *iesc = iso2022jp_esc;
1877
+ int esc_len;
1878
+
1879
+ if (cv->mode != ISO2022_MODE(ISO2022JP_CS_ASCII, ISO2022_SI))
1880
+ {
1881
+ esc_len = 0;
1882
+ if (ISO2022_MODE_SHIFT(cv->mode) != ISO2022_SI)
1883
+ esc_len += 1;
1884
+ if (ISO2022_MODE_CS(cv->mode) != ISO2022JP_CS_ASCII)
1885
+ esc_len += iesc[ISO2022JP_CS_ASCII].esc_len;
1886
+ if (bufsize < esc_len)
1887
+ return seterror(E2BIG);
1888
+
1889
+ esc_len = 0;
1890
+ if (ISO2022_MODE_SHIFT(cv->mode) != ISO2022_SI)
1891
+ {
1892
+ memcpy(buf, iso2022_SI_seq, 1);
1893
+ esc_len += 1;
1894
+ }
1895
+ if (ISO2022_MODE_CS(cv->mode) != ISO2022JP_CS_ASCII)
1896
+ {
1897
+ memcpy(buf + esc_len, iesc[ISO2022JP_CS_ASCII].esc,
1898
+ iesc[ISO2022JP_CS_ASCII].esc_len);
1899
+ esc_len += iesc[ISO2022JP_CS_ASCII].esc_len;
1900
+ }
1901
+ return esc_len;
1902
+ }
1903
+ return 0;
1904
+ }
1905
+
1906
+ #if defined(MAKE_DLL) && defined(USE_LIBICONV_DLL)
1907
+ BOOL WINAPI
1908
+ DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved)
1909
+ {
1910
+ switch( fdwReason )
1911
+ {
1912
+ case DLL_PROCESS_ATTACH:
1913
+ hwiniconv = (HMODULE)hinstDLL;
1914
+ break;
1915
+ case DLL_THREAD_ATTACH:
1916
+ case DLL_THREAD_DETACH:
1917
+ case DLL_PROCESS_DETACH:
1918
+ break;
1919
+ }
1920
+ return TRUE;
1921
+ }
1922
+ #endif
1923
+
1924
+ #if defined(MAKE_EXE)
1925
+ #include <stdio.h>
1926
+ #include <fcntl.h>
1927
+ #include <io.h>
1928
+ int
1929
+ main(int argc, char **argv)
1930
+ {
1931
+ char *fromcode = NULL;
1932
+ char *tocode = NULL;
1933
+ int i;
1934
+ char inbuf[BUFSIZ];
1935
+ char outbuf[BUFSIZ];
1936
+ char *pin;
1937
+ char *pout;
1938
+ size_t inbytesleft;
1939
+ size_t outbytesleft;
1940
+ size_t rest = 0;
1941
+ iconv_t cd;
1942
+ size_t r;
1943
+ FILE *in = stdin;
1944
+ int ignore = 0;
1945
+ char *p;
1946
+
1947
+ _setmode(_fileno(stdin), _O_BINARY);
1948
+ _setmode(_fileno(stdout), _O_BINARY);
1949
+
1950
+ for (i = 1; i < argc; ++i)
1951
+ {
1952
+ if (strcmp(argv[i], "-l") == 0)
1953
+ {
1954
+ for (i = 0; codepage_alias[i].name != NULL; ++i)
1955
+ printf("%s\n", codepage_alias[i].name);
1956
+ return 0;
1957
+ }
1958
+
1959
+ if (strcmp(argv[i], "-f") == 0)
1960
+ fromcode = argv[++i];
1961
+ else if (strcmp(argv[i], "-t") == 0)
1962
+ tocode = argv[++i];
1963
+ else if (strcmp(argv[i], "-c") == 0)
1964
+ ignore = 1;
1965
+ else
1966
+ {
1967
+ in = fopen(argv[i], "rb");
1968
+ if (in == NULL)
1969
+ {
1970
+ fprintf(stderr, "cannot open %s\n", argv[i]);
1971
+ return 1;
1972
+ }
1973
+ break;
1974
+ }
1975
+ }
1976
+
1977
+ if (fromcode == NULL || tocode == NULL)
1978
+ {
1979
+ printf("usage: %s [-c] -f from-enc -t to-enc [file]\n", argv[0]);
1980
+ return 0;
1981
+ }
1982
+
1983
+ if (ignore)
1984
+ {
1985
+ p = tocode;
1986
+ tocode = (char *)malloc(strlen(p) + strlen("//IGNORE") + 1);
1987
+ if (tocode == NULL)
1988
+ {
1989
+ perror("fatal error");
1990
+ return 1;
1991
+ }
1992
+ strcpy(tocode, p);
1993
+ strcat(tocode, "//IGNORE");
1994
+ }
1995
+
1996
+ cd = iconv_open(tocode, fromcode);
1997
+ if (cd == (iconv_t)(-1))
1998
+ {
1999
+ perror("iconv_open error");
2000
+ return 1;
2001
+ }
2002
+
2003
+ while ((inbytesleft = fread(inbuf + rest, 1, sizeof(inbuf) - rest, in)) != 0
2004
+ || rest != 0)
2005
+ {
2006
+ inbytesleft += rest;
2007
+ pin = inbuf;
2008
+ pout = outbuf;
2009
+ outbytesleft = sizeof(outbuf);
2010
+ r = iconv(cd, &pin, &inbytesleft, &pout, &outbytesleft);
2011
+ fwrite(outbuf, 1, sizeof(outbuf) - outbytesleft, stdout);
2012
+ if (r == (size_t)(-1) && errno != E2BIG && (errno != EINVAL || feof(in)))
2013
+ {
2014
+ perror("conversion error");
2015
+ return 1;
2016
+ }
2017
+ memmove(inbuf, pin, inbytesleft);
2018
+ rest = inbytesleft;
2019
+ }
2020
+ pout = outbuf;
2021
+ outbytesleft = sizeof(outbuf);
2022
+ r = iconv(cd, NULL, NULL, &pout, &outbytesleft);
2023
+ fwrite(outbuf, 1, sizeof(outbuf) - outbytesleft, stdout);
2024
+ if (r == (size_t)(-1))
2025
+ {
2026
+ perror("conversion error");
2027
+ return 1;
2028
+ }
2029
+
2030
+ iconv_close(cd);
2031
+
2032
+ return 0;
2033
+ }
2034
+ #endif
2035
+