/*
:::::::::::::::::::::::::::::::::::::::::::::::::::
::
::    GIFReadExtension ( )
::
344046
public function readDescriptor()
    {
        $this->getByte(9);
        $GIF_screen = $this->GIF_buffer;
        $GIF_colorF = $this->GIF_buffer[8] & 0x80 ? 1 : 0;
        if ($GIF_colorF) {
            $GIF_code = $this->GIF_buffer[8] & 0x07;
            $GIF_sort = $this->GIF_buffer[8] & 0x20 ? 1 : 0;
        } else {
            $GIF_code = $this->GIF_colorC;
            $GIF_sort = $this->GIF_sorted;
        }
        $GIF_size = 2 << $GIF_code;
        $this->GIF_screen[4] &= 0x70;
        $this->GIF_screen[4] |= 0x80;
        $this->GIF_screen[4] |= $GIF_code;
        if ($GIF_sort) {
            $this->GIF_screen[4] |= 0x08;
        }
        $this->GIF_string = "GIF87a";
        $this->putByte($this->GIF_screen);
        if (1 == $GIF_colorF) {
            $this->getByte(3 * $GIF_size);
            $this->putByte($this->GIF_buffer);
        } else {
            $this->putByte($this->GIF_global);
        }
        $this->GIF_string .= chr(0x2C);
        $GIF_screen[8] &= 0x40;
        $this->putByte($GIF_screen);
        $this->getByte(1);
        $this->putByte($this->GIF_buffer);
        for (; ;) {
            $this->getByte(1);
            $this->putByte($this->GIF_buffer);
            if (($u = $this->GIF_buffer[0]) == 0x00) {
                break;
            }
            $this->getByte($u);
            $this->putByte($this->GIF_buffer);
        }
        $this->GIF_string .= chr(0x3B);
        /*
        Add frames into $GIF_stream array...
         */
        $this->GIF_arrays[] = $this->GIF_string;
    }
221900
function _gpfHttpGenSetHeaders (methodName) {
    return function (httpObj, headers) {
        if (headers) {
            Object.keys(headers).forEach(function (headerName) {
                httpObj[methodName](headerName, headers[headerName]);
            });
        }
    };
}
36184
function start_gifmerge_process($fp)
	{
		if($this->mod == 'C_FILE')
		{
			if(!$this->fin = fopen($fp, 'rb'))
			{
				if($this->debug) print "Error file open: $fp\<br>";
				return;
			}
		}
		else if($this->mod == 'C_MEMORY')
		{
			$this->ch  = 0;
			$this->fin = $fp;
		}
		$this->getbytes (6);
		if(!$this->arrcmp($this->buffer, $this->gif, 3))
		{
			if($this->debug) print "Isn't GIF file!\n<br>";
			return;
		}
		$this->getbytes (7);
		if($this->first) $this->logical_screen_descriptor = $this->buffer;
		$this->global_color_table_sizecode = $this->buffer[4] & 0x07;
		$this->global_color_table_size = 2 << $this->global_color_table_sizecode;
		if($this->buffer[4] & 0x80)
		{
			$this->getbytes((3 * $this->global_color_table_size));
			for($i = 0; $i < ((3 * $this->global_color_table_size)); $i++)
			{
				$this->global_in[$i] = $this->buffer[$i];
			}
			if($this->out_color_table_size == 0)
			{
				$this->out_color_table_size = $this->global_color_table_size;
				$out_color_table_sizecode = $this->global_color_table_sizecode;
				$this->global_out = $this->global_in;
			}
			if($this->global_color_table_size != $this->out_color_table_size || $this->arrcmp($this->global_out, $this->global_in, (3 * $this->global_color_table_size)))
			{
				$this->use_global_in = true;
			}
		}
		for($loop = true; $loop;)
		{
			$this->getbytes(1);
			switch ($this->buffer[0])
			{
				case 0x21:
					$this->read_extension();
					break;
				case 0x2c:
					$this->read_image_descriptor();
					break;
				case 0x3b:
					$loop = false;
				break;
				default:
            		if($this->debug) printf("Unrecognized byte code 0x%x, truncating file!\n<br>", $this->buffer[0]);
						$loop = false;
			}
		}
		if($this->mod == 'C_FILE')
			fclose($this->fin);
	}
734431
function () {
            if (this._content) {
                var error = new gpf.Error.InvalidCSV();
                this._setReadError(error);
                return Promise.reject(error);
            }
            this._completeReadBuffer();
            return Promise.resolve();
        }
