summaryrefslogtreecommitdiff
path: root/apps/buffering.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/buffering.c')
-rw-r--r--apps/buffering.c63
1 files changed, 34 insertions, 29 deletions
diff --git a/apps/buffering.c b/apps/buffering.c
index d49669777b..d891382d81 100644
--- a/apps/buffering.c
+++ b/apps/buffering.c
@@ -225,7 +225,7 @@ buf_ridx == buf_widx means the buffer is empty.
225 only potential side effect is to allocate space for the cur_handle 225 only potential side effect is to allocate space for the cur_handle
226 if it returns NULL. 226 if it returns NULL.
227 */ 227 */
228static struct memory_handle *add_handle(size_t data_size, const bool can_wrap, 228static struct memory_handle *add_handle(const size_t data_size, const bool can_wrap,
229 const bool alloc_all) 229 const bool alloc_all)
230{ 230{
231 /* gives each handle a unique id */ 231 /* gives each handle a unique id */
@@ -873,8 +873,10 @@ management functions for all the actual handle management work.
873 return value: <0 if the file cannot be opened, or one file already 873 return value: <0 if the file cannot be opened, or one file already
874 queued to be opened, otherwise the handle for the file in the buffer 874 queued to be opened, otherwise the handle for the file in the buffer
875*/ 875*/
876int bufopen(const char *file, size_t offset, const enum data_type type) 876int bufopen(const char *file, const size_t offset, const enum data_type type)
877{ 877{
878 size_t adjusted_offset = offset;
879
878 int fd = open(file, O_RDONLY); 880 int fd = open(file, O_RDONLY);
879 if (fd < 0) 881 if (fd < 0)
880 return ERR_FILE_ERROR; 882 return ERR_FILE_ERROR;
@@ -882,10 +884,10 @@ int bufopen(const char *file, size_t offset, const enum data_type type)
882 size_t size = filesize(fd); 884 size_t size = filesize(fd);
883 bool can_wrap = type==TYPE_PACKET_AUDIO || type==TYPE_CODEC; 885 bool can_wrap = type==TYPE_PACKET_AUDIO || type==TYPE_CODEC;
884 886
885 if (offset > size) 887 if (adjusted_offset > size)
886 offset = 0; 888 adjusted_offset = 0;
887 889
888 struct memory_handle *h = add_handle(size-offset, can_wrap, false); 890 struct memory_handle *h = add_handle(size-adjusted_offset, can_wrap, false);
889 if (!h) 891 if (!h)
890 { 892 {
891 DEBUGF("bufopen: failed to add handle\n"); 893 DEBUGF("bufopen: failed to add handle\n");
@@ -894,7 +896,7 @@ int bufopen(const char *file, size_t offset, const enum data_type type)
894 } 896 }
895 897
896 strncpy(h->path, file, MAX_PATH); 898 strncpy(h->path, file, MAX_PATH);
897 h->offset = offset; 899 h->offset = adjusted_offset;
898 h->ridx = buf_widx; 900 h->ridx = buf_widx;
899 h->data = buf_widx; 901 h->data = buf_widx;
900 h->type = type; 902 h->type = type;
@@ -923,7 +925,7 @@ int bufopen(const char *file, size_t offset, const enum data_type type)
923 else 925 else
924#endif 926#endif
925 { 927 {
926 h->filerem = size - offset; 928 h->filerem = size - adjusted_offset;
927 h->filesize = size; 929 h->filesize = size;
928 h->available = 0; 930 h->available = 0;
929 h->widx = buf_widx; 931 h->widx = buf_widx;
@@ -1094,27 +1096,28 @@ static struct memory_handle *prep_bufdata(const int handle_id, size_t *size,
1094 Return the number of bytes copied or < 0 for failure (handle not found). 1096 Return the number of bytes copied or < 0 for failure (handle not found).
1095 The caller is blocked until the requested amount of data is available. 1097 The caller is blocked until the requested amount of data is available.
1096*/ 1098*/
1097ssize_t bufread(const int handle_id, size_t size, void *dest) 1099ssize_t bufread(const int handle_id, const size_t size, void *dest)
1098{ 1100{
1099 const struct memory_handle *h; 1101 const struct memory_handle *h;
1102 size_t adjusted_size = size;
1100 1103
1101 h = prep_bufdata(handle_id, &size, false); 1104 h = prep_bufdata(handle_id, &adjusted_size, false);
1102 if (!h) 1105 if (!h)
1103 return ERR_HANDLE_NOT_FOUND; 1106 return ERR_HANDLE_NOT_FOUND;
1104 1107
1105 if (h->ridx + size > buffer_len) 1108 if (h->ridx + adjusted_size > buffer_len)
1106 { 1109 {
1107 /* the data wraps around the end of the buffer */ 1110 /* the data wraps around the end of the buffer */
1108 size_t read = buffer_len - h->ridx; 1111 size_t read = buffer_len - h->ridx;
1109 memcpy(dest, &buffer[h->ridx], read); 1112 memcpy(dest, &buffer[h->ridx], read);
1110 memcpy(dest+read, buffer, size - read); 1113 memcpy(dest+read, buffer, adjusted_size - read);
1111 } 1114 }
1112 else 1115 else
1113 { 1116 {
1114 memcpy(dest, &buffer[h->ridx], size); 1117 memcpy(dest, &buffer[h->ridx], adjusted_size);
1115 } 1118 }
1116 1119
1117 return size; 1120 return adjusted_size;
1118} 1121}
1119 1122
1120/* Update the "data" pointer to make the handle's data available to the caller. 1123/* Update the "data" pointer to make the handle's data available to the caller.
@@ -1126,20 +1129,21 @@ ssize_t bufread(const int handle_id, size_t size, void *dest)
1126 The guard buffer may be used to provide the requested size. This means it's 1129 The guard buffer may be used to provide the requested size. This means it's
1127 unsafe to request more than the size of the guard buffer. 1130 unsafe to request more than the size of the guard buffer.
1128*/ 1131*/
1129ssize_t bufgetdata(const int handle_id, size_t size, void **data) 1132ssize_t bufgetdata(const int handle_id, const size_t size, void **data)
1130{ 1133{
1131 const struct memory_handle *h; 1134 const struct memory_handle *h;
1135 size_t adjusted_size = size;
1132 1136
1133 h = prep_bufdata(handle_id, &size, true); 1137 h = prep_bufdata(handle_id, &adjusted_size, true);
1134 if (!h) 1138 if (!h)
1135 return ERR_HANDLE_NOT_FOUND; 1139 return ERR_HANDLE_NOT_FOUND;
1136 1140
1137 if (h->ridx + size > buffer_len) 1141 if (h->ridx + adjusted_size > buffer_len)
1138 { 1142 {
1139 /* the data wraps around the end of the buffer : 1143 /* the data wraps around the end of the buffer :
1140 use the guard buffer to provide the requested amount of data. */ 1144 use the guard buffer to provide the requested amount of data. */
1141 size_t copy_n = h->ridx + size - buffer_len; 1145 size_t copy_n = h->ridx + adjusted_size - buffer_len;
1142 /* prep_bufdata ensures size <= buffer_len - h->ridx + GUARD_BUFSIZE, 1146 /* prep_bufdata ensures adjusted_size <= buffer_len - h->ridx + GUARD_BUFSIZE,
1143 so copy_n <= GUARD_BUFSIZE */ 1147 so copy_n <= GUARD_BUFSIZE */
1144 memcpy(guard_buffer, (const unsigned char *)buffer, copy_n); 1148 memcpy(guard_buffer, (const unsigned char *)buffer, copy_n);
1145 } 1149 }
@@ -1147,7 +1151,7 @@ ssize_t bufgetdata(const int handle_id, size_t size, void **data)
1147 if (data) 1151 if (data)
1148 *data = &buffer[h->ridx]; 1152 *data = &buffer[h->ridx];
1149 1153
1150 return size; 1154 return adjusted_size;
1151} 1155}
1152 1156
1153ssize_t bufgettail(const int handle_id, const size_t size, void **data) 1157ssize_t bufgettail(const int handle_id, const size_t size, void **data)
@@ -1180,9 +1184,10 @@ ssize_t bufgettail(const int handle_id, const size_t size, void **data)
1180 return size; 1184 return size;
1181} 1185}
1182 1186
1183ssize_t bufcuttail(const int handle_id, size_t size) 1187ssize_t bufcuttail(const int handle_id, const size_t size)
1184{ 1188{
1185 struct memory_handle *h; 1189 struct memory_handle *h;
1190 size_t adjusted_size = size;
1186 1191
1187 h = find_handle(handle_id); 1192 h = find_handle(handle_id);
1188 1193
@@ -1192,16 +1197,16 @@ ssize_t bufcuttail(const int handle_id, size_t size)
1192 if (h->filerem) 1197 if (h->filerem)
1193 return ERR_HANDLE_NOT_DONE; 1198 return ERR_HANDLE_NOT_DONE;
1194 1199
1195 if (h->available < size) 1200 if (h->available < adjusted_size)
1196 size = h->available; 1201 adjusted_size = h->available;
1197 1202
1198 h->available -= size; 1203 h->available -= adjusted_size;
1199 h->filesize -= size; 1204 h->filesize -= adjusted_size;
1200 h->widx = RINGBUF_SUB(h->widx, size); 1205 h->widx = RINGBUF_SUB(h->widx, adjusted_size);
1201 if (h == cur_handle) 1206 if (h == cur_handle)
1202 buf_widx = h->widx; 1207 buf_widx = h->widx;
1203 1208
1204 return size; 1209 return adjusted_size;
1205} 1210}
1206 1211
1207 1212