• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3  * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this list of
9  *    conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12  *    of conditions and the following disclaimer in the documentation and/or other materials
13  *    provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16  *    to endorse or promote products derived from this software without specific prior written
17  *    permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include "los_seq_buf.h"
33 #include <stdlib.h>
34 
ExpandSeqBuf(struct SeqBuf * seqBuf,size_t oldCount)35 static int ExpandSeqBuf(struct SeqBuf *seqBuf, size_t oldCount)
36 {
37     char *newBuf = NULL;
38     int ret;
39 
40     if ((seqBuf == NULL) || (seqBuf->buf == NULL)) {
41         return -LOS_NOK;
42     }
43 
44     if (seqBuf->size >= SEQBUF_LIMIT_SIZE) {
45         goto EXPAND_FAILED;
46     }
47 
48     newBuf = (char*)malloc(seqBuf->size <<= 1);
49     if (newBuf == NULL) {
50         goto EXPAND_FAILED;
51     }
52     (void)memset_s(newBuf + oldCount, seqBuf->size - oldCount, 0, seqBuf->size - oldCount);
53 
54     ret = memcpy_s(newBuf, seqBuf->size, seqBuf->buf, oldCount);
55     if (ret != LOS_OK) {
56         free(newBuf);
57         goto EXPAND_FAILED;
58     }
59     seqBuf->count = oldCount;
60 
61     free(seqBuf->buf);
62     seqBuf->buf = newBuf;
63 
64     return LOS_OK;
65 EXPAND_FAILED:
66     free(seqBuf->buf);
67     seqBuf->buf = NULL;
68     seqBuf->count = 0;
69     seqBuf->size = 0;
70 
71     return -LOS_NOK;
72 }
73 
LosBufCreat(void)74 struct SeqBuf *LosBufCreat(void)
75 {
76     struct SeqBuf *seqBuf = NULL;
77 
78     seqBuf = (struct SeqBuf *)malloc(sizeof(struct SeqBuf));
79     if (seqBuf == NULL) {
80         errno = -LOS_ENOMEM;
81         return NULL;
82     }
83     (void)memset_s(seqBuf, sizeof(struct SeqBuf), 0, sizeof(struct SeqBuf));
84 
85     return seqBuf;
86 }
87 
LosBufVprintf(struct SeqBuf * seqBuf,const char * fmt,va_list argList)88 int LosBufVprintf(struct SeqBuf *seqBuf, const char *fmt, va_list argList)
89 {
90     bool needreprintf = FALSE;
91     int bufLen;
92 
93     if (seqBuf == NULL) {
94         return -LOS_EPERM;
95     }
96 
97     if (seqBuf->buf == NULL) {
98         seqBuf->size = SEQBUF_PAGE_SIZE;
99         seqBuf->buf = (char *)malloc(seqBuf->size);
100         if (seqBuf->buf == NULL) {
101             return -LOS_ENOMEM;
102         }
103         (void)memset_s(seqBuf->buf, seqBuf->size, 0, seqBuf->size);
104         seqBuf->count = 0;
105     }
106 
107     do {
108         bufLen = vsnprintf_s(seqBuf->buf + seqBuf->count, seqBuf->size - seqBuf->count,
109                              seqBuf->size - seqBuf->count - 1, fmt, argList);
110         if (bufLen >= 0) {
111             /* succeed write. */
112             seqBuf->count += bufLen;
113             return 0;
114         }
115         if (seqBuf->buf[seqBuf->count] == '\0') {
116             free(seqBuf->buf);
117             seqBuf->buf = NULL;
118             break;
119         }
120 
121         needreprintf = TRUE;
122 
123         if (ExpandSeqBuf(seqBuf, seqBuf->count) != 0) {
124             break;
125         }
126     } while (needreprintf);
127 
128     return -LOS_NOK;
129 }
130 
LosBufPrintf(struct SeqBuf * seqBuf,const char * fmt,...)131 int LosBufPrintf(struct SeqBuf *seqBuf, const char *fmt, ...)
132 {
133     va_list argList;
134     int ret;
135 
136     va_start(argList, fmt);
137     ret = LosBufVprintf(seqBuf, fmt, argList);
138     va_end(argList);
139 
140     return ret;
141 }
142 
LosBufRelease(struct SeqBuf * seqBuf)143 int LosBufRelease(struct SeqBuf *seqBuf)
144 {
145     if (seqBuf == NULL) {
146         return -LOS_EPERM;
147     }
148 
149     if (seqBuf->buf != NULL) {
150         free(seqBuf->buf);
151         seqBuf->buf = NULL;
152     }
153     free(seqBuf);
154 
155     return LOS_OK;
156 }
157