tfussell / xlnt

:bar_chart: Cross-platform user-friendly xlsx library for C++11+
Other
1.5k stars 423 forks source link

password type is wrong #144

Closed sermobile closed 7 years ago

sermobile commented 7 years ago

Hi, I think std::string for password is wrong, because password must be uncode UTF-16. std::string holds UTF-8. Try use std::u16string.

tfussell commented 7 years ago

That's a good point. I am converting it to std::wstring internally. I would still probably continue to accept std::string for the password since that's easiest for users, but I may add overloads that accept std::wstring. Thanks for the contribution.

sermobile commented 7 years ago

std::vector password_wide(password.begin(), password.end()); - it is not convert. You just write utf-8 symbols to 2 bytes. You need something like this http://stackoverflow.com/questions/7153935/how-to-convert-utf-8-stdstring-to-utf-16-stdwstring

sermobile commented 7 years ago

I just tried cyrilic password. And it's does not work.

tfussell commented 7 years ago

I see what you mean. Let me think about this.

paulharris commented 7 years ago

Please stick with a UTF-8 interface, and convert to UTF-16 at the very last moment (ie when you have to interact with MS libraries). I would suggest this library: https://github.com/nemtrif/utfcpp

UTF-8 all the way, baby!

tfussell commented 7 years ago

Haha, thanks for the input @paulharris. That does seem like the correct approach.

sukoi26 commented 7 years ago

utfcpp, it was used before? no? in same way, it will great to add on console windows the conversion for an appli exe to pass some args as a file name ( change font UTF-8 by chcp 65001 ) the reading (std::cin) , the output (std::cout) seem ok but i can't save the name of the file on xlnt cell, i received an error on xml::serialzation UTF8

tfussell commented 7 years ago

@sukoi26 You're right--utfcpp was a dependency of xlnt in the past, but it wasn't being used so I got rid of it. I'm using codecvt from the standard library to convert UTF-8 filenames to UTF-16 on Windows, so I will probably try that for converting passwords too so I don't have to add a new dependency. Could you open a separate issue for the UTF-8 XML serialization problem with an example?

sukoi26 commented 7 years ago

Ok , i will do

tfussell commented 7 years ago

@sermobile I've improved the internal handling of passwords. The API isn't changed, but if you pass a UTF-8 encoded std::string as a password, it should work to decrypt a workbook now. Please give it a try and leave an update here if it works or doesn't work.

By the way, if you're passing the password as a string literal, you should be able to convert it to UTF-8 by adding the u8 prefix:

wb.load("a.xlsx", u8"пароль");
tfussell commented 7 years ago

Closing for now. Please comment here if you're still having trouble opening workbooks with non-ASCII passwords.