66-local-char-array.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* -*-comment-start: "//";comment-end:""-*-
  2. * GNU Mes --- Maxwell Equations of Software
  3. * Copyright © 2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  4. *
  5. * This file is part of GNU Mes.
  6. *
  7. * GNU Mes is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * GNU Mes is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <mes/lib.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #define memset xmemset
  25. #define calloc xcalloc
  26. void *
  27. memset (void *s, int c, size_t n)
  28. {
  29. char *p = s;
  30. while (n--)
  31. *p++ = c;
  32. return s;
  33. }
  34. void *
  35. calloc (size_t nmemb, size_t size)
  36. {
  37. size_t count = nmemb * size;
  38. void *p = malloc (count);
  39. memset (p, 0, count);
  40. return p;
  41. }
  42. /* {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'} */
  43. char little_endian_table[16] =
  44. { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46 };
  45. char *
  46. little_endian (unsigned value, char *c, int number_of_bytes)
  47. {
  48. char table[16] =
  49. { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46 };
  50. switch (number_of_bytes)
  51. {
  52. case 4:
  53. {
  54. c[6] = table[value >> 28];
  55. c[7] = table[(value >> 24) % 16];
  56. }
  57. case 3:
  58. {
  59. c[4] = table[(value >> 20) % 16];
  60. c[5] = table[(value >> 16) % 16];
  61. }
  62. case 2:
  63. {
  64. c[2] = table[(value >> 12) % 16];
  65. c[3] = table[(value >> 8) % 16];
  66. }
  67. case 1:
  68. {
  69. c[0] = table[(value >> 4) % 16];
  70. c[1] = table[value % 16];
  71. break;
  72. }
  73. default:
  74. return "invalid";
  75. }
  76. return c;
  77. }
  78. int
  79. main ()
  80. {
  81. char table[3] = { '0', '1', '2' };
  82. char *s;
  83. s = calloc (10, sizeof (char));
  84. eputs ("2=");
  85. eputs (little_endian (2, s, 1));
  86. eputs ("\n");
  87. if (strcmp (s, "02"))
  88. return 1;
  89. eputs ("8=");
  90. eputs (little_endian (8, s, 2));
  91. eputs ("\n");
  92. if (strcmp (s, "0800"))
  93. return 2;
  94. eputs ("16=");
  95. eputs (little_endian (16, s, 4));
  96. eputs ("\n");
  97. if (strcmp (s, "10000000"))
  98. return 3;
  99. return 0;
  100. }