about summary refs log tree commit diff
path: root/lib/libpm.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libpm.c')
-rw-r--r--lib/libpm.c55
1 files changed, 52 insertions, 3 deletions
diff --git a/lib/libpm.c b/lib/libpm.c
index 6f9dea3d..4dd19b70 100644
--- a/lib/libpm.c
+++ b/lib/libpm.c
@@ -442,12 +442,20 @@ pm_maxvaltobits(int const maxval) {
     assert(false);
 }
 
+
+
 int
-pm_bitstomaxval(int const bits) {
-    return ( 1 << bits ) - 1;
+pm_bitstomaxval(int const bitCt) {
+
+    if (bitCt > 16)
+        pm_error("Bits per sample %u too large.  16 is maximum acceptable",
+                 bitCt);
+
+    return (1 << bitCt) - 1;
 }
 
 
+
 unsigned int PURE_FN_ATTR
 pm_lcm(unsigned int const x,
        unsigned int const y,
@@ -698,7 +706,16 @@ pm_proginit(int *         const argcP,
    find.  We remove the common options from argv[], updating *argcP
    accordingly.
 
-   This includes calling pm_init() to initialize the Netpbm libraries.
+   This includes calling 'pm_init' to initialize the Netpbm libraries.
+
+   argv[] is intended to be the program's POSIX arguments (as passed to the
+   program's main function).  But note that POSIX arguments are modifiable and
+   as such, the conventional way to declare them in a program is with "char
+   **", as opposed to pm_proginit's "const char **".  To avoid a compiler
+   warning, the program has to use the unconventional "const char **" type in
+   the declaration of its main function.  (Alternatively, it can just cast it
+   to const char ** later).  'pm_proginit' does not modify any argument
+   strings.
 -----------------------------------------------------------------------------*/
     const char * const progname = pm_arg0toprogname(argv[0]);
         /* points to static memory in this library */
@@ -844,6 +861,14 @@ pm_parse_width(const char * const arg) {
    Return the image width represented by the decimal ASCIIZ string
    'arg'.  Fail if it doesn't validly represent a width or represents
    a width that can't be conveniently used in computation.
+
+   See comments at 'validateComputableSize' in libpam.c for details on
+   the purpose of these validations.
+
+   This isn't just for Netpbm format images.
+
+   Typical places from which widths come: headers of non-Netpbm images;
+   command line options.
 -----------------------------------------------------------------------------*/
     unsigned int width;
     const char * error;
@@ -890,3 +915,27 @@ pm_parse_height(const char * const arg) {
 
 
 
+unsigned int
+pm_parse_maxval(const char * const arg) {
+/*----------------------------------------------------------------------------
+  Same as pm_parse_width(), but for maxval.
+-----------------------------------------------------------------------------*/
+    unsigned int maxval;
+    const char * error;
+
+    pm_string_to_uint(arg, &maxval, &error);
+
+    if (error) {
+        pm_error("'%s' is invalid as a maxval.  %s", arg, error);
+        pm_strfree(error);
+    } else {
+        if (maxval > INT_MAX-1)
+            pm_error("Maxval %u is too large for computations.", maxval);
+        if (maxval == 0)
+            pm_error("Maxval argument must be a positive number.  You "
+                     "specified 0.");
+    }
+    return maxval;
+}
+
+