/bin/dd/conv.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 268 lines · 149 code · 22 blank · 97 comment · 50 complexity · 8933c0c9727525ad9c0297872563b4b8 MD5 · raw file

  1. /*-
  2. * Copyright (c) 1991, 1993, 1994
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * This code is derived from software contributed to Berkeley by
  6. * Keith Muller of the University of California, San Diego and Lance
  7. * Visser of Convex Computer Corporation.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 4. Neither the name of the University nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. */
  33. #ifndef lint
  34. #if 0
  35. static char sccsid[] = "@(#)conv.c 8.3 (Berkeley) 4/2/94";
  36. #endif
  37. #endif /* not lint */
  38. #include <sys/cdefs.h>
  39. __FBSDID("$FreeBSD$");
  40. #include <sys/param.h>
  41. #include <err.h>
  42. #include <inttypes.h>
  43. #include <string.h>
  44. #include "dd.h"
  45. #include "extern.h"
  46. /*
  47. * def --
  48. * Copy input to output. Input is buffered until reaches obs, and then
  49. * output until less than obs remains. Only a single buffer is used.
  50. * Worst case buffer calculation is (ibs + obs - 1).
  51. */
  52. void
  53. def(void)
  54. {
  55. u_char *inp;
  56. const u_char *t;
  57. size_t cnt;
  58. if ((t = ctab) != NULL)
  59. for (inp = in.dbp - (cnt = in.dbrcnt); cnt--; ++inp)
  60. *inp = t[*inp];
  61. /* Make the output buffer look right. */
  62. out.dbp = in.dbp;
  63. out.dbcnt = in.dbcnt;
  64. if (in.dbcnt >= out.dbsz) {
  65. /* If the output buffer is full, write it. */
  66. dd_out(0);
  67. /*
  68. * Ddout copies the leftover output to the beginning of
  69. * the buffer and resets the output buffer. Reset the
  70. * input buffer to match it.
  71. */
  72. in.dbp = out.dbp;
  73. in.dbcnt = out.dbcnt;
  74. }
  75. }
  76. void
  77. def_close(void)
  78. {
  79. /* Just update the count, everything is already in the buffer. */
  80. if (in.dbcnt)
  81. out.dbcnt = in.dbcnt;
  82. }
  83. /*
  84. * Copy variable length newline terminated records with a max size cbsz
  85. * bytes to output. Records less than cbs are padded with spaces.
  86. *
  87. * max in buffer: MAX(ibs, cbsz)
  88. * max out buffer: obs + cbsz
  89. */
  90. void
  91. block(void)
  92. {
  93. u_char *inp, *outp;
  94. const u_char *t;
  95. size_t cnt, maxlen;
  96. static int intrunc;
  97. int ch;
  98. /*
  99. * Record truncation can cross block boundaries. If currently in a
  100. * truncation state, keep tossing characters until reach a newline.
  101. * Start at the beginning of the buffer, as the input buffer is always
  102. * left empty.
  103. */
  104. if (intrunc) {
  105. for (inp = in.db, cnt = in.dbrcnt; cnt && *inp++ != '\n'; --cnt)
  106. ;
  107. if (!cnt) {
  108. in.dbcnt = 0;
  109. in.dbp = in.db;
  110. return;
  111. }
  112. intrunc = 0;
  113. /* Adjust the input buffer numbers. */
  114. in.dbcnt = cnt - 1;
  115. in.dbp = inp + cnt - 1;
  116. }
  117. /*
  118. * Copy records (max cbsz size chunks) into the output buffer. The
  119. * translation is done as we copy into the output buffer.
  120. */
  121. ch = 0;
  122. for (inp = in.dbp - in.dbcnt, outp = out.dbp; in.dbcnt;) {
  123. maxlen = MIN(cbsz, in.dbcnt);
  124. if ((t = ctab) != NULL)
  125. for (cnt = 0; cnt < maxlen && (ch = *inp++) != '\n';
  126. ++cnt)
  127. *outp++ = t[ch];
  128. else
  129. for (cnt = 0; cnt < maxlen && (ch = *inp++) != '\n';
  130. ++cnt)
  131. *outp++ = ch;
  132. /*
  133. * Check for short record without a newline. Reassemble the
  134. * input block.
  135. */
  136. if (ch != '\n' && in.dbcnt < cbsz) {
  137. (void)memmove(in.db, in.dbp - in.dbcnt, in.dbcnt);
  138. break;
  139. }
  140. /* Adjust the input buffer numbers. */
  141. in.dbcnt -= cnt;
  142. if (ch == '\n')
  143. --in.dbcnt;
  144. /* Pad short records with spaces. */
  145. if (cnt < cbsz)
  146. (void)memset(outp, ctab ? ctab[' '] : ' ', cbsz - cnt);
  147. else {
  148. /*
  149. * If the next character wouldn't have ended the
  150. * block, it's a truncation.
  151. */
  152. if (!in.dbcnt || *inp != '\n')
  153. ++st.trunc;
  154. /* Toss characters to a newline. */
  155. for (; in.dbcnt && *inp++ != '\n'; --in.dbcnt)
  156. ;
  157. if (!in.dbcnt)
  158. intrunc = 1;
  159. else
  160. --in.dbcnt;
  161. }
  162. /* Adjust output buffer numbers. */
  163. out.dbp += cbsz;
  164. if ((out.dbcnt += cbsz) >= out.dbsz)
  165. dd_out(0);
  166. outp = out.dbp;
  167. }
  168. in.dbp = in.db + in.dbcnt;
  169. }
  170. void
  171. block_close(void)
  172. {
  173. /*
  174. * Copy any remaining data into the output buffer and pad to a record.
  175. * Don't worry about truncation or translation, the input buffer is
  176. * always empty when truncating, and no characters have been added for
  177. * translation. The bottom line is that anything left in the input
  178. * buffer is a truncated record. Anything left in the output buffer
  179. * just wasn't big enough.
  180. */
  181. if (in.dbcnt) {
  182. ++st.trunc;
  183. (void)memmove(out.dbp, in.dbp - in.dbcnt, in.dbcnt);
  184. (void)memset(out.dbp + in.dbcnt, ctab ? ctab[' '] : ' ',
  185. cbsz - in.dbcnt);
  186. out.dbcnt += cbsz;
  187. }
  188. }
  189. /*
  190. * Convert fixed length (cbsz) records to variable length. Deletes any
  191. * trailing blanks and appends a newline.
  192. *
  193. * max in buffer: MAX(ibs, cbsz) + cbsz
  194. * max out buffer: obs + cbsz
  195. */
  196. void
  197. unblock(void)
  198. {
  199. u_char *inp;
  200. const u_char *t;
  201. size_t cnt;
  202. /* Translation and case conversion. */
  203. if ((t = ctab) != NULL)
  204. for (inp = in.dbp - (cnt = in.dbrcnt); cnt--; ++inp)
  205. *inp = t[*inp];
  206. /*
  207. * Copy records (max cbsz size chunks) into the output buffer. The
  208. * translation has to already be done or we might not recognize the
  209. * spaces.
  210. */
  211. for (inp = in.db; in.dbcnt >= cbsz; inp += cbsz, in.dbcnt -= cbsz) {
  212. for (t = inp + cbsz - 1; t >= inp && *t == ' '; --t)
  213. ;
  214. if (t >= inp) {
  215. cnt = t - inp + 1;
  216. (void)memmove(out.dbp, inp, cnt);
  217. out.dbp += cnt;
  218. out.dbcnt += cnt;
  219. }
  220. *out.dbp++ = '\n';
  221. if (++out.dbcnt >= out.dbsz)
  222. dd_out(0);
  223. }
  224. if (in.dbcnt)
  225. (void)memmove(in.db, in.dbp - in.dbcnt, in.dbcnt);
  226. in.dbp = in.db + in.dbcnt;
  227. }
  228. void
  229. unblock_close(void)
  230. {
  231. u_char *t;
  232. size_t cnt;
  233. if (in.dbcnt) {
  234. warnx("%s: short input record", in.name);
  235. for (t = in.db + in.dbcnt - 1; t >= in.db && *t == ' '; --t)
  236. ;
  237. if (t >= in.db) {
  238. cnt = t - in.db + 1;
  239. (void)memmove(out.dbp, in.db, cnt);
  240. out.dbp += cnt;
  241. out.dbcnt += cnt;
  242. }
  243. ++out.dbcnt;
  244. *out.dbp++ = '\n';
  245. }
  246. }