Link para_server and para_afh against libid3tag if available.
[paraslash.git] / afh.c
diff --git a/afh.c b/afh.c
index 2ff2c7867e255f2c7b5b5c9cdc5ae7eb453bc59a..4c5ddae51499984c02da75ae2898517c5d6e24fe 100644 (file)
--- a/afh.c
+++ b/afh.c
@@ -60,22 +60,30 @@ static int cat_file(void *audio_file_data, struct afh_info *afhi)
        int ret;
        struct timeval stream_start;
        long unsigned i, first_chunk, last_chunk;
-       char *buf;
+       const char *buf;
        size_t size;
 
 
-       if (conf.begin_chunk_arg < 0)
-               return -ERRNO_TO_PARA_ERROR(EINVAL);
-       first_chunk = conf.begin_chunk_arg;
-
-       if (conf.end_chunk_given) {
-               if (conf.end_chunk_arg < 0)
+       if (conf.begin_chunk_arg < 0) {
+               if (-conf.begin_chunk_arg > afhi->chunks_total)
                        return -ERRNO_TO_PARA_ERROR(EINVAL);
-               if (conf.end_chunk_arg >= afhi->chunks_total)
-                       return -ERRNO_TO_PARA_ERROR(EINVAL);
-               last_chunk = conf.end_chunk_arg;
+               first_chunk = afhi->chunks_total + conf.begin_chunk_arg;
+       } else
+               first_chunk = conf.begin_chunk_arg;
+       if (conf.end_chunk_given) {
+               if (conf.end_chunk_arg < 0) {
+                       if (-conf.end_chunk_arg > afhi->chunks_total)
+                               return -ERRNO_TO_PARA_ERROR(EINVAL);
+                       last_chunk = afhi->chunks_total + conf.end_chunk_arg;
+               } else {
+                       if (conf.end_chunk_arg >= afhi->chunks_total)
+                               return -ERRNO_TO_PARA_ERROR(EINVAL);
+                       last_chunk = conf.end_chunk_arg;
+               }
        } else
                last_chunk = afhi->chunks_total - 1;
+       if (first_chunk >= last_chunk)
+               return -ERRNO_TO_PARA_ERROR(EINVAL);
        if (!afhi->chunks_total)
                return 1;
        afh_get_header(afhi, audio_file_data, &buf, &size);
@@ -105,11 +113,9 @@ static int cat_file(void *audio_file_data, struct afh_info *afhi)
                        }
                }
                PARA_INFO_LOG("writing chunk %lu\n", i);
-               ret = write(STDOUT_FILENO, buf, size);
+               ret = write_all(STDOUT_FILENO, buf, &size);
                if (ret < 0)
                        return ret;
-               if (ret != size)
-                       return -E_AFH_SHORT_WRITE;
        }
        return 1;
 }