1
0

encname.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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,size_t NameSize,byte *EncName,size_t EncSize,
  10. wchar *NameW,size_t MaxDecSize)
  11. {
  12. size_t EncPos=0,DecPos=0;
  13. byte HighByte=EncPos<EncSize ? EncName[EncPos++] : 0;
  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. if (EncPos>=EncSize)
  25. break;
  26. NameW[DecPos++]=EncName[EncPos++];
  27. break;
  28. case 1:
  29. if (EncPos>=EncSize)
  30. break;
  31. NameW[DecPos++]=EncName[EncPos++]+(HighByte<<8);
  32. break;
  33. case 2:
  34. if (EncPos+1>=EncSize)
  35. break;
  36. NameW[DecPos++]=EncName[EncPos]+(EncName[EncPos+1]<<8);
  37. EncPos+=2;
  38. break;
  39. case 3:
  40. {
  41. if (EncPos>=EncSize)
  42. break;
  43. int Length=EncName[EncPos++];
  44. if ((Length & 0x80)!=0)
  45. {
  46. if (EncPos>=EncSize)
  47. break;
  48. byte Correction=EncName[EncPos++];
  49. for (Length=(Length&0x7f)+2;Length>0 && DecPos<MaxDecSize && DecPos<NameSize;Length--,DecPos++)
  50. NameW[DecPos]=((Name[DecPos]+Correction)&0xff)+(HighByte<<8);
  51. }
  52. else
  53. for (Length+=2;Length>0 && DecPos<MaxDecSize && DecPos<NameSize;Length--,DecPos++)
  54. NameW[DecPos]=Name[DecPos];
  55. }
  56. break;
  57. }
  58. Flags<<=2;
  59. FlagBits-=2;
  60. }
  61. NameW[DecPos<MaxDecSize ? DecPos:MaxDecSize-1]=0;
  62. }