209010
static function snippet($text, $keywords, $limit, $max_length, $around = array('<b>', '</b>'))
	{
		$result = array();

		$keywords = self::normalize($keywords);
		list($pre, $post) = $around;
		$prev = array();
		$after = false;

		foreach(self::normalize_hash($text) as $word_orig => $word_norm)
		{
			$is_kw = false;
			if(in_array($word_norm, $keywords))
			{
				$prev[] = $pre.$word_orig.$post;
				$is_kw = true;
			}
			else
				$prev[] = $word_orig;

			if($is_kw)
			{
				if($after)
				{
					$result = array_merge($result, $prev);
					$prev = array();
				}
				else
				{
					$result = array_merge($result, count($prev) > $limit ? array('&#133') : array(), array_slice($prev, -$limit-1));
					$prev = array();
				}

				$after = true;
			}
			else
			{
				if($after && count($prev) >= $limit)
				{
					$after = false;
					$result = array_merge($result, $prev);
					$prev = array();
				}
			}
		}

		if($after)
			$result = array_merge($result, array('&#133'), $prev);

		return strip_text(join(' ', $result).'&#133;', $max_length);
	}
1147725
function _gpfArrayFromString(array, string) {
        var length = string.length, index = 0;
        for (; index < length; ++index) {
            array.push(string.charAt(index));
        }
    }
638076
function (o, cb) {
    try {
      var reader = new FileReader();
      reader.onload = function (event) {
        cb && cb(reader.result);
      };
      reader.readAsText(o);
    } catch (error) {
      throw error
    }
  }
1232758
private void readContents(int maxFrames) {
        // Read GIF file content blocks.
        boolean done = false;
        while (!(done || err() || header.frameCount > maxFrames)) {
            int code = read();
            switch (code) {
                // Image separator.
                case 0x2C:
                    // The graphics control extension is optional, but will always come first if it exists.
                    // If one did
                    // exist, there will be a non-null current frame which we should use. However if one
                    // did not exist,
                    // the current frame will be null and we must create it here. See issue #134.
                    if (header.currentFrame == null) {
                        header.currentFrame = new GifFrame();
                    }
                    readBitmap();
                    break;
                // Extension.
                case 0x21:
                    code = read();
                    switch (code) {
                        // Graphics control extension.
                        case 0xf9:
                            // Start a new frame.
                            header.currentFrame = new GifFrame();
                            readGraphicControlExt();
                            break;
                        // Application extension.
                        case 0xff:
                            readBlock();
                            String app = "";
                            for (int i = 0; i < 11; i++) {
                                app += (char) block[i];
                            }
                            if (app.equals("NETSCAPE2.0")) {
                                readNetscapeExt();
                            } else {
                                // Don't care.
                                skip();
                            }
                            break;
                        // Comment extension.
                        case 0xfe:
                            skip();
                            break;
                        // Plain text extension.
                        case 0x01:
                            skip();
                            break;
                        // Uninteresting extension.
                        default:
                            skip();
                    }
                    break;
                // Terminator.
                case 0x3b:
                    done = true;
                    break;
                // Bad byte, but keep going and see what happens break;
                case 0x00:
                default:
                    header.status = GifDecoder.STATUS_FORMAT_ERROR;
            }
        }
    }
388118
function removeExtension(file) {
  let parts = path.parse(file);
  delete parts.ext;
  delete parts.base;
  return path.format(parts);
}
563038
function validateExtensions(input) {
  return new Promise((resolve, reject) => {
    try {
      const extensionPath = getExtensionsPath();

      if (!fs.existsSync(extensionPath)) {
        fs.mkdirSync(extensionPath);
      }

      if (!input.imports.length) {
        return resolve(null);
      }

      return resolve(input);
    } catch (e) {
      return reject(new Error(t('EXTENSION_COULD_NOT_BE_VALIDATED')));
    }
  });
}
1006594
function appendExtension (obj, extension) {
  if (!obj.extensions) {
    obj.extensions = []
  }

  if (obj.extensions.indexOf(extension) === -1) {
    obj.extensions.push(extension)
  }
}
1325709
function _gpfReadWScript (path) {
    return new Promise(function (resolve) {
        var file = _gpfMsFSO.OpenTextFile(path, _GPF_FS_WSCRIPT_READING, false);
        resolve(file.ReadAll());
        file.Close();
    });
}
780327
function(inText) {
			return inText != null ? String(inText).replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;') : '';
		}
