030-archindependent-bytecode.patch 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. --- a/src/ldump.c
  2. +++ b/src/ldump.c
  3. @@ -67,12 +67,12 @@ static void DumpString(const TString* s,
  4. {
  5. if (s==NULL || getstr(s)==NULL)
  6. {
  7. - size_t size=0;
  8. + unsigned int size=0;
  9. DumpVar(size,D);
  10. }
  11. else
  12. {
  13. - size_t size=s->tsv.len+1; /* include trailing '\0' */
  14. + unsigned int size=s->tsv.len+1; /* include trailing '\0' */
  15. DumpVar(size,D);
  16. DumpBlock(getstr(s),size,D);
  17. }
  18. --- a/src/lundump.c
  19. +++ b/src/lundump.c
  20. @@ -25,6 +25,7 @@ typedef struct {
  21. ZIO* Z;
  22. Mbuffer* b;
  23. const char* name;
  24. + int swap;
  25. } LoadState;
  26. #ifdef LUAC_TRUST_BINARIES
  27. @@ -40,7 +41,6 @@ static void error(LoadState* S, const ch
  28. }
  29. #endif
  30. -#define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size))
  31. #define LoadByte(S) (lu_byte)LoadChar(S)
  32. #define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x))
  33. #define LoadVector(S,b,n,size) LoadMem(S,b,n,size)
  34. @@ -51,6 +51,49 @@ static void LoadBlock(LoadState* S, void
  35. IF (r!=0, "unexpected end");
  36. }
  37. +static void LoadMem (LoadState* S, void* b, int n, size_t size)
  38. +{
  39. + LoadBlock(S,b,n*size);
  40. + if (S->swap)
  41. + {
  42. + char* p=(char*) b;
  43. + char c;
  44. + switch (size)
  45. + {
  46. + case 1:
  47. + break;
  48. + case 2:
  49. + while (n--)
  50. + {
  51. + c=p[0]; p[0]=p[1]; p[1]=c;
  52. + p+=2;
  53. + }
  54. + break;
  55. + case 4:
  56. + while (n--)
  57. + {
  58. + c=p[0]; p[0]=p[3]; p[3]=c;
  59. + c=p[1]; p[1]=p[2]; p[2]=c;
  60. + p+=4;
  61. + }
  62. + break;
  63. + case 8:
  64. + while (n--)
  65. + {
  66. + c=p[0]; p[0]=p[7]; p[7]=c;
  67. + c=p[1]; p[1]=p[6]; p[6]=c;
  68. + c=p[2]; p[2]=p[5]; p[5]=c;
  69. + c=p[3]; p[3]=p[4]; p[4]=c;
  70. + p+=8;
  71. + }
  72. + break;
  73. + default:
  74. + IF(1, "bad size");
  75. + break;
  76. + }
  77. + }
  78. +}
  79. +
  80. static int LoadChar(LoadState* S)
  81. {
  82. char x;
  83. @@ -82,7 +125,7 @@ static lua_Integer LoadInteger(LoadState
  84. static TString* LoadString(LoadState* S)
  85. {
  86. - size_t size;
  87. + unsigned int size;
  88. LoadVar(S,size);
  89. if (size==0)
  90. return NULL;
  91. @@ -196,6 +239,7 @@ static void LoadHeader(LoadState* S)
  92. char s[LUAC_HEADERSIZE];
  93. luaU_header(h);
  94. LoadBlock(S,s,LUAC_HEADERSIZE);
  95. + S->swap=(s[6]!=h[6]); s[6]=h[6];
  96. IF (memcmp(h,s,LUAC_HEADERSIZE)!=0, "bad header");
  97. }
  98. @@ -230,7 +274,7 @@ void luaU_header (char* h)
  99. *h++=(char)LUAC_FORMAT;
  100. *h++=(char)*(char*)&x; /* endianness */
  101. *h++=(char)sizeof(int);
  102. - *h++=(char)sizeof(size_t);
  103. + *h++=(char)sizeof(unsigned int);
  104. *h++=(char)sizeof(Instruction);
  105. *h++=(char)sizeof(lua_Number);