dl.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. static char rcsid[] = "$Id$";
  2. #define MAXBYTE 24
  3. #include <stdio.h>
  4. #include <out.h>
  5. char hex[] = "0123456789ABCDEF";
  6. FILE *fp, *fopen();
  7. char **s;
  8. int bytes, bytcnt, checksum;
  9. long pc;
  10. struct outhead ohead;
  11. struct outsect sect[MAXSECT];
  12. main (argc,argv)
  13. int argc;
  14. char *argv[];
  15. {
  16. if (argc != 2) fatal ("usage: %s filename\n",argv[0]);
  17. if (! rd_open (*++argv))
  18. fatal ("can't open %s\n",*argv);
  19. else {
  20. s = argv;
  21. convert ();
  22. }
  23. }
  24. convert ()
  25. {
  26. int i;
  27. rd_ohead(&ohead);
  28. if (ohead.oh_flags & HF_LINK) {
  29. fatal("%s contains unresolved references\n",s);
  30. }
  31. rd_sect(sect, ohead.oh_nsect);
  32. for (i = 0; i < ohead.oh_nsect; i++) {
  33. rd_outsect(i);
  34. pc = sect[i].os_base;
  35. while (sect[i].os_size) {
  36. unsigned int sz = 8096, fl;
  37. extern char *calloc();
  38. register char *buf;
  39. char *pbuf;
  40. if (sz > sect[i].os_size) sz = sect[i].os_size;
  41. sect[i].os_size -= sz;
  42. pbuf = buf = calloc(sz, 1);
  43. if (fl = sect[i].os_flen) {
  44. if (fl > sz) fl = sz;
  45. sect[i].os_flen -= fl;
  46. rd_emit(buf, (long) fl);
  47. }
  48. while (sz > 0) {
  49. int p = bytcnt = sz < MAXBYTE ? sz : MAXBYTE;
  50. checksum = 0;
  51. sz -= p;
  52. if (pc > 0xffffL)
  53. S2record (buf);
  54. else S1record (buf);
  55. buf += p;
  56. }
  57. free(pbuf);
  58. }
  59. }
  60. printf ("S9030000FC\n");
  61. }
  62. S2record (buf)
  63. char *buf;
  64. {
  65. printf ("S2");
  66. bytcnt += 4;
  67. outbyte (bytcnt);
  68. outbyte ((int) (pc >> 16));
  69. outbyte ((int) (pc >> 8));
  70. outbyte ((int) pc);
  71. record ();
  72. }
  73. S1record (buf)
  74. char *buf;
  75. {
  76. printf ("S1");
  77. bytcnt += 3;
  78. outbyte (bytcnt);
  79. outbyte ((int) (pc >> 8));
  80. outbyte((int) pc);
  81. record (buf);
  82. }
  83. record (buf)
  84. register char *buf;
  85. {
  86. while (bytcnt != 0)
  87. {
  88. outbyte (*buf++);
  89. pc ++;
  90. }
  91. outbyte (~checksum);
  92. putchar ('\n');
  93. putchar (0);
  94. putchar (0);
  95. }
  96. outbyte (b)
  97. int b;
  98. {
  99. checksum = (checksum + b) & 0377;
  100. putchar (hex[(b>>4) & 017]);
  101. putchar (hex[b & 017]);
  102. -- bytcnt;
  103. }
  104. rd_fatal() { fatal("Read error\n"); }
  105. fatal (s,a)
  106. {
  107. printf (s,a);
  108. exit (-1);
  109. }