encname.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include "rar.hpp"
  2. EncodeFileName::EncodeFileName()
  3. {
  4. Flags=0;
  5. FlagBits=0;
  6. FlagsPos=0;
  7. DestSize=0;
  8. }
  9. void EncodeFileName::Decode(char *Name,byte *EncName,size_t EncSize,wchar *NameW,
  10. size_t MaxDecSize)
  11. {
  12. size_t EncPos=0,DecPos=0;
  13. byte HighByte=EncName[EncPos++];
  14. while (EncPos<EncSize && DecPos<MaxDecSize)
  15. {
  16. if (FlagBits==0)
  17. {
  18. Flags=EncName[EncPos++];
  19. FlagBits=8;
  20. }
  21. switch(Flags>>6)
  22. {
  23. case 0:
  24. NameW[DecPos++]=EncName[EncPos++];
  25. break;
  26. case 1:
  27. NameW[DecPos++]=EncName[EncPos++]+(HighByte<<8);
  28. break;
  29. case 2:
  30. NameW[DecPos++]=EncName[EncPos]+(EncName[EncPos+1]<<8);
  31. EncPos+=2;
  32. break;
  33. case 3:
  34. {
  35. int Length=EncName[EncPos++];
  36. if (Length & 0x80)
  37. {
  38. byte Correction=EncName[EncPos++];
  39. for (Length=(Length&0x7f)+2;Length>0 && DecPos<MaxDecSize;Length--,DecPos++)
  40. NameW[DecPos]=((Name[DecPos]+Correction)&0xff)+(HighByte<<8);
  41. }
  42. else
  43. for (Length+=2;Length>0 && DecPos<MaxDecSize;Length--,DecPos++)
  44. NameW[DecPos]=Name[DecPos];
  45. }
  46. break;
  47. }
  48. Flags<<=2;
  49. FlagBits-=2;
  50. }
  51. NameW[DecPos<MaxDecSize ? DecPos:MaxDecSize-1]=0;
  52. }