1/* Add string to a section.
2   Copyright (C) 2002 Red Hat, Inc.
3   This file is part of Red Hat elfutils.
4   Written by Ulrich Drepper <drepper@redhat.com>, 2002.
5
6   Red Hat elfutils is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by the
8   Free Software Foundation; version 2 of the License.
9
10   Red Hat elfutils is distributed in the hope that it will be useful, but
11   WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   General Public License for more details.
14
15   You should have received a copy of the GNU General Public License along
16   with Red Hat elfutils; if not, write to the Free Software Foundation,
17   Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
18
19   Red Hat elfutils is an included package of the Open Invention Network.
20   An included package of the Open Invention Network is a package for which
21   Open Invention Network licensees cross-license their patents.  No patent
22   license is granted, either expressly or impliedly, by designation as an
23   included package.  Should you wish to participate in the Open Invention
24   Network licensing program, please visit www.openinventionnetwork.com
25   <http://www.openinventionnetwork.com>.  */
26
27#ifdef HAVE_CONFIG_H
28# include <config.h>
29#endif
30
31#include <ctype.h>
32#include <stdio.h>
33#include <string.h>
34
35#include <libasmP.h>
36
37
38/* Add zero terminated string STR of size LEN to (sub)section ASMSCN.  */
39int
40asm_addstrz (asmscn, str, len)
41     AsmScn_t *asmscn;
42     const char *str;
43     size_t len;
44{
45  if (asmscn == NULL)
46    return -1;
47
48  if (unlikely (asmscn->type == SHT_NOBITS))
49    {
50      if (len == 0)
51	{
52	  if (str[0] != '\0')
53	    {
54	      __libasm_seterrno (ASM_E_TYPE);
55	      return -1;
56	    }
57	}
58      else
59	{
60	  size_t cnt;
61
62	  for (cnt = 0; cnt < len; ++cnt)
63	    if (str[cnt] != '\0')
64	      {
65		__libasm_seterrno (ASM_E_TYPE);
66		return -1;
67	      }
68	}
69    }
70
71  if (len == 0)
72    len = strlen (str) + 1;
73
74  if (unlikely (asmscn->ctx->textp))
75    {
76      bool nextline = true;
77
78      do
79	{
80	  if (nextline)
81	    {
82	      fputs ("\t.string\t\"", asmscn->ctx->out.file);
83	      nextline = false;
84	    }
85
86	  if (*str == '\0')
87	    fputs ("\\000", asmscn->ctx->out.file);
88	  else if (! isascii (*str))
89	    fprintf (asmscn->ctx->out.file, "\\%03o",
90		     (unsigned int) *((unsigned char *)str));
91	  else if (*str == '\\')
92	    fputs ("\\\\", asmscn->ctx->out.file);
93	  else if (*str == '\n')
94	    {
95	      fputs ("\\n\"", asmscn->ctx->out.file);
96	      nextline = true;
97	    }
98	  else
99	    fputc (*str, asmscn->ctx->out.file);
100
101	  ++str;
102	}
103      while (--len > 0 && (len > 1 || *str != '\0'));
104
105      if (! nextline)
106	fputs ("\"\n", asmscn->ctx->out.file);
107    }
108  else
109    {
110      /* Make sure there is enough room.  */
111      if (__libasm_ensure_section_space (asmscn, len) != 0)
112	return -1;
113
114      /* Copy the string.  */
115      memcpy (&asmscn->content->data[asmscn->content->len], str, len);
116
117      /* Adjust the pointer in the data buffer.  */
118      asmscn->content->len += len;
119
120      /* Increment the offset in the (sub)section.  */
121      asmscn->offset += len;
122    }
123
124  return 0;
125}
126