Nife version Beta
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

2 anos atrás
10 anos atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* Copyright (C) 2011-2022 Patrick H. E. Foubet - S.E.R.I.A.N.E.
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or any
  5. later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>
  12. *******************************************************************/
  13. /* scs.c */
  14. /* Scs means Sister Chip Signature */
  15. #include <stdio.h>
  16. #include <sys/types.h>
  17. #include "scs.h"
  18. static struct Ref { /* Representatives data */
  19. double D;
  20. char C1;
  21. char C2;
  22. short S;
  23. char C3;
  24. int I;
  25. char C4;
  26. long long LL;
  27. } E;
  28. static uint32_t MyScs=0;
  29. static void Scs_init(void)
  30. {
  31. E.C1 = 'N';
  32. E.C2 = 'i';
  33. E.C3 = 'f';
  34. E.C4 = 'e';
  35. E.D = 2.7182818284590452354; /* e */
  36. E.S = 763;
  37. E.I = 33497;
  38. E.LL = (long long) 762572642;
  39. }
  40. uint32_t getScs(void)
  41. {
  42. int i,l;
  43. unsigned char *t, k;
  44. uint32_t r;
  45. if (MyScs == 0) {
  46. Scs_init();
  47. l = sizeof(E);
  48. t =(unsigned char*)&E;
  49. r=0;
  50. k=0;
  51. for (i=0; i<l; i++) {
  52. /* printf("%d : k=%d %u r=0x%.8lx\n",i,(int)k,(unsigned int)t[i],r);*/
  53. switch(k) {
  54. case 0:
  55. r+=(unsigned int)t[i];
  56. break;
  57. case 1:
  58. r+=(unsigned int)(((int)t[i])<<5);
  59. break;
  60. case 2:
  61. r+=(unsigned int)(((int)t[i])<<10);
  62. break;
  63. default:
  64. r+=(unsigned int)(((int)t[i])<<15);
  65. break;
  66. }
  67. if (++k==4) k=0;
  68. }
  69. /* printf("Key : l=%d r=%lu (0x%.8lx)\n",l,r,r); */
  70. MyScs = r + (uint32_t)(l<<24);
  71. }
  72. return MyScs;
  73. }
  74. /* ******************** for tests ************************************
  75. int main(int N, char*P[])
  76. {
  77. int i;
  78. unsigned char *d, *f;
  79. unsigned short *ds, *fs;
  80. unsigned int *di, *fi;
  81. unsigned long long *dl, *fl;
  82. printf("short : %.2d\n", sizeof(short));
  83. printf("int : %.2d\n", sizeof(int));
  84. printf("size_t : %.2d\n", sizeof(size_t));
  85. printf("long : %.2d\n", sizeof(long));
  86. printf("long long : %.2d\n", sizeof(long long));
  87. printf("float : %.2d\n", sizeof(float));
  88. printf("double : %.2d\n", sizeof(double));
  89. printf("long double : %.2d\n", sizeof(long double));
  90. printf("E : %.2d\n", sizeof(E));
  91. Scs_init();
  92. d=(char*)&E;
  93. f=d+sizeof(E);
  94. while (d < f) printf("%.2x",(int)*d++);
  95. printf("\n");
  96. ds=(short*)&E;
  97. fs=ds+(sizeof(E)/sizeof(short));
  98. while (ds < fs) printf("%4x",(int)*ds++);
  99. printf("\n");
  100. di=(int*)&E;
  101. fi=di+(sizeof(E)/sizeof(int));
  102. while (di < fi) printf("%8x",*di++);
  103. printf("\n");
  104. dl=(long long*)&E;
  105. fl=dl+(sizeof(E)/sizeof(long long));
  106. while (dl < fl) printf("%.16llx",*dl++);
  107. printf("\n");
  108. printf("D : %.2d s=%.2d\n",(void*)(&E.D)-(void*)(&E),sizeof(E.D));
  109. printf("S : %.2d s=%.2d\n",(void*)(&E.S)-(void*)(&E),sizeof(E.S));
  110. printf("I : %.2d s=%.2d\n",(void*)(&E.I)-(void*)(&E),sizeof(E.I));
  111. printf("LL: %.2d s=%.2d\n",(void*)(&E.LL)-(void*)(&E),sizeof(E.LL));
  112. printf("MyScs = 0x%.8lx\n",getScs());
  113. printf("MyScs = 0x%.8lx\n",getScs());
  114. }
  115. ****************/