5637
private void parseExtension(Extension ext) throws IOException {
        try {
            Class<?> extClass = OIDMap.getClass(ext.getExtensionId());
            if (extClass == null) {   // Unsupported extension
                if (ext.isCritical()) {
                    unsupportedCritExt = true;
                }
                if (map.put(ext.getExtensionId().toString(), ext) == null) {
                    return;
                } else {
                    throw new IOException("Duplicate extensions not allowed");
                }
            }
            Constructor<?> cons = extClass.getConstructor(PARAMS);

            Object[] passed = new Object[] {Boolean.valueOf(ext.isCritical()),
                    ext.getExtensionValue()};
                    CertAttrSet<?> certExt = (CertAttrSet<?>)
                            cons.newInstance(passed);
                    if (map.put(certExt.getName(), (Extension)certExt) != null) {
                        throw new IOException("Duplicate extensions not allowed");
                    }
        } catch (InvocationTargetException invk) {
            Throwable e = invk.getTargetException();
            if (ext.isCritical() == false) {
                // ignore errors parsing non-critical extensions
                if (unparseableExtensions == null) {
                    unparseableExtensions = new TreeMap<String,Extension>();
                }
                unparseableExtensions.put(ext.getExtensionId().toString(),
                        new UnparseableExtension(ext, e));
                if (debug != null) {
                    debug.println("Error parsing extension: " + ext);
                    e.printStackTrace();
                    HexDumpEncoder h = new HexDumpEncoder();
                    System.err.println(h.encodeBuffer(ext.getExtensionValue()));
                }
                return;
            }
            if (e instanceof IOException) {
                throw (IOException)e;
            } else {
                throw new IOException(e);
            }
        } catch (IOException e) {
            throw e;
        } catch (Exception e) {
            throw new IOException(e);
        }
    }
360580
private final void readGlyf(FontFileReader in) throws IOException {
        TTFDirTabEntry dirTab = (TTFDirTabEntry)dirTabs.get("glyf");
        if (dirTab == null) {
            throw new IOException("glyf table not found, cannot continue");
        }
        for (int i = 0; i < (numberOfGlyphs - 1); i++) {
            if (mtxTab[i].getOffset() != mtxTab[i + 1].getOffset()) {
                in.seekSet(dirTab.getOffset() + mtxTab[i].getOffset());
                in.skip(2);
                final int[] bbox = {
                    in.readTTFShort(),
                    in.readTTFShort(),
                    in.readTTFShort(),
                    in.readTTFShort()};
                mtxTab[i].setBoundingBox(bbox);
            } else {
                mtxTab[i].setBoundingBox(mtxTab[0].getBoundingBox());
            }
        }


        long n = ((TTFDirTabEntry)dirTabs.get("glyf")).getOffset();
        for (int i = 0; i < numberOfGlyphs; i++) {
            if ((i + 1) >= mtxTab.length
                    || mtxTab[i].getOffset() != mtxTab[i + 1].getOffset()) {
                in.seekSet(n + mtxTab[i].getOffset());
                in.skip(2);
                final int[] bbox = {
                    in.readTTFShort(),
                    in.readTTFShort(),
                    in.readTTFShort(),
                    in.readTTFShort()};
                mtxTab[i].setBoundingBox(bbox);
            } else {
                /**@todo Verify that this is correct, looks like a copy/paste bug (jm)*/
                final int bbox0 = mtxTab[0].getBoundingBox()[0];
                final int[] bbox = {bbox0, bbox0, bbox0, bbox0};
                mtxTab[i].setBoundingBox(bbox);
                /* Original code
                mtxTab[i].bbox[0] = mtxTab[0].bbox[0];
                mtxTab[i].bbox[1] = mtxTab[0].bbox[0];
                mtxTab[i].bbox[2] = mtxTab[0].bbox[0];
                mtxTab[i].bbox[3] = mtxTab[0].bbox[0]; */
            }
            if (log.isTraceEnabled()) {
                log.trace(mtxTab[i].toString(this));
            }
        }
    }
451956
function (data, regex, {
	multiple = false
} = {}) {
	let result = data
	let extracted
	if (regex) {
		try {
			let rgx = regex
			if (_.isString(regex)) {
				rgx = new RegExp(regex, 'gim')
			}
			extracted = rgx.exec(data)
			if (multiple) {
				result = extracted
				// result = data.match(rgx)
			} else {
				if (extracted[1]) {
					result = extracted[1]
				} else {
					result = extracted[0]
				}
				// result = data.match(rgx)[0]
			}
		} catch (error) {
			// console.log("Regex error: ", error)
		}
	}
	return result
}
809094
function readExtension(file, cb) {
    fs.readFile(metapath(file), 'utf-8', function(err, data) {
        if (err) {
            if (err.code === 'ENOENT') return cb(new Error('Metadata file does not exist.'));
            return cb(err);
        }
        try {
            var ext = guessExtension(JSON.parse(data));
            if (ext) {
                if (env == 'development') console.error("[millstone] detected extension of '" + ext + "' for '" + file + "'");
            }
            return cb(null, ext);
        } catch (e) {
            return cb(e);
        }
    });
}